完善 MainGame
This commit is contained in:
parent
41197b65f7
commit
0cb5b54eb7
@ -5,6 +5,7 @@
|
|||||||
# -------------------------------
|
# -------------------------------
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
import time
|
||||||
import importlib
|
import importlib
|
||||||
import traceback
|
import traceback
|
||||||
import contextlib
|
import contextlib
|
||||||
|
@ -15,6 +15,7 @@ import os
|
|||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
import logging
|
import logging
|
||||||
|
import traceback
|
||||||
import importlib
|
import importlib
|
||||||
import importlib.util
|
import importlib.util
|
||||||
import logging.config
|
import logging.config
|
||||||
@ -28,16 +29,16 @@ if __name__ == '__main__': # been start will not run this
|
|||||||
sys.path.append('/bin/libs')
|
sys.path.append('/bin/libs')
|
||||||
sys.path.append('/bin')
|
sys.path.append('/bin')
|
||||||
|
|
||||||
from Difficult_Rocket import client, server, DR_option, DR_runtime
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from Difficult_Rocket.api.mod import ModInfo
|
from Difficult_Rocket.api.mod import ModInfo
|
||||||
else:
|
else:
|
||||||
ModInfo = TypeVar('ModInfo')
|
ModInfo = TypeVar('ModInfo')
|
||||||
from Difficult_Rocket.utils import tools
|
from Difficult_Rocket.utils import tools
|
||||||
from Difficult_Rocket.utils.translate import tr
|
|
||||||
from Difficult_Rocket.crash import write_info_to_cache
|
|
||||||
from Difficult_Rocket.api.types import Options
|
from Difficult_Rocket.api.types import Options
|
||||||
|
from Difficult_Rocket.utils.translate import tr
|
||||||
from Difficult_Rocket.utils.thread import new_thread
|
from Difficult_Rocket.utils.thread import new_thread
|
||||||
|
from Difficult_Rocket.crash import write_info_to_cache
|
||||||
|
from Difficult_Rocket import client, server, DR_option, DR_runtime
|
||||||
|
|
||||||
|
|
||||||
class Game:
|
class Game:
|
||||||
@ -206,12 +207,23 @@ class MainGame(Options):
|
|||||||
console: Console
|
console: Console
|
||||||
|
|
||||||
main_config: Dict
|
main_config: Dict
|
||||||
|
logging_config: Dict
|
||||||
logger: logging.Logger
|
logger: logging.Logger
|
||||||
|
|
||||||
mod_module: List["ModInfo"]
|
mod_module: List["ModInfo"]
|
||||||
|
|
||||||
def init_logger(self) -> None:
|
def init_logger(self) -> None:
|
||||||
...
|
log_path = self.logging_config['handlers']['file']['filename']
|
||||||
|
log_path = Path(f"logs/{log_path.format(time.strftime('%Y-%m-%d %H-%M-%S' , time.gmtime(DR_runtime.start_time_ns)))}")
|
||||||
|
mkdir = False
|
||||||
|
if not log_path.exists():
|
||||||
|
log_path.mkdir(parents=True)
|
||||||
|
mkdir = True
|
||||||
|
self.logging_config['handlers']['file']['filename'] = log_path.name
|
||||||
|
logging.config.dictConfig(self.logging_config)
|
||||||
|
self.logger = logging.getLogger('main')
|
||||||
|
if mkdir:
|
||||||
|
self.logger.info(tr().main.logger.mkdir())
|
||||||
|
|
||||||
def init_console(self) -> None:
|
def init_console(self) -> None:
|
||||||
self.console = Console()
|
self.console = Console()
|
||||||
@ -274,14 +286,33 @@ class MainGame(Options):
|
|||||||
if hasattr(mod, event_name):
|
if hasattr(mod, event_name):
|
||||||
try:
|
try:
|
||||||
getattr(mod, event_name)(*args, **kwargs)
|
getattr(mod, event_name)(*args, **kwargs)
|
||||||
except Exception as e:
|
except Exception:
|
||||||
self.logger.error(tr().main.mod.event.error().format(event_name, e, mod.mod_id))
|
error = traceback.format_exc()
|
||||||
|
self.logger.error(tr().main.mod.event.error().format(event_name, error, mod.mod_id))
|
||||||
|
|
||||||
def init(self, **kwargs) -> None:
|
def log_env(self) -> None:
|
||||||
...
|
cache_steam = StringIO()
|
||||||
|
write_info_to_cache(cache_steam)
|
||||||
|
text = cache_steam.getvalue()
|
||||||
|
self.logger.info(text)
|
||||||
|
self.flush_option()
|
||||||
|
self.logger.info(self.as_markdown())
|
||||||
|
|
||||||
|
def setup(self) -> None:
|
||||||
|
self.client = client.Client(game=self, net_mode='local')
|
||||||
|
self.server = server.Server(net_mode='local')
|
||||||
|
|
||||||
|
def init(self, **kwargs) -> bool:
|
||||||
|
self.load_file()
|
||||||
|
self.setup()
|
||||||
|
self.log_env()
|
||||||
|
return True
|
||||||
|
|
||||||
def load_file(self) -> bool:
|
def load_file(self) -> bool:
|
||||||
"""加载文件"""
|
"""加载文件"""
|
||||||
|
self.logging_config = tools.load_file('configs/logger.toml')
|
||||||
self.init_logger()
|
self.init_logger()
|
||||||
self.init_mods()
|
self.init_mods()
|
||||||
|
self.init_console()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -66,9 +66,10 @@ class Options:
|
|||||||
if option not in self.cached_options:
|
if option not in self.cached_options:
|
||||||
raise OptionNameNotDefined(f"option: {option} with value: {value} is not defined")
|
raise OptionNameNotDefined(f"option: {option} with value: {value} is not defined")
|
||||||
setattr(self, option, value)
|
setattr(self, option, value)
|
||||||
|
run_load_file = True
|
||||||
if hasattr(self, 'init'):
|
if hasattr(self, 'init'):
|
||||||
self.init(**kwargs)
|
run_load_file = not self.init(**kwargs) # 默认 False/None
|
||||||
if hasattr(self, 'load_file'):
|
if hasattr(self, 'load_file') and run_load_file:
|
||||||
try:
|
try:
|
||||||
self.load_file()
|
self.load_file()
|
||||||
except Exception:
|
except Exception:
|
||||||
@ -78,8 +79,10 @@ class Options:
|
|||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
_options: Dict[str, Union[Callable, object]] = {}
|
_options: Dict[str, Union[Callable, object]] = {}
|
||||||
|
|
||||||
def init(self, **kwargs) -> None:
|
def init(self, **kwargs) -> bool:
|
||||||
""" 如果子类定义了这个函数,则会在 __init__ 之后调用这个函数 """
|
""" 如果子类定义了这个函数,则会在 __init__ 之后调用这个函数
|
||||||
|
返回值为 True 则不会调用 load_file 函数
|
||||||
|
"""
|
||||||
|
|
||||||
def load_file(self) -> bool:
|
def load_file(self) -> bool:
|
||||||
"""如果子类定义了这个函数,则会在 __init__ 和 init 之后再调用这个函数
|
"""如果子类定义了这个函数,则会在 __init__ 和 init 之后再调用这个函数
|
||||||
|
@ -27,7 +27,6 @@ level = "DEBUG"
|
|||||||
[handlers.file]
|
[handlers.file]
|
||||||
class = "logging.FileHandler"
|
class = "logging.FileHandler"
|
||||||
filename = "{} DR.log"
|
filename = "{} DR.log"
|
||||||
datefmt = "%Y-%m-%d %H:%M:%S"
|
|
||||||
encoding = "utf-8"
|
encoding = "utf-8"
|
||||||
formatter = "file"
|
formatter = "file"
|
||||||
level = "DEBUG"
|
level = "DEBUG"
|
||||||
|
@ -19,15 +19,15 @@ use pyo3::prelude::*;
|
|||||||
// const MOD_PATH: String = String::from("mods");
|
// const MOD_PATH: String = String::from("mods");
|
||||||
|
|
||||||
enum LoadState {
|
enum LoadState {
|
||||||
init,
|
Init,
|
||||||
wait_start,
|
WaitStart,
|
||||||
pre_start,
|
PreStart,
|
||||||
running,
|
Running,
|
||||||
clean,
|
Clean,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[pyfunction]
|
#[pyfunction]
|
||||||
fn get_version_str() -> String { "0.2.7.0".to_string() }
|
fn get_version_str() -> String { "0.2.8.0".to_string() }
|
||||||
|
|
||||||
#[pyfunction]
|
#[pyfunction]
|
||||||
fn test_call(py_obj: &PyAny) -> PyResult<bool> {
|
fn test_call(py_obj: &PyAny) -> PyResult<bool> {
|
||||||
|
@ -212,7 +212,7 @@ pub mod console {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn stop(&self) -> bool {
|
fn stop(&self) -> bool {
|
||||||
if let (Some(sender)) = &self.stop_sender {
|
if let Some(sender) = &self.stop_sender {
|
||||||
sender.send(()).unwrap();
|
sender.send(()).unwrap();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -221,7 +221,7 @@ pub mod console {
|
|||||||
|
|
||||||
fn get_command(&self) -> Option<String> {
|
fn get_command(&self) -> Option<String> {
|
||||||
// 获取输入
|
// 获取输入
|
||||||
if let (Some(receiver)) = &self.keyboard_input_receiver {
|
if let Some(receiver) = &self.keyboard_input_receiver {
|
||||||
if let Ok(string) = receiver.try_recv() {
|
if let Ok(string) = receiver.try_recv() {
|
||||||
return Some(string);
|
return Some(string);
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ from Difficult_Rocket.api.mod import ModInfo
|
|||||||
from Difficult_Rocket.api.types import Options
|
from Difficult_Rocket.api.types import Options
|
||||||
from Difficult_Rocket.client import ClientWindow
|
from Difficult_Rocket.client import ClientWindow
|
||||||
|
|
||||||
DR_rust_version = Version("0.2.7.0") # DR_mod 的 Rust 编写部分的兼容版本
|
DR_rust_version = Version("0.2.8.0") # DR_mod 的 Rust 编写部分的兼容版本
|
||||||
|
|
||||||
|
|
||||||
class _DR_mod_runtime(Options):
|
class _DR_mod_runtime(Options):
|
||||||
|
Loading…
Reference in New Issue
Block a user