mirror of
https://github.com/xiaozhou233/OrangeX-Client.git
synced 2026-12-05 21:15:53 +08:00
Feat: Add loader, load orangex via loader
This commit is contained in:
1 parent
22fad7c7b7
commit
07f4cc0663
9 files changed
+168
-4
No files matched your search
Generated
+1
@@ -10,6 +10,7 @@
|
||||
<set>
|
||||
<option value="$PROJECT_DIR$" />
|
||||
<option value="$PROJECT_DIR$/Injector" />
|
||||
<option value="$PROJECT_DIR$/Loader" />
|
||||
<option value="$PROJECT_DIR$/Minecraft-1.8.8" />
|
||||
<option value="$PROJECT_DIR$/OrangeX" />
|
||||
</set>
|
||||
|
||||
Generated
+1
-1
@@ -10,7 +10,7 @@
|
||||
</writeAnnotations>
|
||||
</component>
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="zulu-1.8" project-jdk-type="JavaSDK">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
@@ -0,0 +1,19 @@
|
||||
plugins {
|
||||
id 'java'
|
||||
id "com.gradleup.shadow" version "8.3.9"
|
||||
}
|
||||
|
||||
group = 'cn.xiaozhou233.orangex.loader'
|
||||
version = 'unspecified'
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compileOnly files("../libraries/JuiceAgent-API-1.4.1+build.1.jar")
|
||||
}
|
||||
|
||||
shadowJar {
|
||||
archiveFileName = 'Loader.jar'
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package cn.xiaozhou233.orangex.loader;
|
||||
|
||||
public class ClassProbe {
|
||||
public static boolean exists(ClassLoader loader, String... names) {
|
||||
if(loader == null)
|
||||
return false;
|
||||
|
||||
if (names == null)
|
||||
return false;
|
||||
|
||||
for (String name : names) {
|
||||
// Check for empty names
|
||||
if (name == null || name.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check for class existence
|
||||
try {
|
||||
Class.forName(name, false, loader);
|
||||
return true;
|
||||
} catch (Throwable ignored) {
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static Class<?> find(ClassLoader loader, String... names) {
|
||||
if(loader == null)
|
||||
return null;
|
||||
if (names == null)
|
||||
return null;
|
||||
|
||||
for (String name : names) {
|
||||
// Check for empty names
|
||||
if (name == null || name.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
return Class.forName(name, false, loader);
|
||||
} catch (Throwable ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package cn.xiaozhou233.orangex.loader;
|
||||
|
||||
import cn.xiaozhou233.juiceagent.api.JuiceAgent;
|
||||
import cn.xiaozhou233.orangex.loader.classloader.GameClassLoaderResolver;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class Loader {
|
||||
public static String userHome = System.getProperty("user.home");
|
||||
public static ClassLoader minecraftClassLoader;
|
||||
|
||||
public static void entry() {
|
||||
System.out.println("======== Loader START ========");
|
||||
// Find Minecraft ClassLoader
|
||||
minecraftClassLoader = GameClassLoaderResolver.resolve();
|
||||
System.out.println("Minecraft ClassLoader: " + minecraftClassLoader);
|
||||
|
||||
// Load OrangeX
|
||||
File injectionFile = new File(userHome + "/.orangex/OrangeX.jar");
|
||||
System.out.println("OrangeX Injection: " + injectionFile.getAbsolutePath());
|
||||
JuiceAgent.addToClassLoader(injectionFile.getAbsolutePath(), minecraftClassLoader);
|
||||
|
||||
try {
|
||||
Class<?> orangeXClass = Class.forName("cn.xiaozhou233.orangex.entry.Entry", true, minecraftClassLoader);
|
||||
orangeXClass.getMethod("entry").invoke(null);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
System.out.println("======== Loader END ========");
|
||||
}
|
||||
}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
package cn.xiaozhou233.orangex.loader.classloader;
|
||||
|
||||
import cn.xiaozhou233.orangex.loader.ClassProbe;
|
||||
|
||||
public final class GameClassLoaderResolver {
|
||||
|
||||
public static ClassLoader resolve() {
|
||||
ClassLoader loader = findGameThreadLoader();
|
||||
if (loader != null) {
|
||||
return loader;
|
||||
}
|
||||
|
||||
loader = findMinecraftLoader(Thread.currentThread().getContextClassLoader());
|
||||
if (loader != null) {
|
||||
return loader;
|
||||
}
|
||||
|
||||
return Thread.currentThread().getContextClassLoader();
|
||||
}
|
||||
|
||||
private static ClassLoader findGameThreadLoader() {
|
||||
try {
|
||||
for (Thread thread : Thread.getAllStackTraces().keySet()) {
|
||||
String name = thread.getName();
|
||||
|
||||
if (!"Client thread".equals(name) && !"Render thread".equals(name)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ClassLoader loader = thread.getContextClassLoader();
|
||||
if (loader != null) {
|
||||
return loader;
|
||||
}
|
||||
}
|
||||
} catch (Throwable ignored) {
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static ClassLoader findMinecraftLoader(ClassLoader candidate) {
|
||||
if (candidate == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String[] minecraftClasses = {
|
||||
"net.minecraft.client.Minecraft",
|
||||
"net.minecraft.class_310"
|
||||
};
|
||||
|
||||
Class<?> minecraftClass = ClassProbe.find(candidate, minecraftClasses);
|
||||
if (minecraftClass != null) {
|
||||
ClassLoader minecraftLoader = minecraftClass.getClassLoader();
|
||||
return minecraftLoader == null ? candidate : minecraftLoader;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private GameClassLoaderResolver() {
|
||||
}
|
||||
}
|
||||
+3
-1
@@ -18,10 +18,12 @@ tasks.register("buildInjection", Copy) {
|
||||
dependsOn(
|
||||
tasks.named("cleanTmp"),
|
||||
project(':OrangeX').tasks.named('shadowJar'),
|
||||
project(':Injector').tasks.named('shadowJar')
|
||||
project(':Injector').tasks.named('shadowJar'),
|
||||
project(':Loader').tasks.named('shadowJar')
|
||||
)
|
||||
|
||||
from(project(':OrangeX').tasks.named('shadowJar').flatMap { it.archiveFile })
|
||||
from(project(':Loader').tasks.named('shadowJar').flatMap { it.archiveFile })
|
||||
from(rootProject.layout.projectDirectory.dir('natives'))
|
||||
from(rootProject.layout.projectDirectory.dir('resources'))
|
||||
into(tmpDir)
|
||||
|
||||
@@ -9,6 +9,6 @@ JuiceAgentNativeLibraryPath = ""
|
||||
[JuiceAgent.Modules.JarLoader]
|
||||
Enabled = true
|
||||
InjectionDir = "./injection"
|
||||
JarPath = "./OrangeX.jar"
|
||||
EntryClass = "cn.xiaozhou233.orangex.entry.Entry"
|
||||
JarPath = "./Loader.jar"
|
||||
EntryClass = "cn.xiaozhou233.orangex.loader.Loader"
|
||||
EntryMethod = "entry"
|
||||
@@ -2,4 +2,5 @@ rootProject.name = 'OrangeX-Client'
|
||||
include 'Minecraft-1.8.8'
|
||||
include 'OrangeX'
|
||||
include 'Injector'
|
||||
include 'Loader'
|
||||
|
||||
Reference in new issue
Block a user