mcvm_core 0.9.0

Core functionality for launching Minecraft
Documentation
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/// System Java installation
mod system;

use std::fs::File;
use std::io::{BufReader, Read, Seek};

#[cfg(target_family = "unix")]
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};

use anyhow::{bail, Context};
use mcvm_shared::output::{MCVMOutput, MessageContents, MessageLevel};
use mcvm_shared::{translate, UpdateDepth};
use tar::Archive;
use zip::ZipArchive;

use crate::io::files::{self, paths::Paths};
use crate::io::persistent::{PersistentData, PersistentDataJavaInstallation};
use crate::io::update::UpdateManager;
use crate::net::{self, download};
use mcvm_shared::util::preferred_archive_extension;

use super::JavaMajorVersion;

/// Type of Java installation
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub enum JavaInstallationKind {
	/// Automatically chooses different Java
	/// flavors based on system conditions
	Auto,
	/// Trys to use a Java installation that is
	/// already on the system
	System,
	/// Adoptium
	Adoptium,
	/// Azul Zulu
	Zulu,
	/// GraalVM
	GraalVM,
	/// A user-specified installation
	Custom {
		/// The path to the installation. The JVM must live at
		/// `{path}/bin/java`
		path: PathBuf,
	},
}

impl JavaInstallationKind {
	/// Parse a string into a JavaKind
	pub fn parse(string: &str) -> Self {
		match string {
			"auto" => Self::Auto,
			"system" => Self::System,
			"adoptium" => Self::Adoptium,
			"zulu" => Self::Zulu,
			"graalvm" => Self::GraalVM,
			path => Self::Custom {
				path: PathBuf::from(path),
			},
		}
	}
}

/// A Java installation used to launch the game
#[derive(Debug, Clone)]
pub struct JavaInstallation {
	/// The major version of the Java installation
	major_version: JavaMajorVersion,
	/// The path to the directory where the installation is, which will be filled when it is installed
	path: PathBuf,
}

impl JavaInstallation {
	/// Load a new Java installation
	pub(crate) async fn install(
		kind: JavaInstallationKind,
		major_version: JavaMajorVersion,
		mut params: JavaInstallParameters<'_>,
		o: &mut impl MCVMOutput,
	) -> anyhow::Result<Self> {
		o.start_process();
		o.display(
			MessageContents::StartProcess(translate!(o, StartCheckingForJavaUpdates)),
			MessageLevel::Important,
		);

		let vers_str = major_version.to_string();

		let path = match &kind {
			JavaInstallationKind::Auto => install_auto(&vers_str, params, o).await?,
			JavaInstallationKind::System => system::install(&vers_str)?,
			JavaInstallationKind::Adoptium => install_adoptium(&vers_str, &mut params, o).await?,
			JavaInstallationKind::Zulu => install_zulu(&vers_str, &mut params, o).await?,
			JavaInstallationKind::GraalVM => install_graalvm(&vers_str, &mut params, o).await?,
			JavaInstallationKind::Custom { path } => path.clone(),
		};

		o.display(
			MessageContents::Success(translate!(o, FinishCheckingForJavaUpdates)),
			MessageLevel::Important,
		);

		o.end_process();

		let out = Self {
			major_version,
			path,
		};

		Ok(out)
	}

	/// Get the major version of the Java installation
	pub fn get_major_version(&self) -> &JavaMajorVersion {
		&self.major_version
	}

	/// Get the path to the Java installation
	pub fn get_path(&self) -> &Path {
		&self.path
	}

	/// Get the path to the JVM.
	pub fn get_jvm_path(&self) -> PathBuf {
		#[cfg(target_family = "windows")]
		let path = "bin/java.exe";
		#[cfg(not(target_family = "windows"))]
		let path = "bin/java";
		self.path.join(path)
	}

	/// Verifies that this installation is set up correctly
	pub fn verify(&self) -> anyhow::Result<bool> {
		let jvm_path = self.get_jvm_path();
		if !jvm_path.exists() || !jvm_path.is_file() {
			return Ok(false);
		}
		#[cfg(target_family = "unix")]
		{
			// Check if JVM is executable
			let mode = jvm_path
				.metadata()
				.context("Failed to get JVM metadata")?
				.permissions()
				.mode();
			if mode & 0o111 == 0 {
				return Ok(false);
			}
		}

		Ok(true)
	}
}

/// Container struct for parameters for loading Java installations
pub(crate) struct JavaInstallParameters<'a> {
	pub paths: &'a Paths,
	pub update_manager: &'a mut UpdateManager,
	pub persistent: &'a mut PersistentData,
	pub req_client: &'a reqwest::Client,
}

