Applied-Energistics-2-tiler.../src/main/java/appeng/core/api/ApiPart.java

358 lines
9.6 KiB
Java
Raw Normal View History

2014-11-14 12:02:52 +01:00
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2015, AlgorithmX2, All rights reserved.
2014-11-14 12:02:52 +01:00
*
* 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>.
*/
2014-09-24 02:26:27 +02:00
package appeng.core.api;
import java.io.IOException;
2014-09-24 02:26:27 +02:00
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
2014-09-24 02:26:27 +02:00
2015-12-24 02:07:03 +01:00
import com.google.common.base.Joiner;
2014-09-24 02:26:27 +02:00
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.commons.Remapper;
import org.objectweb.asm.commons.RemappingClassAdapter;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.MethodNode;
2015-12-24 02:07:03 +01:00
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumActionResult;
2015-12-24 02:07:03 +01:00
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
2015-12-24 02:07:03 +01:00
import net.minecraft.world.World;
2014-09-24 02:26:27 +02:00
import appeng.api.parts.CableRenderMode;
import appeng.api.parts.IPartHelper;
import appeng.api.parts.LayerBase;
import appeng.core.AELog;
import appeng.core.CommonHelper;
import appeng.integration.IntegrationRegistry;
2014-09-24 02:26:27 +02:00
import appeng.integration.IntegrationType;
import appeng.integration.abstraction.IFMP;
import appeng.parts.PartPlacement;
import appeng.tile.networking.TileCableBus;
2014-09-24 02:26:27 +02:00
public class ApiPart implements IPartHelper
{
2014-10-05 15:25:36 +02:00
private final Map<String, Class> tileImplementations = new HashMap<String, Class>();
private final Map<Class<?>, String> interfaces2Layer = new HashMap<Class<?>, String>();
2014-10-05 15:25:09 +02:00
private final Map<String, Class> roots = new HashMap<String, Class>();
private final List<String> desc = new LinkedList<String>();
2014-09-24 02:26:27 +02:00
public void initFMPSupport()
{
2015-09-30 14:24:40 +02:00
for( final Class layerInterface : this.interfaces2Layer.keySet() )
2014-09-24 02:26:27 +02:00
{
if( IntegrationRegistry.INSTANCE.isEnabled( IntegrationType.FMP ) )
2015-04-29 02:30:53 +02:00
{
( (IFMP) IntegrationRegistry.INSTANCE.getInstance( IntegrationType.FMP ) ).registerPassThrough( layerInterface );
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
}
}
2015-09-30 14:24:40 +02:00
public Class getCombinedInstance( final String base )
2014-09-24 02:26:27 +02:00
{
2015-09-25 23:18:27 +02:00
if( this.desc.isEmpty() )
2014-09-24 02:26:27 +02:00
{
try
{
return Class.forName( base );
}
2015-09-30 14:24:40 +02:00
catch( final ClassNotFoundException e )
2014-09-24 02:26:27 +02:00
{
throw new IllegalStateException( e );
2014-09-24 02:26:27 +02:00
}
}
2015-09-30 14:24:40 +02:00
final String description = base + ':' + Joiner.on( ";" ).skipNulls().join( this.desc.iterator() );
2014-09-24 02:26:27 +02:00
if( this.tileImplementations.get( description ) != null )
2014-09-24 02:26:27 +02:00
{
return this.tileImplementations.get( description );
2014-09-24 02:26:27 +02:00
}
String f = base;// TileCableBus.class.getName();
String Addendum = "";
try
{
Addendum = Class.forName( base ).getSimpleName();
}
2015-09-30 14:24:40 +02:00
catch( final ClassNotFoundException e )
2014-09-24 02:26:27 +02:00
{
AELog.debug( e );
2014-09-24 02:26:27 +02:00
}
Class myCLass;
try
{
myCLass = Class.forName( f );
}
2015-09-30 14:24:40 +02:00
catch( final ClassNotFoundException e )
2014-09-24 02:26:27 +02:00
{
throw new IllegalStateException( e );
2014-09-24 02:26:27 +02:00
}
String path = f;
2015-09-30 14:24:40 +02:00
for( final String name : this.desc )
2014-09-24 02:26:27 +02:00
{
try
{
2015-09-30 14:24:40 +02:00
final String newPath = path + ';' + name;
2014-12-29 15:13:47 +01:00
myCLass = this.getClassByDesc( Addendum, newPath, f, this.interfaces2Layer.get( Class.forName( name ) ) );
2014-09-24 02:26:27 +02:00
path = newPath;
}
2015-09-30 14:24:40 +02:00
catch( final Throwable t )
2014-09-24 02:26:27 +02:00
{
AELog.warn( "Error loading " + name );
AELog.debug( t );
2014-09-24 02:26:27 +02:00
// throw new RuntimeException( t );
}
f = myCLass.getName();
}
2014-12-29 15:13:47 +01:00
this.tileImplementations.put( description, myCLass );
2014-09-24 02:26:27 +02:00
return myCLass;
2014-09-24 02:26:27 +02:00
}
private Class getClassByDesc( final String addendum, final String fullPath, final String root, final String next )
2014-09-24 02:26:27 +02:00
{
if( this.roots.get( fullPath ) != null )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
return this.roots.get( fullPath );
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
2015-09-30 14:24:40 +02:00
final ClassWriter cw = new ClassWriter( ClassWriter.COMPUTE_MAXS );
final ClassNode n = this.getReader( next );
final String originalName = n.name;
2014-09-24 02:26:27 +02:00
try
{
n.name = n.name + '_' + addendum;
2014-09-24 02:26:27 +02:00
n.superName = Class.forName( root ).getName().replace( ".", "/" );
}
2015-09-30 14:24:40 +02:00
catch( final Throwable t )
2014-09-24 02:26:27 +02:00
{
AELog.debug( t );
2014-09-24 02:26:27 +02:00
}
2015-09-30 14:24:40 +02:00
for( final MethodNode mn : n.methods )
2014-09-24 02:26:27 +02:00
{
2015-09-30 14:24:40 +02:00
final Iterator<AbstractInsnNode> i = mn.instructions.iterator();
while( i.hasNext() )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.processNode( i.next(), n.superName );
2014-09-24 02:26:27 +02:00
}
}
2015-09-30 14:24:40 +02:00
final DefaultPackageClassNameRemapper remapper = new DefaultPackageClassNameRemapper();
2014-09-24 02:26:27 +02:00
remapper.inputOutput.put( "appeng/api/parts/LayerBase", n.superName );
remapper.inputOutput.put( originalName, n.name );
n.accept( new RemappingClassAdapter( cw, remapper ) );
// n.accept( cw );
// n.accept( new TraceClassVisitor( new PrintWriter( System.out ) ) );
2015-09-30 14:24:40 +02:00
final byte[] byteArray = cw.toByteArray();
final int size = byteArray.length;
final Class clazz = this.loadClass( n.name.replace( "/", "." ), byteArray );
2014-09-24 02:26:27 +02:00
try
{
2015-09-30 14:24:40 +02:00
final Object fish = clazz.newInstance();
final Class rootC = Class.forName( root );
2014-09-24 02:26:27 +02:00
boolean hasError = false;
2014-09-24 02:26:27 +02:00
if( !rootC.isInstance( fish ) )
2014-09-24 02:26:27 +02:00
{
hasError = true;
AELog.error( "Error, Expected layer to implement " + root + " did not." );
2014-09-24 02:26:27 +02:00
}
if( fish instanceof LayerBase )
2014-09-24 02:26:27 +02:00
{
hasError = true;
AELog.error( "Error, Expected layer to NOT implement LayerBase but it DID." );
2014-09-24 02:26:27 +02:00
}
if( !fullPath.contains( ".fmp." ) )
2014-09-24 02:26:27 +02:00
{
if( !( fish instanceof TileCableBus ) )
2014-09-24 02:26:27 +02:00
{
hasError = true;
AELog.error( "Error, Expected layer to implement TileCableBus did not." );
2014-09-24 02:26:27 +02:00
}
if( !( fish instanceof TileEntity ) )
2014-09-24 02:26:27 +02:00
{
hasError = true;
AELog.error( "Error, Expected layer to implement TileEntity did not." );
2014-09-24 02:26:27 +02:00
}
}
if( !hasError )
2014-09-24 02:26:27 +02:00
{
AELog.info( "Layer: " + n.name + " loaded successfully - " + size + " bytes" );
}
}
2015-09-30 14:24:40 +02:00
catch( final Throwable t )
2014-09-24 02:26:27 +02:00
{
AELog.error( "Layer: " + n.name + " Failed." );
AELog.debug( t );
2014-09-24 02:26:27 +02:00
}
2014-12-29 15:13:47 +01:00
this.roots.put( fullPath, clazz );
return clazz;
2014-09-24 02:26:27 +02:00
}
private ClassNode getReader( final String name )
{
2015-09-30 14:24:40 +02:00
final String path = '/' + name.replace( ".", "/" ) + ".class";
final InputStream is = this.getClass().getResourceAsStream( path );
try
{
2015-09-30 14:24:40 +02:00
final ClassReader cr = new ClassReader( is );
2015-09-30 14:24:40 +02:00
final ClassNode cn = new ClassNode();
cr.accept( cn, ClassReader.EXPAND_FRAMES );
return cn;
}
2015-09-30 14:24:40 +02:00
catch( final IOException e )
{
throw new IllegalStateException( "Error loading " + name, e );
}
}
2015-09-30 14:24:40 +02:00
private void processNode( final AbstractInsnNode next, final String nePar )
2014-09-24 02:26:27 +02:00
{
if( next instanceof MethodInsnNode )
2014-09-24 02:26:27 +02:00
{
2015-09-30 14:24:40 +02:00
final MethodInsnNode min = (MethodInsnNode) next;
if( min.owner.equals( "appeng/api/parts/LayerBase" ) )
2014-09-24 02:26:27 +02:00
{
min.owner = nePar;
}
}
}
2015-09-30 14:24:40 +02:00
private Class loadClass( final String name, byte[] b )
2014-09-24 02:26:27 +02:00
{
// override classDefine (as it is protected) and define the class.
Class clazz = null;
try
{
2015-09-30 14:24:40 +02:00
final ClassLoader loader = this.getClass().getClassLoader();// ClassLoader.getSystemClassLoader();
final Class<ClassLoader> root = ClassLoader.class;
final Class<? extends ClassLoader> cls = loader.getClass();
final Method defineClassMethod = root.getDeclaredMethod( "defineClass", String.class, byte[].class, int.class, int.class );
final Method runTransformersMethod = cls.getDeclaredMethod( "runTransformers", String.class, String.class, byte[].class );
2014-09-24 02:26:27 +02:00
runTransformersMethod.setAccessible( true );
defineClassMethod.setAccessible( true );
try
{
2015-09-30 14:24:40 +02:00
final Object[] argsA = { name, name, b };
b = (byte[]) runTransformersMethod.invoke( loader, argsA );
2015-09-30 14:24:40 +02:00
final Object[] args = { name, b, 0, b.length };
clazz = (Class) defineClassMethod.invoke( loader, args );
}
finally
{
runTransformersMethod.setAccessible( false );
defineClassMethod.setAccessible( false );
}
}
2015-09-30 14:24:40 +02:00
catch( final Exception e )
{
AELog.debug( e );
throw new IllegalStateException( "Unable to manage part API.", e );
}
return clazz;
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public boolean registerNewLayer( final String layer, final String layerInterface )
2014-09-24 02:26:27 +02:00
{
try
{
final Class<?> layerInterfaceClass = Class.forName( layerInterface );
if( this.interfaces2Layer.get( layerInterfaceClass ) == null )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.interfaces2Layer.put( layerInterfaceClass, layer );
this.desc.add( layerInterface );
2014-09-24 02:26:27 +02:00
return true;
}
else
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
AELog.info( "Layer " + layer + " not registered, " + layerInterface + " already has a layer." );
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
}
2015-09-30 14:24:40 +02:00
catch( final Throwable ignored )
2014-09-24 02:26:27 +02:00
{
}
return false;
}
@Override
public EnumActionResult placeBus( final ItemStack is, final BlockPos pos, final EnumFacing side, final EntityPlayer player, final EnumHand hand, final World w )
{
return PartPlacement.place( is, pos, side, player, hand, w, PartPlacement.PlaceType.PLACE_ITEM, 0 );
}
2014-09-24 02:26:27 +02:00
@Override
public CableRenderMode getCableRenderMode()
{
return CommonHelper.proxy.getRenderMode();
}
private static class DefaultPackageClassNameRemapper extends Remapper
{
private final HashMap<String, String> inputOutput = new HashMap<String, String>();
@Override
2015-09-30 14:24:40 +02:00
public String map( final String typeName )
{
2015-09-30 14:24:40 +02:00
final String o = this.inputOutput.get( typeName );
if( o == null )
2015-04-29 02:30:53 +02:00
{
return typeName;
2015-04-29 02:30:53 +02:00
}
return o;
}
}
2014-09-24 02:26:27 +02:00
}