[FIX] Исправление ошибок сериализации

This commit is contained in:
Gravit 2019-07-03 14:23:26 +07:00
parent ea9084037c
commit 6a5d110f9a
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
13 changed files with 60 additions and 148 deletions

View file

@ -9,6 +9,7 @@
import pro.gravit.launcher.managers.GsonManager; import pro.gravit.launcher.managers.GsonManager;
import pro.gravit.launcher.request.JsonResultSerializeAdapter; import pro.gravit.launcher.request.JsonResultSerializeAdapter;
import pro.gravit.launcher.request.WebSocketEvent; 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.handler.AuthHandler;
import pro.gravit.launchserver.auth.hwid.HWIDHandler; import pro.gravit.launchserver.auth.hwid.HWIDHandler;
import pro.gravit.launchserver.auth.permissions.PermissionsHandler; 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(DaoProvider.class, new UniversalJsonAdapter<>(DaoProvider.providers));
builder.registerTypeAdapter(HWID.class, new UniversalJsonAdapter<>(HWIDProvider.hwids)); builder.registerTypeAdapter(HWID.class, new UniversalJsonAdapter<>(HWIDProvider.hwids));
builder.registerTypeAdapter(WebSocketServerResponse.class, new UniversalJsonAdapter<>(WebSocketService.providers)); builder.registerTypeAdapter(WebSocketServerResponse.class, new UniversalJsonAdapter<>(WebSocketService.providers));
builder.registerTypeAdapter(HashedEntry.class, new HashedEntryAdapter());
builder.registerTypeAdapter(WebSocketEvent.class, new JsonResultSerializeAdapter()); builder.registerTypeAdapter(WebSocketEvent.class, new JsonResultSerializeAdapter());
//ClientWebSocketService.appendTypeAdapters(builder);
} }
} }

View file

@ -5,6 +5,7 @@
import pro.gravit.launcher.client.UserSettings; import pro.gravit.launcher.client.UserSettings;
import pro.gravit.launcher.hwid.HWID; import pro.gravit.launcher.hwid.HWID;
import pro.gravit.launcher.hwid.HWIDProvider; import pro.gravit.launcher.hwid.HWIDProvider;
import pro.gravit.launcher.request.websockets.ClientWebSocketService;
import pro.gravit.utils.UniversalJsonAdapter; import pro.gravit.utils.UniversalJsonAdapter;
public class ClientGsonManager extends GsonManager { public class ClientGsonManager extends GsonManager {
@ -13,5 +14,6 @@ public void registerAdapters(GsonBuilder builder) {
super.registerAdapters(builder); super.registerAdapters(builder);
builder.registerTypeAdapter(UserSettings.class, new UniversalJsonAdapter<>(UserSettings.providers)); builder.registerTypeAdapter(UserSettings.class, new UniversalJsonAdapter<>(UserSettings.providers));
builder.registerTypeAdapter(HWID.class, new UniversalJsonAdapter<>(HWIDProvider.hwids)); builder.registerTypeAdapter(HWID.class, new UniversalJsonAdapter<>(HWIDProvider.hwids));
ClientWebSocketService.appendTypeAdapters(builder);
} }
} }

View file

@ -7,5 +7,6 @@ public class HWIDProvider {
public static void registerHWIDs() public static void registerHWIDs()
{ {
hwids.register("oshi", OshiHWID.class); hwids.register("oshi", OshiHWID.class);
hwids.register("no", NoHWID.class);
} }
} }

View file

@ -1,5 +1,7 @@
package pro.gravit.launcher.request; package pro.gravit.launcher.request;
public interface WebSocketEvent { import pro.gravit.utils.TypeSerializeInterface;
public interface WebSocketEvent extends TypeSerializeInterface {
String getType(); String getType();
} }

View file

@ -4,6 +4,7 @@
import pro.gravit.launcher.LauncherAPI; import pro.gravit.launcher.LauncherAPI;
import pro.gravit.launcher.LauncherNetworkAPI; import pro.gravit.launcher.LauncherNetworkAPI;
import pro.gravit.launcher.events.request.AuthRequestEvent; import pro.gravit.launcher.events.request.AuthRequestEvent;
import pro.gravit.launcher.hwid.OshiHWID;
import pro.gravit.launcher.request.Request; import pro.gravit.launcher.request.Request;
import pro.gravit.launcher.request.websockets.WebSocketRequest; import pro.gravit.launcher.request.websockets.WebSocketRequest;
import pro.gravit.utils.helper.VerifyHelper; import pro.gravit.utils.helper.VerifyHelper;

