minecraft_java_rs_core/loader/
quilt.rs1use tokio::sync::mpsc::Sender;
2
3use crate::error::LoaderError;
4use crate::launcher::events::LaunchEvent;
5use crate::launcher::options::LaunchOptions;
6use crate::models::loader::{FabricJson, QuiltMeta};
7use crate::models::minecraft::AssetItem;
8use crate::net::downloader::{DownloadItem, Downloader};
9use crate::net::http::fetch_json;
10use crate::utils::paths::get_path_libraries;
11
12use super::fabric::resolve_lib_url;
13
14const QUILT_META: &str = "https://meta.quiltmc.org/v3/versions";
15const QUILT_PROFILE: &str =
16 "https://meta.quiltmc.org/v3/versions/loader/${version}/${build}/profile/json";
17
18pub struct QuiltMC;
21
22impl QuiltMC {
23 pub fn new() -> Self {
24 Self
25 }
26
27 pub async fn download_json(
34 &self,
35 mc_version: &str,
36 build: &str,
37 client: &reqwest::Client,
38 ) -> Result<FabricJson, LoaderError> {
39 let meta: QuiltMeta = fetch_json(client, QUILT_META)
40 .await
41 .map_err(LoaderError::ApiError)?;
42
43 if !meta.game.iter().any(|g| g.version == mc_version) {
44 return Err(LoaderError::VersionNotFound(format!(
45 "QuiltMC doesn't support Minecraft {mc_version}"
46 )));
47 }
48
49 let build_ver = match build {
50 "latest" => meta
51 .loader
52 .first()
53 .map(|b| b.version.clone())
54 .ok_or_else(|| LoaderError::VersionNotFound("No Quilt builds available".into()))?,
55 "recommended" => meta
56 .loader
57 .iter()
58 .find(|b| !b.version.contains("beta"))
59 .map(|b| b.version.clone())
60 .ok_or_else(|| {
61 LoaderError::VersionNotFound("No stable Quilt build found".into())
62 })?,
63 ver => meta
64 .loader
65 .iter()
66 .find(|b| b.version == ver)
67 .map(|b| b.version.clone())
68 .ok_or_else(|| {
69 let available: Vec<_> = meta.loader.iter().map(|b| b.version.as_str()).collect();
70 LoaderError::VersionNotFound(format!(
71 "Quilt build {ver} not found. Available: {}",
72 available.join(", ")
73 ))
74 })?,
75 };
76
77 let profile_url = QUILT_PROFILE
78 .replace("${version}", mc_version)
79 .replace("${build}", &build_ver);
80
81 let json: FabricJson = fetch_json(client, &profile_url)
82 .await
83 .map_err(LoaderError::ApiError)?;
84
85 Ok(json)
86 }
87
88 pub async fn download_libraries(
92 &self,
93 options: &LaunchOptions,
94 quilt_json: &FabricJson,
95 _client: &reqwest::Client,
96 event_tx: &Sender<LaunchEvent>,
97 ) -> Result<Vec<AssetItem>, LoaderError> {
98 let libs = &quilt_json.libraries;
99 let total = libs.len();
100 let mut items: Vec<AssetItem> = Vec::with_capacity(total);
101 let mut pending: Vec<DownloadItem> = Vec::new();
102
103 for (idx, lib) in libs.iter().enumerate() {
104 let _ = event_tx
105 .send(LaunchEvent::Check {
106 current: idx + 1,
107 total,
108 kind: "libraries".into(),
109 })
110 .await;
111
112 if lib.rules.is_some() {
113 continue;
114 }
115
116 let lib_info = match get_path_libraries(&lib.name, None, None) {
117 Ok(i) => i,
118 Err(_) => continue,
119 };
120
121 let folder = options
122 .loader_dir("quilt")
123 .join("libraries")
124 .join(&lib_info.path);
125 let dest = folder.join(&lib_info.name);
126 let url = resolve_lib_url(lib, &lib_info.path, &lib_info.name);
127
128 items.push(AssetItem::Asset {
129 path: dest.to_string_lossy().into_owned(),
130 sha1: lib
131 .downloads
132 .as_ref()
133 .and_then(|d| d.artifact.as_ref())
134 .and_then(|a| a.sha1.clone())
135 .unwrap_or_default(),
136 size: lib
137 .downloads
138 .as_ref()
139 .and_then(|d| d.artifact.as_ref())
140 .and_then(|a| a.size)
141 .unwrap_or(0),
142 url: url.clone(),
143 });
144
145 if !dest.exists() {
146 pending.push(DownloadItem {
147 url,
148 path: dest,
149 folder,
150 name: lib_info.name,
151 size: 0,
152 r#type: Some("libraries".into()),
153 sha1: None,
154 });
155 }
156 }
157
158 if !pending.is_empty() {
159 let downloader = Downloader::new(options.timeout_secs, options.download_concurrency);
160 downloader
161 .download_multiple(pending, event_tx.clone())
162 .await
163 .map_err(|e| {
164 LoaderError::Io(std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))
165 })?;
166 }
167
168 Ok(items)
169 }
170}
171
172impl Default for QuiltMC {
173 fn default() -> Self {
174 Self::new()
175 }
176}
177
178#[cfg(test)]
181mod tests {
182 use super::*;
183
184 #[test]
185 fn quilt_mc_constructs_without_options() {
186 let _q = QuiltMC::new();
187 }
188
189 #[test]
190 fn quilt_mc_default_same_as_new() {
191 let _q = QuiltMC::default();
192 }
193}