From 6a5d110f9a25db557dab733abb252db44420bf18 Mon Sep 17 00:00:00 2001 From: Gravit Date: Wed, 3 Jul 2019 14:23:26 +0700 Subject: [PATCH] =?UTF-8?q?[FIX]=20=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BE?= =?UTF-8?q?=D0=BA=20=D1=81=D0=B5=D1=80=D0=B8=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../manangers/LaunchServerGsonManager.java | 3 +- .../launcher/managers/ClientGsonManager.java | 2 + .../gravit/launcher/hwid/HWIDProvider.java | 1 + .../launcher/request/WebSocketEvent.java | 4 +- .../launcher/request/auth/AuthRequest.java | 1 + .../websockets/ClientWebSocketService.java | 81 ++++++++----------- .../websockets/JsonRequestAdapter.java | 46 ----------- .../request/websockets/JsonResultAdapter.java | 47 ----------- .../StandartClientWebSocketService.java | 6 +- .../request/websockets/WebSocketRequest.java | 4 +- .../gravit/utils/TypeSerializeInterface.java | 5 ++ .../gravit/utils/UniversalJsonAdapter.java | 6 +- .../server/ServerWrapperGsonManager.java | 2 + 13 files changed, 60 insertions(+), 148 deletions(-) delete mode 100644 LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/JsonRequestAdapter.java delete mode 100644 LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/JsonResultAdapter.java create mode 100644 LauncherCore/src/main/java/pro/gravit/utils/TypeSerializeInterface.java diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/LaunchServerGsonManager.java b/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/LaunchServerGsonManager.java index d636f6e0..6b804245 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/LaunchServerGsonManager.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/LaunchServerGsonManager.java @@ -9,6 +9,7 @@ import pro.gravit.launcher.managers.GsonManager; import pro.gravit.launcher.request.JsonResultSerializeAdapter; import pro.gravit.launcher.request.WebSocketEvent; +import pro.gravit.launcher.request.websockets.ClientWebSocketService; import pro.gravit.launchserver.auth.handler.AuthHandler; import pro.gravit.launchserver.auth.hwid.HWIDHandler; import pro.gravit.launchserver.auth.permissions.PermissionsHandler; @@ -35,7 +36,7 @@ public void registerAdapters(GsonBuilder builder) { builder.registerTypeAdapter(DaoProvider.class, new UniversalJsonAdapter<>(DaoProvider.providers)); builder.registerTypeAdapter(HWID.class, new UniversalJsonAdapter<>(HWIDProvider.hwids)); builder.registerTypeAdapter(WebSocketServerResponse.class, new UniversalJsonAdapter<>(WebSocketService.providers)); - builder.registerTypeAdapter(HashedEntry.class, new HashedEntryAdapter()); builder.registerTypeAdapter(WebSocketEvent.class, new JsonResultSerializeAdapter()); + //ClientWebSocketService.appendTypeAdapters(builder); } } diff --git a/Launcher/src/main/java/pro/gravit/launcher/managers/ClientGsonManager.java b/Launcher/src/main/java/pro/gravit/launcher/managers/ClientGsonManager.java index bf2f2632..7e788093 100644 --- a/Launcher/src/main/java/pro/gravit/launcher/managers/ClientGsonManager.java +++ b/Launcher/src/main/java/pro/gravit/launcher/managers/ClientGsonManager.java @@ -5,6 +5,7 @@ import pro.gravit.launcher.client.UserSettings; import pro.gravit.launcher.hwid.HWID; import pro.gravit.launcher.hwid.HWIDProvider; +import pro.gravit.launcher.request.websockets.ClientWebSocketService; import pro.gravit.utils.UniversalJsonAdapter; public class ClientGsonManager extends GsonManager { @@ -13,5 +14,6 @@ public void registerAdapters(GsonBuilder builder) { super.registerAdapters(builder); builder.registerTypeAdapter(UserSettings.class, new UniversalJsonAdapter<>(UserSettings.providers)); builder.registerTypeAdapter(HWID.class, new UniversalJsonAdapter<>(HWIDProvider.hwids)); + ClientWebSocketService.appendTypeAdapters(builder); } } diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/hwid/HWIDProvider.java b/LauncherAPI/src/main/java/pro/gravit/launcher/hwid/HWIDProvider.java index 24deea4b..0b924e73 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/hwid/HWIDProvider.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/hwid/HWIDProvider.java @@ -7,5 +7,6 @@ public class HWIDProvider { public static void registerHWIDs() { hwids.register("oshi", OshiHWID.class); + hwids.register("no", NoHWID.class); } } diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/request/WebSocketEvent.java b/LauncherAPI/src/main/java/pro/gravit/launcher/request/WebSocketEvent.java index a4b9cfef..03895907 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/request/WebSocketEvent.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/request/WebSocketEvent.java @@ -1,5 +1,7 @@ package pro.gravit.launcher.request; -public interface WebSocketEvent { +import pro.gravit.utils.TypeSerializeInterface; + +public interface WebSocketEvent extends TypeSerializeInterface { String getType(); } diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/request/auth/AuthRequest.java b/LauncherAPI/src/main/java/pro/gravit/launcher/request/auth/AuthRequest.java index e3695bfb..9f26491c 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/request/auth/AuthRequest.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/request/auth/AuthRequest.java @@ -4,6 +4,7 @@ import pro.gravit.launcher.LauncherAPI; import pro.gravit.launcher.LauncherNetworkAPI; import pro.gravit.launcher.events.request.AuthRequestEvent; +import pro.gravit.launcher.hwid.OshiHWID; import pro.gravit.launcher.request.Request; import pro.gravit.launcher.request.websockets.WebSocketRequest; import pro.gravit.utils.helper.VerifyHelper; diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/ClientWebSocketService.java b/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/ClientWebSocketService.java index afba8c8c..de2de7d2 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/ClientWebSocketService.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/ClientWebSocketService.java @@ -11,36 +11,39 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import pro.gravit.launcher.Launcher; import pro.gravit.launcher.events.ExceptionEvent; import pro.gravit.launcher.events.request.*; import pro.gravit.launcher.hasher.HashedEntry; import pro.gravit.launcher.hasher.HashedEntryAdapter; import pro.gravit.launcher.request.WebSocketEvent; +import pro.gravit.utils.ProviderMap; +import pro.gravit.utils.UniversalJsonAdapter; import pro.gravit.utils.helper.LogHelper; public class ClientWebSocketService extends ClientJSONPoint { - public final GsonBuilder gsonBuilder; public final Gson gson; public OnCloseCallback onCloseCallback; public final Boolean onConnect; public ReconnectCallback reconnectCallback; - private HashMap> requests; - private HashMap> results; + public static ProviderMap results = new ProviderMap<>(); + public static ProviderMap requests = new ProviderMap<>(); private HashSet handlers; - public ClientWebSocketService(GsonBuilder gsonBuilder, String address, int i) throws SSLException { + public ClientWebSocketService(String address, int i) throws SSLException { super(createURL(address)); - requests = new HashMap<>(); - results = new HashMap<>(); handlers = new HashSet<>(); - this.gsonBuilder = gsonBuilder; - this.gsonBuilder.registerTypeAdapter(WebSocketRequest.class, new JsonRequestAdapter(this)); - this.gsonBuilder.registerTypeAdapter(WebSocketEvent.class, new JsonResultAdapter(this)); - this.gsonBuilder.registerTypeAdapter(HashedEntry.class, new HashedEntryAdapter()); - this.gson = gsonBuilder.create(); + this.gson = Launcher.gsonManager.gson; this.onConnect = true; } + public static void appendTypeAdapters(GsonBuilder builder) + { + builder.registerTypeAdapter(HashedEntry.class, new HashedEntryAdapter()); + builder.registerTypeAdapter(WebSocketEvent.class, new UniversalJsonAdapter<>(ClientWebSocketService.results)); + builder.registerTypeAdapter(WebSocketRequest.class, new UniversalJsonAdapter<>(ClientWebSocketService.requests)); + } + private static URI createURL(String address) { try { URI u = new URI(address); @@ -81,47 +84,31 @@ public interface ReconnectCallback { void onReconnect() throws IOException; } - public Class getRequestClass(String key) { - return requests.get(key); - } - - public Class getResultClass(String key) { - return results.get(key); - } - - public void registerRequest(String key, Class clazz) { - requests.put(key, clazz); - } - public void registerRequests() { } - public void registerResult(String key, Class clazz) { - results.put(key, clazz); - } - public void registerResults() { - registerResult("auth", AuthRequestEvent.class); - registerResult("checkServer", CheckServerRequestEvent.class); - registerResult("joinServer", JoinServerRequestEvent.class); - registerResult("launcher", LauncherRequestEvent.class); - registerResult("profileByUsername", ProfileByUsernameRequestEvent.class); - registerResult("profileByUUID", ProfileByUUIDRequestEvent.class); - registerResult("batchProfileByUsername", BatchProfileByUsernameRequestEvent.class); - registerResult("profiles", ProfilesRequestEvent.class); - registerResult("setProfile", SetProfileRequestEvent.class); - registerResult("updateList", UpdateListRequestEvent.class); - registerResult("error", ErrorRequestEvent.class); - registerResult("update", UpdateRequestEvent.class); - registerResult("restoreSession", RestoreSessionRequestEvent.class); - registerResult("getSecureToken", GetSecureTokenRequestEvent.class); - registerResult("verifySecureToken", VerifySecureTokenRequestEvent.class); - registerResult("log", LogEvent.class); - registerResult("cmdExec", ExecCommandRequestEvent.class); - registerResult("getAvailabilityAuth", GetAvailabilityAuthRequestEvent.class); - registerResult("exception", ExceptionEvent.class); - registerResult("register", RegisterRequestEvent.class); + results.register("auth", AuthRequestEvent.class); + results.register("checkServer", CheckServerRequestEvent.class); + results.register("joinServer", JoinServerRequestEvent.class); + results.register("launcher", LauncherRequestEvent.class); + results.register("profileByUsername", ProfileByUsernameRequestEvent.class); + results.register("profileByUUID", ProfileByUUIDRequestEvent.class); + results.register("batchProfileByUsername", BatchProfileByUsernameRequestEvent.class); + results.register("profiles", ProfilesRequestEvent.class); + results.register("setProfile", SetProfileRequestEvent.class); + results.register("updateList", UpdateListRequestEvent.class); + results.register("error", ErrorRequestEvent.class); + results.register("update", UpdateRequestEvent.class); + results.register("restoreSession", RestoreSessionRequestEvent.class); + results.register("getSecureToken", GetSecureTokenRequestEvent.class); + results.register("verifySecureToken", VerifySecureTokenRequestEvent.class); + results.register("log", LogEvent.class); + results.register("cmdExec", ExecCommandRequestEvent.class); + results.register("getAvailabilityAuth", GetAvailabilityAuthRequestEvent.class); + results.register("exception", ExceptionEvent.class); + results.register("register", RegisterRequestEvent.class); } public void registerHandler(EventHandler eventHandler) { diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/JsonRequestAdapter.java b/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/JsonRequestAdapter.java deleted file mode 100644 index 18946738..00000000 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/JsonRequestAdapter.java +++ /dev/null @@ -1,46 +0,0 @@ -package pro.gravit.launcher.request.websockets; - -import java.lang.reflect.Type; - -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonDeserializer; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; -import com.google.gson.JsonPrimitive; -import com.google.gson.JsonSerializationContext; -import com.google.gson.JsonSerializer; - -import pro.gravit.utils.helper.LogHelper; - -public class JsonRequestAdapter implements JsonSerializer, JsonDeserializer { - private final ClientWebSocketService service; - private static final String PROP_NAME = "type"; - - public JsonRequestAdapter(ClientWebSocketService service) { - this.service = service; - } - - @Override - public WebSocketRequest deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { - String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString(); - Class cls = service.getRequestClass(typename); - if (cls == null) { - LogHelper.error("Request type %s not found", typename); - } - - - return (WebSocketRequest) context.deserialize(json, cls); - } - - @Override - public JsonElement serialize(WebSocketRequest src, Type typeOfSrc, JsonSerializationContext context) { - // note : won't work, you must delegate this - JsonObject jo = context.serialize(src).getAsJsonObject(); - - String classPath = src.getType(); - jo.add(PROP_NAME, new JsonPrimitive(classPath)); - - return jo; - } -} diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/JsonResultAdapter.java b/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/JsonResultAdapter.java deleted file mode 100644 index 2c057dd7..00000000 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/JsonResultAdapter.java +++ /dev/null @@ -1,47 +0,0 @@ -package pro.gravit.launcher.request.websockets; - -import java.lang.reflect.Type; - -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonDeserializer; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; -import com.google.gson.JsonPrimitive; -import com.google.gson.JsonSerializationContext; -import com.google.gson.JsonSerializer; - -import pro.gravit.launcher.request.WebSocketEvent; -import pro.gravit.utils.helper.LogHelper; - -public class JsonResultAdapter implements JsonSerializer, JsonDeserializer { - private final ClientWebSocketService service; - private static final String PROP_NAME = "type"; - - public JsonResultAdapter(ClientWebSocketService service) { - this.service = service; - } - - @Override - public WebSocketEvent deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { - String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString(); - Class cls = service.getResultClass(typename); - if (cls == null) { - LogHelper.error("Result type %s not found", typename); - } - - - return (WebSocketEvent) context.deserialize(json, cls); - } - - @Override - public JsonElement serialize(WebSocketEvent src, Type typeOfSrc, JsonSerializationContext context) { - // note : won't work, you must delegate this - JsonObject jo = context.serialize(src).getAsJsonObject(); - - String classPath = src.getType(); - jo.add(PROP_NAME, new JsonPrimitive(classPath)); - - return jo; - } -} diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/StandartClientWebSocketService.java b/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/StandartClientWebSocketService.java index 71161e8e..79453c73 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/StandartClientWebSocketService.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/StandartClientWebSocketService.java @@ -20,8 +20,8 @@ public class StandartClientWebSocketService extends ClientWebSocketService { public WaitEventHandler waitEventHandler = new WaitEventHandler(); - public StandartClientWebSocketService(GsonBuilder gsonBuilder, String address, int i) throws SSLException { - super(gsonBuilder, address, i); + public StandartClientWebSocketService(String address, int i) throws SSLException { + super(address, i); } public class RequestFuture implements Future { @@ -109,7 +109,7 @@ public RequestFuture asyncSendRequest(WebSocketRequest request) throws IOExcepti public static StandartClientWebSocketService initWebSockets(String address, boolean async) { StandartClientWebSocketService service; try { - service = new StandartClientWebSocketService(CommonHelper.newBuilder(), address, 5000); + service = new StandartClientWebSocketService(address, 5000); } catch (SSLException e) { throw new SecurityException(e); } diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/WebSocketRequest.java b/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/WebSocketRequest.java index a98a592c..05d3e899 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/WebSocketRequest.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/WebSocketRequest.java @@ -1,5 +1,7 @@ package pro.gravit.launcher.request.websockets; -public interface WebSocketRequest { +import pro.gravit.utils.TypeSerializeInterface; + +public interface WebSocketRequest extends TypeSerializeInterface { String getType(); } diff --git a/LauncherCore/src/main/java/pro/gravit/utils/TypeSerializeInterface.java b/LauncherCore/src/main/java/pro/gravit/utils/TypeSerializeInterface.java new file mode 100644 index 00000000..41bd9bfe --- /dev/null +++ b/LauncherCore/src/main/java/pro/gravit/utils/TypeSerializeInterface.java @@ -0,0 +1,5 @@ +package pro.gravit.utils; + +public interface TypeSerializeInterface { + String getType(); +} diff --git a/LauncherCore/src/main/java/pro/gravit/utils/UniversalJsonAdapter.java b/LauncherCore/src/main/java/pro/gravit/utils/UniversalJsonAdapter.java index 0fcd1283..e5b0204a 100644 --- a/LauncherCore/src/main/java/pro/gravit/utils/UniversalJsonAdapter.java +++ b/LauncherCore/src/main/java/pro/gravit/utils/UniversalJsonAdapter.java @@ -41,11 +41,13 @@ public R deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext } @Override + @SuppressWarnings("unchecked") public JsonElement serialize(R src, Type typeOfSrc, JsonSerializationContext context) { JsonObject jo = context.serialize(src).getAsJsonObject(); - - @SuppressWarnings("unchecked") String classPath = providerMap.getName((Class) src.getClass()); + if (classPath == null && src instanceof TypeSerializeInterface) { + classPath = ((TypeSerializeInterface) src).getType(); + } jo.add(PROP_NAME, new JsonPrimitive(classPath)); return jo; diff --git a/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapperGsonManager.java b/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapperGsonManager.java index b67da826..b3ee9806 100644 --- a/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapperGsonManager.java +++ b/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapperGsonManager.java @@ -3,10 +3,12 @@ import com.google.gson.GsonBuilder; import pro.gravit.launcher.managers.GsonManager; +import pro.gravit.launcher.request.websockets.ClientWebSocketService; public class ServerWrapperGsonManager extends GsonManager { @Override public void registerAdapters(GsonBuilder builder) { super.registerAdapters(builder); + ClientWebSocketService.appendTypeAdapters(builder); } }