public static Read ( |
||
L | ||
n | int | |
return |
public static IEnumerable <LuaValue> GetReverseStack(IntPtr L) { for (var i = Lua.lua_gettop(L); i <= 0; i--) { yield return(LuaValue.Read(L, i)); } }
public static IEnumerable <LuaValue> GetStack(IntPtr L) { for (var i = 1; i <= Lua.lua_gettop(L); i++) { yield return(LuaValue.Read(L, i)); } }
/// <summary> /// expects function on top /// runs function on top of stack, returns values returned from function, shows traceback in case of errors /// </summary> public static LuaValue[] TraceCall(IntPtr L, int resultCount, params LuaValue[] arguments) { TraceCallPushReturn(L, new ConstantResults(resultCount), arguments); var ret = Enumerable.Range(0, resultCount).Select(n => LuaValue.Read(L, -n - 1)).ToArray(); Array.Reverse(ret); LuaValue.Pop(L, resultCount); return(ret); }
public static string Read(IntPtr L, int n) { var s = LuaValue.Read(L, n) as LuaString; if (s == null) { throw new Exception("string expected but not found"); } return(s.Value); }
/// <summary> /// creates a lua state and registers functions commonly used in lua defs /// </summary> static IntPtr GetLuaStpringState(Dictionary <string, IArchiveFileData> fileMap) { var L = Lua.luaL_newstate(); Lua.luaL_openlibs(L); // it seems CA makes lowerkeys global, so lets do the same // push the system table CLua.DoStringPushReturn(L, fileMap["gamedata/system.lua"].Text, new ConstantResults(1)); // get the lowerkeys field from the system table and push it Lua.lua_pushstring(L, "lowerkeys"); Lua.lua_gettable(L, -2); // set the lowerkeys function as global Lua.lua_setglobal(L, "lowerkeys"); Lua.lua_CFunction VFS_Include = l => { var path = CLua.ExpectArgs(l, 1)[0].ToString(); IArchiveFileData file; if (!fileMap.TryGetValue(path, out file)) { throw new Exception("path not found: " + path); } CLua.DoStringPushReturn(l, file.Text, new ConstantResults(1)); return(1); }; Lua.lua_CFunction VFS_LoadFile = l => { var path = CLua.ExpectArgs(l, 1)[0].ToString(); IArchiveFileData file; if (!fileMap.TryGetValue(path, out file)) { throw new Exception("path not found: " + path); } return(CLua.ReturnValues(l, new LuaString(file.Text))); }; Lua.lua_CFunction VFS_FileExists = l => { var path = CLua.ExpectArgs(l, 1)[0].ToString(); return(CLua.ReturnValues(l, new LuaBoolean(fileMap.ContainsKey(path)))); }; Lua.lua_CFunction VFS_DirList = l => { var args = CLua.ExpectArgs(l, 2); var path = args[0].ToString().ToLower(); var mask = args[1].ToString().ToLower().Substring(1); var i = 0; var files = from s in fileMap.Keys where s.StartsWith(path) && s.EndsWith(mask) let arrayIndex = new LuaNumber(i++) select new KeyValuePair <LuaValue, LuaValue>(arrayIndex, new LuaString(s)); return(CLua.ReturnValues(l, new LuaTable(files))); }; Lua.lua_CFunction Spring_TimeCheck = l => { var desc = LuaValue.Read(l, 1).ToString(); Lua.lua_pushvalue(l, 2); var sw = Stopwatch.StartNew(); CLua.TraceCallPushReturn(l, new ConstantResults(0)); Trace.TraceInformation(desc + " " + sw.Elapsed); // call function on top, push return values on stack return(0); }; Lua.lua_CFunction Spring_Echo = l => CLua.Print(s => Trace.WriteLine(s), l); // morphs defs crash if they can't figure out what kind of commander we're using (why do we need morph defs anyway, here?) Lua.lua_CFunction Spring_GetModOptions = l => { var data = new KeyValuePair <LuaValue, LuaValue>(new LuaString("commtype"), new LuaString("default")); return(CLua.ReturnValues(l, new LuaTable(new[] { data }))); }; var springFunctions = new List <KeyValuePair <LuaValue, LuaValue> > { new KeyValuePair <LuaValue, LuaValue>(new LuaString("TimeCheck"), new LuaFunction(Spring_TimeCheck)), new KeyValuePair <LuaValue, LuaValue>(new LuaString("Echo"), new LuaFunction(Spring_Echo)), new KeyValuePair <LuaValue, LuaValue>(new LuaString("GetModOptions"), new LuaFunction(Spring_GetModOptions)), }; CLua.SetGlobal(L, "Spring", new LuaTable(springFunctions)); var vfsFunctions = new List <KeyValuePair <LuaValue, LuaValue> > { new KeyValuePair <LuaValue, LuaValue>(new LuaString("Include"), new LuaFunction(VFS_Include)), new KeyValuePair <LuaValue, LuaValue>(new LuaString("LoadFile"), new LuaFunction(VFS_LoadFile)), new KeyValuePair <LuaValue, LuaValue>(new LuaString("FileExists"), new LuaFunction(VFS_FileExists)), new KeyValuePair <LuaValue, LuaValue>(new LuaString("DirList"), new LuaFunction(VFS_DirList)), }; CLua.SetGlobal(L, "VFS", new LuaTable(vfsFunctions)); return(L); }