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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// Copyright (c) 2025 Hamadi
// Licensed under the MIT License
//! Launch builder for game arguments and JVM options.
use std::collections::{HashMap, HashSet};
use lighty_auth::UserProfile;
use lighty_java::JavaDistribution;
use crate::errors::InstallerResult;
use lighty_loaders::types::{VersionInfo, Loader, LoaderExtensions};
use crate::arguments::Arguments;
use crate::installer::Installer;
#[cfg(feature = "events")]
use lighty_event::EventBus;
/// Launch builder. Created by `version.launch(&profile, java_distribution)`.
pub struct LaunchBuilder<'a, T> {
pub(crate) version: &'a mut T,
pub(crate) profile: &'a UserProfile,
pub(crate) java_distribution: JavaDistribution,
pub(crate) jvm_overrides: HashMap<String, String>,
pub(crate) jvm_removals: HashSet<String>,
pub(crate) arg_overrides: HashMap<String, String>,
pub(crate) arg_removals: HashSet<String>,
pub(crate) raw_args: Vec<String>,
#[cfg(feature = "events")]
pub(crate) event_bus: Option<&'a EventBus>,
}
impl<'a, T> LaunchBuilder<'a, T>
where
T: VersionInfo<LoaderType = Loader>
+ LoaderExtensions
+ Arguments
+ Installer
+ lighty_modsloader::WithMods,
{
/// Create a new launch builder
pub(crate) fn new(
version: &'a mut T,
profile: &'a UserProfile,
java_distribution: JavaDistribution,
) -> Self {
Self {
version,
profile,
java_distribution,
jvm_overrides: HashMap::new(),
jvm_removals: HashSet::new(),
arg_overrides: HashMap::new(),
arg_removals: HashSet::new(),
raw_args: Vec::new(),
#[cfg(feature = "events")]
event_bus: None,
}
}
/// Set an event bus to receive download progress events
///
/// # Example
/// ```rust,no_run
/// # use lighty_auth::UserProfile;
/// # use lighty_core::AppState;
/// # use lighty_launch::errors::InstallerResult;
/// # use lighty_event::EventBus;
/// # use lighty_java::JavaDistribution;
/// # use lighty_launch::launch::Launch;
/// # use lighty_loaders::types::Loader;
/// # use lighty_version::VersionBuilder;
/// # async fn run() -> InstallerResult<()> {
/// # AppState::init("MyLauncher").ok();
/// # let profile = UserProfile::offline("Player", "");
/// # let mut version = VersionBuilder::new("inst", Loader::Vanilla, "", "1.21.1");
/// let event_bus = EventBus::new(100);
/// version.launch(&profile, JavaDistribution::Zulu)
/// .with_event_bus(&event_bus)
/// .run()
/// .await?;
/// # Ok(()) }
/// ```
#[cfg(feature = "events")]
pub fn with_event_bus(mut self, event_bus: &'a EventBus) -> Self {
self.event_bus = Some(event_bus);
self
}
/// Configure JVM options
///
/// # Example
/// ```rust,no_run
/// # use lighty_auth::UserProfile;
/// # use lighty_core::AppState;
/// # use lighty_launch::errors::InstallerResult;
/// # use lighty_java::JavaDistribution;
/// # use lighty_launch::launch::Launch;
/// # use lighty_loaders::types::Loader;
/// # use lighty_version::VersionBuilder;
/// # async fn run() -> InstallerResult<()> {
/// # AppState::init("MyLauncher").ok();
/// # let profile = UserProfile::offline("Player", "");
/// # let mut version = VersionBuilder::new("inst", Loader::Vanilla, "", "1.21.1");
/// version.launch(&profile, JavaDistribution::Zulu)
/// .with_jvm_options()
/// .set("Xmx", "4G")
/// .set("Xms", "2G")
/// .set("XX:+UseG1GC", "")
/// .done()
/// .run()
/// .await?;
/// # Ok(()) }
/// ```
pub fn with_jvm_options(self) -> JvmOptionsBuilder<'a, T> {
JvmOptionsBuilder {
parent: self,
overrides: HashMap::new(),
removals: HashSet::new(),
}
}
/// Configure game arguments
///
/// # Example
/// ```rust,no_run
/// # use lighty_auth::UserProfile;
/// # use lighty_core::AppState;
/// # use lighty_launch::errors::InstallerResult;
/// # use lighty_java::JavaDistribution;
/// # use lighty_launch::launch::Launch;
/// # use lighty_loaders::types::Loader;
/// # use lighty_version::VersionBuilder;
/// # async fn run() -> InstallerResult<()> {
/// # AppState::init("MyLauncher").ok();
/// # let profile = UserProfile::offline("Player", "");
/// # let mut version = VersionBuilder::new("inst", Loader::Vanilla, "", "1.21.1");
/// version.launch(&profile, JavaDistribution::Zulu)
/// .with_arguments()
/// .set("width", "1920")
/// .set("height", "1080")
/// .done()
/// .run()
/// .await?;
/// # Ok(()) }
/// ```
pub fn with_arguments(self) -> ArgumentsBuilder<'a, T> {
ArgumentsBuilder {
parent: self,
overrides: HashMap::new(),
removals: HashSet::new(),
raw_args: Vec::new(),
}
}
/// Execute the launch
///
/// # Example
/// ```rust,no_run
/// # use lighty_auth::UserProfile;
/// # use lighty_core::AppState;
/// # use lighty_launch::errors::InstallerResult;
/// # use lighty_java::JavaDistribution;
/// # use lighty_launch::launch::Launch;
/// # use lighty_loaders::types::Loader;
/// # use lighty_version::VersionBuilder;
/// # async fn run() -> InstallerResult<()> {
/// # AppState::init("MyLauncher").ok();
/// # let profile = UserProfile::offline("Player", "");
/// # let mut version = VersionBuilder::new("inst", Loader::Vanilla, "", "1.21.1");
/// version.launch(&profile, JavaDistribution::Zulu).run().await?;
/// # Ok(()) }
/// ```
pub async fn run(self) -> InstallerResult<()> {
crate::launch::execute_launch(
self.version,
self.profile,
self.java_distribution,
&self.jvm_overrides,
&self.jvm_removals,
&self.arg_overrides,
&self.arg_removals,
&self.raw_args,
#[cfg(feature = "events")]
self.event_bus,
)
.await
}
}
/// JVM options builder
///
/// Configure JVM options like memory, garbage collection, etc.
pub struct JvmOptionsBuilder<'a, T> {
parent: LaunchBuilder<'a, T>,
overrides: HashMap<String, String>,
removals: HashSet<String>,
}
impl<'a, T> JvmOptionsBuilder<'a, T>
where
T: VersionInfo<LoaderType = Loader> + LoaderExtensions + Arguments + Installer,
{
/// Set a JVM option
///
/// The `-` prefix is added automatically based on the key format:
/// - `Xmx`, `Xms` → `-Xmx`, `-Xms`
/// - `XX:+UseG1GC` → `-XX:+UseG1GC`
/// - `Djava.library.path` → `-Djava.library.path`
///
/// # Arguments
/// - `key`: JVM option key (without the `-` prefix)
/// - `value`: Option value (empty string for flags)
///
/// # Example
/// ```rust,no_run
/// # use lighty_auth::UserProfile;
/// # use lighty_core::AppState;
/// # use lighty_launch::errors::InstallerResult;
/// # use lighty_java::JavaDistribution;
/// # use lighty_launch::launch::Launch;
/// # use lighty_loaders::types::Loader;
/// # use lighty_version::VersionBuilder;
/// # async fn run() -> InstallerResult<()> {
/// # AppState::init("MyLauncher").ok();
/// # let profile = UserProfile::offline("Player", "");
/// # let mut version = VersionBuilder::new("inst", Loader::Vanilla, "", "1.21.1");
/// version.launch(&profile, JavaDistribution::Zulu)
/// .with_jvm_options()
/// .set("Xmx", "4G") // → -Xmx4G
/// .set("XX:+UseG1GC", "") // → -XX:+UseG1GC
/// .set("Djava.library.path", "/path") // → -Djava.library.path=/path
/// .done()
/// .run()
/// .await?;
/// # Ok(()) }
/// ```
pub fn set(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.overrides.insert(key.into(), value.into());
self
}
/// Remove a JVM option
///
/// # Arguments
/// - `key`: JVM option key to remove
///
/// # Example
/// ```rust,no_run
/// # use lighty_auth::UserProfile;
/// # use lighty_core::AppState;
/// # use lighty_launch::errors::InstallerResult;
/// # use lighty_java::JavaDistribution;
/// # use lighty_launch::launch::Launch;
/// # use lighty_loaders::types::Loader;
/// # use lighty_version::VersionBuilder;
/// # async fn run() -> InstallerResult<()> {
/// # AppState::init("MyLauncher").ok();
/// # let profile = UserProfile::offline("Player", "");
/// # let mut version = VersionBuilder::new("inst", Loader::Vanilla, "", "1.21.1");
/// version.launch(&profile, JavaDistribution::Zulu)
/// .with_jvm_options()
/// .remove("Xmx")
/// .done()
/// .run()
/// .await?;
/// # Ok(()) }
/// ```
pub fn remove(mut self, key: impl Into<String>) -> Self {
self.removals.insert(key.into());
self
}
/// Finish configuring JVM options and return to the launch builder
pub fn done(self) -> LaunchBuilder<'a, T> {
let mut parent = self.parent;
parent.jvm_overrides = self.overrides;
parent.jvm_removals = self.removals;
parent
}
}
/// Game arguments builder
///
/// Configure game arguments like resolution, game directory, etc.
pub struct ArgumentsBuilder<'a, T> {
parent: LaunchBuilder<'a, T>,
overrides: HashMap<String, String>,
removals: HashSet<String>,
raw_args: Vec<String>,
}
impl<'a, T> ArgumentsBuilder<'a, T>
where
T: VersionInfo<LoaderType = Loader> + LoaderExtensions + Arguments + Installer,
{
/// Set a game argument or placeholder value
///
/// This method intelligently handles two cases:
/// - If the key is a known placeholder constant (like KEY_LAUNCHER_NAME), it overrides the placeholder value
/// - Otherwise, it adds a raw argument with automatic `--` prefix
///
/// # Arguments
/// - `key`: Either a placeholder constant or a custom argument name
/// - `value`: The value for the argument
///
/// # Example
/// ```rust,no_run
/// # use lighty_auth::UserProfile;
/// # use lighty_core::AppState;
/// # use lighty_launch::errors::InstallerResult;
/// # use lighty_java::JavaDistribution;
/// # use lighty_launch::launch::Launch;
/// # use lighty_loaders::types::Loader;
/// # use lighty_version::VersionBuilder;
/// use lighty_launch::arguments::KEY_LAUNCHER_NAME;
/// # async fn run() -> InstallerResult<()> {
/// # AppState::init("MyLauncher").ok();
/// # let profile = UserProfile::offline("Player", "");
/// # let mut version = VersionBuilder::new("inst", Loader::Vanilla, "", "1.21.1");
/// version.launch(&profile, JavaDistribution::Zulu)
/// .with_arguments()
/// .set(KEY_LAUNCHER_NAME, "MyLauncher") // override ${launcher_name}
/// .set("width", "1920") // adds --width 1920
/// .set("fullscreen", "") // adds --fullscreen
/// .done()
/// .run()
/// .await?;
/// # Ok(()) }
/// ```
pub fn set(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
let key_str = key.into();
let value_str = value.into();
// Known launch placeholder keys (overrides target the variable map)
const KNOWN_PLACEHOLDERS: &[&str] = &[
"auth_player_name", "auth_uuid", "auth_access_token", "auth_xuid",
"clientid", "user_type", "user_properties",
"version_name", "version_type",
"game_directory", "assets_root", "natives_directory", "library_directory",
"assets_index_name", "launcher_name", "launcher_version",
"classpath", "classpath_separator",
];
// Known placeholders are recorded as substitutions
if KNOWN_PLACEHOLDERS.contains(&key_str.as_str()) {
self.overrides.insert(key_str, value_str);
} else {
// Anything else is appended as a raw `--key [value]` argument
let formatted_arg = if key_str.starts_with("--") {
key_str
} else if key_str.starts_with('-') {
format!("-{}", key_str)
} else {
format!("--{}", key_str)
};
self.raw_args.push(formatted_arg);
if !value_str.is_empty() {
self.raw_args.push(value_str);
}
}
self
}
/// Remove a game argument
///
/// # Arguments
/// - `key`: Argument key to remove
///
/// # Example
/// ```rust,no_run
/// # use lighty_auth::UserProfile;
/// # use lighty_core::AppState;
/// # use lighty_launch::errors::InstallerResult;
/// # use lighty_java::JavaDistribution;
/// # use lighty_launch::launch::Launch;
/// # use lighty_loaders::types::Loader;
/// # use lighty_version::VersionBuilder;
/// # async fn run() -> InstallerResult<()> {
/// # AppState::init("MyLauncher").ok();
/// # let profile = UserProfile::offline("Player", "");
/// # let mut version = VersionBuilder::new("inst", Loader::Vanilla, "", "1.21.1");
/// version.launch(&profile, JavaDistribution::Zulu)
/// .with_arguments()
/// .remove("width")
/// .done()
/// .run()
/// .await?;
/// # Ok(()) }
/// ```
pub fn remove(mut self, key: impl Into<String>) -> Self {
self.removals.insert(key.into());
self
}
/// Finish configuring arguments and return to the launch builder
pub fn done(self) -> LaunchBuilder<'a, T> {
let mut parent = self.parent;
parent.arg_overrides = self.overrides;
parent.arg_removals = self.removals;
parent.raw_args = self.raw_args;
parent
}
}