View file

@ -11,36 +11,39 @@
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import pro.gravit.launcher.Launcher;
import pro.gravit.launcher.events.ExceptionEvent; import pro.gravit.launcher.events.ExceptionEvent;
import pro.gravit.launcher.events.request.*; import pro.gravit.launcher.events.request.*;
import pro.gravit.launcher.hasher.HashedEntry; import pro.gravit.launcher.hasher.HashedEntry;
import pro.gravit.launcher.hasher.HashedEntryAdapter; import pro.gravit.launcher.hasher.HashedEntryAdapter;
import pro.gravit.launcher.request.WebSocketEvent; import pro.gravit.launcher.request.WebSocketEvent;
import pro.gravit.utils.ProviderMap;
import pro.gravit.utils.UniversalJsonAdapter;
import pro.gravit.utils.helper.LogHelper; import pro.gravit.utils.helper.LogHelper;
public class ClientWebSocketService extends ClientJSONPoint { public class ClientWebSocketService extends ClientJSONPoint {
public final GsonBuilder gsonBuilder;
public final Gson gson; public final Gson gson;
public OnCloseCallback onCloseCallback; public OnCloseCallback onCloseCallback;
public final Boolean onConnect; public final Boolean onConnect;
public ReconnectCallback reconnectCallback; public ReconnectCallback reconnectCallback;
private HashMap<String, Class<? extends WebSocketRequest>> requests; public static ProviderMap<WebSocketEvent> results = new ProviderMap<>();
private HashMap<String, Class<? extends WebSocketEvent>> results; public static ProviderMap<WebSocketRequest> requests = new ProviderMap<>();
private HashSet<EventHandler> handlers; private HashSet<EventHandler> handlers;
public ClientWebSocketService(GsonBuilder gsonBuilder, String address, int i) throws SSLException { public ClientWebSocketService(String address, int i) throws SSLException {
super(createURL(address)); super(createURL(address));
requests = new HashMap<>();
results = new HashMap<>();
handlers = new HashSet<>(); handlers = new HashSet<>();
this.gsonBuilder = gsonBuilder; this.gson = Launcher.gsonManager.gson;
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.onConnect = true; 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) { private static URI createURL(String address) {
try { try {
URI u = new URI(address); URI u = new URI(address);
@ -81,47 +84,31 @@ public interface ReconnectCallback {
void onReconnect() throws IOException; void onReconnect() throws IOException;
} }
public Class<? extends WebSocketRequest> getRequestClass(String key) {
return requests.get(key);
}
public Class<? extends WebSocketEvent> getResultClass(String key) {
return results.get(key);
}
public void registerRequest(String key, Class<? extends WebSocketRequest> clazz) {
requests.put(key, clazz);
}
public void registerRequests() { public void registerRequests() {
} }
public void registerResult(String key, Class<? extends WebSocketEvent> clazz) {
results.put(key, clazz);
}
public void registerResults() { public void registerResults() {
registerResult("auth", AuthRequestEvent.class); results.register("auth", AuthRequestEvent.class);
registerResult("checkServer", CheckServerRequestEvent.class); results.register("checkServer", CheckServerRequestEvent.class);
registerResult("joinServer", JoinServerRequestEvent.class); results.register("joinServer", JoinServerRequestEvent.class);
registerResult("launcher", LauncherRequestEvent.class); results.register("launcher", LauncherRequestEvent.class);
registerResult("profileByUsername", ProfileByUsernameRequestEvent.class); results.register("profileByUsername", ProfileByUsernameRequestEvent.class);
registerResult("profileByUUID", ProfileByUUIDRequestEvent.class); results.register("profileByUUID", ProfileByUUIDRequestEvent.class);
registerResult("batchProfileByUsername", BatchProfileByUsernameRequestEvent.class); results.register("batchProfileByUsername", BatchProfileByUsernameRequestEvent.class);
registerResult("profiles", ProfilesRequestEvent.class); results.register("profiles", ProfilesRequestEvent.class);
registerResult("setProfile", SetProfileRequestEvent.class); results.register("setProfile", SetProfileRequestEvent.class);
registerResult("updateList", UpdateListRequestEvent.class); results.register("updateList", UpdateListRequestEvent.class);
registerResult("error", ErrorRequestEvent.class); results.register("error", ErrorRequestEvent.class);
registerResult("update", UpdateRequestEvent.class); results.register("update", UpdateRequestEvent.class);
registerResult("restoreSession", RestoreSessionRequestEvent.class); results.register("restoreSession", RestoreSessionRequestEvent.class);
registerResult("getSecureToken", GetSecureTokenRequestEvent.class); results.register("getSecureToken", GetSecureTokenRequestEvent.class);
registerResult("verifySecureToken", VerifySecureTokenRequestEvent.class); results.register("verifySecureToken", VerifySecureTokenRequestEvent.class);
registerResult("log", LogEvent.class); results.register("log", LogEvent.class);
registerResult("cmdExec", ExecCommandRequestEvent.class); results.register("cmdExec", ExecCommandRequestEvent.class);
registerResult("getAvailabilityAuth", GetAvailabilityAuthRequestEvent.class); results.register("getAvailabilityAuth", GetAvailabilityAuthRequestEvent.class);
registerResult("exception", ExceptionEvent.class); results.register("exception", ExceptionEvent.class);
registerResult("register", RegisterRequestEvent.class); results.register("register", RegisterRequestEvent.class);
} }
public void registerHandler(EventHandler eventHandler) { public void registerHandler(EventHandler eventHandler) {

View file

@ -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<WebSocketRequest>, JsonDeserializer<WebSocketRequest> {
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<? extends WebSocketRequest> 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;
}
}

View file

@ -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<WebSocketEvent>, JsonDeserializer<WebSocketEvent> {
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<? extends WebSocketEvent> 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;
}
}

View file

@ -20,8 +20,8 @@
public class StandartClientWebSocketService extends ClientWebSocketService { public class StandartClientWebSocketService extends ClientWebSocketService {
public WaitEventHandler waitEventHandler = new WaitEventHandler(); public WaitEventHandler waitEventHandler = new WaitEventHandler();
public StandartClientWebSocketService(GsonBuilder gsonBuilder, String address, int i) throws SSLException { public StandartClientWebSocketService(String address, int i) throws SSLException {
super(gsonBuilder, address, i); super(address, i);
} }
public class RequestFuture implements Future<WebSocketEvent> { public class RequestFuture implements Future<WebSocketEvent> {
@ -109,7 +109,7 @@ public RequestFuture asyncSendRequest(WebSocketRequest request) throws IOExcepti
public static StandartClientWebSocketService initWebSockets(String address, boolean async) { public static StandartClientWebSocketService initWebSockets(String address, boolean async) {
StandartClientWebSocketService service; StandartClientWebSocketService service;
try { try {
service = new StandartClientWebSocketService(CommonHelper.newBuilder(), address, 5000); service = new StandartClientWebSocketService(address, 5000);
} catch (SSLException e) { } catch (SSLException e) {
throw new SecurityException(e); throw new SecurityException(e);
} }

View file

@ -1,5 +1,7 @@
package pro.gravit.launcher.request.websockets; package pro.gravit.launcher.request.websockets;
public interface WebSocketRequest { import pro.gravit.utils.TypeSerializeInterface;
public interface WebSocketRequest extends TypeSerializeInterface {
String getType(); String getType();
} }

View file

@ -0,0 +1,5 @@
package pro.gravit.utils;
public interface TypeSerializeInterface {
String getType();
}

View file

@ -41,11 +41,13 @@ public R deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext
} }
@Override @Override
@SuppressWarnings("unchecked")
public JsonElement serialize(R src, Type typeOfSrc, JsonSerializationContext context) { public JsonElement serialize(R src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject jo = context.serialize(src).getAsJsonObject(); JsonObject jo = context.serialize(src).getAsJsonObject();
@SuppressWarnings("unchecked")
String classPath = providerMap.getName((Class<? extends R>) src.getClass()); String classPath = providerMap.getName((Class<? extends R>) src.getClass());
if (classPath == null && src instanceof TypeSerializeInterface) {
classPath = ((TypeSerializeInterface) src).getType();
}
jo.add(PROP_NAME, new JsonPrimitive(classPath)); jo.add(PROP_NAME, new JsonPrimitive(classPath));
return jo; return jo;

View file

@ -3,10 +3,12 @@
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import pro.gravit.launcher.managers.GsonManager; import pro.gravit.launcher.managers.GsonManager;
import pro.gravit.launcher.request.websockets.ClientWebSocketService;
public class ServerWrapperGsonManager extends GsonManager { public class ServerWrapperGsonManager extends GsonManager {
@Override @Override
public void registerAdapters(GsonBuilder builder) { public void registerAdapters(GsonBuilder builder) {
super.registerAdapters(builder); super.registerAdapters(builder);
ClientWebSocketService.appendTypeAdapters(builder);
} }
} }