use std::path::PathBuf; use super::file::FileConfig; use crate::{constants::*, minecraft::launcher::JavaRuntime}; /// Configuration for the runtime environment used to launch Minecraft. /// /// The `RuntimeConfig` struct holds the configuration settings required to /// launch Minecraft with a specific Java runtime. This includes details about /// the user, the Minecraft version, the available Java runtimes, and JVM options. /// /// The `RuntimeConfig` struct is typically used by the launcher to store and /// manage the necessary settings for running the game. It also supports /// dynamic configuration by allowing the addition of Java runtimes and the /// specification of system paths and arguments. #[derive(Debug)] #[derive(Default)] pub struct RuntimeConfig { /// The username of the player running Minecraft. pub username: String, /// The UUID (unique identifier) of the user. pub uuid: String, /// The version of Minecraft that the user is running. pub version: String, /// The maximum amount of memory (in megabytes) allocated to the Java process. /// This setting determines how much memory the Minecraft instance can use. pub max_memory_mb: u32, /// A list of arguments to pass to the JVM when launching Minecraft. /// These arguments can be used to customize the JVM's behavior, such as /// memory settings, garbage collection options, etc. pub jvm_args: Vec, /// A list of Java runtime environments available for Minecraft. /// If no specific Java runtime is provided, the default one is used. pub runtimes: Vec, /// The directory where Minecraft data (e.g., worlds, logs, configs) is stored. /// This path is critical for loading and saving game data. pub data_dir: PathBuf, } impl RuntimeConfig { /// Creates a new `RuntimeConfig` instance from a `FileConfig`. /// /// This method takes a `FileConfig` object (typically loaded from a configuration file) /// and constructs a `RuntimeConfig` instance. It ensures that if no runtimes are defined /// but a Java path is provided, a default Java runtime is created based on the specified path. /// /// # Parameters /// - `file`: A `FileConfig` instance containing initial configuration values for the /// Minecraft runtime. This includes user details, Minecraft version, JVM arguments, /// and the path to Java (if specified). /// /// # Returns /// - A `RuntimeConfig` instance initialized with the values from `file`. If no runtimes /// were provided in `file` but a Java path was given, the method will add a default Java runtime. /// /// # Example /// ```rust /// let file_config = FileConfig { /// username: "Player1".into(), /// uuid: "1234-5678-9101".into(), /// version: "1.18.2".into(), /// max_memory_mb: 2048, /// jvm_args: vec!["-Xmx2G".into()], /// runtimes: Vec::new(), /// java_path: "/path/to/java".into(), /// data_dir: "/path/to/minecraft/data".into(), /// }; /// /// let runtime_config = RuntimeConfig::from_file(file_config); /// ``` pub fn from_file(mut file: FileConfig) -> Self { if file.runtimes.is_empty() && !file.java_path.is_empty() { file.runtimes.push(JavaRuntime { major: DEFAULT_JAVA_MAJOR, path: PathBuf::from(&file.java_path), }); } Self { username: file.username, uuid: file.uuid, version: file.version, max_memory_mb: file.max_memory_mb, jvm_args: file.jvm_args, runtimes: file.runtimes, data_dir: file.data_dir, } } }