From 8a140087066ecf20c4db69e578a83ef1e496b991 Mon Sep 17 00:00:00 2001 From: mxve <68632137+mxve@users.noreply.github.com> Date: Sun, 10 Sep 2023 17:36:01 +0200 Subject: [PATCH] use config values --- src/config.rs | 11 +++-------- src/main.rs | 20 ++++++++++++++------ src/structs.rs | 12 +++++++++++- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/config.rs b/src/config.rs index 4c231b0..5599880 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,19 +2,14 @@ use crate::structs::Config; use std::{fs, path::PathBuf}; -const DEFAULT: Config = Config { - update_only: false, - skip_self_update: false, - bonus_content: false, -}; - pub fn load(config_path: PathBuf) -> Config { if config_path.exists() { let cfg = fs::read_to_string(&config_path).unwrap(); - let cfg: Config = serde_json::from_str(&cfg).unwrap_or(DEFAULT); + let cfg: Config = serde_json::from_str(&cfg).unwrap_or(Config::default()); return cfg; } - DEFAULT + save(config_path.clone(), Config::default()); + Config::default() } pub fn save(config_path: PathBuf, config: Config) { diff --git a/src/main.rs b/src/main.rs index 7abb46b..826d405 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +mod config; mod github; mod global; mod http; @@ -223,23 +224,30 @@ fn launch(file_path: &PathBuf) { fn main() { let mut args: Vec = std::env::args().collect(); + let mut cfg = config::load(PathBuf::from("alterware-launcher.json")); - let mut update_only = false; if args.contains(&String::from("update")) { - update_only = true; + cfg.update_only = true; args.iter() .position(|r| r == "update") .map(|e| args.remove(e)); } - if !args.contains(&String::from("skip-launcher-update")) { - self_update::run(update_only); + if !args.contains(&String::from("skip-launcher-update")) && !cfg.skip_self_update { + self_update::run(cfg.update_only); } else { args.iter() .position(|r| r == "skip-launcher-update") .map(|e| args.remove(e)); } + if args.contains(&String::from("bonus")) { + cfg.bonus_content = true; + args.iter() + .position(|r| r == "bonus") + .map(|e| args.remove(e)); + } + let games_json = http::get_body_string(format!("{}/games.json", MASTER).as_str()); let games: Vec = serde_json::from_str(&games_json).unwrap(); @@ -251,7 +259,7 @@ fn main() { for r in g.references.iter() { if std::path::Path::new(r).exists() { if g.client.len() > 1 { - if update_only { + if cfg.update_only { game = String::from(g.client[0]); break 'main; } @@ -279,7 +287,7 @@ fn main() { for c in g.client.iter() { if c == &game { update(g, &std::env::current_dir().unwrap()); - if !update_only { + if !cfg.update_only { launch(&PathBuf::from(format!("{}.exe", c))); } return; diff --git a/src/structs.rs b/src/structs.rs index 97bb5ab..ffc6011 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -13,9 +13,19 @@ pub struct Game<'a> { pub app_id: u32, } -#[derive(serde::Deserialize, serde::Serialize)] +#[derive(Default, serde::Deserialize, serde::Serialize)] pub struct Config { pub update_only: bool, pub skip_self_update: bool, pub bonus_content: bool, } + +// impl Default for Config { +// fn default() -> Self { +// Self { +// update_only: false, +// skip_self_update: false, +// bonus_content: false, +// } +// } +// }