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);
}
}
}
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);
}
}
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);
}
}
}
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);
}
}
}
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();
}
Aggregations