mirror of
https://github.com/kuitoi/kuitoi-Server.git
synced 2025-08-17 08:15:42 +00:00
Compare commits
3 Commits
9ae200d48a
...
4f7e83a00f
Author | SHA1 | Date | |
---|---|---|---|
4f7e83a00f | |||
aa6716fa75 | |||
6fa07f3e07 |
@ -63,9 +63,9 @@ BeamingDrive Multiplayer (BeamMP) server compatible with BeamMP clients.
|
|||||||
- [ ] Client (Player) class
|
- [ ] Client (Player) class
|
||||||
- [ ] Lua part: (Original BeamMP compatibility)
|
- [ ] Lua part: (Original BeamMP compatibility)
|
||||||
- [x] Load Lua plugins
|
- [x] Load Lua plugins
|
||||||
- [x] MP Class
|
- [x] MP Class (Excluding CreateEventTimer, CreateEventTimer, TriggerLocalEvent, TriggerGlobalEvent, TriggerClientEvent, TriggerClientEventJson)
|
||||||
- [ ] Util class
|
- [ ] Util class
|
||||||
- [ ] FS class
|
- [x] FS class
|
||||||
- [x] MultiLanguage (i18n support)
|
- [x] MultiLanguage (i18n support)
|
||||||
- [ ] Core
|
- [ ] Core
|
||||||
- [x] Console
|
- [x] Console
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import os
|
import os
|
||||||
import platform
|
import platform
|
||||||
|
import random
|
||||||
|
import shutil
|
||||||
|
|
||||||
from lupa.lua53 import LuaRuntime
|
from lupa.lua53 import LuaRuntime
|
||||||
|
|
||||||
@ -16,9 +18,9 @@ class MP:
|
|||||||
|
|
||||||
def __init__(self, name: str, lua: LuaRuntime):
|
def __init__(self, name: str, lua: LuaRuntime):
|
||||||
self.loop = asyncio.get_event_loop()
|
self.loop = asyncio.get_event_loop()
|
||||||
self.tasks = []
|
|
||||||
self.name = name
|
|
||||||
self.log = get_logger(f"LuaPlugin | {name}")
|
self.log = get_logger(f"LuaPlugin | {name}")
|
||||||
|
self.name = name
|
||||||
|
self.tasks = []
|
||||||
self._lua = lua
|
self._lua = lua
|
||||||
|
|
||||||
def _print(self, *args):
|
def _print(self, *args):
|
||||||
@ -40,6 +42,14 @@ class MP:
|
|||||||
self.log.debug("request MP.RegisterEvent()")
|
self.log.debug("request MP.RegisterEvent()")
|
||||||
ev.register_event(event_name, self._lua.globals()[function_name], lua=True)
|
ev.register_event(event_name, self._lua.globals()[function_name], lua=True)
|
||||||
|
|
||||||
|
def CreateEventTimer(self, event_name: str, interval_ms: int, strategy: int = None):
|
||||||
|
self.log.debug("request CreateEventTimer()")
|
||||||
|
# TODO: CreateEventTimer
|
||||||
|
|
||||||
|
def CancelEventTimer(self, event_name: str):
|
||||||
|
self.log.debug("request CancelEventTimer()")
|
||||||
|
# TODO: CreateEventTimer
|
||||||
|
|
||||||
def TriggerLocalEvent(self, event_name, *args):
|
def TriggerLocalEvent(self, event_name, *args):
|
||||||
self.log.debug("request TriggerLocalEvent()")
|
self.log.debug("request TriggerLocalEvent()")
|
||||||
# TODO: TriggerLocalEvent
|
# TODO: TriggerLocalEvent
|
||||||
@ -153,6 +163,169 @@ class MP:
|
|||||||
self.log.warning("KuiToi cannot support this: MP.Settings()")
|
self.log.warning("KuiToi cannot support this: MP.Settings()")
|
||||||
|
|
||||||
|
|
||||||
|
# noinspection PyPep8Naming
|
||||||
|
class Util:
|
||||||
|
def __init__(self, name, lua):
|
||||||
|
self.log = get_logger(f"LuaPlugin | Util | {name}")
|
||||||
|
self.name = name
|
||||||
|
self._lua = lua
|
||||||
|
|
||||||
|
def JsonEncode(self, table):
|
||||||
|
self.log.debug("requesting JsonEncode()")
|
||||||
|
|
||||||
|
def JsonDecode(self, string):
|
||||||
|
self.log.debug("requesting JsonDecode()")
|
||||||
|
|
||||||
|
def JsonPrettify(self, string):
|
||||||
|
self.log.debug("requesting JsonPrettify()")
|
||||||
|
|
||||||
|
def JsonMinify(self, string):
|
||||||
|
self.log.debug("requesting JsonMinify()")
|
||||||
|
|
||||||
|
def JsonFlatten(self, string):
|
||||||
|
self.log.debug("requesting JsonFlatten()")
|
||||||
|
|
||||||
|
def JsonUnflatten(self, string):
|
||||||
|
self.log.debug("requesting JsonUnflatten()")
|
||||||
|
|
||||||
|
def JsonDiff(self, a, b):
|
||||||
|
self.log.debug("requesting JsonDiff()")
|
||||||
|
|
||||||
|
def JsonDiffApply(self, base, diff):
|
||||||
|
self.log.debug("requesting JsonDiffApply()")
|
||||||
|
|
||||||
|
def Random(self) -> int:
|
||||||
|
self.log.debug("requesting Random()")
|
||||||
|
return random.randint(0, 1)
|
||||||
|
|
||||||
|
def RandomIntRange(self, min_v, max_v):
|
||||||
|
self.log.debug("requesting RandomIntRange()")
|
||||||
|
|
||||||
|
def RandomRange(self, min_v, max_v):
|
||||||
|
self.log.debug("requesting RandomRange()")
|
||||||
|
|
||||||
|
|
||||||
|
# noinspection PyPep8Naming
|
||||||
|
class FP:
|
||||||
|
|
||||||
|
def __init__(self, name: str, lua: LuaRuntime):
|
||||||
|
self.log = get_logger(f"LuaPlugin | FP | {name}")
|
||||||
|
self.name = name
|
||||||
|
self._lua = lua
|
||||||
|
|
||||||
|
def CreateDirectory(self, path):
|
||||||
|
self.log.debug("requesting CreateDirectory()")
|
||||||
|
try:
|
||||||
|
os.makedirs(path)
|
||||||
|
return True, None
|
||||||
|
except FileNotFoundError | NotADirectoryError as e:
|
||||||
|
return False, f"{e}"
|
||||||
|
except PermissionError as e:
|
||||||
|
return False, f"{e}"
|
||||||
|
except OSError as e:
|
||||||
|
return False, f"{e}"
|
||||||
|
except TypeError as e:
|
||||||
|
return False, f"{e}"
|
||||||
|
except ValueError as e:
|
||||||
|
return False, f"{e}"
|
||||||
|
|
||||||
|
def Remove(self, path):
|
||||||
|
self.log.debug("requesting Remove()")
|
||||||
|
try:
|
||||||
|
if os.path.isdir(path):
|
||||||
|
os.rmdir(path)
|
||||||
|
else:
|
||||||
|
os.remove(path)
|
||||||
|
return True, None
|
||||||
|
except (FileNotFoundError, NotADirectoryError) as e:
|
||||||
|
return False, f"{e}"
|
||||||
|
except PermissionError as e:
|
||||||
|
return False, f"{e}"
|
||||||
|
except OSError as e:
|
||||||
|
return False, f"{e}"
|
||||||
|
except TypeError as e:
|
||||||
|
return False, f"{e}"
|
||||||
|
|
||||||
|
def Rename(self, path_from, path_to):
|
||||||
|
self.log.debug("requesting Rename()")
|
||||||
|
try:
|
||||||
|
os.rename(path_from, path_to)
|
||||||
|
return True, None
|
||||||
|
except (FileNotFoundError, NotADirectoryError) as e:
|
||||||
|
return False, f"{e}"
|
||||||
|
except PermissionError as e:
|
||||||
|
return False, f"{e}"
|
||||||
|
except OSError as e:
|
||||||
|
return False, f"{e}"
|
||||||
|
except TypeError as e:
|
||||||
|
return False, f"{e}"
|
||||||
|
|
||||||
|
def Copy(self, path_from, path_to):
|
||||||
|
self.log.debug("requesting Copy()")
|
||||||
|
try:
|
||||||
|
if os.path.isfile(path_from):
|
||||||
|
shutil.copy2(path_from, path_to)
|
||||||
|
elif os.path.isdir(path_from):
|
||||||
|
shutil.copytree(path_from, path_to)
|
||||||
|
else:
|
||||||
|
raise ValueError("Invalid path: {}".format(path_from))
|
||||||
|
return True, None
|
||||||
|
except (FileNotFoundError, NotADirectoryError, shutil.Error) as e:
|
||||||
|
return False, f"{e}"
|
||||||
|
except PermissionError as e:
|
||||||
|
return False, f"{e}"
|
||||||
|
except OSError as e:
|
||||||
|
return False, f"{e}"
|
||||||
|
except TypeError as e:
|
||||||
|
return False, f"{e}"
|
||||||
|
|
||||||
|
def GetFilename(self, path):
|
||||||
|
self.log.debug("requesting GetFilename()")
|
||||||
|
return os.path.basename(path)
|
||||||
|
|
||||||
|
def GetExtension(self, path):
|
||||||
|
self.log.debug("requesting GetExtension()")
|
||||||
|
return os.path.splitext(path)[1]
|
||||||
|
|
||||||
|
def GetParentFolder(self, path):
|
||||||
|
self.log.debug("requesting GetParentFolder()")
|
||||||
|
return os.path.dirname(path)
|
||||||
|
|
||||||
|
def Exists(self, path):
|
||||||
|
self.log.debug("requesting Exists()")
|
||||||
|
return os.path.exists(path)
|
||||||
|
|
||||||
|
def IsDirectory(self, path):
|
||||||
|
self.log.debug("requesting IsDirectory()")
|
||||||
|
return os.path.isdir(path)
|
||||||
|
|
||||||
|
def IsFile(self, path):
|
||||||
|
self.log.debug("requesting IsFile()")
|
||||||
|
return os.path.isfile(path)
|
||||||
|
|
||||||
|
def ListDirectories(self, path):
|
||||||
|
self.log.debug("requesting ListDirectories()")
|
||||||
|
directories = []
|
||||||
|
for item in os.listdir(path):
|
||||||
|
item_path = os.path.join(path, item)
|
||||||
|
if os.path.isdir(item_path):
|
||||||
|
directories.append(item)
|
||||||
|
return self._lua.table_from({i: v for i, v in enumerate(directories)})
|
||||||
|
|
||||||
|
def ListFiles(self, path):
|
||||||
|
self.log.debug("requesting ListFiles()")
|
||||||
|
files = []
|
||||||
|
for item in os.listdir(path):
|
||||||
|
item_path = os.path.join(path, item)
|
||||||
|
if os.path.isfile(item_path):
|
||||||
|
files.append(item)
|
||||||
|
return self._lua.table_from({i: v for i, v in enumerate(files)})
|
||||||
|
|
||||||
|
def ConcatPaths(self, *args):
|
||||||
|
self.log.debug("requesting ConcatPaths()")
|
||||||
|
return os.path.join(*args)
|
||||||
|
|
||||||
|
|
||||||
class LuaPluginsLoader:
|
class LuaPluginsLoader:
|
||||||
|
|
||||||
def __init__(self, plugins_dir):
|
def __init__(self, plugins_dir):
|
||||||
@ -188,6 +361,8 @@ class LuaPluginsLoader:
|
|||||||
mp = MP(obj, lua)
|
mp = MP(obj, lua)
|
||||||
lua.globals().MP = mp
|
lua.globals().MP = mp
|
||||||
lua.globals().print = mp._print
|
lua.globals().print = mp._print
|
||||||
|
lua.globals().Util = Util(obj, lua)
|
||||||
|
lua.globals().FP = FP(obj, lua)
|
||||||
code = f'package.path = package.path.."' \
|
code = f'package.path = package.path.."' \
|
||||||
f';{self.plugins_dir}/{obj}/?.lua' \
|
f';{self.plugins_dir}/{obj}/?.lua' \
|
||||||
f';{self.plugins_dir}/{obj}/lua/?.lua"\n'
|
f';{self.plugins_dir}/{obj}/lua/?.lua"\n'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user