Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.

Commit 8bed28f

Browse files
committed
wip
1 parent 9973275 commit 8bed28f

File tree

4 files changed

+86
-28
lines changed

4 files changed

+86
-28
lines changed

src/cdn.rs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ impl File {
2222

2323
/// Temporary (full) file path for downloading
2424
pub fn cache_path(&self) -> PathBuf {
25-
format!("{}/{}", &global::CACHE_DIR, self.cache_name()).into()
25+
format!(
26+
"{}/{}",
27+
utils::get_mutex(&global::CACHE_DIR).display(),
28+
self.cache_name()
29+
)
30+
.into()
2631
}
2732

2833
/// Human-readable file size
@@ -46,19 +51,18 @@ pub fn get_cdn_url() -> String {
4651
)
4752
}
4853

49-
/// Get info from CDN
50-
pub async fn get_info() -> Result<Info, Box<dyn std::error::Error>> {
54+
/// Try each CDN host and set the first working one
55+
async fn find_working_host() -> Result<(), Box<dyn std::error::Error>> {
5156
for host in global::CDN_HOSTS {
5257
println!("Checking {}", host);
5358
utils::set_mutex(&global::CDN_HOST, host.to_owned());
5459

5560
let url = format!("{}/info.json", get_cdn_url());
56-
5761
match http::quick_request(&url).await {
5862
Ok(response) => match serde_json::from_str::<Info>(&response) {
59-
Ok(info) => {
63+
Ok(_) => {
6064
println!("Successfully connected to {}", host);
61-
return Ok(info);
65+
return Ok(());
6266
}
6367
Err(e) => {
6468
println!("Invalid JSON from {}: {}", host, e);
@@ -71,10 +75,30 @@ pub async fn get_info() -> Result<Info, Box<dyn std::error::Error>> {
7175
}
7276
}
7377
}
74-
7578
Err("No CDN host is reachable or returned valid info".into())
7679
}
7780

81+
/// Get info from CDN
82+
pub async fn get_info() -> Result<Info, Box<dyn std::error::Error>> {
83+
async fn get_info_inner() -> Result<Info, Box<dyn std::error::Error>> {
84+
if let Some(host) = utils::get_mutex_opt(&global::CDN_HOST) {
85+
let url = format!("{}/info.json", get_cdn_url());
86+
match http::quick_request(&url).await {
87+
Ok(response) => match serde_json::from_str::<Info>(&response) {
88+
Ok(info) => return Ok(info),
89+
Err(e) => println!("Invalid JSON from {}: {}", host, e),
90+
},
91+
Err(e) => println!("Failed to get info from {}: {}", host, e),
92+
}
93+
}
94+
95+
find_working_host().await?;
96+
Box::pin(get_info()).await
97+
}
98+
99+
get_info_inner().await
100+
}
101+
78102
/// Filter files by game
79103
pub fn filter_files(files: Vec<File>, game: crate::game::Game) -> Vec<File> {
80104
files

src/global.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,29 @@ pub const GITHUB_OWNER: &str = "mxve";
1010
/// The repository of the launcher
1111
pub const GITHUB_REPO: &str = "alterware-launcher";
1212

13+
/// CDN hosts to iterate over
1314
pub const CDN_HOSTS: [&str; 2] = ["test.test", "cdn.getserve.rs"];
15+
16+
/// CDN url procotol (http/https)
1417
pub static CDN_PROTOCOL: OnceCell<Mutex<String>> = OnceCell::new();
18+
19+
/// CDN branch (sub dir)
1520
pub static CDN_BRANCH: OnceCell<Mutex<String>> = OnceCell::new();
21+
22+
/// Chosen CDN host
1623
pub static CDN_HOST: OnceCell<Mutex<String>> = OnceCell::new();
1724

25+
/// Game
1826
pub static GAME: OnceCell<Mutex<crate::game::Game>> = OnceCell::new();
27+
28+
/// Game directory
1929
pub static GAME_DIR: OnceCell<Mutex<PathBuf>> = OnceCell::new();
30+
31+
/// Game client
2032
pub static GAME_CLIENT: OnceCell<Mutex<Option<crate::game::Client>>> = OnceCell::new();
2133

22-
// TODO: Make this configurable
2334
/// The path to the download cache
24-
pub const CACHE_DIR: &str = "awtmp";
35+
pub static CACHE_DIR: OnceCell<Mutex<PathBuf>> = OnceCell::new();
2536

2637
/// User-agent for HTTP requests
2738
pub static USER_AGENT: Lazy<String> = Lazy::new(|| {

src/main.rs

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
6060
arg_path(&args, &["-p", "--path", "--game-path"])
6161
.unwrap_or(std::env::current_dir().unwrap()),
6262
);
63+
utils::set_mutex(
64+
&global::CACHE_DIR,
65+
arg_path(&args, &["--cache"]).unwrap_or(utils::get_mutex(&global::GAME_DIR).join("awtmp")),
66+
);
67+
utils::set_mutex_opt(&global::CDN_HOST, arg_value(&args, &["--cdn"]));
6368

6469
let client = args
6570
.iter()
@@ -68,30 +73,38 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
6873
if let Some(client) = client {
6974
utils::set_mutex(&global::GAME_CLIENT, Some(client));
7075
utils::set_mutex(&global::GAME, client.game());
71-
} else {
72-
if let Some(game) = game::detect_game(&utils::get_mutex(&global::GAME_DIR)) {
73-
utils::set_mutex(&global::GAME, game);
74-
println!("Game: {:?}", game);
76+
} else if let Some(game) = game::detect_game(&utils::get_mutex(&global::GAME_DIR)) {
77+
utils::set_mutex(&global::GAME, game);
78+
println!("Game: {:?}", game);
7579

76-
match game.clients().len() {
77-
0 => {
78-
println!("No clients found for game");
79-
return Ok(());
80-
}
81-
1 => {
82-
utils::set_mutex(&global::GAME_CLIENT, Some(game.clients()[0]));
83-
}
84-
_ => {
85-
println!("Multiple clients found for game, please specify one");
86-
return Ok(());
87-
}
80+
match game.clients().len() {
81+
0 => {
82+
println!("No clients found for game");
83+
return Ok(());
84+
}
85+
1 => {
86+
utils::set_mutex(&global::GAME_CLIENT, Some(game.clients()[0]));
87+
}
88+
_ => {
89+
println!("Multiple clients found for game, please specify one");
90+
return Ok(());
8891
}
89-
} else {
90-
println!("No game detected");
91-
return Ok(());
9292
}
93+
} else {
94+
println!("No game detected");
95+
return Ok(());
9396
}
9497

98+
println!(
99+
"Running with\nGame dir {}\nCache dir {}\nCDN host {}\nCDN protocol {}\nCDN branch {}\nGame {}",
100+
utils::get_mutex(&global::GAME_DIR).display(),
101+
utils::get_mutex(&global::CACHE_DIR).display(),
102+
utils::get_mutex(&global::CDN_HOST),
103+
utils::get_mutex(&global::CDN_PROTOCOL),
104+
utils::get_mutex(&global::CDN_BRANCH),
105+
utils::get_mutex(&global::GAME).name()
106+
);
107+
95108
let game = utils::get_mutex(&global::GAME);
96109
let clients = game.clients();
97110
println!("Clients: {:?}", clients);

src/utils.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,20 @@ pub fn set_mutex<T: Clone>(cell: &OnceCell<Mutex<T>>, value: T) {
1111
}
1212
}
1313

14+
pub fn set_mutex_opt<T: Clone>(cell: &OnceCell<Mutex<T>>, value: Option<T>) {
15+
if let Some(value) = value {
16+
set_mutex(cell, value);
17+
}
18+
}
19+
1420
pub fn get_mutex<T: Clone>(cell: &OnceCell<Mutex<T>>) -> T {
1521
cell.get()
1622
.expect("Failed to get once cell value")
1723
.lock()
1824
.expect("Failed to lock mutex")
1925
.clone()
2026
}
27+
28+
pub fn get_mutex_opt<T: Clone>(cell: &OnceCell<Mutex<T>>) -> Option<T> {
29+
cell.get().map(|m| m.lock().unwrap().clone())
30+
}

0 commit comments

Comments
 (0)