mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-17 11:05:40 +00:00
Reimplement @ObjectHolder scanning, and expose system for others to add handlers.
Fix forgedev and userdev run configs. Fix issue in log functions assuming String arguments.
This commit is contained in:
1 parent
34568a0a74
commit
36d2e67b07
10 files changed
+143
-89
No files matched your search
@@ -19,17 +19,20 @@
|
||||
|
||||
package net.minecraftforge.registries;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.fml.ModList;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.fml.common.registry.GameRegistry;
|
||||
import net.minecraftforge.fml.language.ModFileScanData;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
@@ -37,52 +40,85 @@ import javax.annotation.Nullable;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
import org.objectweb.asm.Type;
|
||||
|
||||
/**
|
||||
* Internal registry for tracking {@link ObjectHolder} references
|
||||
*/
|
||||
public enum ObjectHolderRegistry
|
||||
public class ObjectHolderRegistry
|
||||
{
|
||||
INSTANCE;
|
||||
/**
|
||||
* Exposed to allow modders to register their own notification handlers.
|
||||
* This runnable will be called after a registry snapshot has been injected and finalized.
|
||||
* The internal list is backed by a HashSet so it is HIGHLY recommended you implement a proper equals
|
||||
* and hashCode function to de-duplicate callers here.
|
||||
* The default @ObjectHolder implementation uses the hashCode/equals for the field the annotation is on.
|
||||
*/
|
||||
public static void addHandler(Runnable ref)
|
||||
{
|
||||
objectHolders.add(ref);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removed the specified handler from the notification list.
|
||||
*
|
||||
* The internal list is backed by a hash set, and so proper hashCode and equals operations are required for success.
|
||||
*
|
||||
* The default @ObjectHolder implementation uses the hashCode/equals for the field the annotation is on.
|
||||
*
|
||||
* @return true if handler was matched and removed.
|
||||
*/
|
||||
public static boolean removeHandler(Runnable ref)
|
||||
{
|
||||
return objectHolders.remove(ref);
|
||||
}
|
||||
|
||||
//==============================================================
|
||||
// Everything below is internal, do not use.
|
||||
//==============================================================
|
||||
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
private List<ObjectHolderRef> objectHolders = Lists.newArrayList();
|
||||
/* TODO annotation data
|
||||
public void findObjectHolders(ASMDataTable table)
|
||||
private static final Set<Runnable> objectHolders = new HashSet<>();
|
||||
private static final Type OBJECT_HOLDER = Type.getType(ObjectHolder.class);
|
||||
private static final Type MOD = Type.getType(Mod.class);
|
||||
|
||||
public static void findObjectHolders()
|
||||
{
|
||||
LOGGER.info("Processing ObjectHolder annotations");
|
||||
Set<ASMData> allObjectHolders = table.getAll(ObjectHolder.class.getName());
|
||||
Map<String, String> classModIds = Maps.newHashMap();
|
||||
Map<String, Class<?>> classCache = Maps.newHashMap();
|
||||
final List<ModFileScanData.AnnotationData> annotations = ModList.get().getAllScanData().stream()
|
||||
.map(ModFileScanData::getAnnotations)
|
||||
.flatMap(Collection::stream)
|
||||
.filter(a -> OBJECT_HOLDER.equals(a.getAnnotationType()) || MOD.equals(a.getAnnotationType()))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
table.getAll(Mod.class.getName()).forEach(data -> classModIds.put(data.getClassName(), (String)data.getAnnotationInfo().get("value")));
|
||||
Map<Type, String> classModIds = Maps.newHashMap();
|
||||
Map<Type, Class<?>> classCache = Maps.newHashMap();
|
||||
|
||||
// Gather all @Mod classes, so that @ObjectHolder's in those classes don't need to specify the mod id, Modder convince
|
||||
annotations.stream().filter(a -> MOD.equals(a.getAnnotationType())).forEach(data -> classModIds.put(data.getClassType(), (String)data.getAnnotationData().get("value")));
|
||||
|
||||
// double pass - get all the class level annotations first, then the field level annotations
|
||||
allObjectHolders.stream().filter(data -> data.getObjectName().equals(data.getClassName())).forEach(data ->
|
||||
{
|
||||
String value = (String)data.getAnnotationInfo().get("value");
|
||||
scanTarget(classModIds, classCache, data.getClassName(), data.getObjectName(), value, true, data.getClassName().startsWith("net.minecraft.init"));
|
||||
});
|
||||
allObjectHolders.stream().filter(data -> !data.getObjectName().equals(data.getClassName())).forEach(data ->
|
||||
{
|
||||
String value = (String)data.getAnnotationInfo().get("value");
|
||||
scanTarget(classModIds, classCache, data.getClassName(), data.getObjectName(), value, false, false);
|
||||
});
|
||||
annotations.stream().filter(a -> OBJECT_HOLDER.equals(a.getAnnotationType())).filter(a -> a.getTargetType() == ElementType.TYPE)
|
||||
.forEach(data -> scanTarget(classModIds, classCache, data.getClassType(), null, (String)data.getAnnotationData().get("value"), true, data.getClassType().getClassName().startsWith("net.minecraft.init")));
|
||||
|
||||
annotations.stream().filter(a -> OBJECT_HOLDER.equals(a.getAnnotationType())).filter(a -> a.getTargetType() == ElementType.FIELD)
|
||||
.forEach(data -> scanTarget(classModIds, classCache, data.getClassType(), data.getMemberName(), (String)data.getAnnotationData().get("value"), false, false));
|
||||
LOGGER.info("Found {} ObjectHolder annotations", objectHolders.size());
|
||||
}
|
||||
*/
|
||||
private void scanTarget(Map<String, String> classModIds, Map<String, Class<?>> classCache, String className, @Nullable String annotationTarget, String value, boolean isClass, boolean extractFromValue)
|
||||
|
||||
private static void scanTarget(Map<Type, String> classModIds, Map<Type, Class<?>> classCache, Type type, @Nullable String annotationTarget, String value, boolean isClass, boolean extractFromValue)
|
||||
{
|
||||
Class<?> clazz;
|
||||
if (classCache.containsKey(className))
|
||||
if (classCache.containsKey(type))
|
||||
{
|
||||
clazz = classCache.get(className);
|
||||
clazz = classCache.get(type);
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
clazz = Class.forName(className, extractFromValue, getClass().getClassLoader());
|
||||
classCache.put(className, clazz);
|
||||
clazz = Class.forName(type.getClassName(), extractFromValue, ObjectHolderRegistry.class.getClassLoader());
|
||||
classCache.put(type, clazz);
|
||||
}
|
||||
catch (ClassNotFoundException ex)
|
||||
{
|
||||
@@ -92,24 +128,26 @@ public enum ObjectHolderRegistry
|
||||
}
|
||||
if (isClass)
|
||||
{
|
||||
scanClassForFields(classModIds, className, value, clazz, extractFromValue);
|
||||
scanClassForFields(classModIds, type, value, clazz, extractFromValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (value.indexOf(':') == -1)
|
||||
{
|
||||
String prefix = classModIds.get(className);
|
||||
String prefix = classModIds.get(type);
|
||||
if (prefix == null)
|
||||
{
|
||||
LOGGER.warn("Found an unqualified ObjectHolder annotation ({}) without a modid context at {}.{}, ignoring", value, className, annotationTarget);
|
||||
LOGGER.warn("Found an unqualified ObjectHolder annotation ({}) without a modid context at {}.{}, ignoring", value, type, annotationTarget);
|
||||
throw new IllegalStateException("Unqualified reference to ObjectHolder");
|
||||
}
|
||||
value = prefix + ":" + value;
|
||||
value = prefix + ':' + value;
|
||||
}
|
||||
try
|
||||
{
|
||||
Field f = clazz.getDeclaredField(annotationTarget);
|
||||
addHolderReference(new ObjectHolderRef(f, new ResourceLocation(value), extractFromValue));
|
||||
ObjectHolderRef ref = new ObjectHolderRef(f, value, extractFromValue);
|
||||
if (ref.isValid())
|
||||
addHandler(ref);
|
||||
}
|
||||
catch (NoSuchFieldException ex)
|
||||
{
|
||||
@@ -119,32 +157,24 @@ public enum ObjectHolderRegistry
|
||||
}
|
||||
}
|
||||
|
||||
private void scanClassForFields(Map<String, String> classModIds, String className, String value, Class<?> clazz, boolean extractFromExistingValues)
|
||||
private static void scanClassForFields(Map<Type, String> classModIds, Type targetClass, String value, Class<?> clazz, boolean extractFromExistingValues)
|
||||
{
|
||||
classModIds.put(className, value);
|
||||
classModIds.put(targetClass, value);
|
||||
final int flags = Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC | Opcodes.ACC_SYNTHETIC;
|
||||
for (Field f : clazz.getFields())
|
||||
{
|
||||
if (((f.getModifiers() & flags) != flags) || f.isAnnotationPresent(ObjectHolder.class))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
addHolderReference(new ObjectHolderRef(f, new ResourceLocation(value, f.getName()), extractFromExistingValues));
|
||||
ObjectHolderRef ref = new ObjectHolderRef(f, value + ':' + f.getName().toLowerCase(Locale.ENGLISH), extractFromExistingValues);
|
||||
if (ref.isValid())
|
||||
addHandler(ref);
|
||||
}
|
||||
}
|
||||
|
||||
private void addHolderReference(ObjectHolderRef ref)
|
||||
{
|
||||
if (ref.isValid())
|
||||
{
|
||||
objectHolders.add(ref);
|
||||
}
|
||||
}
|
||||
|
||||
public void applyObjectHolders()
|
||||
public static void applyObjectHolders()
|
||||
{
|
||||
LOGGER.info("Applying holder lookups");
|
||||
objectHolders.forEach(ObjectHolderRef::apply);
|
||||
objectHolders.forEach(Runnable::run);
|
||||
LOGGER.info("Holder lookups applied");
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user