Skip to content

Commit 5071944

Browse files
committed
feat(tick): Add configurable tick option
Signed-off-by: dark0dave <[email protected]>
1 parent a876efd commit 5071944

File tree

5 files changed

+29
-23
lines changed

5 files changed

+29
-23
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,16 @@ These flags work for either of the above commands
333333
334334
> Example: --timeout 7200 (2 hours)
335335
336+
* -i, --tick <TICK>
337+
338+
339+
> What it does: This sets how long the program will wait between polling weidu.
340+
341+
> How to use it: Replace <TIMEOUT> with a number of milliseconds.
342+
343+
> Default: 500 (1/2 a second)
344+
345+
> Example: --tick 1000
336346
337347
* -u, --weidu-log-mode <WEIDU_LOG_MODE>
338348

src/config/args.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ pub(crate) struct Eet {
118118
pub(crate) options: Options,
119119
}
120120

121-
#[derive(Parser, Debug, PartialEq, Clone)]
121+
#[derive(Parser, Debug, PartialEq, Clone, Default)]
122122
pub(crate) struct Options {
123123
/// Absolute Path to weidu binary
124124
#[clap(
@@ -239,6 +239,10 @@ pub(crate) struct Options {
239239
value_parser = BoolishValueParser::new(),
240240
)]
241241
pub(crate) check_last_installed: bool,
242+
243+
/// Tick
244+
#[clap(env, short = 'i', long, default_value_t = 500)]
245+
pub(crate) tick: u64,
242246
}
243247

244248
fn parse_weidu_log_mode(arg: &str) -> Result<String, String> {
@@ -376,6 +380,7 @@ mod tests {
376380
download: true,
377381
overwrite: false,
378382
check_last_installed: false,
383+
tick: 500,
379384
},
380385
}),
381386
};
@@ -424,6 +429,7 @@ mod tests {
424429
download: expected_flag_value,
425430
overwrite: !expected_flag_value,
426431
check_last_installed: !expected_flag_value,
432+
tick: 500,
427433
},
428434
generate_directories: false,
429435
new_pre_eet_dir: None,

src/installers.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,7 @@ pub(crate) fn normal_install(
6464
copy_folder(mod_folder, game_directory.join(&weidu_mod.name))?;
6565
}
6666
log::info!("Installing mod {:?}", &weidu_mod);
67-
match install(
68-
&options.weidu_binary,
69-
&game_directory,
70-
parser_config.clone(),
71-
weidu_mod,
72-
&options.language,
73-
&options.weidu_log_mode,
74-
options.timeout,
75-
) {
67+
match install(&game_directory, parser_config.clone(), weidu_mod, options) {
7668
InstallationResult::Fail(message) => {
7769
return Err(format!(
7870
"Failed to install mod {}, error is '{}'",

src/weidu.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{
22
io::{BufRead, BufReader, ErrorKind, Write},
3-
path::{Path, PathBuf},
3+
path::Path,
44
process::{Child, ChildStdout, Command, Stdio},
55
sync::{
66
atomic::{AtomicUsize, Ordering},
@@ -12,14 +12,12 @@ use std::{
1212

1313
use crate::{
1414
component::Component,
15-
config::parser_config::ParserConfig,
15+
config::{args::Options, parser_config::ParserConfig},
1616
state::State,
1717
utils::{get_user_input, sleep},
1818
weidu_parser::parse_raw_output,
1919
};
2020

21-
const TICK: u64 = 1000;
22-
2321
fn generate_args(weidu_mod: &Component, weidu_log_mode: &str, language: &str) -> Vec<String> {
2422
format!("{mod_name}/{mod_tp_file} {weidu_log_mode} --force-install {component} --use-lang {game_lang} --language {mod_lang}",
2523
mod_name = weidu_mod.name,
@@ -41,16 +39,13 @@ pub(crate) enum InstallationResult {
4139
}
4240

4341
pub(crate) fn install(
44-
weidu_binary: &PathBuf,
4542
game_directory: &Path,
4643
parser_config: Arc<ParserConfig>,
4744
weidu_mod: &Component,
48-
language: &str,
49-
weidu_log_mode: &str,
50-
timeout: usize,
45+
options: &Options,
5146
) -> InstallationResult {
52-
let weidu_args = generate_args(weidu_mod, weidu_log_mode, language);
53-
let mut command = Command::new(weidu_binary);
47+
let weidu_args = generate_args(weidu_mod, &options.weidu_log_mode, &options.language);
48+
let mut command = Command::new(options.weidu_binary.clone());
5449
let weidu_process = command.current_dir(game_directory).args(weidu_args);
5550

5651
let child = weidu_process
@@ -60,13 +55,14 @@ pub(crate) fn install(
6055
.spawn()
6156
.expect("Failed to spawn weidu process");
6257

63-
handle_io(child, parser_config, timeout)
58+
handle_io(child, parser_config, options.timeout, options.tick)
6459
}
6560

6661
pub(crate) fn handle_io(
6762
mut child: Child,
6863
parser_config: Arc<ParserConfig>,
6964
timeout: usize,
65+
tick: u64,
7066
) -> InstallationResult {
7167
let log = Arc::new(RwLock::new(String::new()));
7268
let mut weidu_stdin = child.stdin.take().unwrap();
@@ -80,6 +76,7 @@ pub(crate) fn handle_io(
8076
wait_count.clone(),
8177
log.clone(),
8278
timeout,
79+
tick,
8380
);
8481

8582
loop {
@@ -133,7 +130,7 @@ pub(crate) fn handle_io(
133130
std::io::stdout().flush().expect("Failed to flush stdout");
134131

135132
wait_count.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
136-
sleep(TICK);
133+
sleep(tick);
137134

138135
std::io::stdout().flush().expect("Failed to flush stdout");
139136
}

src/weidu_parser.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub(crate) fn parse_raw_output(
2323
wait_count: Arc<AtomicUsize>,
2424
log: Arc<RwLock<String>>,
2525
timeout: usize,
26+
tick: u64,
2627
) {
2728
let mut current_state = ParserState::LookingForInterestingOutput;
2829
let mut question = String::new();
@@ -98,7 +99,7 @@ pub(crate) fn parse_raw_output(
9899
// there is no new weidu output and we are not waiting for any, so there is nothing to do
99100
}
100101
}
101-
sleep(100);
102+
sleep(tick);
102103
}
103104
Err(TryRecvError::Disconnected) => {
104105
sender

0 commit comments

Comments
 (0)