1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
use derive_builder::Builder;
use std::collections::HashMap;

/// Options for launching Minecraft.
/// The options can be created using the builder pattern:
///
/// ```rust
/// # use cobble_core::minecraft::launch_options::{LaunchOptions, LaunchOptionsBuilder};
/// # fn function() {
///
/// let options: LaunchOptions = LaunchOptionsBuilder::default()
///     .player_name("Steve".to_string())
///     .use_fullscreen(true)
///     .java_exec("/usr/bin/java".to_string())
///     .build();
///
/// # }
/// ```
#[derive(Builder, Clone, Debug)]
#[builder(build_fn(private, name = "fallible_build"))]
pub struct LaunchOptions {
    /// Name of the launcher that gets passed as a game argument.
    ///
    /// Defaults to this crates name when using [`LaunchOptionsBuilder`](LaunchOptionsBuilder).
    #[builder(default = "String::from(env!(\"CARGO_PKG_NAME\"))")]
    pub launcher_name: String,

    /// Version of the launcher that gets passed as a game argument.
    ///
    /// Defaults to this crates version when using [`LaunchOptionsBuilder`](LaunchOptionsBuilder).
    #[builder(default = "String::from(env!(\"CARGO_PKG_VERSION\"))")]
    pub launcher_version: String,

    /// Player name for the game.
    ///
    /// Defaults to `Steve` when using [`LaunchOptionsBuilder`](LaunchOptionsBuilder).
    #[builder(default = "String::from(\"Steve\")")]
    pub player_name: String,

    /// The profile ID.
    ///
    /// This is needed for online mode.
    #[builder(default, setter(strip_option))]
    pub profile_id: Option<String>,

    /// The minecraft access token.
    ///
    /// This is needed for online mode.
    #[builder(default, setter(strip_option))]
    pub token: Option<String>,

    /// Launches the game in fullscreen mode.
    /// When set to `true`, the window size is ignored.
    ///
    /// Defaults to `false` when using [`LaunchOptionsBuilder`](LaunchOptionsBuilder).
    #[builder(default)]
    pub use_fullscreen: bool,

    /// Enables a custom resolution for the game window.
    /// Only has an effect when game is **not** launched in fullscreen mode.
    ///
    /// Defaults to `false` when using [`LaunchOptionsBuilder`](LaunchOptionsBuilder).
    #[builder(default)]
    pub enable_window_size: bool,

    /// Game window width.
    /// Used when `enable_window_size` is set to `true`.
    ///
    /// Defaults to `1280` when using [`LaunchOptionsBuilder`](LaunchOptionsBuilder).
    #[builder(default = "1280")]
    pub window_width: u32,

    /// Game window height.
    /// Used when `enable_window_size` is set to `true`.
    ///
    /// Defaults to `720` when using [`LaunchOptionsBuilder`](LaunchOptionsBuilder).
    #[builder(default = "720")]
    pub window_height: u32,

    /// Enables custom memory JVM arguments.
    ///
    /// Defaults to `false` when using [`LaunchOptionsBuilder`](LaunchOptionsBuilder).
    #[builder(default)]
    pub enable_memory: bool,

    /// JVM initial heap size in megabytes.
    /// Adds the `-Xms` option to the command.
    /// Gets added before `jvm_args`.
    ///
    /// Defaults to `1024` when using [`LaunchOptionsBuilder`](LaunchOptionsBuilder).
    #[builder(default = "512")]
    pub min_memory: u32,

    /// JVM max heap size in megabytes.
    /// Adds the `-Xmx` opti`on to the command.
    /// Gets added before `jvm_args`.
    ///
    /// Defaults to `2048` when using [`LaunchOptionsBuilder`](LaunchOptionsBuilder).
    #[builder(default = "1024")]
    pub max_memory: u32,

    /// Java executable used to launch minecraft.
    ///
    /// Defaults to `java` when using [`LaunchOptionsBuilder`](LaunchOptionsBuilder).
    #[builder(default = "String::from(\"java\")")]
    pub java_exec: String,

    /// Enables custom JVM arguments.
    /// When disabled and the version data provides JVM arguments, the arguments from the version data are used.
    /// When enabled, arguments from version data are ignored.
    ///
    /// Defaults to `false` when using [`LaunchOptionsBuilder`](LaunchOptionsBuilder).
    #[builder(default)]
    pub enable_jvm_args: bool,

    /// Custom JVM arguments.
    ///
    /// Defaults to and empty string when using [`LaunchOptionsBuilder`](LaunchOptionsBuilder).
    #[builder(default)]
    pub jvm_args: String,

    /// Environment variables for the game process.
    ///
    /// No environment variables set by default.
    #[builder(default)]
    pub environment_variables: HashMap<String, Option<String>>,
}

impl LaunchOptionsBuilder {
    /// Builds new `LaunchOptions`.
    pub fn build(&self) -> LaunchOptions {
        self.fallible_build().unwrap()
    }
}