Difficult-Rocket/Difficult_Rocket/client/__init__.py

437 lines
15 KiB
Python
Raw Normal View History

2023-03-02 13:15:29 +08:00
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-20 21:46:14 +08:00
"""
2020-12-13 18:39:12 +08:00
writen by shenjackyuanjie
2021-09-08 23:38:34 +08:00
mail: 3695888@qq.com
github: @shenjackyuanjie
gitee: @shenjackyuanjie
2021-02-20 21:46:14 +08:00
"""
2022-12-25 23:15:49 +08:00
import inspect
# system function
2021-02-20 21:46:14 +08:00
import os
2021-04-10 22:51:08 +08:00
import time
import logging
2022-12-25 23:15:49 +08:00
import functools
2021-10-25 22:08:00 +08:00
import traceback
2023-02-21 23:04:10 +08:00
from typing import List, Callable
2021-09-24 00:04:56 +08:00
from decimal import Decimal
2021-08-24 22:31:52 +08:00
2022-12-11 10:39:05 +08:00
# third function
2023-01-01 10:58:10 +08:00
import rtoml
2022-12-11 10:39:05 +08:00
import pyglet
# from pyglet import gl
2023-01-23 13:54:05 +08:00
# from pyglet.gl import glClearColor
2022-12-11 10:39:05 +08:00
# from pyglet.libs.win32 import _user32
from pyglet.window import Window
from pyglet.window import key, mouse
# Difficult_Rocket function
2023-02-18 14:04:55 +08:00
from Difficult_Rocket.utils import tools
2023-01-23 13:54:05 +08:00
from Difficult_Rocket.api.types import Options
2022-05-11 11:11:39 +08:00
from Difficult_Rocket.command import line, tree
2022-04-26 22:05:58 +08:00
from Difficult_Rocket.utils.translate import tr
2022-12-26 11:46:05 +08:00
from Difficult_Rocket import DR_runtime, DR_option
2022-11-08 20:18:01 +08:00
from Difficult_Rocket.utils.new_thread import new_thread
2022-03-22 21:18:04 +08:00
from Difficult_Rocket.client.fps.fps_log import FpsLogger
from Difficult_Rocket.client.guis.widgets import InputBox
2023-04-05 16:00:38 +08:00
from Difficult_Rocket.exception.language import LanguageNotFound
2022-07-01 13:59:08 +08:00
from Difficult_Rocket.exception.command import CommandError
2022-12-26 11:46:05 +08:00
from Difficult_Rocket.client.render.sr1_ship import SR1ShipRender
from Difficult_Rocket.client.screen import BaseScreen, DRScreen, DRDEBUGScreen
2021-02-03 22:04:34 +08:00
2020-12-16 06:33:33 +08:00
2023-01-23 13:54:05 +08:00
class ClientOption(Options):
fps: int = 60
width: int = 1024
height: int = 768
file_drop: bool = True
fullscreen: bool = False
resizeable: bool = True
visible: bool = True
gui_scale: float = 1.0
2023-01-27 20:20:28 +08:00
caption: str = "Difficult Rocket v{DR_version}|DR_rs v{DR_Rust_get_version}"
2023-01-23 13:54:05 +08:00
def load_file(self) -> None:
2023-01-27 20:20:28 +08:00
file = tools.load_file('./configs/main.toml')
2023-02-09 14:47:39 +08:00
self.fps = int(file['runtime']['fps'])
self.width = int(file['window']['width'])
self.height = int(file['window']['height'])
self.fullscreen = tools.format_bool(file['window']['full_screen'])
self.resizeable = tools.format_bool(file['window']['resizable'])
self.gui_scale = float(file['window']['gui_scale'])
self.caption = DR_runtime.format(file['window']['caption'])
2023-01-23 13:54:05 +08:00
2021-08-13 12:25:29 +08:00
class Client:
def __init__(self, net_mode='local'):
2021-10-01 23:12:01 +08:00
start_time = time.time_ns()
# logging
2021-08-13 12:25:29 +08:00
self.logger = logging.getLogger('client')
2023-04-05 11:53:40 +08:00
self.logger.info(tr().client.setup.start())
2021-08-13 12:25:29 +08:00
# config
2023-02-09 14:47:39 +08:00
self.config = ClientOption()
# value
self.process_id = 'Client'
self.process_name = 'Client process'
2021-02-20 21:46:14 +08:00
self.process_pid = os.getpid()
self.net_mode = net_mode
2023-01-27 21:09:37 +08:00
file_drop = bool(
pyglet.compat_platform != 'darwin' or DR_option.pyglet_macosx_dev_test
)
2023-02-09 14:47:39 +08:00
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,
2023-01-21 11:48:07 +08:00
file_drops=file_drop)
2021-10-01 23:12:01 +08:00
end_time = time.time_ns()
self.use_time = end_time - start_time
if DR_option.use_DR_rust:
from libs.Difficult_Rocket_rs import read_ship_test, part_list_read_test
2023-04-09 03:21:11 +08:00
# part_list_read_test()
# read_ship_test()
self.logger.info(tr().client.setup.use_time().format(Decimal(self.use_time) / 1000000000))
self.logger.debug(tr().client.setup.use_time_ns().format(self.use_time))
2021-02-20 21:46:14 +08:00
def start(self):
2023-01-03 16:24:06 +08:00
DR_runtime.running = True
self.window.start_game() # 游戏启动
2021-08-24 22:31:52 +08:00
# TODO 写一下服务端启动相关,还是需要服务端啊
2021-01-11 21:58:51 +08:00
2022-05-11 11:11:39 +08:00
def pyglet_load_fonts_folder(folder) -> None:
file_folder_list = os.listdir(folder)
for obj in file_folder_list:
if os.path.isfile(os.path.join(folder, obj)):
if obj[-4:] == '.ttf':
pyglet.font.add_file(os.path.join(folder, obj))
else:
pyglet_load_fonts_folder(os.path.join(folder, obj))
2022-12-25 23:15:49 +08:00
def _call_screen_after(func: Callable) -> Callable:
@functools.wraps(func)
2023-04-19 00:44:41 +08:00
def warped(self: "ClientWindow", *args, **kwargs):
2022-12-25 23:15:49 +08:00
result = func(self, *args, **kwargs)
for a_screen in self.screen_list:
2023-04-19 00:44:41 +08:00
a_screen.window_pointer = self
# 提前帮子窗口设置好指针
2022-12-25 23:15:49 +08:00
if hasattr(a_screen, func.__name__):
2022-12-26 11:46:05 +08:00
try:
2023-04-19 00:42:31 +08:00
getattr(a_screen, func.__name__)(*args, **kwargs, window=self)
2023-01-27 21:09:37 +08:00
except Exception:
2022-12-26 11:46:05 +08:00
traceback.print_exc()
2022-12-25 23:15:49 +08:00
return result
warped.__signature__ = inspect.signature(func)
return warped
def _call_screen_before(func: Callable) -> Callable:
@functools.wraps(func)
2023-04-19 00:44:41 +08:00
def warped(self: "ClientWindow", *args, **kwargs):
2022-12-25 23:15:49 +08:00
for a_screen in self.screen_list:
2023-04-19 00:44:41 +08:00
a_screen.window_pointer = self
# 提前帮子窗口设置好指针
2022-12-25 23:15:49 +08:00
if hasattr(a_screen, func.__name__):
2022-12-26 11:46:05 +08:00
try:
2023-04-19 00:42:31 +08:00
getattr(a_screen, func.__name__)(*args, **kwargs, window=self)
2023-01-27 21:09:37 +08:00
except Exception:
2022-12-26 11:46:05 +08:00
traceback.print_exc()
2022-12-25 23:15:49 +08:00
result = func(self, *args, **kwargs)
return result
warped.__signature__ = inspect.signature(func)
return warped
2022-08-12 21:07:36 +08:00
2021-11-06 19:07:32 +08:00
class ClientWindow(Window):
def __init__(self, net_mode='local', *args, **kwargs):
2021-02-16 12:51:07 +08:00
"""
2022-07-16 20:20:23 +08:00
@param net_mode:
@param args:
@param kwargs:
2021-02-16 12:51:07 +08:00
"""
2022-07-16 20:20:23 +08:00
start_time = time.time_ns()
super().__init__(*args, **kwargs)
2021-01-25 12:22:55 +08:00
# logging
2021-08-13 12:25:29 +08:00
self.logger = logging.getLogger('client')
2023-04-05 11:53:40 +08:00
self.logger.info(tr().window.setup.start())
2021-02-03 22:04:34 +08:00
# value
self.net_mode = net_mode
self.run_input = False
2021-08-24 22:31:52 +08:00
# configs
2022-03-22 21:18:04 +08:00
self.set_icon(pyglet.image.load('./textures/icon.png'))
self.main_config = tools.load_file('./configs/main.toml')
self.game_config = tools.load_file('./configs/game.config')
2021-09-02 22:47:10 +08:00
# FPS
2021-11-06 19:07:32 +08:00
self.FPS = Decimal(int(self.main_config['runtime']['fps']))
2021-10-28 06:43:35 +08:00
self.SPF = Decimal('1') / self.FPS
2021-12-15 23:28:08 +08:00
self.fps_log = FpsLogger(stable_fps=int(self.FPS))
2021-02-20 21:46:14 +08:00
# batch
self.part_batch = pyglet.graphics.Batch()
self.label_batch = pyglet.graphics.Batch()
2021-08-24 22:31:52 +08:00
# frame
self.frame = pyglet.gui.Frame(self, order=20)
2021-10-01 23:12:01 +08:00
self.M_frame = pyglet.gui.MovableFrame(self, modifier=key.LCTRL)
2023-04-19 00:44:41 +08:00
self.screen_list: List[BaseScreen] = []
2021-02-03 22:04:34 +08:00
# setup
self.setup()
# 命令显示
self.command_group = pyglet.graphics.Group(0)
2022-05-11 11:11:39 +08:00
self.command_tree = tree.CommandTree(tree.DR_command)
self.input_box = InputBox(x=50, y=30, width=300,
2023-01-03 16:24:06 +08:00
batch=self.label_batch, text='') # 实例化
2022-12-29 10:13:20 +08:00
self.input_box.push_handlers(self)
self.input_box.set_handler('on_commit', self.on_input)
self.set_handlers(self.input_box)
2021-11-06 19:07:32 +08:00
self.input_box.enabled = True
# 设置刷新率
pyglet.clock.schedule_interval(self.draw_update, float(self.SPF))
# 完成设置后的信息输出
2023-02-03 20:31:44 +08:00
self.logger.info(tr().window.os.pid_is().format(os.getpid(), os.getppid()))
2021-10-01 23:12:01 +08:00
end_time = time.time_ns()
self.use_time = end_time - start_time
2022-11-26 21:48:55 +08:00
DR_runtime.client_setup_cause_ns = self.use_time
2023-02-03 20:31:44 +08:00
self.logger.info(tr().window.setup.use_time().format(Decimal(self.use_time) / 1000000000))
self.logger.debug(tr().window.setup.use_time_ns().format(self.use_time))
self.count = 0
2021-09-02 22:47:10 +08:00
2020-12-16 06:33:33 +08:00
def setup(self):
2021-11-06 19:07:32 +08:00
self.load_fonts()
2023-04-19 00:42:31 +08:00
# TODO 读取配置文件,加载不同的屏幕,解耦
self.screen_list.append(DRDEBUGScreen(self))
self.screen_list.append(DRScreen(self))
2023-02-12 12:26:18 +08:00
self.screen_list.append(SR1ShipRender(self))
2021-10-01 23:12:01 +08:00
def load_fonts(self) -> None:
2021-11-06 19:07:32 +08:00
fonts_folder_path = self.main_config['runtime']['fonts_folder']
# 加载字体路径
2022-05-11 11:11:39 +08:00
# 淦,还写了个递归来处理
pyglet_load_fonts_folder(fonts_folder_path)
2021-10-01 23:12:01 +08:00
def start_game(self) -> None:
self.run_input = True
self.read_input()
pyglet.app.event_loop.run(1 / self.main_config['runtime']['fps'])
2021-10-25 22:08:00 +08:00
@new_thread('window read_input', daemon=True)
def read_input(self):
self.logger.debug('read_input start')
while self.run_input:
2023-02-18 19:19:15 +08:00
get = input(">")
2021-10-23 17:01:59 +08:00
if get in ('', ' ', '\n', '\r'):
continue
if get == 'stop':
self.run_input = False
2021-10-25 22:08:00 +08:00
try:
2021-11-06 19:07:32 +08:00
self.on_command(line.CommandText(get))
2021-10-31 23:26:32 +08:00
except CommandError:
2021-10-25 22:08:00 +08:00
self.logger.error(traceback.format_exc())
self.logger.debug('read_input end')
2021-10-25 22:08:00 +08:00
@new_thread('window save_info')
def save_info(self):
2023-04-06 15:31:21 +08:00
self.logger.info(tr().client.config.save.start())
2022-03-22 23:20:07 +08:00
config_file = tools.load_file('./configs/main.toml')
2022-03-22 21:18:04 +08:00
config_file['window']['width'] = self.width
config_file['window']['height'] = self.height
2023-04-05 16:00:38 +08:00
config_file['runtime']['language'] = DR_runtime.language
2023-01-01 10:58:10 +08:00
rtoml.dump(config_file, open('./configs/main.toml', 'w'))
2023-04-06 15:31:21 +08:00
self.logger.info(tr().client.config.save.done())
2021-10-25 22:08:00 +08:00
"""
draws and some event
"""
2021-01-29 14:07:40 +08:00
2022-12-25 23:15:49 +08:00
@_call_screen_after
def draw_update(self, tick: float):
2021-10-25 22:08:00 +08:00
decimal_tick = Decimal(str(tick)[:10])
2022-06-04 11:08:30 +08:00
now_FPS = pyglet.clock.get_frequency()
2022-12-25 23:15:49 +08:00
self.fps_log.update_tick(now_FPS, decimal_tick)
2021-02-21 11:58:47 +08:00
2022-12-25 23:15:49 +08:00
@_call_screen_after
2021-11-06 19:07:32 +08:00
def on_draw(self, *dt):
2022-03-22 23:20:07 +08:00
# self.logger.debug('on_draw call dt: {}'.format(dt))
2022-12-08 09:53:22 +08:00
pyglet.gl.glClearColor(0.1, 0, 0, 0.0)
2021-02-22 21:32:13 +08:00
self.clear()
2021-02-20 21:46:14 +08:00
self.draw_batch()
2022-12-25 23:15:49 +08:00
@_call_screen_after
2021-09-02 22:47:10 +08:00
def on_resize(self, width: int, height: int):
super().on_resize(width, height)
2022-12-25 23:15:49 +08:00
@_call_screen_after
def on_refresh(self, dt):
...
2022-12-25 23:15:49 +08:00
@_call_screen_after
2022-12-08 09:53:22 +08:00
def on_show(self):
# HWND_TOPMOST = -1
# _user32.SetWindowPos(self._hwnd, HWND_TOPMOST, 0, 0, self.width, self.height, 0)
...
2022-12-25 23:15:49 +08:00
@_call_screen_after
2022-12-08 09:53:22 +08:00
def on_hide(self):
# self.set_location(*self.get_location())
print('on hide!')
2022-12-25 23:15:49 +08:00
@_call_screen_after
2021-02-20 21:46:14 +08:00
def draw_batch(self):
self.part_batch.draw()
self.label_batch.draw()
"""
command line event
"""
2023-01-24 23:59:07 +08:00
def on_input(self, message: str) -> None:
command_text = line.CommandText(message)
self.on_command(command_text)
self.input_box.value = ''
2022-12-25 23:15:49 +08:00
@_call_screen_after
2021-11-06 19:07:32 +08:00
def on_command(self, command: line.CommandText):
2023-02-18 19:19:15 +08:00
print(command.re_match('/'))
self.logger.info(tr().window.command.text().format(command))
2023-01-22 09:17:01 +08:00
if command.re_match('stop'):
2022-12-25 23:15:49 +08:00
# self.dispatch_event('on_exit')
pyglet.app.platform_event_loop.stop()
2021-10-23 17:01:59 +08:00
self.dispatch_event('on_close', 'command') # source = command
2023-01-22 09:17:01 +08:00
elif command.re_match('fps'):
if command.re_match('log'):
2021-11-06 19:07:32 +08:00
self.logger.debug(self.fps_log.fps_list)
2023-01-22 09:17:01 +08:00
elif command.re_match('max'):
2021-11-06 19:07:32 +08:00
self.logger.info(self.fps_log.max_fps)
self.command.push_line(self.fps_log.max_fps, block_line=True)
2023-01-22 09:17:01 +08:00
elif command.re_match('min'):
2021-11-06 19:07:32 +08:00
self.logger.info(self.fps_log.min_fps)
self.command.push_line(self.fps_log.min_fps, block_line=True)
2023-01-22 09:17:01 +08:00
elif command.re_match('default'):
2022-12-25 23:15:49 +08:00
self.set_size(int(self.main_config['window_default']['width']),
int(self.main_config['window_default']['height']))
2023-04-05 16:00:38 +08:00
elif command.re_match('lang'):
try:
lang = command.text[5:]
tr._language = lang
self.logger.info(tr().language_set_to())
except LanguageNotFound:
self.logger.info(tr().language_available().format(os.listdir('./configs/lang')))
self.save_info()
2023-01-25 01:39:50 +08:00
# self.command_tree.parse(command.plain_command)
2022-12-25 23:15:49 +08:00
@_call_screen_after
2021-10-31 23:26:32 +08:00
def on_message(self, message: line.CommandLine.text):
self.logger.info(tr().window.message.text().format(message))
"""
2021-01-28 22:50:28 +08:00
keyboard and mouse input
"""
2021-01-28 22:50:28 +08:00
2022-12-25 23:15:49 +08:00
@_call_screen_after
def on_activate(self):
2022-07-01 13:59:08 +08:00
super().activate()
2022-12-25 23:15:49 +08:00
@_call_screen_after
def on_deactivate(self):
...
2023-01-19 22:29:43 +08:00
@_call_screen_before
def on_move(self, x, y):
...
2022-12-25 23:15:49 +08:00
@_call_screen_after
2021-08-13 12:25:29 +08:00
def on_mouse_motion(self, x, y, dx, dy) -> None:
2023-02-09 14:47:39 +08:00
...
2020-12-16 06:33:33 +08:00
2023-01-19 22:29:43 +08:00
@_call_screen_after
def on_mouse_scroll(self, x, y, scroll_x, scroll_y):
...
@_call_screen_after
def on_mouse_enter(self, x, y):
...
@_call_screen_after
def on_mouse_leave(self, x, y):
...
2022-12-25 23:15:49 +08:00
@_call_screen_after
2021-08-13 12:25:29 +08:00
def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers) -> None:
2023-02-09 14:47:39 +08:00
...
2022-12-25 23:15:49 +08:00
@_call_screen_after
2021-08-13 12:25:29 +08:00
def on_mouse_press(self, x, y, button, modifiers) -> None:
2023-01-27 21:09:37 +08:00
self.logger.debug(
2023-02-03 20:31:44 +08:00
tr().window.mouse.press().format(
[x, y], tr().window.mouse[mouse.buttons_string(button)]()
2023-01-27 21:09:37 +08:00
)
)
2021-08-13 12:25:29 +08:00
2022-12-25 23:15:49 +08:00
@_call_screen_after
2021-08-13 12:25:29 +08:00
def on_mouse_release(self, x, y, button, modifiers) -> None:
2023-01-27 21:09:37 +08:00
self.logger.debug(
2023-02-03 20:31:44 +08:00
tr().window.mouse.release().format(
[x, y], tr().window.mouse[mouse.buttons_string(button)]()
2023-01-27 21:09:37 +08:00
)
)
2022-12-25 23:15:49 +08:00
@_call_screen_after
2021-08-13 12:25:29 +08:00
def on_key_press(self, symbol, modifiers) -> None:
2021-02-22 23:17:16 +08:00
if symbol == key.ESCAPE and not (modifiers & ~(key.MOD_NUMLOCK |
key.MOD_CAPSLOCK |
key.MOD_SCROLLLOCK)):
self.dispatch_event('on_close')
2023-02-17 23:44:19 +08:00
if symbol == key.SLASH:
self.input_box._set_focus(True)
2022-12-25 23:15:49 +08:00
self.logger.debug(
2023-02-03 20:31:44 +08:00
tr().window.key.press().format(key.symbol_string(symbol), key.modifiers_string(modifiers)))
2020-12-16 06:33:33 +08:00
2022-12-25 23:15:49 +08:00
@_call_screen_after
2021-08-13 12:25:29 +08:00
def on_key_release(self, symbol, modifiers) -> None:
2022-12-25 23:15:49 +08:00
self.logger.debug(
2023-02-03 20:31:44 +08:00
tr().window.key.release().format(key.symbol_string(symbol), key.modifiers_string(modifiers)))
2022-12-25 23:15:49 +08:00
@_call_screen_after
def on_file_drop(self, x, y, paths):
...
2022-12-25 23:15:49 +08:00
@_call_screen_after
def on_text(self, text):
if text == '\r':
2023-02-03 20:31:44 +08:00
self.logger.debug(tr().window.text.new_line())
else:
2023-02-03 20:31:44 +08:00
self.logger.debug(tr().window.text.input().format(text))
2021-11-06 19:07:32 +08:00
if text == 't':
self.input_box.enabled = True
2021-08-13 12:25:29 +08:00
2022-12-25 23:15:49 +08:00
@_call_screen_after
def on_text_motion(self, motion):
motion_string = key.motion_string(motion)
self.logger.debug(tr().window.text.motion().format(motion_string))
2022-12-25 23:15:49 +08:00
@_call_screen_after
def on_text_motion_select(self, motion):
motion_string = key.motion_string(motion)
self.logger.debug(tr().window.text.motion_select().format(motion_string))
2022-12-25 23:15:49 +08:00
@_call_screen_before
2021-10-23 17:01:59 +08:00
def on_close(self, source: str = 'window') -> None:
self.logger.info(tr().window.game.stop_get().format(tr().game[source]()))
2023-02-03 20:31:44 +08:00
self.logger.info(tr().window.game.stop())
2021-10-29 00:15:31 +08:00
self.fps_log.check_list = False
2022-12-25 23:15:49 +08:00
DR_runtime.running = False
2021-10-23 17:01:59 +08:00
if self.run_input:
self.run_input = False
2021-10-25 22:08:00 +08:00
self.save_info()
super().on_close()
2023-02-03 20:31:44 +08:00
self.logger.info(tr().window.game.end())