use of net.minecraft.launchwrapper.IClassTransformer in project LogisticsPipes by RS485.
the class DevEnvHelper method handleSpecialClassTransformer.
public static void handleSpecialClassTransformer() throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException, IOException, CantLoadMCPMappingException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException {
if (!isDevelopmentEnvironment() || !new File(".mcpMappings").exists()) {
return;
}
DevEnvHelper.m = new MappingLoader_MCP("1.7.10", Side.UNIVERSAL, new File(".mcpMappings")).getForwardCSV();
final Field ucpF = URLClassLoader.class.getDeclaredField("ucp");
ucpF.setAccessible(true);
Object ucp = ucpF.get(Launch.classLoader);
final Field pathF = ucp.getClass().getDeclaredField("path");
final Field loadersF = ucp.getClass().getDeclaredField("loaders");
final Method addURL = ucp.getClass().getMethod("addURL", URL.class);
pathF.setAccessible(true);
loadersF.setAccessible(true);
addURL.setAccessible(true);
ArrayList<File> modFileList = new ArrayList<>();
File modsFolder = new File("mods");
if (modsFolder.exists()) {
File[] modses = modsFolder.listFiles();
if (modses != null) {
Collections.addAll(modFileList, modses);
}
}
for (File f : modFileList) {
if (!f.isFile()) {
continue;
}
String path = f.getAbsolutePath();
if (path.endsWith("LP_DEOBF.jar")) {
URL toMove = f.toURI().toURL();
addURL.invoke(ucp, toMove);
Enumeration<URL> tmp = Launch.classLoader.findResources("notToBeFound");
while (tmp.hasMoreElements()) {
tmp.nextElement();
}
//ucp = ucpF.get(Launch.classLoader);
@SuppressWarnings("unchecked") List<Object> pathes = (List<Object>) pathF.get(ucp);
@SuppressWarnings("unchecked") List<Object> loaders = (List<Object>) loadersF.get(ucp);
Object toMoveLoaders = null;
Class<?> loaderClass = Class.forName("sun.misc.URLClassPath$Loader");
for (Object loader : loaders) {
Field baseF = loaderClass.getDeclaredField("base");
baseF.setAccessible(true);
URL base = (URL) baseF.get(loader);
if (base.toExternalForm().contains(toMove.toExternalForm())) {
toMoveLoaders = loader;
break;
}
}
Object toMovepathes = pathes.get(loaders.indexOf(toMoveLoaders));
for (int i = loaders.indexOf(toMoveLoaders); i > 0; i--) {
loaders.set(i, loaders.get(i - 1));
pathes.set(i, pathes.get(i - 1));
}
loaders.set(0, toMoveLoaders);
pathes.set(0, toMovepathes);
}
}
Field fTransformers = LaunchClassLoader.class.getDeclaredField("transformers");
fTransformers.setAccessible(true);
@SuppressWarnings("unchecked") List<IClassTransformer> transformers = (List<IClassTransformer>) fTransformers.get(Launch.classLoader);
transformers.add(DevEnvHelper.transformer);
for (int i = transformers.size() - 1; i > 0; i--) {
// Move everything one up
transformers.set(i, transformers.get(i - 1));
}
// So that our injector can be first
transformers.set(0, DevEnvHelper.transformer);
}
use of net.minecraft.launchwrapper.IClassTransformer in project MinecraftForge by MinecraftForge.
the class ModClassLoader method addModAPITransformer.
public ModAPITransformer addModAPITransformer(ASMDataTable dataTable) {
mainClassLoader.registerTransformer("net.minecraftforge.fml.common.asm.transformers.ModAPITransformer");
List<IClassTransformer> transformers = mainClassLoader.getTransformers();
ModAPITransformer modAPI = (ModAPITransformer) transformers.get(transformers.size() - 1);
modAPI.initTable(dataTable);
return modAPI;
}
use of net.minecraft.launchwrapper.IClassTransformer in project LogisticsPipes by RS485.
the class DevEnvHelper method detectCoreModInDevEnv.
@SuppressWarnings("unchecked")
public static void detectCoreModInDevEnv() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, NoSuchFieldException, IOException {
if (!isDevelopmentEnvironment()) {
return;
}
Method handleCascadingTweak = CoreModManager.class.getDeclaredMethod("handleCascadingTweak", File.class, JarFile.class, String.class, LaunchClassLoader.class, Integer.class);
handleCascadingTweak.setAccessible(true);
Method loadCoreMod = CoreModManager.class.getDeclaredMethod("loadCoreMod", LaunchClassLoader.class, String.class, File.class);
loadCoreMod.setAccessible(true);
Method setupCoreModDir = CoreModManager.class.getDeclaredMethod("setupCoreModDir", File.class);
setupCoreModDir.setAccessible(true);
Field loadedCoremods = CoreModManager.class.getDeclaredField("loadedCoremods");
loadedCoremods.setAccessible(true);
Field reparsedCoremods = CoreModManager.class.getDeclaredField("reparsedCoremods");
reparsedCoremods.setAccessible(true);
Field mcDir = CoreModManager.class.getDeclaredField("mcDir");
mcDir.setAccessible(true);
Field transformers = LaunchClassLoader.class.getDeclaredField("transformers");
transformers.setAccessible(true);
LaunchClassLoader classLoader = Launch.classLoader;
FMLRelaunchLog.fine("Discovering coremods");
File coreMods = (File) setupCoreModDir.invoke(null, mcDir.get(null));
FilenameFilter ff = (dir, name) -> name.endsWith(".jar");
File[] FMLcoreModListArray = coreMods.listFiles(ff);
File versionedModDir = new File(coreMods, "1.7.10");
if (versionedModDir.isDirectory()) {
File[] versionedCoreMods = versionedModDir.listFiles(ff);
FMLcoreModListArray = ObjectArrays.concat(FMLcoreModListArray, versionedCoreMods, File.class);
}
List<String> FMLcoreModList = new ArrayList<>();
for (File f : FMLcoreModListArray) {
FMLcoreModList.add(f.getName());
}
List<File> coreModList = new ArrayList<>();
for (URL path : classLoader.getURLs()) {
File file = new File(URLDecoder.decode(path.getFile()));
if (!FMLcoreModList.contains(file.getName()) && file.getName().endsWith(".jar")) {
coreModList.add(file);
}
}
coreModList = Arrays.asList(FileListHelper.sortFileList(coreModList.toArray(new File[coreModList.size()])));
for (File coreMod : coreModList) {
FMLRelaunchLog.fine("Examining for coremod candidacy %s", coreMod.getName());
JarFile jar = null;
Attributes mfAttributes;
try {
jar = new JarFile(coreMod);
if (jar.getManifest() == null) {
// Not a coremod and no access transformer list
continue;
}
ModAccessTransformer.addJar(jar);
mfAttributes = jar.getManifest().getMainAttributes();
} catch (IOException ioe) {
FMLRelaunchLog.log(Level.ERROR, ioe, "Unable to read the jar file %s - ignoring", coreMod.getName());
continue;
} finally {
if (jar != null) {
try {
jar.close();
} catch (IOException e) {
// Noise
}
}
}
//AccessTransformer //For NEI
if (mfAttributes.getValue("AccessTransformer") != null) {
String cfg = mfAttributes.getValue("AccessTransformer");
((List<IClassTransformer>) transformers.get(classLoader)).add(new AccessTransformer(cfg) {
});
}
//FMLAT //For newer NEI
if (mfAttributes.getValue("FMLAT") != null) {
String cfg = "META-INF/" + mfAttributes.getValue("FMLAT");
((List<IClassTransformer>) transformers.get(classLoader)).add(new AccessTransformer(cfg) {
});
}
String cascadedTweaker = mfAttributes.getValue("TweakClass");
if (cascadedTweaker != null) {
FMLRelaunchLog.info("Loading tweaker %s from %s", cascadedTweaker, coreMod.getName());
Integer sortOrder = Ints.tryParse(Strings.nullToEmpty(mfAttributes.getValue("TweakOrder")));
sortOrder = (sortOrder == null ? Integer.valueOf(0) : sortOrder);
handleCascadingTweak.invoke(null, coreMod, jar, cascadedTweaker, classLoader, sortOrder);
((List<String>) loadedCoremods.get(null)).add(coreMod.getName());
continue;
}
String fmlCorePlugin = mfAttributes.getValue("FMLCorePlugin");
if (fmlCorePlugin == null) {
// Not a coremod
FMLRelaunchLog.fine("Not found coremod data in %s", coreMod.getName());
continue;
}
//classLoader.addURL(coreMod.toURI().toURL());
if (!mfAttributes.containsKey(DevEnvHelper.COREMODCONTAINSFMLMOD)) {
FMLRelaunchLog.finer("Adding %s to the list of known coremods, it will not be examined again", coreMod.getName());
((List<String>) loadedCoremods.get(null)).add(coreMod.getName());
} else {
FMLRelaunchLog.finer("Found FMLCorePluginContainsFMLMod marker in %s, it will be examined later for regular @Mod instances", coreMod.getName());
//((List<String>)reparsedCoremods.get(null)).add(coreMod.getName());
}
//} catch(MalformedURLException e) {
// FMLRelaunchLog.log(Level.ERROR, e, "Unable to convert file into a URL. weird");
// continue;
//}
loadCoreMod.invoke(null, classLoader, fmlCorePlugin, coreMod);
}
try {
URL resource = Resources.getResource("CoFH_at.cfg");
if (resource != null) {
AccessTransformer acc = new AccessTransformer("CoFH_at.cfg") {
};
insertTransformer(acc);
}
} catch (Throwable t) {
}
}
use of net.minecraft.launchwrapper.IClassTransformer in project LogisticsPipes by RS485.
the class DevEnvHelper method insertTransformer.
@SneakyThrows
private static void insertTransformer(IClassTransformer transformer) {
Field fTransformers = LaunchClassLoader.class.getDeclaredField("transformers");
fTransformers.setAccessible(true);
@SuppressWarnings("unchecked") List<IClassTransformer> transformerList = (List<IClassTransformer>) fTransformers.get(Launch.classLoader);
transformerList.add(transformer);
}
Aggregations