Created
March 26, 2015 00:10
-
-
Save Ivorforce/bdf2c8bf629524b82864 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
* Copyright (c) 2014, Lukas Tenbrink. | |
* * http://lukas.axxim.net | |
*/ | |
package ivorius.reccomplex.worldgen.villages; | |
import com.google.common.collect.BiMap; | |
import com.google.common.collect.HashBiMap; | |
import org.objectweb.asm.ClassWriter; | |
import org.objectweb.asm.MethodVisitor; | |
import org.objectweb.asm.Type; | |
import java.util.HashSet; | |
import java.util.Set; | |
import static org.objectweb.asm.Opcodes.*; | |
/** | |
* Created by lukas on 26.03.15. | |
*/ | |
public class VanillaGenerationClassFactory extends ClassLoader | |
{ | |
private static final VanillaGenerationClassFactory INSTANCE = new VanillaGenerationClassFactory(); | |
protected Set<String> failed = new HashSet<>(); | |
public static VanillaGenerationClassFactory instance() | |
{ | |
return INSTANCE; | |
} | |
public Class<?> define(String name, byte[] data) | |
{ | |
return defineClass(name, data, 0, data.length); | |
} | |
public Class<? extends GenericVillagePiece> getClass(String structureID) | |
{ | |
try | |
{ | |
return (Class<? extends GenericVillagePiece>) findClass(classNameForStructure(structureID)); | |
} | |
catch (ClassNotFoundException ignored) | |
{ | |
} | |
if (!failed.contains(structureID)) | |
{ | |
Class<? extends GenericVillagePiece> created = createClass(structureID); | |
if (created == null) | |
failed.add(structureID); | |
return created; | |
} | |
return null; | |
} | |
protected Class<? extends GenericVillagePiece> createClass(String structureID) | |
{ | |
try | |
{ | |
return (Class<? extends GenericVillagePiece>) define(classNameForStructure(structureID), createClassBinary(structureID)); | |
} | |
catch (Exception ex) | |
{ | |
return null; | |
} | |
} | |
protected String classNameForStructure(String structureID) | |
{ | |
return "ivorius/reccomplex/dynamic/vanillagen/" + structureID; | |
} | |
protected byte[] createClassBinary(String className) | |
{ | |
ClassWriter cw = new ClassWriter(0); | |
cw.visit(V1_5, ACC_PUBLIC, className, null, Type.getInternalName(GenericVillagePiece.class), null); | |
{ | |
// constructor | |
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); | |
mv.visitMaxs(2, 1); | |
mv.visitVarInsn(ALOAD, 0); | |
mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(Object.class), "<init>", "()V", false); | |
mv.visitInsn(RETURN); | |
mv.visitEnd(); | |
} | |
{ | |
// public Object get() { return new Object(); } | |
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "get", "()Ljava/lang/Object;", null, null); | |
mv.visitMaxs(2, 1); | |
mv.visitTypeInsn(NEW, Type.getInternalName(Object.class)); | |
mv.visitInsn(DUP); | |
mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(Object.class), "<init>", "()V", false); | |
mv.visitInsn(ARETURN); | |
mv.visitEnd(); | |
} | |
cw.visitEnd(); | |
return cw.toByteArray(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment