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:
1 parent
19887643bc
commit
412e6ff91a
12 files changed
+395
-74
No files matched your search
@@ -8,4 +8,5 @@ pub mod model;
|
||||
pub mod render;
|
||||
pub mod resolve;
|
||||
pub mod rules;
|
||||
pub mod system;
|
||||
pub mod templates;
|
||||
@@ -31,6 +31,13 @@ pub struct LaunchProfile {
|
||||
pub time: Option<String>,
|
||||
#[serde(rename = "type")]
|
||||
pub type_: Option<String>,
|
||||
// present only in rmcl <= 0.3.0's stripped loader-profile shape.
|
||||
// we deserialize it so the launch-time legacy-detection predicate
|
||||
// can confirm "this really is our old format, not an upstream
|
||||
// profile that happens to omit arguments". skipped on serialize so
|
||||
// we never propagate this field outward.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub game_arguments: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Default, Deserialize, Serialize)]
|
||||
|
||||
@@ -126,6 +126,7 @@ mod tests {
|
||||
downloads: None,
|
||||
release_time: None,
|
||||
time: None,
|
||||
game_arguments: None,
|
||||
type_: None,
|
||||
}
|
||||
}
|
||||
@@ -140,6 +141,7 @@ mod tests {
|
||||
let features = FeatureSet::default();
|
||||
let rule_ctx = RuleContext {
|
||||
os_name: "linux",
|
||||
os_version: "6.0",
|
||||
arch: "x86_64",
|
||||
features: &features,
|
||||
};
|
||||
@@ -167,6 +169,7 @@ mod tests {
|
||||
let features = FeatureSet::default();
|
||||
let rule_ctx = RuleContext {
|
||||
os_name: "linux",
|
||||
os_version: "6.0",
|
||||
arch: "x86_64",
|
||||
features: &features,
|
||||
};
|
||||
@@ -197,6 +200,7 @@ mod tests {
|
||||
let features = FeatureSet::default();
|
||||
let rule_ctx = RuleContext {
|
||||
os_name: "linux",
|
||||
os_version: "6.0",
|
||||
arch: "x86_64",
|
||||
features: &features,
|
||||
};
|
||||
@@ -207,6 +211,7 @@ mod tests {
|
||||
os: Some(OsCondition {
|
||||
name: Some("osx".into()),
|
||||
arch: None,
|
||||
version: None,
|
||||
}),
|
||||
features: None,
|
||||
}],
|
||||
@@ -236,6 +241,7 @@ mod tests {
|
||||
let features = FeatureSet::default();
|
||||
let rule_ctx = RuleContext {
|
||||
os_name: "linux",
|
||||
os_version: "6.0",
|
||||
arch: "x86_64",
|
||||
features: &features,
|
||||
};
|
||||
@@ -246,6 +252,7 @@ mod tests {
|
||||
os: Some(OsCondition {
|
||||
name: Some("linux".into()),
|
||||
arch: None,
|
||||
version: None,
|
||||
}),
|
||||
features: None,
|
||||
}],
|
||||
@@ -278,6 +285,7 @@ mod tests {
|
||||
let features = FeatureSet::default();
|
||||
let rule_ctx = RuleContext {
|
||||
os_name: "linux",
|
||||
os_version: "6.0",
|
||||
arch: "x86_64",
|
||||
features: &features,
|
||||
};
|
||||
@@ -289,6 +297,102 @@ mod tests {
|
||||
assert!(matches!(result, Err(RenderError::MissingMainClass)));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn end_to_end_resolve_then_render_modern_forge_shape() {
|
||||
// exercises the full pipeline: load a synthetic vanilla profile
|
||||
// from disk, load a synthetic loader profile with inheritsFrom,
|
||||
// resolve the chain, then render args. catches integration bugs
|
||||
// that unit tests of each layer would miss.
|
||||
use crate::launch_profile::resolve;
|
||||
use tempfile::TempDir;
|
||||
|
||||
let tmp = TempDir::new().unwrap();
|
||||
let vanilla_path = tmp.path().join("versions").join("1.20.1").join("meta.json");
|
||||
std::fs::create_dir_all(vanilla_path.parent().unwrap()).unwrap();
|
||||
let vanilla_json = br#"{
|
||||
"id": "1.20.1",
|
||||
"mainClass": "net.minecraft.client.main.Main",
|
||||
"libraries": [
|
||||
{
|
||||
"name": "org.lwjgl:lwjgl:3.3.1",
|
||||
"downloads": {
|
||||
"artifact": {
|
||||
"url": "https://example.invalid/lwjgl.jar",
|
||||
"path": "org/lwjgl/lwjgl/3.3.1/lwjgl-3.3.1.jar",
|
||||
"sha1": "1111111111111111111111111111111111111111",
|
||||
"size": 100
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"arguments": {
|
||||
"game": ["--username", "${auth_player_name}", "--version", "${version_name}"],
|
||||
"jvm": ["-Djava.library.path=${natives_directory}"]
|
||||
}
|
||||
}"#;
|
||||
std::fs::write(&vanilla_path, vanilla_json).unwrap();
|
||||
|
||||
let loader_json = r#"{
|
||||
"id": "1.20.1-forge-47.2.0",
|
||||
"inheritsFrom": "1.20.1",
|
||||
"mainClass": "cpw.mods.bootstraplauncher.BootstrapLauncher",
|
||||
"libraries": [
|
||||
{ "name": "net.minecraftforge:forge:47.2.0" }
|
||||
],
|
||||
"arguments": {
|
||||
"game": ["--launchTarget", "forge_client"],
|
||||
"jvm": [
|
||||
"--add-opens", "java.base/sun.security.util=cpw.mods.securejarhandler"
|
||||
]
|
||||
}
|
||||
}"#;
|
||||
let loader_profile: LaunchProfile = serde_json::from_str(loader_json).unwrap();
|
||||
|
||||
let merged = resolve::resolve(loader_profile, tmp.path()).await.unwrap();
|
||||
|
||||
let lib = PathBuf::from("/m/libraries");
|
||||
let nat = PathBuf::from("/m/natives");
|
||||
let game = PathBuf::from("/i/.minecraft");
|
||||
let assets = PathBuf::from("/m/assets");
|
||||
let template_ctx = template_fixture(&lib, &nat, &game, &assets);
|
||||
let features = FeatureSet::default();
|
||||
let rule_ctx = RuleContext {
|
||||
os_name: "linux",
|
||||
os_version: "6.0",
|
||||
arch: "x86_64",
|
||||
features: &features,
|
||||
};
|
||||
|
||||
let rendered = render_args(&merged, &rule_ctx, &template_ctx).unwrap();
|
||||
|
||||
// child main_class wins after merge
|
||||
assert_eq!(
|
||||
rendered.main_class,
|
||||
"cpw.mods.bootstraplauncher.BootstrapLauncher"
|
||||
);
|
||||
// game args: parent first then child
|
||||
assert_eq!(
|
||||
rendered.game,
|
||||
vec![
|
||||
"--username",
|
||||
"Player",
|
||||
"--version",
|
||||
"1.20.1",
|
||||
"--launchTarget",
|
||||
"forge_client"
|
||||
]
|
||||
);
|
||||
// jvm args: parent first then child
|
||||
assert_eq!(
|
||||
rendered.jvm,
|
||||
vec![
|
||||
"-Djava.library.path=/m/natives",
|
||||
"--add-opens",
|
||||
"java.base/sun.security.util=cpw.mods.securejarhandler"
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn modern_arguments_takes_precedence_over_legacy_field() {
|
||||
// a profile that somehow has both arguments and minecraft_arguments
|
||||
@@ -301,6 +405,7 @@ mod tests {
|
||||
let features = FeatureSet::default();
|
||||
let rule_ctx = RuleContext {
|
||||
os_name: "linux",
|
||||
os_version: "6.0",
|
||||
arch: "x86_64",
|
||||
features: &features,
|
||||
};
|
||||
|
||||
@@ -56,6 +56,7 @@ pub fn merge_into(child: LaunchProfile, parent: LaunchProfile) -> LaunchProfile
|
||||
downloads: child.downloads.or(parent.downloads),
|
||||
release_time: child.release_time.or(parent.release_time),
|
||||
time: child.time.or(parent.time),
|
||||
game_arguments: None,
|
||||
type_: child.type_.or(parent.type_),
|
||||
}
|
||||
}
|
||||
@@ -88,7 +89,10 @@ fn merge_libraries(
|
||||
parent: Vec<crate::launch_profile::model::Library>,
|
||||
) -> Vec<crate::launch_profile::model::Library> {
|
||||
use std::collections::HashSet;
|
||||
let child_keys: HashSet<String> = child.iter().map(|l| coord_key(&l.name).to_string()).collect();
|
||||
let child_keys: HashSet<String> = child
|
||||
.iter()
|
||||
.map(|l| coord_key(&l.name).to_string())
|
||||
.collect();
|
||||
|
||||
let mut out: Vec<crate::launch_profile::model::Library> = parent
|
||||
.into_iter()
|
||||
@@ -171,6 +175,7 @@ mod tests {
|
||||
downloads: None,
|
||||
release_time: None,
|
||||
time: None,
|
||||
game_arguments: None,
|
||||
type_: None,
|
||||
}
|
||||
}
|
||||
@@ -190,6 +195,7 @@ mod tests {
|
||||
os: Some(crate::launch_profile::rules::OsCondition {
|
||||
name: Some("linux".into()),
|
||||
arch: None,
|
||||
version: None,
|
||||
}),
|
||||
features: None,
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
// system-detection helpers shared by launch and install paths. mojang
|
||||
// names some things differently from rust's std::env::consts (e.g. macOS
|
||||
// is "osx" in mojang profile rules), so this module is the single source
|
||||
// of truth for translating.
|
||||
|
||||
pub fn mojang_os_name() -> &'static str {
|
||||
match std::env::consts::OS {
|
||||
"macos" => "osx",
|
||||
other => other,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn mojang_arch_name() -> &'static str {
|
||||
match std::env::consts::ARCH {
|
||||
"x86" => "x86",
|
||||
"x86_64" => "x86_64",
|
||||
"aarch64" => "arm64",
|
||||
other => other,
|
||||
}
|
||||
}
|
||||
|
||||
// the host OS version string. mojang rules occasionally constrain natives
|
||||
// selection on os.version with a regex (e.g. macOS 10.x-only natives).
|
||||
// rust's stdlib doesn't expose this directly, so we synthesise something
|
||||
// useful per-platform. on linux we read the kernel version; on macOS the
|
||||
// product version is more useful but reading it requires shelling out so
|
||||
// we fall back to the kernel version. on windows we use a similar
|
||||
// approach. real-world profiles using os.version are rare; if this helper
|
||||
// returns an empty string the rule evaluator treats version constraints
|
||||
// as non-matching (defensive default).
|
||||
pub fn mojang_os_version() -> String {
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::process::Command;
|
||||
if let Ok(out) = Command::new("uname").arg("-r").output()
|
||||
&& out.status.success()
|
||||
{
|
||||
return String::from_utf8_lossy(&out.stdout).trim().to_string();
|
||||
}
|
||||
}
|
||||
#[cfg(windows)]
|
||||
{
|
||||
// crude - sufficient for the rare profile that needs windows version
|
||||
// gating. real launchers use GetVersionEx via winapi; we can add
|
||||
// that later if needed.
|
||||
return std::env::var("OS").unwrap_or_default();
|
||||
}
|
||||
String::new()
|
||||
}
|
||||
@@ -62,6 +62,15 @@ pub fn substitute(input: &str, ctx: &TemplateContext) -> String {
|
||||
out
|
||||
}
|
||||
|
||||
// quick-play templates (`${quickPlayPath}`, `${quickPlaySingleplayer}`,
|
||||
// `${quickPlayMultiplayer}`, `${quickPlayRealms}`) are intentionally not
|
||||
// listed here. they only appear in `arguments.game` entries gated on
|
||||
// `is_quick_play_*` feature flags; since rmcl never sets those flags
|
||||
// (FeatureSet defaults to None across the board), the surrounding
|
||||
// conditional argument is filtered out by the rule evaluator before
|
||||
// template substitution even runs. if we ever expose quick-play to users,
|
||||
// add the variables here AND set the corresponding feature flags in
|
||||
// RuleContext at launch.
|
||||
fn lookup(name: &str, ctx: &TemplateContext) -> Option<String> {
|
||||
Some(match name {
|
||||
"library_directory" => ctx.library_directory.display().to_string(),
|
||||
|
||||
Reference in new issue
Block a user