Skip to content

Commit

Permalink
v0.2.0: added --username and --detach options
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanHelianthicusDoe committed Sep 8, 2019
1 parent fd64224 commit a6b2765
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 34 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
[package]
name = "shticker_book_unwritten"
version = "0.1.1"
version = "0.2.0"
authors = ["Dr. Jonathan Helianthicus Doe, IV <[email protected]>"]
edition = "2018"
description = "Minimal CLI launcher for the Toontown Rewritten MMORPG"
repository = "https://github.com/JonathanHelianthicusDoe/shticker_book_unwritten"
readme = "README.md"
keywords = ["launcher", "ttr", "toontown", "rewritten", "unwritten"]
keywords = ["cli", "launcher", "ttr", "toontown", "rewritten"]
categories = ["games"]
license = "AGPL-3.0-or-later"

[dependencies]
bzip2 = "0.3.3"
clap = "2.33.0"
clap = { version = "2.33.0", default-features = false, features = ["suggestions", "vec_map"] }
reqwest = "0.9.20"
rpassword = "4.0.1"
serde = { version = "1.0.99", features = ["derive"] }
serde = { version = "1.0.100", features = ["derive"] }
serde_json = "1.0.40"
sha-1 = "0.8.1"

Expand Down
54 changes: 41 additions & 13 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,45 @@ const ABOUT_TEXT: &str = concat!(
">\n",
);

pub fn enter_command_mode<P: AsRef<Path>>(
pub fn enter_command_mode<'a, P: AsRef<Path>, U: Iterator<Item = &'a str>>(
config: &mut Config,
config_path: P,
client: &reqwest::Client,
maybe_usernames: Option<U>,
detach: bool,
) -> Result<(), Error> {
let mut children = Vec::new();
if let Some(usernames) = maybe_usernames {
for username in usernames {
if let Some(c) = login::login(
config,
&config_path,
client,
[username].iter().copied(),
)? {
if !detach {
children.push(c);
}

println!("Game launched successfully!");
}
}

if !detach {
println!();
}
};

if detach {
return Ok(());
}

println!(concat!(
"Welcome to ",
crate_name!(),
"! Type help or ? to get a list of commands.",
));

let mut command_buf = String::with_capacity(0x10);
let mut children = Vec::with_capacity(2);

'outer: loop {
print!("> ");
Expand All @@ -56,8 +82,7 @@ pub fn enter_command_mode<P: AsRef<Path>>(
// ^D
if command_buf.is_empty() {
println!();

break;
command_buf.push_str("quit");
}

let mut argv = command_buf
Expand All @@ -80,12 +105,12 @@ pub fn enter_command_mode<P: AsRef<Path>>(
} else if children.len() == 1 {
print!(
"Are you sure are you want to exit? There's still a \
game instance running! [y/n]\n> ",
game instance running. [y/n]\n> ",
);
} else {
print!(
"Are you sure are you want to exit? There are still \
{} game instances running! [y/n]\n> ",
{} game instances running. [y/n]\n> ",
children.len(),
);
}
Expand Down Expand Up @@ -150,10 +175,13 @@ pub fn enter_command_mode<P: AsRef<Path>>(
check_children(&mut children)?;
display_accounts(config, &children)?;
},
_ => println!(
"Unrecognized command. Type help or ? to get a list of \
commands.",
),
_ => {
check_children(&mut children)?;
println!(
"Unrecognized command. Type help or ? to get a list of \
commands.",
);
},
}
}

Expand Down Expand Up @@ -234,12 +262,12 @@ fn display_instances(instances: &[(String, process::Child, time::Instant)]) {
let pid = child.id();

print!("{} ", name);
for _ in 0..max_name_len.saturating_sub(name.len()) {
for _ in 0..max_name_len - name.len() {
print!(" ");
}

print!("| {} ", pid);
for _ in 0..max_pid_len.saturating_sub(count_decimal_digits(pid)) {
for _ in 0..max_pid_len - count_decimal_digits(pid) {
print!(" ");
}

Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl fmt::Display for Error {
match self {
Self::NoPossibleConfigPath => f.write_str(
"No config path was given, and the $XDG_CONFIG_HOME and \
$HOME environment variables are both empty",
$HOME environment variables are both unset or empty",
),
Self::BadConfigPath(bcp) =>
write!(f, "Bad config file path specified: {}", bcp.display()),
Expand Down
14 changes: 8 additions & 6 deletions src/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ pub fn login<'a, P: AsRef<Path>, A: Iterator<Item = &'a str>>(
}) {
(username, password.as_str())
} else {
password_buf =
rpassword::read_password_from_tty(Some("Password: "))
.map_err(Error::PasswordReadError)?;
print!("Password for {}: ", username);
io::stdout().flush().map_err(Error::StdoutError)?;
password_buf = rpassword::read_password_from_tty(None)
.map_err(Error::PasswordReadError)?;

(username, password_buf.as_str())
}
Expand All @@ -80,9 +81,10 @@ pub fn login<'a, P: AsRef<Path>, A: Iterator<Item = &'a str>>(
}) {
password.as_str()
} else {
password_buf =
rpassword::read_password_from_tty(Some("Password: "))
.map_err(Error::PasswordReadError)?;
print!("Password for {}: ", username_buf);
io::stdout().flush().map_err(Error::StdoutError)?;
password_buf = rpassword::read_password_from_tty(None)
.map_err(Error::PasswordReadError)?;

password_buf.as_str()
};
Expand Down
55 changes: 45 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,48 @@ fn run() -> Result<(), Error> {
)
.takes_value(false),
)
.arg(
Arg::with_name("username")
.short("u")
.long("username")
.help("Username(s) to immediately login with")
.long_help(
"If this option is supplied, then after (possibly) \
auto-updating, the game will be launched with these \
username(s). The password(s) will be prompted for as \
normal if they aren't saved. Then, if the login(s) \
succeed, command mode is entered (assuming -d is not \
supplied).",
)
.takes_value(true)
.multiple(true),
)
.arg(
Arg::with_name("detach")
.short("d")
.long("detach")
.help(
"Exit after auto-updating (and possibly launching, if -u \
is supplied)",
)
.long_help(
"After auto-updating (unless -n was supplied), and after \
launching the game (if -u was supplied), \
shticker_book_unwritten will simply exit. If the game \
was launched, then its process is thus orphaned. On \
POSIX, an orphan continues running normally and is \
reparented to the init process (actually, on Linux, the \
closest parent process marked as a subreaper).",
)
.takes_value(false),
)
.get_matches();

let (mut config, config_path) = config::get_config(
arg_matches.is_present("no-config"),
arg_matches.value_of("CONFIG_FILE"),
arg_matches.value_of("INSTALL_DIR"),
arg_matches.value_of("CACHE_DIR"),
arg_matches.value_of("config"),
arg_matches.value_of("install-dir"),
arg_matches.value_of("cache-dir"),
)?;

let client = ClientBuilder::new()
Expand All @@ -122,11 +157,11 @@ fn run() -> Result<(), Error> {
println!();
}

command::enter_command_mode(&mut config, &config_path, &client)?;

if !arg_matches.is_present("no-config") {
config::commit_config(&config, &config_path)?;
}

Ok(())
command::enter_command_mode(
&mut config,
&config_path,
&client,
arg_matches.values_of("username"),
arg_matches.is_present("detach"),
)
}

0 comments on commit a6b2765

Please sign in to comment.