|
1 | 1 | use std::{thread, net::{Shutdown, TcpStream}, sync::mpsc::{Receiver, TryRecvError}, time::Duration, io::Write}; |
2 | 2 |
|
3 | 3 | use clap::Subcommand; |
4 | | -use rw3d_core::utils::ResponseFormatter; |
5 | 4 |
|
6 | | -use crate::{input_waiter::input_waiter, CliOptions}; |
| 5 | +use crate::{input_waiter::input_waiter, CliOptions, response_handling::{HandleResponse, ScriptsReloadHandler, ScriptsExecuteHandler, ScriptsRootpathHandler, ModlistHandler, OpcodeHandler, VarlistHandler}}; |
7 | 6 |
|
8 | 7 | /// Subcommands that require connection to game's socket and sending messages to it |
9 | 8 | #[derive(Subcommand)] |
@@ -75,34 +74,33 @@ pub(crate) fn handle_server_subcommand( cmd: ServerSubcommands, options: CliOpti |
75 | 74 | if !options.no_wait { thread::sleep( Duration::from_millis(1000) ) } |
76 | 75 | println!("Handling the command..."); |
77 | 76 |
|
78 | | - let formatter: ResponseFormatter; |
| 77 | + let handler: Box<dyn HandleResponse>; |
79 | 78 | let p = match cmd { |
80 | 79 | ServerSubcommands::Reload => { |
81 | | - //TODO needs some summary and maybe colored text for errors |
82 | | - // erros are not always displayed at the end and can be easily missed |
83 | | - formatter = rw3d_core::utils::scripts_reload_formatter; |
| 80 | + //TODO maybe colored text for errors |
| 81 | + handler = Box::new(ScriptsReloadHandler::default()); |
84 | 82 | rw3d_core::commands::scripts_reload() |
85 | 83 | } |
86 | 84 | ServerSubcommands::Exec { cmd } => { |
87 | | - formatter = rw3d_core::utils::scripts_execute_formatter; |
| 85 | + handler = Box::new(ScriptsExecuteHandler()); |
88 | 86 | rw3d_core::commands::scripts_execute(cmd) |
89 | 87 | } |
90 | 88 | ServerSubcommands::Rootpath => { |
91 | | - formatter = rw3d_core::utils::scripts_root_path_formatter; |
| 89 | + handler = Box::new(ScriptsRootpathHandler()); |
92 | 90 | rw3d_core::commands::scripts_root_path() |
93 | 91 | } |
94 | 92 | ServerSubcommands::Modlist => { |
95 | 93 | //TODO would be nice to have these mod actually sorted alphabetically at least |
96 | | - formatter = rw3d_core::utils::mod_list_formatter; |
| 94 | + handler = Box::new(ModlistHandler()); |
97 | 95 | rw3d_core::commands::mod_list() |
98 | 96 | } |
99 | 97 | ServerSubcommands::Opcode { func_name, class_name } => { |
100 | | - formatter = rw3d_core::utils::opcode_formatter; |
| 98 | + handler = Box::new(OpcodeHandler()); |
101 | 99 | rw3d_core::commands::opcode(func_name, class_name) |
102 | 100 | } |
103 | 101 | ServerSubcommands::Varlist { section, name } => { |
104 | 102 | //TODO would be nice to have some option to sort those values |
105 | | - formatter = rw3d_core::utils::var_list_formatter; |
| 103 | + handler = Box::new(VarlistHandler()); |
106 | 104 | rw3d_core::commands::var_list(section, name) |
107 | 105 | } |
108 | 106 | // ServerSubcommands::Varset { section, name, value } => { |
@@ -131,7 +129,7 @@ pub(crate) fn handle_server_subcommand( cmd: ServerSubcommands, options: CliOpti |
131 | 129 |
|
132 | 130 | // This function can either finish by itself by the means of response timeout |
133 | 131 | // or be stopped by input waiter thread if that one sends him a signal |
134 | | - read_responses(&mut stream, options.response_timeout, reader_rcv, options.verbose, formatter); |
| 132 | + read_responses(&mut stream, options.response_timeout, reader_rcv, options.verbose, handler); |
135 | 133 |
|
136 | 134 | } else { |
137 | 135 | // Wait a little bit to not finish the connection abruptly |
@@ -172,7 +170,7 @@ fn try_connect(ip: String, max_tries: u8, tries_delay_ms: u64) -> Option<TcpStre |
172 | 170 | None |
173 | 171 | } |
174 | 172 |
|
175 | | -fn read_responses(stream: &mut TcpStream, response_timeout: i64, cancel_token: Receiver<()>, verbose_print: bool, formatter: ResponseFormatter) { |
| 173 | +fn read_responses(stream: &mut TcpStream, response_timeout: i64, cancel_token: Receiver<()>, verbose_print: bool, mut handler: Box<dyn HandleResponse>) { |
176 | 174 | let mut peek_buffer = [0u8;6]; |
177 | 175 | let mut packet_available: bool; |
178 | 176 | let mut response_wait_elapsed: i64 = 0; |
@@ -204,10 +202,10 @@ fn read_responses(stream: &mut TcpStream, response_timeout: i64, cancel_token: R |
204 | 202 | if packet_available { |
205 | 203 | match rw3d_core::packet::WitcherPacket::from_stream(stream) { |
206 | 204 | Ok(packet) => { |
207 | | - if verbose_print { |
208 | | - println!("{:?}", packet); |
209 | | - } else { |
210 | | - println!("{}", formatter(&packet)); |
| 205 | + handler.handle(packet, verbose_print); |
| 206 | + |
| 207 | + if handler.should_exit() { |
| 208 | + break; |
211 | 209 | } |
212 | 210 | } |
213 | 211 | Err(e) => { |
|
0 commit comments