From 0fd7bcf0f59bbf96f769474186af0dca417fb657 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Sun, 18 Jun 2023 15:42:35 +0800 Subject: [PATCH] fix issue on command --- Difficult_Rocket/client/__init__.py | 23 ++++++++++++++++--- Difficult_Rocket/main.py | 3 +++ configs/main.toml | 4 ++-- mods/dr_game/Difficult_Rocket_rs/__init__.py | 1 + .../Difficult_Rocket_rs/src/src/python.rs | 12 ++++++++-- mods/dr_game/console.py | 3 +++ 6 files changed, 39 insertions(+), 7 deletions(-) diff --git a/Difficult_Rocket/client/__init__.py b/Difficult_Rocket/client/__init__.py index 40e4e81..ad88c71 100644 --- a/Difficult_Rocket/client/__init__.py +++ b/Difficult_Rocket/client/__init__.py @@ -15,7 +15,7 @@ import traceback from pathlib import Path from decimal import Decimal -from typing import Callable, Dict, List, TYPE_CHECKING +from typing import Callable, Dict, List, TYPE_CHECKING, Optional # third function import rtoml @@ -122,6 +122,17 @@ def pyglet_load_fonts_folder(folder) -> None: pyglet_load_fonts_folder(os.path.join(folder, obj)) +def _call_back(call_back: Callable) -> Callable: + def wrapper(func): + @functools.wraps(func) + def warp(self: "ClientWindow", *args, **kwargs): + result = func(self, *args, **kwargs) + call_back(self) + return result + return warp + return wrapper + + def _call_screen_after(func: Callable) -> Callable: @functools.wraps(func) def warped(self: "ClientWindow", *args, **kwargs): @@ -306,8 +317,13 @@ class ClientWindow(Window): self.on_command(command_text) self.input_box.value = '' + def new_command(self): + self.game.console.new_command() + + @_call_back(new_command) @_call_screen_after def on_command(self, command: line.CommandText): + command.text = command.text.rstrip('\n') self.logger.info(tr().window.command.text().format(command)) command.find('/') if command.find('stop'): @@ -335,8 +351,9 @@ class ClientWindow(Window): except LanguageNotFound: self.logger.info(tr().language_available().format(os.listdir('./configs/lang'))) self.save_info() - - # self.command_tree.parse(command.plain_command) + elif command.find('mods'): + for mod in self.game.mod_module: + self.logger.info(f"mod: {mod.name} id: {mod.mod_id} version: {mod.version}") @_call_screen_after def on_message(self, message: line.CommandText): diff --git a/Difficult_Rocket/main.py b/Difficult_Rocket/main.py index 2b05854..3d74f74 100644 --- a/Difficult_Rocket/main.py +++ b/Difficult_Rocket/main.py @@ -65,6 +65,9 @@ class Console(Options): def get_command(self) -> Optional[str]: return self.caches.pop(0) if self.caches else None + def new_command(self) -> None: + return None + class Game(Options): name = 'MainGame' diff --git a/configs/main.toml b/configs/main.toml index a6a0078..2d17a1e 100644 --- a/configs/main.toml +++ b/configs/main.toml @@ -7,8 +7,8 @@ fonts_folder = "libs/fonts" [window] style = "None" -width = 2175 -height = 1808 +width = 1094 +height = 1173 visible = true gui_scale = 1 caption = "Difficult Rocket v{DR_version}" diff --git a/mods/dr_game/Difficult_Rocket_rs/__init__.py b/mods/dr_game/Difficult_Rocket_rs/__init__.py index 5600df5..ab367f7 100644 --- a/mods/dr_game/Difficult_Rocket_rs/__init__.py +++ b/mods/dr_game/Difficult_Rocket_rs/__init__.py @@ -128,3 +128,4 @@ if TYPE_CHECKING: def start(self) -> None: ... def stop(self) -> bool: ... def get_command(self) -> Optional[str]: ... + def new_command(self) -> bool: ... diff --git a/mods/dr_game/Difficult_Rocket_rs/src/src/python.rs b/mods/dr_game/Difficult_Rocket_rs/src/src/python.rs index 34f320f..f745414 100644 --- a/mods/dr_game/Difficult_Rocket_rs/src/src/python.rs +++ b/mods/dr_game/Difficult_Rocket_rs/src/src/python.rs @@ -228,6 +228,7 @@ pub mod translate { pub mod console { use pyo3::prelude::*; + use std::io::{self, Write}; #[pyclass] #[pyo3(name = "Console_rs")] @@ -251,7 +252,7 @@ pub mod console { let (stop_sender, stop_receiver) = std::sync::mpsc::channel(); let (keyboard_input_sender, keyboard_input_receiver) = std::sync::mpsc::channel(); std::thread::spawn(move || { - let std_in = std::io::stdin(); + let std_in = io::stdin(); loop { if let Ok(()) = stop_receiver.try_recv() { break; @@ -261,9 +262,10 @@ pub mod console { if !input.is_empty() { keyboard_input_sender.send(input).unwrap(); } - print!(">>"); } }); + print!("rs>"); + io::stdout().flush().unwrap(); self.stop_sender = Some(stop_sender); self.keyboard_input_receiver = Some(keyboard_input_receiver); } @@ -276,6 +278,12 @@ pub mod console { false } + fn new_command(&self) -> bool { + print!("rs>"); + io::stdout().flush().unwrap(); + true + } + fn get_command(&self) -> Option { // 获取输入 if let Some(receiver) = &self.keyboard_input_receiver { diff --git a/mods/dr_game/console.py b/mods/dr_game/console.py index 62f88f8..ede0559 100644 --- a/mods/dr_game/console.py +++ b/mods/dr_game/console.py @@ -22,3 +22,6 @@ class RustConsole(Console): def get_command(self) -> str: return self.console.get_command() + + def new_command(self) -> None: + self.console.new_command()