minecraft-java-rs-core 0.2.1

Core library for launching Minecraft Java Edition
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
use std::collections::HashMap;
use std::path::{Path, PathBuf};

use tokio::sync::mpsc::Sender;

use crate::error::LaunchError;
use crate::launcher::events::LaunchEvent;
use crate::launcher::options::LaunchOptions;
use crate::models::java::{
    AdoptiumRelease, JavaFileItem, JavaManifestData, JavaVersionManifest,
};
use crate::models::minecraft::MinecraftVersionJson;
use crate::net::downloader::{DownloadItem, Downloader};
use crate::net::http::{fetch_json, fetch_text};
use crate::utils::archive::extract_tar_gz;

const ALL_JSON_URL: &str =
    "https://launchermeta.mojang.com/v1/products/java-runtime/2ec0cc96c44e5a76b9c8b7c39df7210883d12871/all.json";
const ADOPTIUM_API_BASE: &str = "https://api.adoptium.net/v3/assets/latest";

// ── Public types ──────────────────────────────────────────────────────────────

pub struct JavaDownloadResult {
    /// Absolute path to the `java` (or `javaw.exe`) executable.
    pub java_path: String,
    /// Flat list of downloaded runtime files for `JavaInfo` / bundle checks.
    pub files: Vec<JavaFileItem>,
}

// ── Public API ────────────────────────────────────────────────────────────────

/// Resolve and (if needed) download the Java runtime for a Minecraft version.
///
/// Priority:
/// 1. `options.java.path` set → use verbatim.
/// 2. Binary already cached at the computed runtime path → return fast.
/// 3. Mojang all.json has an entry for this platform/component → Mojang path.
/// 4. Adoptium API fallback.
pub async fn get_java_files(
    options: &LaunchOptions,
    version_json: &MinecraftVersionJson,
    client: &reqwest::Client,
    event_tx: &Sender<LaunchEvent>,
) -> Result<JavaDownloadResult, LaunchError> {
    if let Some(java_path) = &options.java.path {
        return Ok(JavaDownloadResult {
            java_path: java_path.to_string_lossy().into_owned(),
            files: vec![],
        });
    }

    let (component, major_version) = java_component(options, version_json);
    let platform = mojang_platform_key(options.intel_enabled_mac);
    let runtime_root = options
        .path
        .join("runtime")
        .join(&component)
        .join(&platform);

    let java_bin = find_cached_java_bin(&runtime_root);

    if java_bin.exists() {
        return Ok(JavaDownloadResult {
            java_path: java_bin.to_string_lossy().into_owned(),
            files: vec![],
        });
    }

    if let Some(result) =
        try_mojang(options, &component, &platform, &runtime_root, client, event_tx).await?
    {
        return Ok(result);
    }

    get_from_adoptium(
        options,
        &component,
        &runtime_root,
        major_version,
        client,
        event_tx,
    )
    .await
}

// ── Platform helpers ──────────────────────────────────────────────────────────

pub fn mojang_platform_key(intel_enabled_mac: bool) -> String {
    use std::env::consts::{ARCH, OS};
    match (OS, ARCH) {
        ("linux", "x86_64") => "linux",
        ("linux", "x86") => "linux-i386",
        ("macos", "x86_64") => "mac-os",
        ("macos", "aarch64") if intel_enabled_mac => "mac-os",
        ("macos", "aarch64") => "mac-os-arm64",
        ("windows", "x86_64") => "windows-x64",
        ("windows", "x86") => "windows-x86",
        ("windows", "aarch64") => "windows-arm64",
        _ => "linux",
    }
    .to_string()
}

pub fn java_component(options: &LaunchOptions, version_json: &MinecraftVersionJson) -> (String, u32) {
    if let Some(ver) = &options.java.version {
        let major = ver.parse::<u32>().unwrap_or(8);
        return (format!("jre-{major}"), major);
    }
    match &version_json.java_version {
        Some(jv) => {
            let major = jv.major_version.unwrap_or(8);
            (format!("jre-{major}"), major)
        }
        None => ("jre-8".into(), 8),
    }
}

pub fn java_bin_path(runtime_root: &Path) -> PathBuf {
    let bin = if cfg!(target_os = "windows") {
        "javaw.exe"
    } else {
        "java"
    };
    runtime_root.join("bin").join(bin)
}

