public ThreadStatus PCall(int numArgs, int numResults, int errFunc) { LuaState lua = LuaInstance.instance.Get(); int errorHandlerIndex = 0; #if UNITY_EDITOR || UNITY_STANDALONE lua.GetGlobal("lua_get_debug_info"); lua.PushValue(-2 - numArgs); for (int i = 0; i < numArgs; i++) { lua.PushValue(-numArgs - 2); } errorHandlerIndex = -2 - numArgs; #endif if (LuaAPI.lua_pcall(this.m_lua, numArgs, numResults, errorHandlerIndex) != 0) { Debug.LogError("lua Error stack: " + LuaInstance.ConstructString(lua)); #if UNITY_EDITOR || UNITY_STANDALONE lua.Pop(numArgs + 3); #endif return(ThreadStatus.LUA_ERRRUN); } #if UNITY_EDITOR || UNITY_STANDALONE int removeNum = numArgs + 2; while (removeNum > 0) { lua.Remove(-numResults - 1); removeNum--; } #endif return(ThreadStatus.LUA_OK); }
public override object CreateInstance(Type neosType) { Data.OverloadSetting typeOverloadSetting = GetOverload(neosType); if (typeOverloadSetting is null) { UniLog.Log($"Missing Component overload for {neosType.FullName}"); return(null); } Component instance = InstanceSlot.AttachComponent(typeOverloadSetting.OverloadType ?? neosType.FullName); if (typeOverloadSetting.Initializer != null) { try { //Create a new environment each time LuaTable environment = LuaInstance.CreateEnvironment(); environment.DefineFunction("FindType", new Func <string, Type>(luaFindType)); //Set "Instance" to point to the component we want to modify environment.SetMemberValue("Instance", instance); //Compile and run the initializer script LuaInstance.CompileChunk(typeOverloadSetting.Initializer, "Init.lua", new LuaCompileOptions() { ClrEnabled = false }).Run(environment); } catch (Exception ex) { UniLog.Log($"Unable to run initializer on {typeOverloadSetting.OverloadType}: {ex}"); return(null); } } return(instance); }
void PushObj(object p) { //is object if (!LuaInstance.PushBaseTypeObj(this, p.GetType(), p)) { //not base type or base type in object NewClassUserData(p); } }
public string ErrorInfo() { LuaAPI.lua_geterror_info(this.m_lua); if (this.IsNil(-1)) { return(string.Empty); } string errorInfo = LuaInstance.ConstructString(LuaInstance.instance.Get()); return(errorInfo); }
public void RegisterFuncByMethodInfo(String funcName, MethodInfo m, LuaAPI.lua_CFunction cfunc) { if (m == null) { return; // FIX add warnning } else { methods_.Add(m); paramters_.Add(m.GetParameters()); int a = LuaInstance.MergeInt(register_funtion_, classIndex); lua_.PushInteger(a); lua_.PushLuaClosure(cfunc, 1); lua_.SetField(-2, funcName); register_funtion_++; } }
private void OnDestroy() { if (!CSharpServiceManager.Initialized) { return; } var destroy = CachedClass.GetLuaMethod <LuaUnityEventFunction>("destroy"); destroy?.Invoke(LuaInstance); #if UNITY_DEBUG var luaVm = CSharpServiceManager.Get <LuaVM>(CSharpServiceManager.ServiceType.LUA_SERVICE); LuaInstance.SetMetaTable(luaVm.DestroyedTableMeta); #endif LuaInstance?.Dispose(); LuaInstance = null; foreach (var binding in LuaData) { binding.Destroy(); } }
public void RegisterField() { fields_ = register_type_.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static); if (fields_ == null) { return; // FIX add warnning } else { LuaAPI.lua_CFunction cfunc = LuaInstance.SetField; for (int i = 0; i < fields_.Length; i++) { int a = LuaInstance.MergeInt(field_index_, classIndex); lua_.PushInteger(a); lua_.PushLuaClosure(cfunc, 1); lua_.SetField(-2, fields_[i].Name); field_index_++; } } }
public void CallFunction(GameObject sender, string function, System.Object para) { LuaState lua_ = LuaInstance.instance.Get(); lua_.RawGetI(LuaAPI.LUA_REGISTRYINDEX, Luaclass_ref); if (sender == null) { lua_.GetField(-1, function); } else { lua_.GetField(-1, sender.name + function); } if (lua_.IsNil(-1)) { Debug.LogError("Can't find function: " + sender.name + function); return; } lua_.RawGetI(LuaAPI.LUA_REGISTRYINDEX, Object_ref); if (!LuaInstance.PushBaseTypeObj(lua_, para.GetType(), para)) { lua_.NewClassUserData(para); } if (!IsOnClickPassGameObject || sender == null) { lua_.PCall(2, 0, 0); } else { lua_.NewClassUserData(sender); lua_.PCall(3, 0, 0); } lua_.Pop(1); }
/// <summary> /// Loads this plugin from file /// </summary> /// <param name="filename"></param> public bool Load(string filename) { // Store filename Filename = filename; ShortFilename = Path.GetFileName(filename); Name = Path.GetFileNameWithoutExtension(filename); // Check it exists if (!File.Exists(filename)) { Logger.Error(string.Format("Failed to load plugin {0} (file not found)", Name)); return(false); } // Load it string script = File.ReadAllText(filename); // Attempt to compile LuaFunction func; try { func = LuaInstance.LoadString(script, ShortFilename); } catch (LuaScriptException ex) { Logger.Error(string.Format("Failed to load plugin {0}", Name), ex); return(false); } // Create the plugin table LuaFunction createplugin = LuaInstance["createplugin"] as LuaFunction; if (createplugin == null) { return(false); } table = createplugin.Call()[0] as LuaTable; table["Name"] = Name; table["Filename"] = filename; LuaInstance["PLUGIN"] = table; // Attempt to call try { func.Call(); } catch (LuaScriptException ex) { Logger.Error(string.Format("Failed to load plugin {0}", Name), ex); return(false); } LuaInstance["PLUGIN"] = null; // Get all functions functionmap = new Dictionary <string, LuaFunction>(); foreach (var key in table.Keys) { if (key is string) { var value = table[key]; if (value is LuaFunction) { try { functionmap.Add((string)key, value as LuaFunction); } catch (Exception e) { //Logger.Error(string.Format("Failed to map function {0} in {1} table.Keys", (string)key, Name)); } } } } // Get base functions LuaTable metatable = (LuaInstance["getmetatable"] as LuaFunction).Call(table)[0] as LuaTable; if (metatable != null) { LuaTable basetable = metatable["__index"] as LuaTable; foreach (var key in basetable.Keys) { if (key is string) { var value = basetable[key]; if (value is LuaFunction) { try { functionmap.Add((string)key, value as LuaFunction); } catch (Exception e) { //Logger.Error(string.Format("Failed to map function {0} in {1} metatable", (string)key, Name)); } } } } } // Check plugin descriptors if (!(table["Title"] is string)) { Logger.Error(string.Format("Failed to load plugin {0} (invalid 'Title')", Name)); return(false); } if (!(table["Description"] is string)) { Logger.Error(string.Format("Failed to load plugin {0} (invalid 'Description')", Name)); return(false); } if (!(table["Version"] is string)) { Logger.Error(string.Format("Failed to load plugin {0} (invalid 'Version')", Name)); return(false); } if (!(table["Author"] is string)) { Logger.Error(string.Format("Failed to load plugin {0} (invalid 'Author')", Name)); return(false); } // Load plugin descriptors Title = (string)table["Title"]; Description = (string)table["Description"]; Version = (string)table["Version"]; Author = (string)table["Author"]; // Success return(true); }
public ThreadStatus L_DoFile(string filename) { #if ASSETBUNDLE if ((LuaAPI.luaL_loadpak(this.m_lua, filename) == 0) && (LuaAPI.lua_pcall(this.m_lua, 0, -1, 0) == 0)) { return(ThreadStatus.LUA_OK); } #else if ((LuaAPI.luaL_loadfile(this.m_lua, filename) == 0) && (LuaAPI.lua_pcall(this.m_lua, 0, -1, 0) == 0)) { return(ThreadStatus.LUA_OK); } #endif //string errorInfo = "luaError: " + LuaInstance.instance.Get().ErrorInfo(1) + Environment.NewLine; UnityEngine.Debug.LogError("DoFile Error: " + filename + "\nlua stack: " + LuaInstance.ConstructString(LuaInstance.instance.Get())); return(ThreadStatus.LUA_ERRRUN); }
public static string GetLogInfo(int stackNum = 4) { int idx = -1; LuaType paraType = lua.Type(idx); object p = null; if (paraType == LuaType.LUA_TNUMBER) { p = (float)lua.ToNumber(idx); } else if (paraType == LuaType.LUA_TSTRING) { p = lua.ToString(idx); } else if (paraType == LuaType.LUA_TBOOLEAN) { p = lua.ToBoolean(idx); } else if (paraType == LuaType.LUA_TUSERDATA) { p = lua.ToUserDataObject(idx); } else if (paraType == LuaType.LUA_TNIL) { p = "Nil"; } else if (paraType == LuaType.LUA_TTABLE) { p = "Table Can't be Log."; } lua.GetGlobal("debug"); lua.GetField(-1, "getinfo"); lua.PushNumber(stackNum); lua.PCall(1, 1, 0); string stackInfo = string.Empty; if (!lua.IsNil(-1)) { lua.GetField(-1, "source"); lua.GetField(-2, "currentline"); stackInfo = lua.ToString(-2); stackInfo += lua.ToString(-1); lua.Pop(4); } else { lua.Pop(2); } lua.GetGlobal("lua_get_debug_info"); lua.PCall(0, 1, 0); string fullStackInfo = lua.ToString(-1); lua.Pop(1); string res = string.Empty; if (p != null) { res = p.ToString() + System.Environment.NewLine + stackInfo + System.Environment.NewLine + "StackInfo:\n" + fullStackInfo; } else { DebugLogger.LogError("call LuaDebugError: " + paraType + " stack:" + fullStackInfo + System.Environment.NewLine + LuaInstance.ConstructString(lua)); } return(res); }