From 5a34da7fbebbd9f780a1458b0d310dadbda7d78d Mon Sep 17 00:00:00 2001 From: Glease <4586901+Glease@users.noreply.github.com> Date: Fri, 3 Dec 2021 21:47:12 +0800 Subject: Fix ASM problem attempt 2 Former-commit-id: 9e7f7e39f1dd6c8778506c203387913841f74d6c --- .../com/github/bartimaeusnek/ASM/ASMUtils.java | 66 ---- .../java/com/github/bartimaeusnek/ASM/BWCore.java | 92 ------ .../com/github/bartimaeusnek/ASM/BWCorePlugin.java | 89 ------ .../ASM/BWCoreStaticReplacementMethodes.java | 132 -------- .../bartimaeusnek/ASM/BWCoreTransformer.java | 334 --------------------- .../bartimaeusnek/bartworks/ASM/ASMUtils.java | 66 ++++ .../github/bartimaeusnek/bartworks/ASM/BWCore.java | 92 ++++++ .../bartimaeusnek/bartworks/ASM/BWCorePlugin.java | 89 ++++++ .../ASM/BWCoreStaticReplacementMethodes.java | 132 ++++++++ .../bartworks/ASM/BWCoreTransformer.java | 334 +++++++++++++++++++++ .../common/commands/ClearCraftingCache.java | 2 +- .../bartworks/common/configs/ConfigHandler.java | 2 +- .../CircuitGeneration/CircuitImprintLoader.java | 2 +- 13 files changed, 716 insertions(+), 716 deletions(-) delete mode 100644 src/main/java/com/github/bartimaeusnek/ASM/ASMUtils.java delete mode 100644 src/main/java/com/github/bartimaeusnek/ASM/BWCore.java delete mode 100644 src/main/java/com/github/bartimaeusnek/ASM/BWCorePlugin.java delete mode 100644 src/main/java/com/github/bartimaeusnek/ASM/BWCoreStaticReplacementMethodes.java delete mode 100644 src/main/java/com/github/bartimaeusnek/ASM/BWCoreTransformer.java create mode 100644 src/main/java/com/github/bartimaeusnek/bartworks/ASM/ASMUtils.java create mode 100644 src/main/java/com/github/bartimaeusnek/bartworks/ASM/BWCore.java create mode 100644 src/main/java/com/github/bartimaeusnek/bartworks/ASM/BWCorePlugin.java create mode 100644 src/main/java/com/github/bartimaeusnek/bartworks/ASM/BWCoreStaticReplacementMethodes.java create mode 100644 src/main/java/com/github/bartimaeusnek/bartworks/ASM/BWCoreTransformer.java (limited to 'src/main/java') diff --git a/src/main/java/com/github/bartimaeusnek/ASM/ASMUtils.java b/src/main/java/com/github/bartimaeusnek/ASM/ASMUtils.java deleted file mode 100644 index 9055909567..0000000000 --- a/src/main/java/com/github/bartimaeusnek/ASM/ASMUtils.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (c) 2018-2020 bartimaeusnek - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package com.github.bartimaeusnek.ASM; - -import org.objectweb.asm.tree.MethodNode; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStream; - -public class ASMUtils { - - public static String matchAny(String toCompare, String... args) { - for (String arg : args) { - if (toCompare.equalsIgnoreCase(arg)) - return arg; - } - return ""; - } - - /** - * Call this Method twice, one time for the Descriptor and one time for the Name. - */ - public static boolean isCorrectMethod(MethodNode methodNode, String... args) { - for (String arg : args) { - if (methodNode.name.equalsIgnoreCase(arg) || methodNode.desc.equalsIgnoreCase(arg)) - return true; - } - return false; - } - - public static boolean writeClassToDisk(byte[] towrite, String Classname, String Path) { - try { - if (Path.charAt(Path.length()-1) == '/' || Path.charAt(Path.length()-1) == '\\') - Path = Path.substring(0,Path.length()-1); - OutputStream os = new FileOutputStream(new File(Path + '/' + Classname + ".class")); - os.write(towrite); - } catch (IOException e) { - e.printStackTrace(); - return false; - } - return true; - } - -} diff --git a/src/main/java/com/github/bartimaeusnek/ASM/BWCore.java b/src/main/java/com/github/bartimaeusnek/ASM/BWCore.java deleted file mode 100644 index 6592da592f..0000000000 --- a/src/main/java/com/github/bartimaeusnek/ASM/BWCore.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Copyright (c) 2018-2020 bartimaeusnek - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package com.github.bartimaeusnek.ASM; - -import com.github.bartimaeusnek.bartworks.MainMod; -import com.github.bartimaeusnek.bartworks.common.configs.ConfigHandler; -import com.github.bartimaeusnek.crossmod.BartWorksCrossmod; -import com.google.common.eventbus.EventBus; -import com.google.common.eventbus.Subscribe; -import cpw.mods.fml.common.DummyModContainer; -import cpw.mods.fml.common.LoadController; -import cpw.mods.fml.common.ModMetadata; -import cpw.mods.fml.common.event.FMLPreInitializationEvent; -import cpw.mods.fml.common.versioning.ArtifactVersion; -import cpw.mods.fml.common.versioning.DefaultArtifactVersion; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import java.util.ArrayList; -import java.util.List; - -import static com.github.bartimaeusnek.ASM.BWCoreTransformer.shouldTransform; - -@SuppressWarnings("ALL") -public class BWCore extends DummyModContainer { - - public static final String BWCORE_NAME = "BartWorks ASM Core"; - public static final Logger BWCORE_LOG = LogManager.getLogger(BWCore.BWCORE_NAME); - - public BWCore() { - super(new ModMetadata()); - ModMetadata metadata = this.getMetadata(); - metadata.modId = "BWCore"; - metadata.name = BWCore.BWCORE_NAME; - metadata.version = "0.0.1"; - metadata.authorList.add("bartimaeusnek"); - metadata.dependants = this.getDependants(); - } - - @Subscribe - public void preInit(FMLPreInitializationEvent event) { - shouldTransform[0] = ConfigHandler.enabledPatches[0]; - shouldTransform[1] = ConfigHandler.enabledPatches[1]; - shouldTransform[3] = ConfigHandler.enabledPatches[3]; - shouldTransform[4] = true; - shouldTransform[5] = ConfigHandler.enabledPatches[5]; - shouldTransform[6] = ConfigHandler.enabledPatches[6]; - //shouldTransform[6] = true; - BWCore.BWCORE_LOG.info("Extra Utilities found and ASM Patch enabled? " + shouldTransform[0]); - BWCore.BWCORE_LOG.info("Thaumcraft found and ASM Patch enabled? " + shouldTransform[3]); - BWCore.BWCORE_LOG.info("RWG found and ASM Patch enabled? " + shouldTransform[5]); - } - - @Override - public List getDependants() { - List ret = new ArrayList<>(); - ret.add(new DefaultArtifactVersion("ExtraUtilities", true)); - ret.add(new DefaultArtifactVersion("Thaumcraft", true)); - ret.add(new DefaultArtifactVersion("miscutils", true)); - ret.add(new DefaultArtifactVersion("RWG", true)); - ret.add(new DefaultArtifactVersion("gregtech", true)); - ret.add(new DefaultArtifactVersion(MainMod.MOD_ID, true)); - ret.add(new DefaultArtifactVersion(BartWorksCrossmod.MOD_ID, true)); - return ret; - } - - @Override - public boolean registerBus(EventBus bus, LoadController controller) { - bus.register(this); - return true; - } -} diff --git a/src/main/java/com/github/bartimaeusnek/ASM/BWCorePlugin.java b/src/main/java/com/github/bartimaeusnek/ASM/BWCorePlugin.java deleted file mode 100644 index bbaff54ba1..0000000000 --- a/src/main/java/com/github/bartimaeusnek/ASM/BWCorePlugin.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright (c) 2018-2020 bartimaeusnek - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package com.github.bartimaeusnek.ASM; - -import com.github.bartimaeusnek.bartworks.common.configs.ConfigHandler; -import cpw.mods.fml.relauncher.FMLInjectionData; -import cpw.mods.fml.relauncher.IFMLLoadingPlugin; -import net.minecraftforge.common.config.Configuration; - -import java.io.File; -import java.util.ArrayList; -import java.util.Map; - -@IFMLLoadingPlugin.SortingIndex(Integer.MAX_VALUE)//Load as late as possible (after fastcraft/OptiFine). -@IFMLLoadingPlugin.MCVersion("1.7.10") -@IFMLLoadingPlugin.TransformerExclusions("com.github.bartimaeusnek.ASM") -@IFMLLoadingPlugin.Name(BWCorePlugin.BWCORE_PLUGIN_NAME) -public class BWCorePlugin implements IFMLLoadingPlugin { - - public static final String BWCORE_PLUGIN_NAME = "BartWorks ASM Core Plugin"; - - public static File minecraftDir; - - public BWCorePlugin() { - //Injection Code taken from CodeChickenLib - if (BWCorePlugin.minecraftDir != null) - return;//get called twice, once for IFMLCallHook - BWCorePlugin.minecraftDir = (File) FMLInjectionData.data()[6]; - //do all the configuration already now... - new ConfigHandler(new Configuration(new File(new File(BWCorePlugin.minecraftDir, "config"), "bartworks.cfg"))); - BWCoreTransformer.shouldTransform[2] = false; - } - - @Override - public String[] getASMTransformerClass() { - return new String[]{BWCoreTransformer.class.getName()}; - } - - @Override - public String getModContainerClass() { - return BWCore.class.getName(); - } - - @Override - public String getSetupClass() { - return null; - } - - @Override - @SuppressWarnings("rawtypes") - public void injectData(Map data) { - if (data.get("runtimeDeobfuscationEnabled") != null) { - BWCoreTransformer.obfs = (boolean) data.get("runtimeDeobfuscationEnabled"); - } - if (data.get("coremodList") != null) { - for (Object o : (ArrayList) data.get("coremodList")) { - if (o.toString().contains("MicdoodlePlugin")) { - BWCoreTransformer.shouldTransform[2] = ConfigHandler.enabledPatches[2]; - break; - } - } - } - } - - @Override - public String getAccessTransformerClass() { - return null; - } -} diff --git a/src/main/java/com/github/bartimaeusnek/ASM/BWCoreStaticReplacementMethodes.java b/src/main/java/com/github/bartimaeusnek/ASM/BWCoreStaticReplacementMethodes.java deleted file mode 100644 index 47705042a9..0000000000 --- a/src/main/java/com/github/bartimaeusnek/ASM/BWCoreStaticReplacementMethodes.java +++ /dev/null @@ -1,132 +0,0 @@ -/* - * Copyright (c) 2018-2020 bartimaeusnek - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package com.github.bartimaeusnek.ASM; - -import com.github.bartimaeusnek.bartworks.util.NonNullWrappedHashSet; -import com.github.bartimaeusnek.bartworks.util.accessprioritylist.AccessPriorityList; -import com.github.bartimaeusnek.bartworks.util.accessprioritylist.AccessPriorityListNode; -import net.minecraft.inventory.InventoryCrafting; -import net.minecraft.item.Item; -import net.minecraft.item.ItemStack; -import net.minecraft.item.crafting.CraftingManager; -import net.minecraft.item.crafting.IRecipe; -import net.minecraft.world.World; - -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; - -public class BWCoreStaticReplacementMethodes { - private static ThreadLocal> RECENTLYUSEDRECIPES = ThreadLocal.withInitial(AccessPriorityList::new); - - public static void clearRecentlyUsedRecipes() { - // the easiest way to ensure the cache is flushed without causing synchronization overhead - // is to just replace the whole ThreadLocal instance. - RECENTLYUSEDRECIPES = ThreadLocal.withInitial(AccessPriorityList::new); - } - - @SuppressWarnings("ALL") - public static ItemStack findCachedMatchingRecipe(InventoryCrafting inventoryCrafting, World world) { - int i = 0; - ItemStack itemstack = null; - ItemStack itemstack1 = null; - int j; - - for (j = 0; j < inventoryCrafting.getSizeInventory(); ++j) - { - ItemStack itemstack2 = inventoryCrafting.getStackInSlot(j); - - if (itemstack2 != null) - { - if (i == 0) - itemstack = itemstack2; - - if (i == 1) - itemstack1 = itemstack2; - - ++i; - } - } - - if (i == 2 && itemstack.getItem() == itemstack1.getItem() && itemstack.stackSize == 1 && itemstack1.stackSize == 1 && itemstack.getItem().isRepairable()) { - Item item = itemstack.getItem(); - int j1 = item.getMaxDamage() - itemstack.getItemDamageForDisplay(); - int k = item.getMaxDamage() - itemstack1.getItemDamageForDisplay(); - int l = j1 + k + item.getMaxDamage() * 5 / 100; - int i1 = item.getMaxDamage() - l; - - if (i1 < 0) - i1 = 0; - - return new ItemStack(itemstack.getItem(), 1, i1); - - } else { - - IRecipe iPossibleRecipe = null; - AccessPriorityList cache = RECENTLYUSEDRECIPES.get(); - Iterator> it = cache.nodeIterator(); - - while (it.hasNext()) { - AccessPriorityListNode recipeNode = it.next(); - iPossibleRecipe = recipeNode.getELEMENT(); - - if (!iPossibleRecipe.matches(inventoryCrafting, world)) - continue; - - cache.addPrioToNode(recipeNode); - return iPossibleRecipe.getCraftingResult(inventoryCrafting); - } - - ItemStack stack = null; - - HashSet recipeSet = new NonNullWrappedHashSet<>(); - List recipeList = CraftingManager.getInstance().getRecipeList(); - - for (int k = 0; k < recipeList.size(); k++) { - IRecipe r = (IRecipe) recipeList.get(k); - if (r.matches(inventoryCrafting, world)) - recipeSet.add(r); - } - - Object[] arr = recipeSet.toArray(); - - if (arr.length == 0) - return null; - - IRecipe recipe = (IRecipe) arr[0]; - stack = recipe.getCraftingResult(inventoryCrafting); - - if (arr.length != 1) - return stack; - - if (stack != null) - cache.addLast(recipe); - - return stack; - } - } - - private BWCoreStaticReplacementMethodes() { - } - -} \ No newline at end of file diff --git a/src/main/java/com/github/bartimaeusnek/ASM/BWCoreTransformer.java b/src/main/java/com/github/bartimaeusnek/ASM/BWCoreTransformer.java deleted file mode 100644 index 775c3fc9b1..0000000000 --- a/src/main/java/com/github/bartimaeusnek/ASM/BWCoreTransformer.java +++ /dev/null @@ -1,334 +0,0 @@ -/* - * Copyright (c) 2018-2020 bartimaeusnek - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package com.github.bartimaeusnek.ASM; - -import net.minecraft.launchwrapper.IClassTransformer; -import org.objectweb.asm.ClassReader; -import org.objectweb.asm.ClassWriter; -import org.objectweb.asm.tree.*; - -import java.util.Arrays; -import java.util.List; - -import static org.objectweb.asm.Opcodes.*; - -public class BWCoreTransformer implements IClassTransformer { - public static final String[] DESCRIPTIONFORCONFIG = { - "REMOVING RAIN FROM LAST MILLENIUM (EXU)", - "REMVOING CREATURES FROM LAST MILLENIUM (EXU)", - "PATCHING GLOBAL RENDERER FOR USE WITH MY GALACTIC DIMS", - "PATCHING THAUMCRAFT WAND PEDESTAL TO PREVENT VIS DUPLICATION", - "PLACING MY GLASS-BLOCK RUNNABLE INTO THE GT_API", - "DUCTTAPING RWG WORLDEN FAILS", - "PATCHING CRAFTING MANAGER FOR CACHING RECIPES" - // "REMOVING 12% BONUS OUTPUTS FROM GT++ SIFTER" - }; - public static final String[] CLASSESBEEINGTRANSFORMED = { - "com.rwtema.extrautils.worldgen.endoftime.WorldProviderEndOfTime", - "com.rwtema.extrautils.worldgen.endoftime.ChunkProviderEndOfTime", - "net.minecraft.client.renderer.RenderGlobal", - "thaumcraft.common.tiles.TileWandPedestal", - "gregtech.GT_Mod", - "rwg.world.ChunkGeneratorRealistic", - "net.minecraft.item.crafting.CraftingManager" - // "gtPlusPlus.xmod.gregtech.common.tileentities.machines.multi.processing.GregtechMetaTileEntity_IndustrialSifter" - }; - static boolean obfs; - - public static boolean[] shouldTransform = new boolean[BWCoreTransformer.CLASSESBEEINGTRANSFORMED.length]; - - /** - * Made by DarkShaddow44 - */ - private static MethodNode transformThaumcraftWandPedestal(MethodNode method) { - InsnList nu = new InsnList(); - for (int j = 0; j < method.instructions.size(); j++) { - AbstractInsnNode instruction = method.instructions.get(j); - nu.add(instruction); - if (instruction.getOpcode() == INVOKEVIRTUAL) { - MethodInsnNode invokevirtual = (MethodInsnNode) instruction; - if (invokevirtual.name.equals("addVis")) { - AbstractInsnNode beginning = method.instructions.get(j - 7); - LabelNode label = new LabelNode(); - nu.insertBefore(beginning, new VarInsnNode(ALOAD, 0)); - nu.insertBefore(beginning, new FieldInsnNode(GETFIELD, "thaumcraft/common/tiles/TileWandPedestal", obfs ? "field_145850_b" : "worldObj", "Lnet/minecraft/world/World;")); - nu.insertBefore(beginning, new FieldInsnNode(GETFIELD, "net/minecraft/world/World", obfs ? "field_72995_K" : "isRemote", "Z")); - nu.insertBefore(beginning, new JumpInsnNode(IFNE, label)); - nu.add(new InsnNode(POP)); - nu.add(label); - j++; // Skip actual Pop - } - } - } - method.instructions = nu; - return method; - } - - public static byte[] transform(int id, byte[] basicClass) { - if (!BWCoreTransformer.shouldTransform[id]) { - BWCore.BWCORE_LOG.info("Patch: " + BWCoreTransformer.DESCRIPTIONFORCONFIG[id] + " is disabled, will not patch!"); - return basicClass; - } - - if (id < BWCoreTransformer.CLASSESBEEINGTRANSFORMED.length) { - BWCore.BWCORE_LOG.info(BWCoreTransformer.DESCRIPTIONFORCONFIG[id]); - ClassReader classReader = new ClassReader(basicClass); - ClassNode classNode = new ClassNode(); - classReader.accept(classNode, ClassReader.SKIP_FRAMES); - List methods = classNode.methods; - scase: - switch (id) { - case 0: { - BWCore.BWCORE_LOG.info("Could find: " + BWCoreTransformer.CLASSESBEEINGTRANSFORMED[id]); - String name_deObfs = "canDoRainSnowIce"; - - String dsc_deObfs = "(Lnet/minecraft/world/chunk/Chunk;)Z"; - String dsc_Obfs = "(Lapx;)Z"; - for (int i = 0; i < methods.size(); i++) { - if (methods.get(i).name.equalsIgnoreCase(name_deObfs)) { - BWCore.BWCORE_LOG.info("Found " + name_deObfs + "! Removing!"); - methods.remove(i); - break; - } - } - BWCore.BWCORE_LOG.info("Creating new " + name_deObfs + "!"); - MethodNode nu = new MethodNode(ACC_PUBLIC, name_deObfs, - /*obfs ? dsc_Obfs :*/ dsc_deObfs, - null, - new String[0] - ); - InsnList insnList = new InsnList(); - insnList.add(new InsnNode(ICONST_0)); - insnList.add(new InsnNode(IRETURN)); - nu.instructions = insnList; - nu.maxLocals = 1; - nu.maxStack = 1; - methods.add(nu); - break; - } - case 1: { - BWCore.BWCORE_LOG.info("Could find: " + BWCoreTransformer.CLASSESBEEINGTRANSFORMED[id]); - String name_deObfs = "getPossibleCreatures"; - String name_src = "func_73155_a"; - String name_Obfs = "a"; - String dsc_deObfs = "(Lnet/minecraft/entity/EnumCreatureType;III)Ljava/util/List;"; - String dsc_Obfs = "(Lsx;III)Ljava/util/List;"; - - for (int i = 0; i < methods.size(); i++) { - if (ASMUtils.isCorrectMethod(methods.get(i), name_deObfs, name_Obfs, name_src) && ASMUtils.isCorrectMethod(methods.get(i), dsc_deObfs, dsc_Obfs)) { - BWCore.BWCORE_LOG.info("Found " + (name_deObfs) + "! Patching!"); - MethodNode toPatch = methods.get(i); - InsnList insnList = new InsnList(); - insnList.add(new InsnNode(ACONST_NULL)); - insnList.add(new InsnNode(ARETURN)); - toPatch.instructions = insnList; - toPatch.maxStack = 1; - toPatch.maxLocals = 5; - methods.set(i, toPatch); - break scase; - } - } - } - case 2: { - String name_deObfs = "renderSky"; - String name_src = "func_72714_a"; - String name_Obfs = "a"; - String dsc_universal = "(F)V"; - String field_deObfs = "locationSunPng"; - String field_src = "field_110928_i"; - BWCore.BWCORE_LOG.info("Could find: " + BWCoreTransformer.CLASSESBEEINGTRANSFORMED[id]); - for (MethodNode toPatch : methods) { - if (ASMUtils.isCorrectMethod(toPatch, name_deObfs, name_Obfs, name_src) && ASMUtils.isCorrectMethod(toPatch, dsc_universal)) { - BWCore.BWCORE_LOG.info("Found " + (name_deObfs) + "! Patching!"); - InsnList nu = new InsnList(); - LabelNode[] LabelNodes = {new LabelNode(), new LabelNode()}; - - String theWorld_src = "field_72769_h"; - String renderEngine_src = "field_72770_i"; - String provider_src = "field_73011_w"; - String bindTexture_src = "func_110577_a"; - String nameFieldToPatch; - - for (int j = 0; j < toPatch.instructions.size(); j++) { - if (toPatch.instructions.get(j) instanceof FieldInsnNode && toPatch.instructions.get(j).getOpcode() == GETSTATIC && !(nameFieldToPatch = ASMUtils.matchAny(((FieldInsnNode) toPatch.instructions.get(j)).name, field_deObfs, field_src)).isEmpty()) { - boolean useSrc = nameFieldToPatch.equals(field_src); - if (useSrc) - BWCore.BWCORE_LOG.info("Found either Optifine or Fastcraft... this patch was annoying to make compatible to them..."); - - nu.add(new VarInsnNode(ALOAD, 0)); - nu.add(new FieldInsnNode(GETFIELD, "net/minecraft/client/renderer/RenderGlobal", useSrc ? theWorld_src : "theWorld", "Lnet/minecraft/client/multiplayer/WorldClient;")); - nu.add(new FieldInsnNode(GETFIELD, "net/minecraft/client/multiplayer/WorldClient", useSrc ? provider_src : "provider", "Lnet/minecraft/world/WorldProvider;")); - nu.add(new TypeInsnNode(INSTANCEOF, "com/github/bartimaeusnek/crossmod/galacticraft/planets/AbstractWorldProviderSpace")); - nu.add(new JumpInsnNode(IFEQ, LabelNodes[0])); - nu.add(new VarInsnNode(ALOAD, 0)); - nu.add(new FieldInsnNode(GETFIELD, "net/minecraft/client/renderer/RenderGlobal", useSrc ? renderEngine_src : "renderEngine", "Lnet/minecraft/client/renderer/texture/TextureManager;")); - nu.add(new FieldInsnNode(GETSTATIC, "com/github/bartimaeusnek/crossmod/galacticraft/planets/ross128b/SkyProviderRoss128b", "sunTex", "Lnet/minecraft/util/ResourceLocation;")); - nu.add(new MethodInsnNode(INVOKEVIRTUAL, "net/minecraft/client/renderer/texture/TextureManager", useSrc ? bindTexture_src : "bindTexture", "(Lnet/minecraft/util/ResourceLocation;)V", false)); - nu.add(new JumpInsnNode(GOTO, LabelNodes[1])); - nu.add(LabelNodes[0]); - nu.add(new VarInsnNode(ALOAD, 0)); - nu.add(new FieldInsnNode(GETFIELD, "net/minecraft/client/renderer/RenderGlobal", useSrc ? renderEngine_src : "renderEngine", "Lnet/minecraft/client/renderer/texture/TextureManager;")); - nu.add(new FieldInsnNode(GETSTATIC, "net/minecraft/client/renderer/RenderGlobal", useSrc ? field_src : "locationSunPng", "Lnet/minecraft/util/ResourceLocation;")); - nu.add(new MethodInsnNode(INVOKEVIRTUAL, "net/minecraft/client/renderer/texture/TextureManager", useSrc ? bindTexture_src : "bindTexture", "(Lnet/minecraft/util/ResourceLocation;)V", false)); - nu.add(LabelNodes[1]); - j++; - - } else { - if (j < toPatch.instructions.size() - 2) { - if (toPatch.instructions.get(j + 2) instanceof FieldInsnNode && toPatch.instructions.get(j + 2).getOpcode() == GETSTATIC && !ASMUtils.matchAny(((FieldInsnNode) toPatch.instructions.get(j + 2)).name, field_deObfs, field_src).isEmpty()) - continue; - if (toPatch.instructions.get(j + 1) instanceof FieldInsnNode && toPatch.instructions.get(j + 1).getOpcode() == GETSTATIC && !ASMUtils.matchAny(((FieldInsnNode) toPatch.instructions.get(j + 1)).name, field_deObfs, field_src).isEmpty()) - continue; - } - nu.add(toPatch.instructions.get(j)); - } - } - toPatch.instructions = nu; - break scase; - } - } - } - case 3: { - BWCore.BWCORE_LOG.info("Could find: " + BWCoreTransformer.CLASSESBEEINGTRANSFORMED[id]); - String name_deObfs = "updateEntity"; - String name_src = "func_145845_h"; - String name_Obfs = "h"; - String dsc_universal = "()V"; - - for (int i = 0; i < methods.size(); i++) { - if (ASMUtils.isCorrectMethod(methods.get(i), name_deObfs, name_Obfs, name_src) && ASMUtils.isCorrectMethod(methods.get(i), dsc_universal, dsc_universal)) { - BWCore.BWCORE_LOG.info("Found " + (name_deObfs) + "! Patching!"); - methods.set(i, BWCoreTransformer.transformThaumcraftWandPedestal(methods.get(i))); - break scase; - } - } - } - case 4 : { - BWCore.BWCORE_LOG.info("Could find: " + BWCoreTransformer.CLASSESBEEINGTRANSFORMED[id]); - String name_deObfs = ""; - for (MethodNode toPatch : methods) { - if (ASMUtils.isCorrectMethod(toPatch, name_deObfs) && (toPatch.access & ACC_STATIC) != 0) { - BWCore.BWCORE_LOG.info("Found " + (name_deObfs) + "! Patching!"); - InsnList nu = new InsnList(); - LabelNode[] LabelNodes = {new LabelNode(), new LabelNode()}; - for (int j = 0; j < 2; j++) { - nu.add(toPatch.instructions.get(j)); - } - nu.add(new FieldInsnNode(GETSTATIC, "gregtech/api/GregTech_API", "sBeforeGTPreload", "Ljava/util/List;")); - nu.add(new TypeInsnNode(NEW, "com/github/bartimaeusnek/bartworks/common/loaders/BeforeGTPreload")); - nu.add(new InsnNode(DUP)); - nu.add(new MethodInsnNode(INVOKESPECIAL, "com/github/bartimaeusnek/bartworks/common/loaders/BeforeGTPreload", "", "()V", false)); - nu.add(new MethodInsnNode(INVOKEINTERFACE, "java/util/List", "add", "(Ljava/lang/Object;)Z", true)); - nu.add(new InsnNode(POP)); - for (int j = 2; j < toPatch.instructions.size(); j++) { - nu.add(toPatch.instructions.get(j)); - } - toPatch.instructions = nu; - break scase; - } - } - } - case 5: { - BWCore.BWCORE_LOG.info("Could find: " + BWCoreTransformer.CLASSESBEEINGTRANSFORMED[id]); - String name_deObfs = "getNewNoise"; - for (MethodNode toPatch : methods) { - if (ASMUtils.isCorrectMethod(toPatch, name_deObfs)) { - BWCore.BWCORE_LOG.info("Found " + (name_deObfs) + "! Patching!"); - LabelNode[] LabelNodes = {new LabelNode(), new LabelNode()}; - InsnList nu = new InsnList(); - // if (x < -28675) x %= -28675; - nu.add(new VarInsnNode(ILOAD, 2)); - nu.add(new IntInsnNode(SIPUSH, -28675)); - nu.add(new JumpInsnNode(IF_ICMPGE, LabelNodes[0])); - nu.add(new VarInsnNode(ILOAD, 2)); - nu.add(new LdcInsnNode(-28675)); - nu.add(new InsnNode(IREM)); - nu.add(new VarInsnNode(ISTORE, 2)); - nu.add(LabelNodes[0]); - // if (y < -28675) y %= -28675; - nu.add(new VarInsnNode(ILOAD, 3)); - nu.add(new IntInsnNode(SIPUSH, -28675)); - nu.add(new JumpInsnNode(IF_ICMPGE, LabelNodes[1])); - nu.add(new VarInsnNode(ILOAD, 3)); - nu.add(new LdcInsnNode(-28675)); - nu.add(new InsnNode(IREM)); - nu.add(new VarInsnNode(ISTORE, 3)); - nu.add(LabelNodes[1]); - - for (int j = 1; j < toPatch.instructions.size(); j++) { - nu.add(toPatch.instructions.get(j)); - } - - toPatch.instructions = nu; - break scase; - } - } - } - case 6: { - BWCore.BWCORE_LOG.info("Could find: " + BWCoreTransformer.CLASSESBEEINGTRANSFORMED[id]); - String name_deObfs = "findMatchingRecipe"; - String name_Obfs = "a"; - String name_src = "func_82787_a"; - for (MethodNode toPatch : methods) { - if (ASMUtils.isCorrectMethod(toPatch,name_deObfs,name_Obfs,name_src)){ - toPatch.instructions = new InsnList(); - toPatch.instructions.add(new VarInsnNode(ALOAD,1)); - toPatch.instructions.add(new VarInsnNode(ALOAD,2)); - toPatch.instructions.add(new MethodInsnNode(INVOKESTATIC,"com/github/bartimaeusnek/ASM/BWCoreStaticReplacementMethodes","findCachedMatchingRecipe","(Lnet/minecraft/inventory/InventoryCrafting;Lnet/minecraft/world/World;)Lnet/minecraft/item/ItemStack;",false)); - toPatch.instructions.add(new InsnNode(ARETURN)); - break scase; - } - } - } - -// case 6: { -// BWCore.BWCORE_LOG.info("Could find: " + BWCoreTransformer.CLASSESBEEINGTRANSFORMED[id]); -// ((IntInsnNode) methods.get(11).instructions.get(10)).operand = 10000; -// break scase; -// } - default: { - BWCore.BWCORE_LOG.info("Could not find: " + BWCoreTransformer.CLASSESBEEINGTRANSFORMED[id]); - return basicClass; - } - } - - classNode.methods = methods; - ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES); - classNode.accept(classWriter); - byte[] ret = classWriter.toByteArray(); - if (Arrays.hashCode(basicClass) == Arrays.hashCode(ret)) - BWCore.BWCORE_LOG.warn("Could not patch: " + BWCoreTransformer.CLASSESBEEINGTRANSFORMED[id]); - return ret; - } - return basicClass; - } - - @Override - public byte[] transform(String name, String transformedName, byte[] basicClass) { - for (int i = 0; i < BWCoreTransformer.CLASSESBEEINGTRANSFORMED.length; i++) { - if (name.equalsIgnoreCase(BWCoreTransformer.CLASSESBEEINGTRANSFORMED[i]) || transformedName.equalsIgnoreCase(BWCoreTransformer.CLASSESBEEINGTRANSFORMED[i])) - return BWCoreTransformer.transform(i, basicClass); - } - return basicClass; - } -} diff --git a/src/main/java/com/github/bartimaeusnek/bartworks/ASM/ASMUtils.java b/src/main/java/com/github/bartimaeusnek/bartworks/ASM/ASMUtils.java new file mode 100644 index 0000000000..48a8f49d5c --- /dev/null +++ b/src/main/java/com/github/bartimaeusnek/bartworks/ASM/ASMUtils.java @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2018-2020 bartimaeusnek + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package com.github.bartimaeusnek.bartworks.ASM; + +import org.objectweb.asm.tree.MethodNode; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +public class ASMUtils { + + public static String matchAny(String toCompare, String... args) { + for (String arg : args) { + if (toCompare.equalsIgnoreCase(arg)) + return arg; + } + return ""; + } + + /** + * Call this Method twice, one time for the Descriptor and one time for the Name. + */ + public static boolean isCorrectMethod(MethodNode methodNode, String... args) { + for (String arg : args) { + if (methodNode.name.equalsIgnoreCase(arg) || methodNode.desc.equalsIgnoreCase(arg)) + return true; + } + return false; + } + + public static boolean writeClassToDisk(byte[] towrite, String Classname, String Path) { + try { + if (Path.charAt(Path.length()-1) == '/' || Path.charAt(Path.length()-1) == '\\') + Path = Path.substring(0,Path.length()-1); + OutputStream os = new FileOutputStream(new File(Path + '/' + Classname + ".class")); + os.write(towrite); + } catch (IOException e) { + e.printStackTrace(); + return false; + } + return true; + } + +} diff --git a/src/main/java/com/github/bartimaeusnek/bartworks/ASM/BWCore.java b/src/main/java/com/github/bartimaeusnek/bartworks/ASM/BWCore.java new file mode 100644 index 0000000000..e97de8cf21 --- /dev/null +++ b/src/main/java/com/github/bartimaeusnek/bartworks/ASM/BWCore.java @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2018-2020 bartimaeusnek + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package com.github.bartimaeusnek.bartworks.ASM; + +import com.github.bartimaeusnek.bartworks.MainMod; +import com.github.bartimaeusnek.bartworks.common.configs.ConfigHandler; +import com.github.bartimaeusnek.crossmod.BartWorksCrossmod; +import com.google.common.eventbus.EventBus; +import com.google.common.eventbus.Subscribe; +import cpw.mods.fml.common.DummyModContainer; +import cpw.mods.fml.common.LoadController; +import cpw.mods.fml.common.ModMetadata; +import cpw.mods.fml.common.event.FMLPreInitializationEvent; +import cpw.mods.fml.common.versioning.ArtifactVersion; +import cpw.mods.fml.common.versioning.DefaultArtifactVersion; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.ArrayList; +import java.util.List; + +import static com.github.bartimaeusnek.bartworks.ASM.BWCoreTransformer.shouldTransform; + +@SuppressWarnings("ALL") +public class BWCore extends DummyModContainer { + + public static final String BWCORE_NAME = "BartWorks ASM Core"; + public static final Logger BWCORE_LOG = LogManager.getLogger(BWCore.BWCORE_NAME); + + public BWCore() { + super(new ModMetadata()); + ModMetadata metadata = this.getMetadata(); + metadata.modId = "BWCore"; + metadata.name = BWCore.BWCORE_NAME; + metadata.version = "0.0.1"; + metadata.authorList.add("bartimaeusnek"); + metadata.dependants = this.getDependants(); + } + + @Subscribe + public void preInit(FMLPreInitializationEvent event) { + shouldTransform[0] = ConfigHandler.enabledPatches[0]; + shouldTransform[1] = ConfigHandler.enabledPatches[1]; + shouldTransform[3] = ConfigHandler.enabledPatches[3]; + shouldTransform[4] = true; + shouldTransform[5] = ConfigHandler.enabledPatches[5]; + shouldTransform[6] = ConfigHandler.enabledPatches[6]; + //shouldTransform[6] = true; + BWCore.BWCORE_LOG.info("Extra Utilities found and ASM Patch enabled? " + shouldTransform[0]); + BWCore.BWCORE_LOG.info("Thaumcraft found and ASM Patch enabled? " + shouldTransform[3]); + BWCore.BWCORE_LOG.info("RWG found and ASM Patch enabled? " + shouldTransform[5]); + } + + @Override + public List getDependants() { + List ret = new ArrayList<>(); + ret.add(new DefaultArtifactVersion("ExtraUtilities", true)); + ret.add(new DefaultArtifactVersion("Thaumcraft", true)); + ret.add(new DefaultArtifactVersion("miscutils", true)); + ret.add(new DefaultArtifactVersion("RWG", true)); + ret.add(new DefaultArtifactVersion("gregtech", true)); + ret.add(new DefaultArtifactVersion(MainMod.MOD_ID, true)); + ret.add(new DefaultArtifactVersion(BartWorksCrossmod.MOD_ID, true)); + return ret; + } + + @Override + public boolean registerBus(EventBus bus, LoadController controller) { + bus.register(this); + return true; + } +} diff --git a/src/main/java/com/github/bartimaeusnek/bartworks/ASM/BWCorePlugin.java b/src/main/java/com/github/bartimaeusnek/bartworks/ASM/BWCorePlugin.java new file mode 100644 index 0000000000..37a64add7c --- /dev/null +++ b/src/main/java/com/github/bartimaeusnek/bartworks/ASM/BWCorePlugin.java @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2018-2020 bartimaeusnek + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package com.github.bartimaeusnek.bartworks.ASM; + +import com.github.bartimaeusnek.bartworks.common.configs.ConfigHandler; +import cpw.mods.fml.relauncher.FMLInjectionData; +import cpw.mods.fml.relauncher.IFMLLoadingPlugin; +import net.minecraftforge.common.config.Configuration; + +import java.io.File; +import java.util.ArrayList; +import java.util.Map; + +@IFMLLoadingPlugin.SortingIndex(Integer.MAX_VALUE)//Load as late as possible (after fastcraft/OptiFine). +@IFMLLoadingPlugin.MCVersion("1.7.10") +@IFMLLoadingPlugin.TransformerExclusions("com.github.bartimaeusnek.bartworks.ASM") +@IFMLLoadingPlugin.Name(BWCorePlugin.BWCORE_PLUGIN_NAME) +public class BWCorePlugin implements IFMLLoadingPlugin { + + public static final String BWCORE_PLUGIN_NAME = "BartWorks ASM Core Plugin"; + + public static File minecraftDir; + + public BWCorePlugin() { + //Injection Code taken from CodeChickenLib + if (BWCorePlugin.minecraftDir != null) + return;//get called twice, once for IFMLCallHook + BWCorePlugin.minecraftDir = (File) FMLInjectionData.data()[6]; + //do all the configuration already now... + new ConfigHandler(new Configuration(new File(new File(BWCorePlugin.minecraftDir, "config"), "bartworks.cfg"))); + BWCoreTransformer.shouldTransform[2] = false; + } + + @Override + public String[] getASMTransformerClass() { + return new String[]{BWCoreTransformer.class.getName()}; + } + + @Override + public String getModContainerClass() { + return BWCore.class.getName(); + } + + @Override + public String getSetupClass() { + return null; + } + + @Override + @SuppressWarnings("rawtypes") + public void injectData(Map data) { + if (data.get("runtimeDeobfuscationEnabled") != null) { + BWCoreTransformer.obfs = (boolean) data.get("runtimeDeobfuscationEnabled"); + } + if (data.get("coremodList") != null) { + for (Object o : (ArrayList) data.get("coremodList")) { + if (o.toString().contains("MicdoodlePlugin")) { + BWCoreTransformer.shouldTransform[2] = ConfigHandler.enabledPatches[2]; + break; + } + } + } + } + + @Override + public String getAccessTransformerClass() { + return null; + } +} diff --git a/src/main/java/com/github/bartimaeusnek/bartworks/ASM/BWCoreStaticReplacementMethodes.java b/src/main/java/com/github/bartimaeusnek/bartworks/ASM/BWCoreStaticReplacementMethodes.java new file mode 100644 index 0000000000..e4b14820c7 --- /dev/null +++ b/src/main/java/com/github/bartimaeusnek/bartworks/ASM/BWCoreStaticReplacementMethodes.java @@ -0,0 +1,132 @@ +/* + * Copyright (c) 2018-2020 bartimaeusnek + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package com.github.bartimaeusnek.bartworks.ASM; + +import com.github.bartimaeusnek.bartworks.util.NonNullWrappedHashSet; +import com.github.bartimaeusnek.bartworks.util.accessprioritylist.AccessPriorityList; +import com.github.bartimaeusnek.bartworks.util.accessprioritylist.AccessPriorityListNode; +import net.minecraft.inventory.InventoryCrafting; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.item.crafting.CraftingManager; +import net.minecraft.item.crafting.IRecipe; +import net.minecraft.world.World; + +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; + +public class BWCoreStaticReplacementMethodes { + private static ThreadLocal> RECENTLYUSEDRECIPES = ThreadLocal.withInitial(AccessPriorityList::new); + + public static void clearRecentlyUsedRecipes() { + // the easiest way to ensure the cache is flushed without causing synchronization overhead + // is to just replace the whole ThreadLocal instance. + RECENTLYUSEDRECIPES = ThreadLocal.withInitial(AccessPriorityList::new); + } + + @SuppressWarnings("ALL") + public static ItemStack findCachedMatchingRecipe(InventoryCrafting inventoryCrafting, World world) { + int i = 0; + ItemStack itemstack = null; + ItemStack itemstack1 = null; + int j; + + for (j = 0; j < inventoryCrafting.getSizeInventory(); ++j) + { + ItemStack itemstack2 = inventoryCrafting.getStackInSlot(j); + + if (itemstack2 != null) + { + if (i == 0) + itemstack = itemstack2; + + if (i == 1) + itemstack1 = itemstack2; + + ++i; + } + } + + if (i == 2 && itemstack.getItem() == itemstack1.getItem() && itemstack.stackSize == 1 && itemstack1.stackSize == 1 && itemstack.getItem().isRepairable()) { + Item item = itemstack.getItem(); + int j1 = item.getMaxDamage() - itemstack.getItemDamageForDisplay(); + int k = item.getMaxDamage() - itemstack1.getItemDamageForDisplay(); + int l = j1 + k + item.getMaxDamage() * 5 / 100; + int i1 = item.getMaxDamage() - l; + + if (i1 < 0) + i1 = 0; + + return new ItemStack(itemstack.getItem(), 1, i1); + + } else { + + IRecipe iPossibleRecipe = null; + AccessPriorityList cache = RECENTLYUSEDRECIPES.get(); + Iterator> it = cache.nodeIterator(); + + while (it.hasNext()) { + AccessPriorityListNode recipeNode = it.next(); + iPossibleRecipe = recipeNode.getELEMENT(); + + if (!iPossibleRecipe.matches(inventoryCrafting, world)) + continue; + + cache.addPrioToNode(recipeNode); + return iPossibleRecipe.getCraftingResult(inventoryCrafting); + } + + ItemStack stack = null; + + HashSet recipeSet = new NonNullWrappedHashSet<>(); + List recipeList = CraftingManager.getInstance().getRecipeList(); + + for (int k = 0; k < recipeList.size(); k++) { + IRecipe r = (IRecipe) recipeList.get(k); + if (r.matches(inventoryCrafting, world)) + recipeSet.add(r); + } + + Object[] arr = recipeSet.toArray(); + + if (arr.length == 0) + return null; + + IRecipe recipe = (IRecipe) arr[0]; + stack = recipe.getCraftingResult(inventoryCrafting); + + if (arr.length != 1) + return stack; + + if (stack != null) + cache.addLast(recipe); + + return stack; + } + } + + private BWCoreStaticReplacementMethodes() { + } + +} \ No newline at end of file diff --git a/src/main/java/com/github/bartimaeusnek/bartworks/ASM/BWCoreTransformer.java b/src/main/java/com/github/bartimaeusnek/bartworks/ASM/BWCoreTransformer.java new file mode 100644 index 0000000000..4ea3582bd1 --- /dev/null +++ b/src/main/java/com/github/bartimaeusnek/bartworks/ASM/BWCoreTransformer.java @@ -0,0 +1,334 @@ +/* + * Copyright (c) 2018-2020 bartimaeusnek + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package com.github.bartimaeusnek.bartworks.ASM; + +import net.minecraft.launchwrapper.IClassTransformer; +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.tree.*; + +import java.util.Arrays; +import java.util.List; + +import static org.objectweb.asm.Opcodes.*; + +public class BWCoreTransformer implements IClassTransformer { + public static final String[] DESCRIPTIONFORCONFIG = { + "REMOVING RAIN FROM LAST MILLENIUM (EXU)", + "REMVOING CREATURES FROM LAST MILLENIUM (EXU)", + "PATCHING GLOBAL RENDERER FOR USE WITH MY GALACTIC DIMS", + "PATCHING THAUMCRAFT WAND PEDESTAL TO PREVENT VIS DUPLICATION", + "PLACING MY GLASS-BLOCK RUNNABLE INTO THE GT_API", + "DUCTTAPING RWG WORLDEN FAILS", + "PATCHING CRAFTING MANAGER FOR CACHING RECIPES" + // "REMOVING 12% BONUS OUTPUTS FROM GT++ SIFTER" + }; + public static final String[] CLASSESBEEINGTRANSFORMED = { + "com.rwtema.extrautils.worldgen.endoftime.WorldProviderEndOfTime", + "com.rwtema.extrautils.worldgen.endoftime.ChunkProviderEndOfTime", + "net.minecraft.client.renderer.RenderGlobal", + "thaumcraft.common.tiles.TileWandPedestal", + "gregtech.GT_Mod", + "rwg.world.ChunkGeneratorRealistic", + "net.minecraft.item.crafting.CraftingManager" + // "gtPlusPlus.xmod.gregtech.common.tileentities.machines.multi.processing.GregtechMetaTileEntity_IndustrialSifter" + }; + static boolean obfs; + + public static boolean[] shouldTransform = new boolean[BWCoreTransformer.CLASSESBEEINGTRANSFORMED.length]; + + /** + * Made by DarkShaddow44 + */ + private static MethodNode transformThaumcraftWandPedestal(MethodNode method) { + InsnList nu = new InsnList(); + for (int j = 0; j < method.instructions.size(); j++) { + AbstractInsnNode instruction = method.instructions.get(j); + nu.add(instruction); + if (instruction.getOpcode() == INVOKEVIRTUAL) { + MethodInsnNode invokevirtual = (MethodInsnNode) instruction; + if (invokevirtual.name.equals("addVis")) { + AbstractInsnNode beginning = method.instructions.get(j - 7); + LabelNode label = new LabelNode(); + nu.insertBefore(beginning, new VarInsnNode(ALOAD, 0)); + nu.insertBefore(beginning, new FieldInsnNode(GETFIELD, "thaumcraft/common/tiles/TileWandPedestal", obfs ? "field_145850_b" : "worldObj", "Lnet/minecraft/world/World;")); + nu.insertBefore(beginning, new FieldInsnNode(GETFIELD, "net/minecraft/world/World", obfs ? "field_72995_K" : "isRemote", "Z")); + nu.insertBefore(beginning, new JumpInsnNode(IFNE, label)); + nu.add(new InsnNode(POP)); + nu.add(label); + j++; // Skip actual Pop + } + } + } + method.instructions = nu; + return method; + } + + public static byte[] transform(int id, byte[] basicClass) { + if (!BWCoreTransformer.shouldTransform[id]) { + BWCore.BWCORE_LOG.info("Patch: " + BWCoreTransformer.DESCRIPTIONFORCONFIG[id] + " is disabled, will not patch!"); + return basicClass; + } + + if (id < BWCoreTransformer.CLASSESBEEINGTRANSFORMED.length) { + BWCore.BWCORE_LOG.info(BWCoreTransformer.DESCRIPTIONFORCONFIG[id]); + ClassReader classReader = new ClassReader(basicClass); + ClassNode classNode = new ClassNode(); + classReader.accept(classNode, ClassReader.SKIP_FRAMES); + List methods = classNode.methods; + scase: + switch (id) { + case 0: { + BWCore.BWCORE_LOG.info("Could find: " + BWCoreTransformer.CLASSESBEEINGTRANSFORMED[id]); + String name_deObfs = "canDoRainSnowIce"; + + String dsc_deObfs = "(Lnet/minecraft/world/chunk/Chunk;)Z"; + String dsc_Obfs = "(Lapx;)Z"; + for (int i = 0; i < methods.size(); i++) { + if (methods.get(i).name.equalsIgnoreCase(name_deObfs)) { + BWCore.BWCORE_LOG.info("Found " + name_deObfs + "! Removing!"); + methods.remove(i); + break; + } + } + BWCore.BWCORE_LOG.info("Creating new " + name_deObfs + "!"); + MethodNode nu = new MethodNode(ACC_PUBLIC, name_deObfs, + /*obfs ? dsc_Obfs :*/ dsc_deObfs, + null, + new String[0] + ); + InsnList insnList = new InsnList(); + insnList.add(new InsnNode(ICONST_0)); + insnList.add(new InsnNode(IRETURN)); + nu.instructions = insnList; + nu.maxLocals = 1; + nu.maxStack = 1; + methods.add(nu); + break; + } + case 1: { + BWCore.BWCORE_LOG.info("Could find: " + BWCoreTransformer.CLASSESBEEINGTRANSFORMED[id]); + String name_deObfs = "getPossibleCreatures"; + String name_src = "func_73155_a"; + String name_Obfs = "a"; + String dsc_deObfs = "(Lnet/minecraft/entity/EnumCreatureType;III)Ljava/util/List;"; + String dsc_Obfs = "(Lsx;III)Ljava/util/List;"; + + for (int i = 0; i < methods.size(); i++) { + if (ASMUtils.isCorrectMethod(methods.get(i), name_deObfs, name_Obfs, name_src) && ASMUtils.isCorrectMethod(methods.get(i), dsc_deObfs, dsc_Obfs)) { + BWCore.BWCORE_LOG.info("Found " + (name_deObfs) + "! Patching!"); + MethodNode toPatch = methods.get(i); + InsnList insnList = new InsnList(); + insnList.add(new InsnNode(ACONST_NULL)); + insnList.add(new InsnNode(ARETURN)); + toPatch.instructions = insnList; + toPatch.maxStack = 1; + toPatch.maxLocals = 5; + methods.set(i, toPatch); + break scase; + } + } + } + case 2: { + String name_deObfs = "renderSky"; + String name_src = "func_72714_a"; + String name_Obfs = "a"; + String dsc_universal = "(F)V"; + String field_deObfs = "locationSunPng"; + String field_src = "field_110928_i"; + BWCore.BWCORE_LOG.info("Could find: " + BWCoreTransformer.CLASSESBEEINGTRANSFORMED[id]); + for (MethodNode toPatch : methods) { + if (ASMUtils.isCorrectMethod(toPatch, name_deObfs, name_Obfs, name_src) && ASMUtils.isCorrectMethod(toPatch, dsc_universal)) { + BWCore.BWCORE_LOG.info("Found " + (name_deObfs) + "! Patching!"); + InsnList nu = new InsnList(); + LabelNode[] LabelNodes = {new LabelNo