use std::{fs, path::Path}; use serde::{Deserialize, Serialize}; pub struct ConfigFile { pub latest_date: String, pub cache_folder: String, } pub fn get_home() -> String { let home = match std::env::consts::OS { "linux" => std::env::var("HOME").expect("how is there not a $HOME"), "windows" => std::env::var("userprofile").expect("yell at me if this doesn't work please"), _ => std::env::var("HOME").expect( "if this doesn't work, you're probably on macos, which i literally cannot test on", ), }; // println!("{home}"); home } pub fn create_config(file: &String) -> bool { let replaced = file.replace('~', &get_home()[..]); // replace ~ with $HOME let mut paths = replaced.split('/').collect::>(); // split path by / paths.pop(); let path = paths.join("/"); let file_path = std::path::Path::new(&path); // Path without the filename if let Some(_) = file_path.parent() { // if the file_path has a parent folder fs::create_dir_all(&path).expect("couldn't create needed folders") // create folders }; if file_path.exists() && !Path::new(&replaced).exists() { let config = get_config_location(); let steam = get_steam_location(); let file_contents = format!("{{ \"archives\": \"{}/CyberModManager/Archives\", \"folders\": \"{}/CyberModManager/Mods\", \"game_folder\": \"{}/steamapps/common/Cyberpunk 2077\"}}", config, config, steam); fs::write(replaced, file_contents).expect("couldn't write to config file"); true } else { false } } pub fn get_steam_location() -> String { // windows: C:\Program Files (x86)\Steam // linux: ~/.local/share/Steam match std::env::consts::OS { "linux" => String::from(format!("{}/.local/share/Steam", get_home())), "windows" => String::from("C:\\Program Files (x86)\\Steam"), _ => panic!( "if ya tell me what OS you're on and where your steam location is, i can fix this" ), } } pub fn get_config_location() -> String { // windows: ~\AppData\Roaming // linux: ~/.config match std::env::consts::OS { "linux" => String::from(format!("{}/.config", get_home())), "windows" => String::from(format!("{}\\AppData\\Roaming", get_home())), _ => String::from(format!("{}/.config", get_home())), } } pub fn read_config(file: &String) -> Config { let read = fs::read_to_string(file).expect("couldn't read config file"); serde_json::from_str(&read[..]).expect("couldn't parse json") } pub fn ensure_exists(path: String) { let p = Path::new(&path); if !p.exists() { fs::create_dir_all(p).expect("couldn't create folder") } } #[derive(Serialize, Deserialize)] pub struct Config { pub latest_date: String, pub cache_folder: String, }