From 5a07c7a133dc5a2ec9b79887b1c8b2a121acd3ba Mon Sep 17 00:00:00 2001 From: Leonmmcoset Date: Wed, 4 Feb 2026 01:55:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=9B=B4=E5=A4=9ALua?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Shell.cs | 115 ++++++++++++++++++++++++++++++++++++++++++--- UniLua/LuaOsLib.cs | 16 +++++++ UserSystem.cs | 12 +++++ 3 files changed, 136 insertions(+), 7 deletions(-) diff --git a/Shell.cs b/Shell.cs index 434e5ed..bf0063f 100644 --- a/Shell.cs +++ b/Shell.cs @@ -2491,8 +2491,14 @@ namespace CMLeonOS if (parts.Length == 0) { - ShowError("Error: Please specify Lua script file"); - ShowError("Usage: lua "); + ShowError("Error: Please specify Lua script file or use --shell for interactive mode"); + ShowError("Usage: lua or lua --shell"); + return; + } + + if (parts.Length == 1 && parts[0] == "--shell") + { + EnterLuaShell(); return; } @@ -2517,9 +2523,6 @@ namespace CMLeonOS return; } - // Console.WriteLine($"Executing: {filePath}"); - // Console.WriteLine(); - try { string scriptContent = File.ReadAllText(filePath); @@ -2541,18 +2544,33 @@ namespace CMLeonOS if (callResult == UniLua.ThreadStatus.LUA_OK) { + // 不要动这里 // ShowSuccess("Script run successfully"); } else { string errorMsg = lua.ToString(-1); - ShowError($"Script execution error: {errorMsg}"); + if (string.IsNullOrWhiteSpace(errorMsg)) + { + ShowError($"Script execution error: Unknown error"); + } + else + { + ShowError($"Script execution error: {errorMsg}"); + } } } else { string errorMsg = lua.ToString(-1); - ShowError($"Script load error: {errorMsg}"); + if (string.IsNullOrWhiteSpace(errorMsg)) + { + ShowError($"Script load error: Unknown error"); + } + else + { + ShowError($"Script load error: {errorMsg}"); + } } } catch (Exception ex) @@ -2560,5 +2578,88 @@ namespace CMLeonOS ShowError($"Lua execution error: {ex.Message}"); } } + + private void EnterLuaShell() + { + Console.WriteLine("===================================="); + Console.WriteLine(" Lua Interactive Shell"); + Console.WriteLine("===================================="); + Console.WriteLine("Type 'exit' or 'quit' to exit"); + Console.WriteLine(); + + ILuaState lua = LuaAPI.NewState(); + lua.L_OpenLibs(); + + while (true) + { + Console.Write("lua> "); + string input = Console.ReadLine(); + + if (string.IsNullOrWhiteSpace(input)) + { + continue; + } + + if (input.ToLower() == "exit" || input.ToLower() == "quit") + { + Console.WriteLine("Exiting Lua shell..."); + break; + } + + try + { + UniLua.ThreadStatus loadResult = lua.L_LoadString(input); + + if (loadResult == UniLua.ThreadStatus.LUA_OK) + { + UniLua.ThreadStatus callResult = lua.PCall(0, 0, 0); + + if (callResult == UniLua.ThreadStatus.LUA_OK) + { + int top = lua.GetTop(); + if (top > 0) + { + for (int i = 1; i <= top; i++) + { + string result = lua.ToString(i); + if (!string.IsNullOrWhiteSpace(result)) + { + Console.WriteLine(result); + } + } + } + } + else + { + string errorMsg = lua.ToString(-1); + if (string.IsNullOrWhiteSpace(errorMsg)) + { + ShowError($"Execution error: Unknown error"); + } + else + { + ShowError($"Execution error: {errorMsg}"); + } + } + } + else + { + string errorMsg = lua.ToString(-1); + if (string.IsNullOrWhiteSpace(errorMsg)) + { + ShowError($"Load error: Unknown error"); + } + else + { + ShowError($"Load error: {errorMsg}"); + } + } + } + catch (Exception ex) + { + ShowError($"Lua error: {ex.Message}"); + } + } + } } } \ No newline at end of file diff --git a/UniLua/LuaOsLib.cs b/UniLua/LuaOsLib.cs index 3de6884..81383c0 100644 --- a/UniLua/LuaOsLib.cs +++ b/UniLua/LuaOsLib.cs @@ -29,6 +29,8 @@ namespace UniLua new NameFuncPair("sleep", OS_Sleep), new NameFuncPair("beep", OS_Beep), new NameFuncPair("clear", OS_Clear), + new NameFuncPair("getusername", OS_Getusername), + new NameFuncPair("isadmin", OS_Isadmin), #endif }; @@ -161,6 +163,20 @@ namespace UniLua lua.PushBoolean(true); return 1; } + + private static int OS_Getusername( ILuaState lua ) + { + string username = CMLeonOS.Kernel.userSystem?.CurrentUsername ?? "Not logged in"; + lua.PushString(username); + return 1; + } + + private static int OS_Isadmin( ILuaState lua ) + { + bool isAdmin = CMLeonOS.Kernel.userSystem?.CurrentUserIsAdmin ?? false; + lua.PushBoolean(isAdmin); + return 1; + } #endif } } diff --git a/UserSystem.cs b/UserSystem.cs index b67e92d..bcbeb87 100644 --- a/UserSystem.cs +++ b/UserSystem.cs @@ -253,6 +253,18 @@ namespace CMLeonOS } } + public bool CurrentUserIsAdmin + { + get + { + if (currentLoggedInUser != null) + { + return currentLoggedInUser.IsAdmin; + } + return false; + } + } + public void FirstTimeSetup() { Console.WriteLine("====================================");