package vorquel.mod.simpleskygrid.asm;

import net.minecraft.launchwrapper.IClassTransformer;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;

import static vorquel.mod.simpleskygrid.asm.WorldProviderAdapter.cWorldProvider;

@SuppressWarnings("unused")
public class Transformer implements IClassTransformer {

    private String worldProvider = WorldProviderAdapter.cWorldProvider.replace('/', '.');

    @Override
    public byte[] transform(String name, String transformedName, byte[] clazz) {
        ClassReader cr = new ClassReader(clazz);
        if(transformedName.equals(worldProvider) || cr.getSuperName().equals(cWorldProvider)) {
            ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_FRAMES);
            WorldProviderAdapter wpa = new WorldProviderAdapter(cw);
            cr.accept(wpa, 0);
            return cw.toByteArray();
        } else
            return clazz;
    }
}

@@ -167,23 +167,15 @@
 er;%0A
-%0A
 import 
-static 
 vorq
@@ -200,47 +200,138 @@
 rid.
-asm.WorldProviderAdapter.cWorldProvider
+helper.Log;%0A%0Aimport java.io.ByteArrayInputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.util.HashMap
 ;%0A%0A@
@@ -503,16 +503,173 @@
 ', '.');
+%0A    private HashMap%3CString, Boolean%3E instanceOfWorldProviderMap = new HashMap%3C%3E();%0A%0A    %7B%0A        instanceOfWorldProviderMap.put(worldProvider, true);%0A    %7D
 %0A%0A    @O
@@ -751,18 +751,22 @@
 te%5B%5D cla
-zz
+ssData
 ) %7B%0A    
@@ -773,144 +773,150 @@
     
-ClassReader cr = new ClassReader(clazz);%0A        if(transformedName.equals(worldProvider) %7C%7C cr.getSuperName().equals(cWorldProvider)) %7B
+if(isInstanceOfWorldProvider(transformedName, new ByteArrayInputStream(classData))) %7B%0A            ClassReader cr = new ClassReader(classData);
 %0A   
@@ -1168,10 +1168,944 @@
  cla
-zz
+ssData;%0A    %7D%0A%0A    private boolean isInstanceOfWorldProvider(String className, InputStream classStream) %7B%0A        if(instanceOfWorldProviderMap.containsKey(className))%0A            return instanceOfWorldProviderMap.get(className);%0A        boolean isInstanceOfWorldProvider = false;%0A        try %7B%0A            String superClassName = new ClassReader(classStream).getSuperName();%0A            if(superClassName == null) return false;%0A            InputStream superClassStream = this.getClass().getClassLoader().getResourceAsStream(superClassName + %22.class%22);%0A            isInstanceOfWorldProvider = isInstanceOfWorldProvider(superClassName.replace('/', '.'), superClassStream);%0A        %7D catch (IOException e) %7B%0A            Log.error(%22Exception determining class hierarchy for %25s: %25s%22, className, e.getMessage());%0A        %7D%0A        instanceOfWorldProviderMap.put(className, isInstanceOfWorldProvider);%0A        return isInstanceOfWorldProvider
 ;%0A  

Improve WorldProvider class detection