lighty_launch/launch/config.rs
1// Copyright (c) 2025 Hamadi
2// Licensed under the MIT License
3
4//! Global launch configuration
5//!
6//! Configure username, UUID, and Java distribution globally instead of passing them to each launch call.
7
8use once_cell::sync::OnceCell;
9use lighty_java::JavaDistribution;
10
11/// Launch configuration
12///
13/// Configure these parameters once and reuse them across all launches.
14#[derive(Debug, Clone)]
15pub struct LaunchConfig {
16 /// Username for authentication
17 pub username: String,
18
19 /// Player UUID (with dashes)
20 pub uuid: String,
21
22 /// Java distribution to use
23 pub java_distribution: JavaDistribution,
24}
25
26impl LaunchConfig {
27 /// Create a new launch configuration
28 ///
29 /// # Arguments
30 /// - `username`: Player username
31 /// - `uuid`: Player UUID (format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
32 /// - `java_distribution`: Java distribution to download/use
33 pub fn new(
34 username: impl Into<String>,
35 uuid: impl Into<String>,
36 java_distribution: JavaDistribution,
37 ) -> Self {
38 Self {
39 username: username.into(),
40 uuid: uuid.into(),
41 java_distribution,
42 }
43 }
44}
45
46impl Default for LaunchConfig {
47 fn default() -> Self {
48 Self {
49 username: "Hamadi".to_string(),
50 uuid: "00000000-0000-0000-0000-000000000000".to_string(),
51 java_distribution: JavaDistribution::Temurin,
52 }
53 }
54}
55
56static LAUNCH_CONFIG: OnceCell<LaunchConfig> = OnceCell::new();
57
58/// Initialize the global launch configuration
59///
60/// This function must be called before using `launch()`.
61/// If not called, default values will be used.
62///
63/// # Arguments
64/// - `config`: Launch configuration
65///
66/// # Example
67/// ```no_run
68/// use lighty_launch::launch_config::{init_launch_config, LaunchConfig};
69/// use lighty_java::JavaDistribution;
70///
71/// init_launch_config(LaunchConfig::new(
72/// "Steve",
73/// "12345678-1234-5678-1234-567812345678",
74/// JavaDistribution::Zulu
75/// ));
76/// ```
77pub fn init_launch_config(config: LaunchConfig) {
78 LAUNCH_CONFIG.set(config).ok();
79}
80
81/// Get the current launch configuration
82///
83/// If not initialized via `init_launch_config()`, returns default values.
84pub(crate) fn get_config() -> LaunchConfig {
85 LAUNCH_CONFIG.get_or_init(LaunchConfig::default).clone()
86}