Skip to content

Commit

Permalink
Windows NT support? Maybe!!
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanHelianthicusDoe committed Oct 13, 2019
1 parent 73ae749 commit 0e8e652
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 40 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.4.2"
version = "0.5.0"
authors = ["Dr. Jonathan Helianthicus Doe, IV <[email protected]>"]
edition = "2018"
description = "Minimal CLI launcher for the Toontown Rewritten MMORPG"
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ for the [Toontown Rewritten](https://www.toontownrewritten.com/)
Inspired by [Shticker Book
Rewritten](https://github.com/madsciencecoder/Shticker-Book-Rewritten).

Currently **only** built to support GNU/Linux, because I don&rsquo;t know much
about Windows NT nor about macOS. If you know something about either of those
platforms and want to help out, feel very free to submit a PR or to file an
issue with a description of what can be done to support the platform(s).
Currently **only** built to support GNU/Linux and Windows NT (using the MSVC
toolchain), because I don&rsquo;t know much about ~~Windows NT nor about~~
macOS. If you know something about macOS and want to help out, feel very free
to submit a PR or to file an issue with a description of what can be done to
support the platform. Mostly I just don&rsquo;t have a macOS machine that I
can test on.

## Installing

Expand Down
68 changes: 47 additions & 21 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,30 +71,56 @@ pub fn get_config(
let config_path = if let Some(s) = config_path {
PathBuf::from(s)
} else {
let mut xdg_config_home = String::new();
let mut home = String::new();

for (key, value) in env::vars() {
match key.as_str() {
"XDG_CONFIG_HOME" => xdg_config_home = value,
"HOME" => home = value,
_ =>
if !(home.is_empty() || xdg_config_home.is_empty()) {
break;
},
#[cfg(unix)]
{
let mut xdg_config_home = String::new();
let mut home = String::new();

for (key, value) in env::vars() {
match key.as_str() {
"XDG_CONFIG_HOME" => xdg_config_home = value,
"HOME" => home = value,
_ =>
if !(home.is_empty() || xdg_config_home.is_empty())
{
break;
},
}
}

if !xdg_config_home.is_empty() {
[xdg_config_home.as_str(), crate_name!(), "config.json"]
.iter()
.collect()
} else if !home.is_empty() {
[home.as_str(), ".config", crate_name!(), "config.json"]
.iter()
.collect()
} else {
return Err(Error::NoPossibleConfigPath);
}
}
#[cfg(windows)]
{
let mut appdata = String::new();

if !xdg_config_home.is_empty() {
[xdg_config_home.as_str(), crate_name!(), "config.json"]
.iter()
.collect()
} else if !home.is_empty() {
[home.as_str(), ".config", crate_name!(), "config.json"]
.iter()
.collect()
} else {
return Err(Error::NoPossibleConfigPath);
for (key, value) in env::vars() {
match key.as_str() {
"APPDATA" => appdata = value,
_ =>
if !appdata.is_empty() {
break;
},
}
}

if !appdata.is_empty() {
[appdata.as_str(), crate_name!(), "config.json"]
.iter()
.collect()
} else {
return Err(Error::NoPossibleConfigPath);
}
}
};

Expand Down
18 changes: 14 additions & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ pub enum Error {
FileRenameError(io::Error),
NotDir(PathBuf),
RemoveFileError(io::Error),
#[allow(dead_code)]
MissingFile(&'static str),
#[allow(dead_code)]
PermissionsSetError(io::Error),
MissingCommandLineArg(&'static str),
PasswordReadError(io::Error),
Expand All @@ -45,10 +47,18 @@ pub enum Error {
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::NoPossibleConfigPath => f.write_str(
"No config path was given, and the $XDG_CONFIG_HOME and \
$HOME environment variables are both unset or empty",
),
Self::NoPossibleConfigPath => {
#[cfg(unix)]
const MSG: &str = "No config path was given, and the \
$XDG_CONFIG_HOME and $HOME environment \
variables are both unset or empty";
#[cfg(windows)]
const MSG: &str = "No config path was given, and the \
%APPDATA% environment variable is unset \
or empty";

f.write_str(MSG)
},
Self::BadConfigPath(bcp) =>
write!(f, "Bad config file path specified: {}", bcp.display()),
Self::MkdirError(ioe) =>
Expand Down
15 changes: 14 additions & 1 deletion src/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,20 @@ fn launch<S: AsRef<OsStr>, T: AsRef<OsStr>>(
println!("Launching the game...");
}

process::Command::new("./TTREngine")
#[cfg(unix)]
let command_text = "./TTREngine";
#[cfg(windows)]
let command_text = {
// `.current_dir(&config.install_dir)` doesn't seem to work like it
// does on Linux, so this is just a (naive) way of making real sure
// that we are pointing at the right executable.
let mut command_buf = config.install_dir.clone();
command_buf.push("TTREngine.exe");

command_buf
};

process::Command::new(&command_text)
.current_dir(&config.install_dir)
.env("TTR_PLAYCOOKIE", play_cookie)
.env("TTR_GAMESERVER", game_server)
Expand Down
24 changes: 16 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ fn main() {
}

fn run() -> Result<(), Error> {
#[cfg(unix)]
const CONFIG_LONG_HELP: &str = concat!(
"Configuration JSON file to use. Defaults to \"$XDG_CONFIG_HOME\"/",
crate_name!(),
"/config.json and then to \"$HOME\"/.config/",
crate_name!(),
"/config.json",
);
#[cfg(windows)]
const CONFIG_LONG_HELP: &str = concat!(
r"Configuration JSON file to use. Defaults to %APPDATA%\",
crate_name!(),
r"\config.json",
);

let arg_matches = App::new(crate_name!())
.version(crate_version!())
.author(crate_authors!())
Expand All @@ -40,14 +55,7 @@ fn run() -> Result<(), Error> {
.aliases(&["conf", "configuration"])
.value_name("CONFIG_FILE")
.help("Configuration JSON file to use.")
.long_help(concat!(
"Configuration JSON file to use. Defaults to \
\"$XDG_CONFIG_HOME\"/",
crate_name!(),
"/config.json and then to \"$HOME\"/.config/",
crate_name!(),
"/config.json",
))
.long_help(CONFIG_LONG_HELP)
.takes_value(true)
.conflicts_with("no-config"),
)
Expand Down
3 changes: 2 additions & 1 deletion src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use sha1::{Digest, Sha1};
use std::{
fs::{self, File},
io::{self, prelude::*},
os::unix::fs::PermissionsExt,
path::{Path, PathBuf},
};

Expand Down Expand Up @@ -190,6 +189,8 @@ pub fn update(

#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;

if !quiet {
println!("Making sure TTREngine is executable...");
}
Expand Down

0 comments on commit 0e8e652

Please sign in to comment.