fixed: review findings - robustness, dead code, missing tests

several smaller fixes from the code review:
 - add os_version to RuleContext + OsCondition for mojang rules that
   constrain natives selection by host version (rare, but in the spec).
 - factor mojang_os_name/arch/version into a shared launch_profile::system
   module instead of duplicating between net/mojang.rs and launch/mod.rs.
 - get_json and get_bytes now retry transient failures (5xx, connect,
   body decode) via a shared get_with_retry envelope; previously only
   download_file had retry logic.
 - migrate_legacy_meta_if_needed falls back to the stale cached profile
   with a warn log on network failure instead of erroring out (offline
   launches now work).
 - tightened legacy-loader detection by requiring the unique-to-rmcl
   gameArguments field; clearer error when loader_version is absent.
 - forge legacy versionInfo write uses compact serialization (no
   pretty-print key reorder).
 - deleted dead fetch_version_meta and save_profile_json.
 - documented quick-play templates omission in templates::lookup.
 - added end-to-end test that loads a vanilla profile from disk, loads a
   loader profile with inheritsFrom, resolves, renders, and snapshots.
This commit is contained in:
objz committed 2026-05-25 18:24:06 +02:00
1 parent 19887643bc
commit 412e6ff91a
12 files changed
+395 -74

No files matched your search

+14 -2
View File
@@ -266,13 +266,25 @@ pub(crate) async fn install_forge_from_profile(
}
set_action("Saving Forge profile...");
// write the installer's versionInfo verbatim. it already has the
// write the installer's versionInfo as compact JSON. it already has the
// mainClass, the full library list (with name + url for forge-hosted
// libs), and minecraftArguments (the legacy --tweakClass etc). the
// launch flow parses this as a LaunchProfile and - if there's no
// inheritsFrom field - implicitly inherits from the configured game
// version so vanilla libraries layer in via resolve().
crate::instance::loader::save_profile_json(meta_dir, profile_filename, version_info)?;
//
// we use serde_json::to_vec (not the pretty-print variant via
// save_profile_json) so the written file is content-faithful: every
// field present in the installer's versionInfo round-trips. key order
// and whitespace may differ from the original installer JSON because
// the source is a serde_json::Value (which doesn't preserve order),
// but no field is silently dropped.
let profiles_dir = meta_dir.join("loader-profiles");
std::fs::create_dir_all(&profiles_dir)?;
let profile_path = profiles_dir.join(profile_filename);
let serialized = serde_json::to_vec(version_info)
.map_err(|e| NetError::Parse(format!("Failed to serialize Forge profile: {e}")))?;
std::fs::write(&profile_path, &serialized)?;
Ok(())
}
+41 -2
View File
@@ -69,14 +69,53 @@ impl HttpClient {
}
pub async fn get_json<T: DeserializeOwned>(&self, url: &str) -> Result<T, NetError> {
Ok(self.get(url).await?.json().await?)
get_with_retry(self, url, |resp| async move { Ok(resp.json().await?) }).await
}
pub async fn get_bytes(&self, url: &str) -> Result<Vec<u8>, NetError> {
Ok(self.get(url).await?.bytes().await?.to_vec())
get_with_retry(
self,
url,
|resp| async move { Ok(resp.bytes().await?.to_vec()) },
)
.await
}
}
// shared retry envelope around `client.get(url).await? -> decode`. retries
// transient failures (timeouts, connect errors, 5xx) with exponential
// backoff. used by both get_json and get_bytes.
async fn get_with_retry<T, F, Fut>(client: &HttpClient, url: &str, decode: F) -> Result<T, NetError>
where
F: Fn(reqwest::Response) -> Fut,
Fut: std::future::Future<Output = Result<T, NetError>>,
{
let mut last_error = None;
for attempt in 0..=MAX_RETRIES {
if attempt > 0 {
let delay = RETRY_BASE_DELAY_MS * 2u64.pow(attempt - 1);
tracing::warn!(
"retrying request (attempt {}/{}): {}",
attempt + 1,
MAX_RETRIES + 1,
url
);
tokio::time::sleep(std::time::Duration::from_millis(delay)).await;
}
match client.get(url).await {
Ok(resp) => match decode(resp).await {
Ok(value) => return Ok(value),
Err(e) if is_retryable(&e) => last_error = Some(e),
Err(e) => return Err(e),
},
Err(e) if is_retryable(&e) => last_error = Some(e),
Err(e) => return Err(e),
}
}
Err(last_error.unwrap())
}
const MAX_RETRIES: u32 = 3;
const RETRY_BASE_DELAY_MS: u64 = 500;
+8 -30
View File
@@ -113,17 +113,10 @@ pub async fn fetch_version_manifest(client: &HttpClient) -> Result<VersionManife
client.get_json(MANIFEST_URL).await
}
pub async fn fetch_version_meta(
client: &HttpClient,
entry: &VersionEntry,
) -> Result<VersionMeta, NetError> {
client.get_json(&entry.url).await
}
// like fetch_version_meta but also returns the raw response bytes so the
// caller can write the upstream JSON byte-for-byte to disk. used by the
// install path so we don't lose data (e.g. arguments.jvm) by re-serializing
// through our narrow VersionMeta struct.
// fetches and parses a version's metadata. also returns the raw response
// bytes so the caller can write the upstream JSON byte-for-byte to disk
// - used by the install path so we don't lose data (e.g. arguments.jvm)
// by re-serializing through our narrow VersionMeta struct.
pub async fn fetch_version_meta_with_raw(
client: &HttpClient,
entry: &VersionEntry,
@@ -173,9 +166,11 @@ pub async fn download_libraries(
set_action("Downloading libraries...");
let features = crate::launch_profile::rules::FeatureSet::default();
let host_os_version = crate::launch_profile::system::mojang_os_version();
let rule_ctx = crate::launch_profile::rules::RuleContext {
os_name: mojang_os_name(),
arch: mojang_arch_name(),
os_name: crate::launch_profile::system::mojang_os_name(),
os_version: &host_os_version,
arch: crate::launch_profile::system::mojang_arch_name(),
features: &features,
};
@@ -293,23 +288,6 @@ pub async fn download_assets(
result
}
// mojang calls macOS "osx" because apparently it's still 2012
fn mojang_os_name() -> &'static str {
match std::env::consts::OS {
"macos" => "osx",
other => other,
}
}
fn mojang_arch_name() -> &'static str {
match std::env::consts::ARCH {
"x86" => "x86",
"x86_64" => "x86_64",
"aarch64" => "arm64",
other => other,
}
}
// bounded parallel downloader. spawns up to MAX_CONCURRENT_DOWNLOADS tasks
// and feeds new ones in as each completes. collects errors but keeps going
// so it downloads as much as possible before reporting the first failure.