From b12c26047b2cd6fae11ee3f4bf0a5d23e0246baf Mon Sep 17 00:00:00 2001 From: Gravit Date: Sat, 20 Apr 2019 05:03:06 +0700 Subject: [PATCH] [FEATURE] GsonManager --- .../ru/gravit/launchserver/LaunchServer.java | 41 ++++--------------- .../auth/handler/JsonAuthHandler.java | 8 ++-- .../auth/hwid/JsonFileHWIDHandler.java | 4 +- .../JsonFilePermissionsHandler.java | 6 +-- .../JsonLongFilePermissionsHandler.java | 6 +-- .../command/dump/DumpEntryCacheCommand.java | 4 +- .../command/dump/DumpSessionsCommand.java | 4 +- .../command/hash/DownloadClientCommand.java | 4 +- .../command/service/ComponentCommand.java | 2 +- .../manangers/LaunchServerGsonManager.java | 28 +++++++++++++ .../ru/gravit/launcher/LauncherEngine.java | 7 ++-- .../launcher/client/ClientLauncher.java | 14 +++---- .../launcher/client/LauncherSettings.java | 4 +- .../launcher/managers/ClientGsonManager.java | 11 +++++ .../gravit/launcher/server/ServerWrapper.java | 10 +---- .../server/ServerWrapperGsonManager.java | 11 +++++ .../java/ru/gravit/launcher/Launcher.java | 4 +- .../gravit/launcher/managers/GsonManager.java | 37 +++++++++++++++++ .../gravit/utils/config/JsonConfigurable.java | 4 +- modules | 2 +- 20 files changed, 133 insertions(+), 78 deletions(-) create mode 100644 LaunchServer/src/main/java/ru/gravit/launchserver/manangers/LaunchServerGsonManager.java create mode 100644 Launcher/src/main/java/ru/gravit/launcher/managers/ClientGsonManager.java create mode 100644 ServerWrapper/src/main/java/ru/gravit/launcher/server/ServerWrapperGsonManager.java create mode 100644 libLauncher/src/main/java/ru/gravit/launcher/managers/GsonManager.java diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java index e7ba2cb7..ec0aa8ea 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java @@ -65,7 +65,7 @@ public void reload() throws Exception { config.close(); LogHelper.info("Reading LaunchServer config file"); try (BufferedReader reader = IOHelper.newReader(configFile)) { - config = Launcher.gson.fromJson(reader, Config.class); + config = Launcher.gsonManager.gson.fromJson(reader, Config.class); } config.verify(); Launcher.applyLauncherEnv(config.env); @@ -290,7 +290,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO // Read profile ClientProfile profile; try (BufferedReader reader = IOHelper.newReader(file)) { - profile = Launcher.gson.fromJson(reader, ClientProfile.class); + profile = Launcher.gsonManager.gson.fromJson(reader, ClientProfile.class); } profile.verify(); @@ -403,9 +403,6 @@ public static void main(String... args) throws Throwable { public final Timer taskPool; - public static Gson gson; - public static GsonBuilder gsonBuilder; - public static Class defaultLauncherEXEBinaryClass = null; public LaunchServer(Path dir, boolean testEnv, String[] args) throws IOException, InvalidKeySpecException { @@ -488,7 +485,7 @@ public LaunchServer(Path dir, boolean testEnv, String[] args) throws IOException generateConfigIfNotExists(testEnv); LogHelper.info("Reading LaunchServer config file"); try (BufferedReader reader = IOHelper.newReader(configFile)) { - config = Launcher.gson.fromJson(reader, Config.class); + config = Launcher.gsonManager.gson.fromJson(reader, Config.class); } if(!Files.exists(runtimeConfigFile)) { @@ -500,7 +497,7 @@ public LaunchServer(Path dir, boolean testEnv, String[] args) throws IOException { LogHelper.info("Reading LaunchServer runtime config file"); try (BufferedReader reader = IOHelper.newReader(runtimeConfigFile)) { - runtime = Launcher.gson.fromJson(reader, LaunchServerRuntimeConfig.class); + runtime = Launcher.gsonManager.gson.fromJson(reader, LaunchServerRuntimeConfig.class); } } runtime.verify(); @@ -597,28 +594,8 @@ public LaunchServer(Path dir, boolean testEnv, String[] args) throws IOException } public static void initGson() { - if (Launcher.gson != null) return; - Launcher.gsonBuilder = new GsonBuilder(); - Launcher.gsonBuilder.registerTypeAdapter(AuthProvider.class, new UniversalJsonAdapter<>(AuthProvider.providers)); - Launcher.gsonBuilder.registerTypeAdapter(TextureProvider.class, new UniversalJsonAdapter<>(TextureProvider.providers)); - Launcher.gsonBuilder.registerTypeAdapter(AuthHandler.class, new UniversalJsonAdapter<>(AuthHandler.providers)); - Launcher.gsonBuilder.registerTypeAdapter(PermissionsHandler.class, new UniversalJsonAdapter<>(PermissionsHandler.providers)); - Launcher.gsonBuilder.registerTypeAdapter(HWIDHandler.class, new UniversalJsonAdapter<>(HWIDHandler.providers)); - Launcher.gsonBuilder.registerTypeAdapter(Component.class, new UniversalJsonAdapter<>(Component.providers)); - Launcher.gsonBuilder.registerTypeAdapter(ProtectHandler.class, new UniversalJsonAdapter<>(ProtectHandler.providers)); - Launcher.gson = Launcher.gsonBuilder.create(); - - //Human readable - LaunchServer.gsonBuilder = new GsonBuilder(); - LaunchServer.gsonBuilder.setPrettyPrinting(); - LaunchServer.gsonBuilder.registerTypeAdapter(AuthProvider.class, new UniversalJsonAdapter<>(AuthProvider.providers)); - LaunchServer.gsonBuilder.registerTypeAdapter(TextureProvider.class, new UniversalJsonAdapter<>(TextureProvider.providers)); - LaunchServer.gsonBuilder.registerTypeAdapter(AuthHandler.class, new UniversalJsonAdapter<>(AuthHandler.providers)); - LaunchServer.gsonBuilder.registerTypeAdapter(PermissionsHandler.class, new UniversalJsonAdapter<>(PermissionsHandler.providers)); - LaunchServer.gsonBuilder.registerTypeAdapter(HWIDHandler.class, new UniversalJsonAdapter<>(HWIDHandler.providers)); - LaunchServer.gsonBuilder.registerTypeAdapter(Component.class, new UniversalJsonAdapter<>(Component.providers)); - LaunchServer.gsonBuilder.registerTypeAdapter(ProtectHandler.class, new UniversalJsonAdapter<>(ProtectHandler.providers)); - LaunchServer.gson = LaunchServer.gsonBuilder.create(); + Launcher.gsonManager = new LaunchServerGsonManager(); + Launcher.gsonManager.initGson(); } private LauncherBinary binary() { @@ -654,9 +631,9 @@ public void close() { LogHelper.info("Save LaunchServer runtime config"); try(Writer writer = IOHelper.newWriter(runtimeConfigFile)) { - if(LaunchServer.gson != null) + if(Launcher.gsonManager.configGson != null) { - LaunchServer.gson.toJson(runtime, writer); + Launcher.gsonManager.configGson.toJson(runtime, writer); } else { LogHelper.error("Error writing LaunchServer runtime config file. Gson is null"); } @@ -745,7 +722,7 @@ private void generateConfigIfNotExists(boolean testEnv) throws IOException { // Write LaunchServer config LogHelper.info("Writing LaunchServer config file"); try (BufferedWriter writer = IOHelper.newWriter(configFile)) { - LaunchServer.gson.toJson(newConfig, writer); + Launcher.gsonManager.configGson.toJson(newConfig, writer); } } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/handler/JsonAuthHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/handler/JsonAuthHandler.java index ccd6fbe1..9c0804e4 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/handler/JsonAuthHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/handler/JsonAuthHandler.java @@ -55,22 +55,22 @@ public class SuccessResponse } @Override protected Entry fetchEntry(String username) throws IOException { - return LaunchServer.gson.fromJson(HTTPRequest.jsonRequest(LaunchServer.gson.toJsonTree(new EntryRequestByUsername(username)), getUrl), Entry.class); + return Launcher.gsonManager.configGson.fromJson(HTTPRequest.jsonRequest(Launcher.gsonManager.configGson.toJsonTree(new EntryRequestByUsername(username)), getUrl), Entry.class); } @Override protected Entry fetchEntry(UUID uuid) throws IOException { - return LaunchServer.gson.fromJson(HTTPRequest.jsonRequest(LaunchServer.gson.toJsonTree(new EntryRequestByUUID(uuid)), getUrl), Entry.class); + return Launcher.gsonManager.configGson.fromJson(HTTPRequest.jsonRequest(Launcher.gsonManager.configGson.toJsonTree(new EntryRequestByUUID(uuid)), getUrl), Entry.class); } @Override protected boolean updateAuth(UUID uuid, String username, String accessToken) throws IOException { - return LaunchServer.gson.fromJson(HTTPRequest.jsonRequest(LaunchServer.gson.toJsonTree(new UpdateAuthRequest(uuid, username, accessToken)), updateAuthUrl), SuccessResponse.class).success; + return Launcher.gsonManager.configGson.fromJson(HTTPRequest.jsonRequest(Launcher.gsonManager.configGson.toJsonTree(new UpdateAuthRequest(uuid, username, accessToken)), updateAuthUrl), SuccessResponse.class).success; } @Override protected boolean updateServerID(UUID uuid, String serverID) throws IOException { - return LaunchServer.gson.fromJson(HTTPRequest.jsonRequest(LaunchServer.gson.toJsonTree(new UpdateServerIDRequest(uuid, serverID)), updateServerIdUrl), SuccessResponse.class).success; + return Launcher.gsonManager.configGson.fromJson(HTTPRequest.jsonRequest(Launcher.gsonManager.configGson.toJsonTree(new UpdateServerIDRequest(uuid, serverID)), updateServerIdUrl), SuccessResponse.class).success; } @Override diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/JsonFileHWIDHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/JsonFileHWIDHandler.java index 42bbf627..2fb316a2 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/JsonFileHWIDHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/JsonFileHWIDHandler.java @@ -60,7 +60,7 @@ public void init() { Type type = new TypeToken>() { }.getType(); try (Reader reader = IOHelper.newReader(path)) { - list = Launcher.gson.fromJson(reader, type); + list = Launcher.gsonManager.gson.fromJson(reader, type); } catch (IOException e) { LogHelper.error(e); } @@ -84,7 +84,7 @@ public void check0(HWID hwid, String username) throws HWIDException { public void close() throws Exception { Path path = Paths.get(filename); try (Writer writer = IOHelper.newWriter(path)) { - LaunchServer.gson.toJson(list, writer); + Launcher.gsonManager.configGson.toJson(list, writer); } } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/JsonFilePermissionsHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/JsonFilePermissionsHandler.java index 43778464..dbe172f4 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/JsonFilePermissionsHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/JsonFilePermissionsHandler.java @@ -27,7 +27,7 @@ public void reload() { Type type = new TypeToken>() { }.getType(); try (Reader reader = IOHelper.newReader(path)) { - map = Launcher.gson.fromJson(reader, type); + map = Launcher.gsonManager.gson.fromJson(reader, type); } catch (IOException e) { LogHelper.error(e); } @@ -51,13 +51,13 @@ public void init() { if (!IOHelper.exists(path)) { map = new HashMap<>(); try (Writer writer = IOHelper.newWriter(path)) { - Launcher.gson.toJson(map, writer); + Launcher.gsonManager.gson.toJson(map, writer); } catch (IOException e) { LogHelper.error(e); } } try (Reader reader = IOHelper.newReader(path)) { - map = Launcher.gson.fromJson(reader, type); + map = Launcher.gsonManager.gson.fromJson(reader, type); } catch (IOException e) { LogHelper.error(e); } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/JsonLongFilePermissionsHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/JsonLongFilePermissionsHandler.java index 3cfb3262..8fd82f40 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/JsonLongFilePermissionsHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/JsonLongFilePermissionsHandler.java @@ -28,7 +28,7 @@ public void reload() { Type type = new TypeToken>() { }.getType(); try (Reader reader = IOHelper.newReader(path)) { - map = Launcher.gson.fromJson(reader, type); + map = Launcher.gsonManager.gson.fromJson(reader, type); } catch (IOException e) { LogHelper.error(e); } @@ -52,13 +52,13 @@ public void init() { if (!IOHelper.exists(path)) { map = new HashMap<>(); try (Writer writer = IOHelper.newWriter(path)) { - Launcher.gson.toJson(map, writer); + Launcher.gsonManager.gson.toJson(map, writer); } catch (IOException e) { LogHelper.error(e); } } try (Reader reader = IOHelper.newReader(path)) { - map = Launcher.gson.fromJson(reader, type); + map = Launcher.gsonManager.gson.fromJson(reader, type); } catch (IOException e) { LogHelper.error(e); } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/command/dump/DumpEntryCacheCommand.java b/LaunchServer/src/main/java/ru/gravit/launchserver/command/dump/DumpEntryCacheCommand.java index d3fcc39b..b2cec441 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/command/dump/DumpEntryCacheCommand.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/command/dump/DumpEntryCacheCommand.java @@ -44,7 +44,7 @@ public void invoke(String... args) throws Exception { serializable.entryCache = entryCache; serializable.usernameCache = usernamesCache; try (Writer writer = IOHelper.newWriter(Paths.get(args[1]))) { - LaunchServer.gson.toJson(serializable, writer); + Launcher.gsonManager.configGson.toJson(serializable, writer); } LogHelper.subInfo("Write %d entryCache, %d usernameCache", entryCache.size(), usernamesCache.size()); } else if (args[0].equals("load")) { @@ -52,7 +52,7 @@ public void invoke(String... args) throws Exception { int size_entry = 0; int size_username = 0; try (Reader reader = IOHelper.newReader(Paths.get(args[1]))) { - EntryAndUsername entryAndUsername = LaunchServer.gson.fromJson(reader, EntryAndUsername.class); + EntryAndUsername entryAndUsername = Launcher.gsonManager.configGson.fromJson(reader, EntryAndUsername.class); size_entry = entryAndUsername.entryCache.size(); size_username = entryAndUsername.usernameCache.size(); authHandler.loadEntryCache(entryAndUsername.entryCache); diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/command/dump/DumpSessionsCommand.java b/LaunchServer/src/main/java/ru/gravit/launchserver/command/dump/DumpSessionsCommand.java index 5d36a0db..bfb25d90 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/command/dump/DumpSessionsCommand.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/command/dump/DumpSessionsCommand.java @@ -36,7 +36,7 @@ public void invoke(String... args) throws Exception { LogHelper.info("Sessions write to %s", args[1]); Set clientSet = server.sessionManager.getSessions(); try (Writer writer = IOHelper.newWriter(Paths.get(args[1]))) { - LaunchServer.gson.toJson(clientSet, writer); + Launcher.gsonManager.configGson.toJson(clientSet, writer); } LogHelper.subInfo("Write %d sessions", clientSet.size()); } else if (args[0].equals("load")) { @@ -45,7 +45,7 @@ public void invoke(String... args) throws Exception { try (Reader reader = IOHelper.newReader(Paths.get(args[1]))) { Type setType = new TypeToken>() { }.getType(); - Set clientSet = LaunchServer.gson.fromJson(reader, setType); + Set clientSet = Launcher.gsonManager.configGson.fromJson(reader, setType); size = clientSet.size(); server.sessionManager.loadSessions(clientSet); } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/command/hash/DownloadClientCommand.java b/LaunchServer/src/main/java/ru/gravit/launchserver/command/hash/DownloadClientCommand.java index c2d361b5..fc5b32cf 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/command/hash/DownloadClientCommand.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/command/hash/DownloadClientCommand.java @@ -52,13 +52,13 @@ public void invoke(String... args) throws IOException, CommandException { ClientProfile client; String profilePath = String.format("ru/gravit/launchserver/defaults/profile%s.cfg", version.name); try (BufferedReader reader = IOHelper.newReader(IOHelper.getResourceURL(profilePath))) { - client = LaunchServer.gson.fromJson(reader, ClientProfile.class); + client = Launcher.gsonManager.configGson.fromJson(reader, ClientProfile.class); } client.setTitle(dirName); client.setDir(dirName); try (BufferedWriter writer = IOHelper.newWriter(IOHelper.resolveIncremental(server.profilesDir, dirName, "cfg"))) { - LaunchServer.gson.toJson(client, writer); + Launcher.gsonManager.configGson.toJson(client, writer); } // Finished diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/command/service/ComponentCommand.java b/LaunchServer/src/main/java/ru/gravit/launchserver/command/service/ComponentCommand.java index 2d9ea888..448a6f33 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/command/service/ComponentCommand.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/command/service/ComponentCommand.java @@ -73,7 +73,7 @@ public void invoke(String... args) throws Exception { if (args.length <= 2) throw new IllegalArgumentException("Must set file"); String fileName = args[2]; try (Reader reader = IOHelper.newReader(Paths.get(fileName))) { - Component component = LaunchServer.gson.fromJson(reader, Component.class); + Component component = Launcher.gsonManager.configGson.fromJson(reader, Component.class); component.preInit(server); component.init(server); component.postInit(server); diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/manangers/LaunchServerGsonManager.java b/LaunchServer/src/main/java/ru/gravit/launchserver/manangers/LaunchServerGsonManager.java new file mode 100644 index 00000000..dc2b312f --- /dev/null +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/manangers/LaunchServerGsonManager.java @@ -0,0 +1,28 @@ +package ru.gravit.launchserver.manangers; + +import com.google.gson.GsonBuilder; +import ru.gravit.launcher.Launcher; +import ru.gravit.launcher.managers.GsonManager; +import ru.gravit.launchserver.auth.handler.AuthHandler; +import ru.gravit.launchserver.auth.hwid.HWIDHandler; +import ru.gravit.launchserver.auth.permissions.PermissionsHandler; +import ru.gravit.launchserver.auth.protect.ProtectHandler; +import ru.gravit.launchserver.auth.provider.AuthProvider; +import ru.gravit.launchserver.auth.texture.TextureProvider; +import ru.gravit.launchserver.components.Component; +import ru.gravit.utils.UniversalJsonAdapter; + +public class LaunchServerGsonManager extends GsonManager { + @Override + public void registerAdapters(GsonBuilder builder) { + super.registerAdapters(builder); + Launcher.gsonManager.gsonBuilder = new GsonBuilder(); + Launcher.gsonManager.gsonBuilder.registerTypeAdapter(AuthProvider.class, new UniversalJsonAdapter<>(AuthProvider.providers)); + Launcher.gsonManager.gsonBuilder.registerTypeAdapter(TextureProvider.class, new UniversalJsonAdapter<>(TextureProvider.providers)); + Launcher.gsonManager.gsonBuilder.registerTypeAdapter(AuthHandler.class, new UniversalJsonAdapter<>(AuthHandler.providers)); + Launcher.gsonManager.gsonBuilder.registerTypeAdapter(PermissionsHandler.class, new UniversalJsonAdapter<>(PermissionsHandler.providers)); + Launcher.gsonManager.gsonBuilder.registerTypeAdapter(HWIDHandler.class, new UniversalJsonAdapter<>(HWIDHandler.providers)); + Launcher.gsonManager.gsonBuilder.registerTypeAdapter(Component.class, new UniversalJsonAdapter<>(Component.providers)); + Launcher.gsonManager.gsonBuilder.registerTypeAdapter(ProtectHandler.class, new UniversalJsonAdapter<>(ProtectHandler.providers)); + } +} diff --git a/Launcher/src/main/java/ru/gravit/launcher/LauncherEngine.java b/Launcher/src/main/java/ru/gravit/launcher/LauncherEngine.java index 6638f4b7..0cfa6f86 100644 --- a/Launcher/src/main/java/ru/gravit/launcher/LauncherEngine.java +++ b/Launcher/src/main/java/ru/gravit/launcher/LauncherEngine.java @@ -9,6 +9,7 @@ import ru.gravit.launcher.gui.RuntimeProvider; import ru.gravit.launcher.hasher.HashedEntry; import ru.gravit.launcher.hasher.HashedEntryAdapter; +import ru.gravit.launcher.managers.ClientGsonManager; import ru.gravit.launcher.request.Request; import ru.gravit.launcher.request.websockets.StandartClientWebSocketService; import ru.gravit.utils.helper.CommonHelper; @@ -43,10 +44,8 @@ public static void main(String... args) throws Throwable { } public static void initGson() { - if (Launcher.gson != null) return; - Launcher.gsonBuilder = new GsonBuilder(); - Launcher.gsonBuilder.registerTypeAdapter(HashedEntry.class, new HashedEntryAdapter()); - Launcher.gson = Launcher.gsonBuilder.create(); + Launcher.gsonManager = new ClientGsonManager(); + Launcher.gsonManager.initGson(); } // Instance diff --git a/Launcher/src/main/java/ru/gravit/launcher/client/ClientLauncher.java b/Launcher/src/main/java/ru/gravit/launcher/client/ClientLauncher.java index 013c7a33..8f54ada5 100644 --- a/Launcher/src/main/java/ru/gravit/launcher/client/ClientLauncher.java +++ b/Launcher/src/main/java/ru/gravit/launcher/client/ClientLauncher.java @@ -6,6 +6,7 @@ import ru.gravit.launcher.guard.LauncherGuardManager; import ru.gravit.launcher.gui.JSRuntimeProvider; import ru.gravit.launcher.hasher.*; +import ru.gravit.launcher.managers.ClientGsonManager; import ru.gravit.launcher.profiles.ClientProfile; import ru.gravit.launcher.profiles.PlayerProfile; import ru.gravit.launcher.request.Request; @@ -35,7 +36,6 @@ import java.util.*; public final class ClientLauncher { - private static Gson gson = new Gson(); private static final class ClassPathFileVisitor extends SimpleFileVisitor { private final Collection result; @@ -187,7 +187,7 @@ private static void addClientArgs(Collection args, ClientProfile profile properties.cloakURL = new String[]{pp.cloak.url}; properties.cloakDigest = new String[]{SecurityHelper.toHex(pp.cloak.digest)}; } - Collections.addAll(args, "--userProperties", ClientLauncher.gson.toJson(properties)); + Collections.addAll(args, "--userProperties", Launcher.gsonManager.gson.toJson(properties)); // Add asset index Collections.addAll(args, "--assetIndex", profile.getAssetIndex()); @@ -309,7 +309,7 @@ public static Process launch( } try (HOutput output = new HOutput(client.getOutputStream())) { params.write(output); - output.writeString(Launcher.gson.toJson(profile), 0); + output.writeString(Launcher.gsonManager.gson.toJson(profile), 0); assetHDir.write(output); clientHDir.write(output); } @@ -420,7 +420,7 @@ public static void main(String... args) throws Throwable { socket.connect(new InetSocketAddress(SOCKET_HOST, SOCKET_PORT)); try (HInput input = new HInput(socket.getInputStream())) { params = new Params(input); - profile = gson.fromJson(input.readString(0), ClientProfile.class); + profile = Launcher.gsonManager.gson.fromJson(input.readString(0), ClientProfile.class); assetHDir = new HashedDir(input); clientHDir = new HashedDir(input); } @@ -521,10 +521,8 @@ private static LinkedList resolveClassPathList(Path clientDir, String... c } public static void initGson() { - if (Launcher.gson != null) return; - Launcher.gsonBuilder = new GsonBuilder(); - Launcher.gsonBuilder.registerTypeAdapter(HashedEntry.class, new HashedEntryAdapter()); - Launcher.gson = Launcher.gsonBuilder.create(); + Launcher.gsonManager = new ClientGsonManager(); + Launcher.gsonManager.initGson(); } @LauncherAPI diff --git a/Launcher/src/main/java/ru/gravit/launcher/client/LauncherSettings.java b/Launcher/src/main/java/ru/gravit/launcher/client/LauncherSettings.java index b429289c..4263ccdc 100644 --- a/Launcher/src/main/java/ru/gravit/launcher/client/LauncherSettings.java +++ b/Launcher/src/main/java/ru/gravit/launcher/client/LauncherSettings.java @@ -108,7 +108,7 @@ public void read(HInput input) throws IOException, SignatureException { lastProfiles.clear(); int lastProfilesCount = input.readLength(0); for (int i = 0; i < lastProfilesCount; i++) { - lastProfiles.add(Launcher.gson.fromJson(input.readString(0), ClientProfile.class)); + lastProfiles.add(Launcher.gsonManager.gson.fromJson(input.readString(0), ClientProfile.class)); } lastHDirs.clear(); int lastHDirsCount = input.readLength(0); @@ -150,7 +150,7 @@ public void write(HOutput output) throws IOException { } output.writeLength(lastProfiles.size(), 0); for (ClientProfile profile : lastProfiles) { - output.writeString(Launcher.gson.toJson(profile), 0); + output.writeString(Launcher.gsonManager.gson.toJson(profile), 0); } output.writeLength(lastHDirs.size(), 0); for (Map.Entry entry : lastHDirs.entrySet()) { diff --git a/Launcher/src/main/java/ru/gravit/launcher/managers/ClientGsonManager.java b/Launcher/src/main/java/ru/gravit/launcher/managers/ClientGsonManager.java new file mode 100644 index 00000000..cdd82078 --- /dev/null +++ b/Launcher/src/main/java/ru/gravit/launcher/managers/ClientGsonManager.java @@ -0,0 +1,11 @@ +package ru.gravit.launcher.managers; + +import com.google.gson.GsonBuilder; + +public class ClientGsonManager extends GsonManager { + @Override + public void registerAdapters(GsonBuilder builder) { + super.registerAdapters(builder); + + } +} diff --git a/ServerWrapper/src/main/java/ru/gravit/launcher/server/ServerWrapper.java b/ServerWrapper/src/main/java/ru/gravit/launcher/server/ServerWrapper.java index bf1593b9..fbeb6827 100644 --- a/ServerWrapper/src/main/java/ru/gravit/launcher/server/ServerWrapper.java +++ b/ServerWrapper/src/main/java/ru/gravit/launcher/server/ServerWrapper.java @@ -37,8 +37,6 @@ public class ServerWrapper extends JsonConfigurable { public ClassLoader loader; public ClientPermissions permissions; public static ServerWrapper wrapper; - public static Gson gson; - private static GsonBuilder gsonBuiler; public static Path modulesDir = Paths.get(System.getProperty("serverwrapper.modulesDir", "modules")); public static Path configFile = Paths.get(System.getProperty("serverwrapper.configFile", "ServerWrapperConfig.json")); @@ -97,15 +95,11 @@ public boolean loopAuth(int count, int sleeptime) { } public static void initGson() { - if (Launcher.gson != null) return; - Launcher.gsonBuilder = new GsonBuilder(); - Launcher.gson = Launcher.gsonBuilder.create(); + Launcher.gsonManager = new ServerWrapperGsonManager(); + Launcher.gsonManager.initGson(); } public void run(String... args) throws Throwable { - gsonBuiler = new GsonBuilder(); - gsonBuiler.setPrettyPrinting(); - gson = gsonBuiler.create(); initGson(); if (args.length > 0 && args[0].equals("setup") && !disableSetup) { LogHelper.debug("Read ServerWrapperConfig.json"); diff --git a/ServerWrapper/src/main/java/ru/gravit/launcher/server/ServerWrapperGsonManager.java b/ServerWrapper/src/main/java/ru/gravit/launcher/server/ServerWrapperGsonManager.java new file mode 100644 index 00000000..98813aac --- /dev/null +++ b/ServerWrapper/src/main/java/ru/gravit/launcher/server/ServerWrapperGsonManager.java @@ -0,0 +1,11 @@ +package ru.gravit.launcher.server; + +import com.google.gson.GsonBuilder; +import ru.gravit.launcher.managers.GsonManager; + +public class ServerWrapperGsonManager extends GsonManager { + @Override + public void registerAdapters(GsonBuilder builder) { + super.registerAdapters(builder); + } +} diff --git a/libLauncher/src/main/java/ru/gravit/launcher/Launcher.java b/libLauncher/src/main/java/ru/gravit/launcher/Launcher.java index e966ab05..f8519c25 100644 --- a/libLauncher/src/main/java/ru/gravit/launcher/Launcher.java +++ b/libLauncher/src/main/java/ru/gravit/launcher/Launcher.java @@ -2,6 +2,7 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import ru.gravit.launcher.managers.GsonManager; import ru.gravit.launcher.modules.ModulesManager; import ru.gravit.launcher.profiles.ClientProfile; import ru.gravit.launcher.serialize.HInput; @@ -63,8 +64,7 @@ public final class Launcher { public static final int PATCH = 0; public static final int BUILD = 1; public static final Version.Type RELEASE = Version.Type.DEV; - public static GsonBuilder gsonBuilder; - public static Gson gson; + public static GsonManager gsonManager; @LauncherAPI public static LauncherConfig getConfig() { diff --git a/libLauncher/src/main/java/ru/gravit/launcher/managers/GsonManager.java b/libLauncher/src/main/java/ru/gravit/launcher/managers/GsonManager.java new file mode 100644 index 00000000..a8331679 --- /dev/null +++ b/libLauncher/src/main/java/ru/gravit/launcher/managers/GsonManager.java @@ -0,0 +1,37 @@ +package ru.gravit.launcher.managers; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import ru.gravit.launcher.hasher.HashedEntry; +import ru.gravit.launcher.hasher.HashedEntryAdapter; + +public class GsonManager { + public GsonBuilder gsonBuilder; + public Gson gson; + public GsonBuilder configGsonBuilder; + public Gson configGson; + public void initGson() + { + gsonBuilder = new GsonBuilder(); + configGsonBuilder = new GsonBuilder(); + configGsonBuilder.setPrettyPrinting(); + registerAdapters(gsonBuilder); + registerAdapters(configGsonBuilder); + preConfigGson(configGsonBuilder); + preGson(gsonBuilder); + gson = gsonBuilder.create(); + configGson = configGsonBuilder.create(); + } + public void registerAdapters(GsonBuilder builder) + { + builder.registerTypeAdapter(HashedEntry.class, new HashedEntryAdapter()); + } + public void preConfigGson(GsonBuilder gsonBuilder) + { + //skip + } + public void preGson(GsonBuilder gsonBuilder) + { + //skip + } +} diff --git a/libLauncher/src/main/java/ru/gravit/utils/config/JsonConfigurable.java b/libLauncher/src/main/java/ru/gravit/utils/config/JsonConfigurable.java index d2e646d1..e04b5a8e 100644 --- a/libLauncher/src/main/java/ru/gravit/utils/config/JsonConfigurable.java +++ b/libLauncher/src/main/java/ru/gravit/utils/config/JsonConfigurable.java @@ -30,14 +30,14 @@ public JsonConfigurable(Type type, Path configPath) { @LauncherAPI public void saveConfig(Path configPath) throws IOException { try (BufferedWriter writer = IOHelper.newWriter(configPath)) { - Launcher.gson.toJson(getConfig(), type, writer); + Launcher.gsonManager.gson.toJson(getConfig(), type, writer); } } @LauncherAPI public void loadConfig(Path configPath) throws IOException { if (generateConfigIfNotExists(configPath)) return; try (BufferedReader reader = IOHelper.newReader(configPath)) { - setConfig(Launcher.gson.fromJson(reader, type)); + setConfig(Launcher.gsonManager.gson.fromJson(reader, type)); } } @LauncherAPI diff --git a/modules b/modules index 2d7c696a..f8e6af43 160000 --- a/modules +++ b/modules @@ -1 +1 @@ -Subproject commit 2d7c696aebf750f29b0a185faea25a3262fa905e +Subproject commit f8e6af4366347ba94c45d9d8989256bb45dbe13b