1use std::io::Read;
2use std::path::{Path, PathBuf};
3
4use serde::Deserialize;
5
6use crate::error::LaunchError;
7use crate::net::http::fetch_json;
8use crate::launcher::options::LaunchOptions;
9use crate::models::minecraft::{ArtifactInfo, AssetItem, Library, MinecraftVersionJson};
10use crate::utils::paths::get_path_libraries;
11use crate::utils::platform::{mojang_os, skip_library};
12
13pub fn get_libraries(
28 options: &LaunchOptions,
29 version_json: &MinecraftVersionJson,
30) -> Vec<AssetItem> {
31 let base = &options.path;
32 let current_os = mojang_os();
33 let arch_suffix = arch_suffix_for_natives();
34
35 let mut items: Vec<AssetItem> = Vec::new();
36
37 for lib in &version_json.libraries {
38 if let Some(natives_map) = &lib.natives {
39 let native_key = match natives_map.get(current_os) {
41 Some(k) => k.replace("${arch}", arch_suffix),
42 None => continue,
43 };
44
45 let artifact = lib
46 .downloads
47 .as_ref()
48 .and_then(|d| d.classifiers.as_ref())
49 .and_then(|c| c.get(&native_key));
50
51 if let Some(artifact) = artifact {
52 if let Some(item) = artifact_to_item(base, artifact, &lib.name, true) {
53 items.push(item);
54 }
55 }
56 } else {
57 if skip_library(lib.rules.as_deref().unwrap_or(&[])) {
59 continue;
60 }
61
62 if let Some(item) = resolve_regular_library(base, lib) {
63 items.push(item);
64 }
65 }
66 }
67
68 if let Some(dl) = &version_json.downloads {
70 items.push(AssetItem::Asset {
71 path: base
72 .join("versions")
73 .join(&version_json.id)
74 .join(format!("{}.jar", version_json.id))
75 .to_string_lossy()
76 .into_owned(),
77 sha1: dl.client.sha1.clone(),
78 size: dl.client.size,
79 url: dl.client.url.clone(),
80 });
81 }
82
83 if let Ok(content) = serde_json::to_string(version_json) {
85 items.push(AssetItem::CFile {
86 path: base
87 .join("versions")
88 .join(&version_json.id)
89 .join(format!("{}.json", version_json.id))
90 .to_string_lossy()
91 .into_owned(),
92 content,
93 });
94 }
95
96 items
97}
98
99pub async fn get_assets_others(
108 options: &LaunchOptions,
109 url: Option<&str>,
110 client: &reqwest::Client,
111) -> Result<Vec<AssetItem>, LaunchError> {
112 let url = match url {
113 Some(u) if !u.is_empty() => u,
114 _ => return Ok(vec![]),
115 };
116
117 let raw: Vec<CustomAssetItem> = fetch_json(client, url)
118 .await
119 .map_err(LaunchError::InvalidData)?;
120
121 let mut items = Vec::with_capacity(raw.len());
122
123 for asset in raw {
124 if asset.path.is_empty() {
125 continue;
126 }
127
128 let full_path = match &options.instance {
129 Some(inst) => options
130 .path
131 .join("instances")
132 .join(inst)
133 .join(&asset.path),
134 None => options.path.join(&asset.path),
135 };
136
137 items.push(AssetItem::Asset {
138 path: full_path.to_string_lossy().into_owned(),
139 sha1: asset.hash,
140 size: asset.size,
141 url: asset.url,
142 });
143 }
144
145 Ok(items)
146}
147
148pub async fn extract_natives(
155 options: &LaunchOptions,
156 version_json: &MinecraftVersionJson,
157 bundle: &[AssetItem],
158) -> Result<(), LaunchError> {
159 let native_paths: Vec<PathBuf> = bundle
160 .iter()
161 .filter_map(|item| match item {
162 AssetItem::NativeAsset { path, .. } => Some(PathBuf::from(path)),
163 _ => None,
164 })
165 .collect();
166
167 if native_paths.is_empty() {
168 return Ok(());
169 }
170
171 let natives_dir = options
172 .path
173 .join("versions")
174 .join(&version_json.id)
175 .join("natives");
176 tokio::fs::create_dir_all(&natives_dir).await?;
177
178 for jar_path in native_paths {
179 let dest = natives_dir.clone();
180 tokio::task::spawn_blocking(move || extract_jar_to_dir(&jar_path, &dest))
181 .await
182 .map_err(|e| LaunchError::Archive(e.to_string()))??;
183 }
184
185 Ok(())
186}
187
188fn arch_suffix_for_natives() -> &'static str {
195 match std::env::consts::ARCH {
196 "x86" => "32",
197 "x86_64" => "64",
198 _ => "",
199 }
200}
201
202fn artifact_to_item(
205 base: &Path,
206 artifact: &ArtifactInfo,
207 lib_name: &str,
208 is_native: bool,
209) -> Option<AssetItem> {
210 let rel = artifact.path.clone().or_else(|| {
211 get_path_libraries(lib_name, None, None)
212 .ok()
213 .map(|lp| lp.path)
214 })?;
215
216 let full_path = base
217 .join("libraries")
218 .join(&rel)
219 .to_string_lossy()
220 .into_owned();
221
222 let sha1 = artifact.sha1.clone().unwrap_or_default();
223 let size = artifact.size.unwrap_or(0);
224 let url = artifact.url.clone();
225
226 if is_native {
227 Some(AssetItem::NativeAsset { path: full_path, sha1, size, url })
228 } else {
229 Some(AssetItem::Asset { path: full_path, sha1, size, url })
230 }
231}
232
233fn resolve_regular_library(base: &Path, lib: &Library) -> Option<AssetItem> {
240 let is_native = lib.name.split(':').nth(3)
248 .map(|c| c.starts_with("natives-"))
249 .unwrap_or(false);
250
251 if let Some(artifact) = lib.downloads.as_ref().and_then(|d| d.artifact.as_ref()) {
253 return artifact_to_item(base, artifact, &lib.name, is_native);
254 }
255
256 if let Some(repo) = &lib.url {
258 if let Ok(lp) = get_path_libraries(&lib.name, None, None) {
259 let url = format!("{}/{}", repo.trim_end_matches('/'), lp.path);
260 return Some(AssetItem::Asset {
261 path: base
262 .join("libraries")
263 .join(&lp.path)
264 .to_string_lossy()
265 .into_owned(),
266 sha1: String::new(),
267 size: 0,
268 url,
269 });
270 }
271 }
272
273 None
274}
275
276fn extract_jar_to_dir(jar_path: &Path, dest: &Path) -> Result<(), LaunchError> {
280 let file = std::fs::File::open(jar_path)?;
281 let mut archive =
282 zip::ZipArchive::new(file).map_err(|e| LaunchError::Archive(e.to_string()))?;
283
284 for i in 0..archive.len() {
285 let mut entry = archive
286 .by_index(i)
287 .map_err(|e| LaunchError::Archive(e.to_string()))?;
288
289 let name = entry.name().to_string();
290
291 if name.starts_with("META-INF") {
292 continue;
293 }
294
295 let out = dest.join(&name);
296
297 if entry.is_dir() {
298 std::fs::create_dir_all(&out)?;
299 } else {
300 if let Some(parent) = out.parent() {
301 std::fs::create_dir_all(parent)?;
302 }
303 let mut data = Vec::with_capacity(entry.size() as usize);
304 entry.read_to_end(&mut data)?;
305 std::fs::write(&out, &data)?;
306
307 #[cfg(unix)]
308 {
309 use std::os::unix::fs::PermissionsExt;
310 let mut perms = std::fs::metadata(&out)?.permissions();
311 perms.set_mode(0o755);
312 std::fs::set_permissions(&out, perms)?;
313 }
314 }
315 }
316
317 Ok(())
318}
319
320#[derive(Deserialize)]
323struct CustomAssetItem {
324 path: String,
325 hash: String,
326 size: u64,
327 url: String,
328}
329
330#[cfg(test)]
333mod tests {
334 use super::*;
335 use std::io::Write;
336 use tempfile::TempDir;
337
338 use crate::launcher::options::{JavaOptions, LoaderConfig, MemoryConfig, ScreenConfig};
339 use crate::models::minecraft::{
340 ArtifactInfo, Authenticator, DownloadArtifact, LibraryDownloads, VersionDownloads,
341 };
342
343 fn opts(path: PathBuf) -> LaunchOptions {
344 LaunchOptions {
345 path,
346 version: "1.20.4".into(),
347 authenticator: Authenticator {
348 access_token: "tok".into(),
349 name: "Player".into(),
350 uuid: "uuid".into(),
351 xbox_account: None,
352 user_properties: None,
353 client_id: None,
354 client_token: None,
355 },
356 timeout_secs: 10,
357 download_concurrency: 5,
358 verify_concurrency: 4,
359 memory: MemoryConfig::default(),
360 java: JavaOptions::default(),
361 loader: LoaderConfig::default(),
362 screen: ScreenConfig::default(),
363 verify: false,
364 game_args: vec![],
365 jvm_args: vec![],
366 instance: None,
367 url: None,
368 mcp: None,
369 intel_enabled_mac: false,
370 bypass_offline: false,
371 }
372 }
373
374 fn bare_version() -> MinecraftVersionJson {
375 MinecraftVersionJson {
376 id: "1.20.4".into(),
377 version_type: "release".into(),
378 assets: None,
379 asset_index: None,
380 downloads: None,
381 libraries: vec![],
382 arguments: None,
383 minecraft_arguments: None,
384 java_version: None,
385 main_class: None,
386 has_natives: false,
387 }
388 }
389
390 fn artifact(path: &str, url: &str) -> ArtifactInfo {
391 ArtifactInfo {
392 path: Some(path.into()),
393 sha1: Some("aabbcc".into()),
394 size: Some(1024),
395 url: url.into(),
396 }
397 }
398
399 fn lib_with_artifact(name: &str, path: &str, url: &str) -> Library {
400 Library {
401 name: name.into(),
402 rules: None,
403 natives: None,
404 downloads: Some(LibraryDownloads {
405 artifact: Some(artifact(path, url)),
406 classifiers: None,
407 }),
408 url: None,
409 loader: None,
410 }
411 }
412
413 #[test]
416 fn includes_client_jar_when_downloads_present() {
417 let dir = TempDir::new().unwrap();
418 let mut vj = bare_version();
419 vj.downloads = Some(VersionDownloads {
420 client: DownloadArtifact {
421 sha1: "abc".into(),
422 size: 42,
423 url: "https://example.com/client.jar".into(),
424 },
425 server: None,
426 client_mappings: None,
427 server_mappings: None,
428 });
429
430 let items = get_libraries(&opts(dir.path().to_path_buf()), &vj);
431 assert!(items.iter().any(|i| matches!(i, AssetItem::Asset { path, .. } if path.ends_with("1.20.4.jar"))));
432 }
433
434 #[test]
435 fn includes_version_json_as_cfile() {
436 let dir = TempDir::new().unwrap();
437 let vj = bare_version();
438 let items = get_libraries(&opts(dir.path().to_path_buf()), &vj);
439 assert!(items.iter().any(|i| matches!(i, AssetItem::CFile { path, .. } if path.ends_with("1.20.4.json"))));
440 }
441
442 #[test]
443 fn regular_library_becomes_asset() {
444 let dir = TempDir::new().unwrap();
445 let mut vj = bare_version();
446 vj.libraries = vec![lib_with_artifact(
447 "com.example:lib:1.0",
448 "com/example/lib/1.0/lib-1.0.jar",
449 "https://example.com/lib.jar",
450 )];
451
452 let items = get_libraries(&opts(dir.path().to_path_buf()), &vj);
453 assert!(items.iter().any(|i| matches!(i, AssetItem::Asset { url, .. } if url == "https://example.com/lib.jar")));
454 }
455
456 #[test]
457 fn native_library_becomes_native_asset() {
458 let dir = TempDir::new().unwrap();
459 let mut vj = bare_version();
460
461 let current_os = mojang_os();
462 let classifier_key = format!("natives-{current_os}");
463
464 let mut classifiers = std::collections::HashMap::new();
465 classifiers.insert(
466 classifier_key.clone(),
467 artifact(
468 &format!("org/lwjgl/lwjgl/{classifier_key}/lwjgl-native.jar"),
469 "https://example.com/native.jar",
470 ),
471 );
472
473 let mut natives_map = std::collections::HashMap::new();
474 natives_map.insert(current_os.to_string(), classifier_key);
475
476 vj.libraries = vec![Library {
477 name: "org.lwjgl:lwjgl:3.3.1".into(),
478 rules: None,
479 natives: Some(natives_map),
480 downloads: Some(LibraryDownloads {
481 artifact: None,
482 classifiers: Some(classifiers),
483 }),
484 url: None,
485 loader: None,
486 }];
487
488 let items = get_libraries(&opts(dir.path().to_path_buf()), &vj);
489 assert!(items.iter().any(|i| matches!(i, AssetItem::NativeAsset { url, .. } if url == "https://example.com/native.jar")));
490 }
491
492 #[test]
493 fn modern_native_classifier_in_name_becomes_native_asset() {
494 let dir = TempDir::new().unwrap();
496 let mut vj = bare_version();
497
498 let current_os = mojang_os();
499 let classifier = format!("natives-{current_os}");
500 let lib_name = format!("org.lwjgl:lwjgl-glfw:3.3.2:{classifier}");
501 let jar_path = format!("org/lwjgl/lwjgl-glfw/3.3.2/lwjgl-glfw-3.3.2-{classifier}.jar");
502
503 vj.libraries = vec![Library {
504 name: lib_name,
505 rules: None,
506 natives: None,
507 downloads: Some(LibraryDownloads {
508 artifact: Some(artifact(&jar_path, "https://libraries.minecraft.net/native.jar")),
509 classifiers: None,
510 }),
511 url: None,
512 loader: None,
513 }];
514
515 let items = get_libraries(&opts(dir.path().to_path_buf()), &vj);
516 assert!(
517 items.iter().any(|i| matches!(i, AssetItem::NativeAsset { .. })),
518 "expected NativeAsset for modern natives-<os> classifier, got: {items:?}"
519 );
520 }
521
522 #[test]
523 fn library_with_url_fallback_builds_url() {
524 let dir = TempDir::new().unwrap();
525 let mut vj = bare_version();
526 vj.libraries = vec![Library {
527 name: "net.fabricmc:fabric-loader:0.15.0".into(),
528 rules: None,
529 natives: None,
530 downloads: None,
531 url: Some("https://maven.fabricmc.net".into()),
532 loader: None,
533 }];
534
535 let items = get_libraries(&opts(dir.path().to_path_buf()), &vj);
536 assert!(items.iter().any(|i| match i {
537 AssetItem::Asset { url, .. } => url.starts_with("https://maven.fabricmc.net"),
538 _ => false,
539 }));
540 }
541
542 #[tokio::test]
545 async fn get_assets_others_none_url_returns_empty() {
546 let dir = TempDir::new().unwrap();
547 let client = reqwest::Client::new();
548 let result = get_assets_others(&opts(dir.path().to_path_buf()), None, &client)
549 .await
550 .unwrap();
551 assert!(result.is_empty());
552 }
553
554 #[tokio::test]
555 async fn get_assets_others_empty_string_returns_empty() {
556 let dir = TempDir::new().unwrap();
557 let client = reqwest::Client::new();
558 let result = get_assets_others(&opts(dir.path().to_path_buf()), Some(""), &client)
559 .await
560 .unwrap();
561 assert!(result.is_empty());
562 }
563
564 #[tokio::test]
567 async fn extract_natives_noop_with_empty_bundle() {
568 let dir = TempDir::new().unwrap();
569 let vj = bare_version();
570 extract_natives(&opts(dir.path().to_path_buf()), &vj, &[])
571 .await
572 .unwrap();
573 assert!(!dir.path().join("versions").exists());
574 }
575
576 #[tokio::test]
577 async fn extract_natives_extracts_to_natives_dir() {
578 let dir = TempDir::new().unwrap();
580 let jar_path = dir.path().join("native.jar");
581
582 {
583 use zip::write::SimpleFileOptions;
584 let mut w = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
585 let opts_zip = SimpleFileOptions::default();
586
587 w.start_file("META-INF/MANIFEST.MF", opts_zip).unwrap();
588 w.write_all(b"Manifest-Version: 1.0\n").unwrap();
589
590 w.start_file("liblwjgl.so", opts_zip).unwrap();
591 w.write_all(b"ELF native library").unwrap();
592
593 let finished = w.finish().unwrap();
594 std::fs::write(&jar_path, finished.get_ref()).unwrap();
595 }
596
597 let vj = bare_version();
598 let options = opts(dir.path().to_path_buf());
599
600 let bundle = vec![AssetItem::NativeAsset {
601 path: jar_path.to_string_lossy().into_owned(),
602 sha1: String::new(),
603 size: 0,
604 url: String::new(),
605 }];
606
607 extract_natives(&options, &vj, &bundle).await.unwrap();
608
609 let natives_dir = dir.path().join("versions").join("1.20.4").join("natives");
610 assert!(natives_dir.join("liblwjgl.so").exists());
611 assert!(!natives_dir.join("META-INF").exists());
612
613 let content = std::fs::read(natives_dir.join("liblwjgl.so")).unwrap();
614 assert_eq!(content, b"ELF native library");
615 }
616}