forked from AppliedEnergistics/Applied-Energistics-2
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix the bug that may crash the game when there is a mod including a o…
…ld AE2 api class bytecode, such as TT, completely.
- Loading branch information
xsun
committed
Mar 7, 2017
1 parent
e1fa5c5
commit f453fd1
Showing
2 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <http://www.gnu.org/licenses/lgpl>. | ||
*/ | ||
|
||
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; | ||
} | ||
} |