Skip to content

Commit

Permalink
Experimental | 修复&优化侵入式类加载器兼容
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFloodDragon committed Jul 23, 2024
1 parent ad8de95 commit 9b5fa28
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,6 @@ public final class IntrusiveClassLoader extends ClassLoader {
this.ACCESS_GROUP_NAME = groupName;
}

// START::Providers

private final NavigableMap<Byte, List<ClassProvider>> providers = Collections.synchronizedNavigableMap(new TreeMap<>());

public boolean addProvider(ClassProvider provider, byte priority) {
return providers.computeIfAbsent(priority, key -> Collections.emptyList()).add(provider);
}

public void removeProvider(ClassProvider provider) {
for (List<ClassProvider> list : providers.values()) {
list.remove(provider);
}
}
// END::Providers

@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
return this.loadClass(name, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
*/
public final class IntrusiveCompat {

private IntrusiveCompat() {
private IntrusiveCompat(ClassLoader pluginClassLoader, ClassLoader globalClassLoader) {
this.PLUGIN_CLASS_LOADER = pluginClassLoader;
this.GLOBAL_CLASS_LOADER = globalClassLoader;
}

public static ClassLoader PLUGIN_CLASS_LOADER = IsolatedClassLoader.INSTANCE.getParent();
public static ClassLoader PLUGIN_CLASS_LOADER;

public static ClassLoader GLOBAL_CLASS_LOADER = PLUGIN_CLASS_LOADER.getParent();
public static ClassLoader GLOBAL_CLASS_LOADER;

public static ClassLoader INTRUSIVE_CLASSLOADER;

Expand All @@ -35,7 +37,7 @@ private IntrusiveCompat() {
}
}

private static void inject() throws Throwable {
private void inject() throws Throwable {
// 类加载器示例
INTRUSIVE_CLASSLOADER = new IntrusiveClassLoader(GLOBAL_CLASS_LOADER, PLUGIN_GROUP_NAME);
// 隔离模式下注入类加载器
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,16 @@ public static void init(Class<?> clazz) {
Object delegateObject = delegateClass.getConstructor().newInstance();
delegateClass.getMethod("init").invoke(delegateObject);
// ::Start:: 注入
// Load Injector
Class<?> injectClass = Class.forName("cn.fd.ratziel.compat.inject.IntrusiveCompat", true, INSTANCE);
// Inject
Method injectMethod = injectClass.getDeclaredMethod("inject");
Constructor<?> injectConstructor = injectClass.getDeclaredConstructor();
Constructor<?> injectConstructor = injectClass.getDeclaredConstructor(ClassLoader.class, ClassLoader.class);
injectMethod.setAccessible(true);
injectConstructor.setAccessible(true);
injectMethod.invoke(injectConstructor.newInstance());
ClassLoader pluginClassLoader = INSTANCE.getParent();
ClassLoader globalClassLoader = pluginClassLoader.getParent();
injectMethod.invoke(injectConstructor.newInstance(pluginClassLoader, globalClassLoader));
// ::End::
} catch (Exception e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @since 2023/8/23 16:20
*/
@RuntimeDependencies({
@RuntimeDependency(value = "!org.jetbrains.kotlinx:kotlinx-serialization-core-jvm:1.7.1", test = "!kotlinx.serialization.KSerializer", transitive = false),
@RuntimeDependency(value = "!org.jetbrains.kotlinx:kotlinx-serialization-core-jvm:1.7.1", test = "!kotlinx.serialization.StringFormat", transitive = false),
@RuntimeDependency(value = "!org.jetbrains.kotlinx:kotlinx-serialization-json-jvm:1.7.1", test = "!kotlinx.serialization.json.JsonElement", transitive = false)
})
public class CoreEnv {
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ class RefItemMeta<T : ItemMeta>(raw: T) {
companion object {

/**
* inventory.CraftMetaItem
* Registry for [CraftMetaType]
*/
val META_ITEM: CraftMetaType<ItemMeta> = CraftMetaType("inventory.CraftMetaItem")
val META_SKULL: CraftMetaType<SkullMeta> = CraftMetaType("inventory.CraftMetaSkull")
val registry: MutableSet<CraftMetaType<*>> = CopyOnWriteArraySet()

/**
* Registry for [CraftMetaType]
* inventory.CraftMetaItem
*/
val registry: MutableSet<CraftMetaType<*>> = CopyOnWriteArraySet()
val META_ITEM: CraftMetaType<ItemMeta> = CraftMetaType("inventory.CraftMetaItem")
val META_SKULL: CraftMetaType<SkullMeta> = CraftMetaType("inventory.CraftMetaSkull")

/**
* CraftMetaItem#constructor(NBTTagCompound)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public boolean compile(@NotNull ScriptExecutor executor) {

@Override
public @Nullable CompiledScript getCompiled() {
if (future.isDone())
if (future != null && future.isDone())
return future.getNow(null);
else return null;
}
Expand Down

0 comments on commit 9b5fa28

Please sign in to comment.