/// Like `java_bin_path` but also checks the macOS bundle layout used by some
/// Mojang runtimes (e.g. jre-legacy): `jre.bundle/Contents/Home/bin/java`.
/// Returns the first path that exists on disk, or the standard path as a
/// fallback so callers can still attempt the download.
fn find_cached_java_bin(runtime_root: &Path) -> PathBuf {
    let primary = java_bin_path(runtime_root);
    if primary.exists() {
        return primary;
    }
    #[cfg(target_os = "macos")]
    {
        let bundle = runtime_root.join("jre.bundle/Contents/Home/bin/java");
        if bundle.exists() {
            return bundle;
        }
    }
    primary
}

fn adoptium_os() -> &'static str {
    match std::env::consts::OS {
        "linux" => "linux",
        "macos" => "mac",
        "windows" => "windows",
        _ => "linux",
    }
}

fn adoptium_arch(intel_enabled_mac: bool) -> &'static str {
    use std::env::consts::{ARCH, OS};
    if intel_enabled_mac && OS == "macos" {
        return "x64";
    }
    match ARCH {
        "x86_64" => "x64",
        "x86" => "x86",
        "aarch64" => "aarch64",
        "arm" => "arm",
        _ => "x64",
    }
}

// ── Mojang download path ──────────────────────────────────────────────────────

async fn try_mojang(
    options: &LaunchOptions,
    component: &str,
    platform: &str,
    runtime_root: &Path,
    client: &reqwest::Client,
    event_tx: &Sender<LaunchEvent>,
) -> Result<Option<JavaDownloadResult>, LaunchError> {
    let all_text = match fetch_text(client, ALL_JSON_URL).await {
        Ok(t) => t,
        Err(_) => return Ok(None),
    };

    let all: HashMap<String, HashMap<String, Vec<JavaVersionManifest>>> =
        serde_json::from_str(&all_text)?;

    let manifest_url = all
        .get(platform)
        .and_then(|p| p.get(component))
        .and_then(|versions| versions.first())
        .and_then(|v| v.manifest.as_ref())
        .map(|m| m.url.clone());

    let manifest_url = match manifest_url {
        Some(url) => url,
        None => return Ok(None),
    };

    let manifest_text = fetch_text(client, &manifest_url)
        .await
        .map_err(LaunchError::InvalidData)?;

    let manifest: JavaManifestData = serde_json::from_str(&manifest_text)?;

    let mut items: Vec<DownloadItem> = Vec::new();
    let mut file_records: Vec<JavaFileItem> = Vec::new();

    for (rel_path, entry) in &manifest.files {
        if entry.file_type != "file" {
            continue;
        }
        let raw = match entry.downloads.as_ref().and_then(|d| d.raw.as_ref()) {
            Some(r) => r,
            None => continue,
        };

        let dest = runtime_root.join(rel_path);
        let folder = dest
            .parent()
            .map(|p| p.to_path_buf())
            .unwrap_or_else(|| runtime_root.to_path_buf());

        items.push(DownloadItem {
            url: raw.url.clone(),
            path: dest,
            folder,
            name: rel_path.clone(),
            size: raw.size,
            r#type: Some("java".into()),
            sha1: Some(raw.sha1.clone()),
        });

        file_records.push(JavaFileItem {
            path: rel_path.clone(),
            executable: entry.executable,
            sha1: Some(raw.sha1.clone()),
            size: Some(raw.size),
            url: Some(raw.url.clone()),
            file_type: Some("file".into()),
        });
    }

    let downloader = Downloader::new(options.timeout_secs, options.download_concurrency);
    downloader.download_multiple(items, event_tx.clone()).await?;

    #[cfg(unix)]
    for (rel_path, entry) in &manifest.files {
        if entry.executable == Some(true) {
            use std::os::unix::fs::PermissionsExt;
            let path = runtime_root.join(rel_path);
            if path.exists() {
                let perms = std::fs::Permissions::from_mode(0o755);
                let _ = std::fs::set_permissions(&path, perms);
            }
        }
    }

    // Find the java binary by scanning manifest entries — some Mojang runtimes
    // on macOS use a bundle layout (e.g. jre.bundle/Contents/Home/bin/java)
    // rather than the flat bin/java expected by java_bin_path.
    let java_bin = manifest.files.iter()
        .filter_map(|(rel_path, entry)| {
            if entry.executable != Some(true) {
                return None;
            }
            let p = std::path::Path::new(rel_path);
            let fname = p.file_name()?.to_str()?;
            let in_bin = p.parent()?.file_name()?.to_str()? == "bin";
            if in_bin && (fname == "java" || fname == "javaw.exe") {
                Some(runtime_root.join(rel_path))
            } else {
                None
            }
        })
        .next()
        .unwrap_or_else(|| java_bin_path(runtime_root));

    Ok(Some(JavaDownloadResult {
        java_path: java_bin.to_string_lossy().into_owned(),
        files: file_records,
    }))
}

