Skip to content

Commit

Permalink
v0.3.0: new -q option, updated logo
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanHelianthicusDoe committed Sep 9, 2019
1 parent f00c0c0 commit 656946e
Show file tree
Hide file tree
Showing 8 changed files with 279 additions and 149 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "shticker_book_unwritten"
version = "0.2.0"
version = "0.3.0"
authors = ["Dr. Jonathan Helianthicus Doe, IV <[email protected]>"]
edition = "2018"
description = "Minimal CLI launcher for the Toontown Rewritten MMORPG"
Expand Down
21 changes: 13 additions & 8 deletions img/shticker_book_unwritten.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified img/shticker_book_unwritten_256x256.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
102 changes: 63 additions & 39 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub fn enter_command_mode<'a, P: AsRef<Path>, U: Iterator<Item = &'a str>>(
config: &mut Config,
config_path: P,
client: &reqwest::Client,
quiet: bool,
maybe_usernames: Option<U>,
detach: bool,
) -> Result<(), Error> {
Expand All @@ -45,17 +46,20 @@ pub fn enter_command_mode<'a, P: AsRef<Path>, U: Iterator<Item = &'a str>>(
config,
&config_path,
client,
quiet,
[username].iter().copied(),
)? {
if !detach {
children.push(c);
}

println!("Game launched successfully!");
if !quiet {
println!("Game launched successfully!");
}
}
}

if !detach {
if !detach && !quiet {
println!();
}
};
Expand All @@ -64,11 +68,13 @@ pub fn enter_command_mode<'a, P: AsRef<Path>, U: Iterator<Item = &'a str>>(
return Ok(());
}

println!(concat!(
"Welcome to ",
crate_name!(),
"! Type help or ? to get a list of commands.",
));
if !quiet {
println!(concat!(
"Welcome to ",
crate_name!(),
"! Type help or ? to get a list of commands.",
));
}
let mut command_buf = String::with_capacity(0x10);

