lua2cla&cla命令

This commit is contained in:
2026-02-27 21:39:22 +08:00
parent c83988458a
commit fefb7e737a
8 changed files with 262 additions and 5 deletions

View File

@@ -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<string> showError, Action<string> showWarning)
{
if (string.IsNullOrEmpty(args))
{
showError(UsageGenerator.GenerateSimpleUsage("cla", "<cla_file>"));
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);
}
}
}

View File

@@ -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<string> showError, Action<string> showSuccess)
{
if (string.IsNullOrEmpty(args))
{
showError(UsageGenerator.GenerateSimpleUsage("lua2cla", "<lua_file>"));
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;
}
}
}

View File

@@ -116,6 +116,60 @@ namespace CMLeonOS.Commands.Script
}
}
public static void ExecuteLuaCode(string code, CMLeonOS.FileSystem fileSystem, Shell shell, Action<string> showError, Action<string> 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<string> showError)
{
Console.WriteLine("====================================");