2013-03-08 00:25:48 +00:00
|
|
|
/*
|
2016-06-23 03:49:47 +00:00
|
|
|
* Minecraft Forge
|
|
|
|
* Copyright (c) 2016.
|
2013-06-14 21:21:49 +00:00
|
|
|
*
|
2016-06-23 03:49:47 +00:00
|
|
|
* This library 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 version 2.1
|
|
|
|
* of the License.
|
|
|
|
*
|
|
|
|
* This library 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 this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2013-03-08 00:25:48 +00:00
|
|
|
*/
|
|
|
|
|
2014-09-23 05:01:24 +00:00
|
|
|
package net.minecraftforge.fml.common.asm.transformers;
|
2012-08-09 01:53:41 +00:00
|
|
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
|
2013-06-15 00:45:52 +00:00
|
|
|
import net.minecraft.launchwrapper.IClassTransformer;
|
2014-09-23 05:01:24 +00:00
|
|
|
import net.minecraftforge.fml.relauncher.FMLLaunchHandler;
|
|
|
|
import net.minecraftforge.fml.relauncher.SideOnly;
|
2013-06-15 00:45:52 +00:00
|
|
|
|
2012-08-09 01:53:41 +00:00
|
|
|
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.FieldNode;
|
|
|
|
import org.objectweb.asm.tree.MethodNode;
|
|
|
|
|
|
|
|
public class SideTransformer implements IClassTransformer
|
|
|
|
{
|
2013-06-14 21:21:49 +00:00
|
|
|
private static String SIDE = FMLLaunchHandler.side().name();
|
2012-08-12 08:23:10 +00:00
|
|
|
private static final boolean DEBUG = false;
|
2012-08-09 01:53:41 +00:00
|
|
|
@Override
|
2013-01-27 20:55:37 +00:00
|
|
|
public byte[] transform(String name, String transformedName, byte[] bytes)
|
2012-08-09 01:53:41 +00:00
|
|
|
{
|
2015-05-17 14:08:46 +00:00
|
|
|
if (bytes == null) { return null; }
|
2012-10-23 20:32:02 +00:00
|
|
|
|
2012-08-09 01:53:41 +00:00
|
|
|
ClassNode classNode = new ClassNode();
|
|
|
|
ClassReader classReader = new ClassReader(bytes);
|
|
|
|
classReader.accept(classNode, 0);
|
|
|
|
|
2016-03-23 14:34:48 +00:00
|
|
|
if (remove(classNode.visibleAnnotations, SIDE))
|
2012-08-20 05:26:52 +00:00
|
|
|
{
|
|
|
|
if (DEBUG)
|
|
|
|
{
|
|
|
|
System.out.println(String.format("Attempted to load class %s for invalid side %s", classNode.name, SIDE));
|
|
|
|
}
|
2012-10-23 20:32:02 +00:00
|
|
|
throw new RuntimeException(String.format("Attempted to load class %s for invalid side %s", classNode.name, SIDE));
|
2012-08-20 05:26:52 +00:00
|
|
|
}
|
|
|
|
|
2012-08-09 01:53:41 +00:00
|
|
|
Iterator<FieldNode> fields = classNode.fields.iterator();
|
|
|
|
while(fields.hasNext())
|
|
|
|
{
|
|
|
|
FieldNode field = fields.next();
|
2016-03-23 14:34:48 +00:00
|
|
|
if (remove(field.visibleAnnotations, SIDE))
|
2012-08-09 01:53:41 +00:00
|
|
|
{
|
2012-08-12 08:23:10 +00:00
|
|
|
if (DEBUG)
|
|
|
|
{
|
|
|
|
System.out.println(String.format("Removing Field: %s.%s", classNode.name, field.name));
|
|
|
|
}
|
2012-08-09 01:53:41 +00:00
|
|
|
fields.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Iterator<MethodNode> methods = classNode.methods.iterator();
|
|
|
|
while(methods.hasNext())
|
|
|
|
{
|
|
|
|
MethodNode method = methods.next();
|
2016-03-23 14:34:48 +00:00
|
|
|
if (remove(method.visibleAnnotations, SIDE))
|
2012-08-09 01:53:41 +00:00
|
|
|
{
|
2012-08-12 08:23:10 +00:00
|
|
|
if (DEBUG)
|
|
|
|
{
|
|
|
|
System.out.println(String.format("Removing Method: %s.%s%s", classNode.name, method.name, method.desc));
|
|
|
|
}
|
2012-08-09 01:53:41 +00:00
|
|
|
methods.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
|
|
|
|
classNode.accept(writer);
|
|
|
|
return writer.toByteArray();
|
|
|
|
}
|
2013-01-27 20:55:37 +00:00
|
|
|
|
2012-08-09 01:53:41 +00:00
|
|
|
private boolean remove(List<AnnotationNode> anns, String side)
|
|
|
|
{
|
|
|
|
if (anns == null)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (AnnotationNode ann : anns)
|
|
|
|
{
|
|
|
|
if (ann.desc.equals(Type.getDescriptor(SideOnly.class)))
|
|
|
|
{
|
|
|
|
if (ann.values != null)
|
|
|
|
{
|
|
|
|
for (int x = 0; x < ann.values.size() - 1; x += 2)
|
|
|
|
{
|
|
|
|
Object key = ann.values.get(x);
|
|
|
|
Object value = ann.values.get(x+1);
|
|
|
|
if (key instanceof String && key.equals("value"))
|
|
|
|
{
|
|
|
|
if (value instanceof String[] )
|
|
|
|
{
|
|
|
|
if (!((String[])value)[1].equals(side))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|