add config stuff

This commit is contained in:
SadlyNotSappho 2023-07-09 16:53:01 -07:00
parent 7646293220
commit 6949f0b52d
3 changed files with 71 additions and 3 deletions

View File

@ -1,5 +1,5 @@
[package] [package]
name = "archive-helper" name = "cyber-mod-manager"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
@ -8,3 +8,5 @@ edition = "2021"
[dependencies] [dependencies]
clap = { version = "4.3.11", features = ["derive"] } clap = { version = "4.3.11", features = ["derive"] }
compress-tools = "0.14.3" compress-tools = "0.14.3"
serde = { version = "1.0.170", features = ["derive"] }
serde_json = "1.0.100"

View File

@ -1,3 +1,5 @@
# archive-helper # cyberpunk 2077 mod manager
basically does what video game mod managers (vortex, mo2) do but worse if it works for other games, epic
if it doesn't, don't yell at me i didn't make it for other games which is why it defaults to cyberpunk

64
src/lib.rs Normal file
View File

@ -0,0 +1,64 @@
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 i'm not going to touch windows"),
_ => 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() {
let file_contents = "{{ \"archives\": \"{{CONFIG}/CyberModManager/Archives\", \"folders\": \"{{CONFIG}/CyberModManager/Mods\", \"game_folder\": \"{{STEAM}/steamapps/common/Cyberpunk 2077\"}";
fs::write(replaced, file_contents).expect("couldn't write to config file");
true
} else {
false
}
}
pub fn get_steam_location() -> () {
}
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,
}