Refactor package.path;

Fix: Now .lua run from loadfrom(file);
This commit is contained in:
Maxim Khomutov 2023-07-22 00:20:24 +03:00
parent 0d3699bfee
commit 1b5ddbdd45

View File

@ -514,6 +514,18 @@ class LuaPluginsLoader:
console.add_command("lua_plugins", lambda x: self.loaded_str[:-2]) console.add_command("lua_plugins", lambda x: self.loaded_str[:-2])
console.add_command("lua_pl", lambda x: self.loaded_str[:-2]) console.add_command("lua_pl", lambda x: self.loaded_str[:-2])
def _start(self, obj, lua, file):
try:
f = lua.globals().loadfile(os.path.abspath(f"plugins/{obj}/{file}"))
f()
self.lua_plugins[obj]['ok'] = True
self.loaded_str += f"{obj}:ok, "
lua.globals().MP.TriggerLocalEvent("onInit")
except Exception as e:
self.loaded_str += f"{obj}:no, "
self.log.error(f"Cannot load lua plugin from `{obj}/main.lua`\n{e}")
# self.log.exception(e)
def load(self): def load(self):
self.log.debug("Loading Lua plugins...") self.log.debug("Loading Lua plugins...")
py_folders = ev.call_event("_plugins_get")[0] py_folders = ev.call_event("_plugins_get")[0]
@ -535,22 +547,16 @@ class LuaPluginsLoader:
lua.globals().print = mp._print lua.globals().print = mp._print
lua.globals().Util = Util(obj, lua) lua.globals().Util = Util(obj, lua)
lua.globals().FP = FP(obj, lua) lua.globals().FP = FP(obj, lua)
code = f'package.path = package.path.."' \ pa = os.path.abspath(self.plugins_dir)
f';{self.plugins_dir}/{obj}/?.lua' \ p0 = os.path.join(pa, obj, "?.lua")
f';{self.plugins_dir}/{obj}/lua/?.lua"\n' p1 = os.path.join(pa, obj, "lua", "?.lua")
with open("modules/PluginsLoader/add_in.lua", "r") as f: lua.globals().package.path += f';{p0};{p1}'
code += f.read() # with open("modules/PluginsLoader/add_in.lua", "r") as f:
with open(os.path.join(path, "main.lua"), 'r', encoding=config.enc) as f: # code += f.read()
code += f.read() self.lua_plugins.update({obj: {"mp": mp, "lua": lua, "thread": None, "ok": False}})
try: th = Thread(target=self._start, args=(obj, lua, "main.lua"), name=f"lua_plugin_{obj}-Thread")
th = Thread(target=lua.execute, args=(code,), name=f"lua_plugin_{obj}-Thread") th.start()
th.start() self.lua_plugins[obj]['thread'] = th
self.loaded_str += f"{obj}:ok, "
self.lua_plugins.update({obj: {"mp": mp, "lua": lua, "thread": th}})
except Exception as e:
self.log.error(f"Cannot load lua plugin from `{obj}/main.lua`")
self.log.exception(e)
self.loaded_str += f"{obj}:no, "
def unload(self, _): def unload(self, _):
... ...