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

+87
View File
@@ -16,6 +16,10 @@ pub enum RuleAction {
pub struct OsCondition {
pub name: Option<String>,
pub arch: Option<String>,
// mojang occasionally constrains natives selection on os.version with a
// regex. rare in practice - when present, it's a substring/anchor match
// against the host OS version reported by `system::mojang_os_version`.
pub version: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Default, Deserialize, Serialize)]
@@ -34,6 +38,7 @@ pub struct Rule {
pub struct RuleContext<'a> {
pub os_name: &'a str,
pub os_version: &'a str,
pub arch: &'a str,
pub features: &'a FeatureSet,
}
@@ -63,6 +68,11 @@ fn rule_matches(rule: &Rule, ctx: &RuleContext) -> bool {
{
return false;
}
if let Some(pattern) = &os.version
&& !os_version_matches(pattern, ctx.os_version)
{
return false;
}
}
if let Some(required) = &rule.features
&& !features_match(required, ctx.features)
@@ -72,6 +82,25 @@ fn rule_matches(rule: &Rule, ctx: &RuleContext) -> bool {
true
}
// mojang's os.version constraints are typically anchored regex patterns
// (e.g. `^10\\.`). we do a substring containment check as a defensive
// approximation that doesn't pull in the `regex` crate. when the host
// os_version is empty (Windows fallback path returns ""), version-gated
// rules don't match - which is the conservative default.
fn os_version_matches(pattern: &str, host_version: &str) -> bool {
if host_version.is_empty() {
return false;
}
// strip common regex anchors and metacharacters for substring lookup.
// good enough for the rare profile that uses os.version.
let needle = pattern
.trim_start_matches('^')
.trim_end_matches('$')
.trim_end_matches('.')
.trim_end_matches('\\');
host_version.contains(needle)
}
fn features_match(required: &FeatureSet, current: &FeatureSet) -> bool {
if let Some(want) = required.is_demo_user
&& current.is_demo_user.unwrap_or(false) != want
@@ -93,6 +122,7 @@ mod tests {
fn linux_ctx<'a>(features: &'a FeatureSet) -> RuleContext<'a> {
RuleContext {
os_name: "linux",
os_version: "6.0",
arch: "x86_64",
features,
}
@@ -112,6 +142,7 @@ mod tests {
os: Some(OsCondition {
name: Some("linux".into()),
arch: None,
version: None,
}),
features: None,
}];
@@ -127,6 +158,7 @@ mod tests {
os: Some(OsCondition {
name: Some("linux".into()),
arch: None,
version: None,
}),
features: None,
}];
@@ -144,6 +176,7 @@ mod tests {
os: Some(OsCondition {
name: Some("windows".into()),
arch: None,
version: None,
}),
features: None,
}];
@@ -165,6 +198,7 @@ mod tests {
os: Some(OsCondition {
name: Some("osx".into()),
arch: None,
version: None,
}),
features: None,
},
@@ -172,6 +206,7 @@ mod tests {
let features = FeatureSet::default();
let osx_ctx = RuleContext {
os_name: "osx",
os_version: "6.0",
arch: "x86_64",
features: &features,
};
@@ -194,6 +229,7 @@ mod tests {
os: Some(OsCondition {
name: Some("linux".into()),
arch: None,
version: None,
}),
features: None,
},
@@ -210,6 +246,7 @@ mod tests {
os: Some(OsCondition {
name: Some("linux".into()),
arch: Some("arm64".into()),
version: None,
}),
features: None,
}];
@@ -235,6 +272,7 @@ mod tests {
};
let ctx = RuleContext {
os_name: "linux",
os_version: "6.0",
arch: "x86_64",
features: &demo_features,
};
@@ -268,6 +306,55 @@ mod tests {
assert!(evaluate(&rules, &ctx));
}
#[test]
fn os_version_pattern_matches_against_host() {
let rules = vec![Rule {
action: RuleAction::Allow,
os: Some(OsCondition {
name: Some("osx".into()),
arch: None,
version: Some("^10\\.".into()),
}),
features: None,
}];
let features = FeatureSet::default();
let ctx_match = RuleContext {
os_name: "osx",
os_version: "10.15.7",
arch: "x86_64",
features: &features,
};
assert!(evaluate(&rules, &ctx_match));
let ctx_mismatch = RuleContext {
os_name: "osx",
os_version: "13.2.1",
arch: "x86_64",
features: &features,
};
assert!(!evaluate(&rules, &ctx_mismatch));
}
#[test]
fn os_version_pattern_does_not_match_when_host_unknown() {
let rules = vec![Rule {
action: RuleAction::Allow,
os: Some(OsCondition {
name: Some("windows".into()),
arch: None,
version: Some("^10\\.".into()),
}),
features: None,
}];
let features = FeatureSet::default();
let ctx = RuleContext {
os_name: "windows",
os_version: "",
arch: "x86_64",
features: &features,
};
assert!(!evaluate(&rules, &ctx));
}
#[test]
fn rule_deserializes_from_mojang_json() {
// shape lifted from real mojang library rules.