async fn install_auto(
	major_version: &str,
	mut params: JavaInstallParameters<'_>,
	o: &mut impl MCVMOutput,
) -> anyhow::Result<PathBuf> {
	let out = system::install(major_version);
	if let Ok(out) = out {
		return Ok(out);
	}
	let out = install_adoptium(major_version, &mut params, o).await;
	if let Ok(out) = out {
		return Ok(out);
	}
	let out = install_graalvm(major_version, &mut params, o).await;
	if let Ok(out) = out {
		return Ok(out);
	}
	let out = install_zulu(major_version, &mut params, o).await;
	if let Ok(out) = out {
		return Ok(out);
	}
	bail!("Failed to automatically install Java")
}

async fn install_adoptium(
	major_version: &str,
	params: &mut JavaInstallParameters<'_>,
	o: &mut impl MCVMOutput,
) -> anyhow::Result<PathBuf> {
	if params.update_manager.update_depth == UpdateDepth::Shallow {
		if let Some(directory) = params
			.persistent
			.get_java_path(PersistentDataJavaInstallation::Adoptium, major_version)
		{
			Ok(directory)
		} else {
			update_adoptium(major_version, params, o)
				.await
				.context("Failed to update Adoptium Java")
		}
	} else {
		update_adoptium(major_version, params, o)
			.await
			.context("Failed to update Adoptium Java")
	}
}

async fn install_zulu(
	major_version: &str,
	params: &mut JavaInstallParameters<'_>,
	o: &mut impl MCVMOutput,
) -> anyhow::Result<PathBuf> {
	if params.update_manager.update_depth == UpdateDepth::Shallow {
		if let Some(directory) = params
			.persistent
			.get_java_path(PersistentDataJavaInstallation::Zulu, major_version)
		{
			Ok(directory)
		} else {
			update_zulu(major_version, params, o)
				.await
				.context("Failed to update Zulu Java")
		}
	} else {
		update_zulu(major_version, params, o)
			.await
			.context("Failed to update Zulu Java")
	}
}

async fn install_graalvm(
	major_version: &str,
	params: &mut JavaInstallParameters<'_>,
	o: &mut impl MCVMOutput,
) -> anyhow::Result<PathBuf> {
	if params.update_manager.update_depth == UpdateDepth::Shallow {
		if let Some(directory) = params
			.persistent
			.get_java_path(PersistentDataJavaInstallation::GraalVM, major_version)
		{
			Ok(directory)
		} else {
			update_graalvm(major_version, params, o)
				.await
				.context("Failed to update GraalVM Java")
		}
	} else {
		update_graalvm(major_version, params, o)
			.await
			.context("Failed to update GraalVM Java")
	}
}

/// Updates Adoptium and returns the path to the installation
async fn update_adoptium(
	major_version: &str,
	params: &mut JavaInstallParameters<'_>,
	o: &mut impl MCVMOutput,
) -> anyhow::Result<PathBuf> {
	let out_dir = params.paths.java.join("adoptium");
	files::create_dir(&out_dir)?;
	let version = net::java::adoptium::get_latest(major_version, params.req_client)
		.await
		.context("Failed to obtain Adoptium information")?;

	let release_name = version.release_name.clone();
	let mut extracted_bin_name = release_name.clone();
	extracted_bin_name.push_str("-jre");
	let extracted_bin_dir = out_dir.join(&extracted_bin_name);

	if !params
		.persistent
		.update_java_installation(
			PersistentDataJavaInstallation::Adoptium,
			major_version,
			&release_name,
			&extracted_bin_dir,
		)
		.context("Failed to update Java in lockfile")?
	{
		return Ok(extracted_bin_dir);
	}

	params.persistent.dump(params.paths).await?;

	let arc_extension = preferred_archive_extension();
	let arc_name = format!("adoptium{major_version}{arc_extension}");
	let arc_path = out_dir.join(arc_name);

	let bin_url = version.binary.package.link;

	o.display(
		MessageContents::StartProcess(translate!(
			o,
			DownloadingAdoptium,
			"version" = &release_name
		)),
		MessageLevel::Important,
	);
	download::file(bin_url, &arc_path, params.req_client)
		.await
		.context("Failed to download JRE binaries")?;

	// Extraction
	o.display(
		MessageContents::StartProcess(translate!(o, StartExtractingJava)),
		MessageLevel::Important,
	);
	extract_archive_file(&arc_path, &out_dir).context("Failed to extract")?;
	o.display(
		MessageContents::StartProcess(translate!(o, StartRemovingJavaArchive)),
		MessageLevel::Important,
	);
	std::fs::remove_file(arc_path).context("Failed to remove archive")?;

	o.display(
		MessageContents::Success(translate!(o, FinishJavaInstallation)),
		MessageLevel::Important,
	);

	Ok(extracted_bin_dir)
}

