aboutsummaryrefslogtreecommitdiffstats
path: root/src/config/runtime.rs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/config/runtime.rs95
1 files changed, 95 insertions, 0 deletions
diff --git a/src/config/runtime.rs b/src/config/runtime.rs
new file mode 100644
index 0000000..46857f0
--- /dev/null
+++ b/src/config/runtime.rs
@@ -0,0 +1,95 @@
1use std::path::PathBuf;
2
3use super::file::FileConfig;
4use crate::{constants::*, minecraft::launcher::JavaRuntime};
5
6/// Configuration for the runtime environment used to launch Minecraft.
7///
8/// The `RuntimeConfig` struct holds the configuration settings required to
9/// launch Minecraft with a specific Java runtime. This includes details about
10/// the user, the Minecraft version, the available Java runtimes, and JVM options.
11///
12/// The `RuntimeConfig` struct is typically used by the launcher to store and
13/// manage the necessary settings for running the game. It also supports
14/// dynamic configuration by allowing the addition of Java runtimes and the
15/// specification of system paths and arguments.
16#[derive(Debug)]
17#[derive(Default)]
18pub struct RuntimeConfig {
19 /// The username of the player running Minecraft.
20 pub username: String,
21
22 /// The UUID (unique identifier) of the user.
23 pub uuid: String,
24
25 /// The version of Minecraft that the user is running.
26 pub version: String,
27
28 /// The maximum amount of memory (in megabytes) allocated to the Java process.
29 /// This setting determines how much memory the Minecraft instance can use.
30 pub max_memory_mb: u32,
31
32 /// A list of arguments to pass to the JVM when launching Minecraft.
33 /// These arguments can be used to customize the JVM's behavior, such as
34 /// memory settings, garbage collection options, etc.
35 pub jvm_args: Vec<String>,
36
37 /// A list of Java runtime environments available for Minecraft.
38 /// If no specific Java runtime is provided, the default one is used.
39 pub runtimes: Vec<JavaRuntime>,
40
41 /// The directory where Minecraft data (e.g., worlds, logs, configs) is stored.
42 /// This path is critical for loading and saving game data.
43 pub data_dir: PathBuf,
44}
45
46impl RuntimeConfig {
47 /// Creates a new `RuntimeConfig` instance from a `FileConfig`.
48 ///
49 /// This method takes a `FileConfig` object (typically loaded from a configuration file)
50 /// and constructs a `RuntimeConfig` instance. It ensures that if no runtimes are defined
51 /// but a Java path is provided, a default Java runtime is created based on the specified path.
52 ///
53 /// # Parameters
54 /// - `file`: A `FileConfig` instance containing initial configuration values for the
55 /// Minecraft runtime. This includes user details, Minecraft version, JVM arguments,
56 /// and the path to Java (if specified).
57 ///
58 /// # Returns
59 /// - A `RuntimeConfig` instance initialized with the values from `file`. If no runtimes
60 /// were provided in `file` but a Java path was given, the method will add a default Java runtime.
61 ///
62 /// # Example
63 /// ```rust
64 /// let file_config = FileConfig {
65 /// username: "Player1".into(),
66 /// uuid: "1234-5678-9101".into(),
67 /// version: "1.18.2".into(),
68 /// max_memory_mb: 2048,
69 /// jvm_args: vec!["-Xmx2G".into()],
70 /// runtimes: Vec::new(),
71 /// java_path: "/path/to/java".into(),
72 /// data_dir: "/path/to/minecraft/data".into(),
73 /// };
74 ///
75 /// let runtime_config = RuntimeConfig::from_file(file_config);
76 /// ```
77 pub fn from_file(mut file: FileConfig) -> Self {
78 if file.runtimes.is_empty() && !file.java_path.is_empty() {
79 file.runtimes.push(JavaRuntime {
80 major: DEFAULT_JAVA_MAJOR,
81 path: PathBuf::from(&file.java_path),
82 });
83 }
84
85 Self {
86 username: file.username,
87 uuid: file.uuid,
88 version: file.version,
89 max_memory_mb: file.max_memory_mb,
90 jvm_args: file.jvm_args,
91 runtimes: file.runtimes,
92 data_dir: file.data_dir,
93 }
94 }
95}