From fefb7e737a79520ff6dfb53c54908bd6d9ee8fce Mon Sep 17 00:00:00 2001 From: Leonmmcoset Date: Fri, 27 Feb 2026 21:39:22 +0800 Subject: [PATCH] =?UTF-8?q?lua2cla&cla=E5=91=BD=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BuildTime.txt | 2 +- GitCommit.txt | 2 +- docs/cmleonos/docs/commands.md | 37 ++++++++++- shell/CommandList.cs | 6 ++ shell/Commands/Script/ClaCommand.cs | 82 +++++++++++++++++++++++++ shell/Commands/Script/Lua2ClaCommand.cs | 70 +++++++++++++++++++++ shell/Commands/Script/LuaCommand.cs | 54 ++++++++++++++++ shell/Shell.cs | 14 ++++- 8 files changed, 262 insertions(+), 5 deletions(-) create mode 100644 shell/Commands/Script/ClaCommand.cs create mode 100644 shell/Commands/Script/Lua2ClaCommand.cs diff --git a/BuildTime.txt b/BuildTime.txt index e8fa45f..81e58a8 100644 --- a/BuildTime.txt +++ b/BuildTime.txt @@ -1 +1 @@ -2026-02-26 20:25:24 \ No newline at end of file +2026-02-27 21:34:34 \ No newline at end of file diff --git a/GitCommit.txt b/GitCommit.txt index 3c0f3bf..53c7024 100644 --- a/GitCommit.txt +++ b/GitCommit.txt @@ -1 +1 @@ -c1e0651 \ No newline at end of file +c839884 \ No newline at end of file diff --git a/docs/cmleonos/docs/commands.md b/docs/cmleonos/docs/commands.md index cf0bc56..1c06c71 100644 --- a/docs/cmleonos/docs/commands.md +++ b/docs/cmleonos/docs/commands.md @@ -498,6 +498,41 @@ lua lua script.lua ``` +### lua2cla +将 Lua 文件转换为 CMLeonOS Lua 应用格式(.cla)。 + +**用法:** +```bash +lua2cla +``` + +**示例:** +```bash +lua2cla app.lua +``` + +**说明:** +- 文件头为 `CMLeonOS_CLA` +- 转换后的文件可以通过 `cla` 命令运行 + +### cla +运行 CMLeonOS Lua 应用格式(.cla)文件。 + +**用法:** +```bash +cla +``` + +**示例:** +```bash +cla app.cla +``` + +**说明:** +- 运行 .cla 格式的加密 Lua 应用 +- 自动解密并执行 Lua 代码 +- 文件头必须为 `CMLeonOS_CLA` + ### com 执行命令脚本文件。 @@ -636,7 +671,7 @@ app list # 列出所有可用应用 app install # 安装应用 app uninstall # 卸载应用 app installed # 列出已安装应用 -app help # 显示帮助信息 +app help # 显示帮助信息 ``` **示例:** diff --git a/shell/CommandList.cs b/shell/CommandList.cs index 3e6878d..127d1d9 100644 --- a/shell/CommandList.cs +++ b/shell/CommandList.cs @@ -190,6 +190,12 @@ namespace CMLeonOS.shell case "lua": shell.ExecuteLuaScript(args); break; + case "lua2cla": + shell.ConvertLuaToCla(args); + break; + case "cla": + shell.RunClaFile(args); + break; case "testgui": shell.ProcessTestGui(); break; diff --git a/shell/Commands/Script/ClaCommand.cs b/shell/Commands/Script/ClaCommand.cs new file mode 100644 index 0000000..c6bbac5 --- /dev/null +++ b/shell/Commands/Script/ClaCommand.cs @@ -0,0 +1,82 @@ +using System; +using System.IO; +using System.Text; +using UniLua; + +namespace CMLeonOS.Commands.Script +{ + public static class ClaCommand + { + private const string FILE_HEADER = "CMLeonOS_CLA"; + private static readonly byte[] ENCRYPTION_KEY = { 0x55, 0xAA, 0x33, 0xCC, 0x77, 0x88, 0x11, 0x22, 0x44, 0x66, 0x99, 0xBB }; + + public static void RunClaFile(string args, CMLeonOS.FileSystem fileSystem, Shell shell, Action showError, Action showWarning) + { + if (string.IsNullOrEmpty(args)) + { + showError(UsageGenerator.GenerateSimpleUsage("cla", "")); + showError("Example: cla app.cla"); + return; + } + + string claFilePath = fileSystem.GetFullPath(args); + + if (!File.Exists(claFilePath)) + { + showError($"Error: CLA file not found: {args}"); + return; + } + + if (!claFilePath.EndsWith(".cla", StringComparison.OrdinalIgnoreCase)) + { + showError("Error: Input file must have .cla extension"); + return; + } + + try + { + string luaContent = DecryptClaFile(claFilePath); + + if (string.IsNullOrEmpty(luaContent)) + { + showError("Error: Failed to decrypt CLA file or file is corrupted"); + return; + } + + LuaCommand.ExecuteLuaCode(luaContent, fileSystem, shell, showError, showWarning); + } + catch (Exception ex) + { + showError($"Error running CLA file: {ex.Message}"); + } + } + + private static string DecryptClaFile(string claFilePath) + { + using (BinaryReader reader = new BinaryReader(File.OpenRead(claFilePath))) + { + byte[] headerBytes = reader.ReadBytes(FILE_HEADER.Length); + string header = Encoding.ASCII.GetString(headerBytes); + + if (header != FILE_HEADER) + { + throw new Exception("Invalid CLA file format: Missing or incorrect header"); + } + + byte[] encryptedData = reader.ReadBytes((int)(reader.BaseStream.Length - reader.BaseStream.Position)); + return DecryptData(encryptedData); + } + } + + private static string DecryptData(byte[] data) + { + for (int i = 0; i < data.Length; i++) + { + int keyIndex = i % ENCRYPTION_KEY.Length; + data[i] = (byte)(data[i] ^ ENCRYPTION_KEY[keyIndex]); + } + + return Encoding.UTF8.GetString(data); + } + } +} diff --git a/shell/Commands/Script/Lua2ClaCommand.cs b/shell/Commands/Script/Lua2ClaCommand.cs new file mode 100644 index 0000000..bff046b --- /dev/null +++ b/shell/Commands/Script/Lua2ClaCommand.cs @@ -0,0 +1,70 @@ +using System; +using System.IO; +using System.Text; + +namespace CMLeonOS.Commands.Script +{ + public static class Lua2ClaCommand + { + private const string FILE_HEADER = "CMLeonOS_CLA"; + private static readonly byte[] ENCRYPTION_KEY = { 0x55, 0xAA, 0x33, 0xCC, 0x77, 0x88, 0x11, 0x22, 0x44, 0x66, 0x99, 0xBB }; + + public static void ConvertLuaToCla(string args, CMLeonOS.FileSystem fileSystem, Action showError, Action showSuccess) + { + if (string.IsNullOrEmpty(args)) + { + showError(UsageGenerator.GenerateSimpleUsage("lua2cla", "")); + showError("Example: lua2cla app.lua"); + return; + } + + string luaFilePath = fileSystem.GetFullPath(args); + + if (!File.Exists(luaFilePath)) + { + showError($"Error: Lua file not found: {args}"); + return; + } + + if (!luaFilePath.EndsWith(".lua", StringComparison.OrdinalIgnoreCase)) + { + showError("Error: Input file must have .lua extension"); + return; + } + + try + { + string luaContent = File.ReadAllText(luaFilePath); + byte[] encryptedData = EncryptData(luaContent); + + string claFilePath = luaFilePath.Substring(0, luaFilePath.Length - 4) + ".cla"; + + using (BinaryWriter writer = new BinaryWriter(File.Create(claFilePath))) + { + writer.Write(Encoding.ASCII.GetBytes(FILE_HEADER)); + writer.Write(encryptedData); + } + + showSuccess($"Successfully converted: {args} -> {Path.GetFileName(claFilePath)}"); + Console.WriteLine($"Output file: {claFilePath}"); + } + catch (Exception ex) + { + showError($"Error converting file: {ex.Message}"); + } + } + + private static byte[] EncryptData(string content) + { + byte[] data = Encoding.UTF8.GetBytes(content); + + for (int i = 0; i < data.Length; i++) + { + int keyIndex = i % ENCRYPTION_KEY.Length; + data[i] = (byte)(data[i] ^ ENCRYPTION_KEY[keyIndex]); + } + + return data; + } + } +} diff --git a/shell/Commands/Script/LuaCommand.cs b/shell/Commands/Script/LuaCommand.cs index 50c5cf8..ebb03b1 100644 --- a/shell/Commands/Script/LuaCommand.cs +++ b/shell/Commands/Script/LuaCommand.cs @@ -116,6 +116,60 @@ namespace CMLeonOS.Commands.Script } } + public static void ExecuteLuaCode(string code, CMLeonOS.FileSystem fileSystem, Shell shell, Action showError, Action showWarning) + { + if (string.IsNullOrWhiteSpace(code)) + { + showWarning("Lua code is empty"); + return; + } + + try + { + ILuaState lua = LuaAPI.NewState(); + lua.L_OpenLibs(); + + UniLua.ThreadStatus loadResult = lua.L_LoadString(code); + + if (loadResult == UniLua.ThreadStatus.LUA_OK) + { + UniLua.ThreadStatus callResult = lua.PCall(0, 0, 0); + + if (callResult == UniLua.ThreadStatus.LUA_OK) + { + } + else + { + string errorMsg = lua.ToString(-1); + if (string.IsNullOrWhiteSpace(errorMsg)) + { + showError($"Script execution error: Unknown error"); + } + else + { + showError($"Script execution error: {errorMsg}"); + } + } + } + else + { + string errorMsg = lua.ToString(-1); + if (string.IsNullOrWhiteSpace(errorMsg)) + { + showError($"Script load error: Unknown error"); + } + else + { + showError($"Script load error: {errorMsg}"); + } + } + } + catch (Exception ex) + { + showError($"Lua execution error: {ex.Message}"); + } + } + private static void EnterLuaShell(Action showError) { Console.WriteLine("===================================="); diff --git a/shell/Shell.cs b/shell/Shell.cs index f017b73..23a4bf7 100644 --- a/shell/Shell.cs +++ b/shell/Shell.cs @@ -407,8 +407,8 @@ namespace CMLeonOS "rmdir", "cat", "version", "about", "head", "tail", "wc", "cp", "mv", "rename", "touch", "find", "tree", "grep", "getdisk", "user", "cpass", "hostname", "ipconfig", "setdns", "setgateway", "nslookup", - "ping", "wget", "ftp", "tcpserver", "tcpclient", "lua", "branswe", - "beep", "env", "whoami", "uptime", "alias", + "ping", "wget", "ftp", "tcpserver", "tcpclient", "lua", "lua2cla", "cla", + "branswe", "beep", "env", "whoami", "uptime", "alias", "unalias", "base64", "testgui" }; } @@ -1502,6 +1502,16 @@ namespace CMLeonOS Commands.Script.LuaCommand.ExecuteLuaScript(args, fileSystem, this, ShowError, ShowWarning); } + public void ConvertLuaToCla(string args) + { + Commands.Script.Lua2ClaCommand.ConvertLuaToCla(args, fileSystem, ShowError, ShowSuccess); + } + + public void RunClaFile(string args) + { + Commands.Script.ClaCommand.RunClaFile(args, fileSystem, this, ShowError, ShowWarning); + } + public void ProcessTestGui() { Commands.TestGuiCommand.RunTestGui();