2022-07-30 15:21:11 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2022 FalsePattern
|
|
|
|
|
* All Rights Reserved
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice, this permission notice and the word "SNEED"
|
|
|
|
|
* shall be included in all copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
2022-04-17 17:26:30 +02:00
|
|
|
package com.falsepattern.lib.mixin;
|
|
|
|
|
|
|
|
|
|
import cpw.mods.fml.common.Loader;
|
|
|
|
|
import cpw.mods.fml.common.ModClassLoader;
|
2022-06-08 16:01:06 +02:00
|
|
|
import java.io.File;
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
import java.net.URL;
|
2022-05-07 12:05:09 +02:00
|
|
|
import lombok.AccessLevel;
|
|
|
|
|
import lombok.NoArgsConstructor;
|
2022-04-17 17:26:30 +02:00
|
|
|
import lombok.val;
|
|
|
|
|
import net.minecraft.launchwrapper.LaunchClassLoader;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Backport from spongemixins 1.3 for compat with the curseforge 1.2.0 version.
|
2022-06-08 16:01:06 +02:00
|
|
|
* <p>
|
2022-04-17 17:26:30 +02:00
|
|
|
* Also added Grimoire protection.
|
|
|
|
|
*/
|
2022-05-07 12:05:09 +02:00
|
|
|
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
2022-04-17 17:26:30 +02:00
|
|
|
public final class MinecraftURLClassPath {
|
|
|
|
|
/**
|
2022-06-08 16:01:06 +02:00
|
|
|
* Utility to manipulate the minecraft URL ClassPath
|
2022-04-17 17:26:30 +02:00
|
|
|
*/
|
|
|
|
|
|
2022-05-07 12:05:09 +02:00
|
|
|
private static final Object ucp;
|
|
|
|
|
private static final Method addURL;
|
2022-04-17 17:26:30 +02:00
|
|
|
private static final boolean GRIMOIRE;
|
|
|
|
|
|
|
|
|
|
static {
|
2022-06-27 22:56:19 +02:00
|
|
|
boolean grimoire = false;
|
|
|
|
|
String[] knownGrimoireClassNames = new String[]{
|
|
|
|
|
"io.github.crucible.grimoire.Grimoire",
|
|
|
|
|
"io.github.crucible.grimoire.common.GrimoireCore"
|
|
|
|
|
};
|
|
|
|
|
for (val className: knownGrimoireClassNames) {
|
|
|
|
|
try {
|
|
|
|
|
Class.forName(className, false, MinecraftURLClassPath.class.getClassLoader());
|
|
|
|
|
grimoire = true;
|
|
|
|
|
break;
|
|
|
|
|
} catch (ClassNotFoundException ignored) {}
|
2022-04-17 17:26:30 +02:00
|
|
|
}
|
|
|
|
|
GRIMOIRE = grimoire;
|
|
|
|
|
if (!GRIMOIRE) {
|
|
|
|
|
try {
|
|
|
|
|
val modClassLoaderField = Loader.class.getDeclaredField("modClassLoader");
|
|
|
|
|
modClassLoaderField.setAccessible(true);
|
|
|
|
|
|
|
|
|
|
val loaderinstanceField = Loader.class.getDeclaredField("instance");
|
|
|
|
|
loaderinstanceField.setAccessible(true);
|
|
|
|
|
|
|
|
|
|
val mainClassLoaderField = ModClassLoader.class.getDeclaredField("mainClassLoader");
|
|
|
|
|
mainClassLoaderField.setAccessible(true);
|
|
|
|
|
|
|
|
|
|
val ucpField = LaunchClassLoader.class.getSuperclass().getDeclaredField("ucp");
|
|
|
|
|
ucpField.setAccessible(true);
|
|
|
|
|
|
|
|
|
|
Object loader = loaderinstanceField.get(null);
|
|
|
|
|
val modClassLoader = (ModClassLoader) modClassLoaderField.get(loader);
|
|
|
|
|
val mainClassLoader = (LaunchClassLoader) mainClassLoaderField.get(modClassLoader);
|
2022-05-07 12:05:09 +02:00
|
|
|
ucp = ucpField.get(mainClassLoader);
|
|
|
|
|
addURL = ucp.getClass().getDeclaredMethod("addURL", URL.class);
|
|
|
|
|
} catch (NoSuchFieldException | IllegalAccessException | NoSuchMethodException e) {
|
2022-04-17 17:26:30 +02:00
|
|
|
throw new RuntimeException(e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
ucp = null;
|
2022-05-07 12:05:09 +02:00
|
|
|
addURL = null;
|
2022-04-17 17:26:30 +02:00
|
|
|
System.err.println("Grimoire detected, disabling jar loading utility");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-06-08 16:01:06 +02:00
|
|
|
* Adds a Jar to the Minecraft URL ClassPath - Needed when using mixins on classes outside of Minecraft or other
|
|
|
|
|
* coremods
|
2022-04-17 17:26:30 +02:00
|
|
|
*/
|
|
|
|
|
public static void addJar(File pathToJar) throws Exception {
|
2022-06-08 16:01:06 +02:00
|
|
|
if (!GRIMOIRE) {
|
2022-05-07 12:05:09 +02:00
|
|
|
addURL.invoke(ucp, pathToJar.toURI().toURL());
|
2022-06-08 16:01:06 +02:00
|
|
|
}
|
2022-04-17 17:26:30 +02:00
|
|
|
}
|
|
|
|
|
}
|