From 73fcf4aae42728976629f87223e0b1067dbc0963 Mon Sep 17 00:00:00 2001 From: Gravit Date: Wed, 20 May 2020 21:30:33 +0700 Subject: [PATCH] [FEATURE] Try recursive LauncherModule configuration --- .../launchserver/asm/InjectClassAcceptor.java | 12 ++++++ .../launchermodules/LauncherModuleLoader.java | 38 ++++++++++++++----- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/asm/InjectClassAcceptor.java b/LaunchServer/src/main/java/pro/gravit/launchserver/asm/InjectClassAcceptor.java index 0ac01be2..72a18b1d 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/asm/InjectClassAcceptor.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/asm/InjectClassAcceptor.java @@ -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, 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); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/launchermodules/LauncherModuleLoader.java b/LaunchServer/src/main/java/pro/gravit/launchserver/launchermodules/LauncherModuleLoader.java index ec571b65..5c9c38a2 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/launchermodules/LauncherModuleLoader.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/launchermodules/LauncherModuleLoader.java @@ -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 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()); + } + } + } }