Difficult-Rocket/Difficult_Rocket/main.py

156 lines
5.8 KiB
Python
Raw Normal View History

2021-09-08 23:38:34 +08:00
# -------------------------------
# Difficult Rocket
2023-01-20 14:08:12 +08:00
# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com
2021-09-08 23:38:34 +08:00
# All rights reserved
# -------------------------------
2021-02-16 00:15:29 +08:00
"""
2020-12-05 21:24:54 +08:00
writen by shenjackyuanjie
2021-09-08 23:38:34 +08:00
mail: 3695888@qq.com
github: @shenjackyuanjie
gitee: @shenjackyuanjie
2021-02-16 00:15:29 +08:00
"""
2020-12-05 21:24:54 +08:00
2021-03-24 23:37:10 +08:00
import os
2021-02-16 00:08:11 +08:00
import sys
2021-01-25 12:22:55 +08:00
import time
import logging
2023-04-24 23:08:43 +08:00
import importlib
import importlib.util
import logging.config
import multiprocessing
2023-04-09 03:21:11 +08:00
from io import StringIO
2023-04-24 23:08:43 +08:00
from pathlib import Path
2023-04-09 03:21:11 +08:00
2021-08-24 22:31:52 +08:00
if __name__ == '__main__': # been start will not run this
sys.path.append('/bin/libs')
sys.path.append('/bin')
2023-04-24 23:08:43 +08:00
from Difficult_Rocket import client, server, DR_option, DR_runtime
2023-01-31 22:07:18 +08:00
2023-04-09 03:21:11 +08:00
from Difficult_Rocket.crash import write_info_to_cache
from Difficult_Rocket.utils import tools
from Difficult_Rocket.utils.translate import tr
2020-12-05 21:24:54 +08:00
2022-08-12 21:07:36 +08:00
2021-08-24 22:31:52 +08:00
class Game:
2020-12-05 21:24:54 +08:00
def __init__(self):
2021-01-25 17:01:54 +08:00
# basic config
2021-02-16 11:35:10 +08:00
self.on_python_v_info = sys.version_info
2021-02-26 19:05:24 +08:00
self.on_python_v = sys.version.split(' ')[0]
2021-02-15 23:52:56 +08:00
self.start_time = time.strftime('%Y-%m-%d %H-%M-%S', time.gmtime(time.time()))
2021-02-20 21:46:14 +08:00
# lang_config
2022-12-22 10:54:28 +08:00
self.language = tools.load_file('configs/main.toml', 'runtime')['language']
2022-08-12 21:07:36 +08:00
DR_option.language = self.language
# logging config
log_config = tools.load_file('configs/logger.toml')
file_name = log_config['handlers']['file']['filename']
del log_config['handlers']['file']['datefmt']
log_config['handlers']['file']['filename'] = f'logs/{file_name.format(self.start_time)}'
try:
logging.config.dictConfig(log_config)
self.logger = logging.getLogger('main')
2021-09-22 20:51:30 +08:00
except ValueError: # it should be no 'logs/' folder
os.mkdir('logs')
logging.config.dictConfig(log_config)
self.logger = logging.getLogger('main')
2023-02-03 20:31:44 +08:00
self.logger.info(tr().main.logger.mkdir())
2023-04-05 16:00:38 +08:00
self.logger.info(tr().language_set_to())
2023-02-03 20:31:44 +08:00
self.logger.info(tr().main.logger.created())
2021-02-16 11:35:10 +08:00
# version check
2023-04-09 03:21:11 +08:00
self.log_env()
2021-02-16 00:08:11 +08:00
self.python_version_check()
2023-04-24 23:08:43 +08:00
self.loaded_mods = []
# self.client = client.Client
# self.server = server.Server
2021-08-13 12:25:29 +08:00
self.setup()
2023-04-09 03:21:11 +08:00
def log_env(self) -> None:
cache_steam = StringIO()
write_info_to_cache(cache_steam)
text = cache_steam.getvalue()
self.logger.info(text)
2021-08-13 12:25:29 +08:00
2023-04-24 23:08:43 +08:00
def load_mods(self) -> None:
mods = []
paths = Path(DR_runtime.mod_path).iterdir()
2023-04-26 11:18:11 +08:00
sys.path.append(DR_runtime.mod_path)
2023-04-24 23:08:43 +08:00
for mod_path in paths:
try:
2023-04-26 11:18:11 +08:00
if mod_path.name == '__pycache__':
continue
2023-04-24 23:08:43 +08:00
self.logger.info(tr().main.mod.find.start().format(mod_path))
2023-04-26 11:18:11 +08:00
if mod_path.is_dir():
2023-04-24 23:08:43 +08:00
if importlib.util.find_spec(mod_path.name) is not None:
mods.append(mod_path.name)
else:
2023-04-26 11:18:11 +08:00
self.logger.warning(tr().main.mod.load.faild.info().format(mod_path.name, tr().main.mod.find.faild.no_spec()))
2023-04-24 23:08:43 +08:00
elif mod_path.suffix in ('.pyz', '.zip', '.pyd', '.py'):
if importlib.util.find_spec(mod_path.name) is not None:
mods.append(mod_path.name)
except ImportError as e:
2023-04-26 11:18:11 +08:00
self.logger.warning(tr().main.mod.find.faild().format(mod_path, e))
2023-04-24 23:08:43 +08:00
self.logger.info(tr().main.mod.find.done())
module = []
for mod in mods:
try:
self.logger.info(tr().main.mod.load.start().format(mod))
mod_module = importlib.import_module(mod)
if not hasattr(mod_module, "mod_class"):
2023-04-26 11:18:11 +08:00
self.logger.warning(tr().main.mod.load.faild.info().format(mod, tr().main.mod.load.faild.no_mod_class()))
2023-04-24 23:08:43 +08:00
del mod_module # 释放内存
continue
mod_class = mod_module.mod_class
mod_class = mod_class()
module.append(mod_class)
self.logger.info(tr().main.mod.load.info().format(mod_class.mod_id, mod_class.version))
except ImportError as e:
2023-04-26 11:18:11 +08:00
self.logger.warning(tr().main.mod.load.faild().format(mod, e))
2023-04-24 23:08:43 +08:00
self.logger.info(tr().main.mod.load.done())
self.loaded_mods = module
2023-04-26 11:18:11 +08:00
mod_list = []
for mod in module:
mod_list.append((mod.mod_id, mod.version))
DR_runtime.DR_Mod_List = mod_list
def dispatch_event(self, event_name: str, *args, **kwargs) -> None:
for mod in self.loaded_mods:
if hasattr(mod, event_name):
try:
getattr(mod, event_name)(*args, **kwargs)
except Exception as e:
self.logger.error(tr().main.mod.event.error().format(event_name, e, mod.mod_id))
2023-04-24 23:08:43 +08:00
2021-08-24 22:31:52 +08:00
def setup(self) -> None:
2023-04-24 23:35:00 +08:00
self.load_mods()
2023-04-26 11:18:11 +08:00
self.client = client.Client(game=self, net_mode='local')
self.server = server.Server(net_mode='local')
2021-02-06 16:41:54 +08:00
2021-08-24 22:31:52 +08:00
def python_version_check(self) -> None: # best 3.8+ and write at 3.8.10
2023-02-03 20:31:44 +08:00
self.logger.info(f"{tr().main.version.now_on()} {self.on_python_v}")
2021-02-16 11:35:10 +08:00
if self.on_python_v_info[0] == 2:
2023-02-03 20:31:44 +08:00
self.logger.critical(tr().main.version.need3p())
raise SystemError(tr().main.version.need3p())
2021-08-13 12:25:29 +08:00
elif self.on_python_v_info[1] < 8:
2023-02-03 20:31:44 +08:00
warning = tools.name_handler(tr.main.version.best38p())
self.logger.warning(warning)
2021-02-16 00:08:11 +08:00
2021-09-05 00:50:05 +08:00
# @new_thread('main')
def _start(self):
2022-12-25 23:15:49 +08:00
self.server.run()
2023-01-24 23:59:07 +08:00
if DR_option.use_multiprocess:
try:
2023-02-18 19:19:15 +08:00
game_process = multiprocessing.Process(target=self.client.start, name='pyglet app')
game_process.start()
game_process.join()
2023-01-27 21:09:37 +08:00
except Exception:
return -1
else:
return 1
2021-09-05 00:50:05 +08:00
else:
self.client.start()
2021-09-05 00:50:05 +08:00
def start(self) -> None:
self._start()