diff options
Diffstat (limited to 'src/minecraft/launcher.rs')
| -rw-r--r-- | src/minecraft/launcher.rs | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/src/minecraft/launcher.rs b/src/minecraft/launcher.rs new file mode 100644 index 0000000..f7e3ecc --- /dev/null +++ b/src/minecraft/launcher.rs | |||
| @@ -0,0 +1,73 @@ | |||
| 1 | use std::process::Command; | ||
| 2 | |||
| 3 | use log::{debug, info}; | ||
| 4 | |||
| 5 | use crate::{config::Config, errors::McError, minecraft::manifests::Version, platform::paths}; | ||
| 6 | |||
| 7 | /// Build the full classpath | ||
| 8 | fn build_classpath(config: &Config, version: &Version) -> Result<String, McError> { | ||
| 9 | let sep = if cfg!(windows) { ";" } else { ":" }; | ||
| 10 | let mut entries = Vec::new(); | ||
| 11 | |||
| 12 | for lib in &version.libraries { | ||
| 13 | if let Some(artifact) = &lib.downloads.artifact { | ||
| 14 | let path = paths::library_file(config, &artifact.path)?; | ||
| 15 | entries.push(path.to_string_lossy().to_string()); | ||
| 16 | } | ||
| 17 | } | ||
| 18 | |||
| 19 | let client_jar = paths::client_jar(config, &version.id)?; | ||
| 20 | entries.push(client_jar.to_string_lossy().to_string()); | ||
| 21 | Ok(entries.join(sep)) | ||
| 22 | } | ||
| 23 | |||
| 24 | /// Launch Minecraft | ||
| 25 | pub fn launch(config: &Config, version: &Version) -> Result<(), McError> { | ||
| 26 | let java = &config.java_path; | ||
| 27 | let classpath = build_classpath(config, version)?; | ||
| 28 | let natives_dir = paths::natives_dir(config, &version.id); | ||
| 29 | |||
| 30 | if !natives_dir.exists() { | ||
| 31 | return Err(McError::Runtime(format!( | ||
| 32 | "Natives folder does not exist: {}", | ||
| 33 | natives_dir.display() | ||
| 34 | ))); | ||
| 35 | } | ||
| 36 | |||
| 37 | info!("Launching Minecraft {}", version.id); | ||
| 38 | debug!("Classpath: {}", classpath); | ||
| 39 | debug!("Natives: {}", natives_dir.display()); | ||
| 40 | |||
| 41 | let status = Command::new(java) | ||
| 42 | .arg(format!("-Xmx{}M", config.max_memory_mb)) | ||
| 43 | .arg(format!("-Djava.library.path={}", natives_dir.display())) | ||
| 44 | .arg("-cp") | ||
| 45 | .arg(classpath) | ||
| 46 | .arg(&version.main_class) | ||
| 47 | .arg("--username") | ||
| 48 | .arg(&config.username) | ||
| 49 | .arg("--version") | ||
| 50 | .arg(&version.id) | ||
| 51 | .arg("--gameDir") | ||
| 52 | .arg(paths::minecraft_root(config)) | ||
| 53 | .arg("--assetsDir") | ||
| 54 | .arg(paths::minecraft_root(config).join("assets")) | ||
| 55 | .arg("--assetIndex") | ||
| 56 | .arg(&version.id) | ||
| 57 | .arg("--uuid") | ||
| 58 | .arg(&config.uuid) | ||
| 59 | .arg("--userProperties") | ||
| 60 | .arg("{}") | ||
| 61 | .arg("--accessToken") | ||
| 62 | .arg("0") | ||
| 63 | .arg("--userType") | ||
| 64 | .arg("legacy") | ||
| 65 | .args(&config.jvm_args) | ||
| 66 | .status()?; | ||
| 67 | |||
| 68 | if !status.success() { | ||
| 69 | return Err(McError::Process("Minecraft exited with error".into())); | ||
| 70 | } | ||
| 71 | |||
| 72 | Ok(()) | ||
| 73 | } | ||