'outer: loop {
Expand All @@ -92,14 +98,14 @@ pub fn enter_command_mode<'a, P: AsRef<Path>, U: Iterator<Item = &'a str>>(
None => (),
Some("help") | Some("?") => {
help();
check_children(&mut children)?;
check_children(quiet, &mut children)?;
},
Some("about") => {
about();
check_children(&mut children)?;
check_children(quiet, &mut children)?;
},
Some("quit") | Some("exit") => {
check_children(&mut children)?;
check_children(quiet, &mut children)?;
if children.is_empty() {
break;
} else if children.len() == 1 {
Expand Down Expand Up @@ -137,9 +143,9 @@ pub fn enter_command_mode<'a, P: AsRef<Path>, U: Iterator<Item = &'a str>>(
}
},
Some("update") | Some("up") => {
check_children(&mut children)?;
check_children(quiet, &mut children)?;
if children.is_empty() {
update::update(config, client)?
update::update(config, client, quiet)?
} else if children.len() == 1 {
println!(
"There's still a game instance running, can't update \
Expand All @@ -155,28 +161,30 @@ pub fn enter_command_mode<'a, P: AsRef<Path>, U: Iterator<Item = &'a str>>(
},
Some("login") | Some("play") | Some("launch") => {
if let Some(c) =
login::login(config, &config_path, client, argv)?
login::login(config, &config_path, client, quiet, argv)?
{
children.push(c);

println!("Game launched successfully!");
if !quiet {
println!("Game launched successfully!");
}
}
check_children(&mut children)?;
check_children(quiet, &mut children)?;
},
Some("instances") | Some("running") => {
check_children(&mut children)?;
check_children(quiet, &mut children)?;
display_instances(&children);
},
Some("kill") | Some("close") => {
check_children(&mut children)?;
kill_instance(&mut children, argv.next())?;
check_children(quiet, &mut children)?;
kill_instance(quiet, &mut children, argv.next())?;
},
Some("accounts") | Some("logins") => {
check_children(&mut children)?;
check_children(quiet, &mut children)?;
display_accounts(config, &children)?;
},
_ => {
check_children(&mut children)?;
check_children(quiet, &mut children)?;
println!(
"Unrecognized command. Type help or ? to get a list of \
commands.",
Expand Down Expand Up @@ -280,6 +288,7 @@ fn display_instances(instances: &[(String, process::Child, time::Instant)]) {
}

fn kill_instance(
quiet: bool,
children: &mut Vec<(String, process::Child, time::Instant)>,
arg: Option<&str>,
) -> Result<(), Error> {
Expand Down Expand Up @@ -316,26 +325,35 @@ fn kill_instance(
let pid = child.id();
let uptime_sec = timestamp.elapsed().as_secs();

println!("Killing instance...");
if !quiet {
println!("Killing instance...");
}

if let Err(ioe) = child.kill() {
if ioe.kind() != io::ErrorKind::InvalidInput {
return Err(Error::ProcessKillError(ioe));
}
}

println!("Joining instance's thread...");
if !quiet {
println!("Joining instance's thread...");
}

child.wait().map_err(Error::ThreadJoinError)?;

println!("Successfully killed {}'s instance with pid {},", name, pid);
let secs = uptime_sec % 60;
let minutes = (uptime_sec / 60) % 60;
let hours = uptime_sec / (60 * 60);
println!(
"which had an approximate uptime of {}h {:02}m {:02}s.",
hours, minutes, secs,
);
if !quiet {
println!(
"Successfully killed {}'s instance with pid {},",
name, pid
);
let secs = uptime_sec % 60;
let minutes = (uptime_sec / 60) % 60;
let hours = uptime_sec / (60 * 60);
println!(
"which had an approximate uptime of {}h {:02}m {:02}s.",
hours, minutes, secs,
);
}

children.remove(i);
} else {
Expand Down Expand Up @@ -381,22 +399,28 @@ fn display_accounts(
/// Naïve implementation because, let's be real, how many instances of the game
/// are you really going to run concurrently?
fn check_children(
quiet: bool,
children: &mut Vec<(String, process::Child, time::Instant)>,
) -> Result<(), Error> {
let mut i = 0;
while let Some((username, child, _)) = children.get_mut(i) {
if let Some(exit_status) =
child.try_wait().map_err(Error::ThreadJoinError)?
{
if exit_status.success() {
println!("{}'s instance exited normally.", username);
} else if let Some(exit_code) = exit_status.code() {
println!(
"{}'s instance exited abnormally. Exit code: {}",
username, exit_code,
);
} else {
println!("{}'s instance was killed by a signal.", username);
if !quiet {
if exit_status.success() {
println!("{}'s instance exited normally.", username);
} else if let Some(exit_code) = exit_status.code() {
println!(
"{}'s instance exited abnormally. Exit code: {}",
username, exit_code,
);
} else {
println!(
"{}'s instance was killed by a signal.",
username,
);
}
}

children.remove(i);
Expand Down
9 changes: 9 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub fn get_config(
config_path: Option<&str>,
install_path: Option<&str>,
cache_path: Option<&str>,
quiet: bool,
) -> Result<(Config, PathBuf), Error> {
let inject_arg_values = |c| {
let c = if let Some(ip) = install_path {
Expand Down Expand Up @@ -96,6 +97,10 @@ pub fn get_config(
}
};

if !quiet {
println!("Using {} as the config path...", config_path.display());
}

match File::open(&config_path) {
Ok(f) => serde_json::from_reader(f)
.map_err(Error::DeserializeError)
Expand Down Expand Up @@ -129,6 +134,10 @@ pub fn get_config(
},
}
} else {
if !quiet {
println!("Not using any config file...");
}

Ok((
Config {
install_dir: PathBuf::from(install_path.ok_or_else(
Expand Down
Loading

0 comments on commit 656946e

Please sign in to comment.