From f453fd197666298ba702266201ca7dcabb50ee14 Mon Sep 17 00:00:00 2001 From: xsun <1563770452@qq.com> Date: Tue, 7 Mar 2017 21:04:06 +0800 Subject: [PATCH] fix the bug that may crash the game when there is a mod including a old AE2 api class bytecode, such as TT, completely. --- .../java/appeng/transformer/AppEngCore.java | 2 +- .../appeng/transformer/asm/ASMApiFixer.java | 86 +++++++++++++++++++ 2 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 src/main/java/appeng/transformer/asm/ASMApiFixer.java diff --git a/src/main/java/appeng/transformer/AppEngCore.java b/src/main/java/appeng/transformer/AppEngCore.java index 5a07faff..b561417f 100644 --- a/src/main/java/appeng/transformer/AppEngCore.java +++ b/src/main/java/appeng/transformer/AppEngCore.java @@ -64,7 +64,7 @@ public final class AppEngCore extends DummyModContainer implements IFMLLoadingPl @Override public String[] getASMTransformerClass() { - return new String[] { "appeng.transformer.asm.ASMIntegration" }; + return new String[] { "appeng.transformer.asm.ASMIntegration", "appeng.transformer.asm.ASMApiFixer" }; } @Override diff --git a/src/main/java/appeng/transformer/asm/ASMApiFixer.java b/src/main/java/appeng/transformer/asm/ASMApiFixer.java new file mode 100644 index 00000000..fa0700a0 --- /dev/null +++ b/src/main/java/appeng/transformer/asm/ASMApiFixer.java @@ -0,0 +1,86 @@ +/* + * This file is part of Applied Energistics 2. + * Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved. + * + * Applied Energistics 2 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. + * + * Applied Energistics 2 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Applied Energistics 2. If not, see . + */ + +package appeng.transformer.asm; + + +import appeng.helpers.Reflected; +import cpw.mods.fml.relauncher.FMLRelaunchLog; +import net.minecraft.launchwrapper.IClassTransformer; +import org.apache.commons.io.FileUtils; +import org.apache.logging.log4j.Level; + +import java.io.BufferedInputStream; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.JarURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; + + +/* + * It is a ClassTransformer which can transformer the older AE2 api class that some addons including, + * which can occur the crash due to java.lang.NoSuchMethodError. + * See also : https://github.com/xsun2001/Applied-Energistics-2-Unofficial/issues/1 + */ +@Reflected +public class ASMApiFixer implements IClassTransformer +{ + + public ASMApiFixer() + { + FMLRelaunchLog.log( "AE2-ApiFixer", Level.INFO, "ApiFixer Installed" ); + FMLRelaunchLog.log( "AE2-ApiFixer", Level.INFO, "%s", getClass().getResource( "" ).toString() ); + } + + @Override public byte[] transform( String name, String transformedName, byte[] basicClass ) + { + if( transformedName.startsWith( "appeng.api" ) ) + { + FMLRelaunchLog.log( "AE2-ApiFixer", Level.INFO, "Fixing api class file:%s", transformedName ); + try + { + String clazzurl = getClass().getResource( "" ).toString(); + clazzurl = clazzurl.substring( 0, clazzurl.length() - 23 ) + transformedName.replace( '.', '/' ) + ".class"; + //23 is "appeng/transformer/asm"'s length + 1 + FMLRelaunchLog.log( "AE2-ApiFixer", Level.INFO, "Fixing...:%s", clazzurl ); + URL url = new URL( clazzurl ); + URLConnection connection = url.openConnection(); + byte[] bytes = new byte[connection.getContentLength()]; + if( connection.getInputStream().read( bytes ) == -1 ) + { + FMLRelaunchLog.log( "AE2-ApiFixer", Level.ERROR, "Cannot fix:%s", transformedName ); + return basicClass; + } + else + { + FMLRelaunchLog.log( "AE2-ApiFixer", Level.INFO, "Fix success:%s", transformedName ); + return bytes; + } + } + catch( Exception e ) + { + FMLRelaunchLog.log( "AE2-ApiFixer", Level.ERROR, "Fix failed:%s|%s", transformedName, e.getClass().getName() ); + return basicClass; + } + } + return basicClass; + } +}