CyberModManager/src/lib.rs

83 lines
2.8 KiB
Rust
Raw Normal View History

2023-07-09 16:53:01 -07:00
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"),
2023-07-10 09:04:48 -07:00
"windows" => std::env::var("userprofile").expect("yell at me if this doesn't work please"),
2023-07-09 16:53:01 -07:00
_ => 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::<Vec<&str>>(); // 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() {
2023-07-10 09:04:48 -07:00
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);
2023-07-09 16:53:01 -07:00
fs::write(replaced, file_contents).expect("couldn't write to config file");
true
} else {
false
}
}
2023-07-10 09:02:01 -07:00
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"),
2023-07-10 09:04:48 -07:00
_ => panic!(
"if ya tell me what OS you're on and where your steam location is, i can fix this"
),
2023-07-10 09:02:01 -07:00
}
}
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())),
2023-07-10 09:04:48 -07:00
_ => String::from(format!("{}/.config", get_home())),
2023-07-10 09:02:01 -07:00
}
2023-07-09 16:53:01 -07:00
}
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,
}