// ── Adoptium fallback ─────────────────────────────────────────────────────────

async fn get_from_adoptium(
    options: &LaunchOptions,
    _component: &str,
    runtime_root: &Path,
    major_version: u32,
    client: &reqwest::Client,
    event_tx: &Sender<LaunchEvent>,
) -> Result<JavaDownloadResult, LaunchError> {
    let os = adoptium_os();
    let arch = adoptium_arch(options.intel_enabled_mac);
    let image_type = &options.java.image_type;

    let url = format!(
        "{ADOPTIUM_API_BASE}/{major_version}/hotspot?os={os}&architecture={arch}&image_type={image_type}&jvm_impl=hotspot&vendor=eclipse"
    );

    let releases: Vec<AdoptiumRelease> = fetch_json(client, &url)
        .await
        .map_err(LaunchError::InvalidData)?;

    let release = releases.into_iter().next().ok_or_else(|| {
        LaunchError::Io(std::io::Error::new(
            std::io::ErrorKind::NotFound,
            format!("No Adoptium release found for Java {major_version} on {os}/{arch}"),
        ))
    })?;

    let pkg = release.binary.package;
    let is_windows = cfg!(target_os = "windows");
    let ext = if is_windows { "zip" } else { "tar.gz" };
    let archive_path = runtime_root.join(format!("adoptium-jre.{ext}"));

    if let Some(parent) = archive_path.parent() {
        tokio::fs::create_dir_all(parent).await?;
    }

    let item = DownloadItem {
        url: pkg.link.clone(),
        path: archive_path.clone(),
        folder: runtime_root.to_path_buf(),
        name: pkg.name.clone(),
        size: 0,
        r#type: Some("java".into()),
        sha1: None,
    };

    let downloader = Downloader::new(options.timeout_secs, 1);
    downloader.download_multiple(vec![item], event_tx.clone()).await?;

    if is_windows {
        extract_zip_to(archive_path.clone(), runtime_root).await?;
    } else {
        extract_tar_gz(archive_path.clone(), runtime_root.to_path_buf(), 1).await?;
    }

    let _ = tokio::fs::remove_file(&archive_path).await;

    let java_bin = java_bin_path(runtime_root);

    #[cfg(unix)]
    if java_bin.exists() {
        use std::os::unix::fs::PermissionsExt;
        let perms = std::fs::Permissions::from_mode(0o755);
        let _ = std::fs::set_permissions(&java_bin, perms);
    }

    Ok(JavaDownloadResult {
        java_path: java_bin.to_string_lossy().into_owned(),
        files: vec![JavaFileItem {
            path: java_bin.to_string_lossy().into_owned(),
            executable: Some(true),
            sha1: None,
            size: None,
            url: Some(pkg.link),
            file_type: Some("file".into()),
        }],
    })
}

// ── ZIP extraction (Windows) ──────────────────────────────────────────────────

