From 72ddd7b7704f2087a52c9c0552446682918c513b Mon Sep 17 00:00:00 2001 From: Filip Wandzio Date: Thu, 22 Jan 2026 23:14:08 +0100 Subject: Implement basic game files download logic Implement core clap arguments Respect XDG_BASE_DIR Currently library extraction is broken because it assumes every instace has it's own library folder. This should be refactored so instances share libraries Signed-off-by: Filip Wandzio --- src/config/loader.rs | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/config/mod.rs | 3 +++ 2 files changed, 73 insertions(+) create mode 100644 src/config/loader.rs create mode 100644 src/config/mod.rs (limited to 'src/config') diff --git a/src/config/loader.rs b/src/config/loader.rs new file mode 100644 index 0000000..81a4351 --- /dev/null +++ b/src/config/loader.rs @@ -0,0 +1,70 @@ +use std::{env, path::PathBuf}; + +use serde::Deserialize; + +use crate::{constants::*, errors::McError}; + +#[allow(dead_code)] +#[derive(Debug, Deserialize)] +pub struct Config { + pub username: String, + pub uuid: String, + pub version: String, + pub java_path: String, + pub max_memory_mb: u32, + pub data_dir: PathBuf, + pub cache_dir: PathBuf, + pub config_dir: PathBuf, + #[serde(default)] + pub jvm_args: Vec, +} + +impl Config { + pub fn load() -> Result { + let cfg_path = default_config_path()?; + let mut cfg: Config = if cfg_path.exists() { + let txt = std::fs::read_to_string(&cfg_path)?; + toml::from_str(&txt).map_err(|e| McError::Config(e.to_string()))? + } else { + Self::default() + }; + + if let Ok(v) = env::var("MC_USERNAME") { + cfg.username = v; + } + if let Ok(v) = env::var("MC_VERSION") { + cfg.version = v; + } + if let Ok(v) = env::var("MC_JAVA_PATH") { + cfg.java_path = v; + } + if let Ok(v) = env::var("MC_MAX_MEMORY_MB") { + cfg.max_memory_mb = v.parse().unwrap_or(cfg.max_memory_mb); + } + + Ok(cfg) + } + + fn default() -> Self { + let base = + directories::ProjectDirs::from("com", "example", "mccl").expect("platform dirs"); + + Self { + username: "Player".into(), + uuid: uuid::Uuid::new_v4().to_string(), + version: DEFAULT_VERSION.into(), + java_path: DEFAULT_JAVA_PATH.into(), + max_memory_mb: DEFAULT_MAX_MEMORY_MB, + data_dir: base.data_dir().into(), + cache_dir: base.cache_dir().into(), + config_dir: base.config_dir().into(), + jvm_args: vec![], + } + } +} + +fn default_config_path() -> Result { + let base = directories::ProjectDirs::from("com", "example", "mccl") + .ok_or_else(|| McError::Config("cannot determine config dir".into()))?; + Ok(base.config_dir().join("config.toml")) +} diff --git a/src/config/mod.rs b/src/config/mod.rs new file mode 100644 index 0000000..c5fc004 --- /dev/null +++ b/src/config/mod.rs @@ -0,0 +1,3 @@ +pub mod loader; + +pub use loader::Config; -- cgit v1.2.3