refactor(instance): split launch module and compile java shim from source
This commit is contained in:
1 parent
edb8efd033
commit
ce34a28776
5 files changed
+533
-136
No files matched your search
@@ -1,5 +1,8 @@
|
||||
// builds the full java command line and spawns minecraft as a child process.
|
||||
// handles classpath assembly, auth token injection, and log capture.
|
||||
// loader-specific patches live in submodules (e.g. patches.rs for lwjgl3ify).
|
||||
|
||||
mod patches;
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
@@ -76,6 +79,13 @@ struct LoaderLibrary {
|
||||
name: String,
|
||||
}
|
||||
|
||||
struct GameAuth {
|
||||
username: String,
|
||||
uuid: String,
|
||||
token: String,
|
||||
user_type: String,
|
||||
}
|
||||
|
||||
// mojang's library rules are a fun little state machine: each rule can allow
|
||||
// or disallow based on OS. if no rule matches the current OS, the library is
|
||||
// included only if no rule "dominated" (matched at all). yes, it's weird.
|
||||
@@ -107,6 +117,38 @@ fn lib_allowed(lib: &MetaLibrary) -> bool {
|
||||
!dominated
|
||||
}
|
||||
|
||||
fn build_game_args(
|
||||
config: &InstanceConfig,
|
||||
minecraft_dir: &Path,
|
||||
meta_dir: &Path,
|
||||
asset_index_id: &str,
|
||||
auth: GameAuth,
|
||||
loader_game_args: Vec<String>,
|
||||
) -> Vec<String> {
|
||||
let mut game_args = vec![
|
||||
"--username".to_string(),
|
||||
auth.username,
|
||||
"--version".to_string(),
|
||||
config.game_version.clone(),
|
||||
"--gameDir".to_string(),
|
||||
minecraft_dir.to_string_lossy().into_owned(),
|
||||
"--assetsDir".to_string(),
|
||||
meta_dir.join("assets").to_string_lossy().into_owned(),
|
||||
"--assetIndex".to_string(),
|
||||
asset_index_id.to_string(),
|
||||
"--uuid".to_string(),
|
||||
auth.uuid,
|
||||
"--accessToken".to_string(),
|
||||
auth.token,
|
||||
"--userProperties".to_string(),
|
||||
"{}".to_string(),
|
||||
"--userType".to_string(),
|
||||
auth.user_type,
|
||||
];
|
||||
game_args.extend(loader_game_args);
|
||||
game_args
|
||||
}
|
||||
|
||||
pub async fn launch(
|
||||
config: &InstanceConfig,
|
||||
instances_dir: &Path,
|
||||
@@ -193,15 +235,14 @@ pub async fn launch(
|
||||
.join(format!("{}.jar", config.game_version)),
|
||||
);
|
||||
|
||||
// lwjgl3ify ships a patched launchwrapper and retrofuturabootstrap for
|
||||
// java 9+ compat. if present, prepend its patches to the classpath,
|
||||
// override the main class to use RFB's bootstrap, and add the required
|
||||
// --add-opens and system classloader flags.
|
||||
let (lwjgl3ify_jvm_args, main_class) = if matches!(config.loader, ModLoader::Forge) {
|
||||
let (jvm_args, override_main) = apply_lwjgl3ify_patches(&minecraft_dir, &mut classpath);
|
||||
(jvm_args, override_main.unwrap_or(main_class))
|
||||
// apply loader-specific patches (lwjgl3ify for old forge on java 9+)
|
||||
let (patch_jvm_args, main_class, extra_args) = if matches!(config.loader, ModLoader::Forge) {
|
||||
match patches::apply(&minecraft_dir, &lib_dir, &mut classpath).await {
|
||||
Some(p) => (p.jvm_args, p.main_class, p.extra_args),
|
||||
None => (Vec::new(), main_class, Vec::new()),
|
||||
}
|
||||
} else {
|
||||
(Vec::new(), main_class)
|
||||
(Vec::new(), main_class, Vec::new())
|
||||
};
|
||||
|
||||
let sep = if cfg!(windows) { ";" } else { ":" };
|
||||
@@ -227,7 +268,7 @@ pub async fn launch(
|
||||
format!("-Xms{}", config.memory_min.as_deref().unwrap_or("512M")),
|
||||
format!("-Xmx{}", config.memory_max.as_deref().unwrap_or("2G")),
|
||||
];
|
||||
jvm.extend(lwjgl3ify_jvm_args);
|
||||
jvm.extend(patch_jvm_args);
|
||||
jvm.extend(config.jvm_args.clone());
|
||||
|
||||
// resolve auth credentials, refreshing the microsoft token if needed.
|
||||
@@ -238,26 +279,36 @@ pub async fn launch(
|
||||
.cloned()
|
||||
{
|
||||
Some(acc) => {
|
||||
let (token, new_refresh) = match acc.account_type {
|
||||
let (token, new_refresh, new_expires) = match acc.account_type {
|
||||
crate::auth::AccountType::Microsoft => {
|
||||
match crate::auth::refresh_and_get_token(&acc).await {
|
||||
Ok(pair) => pair,
|
||||
Ok(triple) => triple,
|
||||
Err(e) => {
|
||||
return Err(LaunchError::Auth(format!("Authentication failed: {e}")));
|
||||
}
|
||||
}
|
||||
}
|
||||
crate::auth::AccountType::Offline => ("0".to_string(), None),
|
||||
crate::auth::AccountType::Offline => ("0".to_string(), None, None),
|
||||
};
|
||||
if let Some(new_rt) = new_refresh
|
||||
&& let Some(stored) = account_store
|
||||
.accounts
|
||||
.iter_mut()
|
||||
.find(|a| a.uuid == acc.uuid)
|
||||
{
|
||||
if let Some(stored) = account_store
|
||||
.accounts
|
||||
.iter_mut()
|
||||
.find(|a| a.uuid == acc.uuid)
|
||||
{
|
||||
let mut changed = false;
|
||||
if let Some(new_rt) = new_refresh {
|
||||
stored.refresh_token = Some(new_rt);
|
||||
changed = true;
|
||||
}
|
||||
if let Some(expires) = new_expires {
|
||||
stored.cached_mc_token = Some(token.clone());
|
||||
stored.cached_mc_token_expires_at = Some(expires);
|
||||
changed = true;
|
||||
}
|
||||
if changed {
|
||||
account_store.save();
|
||||
}
|
||||
}
|
||||
let user_type = match acc.account_type {
|
||||
crate::auth::AccountType::Microsoft => "msa",
|
||||
crate::auth::AccountType::Offline => "legacy",
|
||||
@@ -277,25 +328,19 @@ pub async fn launch(
|
||||
),
|
||||
};
|
||||
|
||||
let mut game_args = vec![
|
||||
"--username".to_string(),
|
||||
mc_username,
|
||||
"--version".to_string(),
|
||||
config.game_version.clone(),
|
||||
"--gameDir".to_string(),
|
||||
minecraft_dir.to_string_lossy().into_owned(),
|
||||
"--assetsDir".to_string(),
|
||||
meta_dir.join("assets").to_string_lossy().into_owned(),
|
||||
"--assetIndex".to_string(),
|
||||
meta.asset_index.id.clone(),
|
||||
"--uuid".to_string(),
|
||||
mc_uuid,
|
||||
"--accessToken".to_string(),
|
||||
mc_token,
|
||||
"--userType".to_string(),
|
||||
mc_user_type,
|
||||
];
|
||||
game_args.extend(loader_game_args);
|
||||
let game_args = build_game_args(
|
||||
config,
|
||||
&minecraft_dir,
|
||||
meta_dir,
|
||||
&meta.asset_index.id,
|
||||
GameAuth {
|
||||
username: mc_username,
|
||||
uuid: mc_uuid,
|
||||
token: mc_token,
|
||||
user_type: mc_user_type,
|
||||
},
|
||||
loader_game_args,
|
||||
);
|
||||
|
||||
let (kill_tx, kill_rx) = tokio::sync::oneshot::channel::<()>();
|
||||
crate::running::register_kill(&name, kill_tx);
|
||||
@@ -307,10 +352,24 @@ pub async fn launch(
|
||||
config.loader
|
||||
);
|
||||
|
||||
tracing::info!("[{}] Java: {}", name, java);
|
||||
tracing::info!("[{}] JVM args: {:?}", name, jvm);
|
||||
tracing::info!(
|
||||
"[{}] Classpath:\n{}",
|
||||
name,
|
||||
classpath
|
||||
.iter()
|
||||
.map(|p| p.display().to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n")
|
||||
);
|
||||
tracing::info!("[{}] Main class: {}", name, main_class);
|
||||
|
||||
let mut cmd = tokio::process::Command::new(&java);
|
||||
cmd.args(&jvm);
|
||||
cmd.arg("-cp").arg(&cp_str);
|
||||
cmd.arg(&main_class);
|
||||
cmd.args(&extra_args);
|
||||
cmd.args(&game_args);
|
||||
cmd.current_dir(&minecraft_dir);
|
||||
cmd.stdout(std::process::Stdio::piped());
|
||||
@@ -353,9 +412,10 @@ pub async fn launch(
|
||||
tracing::info!(target: "mc_instance", "[{}] {}", n, line);
|
||||
crate::instance_logs::push(&n, &line);
|
||||
if let Ok(mut f) = w.lock()
|
||||
&& let Some(f) = f.as_mut() {
|
||||
let _ = writeln!(f, "{}", line);
|
||||
}
|
||||
&& let Some(f) = f.as_mut()
|
||||
{
|
||||
let _ = writeln!(f, "{}", line);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -369,9 +429,10 @@ pub async fn launch(
|
||||
tracing::warn!(target: "mc_instance", "[{}] {}", n, line);
|
||||
crate::instance_logs::push(&n, &line);
|
||||
if let Ok(mut f) = w.lock()
|
||||
&& let Some(f) = f.as_mut() {
|
||||
let _ = writeln!(f, "[STDERR] {}", line);
|
||||
}
|
||||
&& let Some(f) = f.as_mut()
|
||||
{
|
||||
let _ = writeln!(f, "[STDERR] {}", line);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -411,99 +472,54 @@ pub async fn launch(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// lwjgl3ify (used by GTNH and other old forge modpacks) bundles a
|
||||
// forgePatches.zip with patched launchwrapper classes that fix the
|
||||
// URLClassLoader cast on java 9+. if we find it, extract the patches
|
||||
// and prepend them to the classpath so they shadow the vanilla classes.
|
||||
// also parses Add-Opens from the zip's manifest for the required
|
||||
// --add-opens flags.
|
||||
fn apply_lwjgl3ify_patches(
|
||||
minecraft_dir: &Path,
|
||||
classpath: &mut Vec<PathBuf>,
|
||||
) -> (Vec<String>, Option<String>) {
|
||||
let mods_dir = minecraft_dir.join("mods");
|
||||
let lwjgl3ify_jar = match find_lwjgl3ify_jar(&mods_dir) {
|
||||
Some(p) => p,
|
||||
None => return (Vec::new(), None),
|
||||
};
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use chrono::Utc;
|
||||
|
||||
let patches_dest = minecraft_dir.join(".forge-patches.zip");
|
||||
|
||||
if let Err(e) = extract_forge_patches(&lwjgl3ify_jar, &patches_dest) {
|
||||
tracing::warn!("Failed to extract lwjgl3ify forge patches: {e}");
|
||||
return (Vec::new(), None);
|
||||
}
|
||||
|
||||
// prepend so patched classes shadow vanilla launchwrapper
|
||||
classpath.insert(0, patches_dest.clone());
|
||||
|
||||
let mut jvm_args = parse_add_opens(&patches_dest).unwrap_or_default();
|
||||
|
||||
// RFB requires its own classloader to be the system classloader, and
|
||||
// its Main class handles bootstrapping into launchwrapper.
|
||||
// the other flags match lwjgl3ify's java9args.txt.
|
||||
jvm_args.extend([
|
||||
"-Djava.system.class.loader=com.gtnewhorizons.retrofuturabootstrap.RfbSystemClassLoader".to_string(),
|
||||
"-Dfile.encoding=UTF-8".to_string(),
|
||||
"--enable-native-access".to_string(),
|
||||
"ALL-UNNAMED".to_string(),
|
||||
]);
|
||||
|
||||
// also prepend the lwjgl3ify jar itself so RFB can find its plugin classes
|
||||
classpath.insert(1, lwjgl3ify_jar);
|
||||
|
||||
let main_class = "com.gtnewhorizons.retrofuturabootstrap.Main".to_string();
|
||||
|
||||
(jvm_args, Some(main_class))
|
||||
}
|
||||
|
||||
fn find_lwjgl3ify_jar(mods_dir: &Path) -> Option<PathBuf> {
|
||||
let entries = std::fs::read_dir(mods_dir).ok()?;
|
||||
for entry in entries.flatten() {
|
||||
let name = entry.file_name();
|
||||
let name = name.to_string_lossy();
|
||||
if name.starts_with("lwjgl3ify") && name.ends_with(".jar") {
|
||||
return Some(entry.path());
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn extract_forge_patches(lwjgl3ify_jar: &Path, dest: &Path) -> Result<(), std::io::Error> {
|
||||
use std::io::Read;
|
||||
|
||||
let file = std::fs::File::open(lwjgl3ify_jar)?;
|
||||
let mut archive = zip::ZipArchive::new(file)
|
||||
.map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?;
|
||||
let mut entry = archive
|
||||
.by_name("me/eigenraven/lwjgl3ify/relauncher/forgePatches.zip")
|
||||
.map_err(|e| std::io::Error::new(std::io::ErrorKind::NotFound, e))?;
|
||||
let mut buf = Vec::new();
|
||||
entry.read_to_end(&mut buf)?;
|
||||
std::fs::write(dest, &buf)
|
||||
}
|
||||
|
||||
fn parse_add_opens(patches_zip: &Path) -> Option<Vec<String>> {
|
||||
use std::io::Read;
|
||||
|
||||
let file = std::fs::File::open(patches_zip).ok()?;
|
||||
let mut archive = zip::ZipArchive::new(file).ok()?;
|
||||
let mut entry = archive.by_name("META-INF/MANIFEST.MF").ok()?;
|
||||
let mut manifest = String::new();
|
||||
entry.read_to_string(&mut manifest).ok()?;
|
||||
|
||||
// manifest continuation lines start with a single space
|
||||
let manifest = manifest.replace("\r\n ", "").replace("\n ", "");
|
||||
|
||||
let mut args = Vec::new();
|
||||
for line in manifest.lines() {
|
||||
if let Some(value) = line.strip_prefix("Add-Opens: ") {
|
||||
for module_package in value.split_whitespace() {
|
||||
args.push("--add-opens".to_string());
|
||||
args.push(format!("{module_package}=ALL-UNNAMED"));
|
||||
}
|
||||
fn test_config() -> InstanceConfig {
|
||||
InstanceConfig {
|
||||
name: "test".to_owned(),
|
||||
game_version: "1.7.10".to_owned(),
|
||||
loader: ModLoader::Forge,
|
||||
loader_version: Some("10.13.4.1614".to_owned()),
|
||||
created: Utc::now(),
|
||||
last_played: None,
|
||||
java_path: None,
|
||||
memory_max: None,
|
||||
memory_min: None,
|
||||
jvm_args: Vec::new(),
|
||||
resolution: None,
|
||||
}
|
||||
}
|
||||
|
||||
Some(args)
|
||||
#[test]
|
||||
fn game_args_include_empty_user_properties() {
|
||||
let args = build_game_args(
|
||||
&test_config(),
|
||||
Path::new("/instances/test/.minecraft"),
|
||||
Path::new("/meta"),
|
||||
"legacy",
|
||||
GameAuth {
|
||||
username: "TestPlayer".to_owned(),
|
||||
uuid: "00000000-0000-0000-0000-000000000000".to_owned(),
|
||||
token: "token".to_owned(),
|
||||
user_type: "msa".to_owned(),
|
||||
},
|
||||
vec![
|
||||
"--tweakClass".to_owned(),
|
||||
"cpw.mods.fml.common.launcher.FMLTweaker".to_owned(),
|
||||
],
|
||||
);
|
||||
|
||||
let position = args
|
||||
.iter()
|
||||
.position(|arg| arg == "--userProperties")
|
||||
.expect("game args should include --userProperties");
|
||||
assert_eq!(args.get(position + 1).map(String::as_str), Some("{}"));
|
||||
assert!(
|
||||
args.windows(2)
|
||||
.any(|pair| pair == ["--tweakClass", "cpw.mods.fml.common.launcher.FMLTweaker"])
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,324 @@
|
||||
// classpath and jvm patches for legacy forge on java 9+.
|
||||
// lwjgl3ify ships a patched launchwrapper and retrofuturabootstrap that
|
||||
// replace the old URLClassLoader-based launcher with one that works on
|
||||
// modern java. this module handles extracting those patches, building
|
||||
// the right --add-opens flags, and swapping out the broken log4j jars.
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
const LOG4J_FIXED_BASE: &str = "https://files.prismlauncher.org/maven/org/apache/logging/log4j";
|
||||
|
||||
pub struct LwjglifyPatches {
|
||||
pub jvm_args: Vec<String>,
|
||||
pub main_class: String,
|
||||
// extra args inserted before game args (used by the shim to pass
|
||||
// the real main class name)
|
||||
pub extra_args: Vec<String>,
|
||||
}
|
||||
|
||||
// checks if lwjgl3ify is present in the mods folder, and if so:
|
||||
// 1. extracts forge patches from the lwjgl3ify jar
|
||||
// 2. prepends them to the classpath so they shadow vanilla launchwrapper
|
||||
// 3. parses --add-opens from the patches manifest
|
||||
// 4. replaces log4j 2.0-beta9 with prism's patched builds
|
||||
// 5. returns the jvm args and overridden main class
|
||||
pub async fn apply(
|
||||
minecraft_dir: &Path,
|
||||
lib_dir: &Path,
|
||||
classpath: &mut Vec<PathBuf>,
|
||||
) -> Option<LwjglifyPatches> {
|
||||
let mods_dir = minecraft_dir.join("mods");
|
||||
let lwjgl3ify_jar = find_lwjgl3ify_jar(&mods_dir)?;
|
||||
|
||||
// rfb only scans jars for plugin metadata. keeping the extracted
|
||||
// forgePatches payload as a zip makes it skip rfb-asm-safety and
|
||||
// rfb-modern-java, which GTNH needs on modern Java.
|
||||
let patches_dest = minecraft_dir.join(".forge-patches.jar");
|
||||
|
||||
if let Err(e) = extract_forge_patches(&lwjgl3ify_jar, &patches_dest) {
|
||||
tracing::warn!("Failed to extract lwjgl3ify forge patches: {e}");
|
||||
return None;
|
||||
}
|
||||
|
||||
// prepend so patched classes shadow vanilla launchwrapper
|
||||
classpath.insert(0, patches_dest.clone());
|
||||
|
||||
let mut jvm_args = parse_add_opens(&patches_dest).unwrap_or_default();
|
||||
|
||||
// RFB requires its own classloader to be the system classloader, and
|
||||
// its Main class handles bootstrapping into launchwrapper.
|
||||
// the other flags match lwjgl3ify's java9args.txt.
|
||||
jvm_args.extend([
|
||||
"-Djava.system.class.loader=com.gtnewhorizons.retrofuturabootstrap.RfbSystemClassLoader"
|
||||
.to_string(),
|
||||
"-Dfile.encoding=UTF-8".to_string(),
|
||||
]);
|
||||
|
||||
// forge patches replace launchwrapper, asm, and old lwjgl2.
|
||||
// lwjgl3ify redirects lwjgl2 calls to lwjgl3 at runtime, so lwjgl3
|
||||
// must be on the classpath (matching what prism does).
|
||||
strip_replaced_libs(classpath);
|
||||
add_lwjgl3(lib_dir, classpath);
|
||||
|
||||
replace_log4j_fixed(lib_dir, classpath).await;
|
||||
|
||||
// on java 24+, SecurityManager.getClassContext() was reimplemented to use
|
||||
// StackWalker. log4j 2.0-beta9's ThrowableProxy calls getClassContext() to
|
||||
// resolve stack frames, but StackWalker triggers class loading through
|
||||
// LaunchClassLoader, which debug-logs failures, which creates another
|
||||
// ThrowableProxy... infinite recursion. we break the loop by providing a
|
||||
// log4j config that sets the root level to INFO, so the debug() call in
|
||||
// LaunchClassLoader is a no-op and never creates a ThrowableProxy.
|
||||
write_log4j_config(minecraft_dir, &mut jvm_args);
|
||||
|
||||
// RfbSystemClassLoader discovers plugins differently depending on whether
|
||||
// the main class is loaded by the JVM directly or through the system
|
||||
// classloader's loadClass(). direct invocation misses the rfb-asm-safety
|
||||
// and rfb-modern-java plugins from forgePatches, causing
|
||||
// ClassCircularityErrors. we use a tiny shim jar that loads the real main
|
||||
// class through ClassLoader.getSystemClassLoader().loadClass(), matching
|
||||
// how prism's EntryPoint does it.
|
||||
let shim_path = deploy_shim(minecraft_dir);
|
||||
classpath.insert(0, shim_path);
|
||||
|
||||
Some(LwjglifyPatches {
|
||||
jvm_args,
|
||||
main_class: "MclShim".to_string(),
|
||||
extra_args: vec!["com.gtnewhorizons.retrofuturabootstrap.Main".to_string()],
|
||||
})
|
||||
}
|
||||
|
||||
const SHIM_JAR: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/mcl-shim.jar"));
|
||||
|
||||
fn deploy_shim(minecraft_dir: &Path) -> PathBuf {
|
||||
let dest = minecraft_dir.join(".mcl-shim.jar");
|
||||
if let Err(e) = std::fs::write(&dest, SHIM_JAR) {
|
||||
tracing::warn!("Failed to write mcl-shim.jar: {e}");
|
||||
}
|
||||
dest
|
||||
}
|
||||
|
||||
fn find_lwjgl3ify_jar(mods_dir: &Path) -> Option<PathBuf> {
|
||||
let entries = std::fs::read_dir(mods_dir).ok()?;
|
||||
for entry in entries.flatten() {
|
||||
let name = entry.file_name();
|
||||
let name = name.to_string_lossy();
|
||||
if name.starts_with("lwjgl3ify") && name.ends_with(".jar") {
|
||||
return Some(entry.path());
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn extract_forge_patches(lwjgl3ify_jar: &Path, dest: &Path) -> Result<(), std::io::Error> {
|
||||
use std::io::Read;
|
||||
|
||||
let file = std::fs::File::open(lwjgl3ify_jar)?;
|
||||
let mut archive = zip::ZipArchive::new(file)
|
||||
.map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?;
|
||||
let mut entry = archive
|
||||
.by_name("me/eigenraven/lwjgl3ify/relauncher/forgePatches.zip")
|
||||
.map_err(|e| std::io::Error::new(std::io::ErrorKind::NotFound, e))?;
|
||||
let mut buf = Vec::new();
|
||||
entry.read_to_end(&mut buf)?;
|
||||
std::fs::write(dest, &buf)
|
||||
}
|
||||
|
||||
fn parse_add_opens(patches_archive: &Path) -> Option<Vec<String>> {
|
||||
use std::io::Read;
|
||||
|
||||
let file = std::fs::File::open(patches_archive).ok()?;
|
||||
let mut archive = zip::ZipArchive::new(file).ok()?;
|
||||
let mut entry = archive.by_name("META-INF/MANIFEST.MF").ok()?;
|
||||
let mut manifest = String::new();
|
||||
entry.read_to_string(&mut manifest).ok()?;
|
||||
|
||||
// manifest continuation lines start with a single space
|
||||
let manifest = manifest.replace("\r\n ", "").replace("\n ", "");
|
||||
|
||||
let mut args = Vec::new();
|
||||
for line in manifest.lines() {
|
||||
if let Some(value) = line.strip_prefix("Add-Opens: ") {
|
||||
for module_package in value.split_whitespace() {
|
||||
args.push("--add-opens".to_string());
|
||||
args.push(format!("{module_package}=ALL-UNNAMED"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Some(args)
|
||||
}
|
||||
|
||||
// strips libraries from the classpath that are replaced by forge patches
|
||||
// or not needed with lwjgl3ify. matches what prism's component system does:
|
||||
// no old lwjgl2, no vanilla launchwrapper/asm, no extra vanilla-only libs.
|
||||
fn strip_replaced_libs(classpath: &mut Vec<PathBuf>) {
|
||||
let replaced = [
|
||||
"launchwrapper-",
|
||||
"asm-all-",
|
||||
"lwjgl-2.",
|
||||
"lwjgl_util-",
|
||||
"commons-compress-",
|
||||
"commons-io-",
|
||||
"guava-15.",
|
||||
];
|
||||
|
||||
classpath.retain(|entry| {
|
||||
let name = entry
|
||||
.file_name()
|
||||
.map(|n| n.to_string_lossy())
|
||||
.unwrap_or_default();
|
||||
let dominated = replaced.iter().any(|prefix| name.starts_with(prefix));
|
||||
if dominated {
|
||||
tracing::info!("Stripping {name} from classpath (replaced by forge-patches)");
|
||||
}
|
||||
!dominated
|
||||
});
|
||||
}
|
||||
|
||||
// adds lwjgl 3.3.3 to the classpath. lwjgl3ify redirects old lwjgl2 calls
|
||||
// to lwjgl3 at runtime, but the lwjgl3 jars need to be on the system
|
||||
// classpath for this to work. prism includes these via its org.lwjgl3
|
||||
// component; we add them from the meta library cache.
|
||||
fn add_lwjgl3(lib_dir: &Path, classpath: &mut Vec<PathBuf>) {
|
||||
let lwjgl3_modules = [
|
||||
"lwjgl",
|
||||
"lwjgl-freetype",
|
||||
"lwjgl-glfw",
|
||||
"lwjgl-jemalloc",
|
||||
"lwjgl-openal",
|
||||
"lwjgl-opengl",
|
||||
"lwjgl-stb",
|
||||
"lwjgl-tinyfd",
|
||||
];
|
||||
|
||||
let os_classifier = match std::env::consts::OS {
|
||||
"macos" => "natives-macos",
|
||||
"windows" => "natives-windows",
|
||||
_ => "natives-linux",
|
||||
};
|
||||
|
||||
// insert lwjgl3 jars right after forge patches (position 1+)
|
||||
let mut insert_pos = 1.min(classpath.len());
|
||||
for module in &lwjgl3_modules {
|
||||
let base = if *module == "lwjgl" {
|
||||
lib_dir.join("org/lwjgl/lwjgl/3.3.3/lwjgl-3.3.3.jar")
|
||||
} else {
|
||||
lib_dir.join(format!("org/lwjgl/{0}/3.3.3/{0}-3.3.3.jar", module))
|
||||
};
|
||||
if base.exists() {
|
||||
classpath.insert(insert_pos, base);
|
||||
insert_pos += 1;
|
||||
}
|
||||
|
||||
// natives
|
||||
let natives = if *module == "lwjgl" {
|
||||
lib_dir.join(format!(
|
||||
"org/lwjgl/lwjgl/3.3.3/lwjgl-3.3.3-{os_classifier}.jar"
|
||||
))
|
||||
} else {
|
||||
lib_dir.join(format!(
|
||||
"org/lwjgl/{0}/3.3.3/{0}-3.3.3-{1}.jar",
|
||||
module, os_classifier
|
||||
))
|
||||
};
|
||||
if natives.exists() {
|
||||
classpath.insert(insert_pos, natives);
|
||||
insert_pos += 1;
|
||||
}
|
||||
}
|
||||
|
||||
tracing::info!("Added {} LWJGL 3.3.3 jars to classpath", insert_pos - 1);
|
||||
}
|
||||
|
||||
// writes a log4j2.xml config that sets the root logger to INFO. this
|
||||
// prevents LaunchClassLoader's debug() call from ever firing, which
|
||||
// avoids the ThrowableProxy -> SecurityManager -> StackWalker infinite
|
||||
// recursion on java 24+. the config is written to .minecraft/ and
|
||||
// pointed to via -Dlog4j.configurationFile.
|
||||
fn write_log4j_config(minecraft_dir: &Path, jvm_args: &mut Vec<String>) {
|
||||
let config_path = minecraft_dir.join(".mcl-log4j2.xml");
|
||||
let config = r#"<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Configuration status="WARN">
|
||||
<Appenders>
|
||||
<Console name="SysOut" target="SYSTEM_OUT">
|
||||
<PatternLayout pattern="[%d{HH:mm:ss}] [%t/%level] [%logger]: %msg%n"/>
|
||||
</Console>
|
||||
<Queue name="ServerGuiConsole">
|
||||
<PatternLayout pattern="[%d{HH:mm:ss} %level]: %msg%n"/>
|
||||
</Queue>
|
||||
<RollingRandomAccessFile name="File" fileName="logs/latest.log"
|
||||
filePattern="logs/%d{yyyy-MM-dd}-%i.log.gz">
|
||||
<PatternLayout pattern="[%d{HH:mm:ss}] [%t/%level]: %msg%n"/>
|
||||
<Policies>
|
||||
<TimeBasedTriggeringPolicy/>
|
||||
<OnStartupTriggeringPolicy/>
|
||||
</Policies>
|
||||
</RollingRandomAccessFile>
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Root level="info">
|
||||
<AppenderRef ref="SysOut"/>
|
||||
<AppenderRef ref="File"/>
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
||||
"#;
|
||||
|
||||
if let Err(e) = std::fs::write(&config_path, config) {
|
||||
tracing::warn!("Failed to write log4j2 config: {e}");
|
||||
return;
|
||||
}
|
||||
|
||||
jvm_args.push(format!(
|
||||
"-Dlog4j.configurationFile={}",
|
||||
config_path.display()
|
||||
));
|
||||
}
|
||||
|
||||
// replaces log4j-api and log4j-core 2.0-beta9 in the classpath with
|
||||
// prism's patched "-fixed" builds. the vanilla 2.0-beta9 has a bug
|
||||
// where ThrowableProxy calls SecurityManager.getClassContext() which
|
||||
// on java 24+ uses StackWalker internally, triggering class loading
|
||||
// through LaunchClassLoader, which triggers more logging, infinite
|
||||
// recursion, stack overflow. the fixed builds patch this out.
|
||||
async fn replace_log4j_fixed(lib_dir: &Path, classpath: &mut [PathBuf]) {
|
||||
let replacements = [
|
||||
(
|
||||
"log4j-api-2.0-beta9.jar",
|
||||
"org/apache/logging/log4j/log4j-api/2.0-beta9-fixed/log4j-api-2.0-beta9-fixed.jar",
|
||||
format!("{LOG4J_FIXED_BASE}/log4j-api/2.0-beta9-fixed/log4j-api-2.0-beta9-fixed.jar"),
|
||||
),
|
||||
(
|
||||
"log4j-core-2.0-beta9.jar",
|
||||
"org/apache/logging/log4j/log4j-core/2.0-beta9-fixed/log4j-core-2.0-beta9-fixed.jar",
|
||||
format!("{LOG4J_FIXED_BASE}/log4j-core/2.0-beta9-fixed/log4j-core-2.0-beta9-fixed.jar"),
|
||||
),
|
||||
];
|
||||
|
||||
for (old_name, fixed_rel, url) in &replacements {
|
||||
let fixed_path = lib_dir.join(fixed_rel);
|
||||
|
||||
if !fixed_path.exists() {
|
||||
tracing::info!("Downloading patched {old_name}...");
|
||||
if let Some(parent) = fixed_path.parent() {
|
||||
let _ = tokio::fs::create_dir_all(parent).await;
|
||||
}
|
||||
let client = crate::net::HttpClient::new();
|
||||
if let Err(e) = crate::net::download_file(&client, url, &fixed_path, |_, _| {}).await {
|
||||
tracing::error!("Failed to download patched {old_name}: {e}, continuing with unpatched version");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
for entry in classpath.iter_mut() {
|
||||
if entry
|
||||
.file_name()
|
||||
.is_some_and(|n| n.to_string_lossy() == *old_name)
|
||||
{
|
||||
tracing::info!("Replacing {old_name} with patched version");
|
||||
*entry = fixed_path.clone();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user