From 602f8f7dafd1ad9f85dd4408fa29a69ca73ec485 Mon Sep 17 00:00:00 2001 From: microwin7 Date: Sun, 5 Sep 2021 23:09:51 +0300 Subject: [PATCH] [FEATURE][EXPERIMENTAL] Update method read and repair config LaunchServer, Runtime and Profile's --- .../pro/gravit/launchserver/LaunchServer.java | 7 +-- .../launchserver/LaunchServerStarter.java | 50 +++++++++++++------ 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServer.java b/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServer.java index 7e2d6a76..ed81d0c5 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServer.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServer.java @@ -2,7 +2,6 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import pro.gravit.launcher.Launcher; import pro.gravit.launcher.NeedGarbageCollection; import pro.gravit.launcher.hasher.HashedDir; import pro.gravit.launcher.managers.ConfigManager; @@ -33,7 +32,6 @@ import pro.gravit.utils.helper.JVMHelper; import pro.gravit.utils.helper.SecurityHelper; -import java.io.BufferedReader; import java.io.IOException; import java.lang.ProcessBuilder.Redirect; import java.lang.invoke.MethodHandles; @@ -505,10 +503,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO logger.info("Syncing '{}' profile", IOHelper.getFileName(file)); // Read profile - ClientProfile profile; - try (BufferedReader reader = IOHelper.newReader(file)) { - profile = Launcher.gsonManager.gson.fromJson(reader, ClientProfile.class); - } + ClientProfile profile = LaunchServerStarter.readConfig(file, ClientProfile.class); profile.verify(); // Add SIGNED profile to result list diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServerStarter.java b/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServerStarter.java index 487d5f84..a223ed70 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServerStarter.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServerStarter.java @@ -1,5 +1,7 @@ package pro.gravit.launchserver; +import com.google.gson.JsonElement; +import com.google.gson.JsonSyntaxException; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.bouncycastle.jce.provider.BouncyCastleProvider; @@ -118,37 +120,25 @@ public static void main(String[] args) throws Exception { modulesManager.invokeEvent(new PreConfigPhase()); generateConfigIfNotExists(configFile, localCommandHandler, env); logger.info("Reading LaunchServer config file"); - try (BufferedReader reader = IOHelper.newReader(configFile)) { - config = Launcher.gsonManager.gson.fromJson(reader, LaunchServerConfig.class); - } + config = readConfig(configFile, LaunchServerConfig.class); if (!Files.exists(runtimeConfigFile)) { logger.info("Reset LaunchServer runtime config file"); runtimeConfig = new LaunchServerRuntimeConfig(); runtimeConfig.reset(); } else { logger.info("Reading LaunchServer runtime config file"); - try (BufferedReader reader = IOHelper.newReader(runtimeConfigFile)) { - runtimeConfig = Launcher.gsonManager.gson.fromJson(reader, LaunchServerRuntimeConfig.class); - } + runtimeConfig = readConfig(runtimeConfigFile, LaunchServerRuntimeConfig.class); } LaunchServer.LaunchServerConfigManager launchServerConfigManager = new LaunchServer.LaunchServerConfigManager() { @Override public LaunchServerConfig readConfig() throws IOException { - LaunchServerConfig config1; - try (BufferedReader reader = IOHelper.newReader(configFile)) { - config1 = Launcher.gsonManager.gson.fromJson(reader, LaunchServerConfig.class); - } - return config1; + return LaunchServerStarter.readConfig(configFile, LaunchServerConfig.class); } @Override public LaunchServerRuntimeConfig readRuntimeConfig() throws IOException { - LaunchServerRuntimeConfig config1; - try (BufferedReader reader = IOHelper.newReader(runtimeConfigFile)) { - config1 = Launcher.gsonManager.gson.fromJson(reader, LaunchServerRuntimeConfig.class); - } - return config1; + return LaunchServerStarter.readConfig(runtimeConfigFile, LaunchServerRuntimeConfig.class); } @Override @@ -192,6 +182,34 @@ public void writeRuntimeConfig(LaunchServerRuntimeConfig config) throws IOExcept } } + public static T readConfig(Path file, Class readTo) throws IOException { + try (BufferedReader reader = IOHelper.newReader(file)) { + return Launcher.gsonManager.gson.fromJson(reader, readTo); + } catch (JsonSyntaxException ignored) { + return repairConfig(file, readTo); + } + } + + public static T repairConfig(Path file, Class readTo) throws IOException { + logger.warn("Try to repair " + file.getFileName()); + try (BufferedReader reader = IOHelper.newReader(file)) { + StringBuilder sb = new StringBuilder(); + String line; + while ((line = reader.readLine()) != null) { + sb.append(line.trim()); + } + String s = sb.toString(); + s = s.replaceAll(",\\s*}", "}").replaceAll("}\\{", "},{").replaceAll("]\\[", "],["); + T toReturn = Launcher.gsonManager.configGson.fromJson(s, readTo); + BufferedWriter bw = IOHelper.newWriter(file, false); + JsonElement nativeElement = Launcher.gsonManager.configGson.fromJson(s, JsonElement.class); + bw.write(Launcher.gsonManager.configGson.toJson(nativeElement)); + bw.flush(); + bw.close(); + return toReturn; + } + } + public static void initGson(LaunchServerModulesManager modulesManager) { Launcher.gsonManager = new LaunchServerGsonManager(modulesManager); Launcher.gsonManager.initGson();