Skip to content

Commit 095121e

Browse files
refactor: auto exit on server command finish
1 parent 9a03668 commit 095121e

4 files changed

Lines changed: 138 additions & 124 deletions

File tree

src/cli/cli.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,11 @@ pub(crate) struct CliOptions {
3838
#[clap(long)]
3939
no_wait: bool,
4040

41-
/// The maximum amount of milliseconds that program should wait for any game messages until it will automatically exit.
42-
/// This setting is ignored if --no-listen is set.
43-
/// If set to a negative number will wait indefinitely for user's input.
44-
/// Doesn't apply to scriptslog command.
45-
#[clap(long, short, default_value_t=-1)]
46-
response_timeout: i64,
41+
/// The maximum amount of milliseconds that program should wait for game response until it will automatically exit.
42+
/// This will be extended by any command that may specify that the game would need additional time for computation.
43+
/// This setting is ignored if --no-listen is set and doesn't apply to scriptslog command.
44+
#[clap(long, short, default_value_t=5000)]
45+
response_timeout: u64,
4746
}
4847

4948
#[derive(Subcommand)]

src/cli/response_handling.rs

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,34 @@ use rw3d_core::utils::{scripts_execute_formatter, scripts_root_path_formatter, m
66

77
pub(crate) trait HandleResponse {
88
fn handle_response(&mut self, response: WitcherPacket, verbose_print: bool);
9+
/// Whether the handler has finished the work
910
fn is_done(&self) -> bool;
11+
/// Max time for the next response to come in millis
12+
/// Used to indicate when the game needs to do some work before sending the response
13+
fn response_await_time(&self) -> u64;
1014
}
1115

1216

1317

1418
pub(crate) struct ScriptsReloadPrinter {
15-
has_finished: bool,
19+
max_compile_time: u64,
1620
warnings: Vec<String>,
17-
errors: Vec<String>
21+
errors: Vec<String>,
22+
is_compiling: bool,
23+
has_finished: bool
1824
}
1925

2026
impl ScriptsReloadPrinter {
27+
pub fn new(max_compile_time: u64) -> Self {
28+
ScriptsReloadPrinter {
29+
max_compile_time,
30+
warnings: Vec::new(),
31+
errors: Vec::new(),
32+
is_compiling: false,
33+
has_finished: false,
34+
}
35+
}
36+
2137
fn print_summary(&self) {
2238
println!("========== {} Errors, {} Warnings ==========\n", self.errors.len(), self.warnings.len());
2339

@@ -33,16 +49,6 @@ impl ScriptsReloadPrinter {
3349
}
3450
}
3551

36-
impl Default for ScriptsReloadPrinter {
37-
fn default() -> Self {
38-
ScriptsReloadPrinter {
39-
has_finished: false,
40-
warnings: Vec::new(),
41-
errors: Vec::new(),
42-
}
43-
}
44-
}
45-
4652
impl HandleResponse for ScriptsReloadPrinter {
4753
fn handle_response(&mut self, response: WitcherPacket, verbose_print: bool) {
4854
let msg = scripts_reload_formatter(&response);
@@ -61,7 +67,9 @@ impl HandleResponse for ScriptsReloadPrinter {
6167
ScriptsReloadResponseType::Finished(_) => {
6268
self.has_finished = true;
6369
}
64-
_ => {}
70+
ScriptsReloadResponseType::Log(s) => {
71+
self.is_compiling = s.contains("Compiling functions");
72+
}
6573
}
6674
}
6775

@@ -79,6 +87,14 @@ impl HandleResponse for ScriptsReloadPrinter {
7987
fn is_done(&self) -> bool {
8088
self.has_finished
8189
}
90+
91+
fn response_await_time(&self) -> u64 {
92+
if self.is_compiling {
93+
self.max_compile_time
94+
} else {
95+
500
96+
}
97+
}
8298
}
8399

84100

@@ -97,6 +113,10 @@ impl HandleResponse for ScriptsExecutePrinter {
97113
fn is_done(&self) -> bool {
98114
true // only one packet
99115
}
116+
117+
fn response_await_time(&self) -> u64 {
118+
500
119+
}
100120
}
101121

102122

@@ -115,6 +135,10 @@ impl HandleResponse for ScriptsRootpathPrinter {
115135
fn is_done(&self) -> bool {
116136
true // only one packet
117137
}
138+
139+
fn response_await_time(&self) -> u64 {
140+
500
141+
}
118142
}
119143

120144

@@ -133,6 +157,10 @@ impl HandleResponse for ModlistPrinter {
133157
fn is_done(&self) -> bool {
134158
true // only one packet
135159
}
160+
161+
fn response_await_time(&self) -> u64 {
162+
500
163+
}
136164
}
137165

138166

@@ -151,6 +179,10 @@ impl HandleResponse for OpcodePrinter {
151179
fn is_done(&self) -> bool {
152180
true // only one packet
153181
}
182+
183+
fn response_await_time(&self) -> u64 {
184+
500
185+
}
154186
}
155187

156188

@@ -169,4 +201,8 @@ impl HandleResponse for VarlistPrinter {
169201
fn is_done(&self) -> bool {
170202
true // only one packet
171203
}
204+
205+
fn response_await_time(&self) -> u64 {
206+
500
207+
}
172208
}

0 commit comments

Comments
 (0)