[FEATURE][EXPERIMENTAL] Update method read and repair config LaunchServer, Runtime and Profile's

This commit is contained in:
microwin7 2021-09-05 23:09:51 +03:00
parent c44162dbbe
commit 602f8f7daf
2 changed files with 35 additions and 22 deletions

View file

@ -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

View file

@ -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> T readConfig(Path file, Class<T> 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> T repairConfig(Path file, Class<T> 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();