DR -> DR SDK #16

Merged
shenjackyuanjie merged 41 commits from feature/dr-sdk into main 2023-05-03 00:40:53 +08:00
8 changed files with 35 additions and 53 deletions
Showing only changes of commit 98b049c271 - Show all commits

View File

@ -4,20 +4,14 @@
# All rights reserved
# -------------------------------
"""
writen by shenjackyuanjie
mail: 3695888@qq.com
github: @shenjackyuanjie
gitee: @shenjackyuanjie
"""
# import ctypes
import os
import sys
import warnings
import importlib
import traceback
import contextlib
import importlib.util
from pathlib import Path
from typing import Optional, List, Tuple
from Difficult_Rocket.api.types import Options
@ -78,14 +72,11 @@ class _DR_option(Options):
playing: bool = False
debugging: bool = False
crash_report_test: bool = True
pyglet_macosx_dev_test: bool = True
# window option
gui_scale: float = 1.0 # default 1.0 2.0 -> 2x 3 -> 3x
def init(self, **kwargs):
if sys.platform != 'darwin': # MacOS 的测试只能在 Macos 上跑
self.pyglet_macosx_dev_test = False
try:
from libs.Difficult_Rocket_rs import test_call, get_version_str
test_call(self)
@ -149,7 +140,6 @@ class _DR_runtime(Options):
relationship = 'larger' if self.DR_Rust_version > self.DR_Rust_get_version else 'smaller'
warnings.warn(f'DR_rust builtin version is {self.DR_Rust_version} but true version is {get_version_str()}.\n'
f'Builtin version {relationship} than true version')
self.find_mods()
def load_file(self) -> bool:
with contextlib.suppress(FileNotFoundError):
@ -165,13 +155,28 @@ class _DR_runtime(Options):
mod_list = self.find_mods()
def find_mods(self) -> List[str]:
objects = os.listdir(self.mod_path)
for obj in objects:
if os.path.isdir(os.path.join(self.mod_path, obj)):
...
mods = []
paths = Path(self.mod_path).iterdir()
sys.path.append(self.mod_path)
for mod_path in paths:
try:
if mod_path.is_dir: # 文件夹格式的 mod
if not (mod_path / '__init__.py').exists(): # 不符合格式要求
continue
if importlib.util.find_spec(mod_path.name) is not None:
module = importlib.import_module(mod_path.name)
info: Options = module.INFO
mods.append(mod_path.name)
print(f'find mod in {mod_path} ID: {info.mod_id} name: {info.name} version: {info.version}')
print(f'import mod {mod_path}')
else:
print(f'can not import mod {mod_path} because importlib can not find spec')
else:
...
...
except ImportError:
print(f'ImportError when loading mod {mod_path}')
traceback.print_exc()
return mods
DR_option = _DR_option()

View File

@ -31,6 +31,7 @@ class MODInfo(Options):
加载mod时候的参数
"""
"""基本信息"""
mod_id: str # mod id
name: str # mod 名称
version: Version # mod 版本

View File

@ -5,17 +5,10 @@
# All rights reserved
# -------------------------------
"""
writen by shenjackyuanjie
mail: 3695888@qq.com
github: @shenjackyuanjie
gitee: @shenjackyuanjie
"""
import inspect
# system function
import os
import time
import logging
import inspect
import functools
import traceback
@ -59,7 +52,7 @@ class ClientOption(Options):
caption: str = "Difficult Rocket v{DR_version}|DR_rs v{DR_Rust_get_version}"
def load_file(self) -> None:
file = tools.load_file('./configs/main.toml')
file: dict = tools.load_file('./configs/main.toml')
self.fps = int(file['runtime']['fps'])
self.width = int(file['window']['width'])
self.height = int(file['window']['height'])
@ -82,13 +75,10 @@ class Client:
self.process_name = 'Client process'
self.process_pid = os.getpid()
self.net_mode = net_mode
file_drop = bool(
pyglet.compat_platform != 'darwin' or DR_option.pyglet_macosx_dev_test
)
self.window = ClientWindow(net_mode=self.net_mode, width=self.config.width, height=self.config.height,
fullscreen=self.config.fullscreen, caption=self.config.caption,
resizable=self.config.resizeable, visible=self.config.visible,
file_drops=file_drop)
file_drops=True)
end_time = time.time_ns()
self.use_time = end_time - start_time
if DR_option.use_DR_rust:
@ -206,6 +196,8 @@ class ClientWindow(Window):
def setup(self):
self.set_icon(pyglet.image.load('./textures/icon.png'))
self.logger.info(f"=== finding mods from {DR_runtime.mod_path} ===")
self.logger.info(f'find mods: {DR_runtime.find_mods()}')
self.load_fonts()
# TODO 读取配置文件,加载不同的屏幕,解耦
self.screen_list.append(DRDEBUGScreen(self))
@ -242,7 +234,7 @@ class ClientWindow(Window):
@new_thread('window save_info')
def save_info(self):
self.logger.info(tr().client.config.save.start())
config_file = tools.load_file('./configs/main.toml')
config_file: dict = tools.load_file('./configs/main.toml')
config_file['window']['width'] = self.width
config_file['window']['height'] = self.height
config_file['runtime']['language'] = DR_runtime.language
@ -253,7 +245,7 @@ class ClientWindow(Window):
client api
"""
def add_sub_screen(self, sub_screen: BaseScreen.__class__):
def add_sub_screen(self, sub_screen: type(BaseScreen)):
self.screen_list.append(sub_screen(self))
"""

View File

@ -121,7 +121,7 @@ class Translates:
self._config.is_final = True
return self
def __call__(self, *args, **kwargs) -> Union[dict, list, int, str]:
def __call__(self, *args, **kwargs) -> str:
return self.__str__()
def copy(self):

View File

@ -15,11 +15,10 @@
pub mod plugin_trait {
pub struct ModInfo {
pub name: String,
pub version: String
pub version: String,
}
pub trait ModInfoTrait {
fn info() -> ModInfo;
}
}

View File

@ -9,7 +9,6 @@
use pyo3::prelude::*;
use rapier2d_f64::prelude::*;
#[pyfunction]
#[pyo3(name = "simluation")]
pub fn simluation() -> PyResult<()> {
@ -64,4 +63,3 @@ pub fn simluation() -> PyResult<()> {
}
Ok(())
}

View File

@ -8,6 +8,7 @@ from MCDR.version import Version
from Difficult_Rocket.mod import MODInfo
INFO = MODInfo(
mod_id="Difficult_Rocket_mod",
name="Difficult_Rocket_mod",
version=Version("0.7.2.0")
)

View File

@ -1,14 +0,0 @@
struct a_part
part_type :: String
part_id :: UInt64
enable :: Bool
x :: Float64
y :: Float64
x_v :: Float64
y_v :: Float64
angle :: Float16
angle_v :: Float64
end