    001    package cpw.mods.fml.common.asm;
    002
    003    import java.util.Map;
    004
    005    import javax.swing.JOptionPane;
    006
    007    import org.objectweb.asm.ClassReader;
    008    import org.objectweb.asm.ClassVisitor;
    009    import org.objectweb.asm.FieldVisitor;
    010    import org.objectweb.asm.Opcodes;
    011
    012    import cpw.mods.fml.relauncher.IFMLCallHook;
    013    import cpw.mods.fml.relauncher.RelaunchClassLoader;
    014
    015    public class FMLSanityChecker implements IFMLCallHook
    016    {
    017        static class MLDetectorClassVisitor extends ClassVisitor
    018        {
    019            private boolean foundMarker = false;
    020            private MLDetectorClassVisitor()
    021            {
    022                super(Opcodes.ASM4);
    023            }
    024
    025            @Override
    026            public FieldVisitor visitField(int arg0, String arg1, String arg2, String arg3, Object arg4)
    027            {
    028                if ("fmlMarker".equals(arg1))
    029                {
    030                    foundMarker = true;
    031                }
    032                return null;
    033            }
    034        }
    035
    036        private RelaunchClassLoader cl;
    037
    038        @Override
    039        public Void call() throws Exception
    040        {
    041            byte[] mlClass = cl.getClassBytes("ModLoader");
    042            // Only care in obfuscated env
    043            if (mlClass == null)
    044            {
    045                return null;
    046            }
    047            MLDetectorClassVisitor mlTester = new MLDetectorClassVisitor();
    048            ClassReader cr = new ClassReader(mlClass);
    049            cr.accept(mlTester, ClassReader.SKIP_CODE);
    050            if (!mlTester.foundMarker)
    051            {
    052                JOptionPane.showMessageDialog(null, "<html>CRITICAL ERROR<br/>" +
    053                        "ModLoader was detected in this environment<br/>" +
    054                            "ForgeModLoader cannot be installed alongside ModLoader<br/>" +
    055                            "All mods should work without ModLoader being installed<br/>" +
    056                            "Because ForgeModLoader is 100% compatible with ModLoader<br/>" +
    057                            "Re-install Minecraft Forge or Forge ModLoader into a clean<br/>" +
    058                            "jar and try again.",
    059                            "ForgeModLoader critical error",
    060                            JOptionPane.ERROR_MESSAGE);
    061                throw new RuntimeException("Invalid ModLoader class detected");
    062            }
    063            return null;
    064        }
    065
    066        @Override
    067        public void injectData(Map<String, Object> data)
    068        {
    069            cl = (RelaunchClassLoader) data.get("classLoader");
    070        }
    071
    072    }