Search in sources :

Example 6 with ASMData

use of net.minecraftforge.fml.common.discovery.ASMDataTable.ASMData in project MinecraftForge by MinecraftForge.

the class ConfigManager method load.

public static void load(String modid, Config.Type type) {
    FMLLog.fine("Attempting to inject @Config classes into %s for type %s", modid, type);
    ClassLoader mcl = Loader.instance().getModClassLoader();
    File configDir = Loader.instance().getConfigDir();
    Multimap<Config.Type, ASMData> map = asm_data.get(modid);
    if (map == null)
        return;
    for (ASMData targ : map.get(type)) {
        try {
            Class<?> cls = Class.forName(targ.getClassName(), true, mcl);
            String name = (String) targ.getAnnotationInfo().get("name");
            if (name == null)
                name = modid;
            String category = (String) targ.getAnnotationInfo().get("category");
            if (category == null)
                category = "general";
            File file = new File(configDir, name + ".cfg");
            Configuration cfg = CONFIGS.get(file.getAbsolutePath());
            if (cfg == null) {
                cfg = new Configuration(file);
                cfg.load();
                CONFIGS.put(file.getAbsolutePath(), cfg);
            }
            createConfig(cfg, cls, modid, type == Config.Type.INSTANCE, category);
            cfg.save();
        } catch (Exception e) {
            FMLLog.log(Level.ERROR, e, "An error occurred trying to load a config for %s into %s", modid, targ.getClassName());
            throw new LoaderException(e);
        }
    }
}
Also used : ASMData(net.minecraftforge.fml.common.discovery.ASMDataTable.ASMData) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) LoaderException(net.minecraftforge.fml.common.LoaderException) File(java.io.File) LoaderException(net.minecraftforge.fml.common.LoaderException)

Example 7 with ASMData

use of net.minecraftforge.fml.common.discovery.ASMDataTable.ASMData in project MinecraftForge by MinecraftForge.

the class ConfigManager method loadData.

public static void loadData(ASMDataTable data) {
    FMLLog.fine("Loading @Config anotation data");
    for (ASMData target : data.getAll(Config.class.getName())) {
        String modid = (String) target.getAnnotationInfo().get("modid");
        Multimap<Config.Type, ASMData> map = asm_data.get(modid);
        if (map == null) {
            map = ArrayListMultimap.create();
            asm_data.put(modid, map);
        }
        EnumHolder tholder = (EnumHolder) target.getAnnotationInfo().get("type");
        Config.Type type = tholder == null ? Config.Type.INSTANCE : Config.Type.valueOf(tholder.getValue());
        map.put(type, target);
    }
}
Also used : ASMData(net.minecraftforge.fml.common.discovery.ASMDataTable.ASMData) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) EnumHolder(net.minecraftforge.fml.common.discovery.asm.ModAnnotation.EnumHolder)

Example 8 with ASMData

use of net.minecraftforge.fml.common.discovery.ASMDataTable.ASMData in project MinecraftForge by MinecraftForge.

the class FMLModContainer method parseSimpleFieldAnnotation.

private void parseSimpleFieldAnnotation(SetMultimap<String, ASMData> annotations, String annotationClassName, Function<ModContainer, Object> retriever) throws IllegalAccessException {
    Set<ASMDataTable.ASMData> mods = annotations.get(Mod.class.getName());
    String[] annName = annotationClassName.split("\\.");
    String annotationName = annName[annName.length - 1];
    for (ASMData targets : annotations.get(annotationClassName)) {
        String targetMod = (String) targets.getAnnotationInfo().get("value");
        String owner = (String) targets.getAnnotationInfo().get("owner");
        if (Strings.isNullOrEmpty(owner)) {
            owner = ASMDataTable.getOwnerModID(mods, targets);
            if (Strings.isNullOrEmpty(owner)) {
                FMLLog.bigWarning("Could not determine owning mod for @%s on %s for mod %s", annotationClassName, targets.getClassName(), this.getModId());
                continue;
            }
        }
        if (!this.getModId().equals(owner)) {
            FMLLog.fine("Skipping @%s injection for %s.%s since it is not for mod %s", annotationClassName, targets.getClassName(), targets.getObjectName(), this.getModId());
            continue;
        }
        Field f = null;
        Object injectedMod = null;
        ModContainer mc = this;
        boolean isStatic = false;
        Class<?> clz = modInstance.getClass();
        if (!Strings.isNullOrEmpty(targetMod)) {
            if (Loader.isModLoaded(targetMod)) {
                mc = Loader.instance().getIndexedModList().get(targetMod);
            } else {
                mc = null;
            }
        }
        if (mc != null) {
            try {
                clz = Class.forName(targets.getClassName(), true, Loader.instance().getModClassLoader());
                f = clz.getDeclaredField(targets.getObjectName());
                f.setAccessible(true);
                isStatic = Modifier.isStatic(f.getModifiers());
                injectedMod = retriever.apply(mc);
            } catch (Exception e) {
                Throwables.propagateIfPossible(e);
                FMLLog.log(getModId(), Level.WARN, e, "Attempting to load @%s in class %s for %s and failing", annotationName, targets.getClassName(), mc.getModId());
            }
        }
        if (f != null) {
            Object target = null;
            if (!isStatic) {
                target = modInstance;
                if (!modInstance.getClass().equals(clz)) {
                    FMLLog.log(getModId(), Level.WARN, "Unable to inject @%s in non-static field %s.%s for %s as it is NOT the primary mod instance", annotationName, targets.getClassName(), targets.getObjectName(), mc.getModId());
                    continue;
                }
            }
            f.set(target, injectedMod);
        }
    }
}
Also used : ASMData(net.minecraftforge.fml.common.discovery.ASMDataTable.ASMData) Field(java.lang.reflect.Field) MalformedURLException(java.net.MalformedURLException)

Example 9 with ASMData

use of net.minecraftforge.fml.common.discovery.ASMDataTable.ASMData in project MinecraftForge by MinecraftForge.

the class AutomaticEventSubscriber method inject.

public static void inject(ModContainer mod, ASMDataTable data, Side side) {
    FMLLog.fine("Attempting to inject @EventBusSubscriber classes into the eventbus for %s", mod.getModId());
    SetMultimap<String, ASMData> modData = data.getAnnotationsFor(mod);
    Set<ASMDataTable.ASMData> mods = modData.get(Mod.class.getName());
    Set<ASMDataTable.ASMData> targets = modData.get(Mod.EventBusSubscriber.class.getName());
    ClassLoader mcl = Loader.instance().getModClassLoader();
    for (ASMDataTable.ASMData targ : targets) {
        try {
            //noinspection unchecked
            List<ModAnnotation.EnumHolder> sidesEnum = (List<ModAnnotation.EnumHolder>) targ.getAnnotationInfo().get("value");
            EnumSet<Side> sides = DEFAULT;
            if (sidesEnum != null) {
                sides = EnumSet.noneOf(Side.class);
                for (ModAnnotation.EnumHolder h : sidesEnum) {
                    sides.add(Side.valueOf(h.getValue()));
                }
            }
            if (sides == DEFAULT || sides.contains(side)) {
                FMLLog.fine("Found @EventBusSubscriber class %s", targ.getClassName());
                String amodid = (String) targ.getAnnotationInfo().get("modid");
                if (Strings.isNullOrEmpty(amodid)) {
                    amodid = ASMDataTable.getOwnerModID(mods, targ);
                    if (Strings.isNullOrEmpty(amodid)) {
                        FMLLog.bigWarning("Could not determine owning mod for @EventBusSubscriber on %s for mod %s", targ.getClassName(), mod.getModId());
                        continue;
                    }
                }
                if (!mod.getModId().equals(amodid)) {
                    FMLLog.fine("Skipping @EventBusSubscriber injection for %s since it is not for mod %s", targ.getClassName(), mod.getModId());
                    //We're not injecting this guy
                    continue;
                }
                Class<?> subscriptionTarget = Class.forName(targ.getClassName(), true, mcl);
                MinecraftForge.EVENT_BUS.register(subscriptionTarget);
                FMLLog.fine("Injected @EventBusSubscriber class %s", targ.getClassName());
            }
        } catch (Exception e) {
            FMLLog.log(Level.ERROR, e, "An error occurred trying to load an EventBusSubscriber %s for modid %s", targ.getClassName(), mod.getModId());
            throw new LoaderException(e);
        }
    }
}
Also used : ASMData(net.minecraftforge.fml.common.discovery.ASMDataTable.ASMData) Side(net.minecraftforge.fml.relauncher.Side) ASMData(net.minecraftforge.fml.common.discovery.ASMDataTable.ASMData) ASMDataTable(net.minecraftforge.fml.common.discovery.ASMDataTable) ModAnnotation(net.minecraftforge.fml.common.discovery.asm.ModAnnotation) List(java.util.List)

Example 10 with ASMData

use of net.minecraftforge.fml.common.discovery.ASMDataTable.ASMData in project MinecraftForge by MinecraftForge.

the class ModAPITransformer method transform.

@Override
public byte[] transform(String name, String transformedName, byte[] basicClass) {
    String lookupName = name;
    if (name.endsWith("$class")) {
        lookupName = name.substring(0, name.length() - 6);
    }
    if (optionals == null || !optionals.containsKey(lookupName)) {
        return basicClass;
    }
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(basicClass);
    classReader.accept(classNode, 0);
    if (logDebugInfo)
        FMLRelaunchLog.finer("Optional removal - found optionals for class %s - processing", name);
    for (ASMData optional : optionals.get(lookupName)) {
        String modId = (String) optional.getAnnotationInfo().get("modid");
        if (Loader.isModLoaded(modId) || ModAPIManager.INSTANCE.hasAPI(modId)) {
            if (logDebugInfo)
                FMLRelaunchLog.finer("Optional removal skipped - mod present %s", modId);
            continue;
        }
        if (logDebugInfo)
            FMLRelaunchLog.finer("Optional on %s triggered - mod missing %s", name, modId);
        if (optional.getAnnotationInfo().containsKey("iface")) {
            Boolean stripRefs = (Boolean) optional.getAnnotationInfo().get("striprefs");
            if (stripRefs == null)
                stripRefs = Boolean.FALSE;
            stripInterface(classNode, (String) optional.getAnnotationInfo().get("iface"), stripRefs);
        } else {
            stripMethod(classNode, optional.getObjectName());
        }
    }
    if (logDebugInfo)
        FMLRelaunchLog.finer("Optional removal - class %s processed", name);
    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    classNode.accept(writer);
    return writer.toByteArray();
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ASMData(net.minecraftforge.fml.common.discovery.ASMDataTable.ASMData) ClassReader(org.objectweb.asm.ClassReader) ClassWriter(org.objectweb.asm.ClassWriter)

Aggregations

ASMData (net.minecraftforge.fml.common.discovery.ASMDataTable.ASMData)12 Field (java.lang.reflect.Field)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 Type (java.lang.reflect.Type)2 MalformedURLException (java.net.MalformedURLException)2 List (java.util.List)2 ASMDataTable (net.minecraftforge.fml.common.discovery.ASMDataTable)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSetMultimap (com.google.common.collect.ImmutableSetMultimap)1 SetMultimap (com.google.common.collect.SetMultimap)1 Subscribe (com.google.common.eventbus.Subscribe)1 File (java.io.File)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 LoaderException (net.minecraftforge.fml.common.LoaderException)1 ModContainer (net.minecraftforge.fml.common.ModContainer)1 ModCandidate (net.minecraftforge.fml.common.discovery.ModCandidate)1 ModAnnotation (net.minecraftforge.fml.common.discovery.asm.ModAnnotation)1 EnumHolder (net.minecraftforge.fml.common.discovery.asm.ModAnnotation.EnumHolder)1 ModIdFunction (net.minecraftforge.fml.common.functions.ModIdFunction)1