/// Updates Zulu and returns the path to the installation
async fn update_zulu(
	major_version: &str,
	params: &mut JavaInstallParameters<'_>,
	o: &mut impl MCVMOutput,
) -> anyhow::Result<PathBuf> {
	let out_dir = params.paths.java.join("zulu");
	files::create_dir(&out_dir)?;

	let package = net::java::zulu::get_latest(major_version, params.req_client)
		.await
		.context("Failed to get the latest Zulu version")?;

	let extracted_dir = out_dir.join(net::java::zulu::extract_dir_name(&package.name));

	if !params
		.persistent
		.update_java_installation(
			PersistentDataJavaInstallation::Zulu,
			major_version,
			&package.name,
			&extracted_dir,
		)
		.context("Failed to update Java in lockfile")?
	{
		return Ok(extracted_dir);
	}

	params.persistent.dump(params.paths).await?;

	let arc_path = out_dir.join(&package.name);

	o.display(
		MessageContents::StartProcess(translate!(o, DownloadingZulu, "version" = &package.name)),
		MessageLevel::Important,
	);
	download::file(&package.download_url, &arc_path, params.req_client)
		.await
		.context("Failed to download JRE binaries")?;

	// Extraction
	o.display(
		MessageContents::StartProcess(translate!(o, StartExtractingJava)),
		MessageLevel::Important,
	);
	extract_archive_file(&arc_path, &out_dir).context("Failed to extract")?;
	o.display(
		MessageContents::StartProcess(translate!(o, StartRemovingJavaArchive)),
		MessageLevel::Important,
	);
	std::fs::remove_file(arc_path).context("Failed to remove archive")?;

	o.display(
		MessageContents::Success(translate!(o, FinishJavaInstallation)),
		MessageLevel::Important,
	);

	Ok(extracted_dir)
}

/// Updates GraalVM and returns the path to the installation
async fn update_graalvm(
	major_version: &str,
	params: &mut JavaInstallParameters<'_>,
	o: &mut impl MCVMOutput,
) -> anyhow::Result<PathBuf> {
	let out_dir = params.paths.java.join("graalvm");
	files::create_dir(&out_dir)?;

	o.display(
		MessageContents::StartProcess(translate!(o, DownloadingGraalVM)),
		MessageLevel::Important,
	);
	let archive = net::java::graalvm::get_latest(major_version, params.req_client)
		.await
		.context("Failed to download the latest GraalVM version")?;

	// We have to extract now since we need the extracted dir
	o.display(
		MessageContents::StartProcess(translate!(o, StartExtractingJava)),
		MessageLevel::Important,
	);
	let dir_name = extract_archive(std::io::Cursor::new(archive), &out_dir)
		.context("Failed to extract GraalVM archive")?;

	let extracted_dir = out_dir.join(&dir_name);

	let version = dir_name.replace("graalvm-jdk-", "");

	if !params
		.persistent
		.update_java_installation(
			PersistentDataJavaInstallation::GraalVM,
			major_version,
			&version,
			&extracted_dir,
		)
		.context("Failed to update Java in lockfile")?
	{
		return Ok(extracted_dir);
	}

	params.persistent.dump(params.paths).await?;

	o.display(
		MessageContents::Success(translate!(o, FinishJavaInstallation)),
		MessageLevel::Important,
	);

	Ok(extracted_dir)
}

/// Extracts the archive file
fn extract_archive_file(arc_path: &Path, out_dir: &Path) -> anyhow::Result<()> {
	let file = File::open(arc_path).context("Failed to read archive file")?;
	let file = BufReader::new(file);

	extract_archive(file, out_dir)?;

	Ok(())
}

/// Extracts the JRE archive (either a tar or a zip) and also returns the internal extraction directory name
fn extract_archive<R: Read + Seek>(reader: R, out_dir: &Path) -> anyhow::Result<String> {
	let dir_name = if cfg!(windows) {
		let mut archive = ZipArchive::new(reader).context("Failed to open zip archive")?;

		let dir_name = archive
			.file_names()
			.next()
			.context("Missing archive internal directory")?
			.to_string();

		archive
			.extract(out_dir)
			.context("Failed to extract zip file")?;

		dir_name
	} else {
		let mut decoder =
			libflate::gzip::Decoder::new(reader).context("Failed to decode tar.gz")?;
		// Get the archive twice because of archive shenanigans
		let mut arc = Archive::new(&mut decoder);

		// Wow
		let dir_name = arc
			.entries()
			.context("Failed to get Tar entries")?
			.next()
			.context("Missing archive internal directory")?
			.context("Failed to get entry")?
			.path()
			.context("Failed to get entry path name")?
			.to_string_lossy()
			.to_string();

		let mut arc = Archive::new(&mut decoder);
		arc.unpack(out_dir).context("Failed to unarchive tar")?;

		dir_name
	};

	Ok(dir_name)
}