Sync local working tree: injector, engine, mixin and build updates
Add compatibility notes, resource-provider and hook checks, and payload bundling script. Remove generated engine mixin stubs, engine target lists and prebuilt binaries now produced by the build. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
6d94493e58
commit
05d169a2dd
210 files changed
+1862
-849
No files matched your search
@@ -170,7 +170,7 @@ unsafe extern "system" fn collect(hwnd: Handle, param: isize) -> i32 {
|
||||
1
|
||||
}
|
||||
|
||||
/// Visible top-level windows that belong to java.exe / javaw.exe, one entry per process.
|
||||
/// Visible JVM windows, including clients that embed HotSpot in their own executable.
|
||||
pub fn java_processes() -> Vec<JavaProcess> {
|
||||
let mut windows: Vec<(u32, String)> = Vec::new();
|
||||
unsafe {
|
||||
@@ -185,7 +185,10 @@ pub fn java_processes() -> Vec<JavaProcess> {
|
||||
continue;
|
||||
};
|
||||
let exe = path.rsplit(['\\', '/']).next().unwrap_or("").to_string();
|
||||
if exe.eq_ignore_ascii_case("javaw.exe") || exe.eq_ignore_ascii_case("java.exe") {
|
||||
if exe.eq_ignore_ascii_case("javaw.exe")
|
||||
|| exe.eq_ignore_ascii_case("java.exe")
|
||||
|| module_loaded(pid, "jvm.dll")
|
||||
{
|
||||
out.push(JavaProcess { pid, title, exe });
|
||||
}
|
||||
}
|
||||
@@ -344,3 +347,61 @@ unsafe fn inject_into(process: Handle, pid: u32, dll: &Path) -> Result<(), Strin
|
||||
}
|
||||
Err(format!("LoadLibraryW returned {exit:#x} but {file_name} is not mapped (missing dependency or blocked by security software?)"))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
extern "system" {
|
||||
fn LoadLibraryExW(path: *const u16, file: Handle, flags: u32) -> Handle;
|
||||
fn FreeLibrary(module: Handle) -> i32;
|
||||
}
|
||||
|
||||
#[link(name = "user32")]
|
||||
extern "system" {
|
||||
fn CreateWindowExW(ex: u32, class: *const u16, title: *const u16, style: u32,
|
||||
x: i32, y: i32, width: i32, height: i32, parent: Handle, menu: Handle,
|
||||
instance: Handle, param: *mut c_void) -> Handle;
|
||||
fn DestroyWindow(window: Handle) -> i32;
|
||||
}
|
||||
|
||||
struct Fixture { window: Handle, library: Handle }
|
||||
impl Drop for Fixture {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
if !self.window.is_null() { DestroyWindow(self.window); }
|
||||
if !self.library.is_null() { FreeLibrary(self.library); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn discovers_a_custom_executable_only_when_it_hosts_a_jvm() {
|
||||
let pid = std::process::id();
|
||||
let image = image_name(pid).expect("read test executable path");
|
||||
assert!(!image.ends_with("\\java.exe") && !image.ends_with("\\javaw.exe"));
|
||||
let home = std::env::var_os("JAVA_HOME").expect("set JAVA_HOME to a 64-bit JDK for this test");
|
||||
let jvm = Path::new(&home).join("bin/server/jvm.dll");
|
||||
assert!(jvm.is_file(), "missing {}", jvm.display());
|
||||
unsafe {
|
||||
let mut fixture = Fixture { window: std::ptr::null_mut(), library: std::ptr::null_mut() };
|
||||
// The picker requires a visible window; place this 1px fixture off-screen.
|
||||
fixture.window = CreateWindowExW(0, wide(Path::new("STATIC")).as_ptr(),
|
||||
wide(Path::new("OpenPrestige JVM discovery test")).as_ptr(), 0x90000000,
|
||||
-32000, -32000, 1, 1, std::ptr::null_mut(), std::ptr::null_mut(),
|
||||
GetModuleHandleW(std::ptr::null()), std::ptr::null_mut());
|
||||
assert!(!fixture.window.is_null(), "{}", last_error("CreateWindowExW"));
|
||||
assert!(!module_loaded(pid, "jvm.dll"));
|
||||
assert!(!java_processes().iter().any(|process| process.pid == pid));
|
||||
|
||||
fixture.library = LoadLibraryExW(wide(&jvm).as_ptr(), std::ptr::null_mut(), 0x00001100);
|
||||
assert!(!fixture.library.is_null(), "{}", last_error("LoadLibraryExW(jvm.dll)"));
|
||||
assert!(module_loaded(pid, "JVM.DLL"));
|
||||
assert!(!module_loaded(pid, "openprestige-missing-test-module.dll"));
|
||||
let processes = java_processes();
|
||||
let found = processes.iter().find(|process| process.pid == pid)
|
||||
.expect("custom executable hosting HotSpot was not listed");
|
||||
assert_eq!(found.title, "OpenPrestige JVM discovery test");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,46 +1,33 @@
|
||||
//! Locates the remapped Prestige payload jar (built by `gradlew remapJar`) so lib.rs can embed it with include_bytes!.
|
||||
use std::env;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
//! Embed exact-version payloads and one copy of their common libraries.
|
||||
use std::{env, fs, path::{Path, PathBuf}};
|
||||
|
||||
fn include(path: &Path) -> String {
|
||||
let path = path.canonicalize().unwrap_or_else(|e| panic!("{}: {e}", path.display()));
|
||||
println!("cargo:rerun-if-changed={}", path.display());
|
||||
format!("include_bytes!({:?})", path.to_str().expect("UTF-8 payload path"))
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("cargo:rerun-if-env-changed=PRESTIGE_PAYLOAD");
|
||||
let jar = match env::var("PRESTIGE_PAYLOAD") {
|
||||
Ok(p) => PathBuf::from(p),
|
||||
Err(_) => newest_remapped_jar(),
|
||||
};
|
||||
if !jar.is_file() {
|
||||
panic!(
|
||||
"payload jar not found: {} (run `gradlew remapJar` or set PRESTIGE_PAYLOAD)",
|
||||
jar.display()
|
||||
);
|
||||
}
|
||||
println!("cargo:rerun-if-changed={}", jar.display());
|
||||
println!("cargo:rustc-env=PRESTIGE_PAYLOAD_JAR={}", jar.display());
|
||||
}
|
||||
|
||||
fn newest_remapped_jar() -> PathBuf {
|
||||
let libs = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()).join("../../../build/libs");
|
||||
let mut best: Option<(std::time::SystemTime, PathBuf)> = None;
|
||||
if let Ok(entries) = fs::read_dir(&libs) {
|
||||
for entry in entries.flatten() {
|
||||
let name = entry.file_name().to_string_lossy().to_string();
|
||||
if !name.starts_with("prestige-")
|
||||
|| !name.ends_with(".jar")
|
||||
|| name.ends_with("-dev.jar")
|
||||
|| name.ends_with("-sources.jar")
|
||||
{
|
||||
continue;
|
||||
}
|
||||
let time = entry
|
||||
.metadata()
|
||||
.and_then(|m| m.modified())
|
||||
.unwrap_or(std::time::UNIX_EPOCH);
|
||||
if best.as_ref().map_or(true, |(t, _)| time > *t) {
|
||||
best = Some((time, entry.path()));
|
||||
}
|
||||
println!("cargo:rerun-if-env-changed=PRESTIGE_BUNDLE");
|
||||
let generated = if let Ok(jar) = env::var("PRESTIGE_PAYLOAD") {
|
||||
// Explicit single-target developer builds retain Java's exact-target guard.
|
||||
format!("static COMMON: &[u8] = &[];\nstatic PAYLOADS: &[(&str, &[u8])] = &[(\"\", {})];", include(Path::new(&jar)))
|
||||
} else {
|
||||
let dir = env::var_os("PRESTIGE_BUNDLE").map(PathBuf::from).unwrap_or_else(||
|
||||
PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap()).join("../../../build/bundle"));
|
||||
let manifest = dir.join("versions.txt");
|
||||
println!("cargo:rerun-if-changed={}", manifest.display());
|
||||
let versions = fs::read_to_string(&manifest)
|
||||
.expect("run gradlew bundlePayloads before building the native bundle");
|
||||
let expected: Vec<_> = (1..=11).map(|n| format!("1.21.{n}")).collect();
|
||||
assert_eq!(versions.lines().collect::<Vec<_>>(), expected, "incomplete payload matrix");
|
||||
let mut code = format!("static COMMON: &[u8] = {};\nstatic PAYLOADS: &[(&str, &[u8])] = &[\n", include(&dir.join("common.jar")));
|
||||
for version in versions.lines() {
|
||||
code.push_str(&format!("({version:?}, {}),\n", include(&dir.join(format!("{version}.jar")))));
|
||||
}
|
||||
}
|
||||
best.map(|(_, p)| p)
|
||||
.unwrap_or_else(|| libs.join("prestige-0.1.0.jar"))
|
||||
code.push_str("];\n");
|
||||
code
|
||||
};
|
||||
fs::write(PathBuf::from(env::var_os("OUT_DIR").unwrap()).join("payloads.rs"), generated).unwrap();
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
//! prestige_native.dll: loaded into a running Minecraft 1.21.11 (Fabric) JVM by PrestigeInjector.exe.
|
||||
//! prestige_native.dll: loads the exact Minecraft 1.21.1–1.21.11 Fabric payload for the running JVM.
|
||||
//!
|
||||
//! The DLL worker waits for the JVM, appends the embedded Prestige payload jar to Fabric's Knot class loader, registers the
|
||||
//! JVMTI-backed natives of `dev.zprestige.prestige.inject.NativeBridge` and starts `Bootstrap`, which transforms the already
|
||||
|
||||
@@ -15,7 +15,7 @@ extern "system" {
|
||||
fn GetProcAddress(module: *mut c_void, name: *const c_char) -> *mut c_void;
|
||||
}
|
||||
|
||||
static PAYLOAD: &[u8] = include_bytes!(env!("PRESTIGE_PAYLOAD_JAR"));
|
||||
include!(concat!(env!("OUT_DIR"), "/payloads.rs"));
|
||||
|
||||
const NATIVE_BRIDGE: &str = "dev.zprestige.prestige.inject.NativeBridge";
|
||||
const BOOTSTRAP: &str = "dev.zprestige.prestige.inject.Bootstrap";
|
||||
@@ -45,7 +45,17 @@ unsafe fn install() -> Result<(), String> {
|
||||
init_jvmti(vm)?;
|
||||
log!("JVMTI ready");
|
||||
|
||||
let jar = write_payload()?;
|
||||
let (_, fabric_loader) = locate_fabric(&jni)?;
|
||||
let version = minecraft_version(&jni, fabric_loader)?;
|
||||
let payload = select_payload(&version, PAYLOADS)
|
||||
.ok_or_else(|| format!("unsupported Minecraft {version}; bundled targets: {}",
|
||||
PAYLOADS.iter().map(|(target, _)| *target).collect::<Vec<_>>().join(", ")))?;
|
||||
log!("selected Minecraft {version} payload");
|
||||
if !COMMON.is_empty() {
|
||||
let common = write_payload("common", COMMON)?;
|
||||
add_to_knot(&jni, &common)?;
|
||||
}
|
||||
let jar = write_payload(&version, payload.1)?;
|
||||
log!("using payload at {}", jar.display());
|
||||
|
||||
let loader = add_to_knot(&jni, &jar)?;
|
||||
@@ -103,15 +113,53 @@ unsafe fn find_vm() -> Result<VmPtr, String> {
|
||||
Err("no JavaVM was created".into())
|
||||
}
|
||||
|
||||
fn write_payload() -> Result<PathBuf, String> {
|
||||
fn write_payload(target: &str, bytes: &[u8]) -> Result<PathBuf, String> {
|
||||
let dir = crate::paths::initialize().map_err(|e| format!("create OpenPrestige data directory: {e}"))?
|
||||
.join("runtime").join("payloads");
|
||||
std::fs::create_dir_all(&dir).map_err(|e| format!("create {}: {e}", dir.display()))?;
|
||||
let path = dir.join(format!("prestige-payload-{}.jar", std::process::id()));
|
||||
std::fs::write(&path, PAYLOAD).map_err(|e| format!("write {}: {e}", path.display()))?;
|
||||
if !target.bytes().all(|c| c.is_ascii_alphanumeric() || c == b'.' || c == b'-') {
|
||||
return Err(format!("invalid Minecraft target: {target}"));
|
||||
}
|
||||
let path = dir.join(format!("prestige-{target}-{}.jar", std::process::id()));
|
||||
std::fs::write(&path, bytes).map_err(|e| format!("write {}: {e}", path.display()))?;
|
||||
Ok(path)
|
||||
}
|
||||
|
||||
/// Read stable Fabric metadata before adding a payload to the game loader.
|
||||
unsafe fn minecraft_version(jni: &Jni, loader: JObject) -> Result<String, String> {
|
||||
let api = load_fabric_class(jni, "net.fabricmc.loader.api.FabricLoader", loader)?;
|
||||
let instance = jni.get_static_method_id(api, "getInstance", "()Lnet/fabricmc/loader/api/FabricLoader;");
|
||||
jni.check("FabricLoader.getInstance lookup")?;
|
||||
let fabric = jni.call_static_object(api, instance, &[]);
|
||||
jni.check("FabricLoader.getInstance")?;
|
||||
let get_mod = jni.get_method_id(api, "getModContainer", "(Ljava/lang/String;)Ljava/util/Optional;");
|
||||
jni.check("FabricLoader.getModContainer lookup")?;
|
||||
let minecraft = jni.new_string("minecraft");
|
||||
let optional = jni.call_object(jni_non_null(fabric)?, get_mod, &[obj(minecraft)]);
|
||||
jni.check("Minecraft mod container")?;
|
||||
let optional_class = jni.find_class("java/util/Optional");
|
||||
let get = jni.get_method_id(optional_class, "get", "()Ljava/lang/Object;");
|
||||
jni.check("Optional.get lookup")?;
|
||||
let container = jni.call_object(jni_non_null(optional)?, get, &[]);
|
||||
jni.check("Minecraft mod present")?;
|
||||
let container_class = load_fabric_class(jni, "net.fabricmc.loader.api.ModContainer", loader)?;
|
||||
let metadata = jni.get_method_id(container_class, "getMetadata", "()Lnet/fabricmc/loader/api/metadata/ModMetadata;");
|
||||
jni.check("ModContainer.getMetadata lookup")?;
|
||||
let metadata = jni.call_object(jni_non_null(container)?, metadata, &[]);
|
||||
jni.check("Minecraft metadata")?;
|
||||
let metadata_class = load_fabric_class(jni, "net.fabricmc.loader.api.metadata.ModMetadata", loader)?;
|
||||
let version = jni.get_method_id(metadata_class, "getVersion", "()Lnet/fabricmc/loader/api/Version;");
|
||||
jni.check("ModMetadata.getVersion lookup")?;
|
||||
let version = jni.call_object(jni_non_null(metadata)?, version, &[]);
|
||||
jni.check("Minecraft version")?;
|
||||
let version_class = load_fabric_class(jni, "net.fabricmc.loader.api.Version", loader)?;
|
||||
let friendly = jni.get_method_id(version_class, "getFriendlyString", "()Ljava/lang/String;");
|
||||
jni.check("Version.getFriendlyString lookup")?;
|
||||
let friendly = jni.call_object(jni_non_null(version)?, friendly, &[]);
|
||||
jni.check("Minecraft version string")?;
|
||||
Ok(jni.get_string_utf(jni_non_null(friendly)?))
|
||||
}
|
||||
|
||||
/// FabricLauncherBase.getLauncher().addToClassPath(path) so the payload and the game classes share the Knot class loader.
|
||||
/// Returns a local reference to the Fabric launcher's target class loader.
|
||||
unsafe fn add_to_knot(jni: &Jni, jar: &std::path::Path) -> Result<JObject, String> {
|
||||
@@ -156,6 +204,28 @@ unsafe fn add_to_knot(jni: &Jni, jar: &std::path::Path) -> Result<JObject, Strin
|
||||
|
||||
const FABRIC_BASE: &str = "net.fabricmc.loader.impl.launch.FabricLauncherBase";
|
||||
|
||||
fn select_payload<'a>(version: &str, payloads: &'a [(&str, &[u8])]) -> Option<&'a (&'a str, &'a [u8])> {
|
||||
payloads.iter().find(|(target, _)| target.is_empty() || *target == version)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod payload_tests {
|
||||
use super::select_payload;
|
||||
|
||||
#[test]
|
||||
fn only_selects_exact_target() {
|
||||
let versions: Vec<_> = (1..=11).map(|n| format!("1.21.{n}")).collect();
|
||||
let payloads: Vec<_> = versions.iter().enumerate()
|
||||
.map(|(i, version)| (version.as_str(), &b"123456789ab"[i..i+1])).collect();
|
||||
for (index, version) in versions.iter().enumerate() {
|
||||
assert_eq!(select_payload(version, &payloads).unwrap().1, &b"123456789ab"[index..index+1]);
|
||||
}
|
||||
for unsupported in ["1.21", "1.21.0", "1.21.12", "1.21.1-pre1", ""] {
|
||||
assert!(select_payload(unsupported, &payloads).is_none());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Prefer the render thread's context loader when a launcher supplies its own loader hierarchy.
|
||||
/// Fall back to the system loader and then the other thread context loaders.
|
||||
unsafe fn locate_fabric(jni: &Jni) -> Result<(JClass, JObject), String> {
|
||||
|
||||
Reference in new issue
Block a user