Refactor config file handling

This commit is contained in:
a2x 2024-04-01 20:39:08 +10:00
parent ef5ff1c5f4
commit 86dc0fa8f6
1 changed files with 23 additions and 8 deletions

View File

@ -5,16 +5,16 @@ use std::{env, fs};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
pub static CONFIG: LazyLock<Config> = LazyLock::new(|| { pub static CONFIG: LazyLock<Config> = LazyLock::new(|| {
let file_name = match env::consts::OS { let file_name = get_config_file_name();
"linux" => "config_linux.json",
"windows" => "config_win.json",
os => panic!("unsupported os: {}", os),
};
let content = fs::read_to_string(file_name).expect("unable to read config file"); let content = fs::read_to_string(&file_name).unwrap_or_else(|_| {
let config: Config = serde_json::from_str(&content).expect("unable to parse config file"); panic!(
"unable to read config file: {}\nmake sure the file is placed in the same directory as the cs2-dumper executable",
file_name
)
});
config serde_json::from_str(&content).expect("unable to parse config file")
}); });
#[derive(Debug, Deserialize, Serialize)] #[derive(Debug, Deserialize, Serialize)]
@ -59,3 +59,18 @@ pub struct Signature {
/// List of operations to perform on the matched address. /// List of operations to perform on the matched address.
pub operations: Vec<Operation>, pub operations: Vec<Operation>,
} }
/// Returns the correct config file name, depending on the target OS.
fn get_config_file_name() -> &'static str {
// Assume that if the user has provided a connector name, they are targetting the Windows
// version of the game.
if env::args().any(|arg| arg.starts_with("--connector") || arg.starts_with("-c")) {
"config_win.json"
} else {
match env::consts::OS {
"linux" => "config_linux.json",
"windows" => "config_win.json",
os => panic!("unsupported os: {}", os),
}
}
}