增加更多Lua功能

This commit is contained in:
2026-02-04 01:55:14 +08:00
parent 79853052ae
commit 5a07c7a133
3 changed files with 136 additions and 7 deletions

111
Shell.cs
View File

@@ -2491,8 +2491,14 @@ namespace CMLeonOS
if (parts.Length == 0) if (parts.Length == 0)
{ {
ShowError("Error: Please specify Lua script file"); ShowError("Error: Please specify Lua script file or use --shell for interactive mode");
ShowError("Usage: lua <file>"); ShowError("Usage: lua <file> or lua --shell");
return;
}
if (parts.Length == 1 && parts[0] == "--shell")
{
EnterLuaShell();
return; return;
} }
@@ -2517,9 +2523,6 @@ namespace CMLeonOS
return; return;
} }
// Console.WriteLine($"Executing: {filePath}");
// Console.WriteLine();
try try
{ {
string scriptContent = File.ReadAllText(filePath); string scriptContent = File.ReadAllText(filePath);
@@ -2541,24 +2544,122 @@ namespace CMLeonOS
if (callResult == UniLua.ThreadStatus.LUA_OK) if (callResult == UniLua.ThreadStatus.LUA_OK)
{ {
// 不要动这里
// ShowSuccess("Script run successfully"); // ShowSuccess("Script run successfully");
} }
else else
{ {
string errorMsg = lua.ToString(-1); string errorMsg = lua.ToString(-1);
if (string.IsNullOrWhiteSpace(errorMsg))
{
ShowError($"Script execution error: Unknown error");
}
else
{
ShowError($"Script execution error: {errorMsg}"); ShowError($"Script execution error: {errorMsg}");
} }
} }
}
else else
{ {
string errorMsg = lua.ToString(-1); string errorMsg = lua.ToString(-1);
if (string.IsNullOrWhiteSpace(errorMsg))
{
ShowError($"Script load error: Unknown error");
}
else
{
ShowError($"Script load error: {errorMsg}"); ShowError($"Script load error: {errorMsg}");
} }
} }
}
catch (Exception ex) catch (Exception ex)
{ {
ShowError($"Lua execution error: {ex.Message}"); 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}");
}
}
}
} }
} }

View File

@@ -29,6 +29,8 @@ namespace UniLua
new NameFuncPair("sleep", OS_Sleep), new NameFuncPair("sleep", OS_Sleep),
new NameFuncPair("beep", OS_Beep), new NameFuncPair("beep", OS_Beep),
new NameFuncPair("clear", OS_Clear), new NameFuncPair("clear", OS_Clear),
new NameFuncPair("getusername", OS_Getusername),
new NameFuncPair("isadmin", OS_Isadmin),
#endif #endif
}; };
@@ -161,6 +163,20 @@ namespace UniLua
lua.PushBoolean(true); lua.PushBoolean(true);
return 1; 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 #endif
} }
} }

View File

@@ -253,6 +253,18 @@ namespace CMLeonOS
} }
} }
public bool CurrentUserIsAdmin
{
get
{
if (currentLoggedInUser != null)
{
return currentLoggedInUser.IsAdmin;
}
return false;
}
}
public void FirstTimeSetup() public void FirstTimeSetup()
{ {
Console.WriteLine("===================================="); Console.WriteLine("====================================");