/// <summary> /// Creates a function. /// </summary> /// <param name="source"></param> /// <param name="status"></param> /// <param name="factory"></param> /// <returns>The new function. If an error is thrown while creating the function, null is returned /// and status is set accordingly (if applicable).</returns> public static LuaFunction CreateFunction(string source, ref LuaStatus status, Lua factory) { var state = factory.State; var global = (LuaTable)factory.GetObjectFromPath("_G"); var globalKeysBefore = (ICollection <object>)global.Keys; // compiles and pushes to the stack as a function // if theres an error then it pushes that error to the stack // we need to clean that up if that happens status = state.LoadString(source); if (status != LuaStatus.OK) { state.Pop(1); return(null); } status = state.PCall(0, 0, 0); // run the function on the stack, ie the chunk if (status != LuaStatus.OK) { return(null); } var globalKeysAfter = (ICollection <object>)global.Keys; var difference = Enumerable.Except(globalKeysAfter, globalKeysBefore); Console.WriteLine(difference.Count()); var first = difference.First(); Console.WriteLine(first); return((LuaFunction)global[first]); }
private static LuaTable CreateConfigurationTable(NLua.Lua luaInterpreter, string luaVisibility) { LuaTable table = NLuaUtilities.MakeLuaTable(luaInterpreter, "the_table"); WorkshopItemVisibility visibility = (WorkshopItemVisibility)luaInterpreter.GetObjectFromPath("visibility." + luaVisibility); table["app_id"] = AppIdAsLong; table["item_id"] = ItemIdAsLong; table["title"] = Title; table["description_file"] = DescriptionFilePath; table["item_folder"] = FolderPath; table["visibility"] = visibility; table["language"] = Language; return(table); }