async fn extract_zip_to(archive: PathBuf, dest: &Path) -> Result<(), LaunchError> {
    let dest = dest.to_path_buf();
    tokio::task::spawn_blocking(move || -> Result<(), LaunchError> {
        let file = std::fs::File::open(&archive)?;
        let mut zip = zip::ZipArchive::new(file).map_err(|e| {
            std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string())
        })?;
        for i in 0..zip.len() {
            let mut entry = zip.by_index(i).map_err(|e| {
                std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string())
            })?;
            if entry.is_dir() {
                continue;
            }
            let name = entry.name().to_owned();
            let stripped = name.splitn(2, '/').nth(1).unwrap_or(&name).to_owned();
            let out = dest.join(&stripped);
            if let Some(parent) = out.parent() {
                std::fs::create_dir_all(parent)?;
            }
            let mut f = std::fs::File::create(&out)?;
            std::io::copy(&mut entry, &mut f)?;
        }
        Ok(())
    })
    .await
    .map_err(|e| LaunchError::Io(std::io::Error::new(std::io::ErrorKind::Other, e.to_string())))??;
    Ok(())
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::PathBuf;

    fn bare_version() -> MinecraftVersionJson {
        MinecraftVersionJson {
            id: "1.20.4".into(),
            version_type: "release".into(),
            assets: None,
            asset_index: None,
            downloads: None,
            libraries: vec![],
            arguments: None,
            minecraft_arguments: None,
            java_version: None,
            main_class: None,
            has_natives: false,
        }
    }

    fn bare_options() -> LaunchOptions {
        use crate::launcher::options::{JavaOptions, LoaderConfig, MemoryConfig, ScreenConfig};
        use crate::models::minecraft::Authenticator;
        LaunchOptions {
            path: PathBuf::from("/mc"),
            version: "1.20.4".into(),
            authenticator: Authenticator {
                access_token: "tok".into(),
                name: "Player".into(),
                uuid: "uuid".into(),
                xbox_account: None,
                user_properties: None,
                client_id: None,
                client_token: None,
            },
            timeout_secs: 10,
            download_concurrency: 5,
            verify_concurrency: 4,
            memory: MemoryConfig::default(),
            java: JavaOptions::default(),
            loader: LoaderConfig::default(),
            screen: ScreenConfig::default(),
            verify: false,
            game_args: vec![],
            jvm_args: vec![],
            instance: None,
            url: None,
            mcp: None,
            intel_enabled_mac: false,
            bypass_offline: false,
            skip_bundle_check: false,
        }
    }

    #[test]
    fn java_component_defaults_when_no_java_version() {
        let opts = bare_options();
        let vj = bare_version();
        let (comp, major) = java_component(&opts, &vj);
        assert_eq!(comp, "jre-8");
        assert_eq!(major, 8);
    }

    #[test]
    fn java_component_uses_version_json() {
        use crate::models::minecraft::JavaVersionInfo;
        let opts = bare_options();
        let mut vj = bare_version();
        vj.java_version = Some(JavaVersionInfo {
            component: Some("java-runtime-gamma".into()),
            major_version: Some(17),
        });
        let (comp, major) = java_component(&opts, &vj);
        assert_eq!(comp, "jre-17");
        assert_eq!(major, 17);
    }

    #[test]
    fn java_component_java_option_overrides_version_json() {
        use crate::models::minecraft::JavaVersionInfo;
        let mut opts = bare_options();
        opts.java.version = Some("21".into());
        let mut vj = bare_version();
        vj.java_version = Some(JavaVersionInfo {
            component: Some("java-runtime-gamma".into()),
            major_version: Some(17),
        });
        let (comp, major) = java_component(&opts, &vj);
        assert_eq!(comp, "jre-21");
        assert_eq!(major, 21);
    }

    #[test]
    fn java_bin_path_is_runtime_root_bin_java() {
        let root = PathBuf::from("/mc/runtime/jre-legacy/linux");
        let bin = java_bin_path(&root);
        let path_str = bin.to_string_lossy();
        // Must be exactly runtime_root/bin/java — no extra component segment.
        assert!(path_str.ends_with("java") || path_str.ends_with("javaw.exe"));
        assert!(path_str.contains("/bin/"));
        assert!(!path_str[root.to_str().unwrap().len()..].contains("jre-legacy"),
            "component name must not appear after runtime_root: {path_str}");
    }

    #[test]
    fn mojang_platform_key_returns_non_empty() {
        let key = mojang_platform_key(false);
        assert!(!key.is_empty());
    }

    #[test]
    fn mojang_platform_key_intel_mac_overrides_arm() {
        // On any platform intel_enabled_mac=true must not produce the arm64 key.
        let key = mojang_platform_key(true);
        assert_ne!(key, "mac-os-arm64");
    }

    #[tokio::test]
    async fn get_java_files_respects_custom_java_path() {
        use crate::launcher::options::JavaOptions;
        use tokio::sync::mpsc;
        let mut opts = bare_options();
        opts.java = JavaOptions {
            path: Some(PathBuf::from("/usr/bin/java")),
            version: None,
            image_type: "jre".into(),
        };
        let client = reqwest::Client::new();
        let (tx, _rx) = mpsc::channel(16);
        let result = get_java_files(&opts, &bare_version(), &client, &tx)
            .await
            .unwrap();
        assert_eq!(result.java_path, "/usr/bin/java");
        assert!(result.files.is_empty());
    }

    #[tokio::test]
    async fn get_java_files_returns_cached_when_binary_exists() {
        use tempfile::TempDir;
        use tokio::sync::mpsc;

        let dir = TempDir::new().unwrap();
        let mut opts = bare_options();
        opts.path = dir.path().to_path_buf();

        let (comp, _) = java_component(&opts, &bare_version());
        let platform = mojang_platform_key(false);
        let runtime_root = dir.path().join("runtime").join(&comp).join(&platform);
        let bin_dir = runtime_root.join("bin");
        tokio::fs::create_dir_all(&bin_dir).await.unwrap();
        tokio::fs::write(bin_dir.join("java"), b"#!/bin/sh\nexec java").await.unwrap();

        let client = reqwest::Client::new();
        let (tx, _rx) = mpsc::channel(16);
        let result = get_java_files(&opts, &bare_version(), &client, &tx)
            .await
            .unwrap();

        assert!(result.java_path.contains("java"));
        assert!(result.files.is_empty());
    }
}