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
|
2021-09-22 06:21:48 +08:00
|
|
|
import logging
|
2023-04-24 23:08:43 +08:00
|
|
|
import importlib
|
|
|
|
import importlib.util
|
2021-09-22 06:21:48 +08:00
|
|
|
import logging.config
|
|
|
|
import multiprocessing
|
2021-01-24 22:00:31 +08:00
|
|
|
|
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')
|
2021-01-23 21:43:04 +08:00
|
|
|
|
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
|
2022-06-18 17:53:35 +08:00
|
|
|
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
|
2021-09-22 06:21:48 +08:00
|
|
|
# logging config
|
2022-06-18 17:53:35 +08:00
|
|
|
log_config = tools.load_file('configs/logger.toml')
|
2021-09-22 06:21:48 +08:00
|
|
|
file_name = log_config['handlers']['file']['filename']
|
|
|
|
del log_config['handlers']['file']['datefmt']
|
2021-09-22 06:32:10 +08:00
|
|
|
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
|
2021-09-22 06:32:10 +08:00
|
|
|
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()
|
|
|
|
for mod_path in paths:
|
|
|
|
try:
|
|
|
|
self.logger.info(tr().main.mod.find.start().format(mod_path))
|
|
|
|
if mod_path.is_dir() and mod_path.name != '__pycache__':
|
|
|
|
if importlib.util.find_spec(mod_path.name) is not None:
|
|
|
|
mods.append(mod_path.name)
|
|
|
|
else:
|
|
|
|
self.logger.warning(tr().main.mod.load.failed.info().format(mod_path.name, tr().main.mod.find.failed.no_spce()))
|
|
|
|
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:
|
|
|
|
self.logger.warning(tr().main.mod.find.failed().format(mod_path, e))
|
|
|
|
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"):
|
|
|
|
self.logger.warning(tr().main.mod.load.failed.info().format(mod, tr().main.mod.load.failed.no_mod_class()))
|
|
|
|
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:
|
|
|
|
self.logger.warning(tr().main.mod.load.failed().format(mod, e))
|
|
|
|
self.logger.info(tr().main.mod.load.done())
|
|
|
|
self.loaded_mods = module
|
|
|
|
|
2021-08-24 22:31:52 +08:00
|
|
|
def setup(self) -> None:
|
2021-09-22 06:21:48 +08:00
|
|
|
self.client = client.Client(net_mode='local')
|
2022-12-25 23:15:49 +08:00
|
|
|
self.server = server.Server(net_mode='local')
|
2023-04-24 23:08:43 +08:00
|
|
|
# self.load_mods()
|
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())
|
2021-09-22 06:21:48 +08:00
|
|
|
self.logger.warning(warning)
|
2021-02-16 00:08:11 +08:00
|
|
|
|
2021-09-05 00:50:05 +08:00
|
|
|
# @new_thread('main')
|
2021-09-22 06:21:48 +08:00
|
|
|
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:
|
2021-09-22 06:21:48 +08:00
|
|
|
try:
|
2023-02-18 19:19:15 +08:00
|
|
|
game_process = multiprocessing.Process(target=self.client.start, name='pyglet app')
|
2021-09-22 06:21:48 +08:00
|
|
|
game_process.start()
|
|
|
|
game_process.join()
|
2023-01-27 21:09:37 +08:00
|
|
|
except Exception:
|
2021-09-22 06:21:48 +08:00
|
|
|
return -1
|
|
|
|
else:
|
|
|
|
return 1
|
2021-09-05 00:50:05 +08:00
|
|
|
else:
|
2021-09-22 06:21:48 +08:00
|
|
|
self.client.start()
|
2021-09-05 00:50:05 +08:00
|
|
|
|
|
|
|
def start(self) -> None:
|
|
|
|
self._start()
|