Lua 5.2支持

This commit is contained in:
2026-02-03 02:44:58 +08:00
parent c64330b22f
commit 3c21a1ce3a
33 changed files with 14339 additions and 0 deletions

View File

@@ -13,6 +13,7 @@ using System.Text;
using CosmosHttp.Client;
using Cosmos.Core;
using Cosmos.Core.Memory;
using UniLua;
using Cosmos.HAL;
namespace CMLeonOS
@@ -199,6 +200,7 @@ namespace CMLeonOS
" whoami - Show current username",
" base64 encrypt <text> - Encode text to Base64",
" base64 decrypt <text> - Decode Base64 to text",
" lua <file> - Execute Lua script",
" version - Show OS version",
" about - Show about information",
" help <page> - Show help page (1-3)",
@@ -465,6 +467,9 @@ namespace CMLeonOS
case "base64":
ProcessBase64Command(args);
break;
case "lua":
ExecuteLuaScript(args);
break;
default:
ShowError($"Unknown command: {command}");
break;
@@ -2083,5 +2088,81 @@ namespace CMLeonOS
ShowError($"Base64 error: {ex.Message}");
}
}
private void ExecuteLuaScript(string args)
{
string[] parts = args.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length == 0)
{
ShowError("Error: Please specify Lua script file");
ShowError("Usage: lua <file>");
return;
}
string filePath = parts[0];
string originalPath = filePath;
if (!filePath.StartsWith("0:\\") && !filePath.StartsWith("0:/"))
{
if (prompt == "/" || prompt == "\\")
{
filePath = "0:\\" + filePath.TrimStart('/').TrimStart('\\');
}
else
{
filePath = Path.Combine(prompt, filePath);
}
}
if (!File.Exists(filePath))
{
ShowError($"Error: File not found: {filePath}");
return;
}
// Console.WriteLine($"Executing: {filePath}");
// Console.WriteLine();
try
{
string scriptContent = File.ReadAllText(filePath);
if (string.IsNullOrWhiteSpace(scriptContent))
{
ShowWarning("Script file is empty");
return;
}
ILuaState lua = LuaAPI.NewState();
lua.L_OpenLibs();
UniLua.ThreadStatus loadResult = lua.L_LoadString(scriptContent);
if (loadResult == UniLua.ThreadStatus.LUA_OK)
{
UniLua.ThreadStatus callResult = lua.PCall(0, 0, 0);
if (callResult == UniLua.ThreadStatus.LUA_OK)
{
// ShowSuccess("Script run successfully");
}
else
{
string errorMsg = lua.ToString(-1);
ShowError($"Script execution error: {errorMsg}");
}
}
else
{
string errorMsg = lua.ToString(-1);
ShowError($"Script load error: {errorMsg}");
}
}
catch (Exception ex)
{
ShowError($"Lua execution error: {ex.Message}");
}
}
}
}