Is this LUAAAAA? No...

This commit is contained in:
Maxim Khomutov 2023-07-20 05:21:00 +03:00
parent 744a7347a3
commit 752e981462
5 changed files with 60 additions and 8 deletions

View File

@ -16,7 +16,7 @@ from core import utils
from core.Client import Client from core.Client import Client
from core.tcp_server import TCPServer from core.tcp_server import TCPServer
from core.udp_server import UDPServer from core.udp_server import UDPServer
from modules import PluginsLoader from modules import PluginsLoader, LuaPluginsLoader
from modules.WebAPISystem import app as webapp from modules.WebAPISystem import app as webapp
@ -217,11 +217,14 @@ class Core:
lambda x: f"Players list: {self.get_clients_list(True)}" lambda x: f"Players list: {self.get_clients_list(True)}"
) )
self.log.debug("Initializing PluginsLoader...") pl_dir = "plugins"
if not os.path.exists("plugins"): self.log.debug("Initializing PluginsLoaders...")
os.mkdir("plugins") if not os.path.exists(pl_dir):
pl = PluginsLoader("plugins") os.mkdir(pl_dir)
pl = PluginsLoader(pl_dir)
await pl.load() await pl.load()
lpl = LuaPluginsLoader(pl_dir)
await lpl.load()
try: try:
# WebApi Start # WebApi Start
@ -263,6 +266,7 @@ class Core:
t = asyncio.wait(tasks, return_when=asyncio.FIRST_EXCEPTION) t = asyncio.wait(tasks, return_when=asyncio.FIRST_EXCEPTION)
await ev.call_async_event("_plugins_start") await ev.call_async_event("_plugins_start")
await ev.call_async_event("_lua_plugins_start")
self.run = True self.run = True
self.log.info(i18n.start) self.log.info(i18n.start)
@ -287,6 +291,7 @@ class Core:
ev.call_event("onServerStopped") ev.call_event("onServerStopped")
await ev.call_async_event("onServerStopped") await ev.call_async_event("onServerStopped")
await ev.call_async_event("_plugins_unload") await ev.call_async_event("_plugins_unload")
await ev.call_async_event("_lua_plugins_unload")
self.run = False self.run = False
self.log.info(i18n.stop) self.log.info(i18n.stop)
if config.WebAPI["enabled"]: if config.WebAPI["enabled"]:

View File

@ -1 +1,2 @@
from .plugins_loader import PluginsLoader from .plugins_loader import PluginsLoader
from .lua_plugins_loader import LuaPluginsLoader

View File

@ -0,0 +1,45 @@
import asyncio
import os
from core import get_logger
class MP:
def __init__(self):
pass
class LuaPluginsLoader:
def __init__(self, plugins_dir):
self.loop = asyncio.get_event_loop()
self.lua_plugins = {}
self.lua_plugins_tasks = []
self.plugins_dir = plugins_dir
self.lua_dirs = []
self.log = get_logger("LuaPluginsLoader")
self.loaded_str = "Lua plugins: "
ev.register_event("_lua_plugins_start", self.start)
ev.register_event("_lua_plugins_unload", self.unload)
console.add_command("lua_plugins", lambda x: self.loaded_str[:-2])
console.add_command("lua_pl", lambda x: self.loaded_str[:-2])
async def load(self):
self.log.debug("Loading Lua plugins...")
py_folders = ev.call_event("_plugins_get")[0]
folders = []
for obj in os.listdir(self.plugins_dir):
path = os.path.join(self.plugins_dir, obj)
if os.path.isdir(path) and obj not in py_folders and obj not in "__pycache__":
if os.path.isfile(os.path.join(path, "main.lua")):
folders.append(path)
self.log.debug(f"py_folders {py_folders}, folders {folders}")
# for dirpath, dirnames, filenames in os.walk(f"/{self.plugins_dir}/{plugin_dir}"):
# if "main.lua" in filenames:
# return os.path.join(dirpath, "main.lua")
async def start(self, _): ...
async def unload(self, _): ...

View File

@ -73,7 +73,7 @@ class PluginsLoader:
self.loaded_str = "Plugins: " self.loaded_str = "Plugins: "
ev.register_event("_plugins_start", self.start) ev.register_event("_plugins_start", self.start)
ev.register_event("_plugins_unload", self.unload) ev.register_event("_plugins_unload", self.unload)
ev.register_event("_plugins_get", lambda x: self.plugins) ev.register_event("_plugins_get", lambda x: list(self.plugins.keys()))
console.add_command("plugins", lambda x: self.loaded_str[:-2]) console.add_command("plugins", lambda x: self.loaded_str[:-2])
console.add_command("pl", lambda x: self.loaded_str[:-2]) console.add_command("pl", lambda x: self.loaded_str[:-2])
@ -81,14 +81,14 @@ class PluginsLoader:
self.log.debug("Loading plugins...") self.log.debug("Loading plugins...")
files = os.listdir(self.plugins_dir) files = os.listdir(self.plugins_dir)
for file in files: for file in files:
if os.path.isfile(file) and file.endswith(".py"): file_path = os.path.join(self.plugins_dir, file)
if os.path.isfile(file_path) and file.endswith(".py"):
try: try:
self.log.debug(f"Loading plugin: {file[:-3]}") self.log.debug(f"Loading plugin: {file[:-3]}")
plugin = types.ModuleType(file[:-3]) plugin = types.ModuleType(file[:-3])
plugin.KuiToi = KuiToi plugin.KuiToi = KuiToi
plugin.KuiToi._plugins_dir = self.plugins_dir plugin.KuiToi._plugins_dir = self.plugins_dir
plugin.print = print plugin.print = print
file_path = os.path.join(self.plugins_dir, file)
plugin.__file__ = file_path plugin.__file__ = file_path
with open(f'{file_path}', 'r', encoding=config.enc) as f: with open(f'{file_path}', 'r', encoding=config.enc) as f:
code = f.read() code = f.read()

View File

@ -11,5 +11,6 @@ from .ConfigProvider import ConfigProvider, Config
from .i18n import MultiLanguage from .i18n import MultiLanguage
from .EventsSystem import EventsSystem from .EventsSystem import EventsSystem
from .PluginsLoader import PluginsLoader from .PluginsLoader import PluginsLoader
from .PluginsLoader import LuaPluginsLoader
from .WebAPISystem import web_app from .WebAPISystem import web_app
from .WebAPISystem import _stop as stop_web from .WebAPISystem import _stop as stop_web