Applied-Energistics-2-tiler.../src/main/java/appeng/coremod/transformer/ASMIntegration.java

256 lines
7.1 KiB
Java
Raw Normal View History

/*
* 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.coremod.transformer;
import java.util.Iterator;
2015-05-09 13:06:09 +02:00
import javax.annotation.Nullable;
import org.apache.logging.log4j.Level;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.AnnotationNode;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.MethodNode;
2015-12-24 02:07:03 +01:00
import net.minecraft.launchwrapper.IClassTransformer;
import net.minecraftforge.fml.relauncher.FMLRelaunchLog;
import appeng.coremod.annotations.Integration;
import appeng.helpers.Reflected;
import appeng.integration.IntegrationRegistry;
2014-07-24 00:26:23 +02:00
import appeng.integration.IntegrationType;
@Reflected
public final class ASMIntegration implements IClassTransformer
{
@Reflected
public ASMIntegration()
{
/**
* Side, Display Name, ModID ClassPostFix
*/
2015-09-30 14:24:40 +02:00
for( final IntegrationType type : IntegrationType.values() )
2014-07-24 00:26:23 +02:00
{
IntegrationRegistry.INSTANCE.add( type );
2014-07-24 00:26:23 +02:00
}
2014-07-24 00:26:23 +02:00
// integrationModules.add( IntegrationSide.BOTH, "Thermal Expansion", "ThermalExpansion", IntegrationType.TE );
// integrationModules.add( IntegrationSide.BOTH, "Mystcraft", "Mystcraft", IntegrationType.Mystcraft );
// integrationModules.add( IntegrationSide.BOTH, "Greg Tech", "gregtech_addon", IntegrationType.GT );
// integrationModules.add( IntegrationSide.BOTH, "Universal Electricity", null, IntegrationType.UE );
// integrationModules.add( IntegrationSide.BOTH, "Logistics Pipes", "LogisticsPipes|Main", IntegrationType.LP );
// integrationModules.add( IntegrationSide.BOTH, "Better Storage", IntegrationType.betterstorage );
// integrationModules.add( IntegrationSide.BOTH, "Forestry", "Forestry", IntegrationType.Forestry );
// integrationModules.add( IntegrationSide.BOTH, "Mekanism", "Mekanism", IntegrationType.Mekanism );
}
@Nullable
@Override
2015-09-30 14:24:40 +02:00
public byte[] transform( final String name, final String transformedName, final byte[] basicClass )
{
if( basicClass == null || transformedName.startsWith( "appeng.coremod" ) )
2015-04-29 02:30:53 +02:00
{
return basicClass;
2015-04-29 02:30:53 +02:00
}
if( transformedName.startsWith( "appeng." ) )
{
2015-09-30 14:24:40 +02:00
final ClassNode classNode = new ClassNode();
final ClassReader classReader = new ClassReader( basicClass );
classReader.accept( classNode, 0 );
try
{
2015-09-30 14:24:40 +02:00
final boolean reWrite = this.removeOptionals( classNode );
if( reWrite )
{
2015-09-30 14:24:40 +02:00
final ClassWriter writer = new ClassWriter( ClassWriter.COMPUTE_MAXS );
classNode.accept( writer );
return writer.toByteArray();
}
}
2015-09-30 14:24:40 +02:00
catch( final Throwable t )
{
t.printStackTrace();
}
}
return basicClass;
}
2015-09-30 14:24:40 +02:00
private boolean removeOptionals( final ClassNode classNode )
{
boolean changed = false;
if( classNode.visibleAnnotations != null )
{
2015-09-30 14:24:40 +02:00
for( final AnnotationNode an : classNode.visibleAnnotations )
{
if( this.hasAnnotation( an, Integration.Interface.class ) )
{
if( this.stripInterface( classNode, Integration.Interface.class, an ) )
2015-04-29 02:30:53 +02:00
{
changed = true;
2015-04-29 02:30:53 +02:00
}
}
else if( this.hasAnnotation( an, Integration.InterfaceList.class ) )
{
2015-09-30 14:24:40 +02:00
for( final Object o : ( (Iterable) an.values.get( 1 ) ) )
{
if( this.stripInterface( classNode, Integration.InterfaceList.class, (AnnotationNode) o ) )
2015-04-29 02:30:53 +02:00
{
changed = true;
2015-04-29 02:30:53 +02:00
}
}
}
}
}
2015-09-30 14:24:40 +02:00
final Iterator<MethodNode> i = classNode.methods.iterator();
while( i.hasNext() )
{
2015-09-30 14:24:40 +02:00
final MethodNode mn = i.next();
if( mn.visibleAnnotations != null )
{
2015-09-30 14:24:40 +02:00
for( final AnnotationNode an : mn.visibleAnnotations )
{
if( this.hasAnnotation( an, Integration.Method.class ) )
{
if( this.stripMethod( classNode, mn, i, Integration.Method.class, an ) )
2015-04-29 02:30:53 +02:00
{
changed = true;
2015-04-29 02:30:53 +02:00
}
}
}
}
}
if( changed )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.log( "Updated " + classNode.name );
2015-04-29 02:30:53 +02:00
}
return changed;
}
2015-09-30 14:24:40 +02:00
private boolean hasAnnotation( final AnnotationNode ann, final Class<?> annotation )
{
return ann.desc.equals( Type.getDescriptor( annotation ) );
}
2015-09-30 14:24:40 +02:00
private boolean stripInterface( final ClassNode classNode, final Class<?> class1, final AnnotationNode an )
{
if( an.values.size() != 4 )
2015-04-29 02:30:53 +02:00
{
throw new IllegalArgumentException( "Unable to handle Interface annotation on " + classNode.name );
2015-04-29 02:30:53 +02:00
}
String iFace = null;
if( an.values.get( 0 ).equals( "iface" ) )
2015-04-29 02:30:53 +02:00
{
iFace = (String) an.values.get( 1 );
2015-04-29 02:30:53 +02:00
}
else if( an.values.get( 2 ).equals( "iface" ) )
2015-04-29 02:30:53 +02:00
{
iFace = (String) an.values.get( 3 );
2015-04-29 02:30:53 +02:00
}
2015-09-30 14:22:21 +02:00
String iName = null;
if( an.values.get( 0 ).equals( "iname" ) )
2015-04-29 02:30:53 +02:00
{
iName = ( (String[]) an.values.get( 1 ) )[1];
2015-04-29 02:30:53 +02:00
}
else if( an.values.get( 2 ).equals( "iname" ) )
2015-04-29 02:30:53 +02:00
{
iName = ( (String[]) an.values.get( 3 ) )[1];
2015-04-29 02:30:53 +02:00
}
if( iName != null && iFace != null )
{
final IntegrationType type = IntegrationType.valueOf( iName );
if( !IntegrationRegistry.INSTANCE.isEnabled( type ) )
{
this.log( "Removing Interface " + iFace + " from " + classNode.name + " because " + iName + " integration is disabled." );
classNode.interfaces.remove( iFace.replace( '.', '/' ) );
return true;
}
else
2015-04-29 02:30:53 +02:00
{
this.log( "Allowing Interface " + iFace + " from " + classNode.name + " because " + iName + " integration is enabled." );
2015-04-29 02:30:53 +02:00
}
}
else
2015-04-29 02:30:53 +02:00
{
throw new IllegalStateException( "Unable to handle Method annotation on " + classNode.name );
2015-04-29 02:30:53 +02:00
}
return false;
}
2015-09-30 14:24:40 +02:00
private boolean stripMethod( final ClassNode classNode, final MethodNode mn, final Iterator<MethodNode> i, final Class class1, final AnnotationNode an )
{
if( an.values.size() != 2 )
2015-04-29 02:30:53 +02:00
{
throw new IllegalArgumentException( "Unable to handle Method annotation on " + classNode.name );
2015-04-29 02:30:53 +02:00
}
String iName = null;
if( an.values.get( 0 ).equals( "iname" ) )
2015-04-29 02:30:53 +02:00
{
iName = ( (String[]) an.values.get( 1 ) )[1];
2015-04-29 02:30:53 +02:00
}
if( iName != null )
{
2015-09-30 14:24:40 +02:00
final IntegrationType type = IntegrationType.valueOf( iName );
if( !IntegrationRegistry.INSTANCE.isEnabled( type ) )
{
this.log( "Removing Method " + mn.name + " from " + classNode.name + " because " + iName + " integration is disabled." );
i.remove();
return true;
}
else
2015-04-29 02:30:53 +02:00
{
this.log( "Allowing Method " + mn.name + " from " + classNode.name + " because " + iName + " integration is enabled." );
2015-04-29 02:30:53 +02:00
}
}
else
2015-04-29 02:30:53 +02:00
{
throw new IllegalStateException( "Unable to handle Method annotation on " + classNode.name );
2015-04-29 02:30:53 +02:00
}
return false;
}
2015-09-30 14:24:40 +02:00
private void log( final String string )
{
FMLRelaunchLog.log( "AE2-CORE", Level.INFO, string );
}
}