[FEATURE] Try recursive LauncherModule configuration

This commit is contained in:
Gravit 2020-05-20 21:30:33 +07:00
parent fb00adb129
commit 73fcf4aae4
2 changed files with 41 additions and 9 deletions

View file

@ -175,6 +175,18 @@ private static InsnList serializeValue(Object value) {
value.getClass()));
}
public static boolean isSerializableValue(Object value)
{
if(value == null) return true;
if (primitiveLDCClasses.contains(value.getClass())) return true;
for (Map.Entry<Class<?>, Serializer<?>> serializerEntry : serializers.entrySet()) {
if (serializerEntry.getKey().isInstance(value)) {
return true;
}
}
return false;
}
@Override
public void transform(ClassNode classNode, String className, BuildContext context) {
visit(classNode, values);

View file

@ -2,6 +2,7 @@
import pro.gravit.launcher.Launcher;
import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.asm.InjectClassAcceptor;
import pro.gravit.launchserver.binary.tasks.MainBuildTask;
import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.LogHelper;
@ -120,15 +121,17 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
targetConfig = Launcher.gsonManager.configGson.fromJson(reader, clazz);
}
}
Field[] fields = clazz.getFields();
for (Field field : fields) {
if ((field.getModifiers() & Modifier.STATIC) != 0) continue;
Object obj = field.get(targetConfig);
String configPropertyName = "modules.".concat(entity.moduleConfigName.toLowerCase()).concat(".").concat(field.getName().toLowerCase());
if (entity.propertyMap == null) entity.propertyMap = new HashMap<>();
LogHelper.dev("Property name %s", configPropertyName);
entity.propertyMap.put(configPropertyName, obj);
}
//Field[] fields = clazz.getFields();
//for (Field field : fields) {
// if ((field.getModifiers() & Modifier.STATIC) != 0) continue;
// Object obj = field.get(targetConfig);
// String configPropertyName = "modules.".concat(entity.moduleConfigName.toLowerCase()).concat(".").concat(field.getName().toLowerCase());
// if (entity.propertyMap == null) entity.propertyMap = new HashMap<>();
// LogHelper.dev("Property name %s", configPropertyName);
// entity.propertyMap.put(configPropertyName, obj);
//}
if (entity.propertyMap == null) entity.propertyMap = new HashMap<>();
addClassFieldsToProperties(entity.propertyMap, "modules.".concat(entity.moduleConfigName.toLowerCase()), targetConfig, clazz);
} catch (Throwable e) {
LogHelper.error(e);
}
@ -140,4 +143,21 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
return super.visitFile(file, attrs);
}
}
public void addClassFieldsToProperties(Map<String, Object> propertyMap, String prefix, Object object, Class<?> classOfObject) throws IllegalAccessException {
Field[] fields = classOfObject.getFields();
for (Field field : fields) {
if ((field.getModifiers() & Modifier.STATIC) != 0) continue;
Object obj = field.get(object);
String propertyName = prefix.concat(".").concat(field.getName());
if(InjectClassAcceptor.isSerializableValue(obj))
{
propertyMap.put(propertyName, obj);
}
else
{
//Try recursive add fields
addClassFieldsToProperties(propertyMap, propertyName, obj, obj.getClass());
}
}
}
}