package openmods.asm;

import net.minecraft.launchwrapper.IClassTransformer;

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;

public class OpenModsClassTransformer implements IClassTransformer {

	public static interface TransformProvider {
		public ClassVisitor createVisitor(ClassVisitor cv);
	}

	private final static TransformProvider INCLUDING_CV = new TransformProvider() {

		@Override
		public ClassVisitor createVisitor(ClassVisitor cv) {
			return new IncludingClassVisitor(cv);
		}
	};

	public static byte[] applyVisitor(byte[] bytes, int flags, TransformProvider context) {
		ClassReader cr = new ClassReader(bytes);
		ClassWriter cw = new ClassWriter(cr, flags);
		ClassVisitor mod = context.createVisitor(cw);

		try {
			cr.accept(mod, 0);
			return cw.toByteArray();
		} catch (StopTransforming e) {
			return bytes;
		}
	}

	@Override
	public byte[] transform(final String name, String transformedName, byte[] bytes) {
		if (name.startsWith("openmods.asm")) return bytes;
		// / no need for COMPUTE_FRAMES, we can handle simple stuff
		return applyVisitor(bytes, 0, INCLUDING_CV);
	}
}

@@ -187,16 +187,62 @@
 riter;%0A%0A
+import com.google.common.base.Preconditions;%0A%0A
 public c
@@ -694,16 +694,53 @@
 text) %7B%0A
+%09%09Preconditions.checkNotNull(bytes);%0A
 %09%09ClassR
@@ -1087,16 +1087,33 @@
 %7B%0A%09%09if (
+bytes == null %7C%7C 
 name.sta
@@ -1135,16 +1135,53 @@
 ds.asm%22)
+ %7C%7C name.startsWith(%22net.minecraft.%22)
 ) return

Fix crash on missing classes (it will crash later, but nobody will blame us