use config values

This commit is contained in:
mxve 2023-09-10 17:36:01 +02:00
parent ad7e78ec47
commit 8a14008706
3 changed files with 28 additions and 15 deletions

View File

@ -2,19 +2,14 @@ use crate::structs::Config;
use std::{fs, path::PathBuf}; 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 { pub fn load(config_path: PathBuf) -> Config {
if config_path.exists() { if config_path.exists() {
let cfg = fs::read_to_string(&config_path).unwrap(); 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; return cfg;
} }
DEFAULT save(config_path.clone(), Config::default());
Config::default()
} }
pub fn save(config_path: PathBuf, config: Config) { pub fn save(config_path: PathBuf, config: Config) {

View File

@ -1,3 +1,4 @@
mod config;
mod github; mod github;
mod global; mod global;
mod http; mod http;
@ -223,23 +224,30 @@ fn launch(file_path: &PathBuf) {
fn main() { fn main() {
let mut args: Vec<String> = std::env::args().collect(); let mut args: Vec<String> = 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")) { if args.contains(&String::from("update")) {
update_only = true; cfg.update_only = true;
args.iter() args.iter()
.position(|r| r == "update") .position(|r| r == "update")
.map(|e| args.remove(e)); .map(|e| args.remove(e));
} }
if !args.contains(&String::from("skip-launcher-update")) { if !args.contains(&String::from("skip-launcher-update")) && !cfg.skip_self_update {
self_update::run(update_only); self_update::run(cfg.update_only);
} else { } else {
args.iter() args.iter()
.position(|r| r == "skip-launcher-update") .position(|r| r == "skip-launcher-update")
.map(|e| args.remove(e)); .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_json = http::get_body_string(format!("{}/games.json", MASTER).as_str());
let games: Vec<Game> = serde_json::from_str(&games_json).unwrap(); let games: Vec<Game> = serde_json::from_str(&games_json).unwrap();
@ -251,7 +259,7 @@ fn main() {
for r in g.references.iter() { for r in g.references.iter() {
if std::path::Path::new(r).exists() { if std::path::Path::new(r).exists() {
if g.client.len() > 1 { if g.client.len() > 1 {
if update_only { if cfg.update_only {
game = String::from(g.client[0]); game = String::from(g.client[0]);
break 'main; break 'main;
} }
@ -279,7 +287,7 @@ fn main() {
for c in g.client.iter() { for c in g.client.iter() {
if c == &game { if c == &game {
update(g, &std::env::current_dir().unwrap()); update(g, &std::env::current_dir().unwrap());
if !update_only { if !cfg.update_only {
launch(&PathBuf::from(format!("{}.exe", c))); launch(&PathBuf::from(format!("{}.exe", c)));
} }
return; return;

View File

@ -13,9 +13,19 @@ pub struct Game<'a> {
pub app_id: u32, pub app_id: u32,
} }
#[derive(serde::Deserialize, serde::Serialize)] #[derive(Default, serde::Deserialize, serde::Serialize)]
pub struct Config { pub struct Config {
pub update_only: bool, pub update_only: bool,
pub skip_self_update: bool, pub skip_self_update: bool,
pub bonus_content: bool, pub bonus_content: bool,
} }
// impl Default for Config {
// fn default() -> Self {
// Self {
// update_only: false,
// skip_self_update: false,
// bonus_content: false,
// }
// }
// }