Files
CMLeonOS/UniLua/LuaOsLib.cs

87 lines
1.9 KiB
C#
Raw Normal View History

2026-02-03 02:44:58 +08:00
namespace UniLua
{
using System.Diagnostics;
2026-02-04 00:27:38 +08:00
using CMLeonOS;
2026-02-03 02:44:58 +08:00
internal class LuaOSLib
{
public const string LIB_NAME = "os";
public static int OpenLib( ILuaState lua )
{
NameFuncPair[] define = new NameFuncPair[]
{
#if !UNITY_WEBPLAYER
new NameFuncPair("clock", OS_Clock),
2026-02-03 23:41:11 +08:00
new NameFuncPair("gethostname", OS_Gethostname),
2026-02-04 00:27:38 +08:00
new NameFuncPair("getenv", OS_Getenv),
new NameFuncPair("setenv", OS_Setenv),
new NameFuncPair("delenv", OS_Delenv),
new NameFuncPair("addenv", OS_Addenv),
2026-02-03 02:44:58 +08:00
#endif
};
lua.L_NewLib( define );
return 1;
}
#if !UNITY_WEBPLAYER
private static int OS_Clock( ILuaState lua )
{
2026-02-03 23:41:11 +08:00
lua.PushNumber(0);
return 1;
}
private static int OS_Gethostname( ILuaState lua )
{
string hostname = CMLeonOS.Kernel.userSystem?.GetHostname() ?? "Not set";
lua.PushString(hostname);
2026-02-03 02:44:58 +08:00
return 1;
}
2026-02-04 00:27:38 +08:00
private static int OS_Getenv( ILuaState lua )
{
string varName = lua.L_CheckString(1);
string varValue = EnvironmentVariableManager.Instance.GetVariable(varName);
if (varValue == null)
{
lua.PushNil();
}
else
{
lua.PushString(varValue);
}
return 1;
}
private static int OS_Setenv( ILuaState lua )
{
string varName = lua.L_CheckString(1);
string varValue = lua.L_CheckString(2);
EnvironmentVariableManager.Instance.SetVariable(varName, varValue);
lua.PushBoolean(true);
return 1;
}
private static int OS_Delenv( ILuaState lua )
{
string varName = lua.L_CheckString(1);
bool success = EnvironmentVariableManager.Instance.DeleteVariable(varName);
lua.PushBoolean(success);
return 1;
}
private static int OS_Addenv( ILuaState lua )
{
string varName = lua.L_CheckString(1);
string varValue = lua.L_CheckString(2);
EnvironmentVariableManager.Instance.SetVariable(varName, varValue);
lua.PushBoolean(true);
return 1;
}
2026-02-03 02:44:58 +08:00
#endif
}
}