[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.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);
}
}

View file

@ -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);
}
}

View file

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

View file

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

View file

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

View file

@ -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<String, Class<? extends WebSocketRequest>> requests;
private HashMap<String, Class<? extends WebSocketEvent>> results;
public static ProviderMap<WebSocketEvent> results = new ProviderMap<>();
public static ProviderMap<WebSocketRequest> requests = new ProviderMap<>();
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));
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<? 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 registerResult(String key, Class<? extends WebSocketEvent> 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) {

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 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<WebSocketEvent> {
@ -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);
}

View file

@ -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();
}

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
@SuppressWarnings("unchecked")
public JsonElement serialize(R src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject jo = context.serialize(src).getAsJsonObject();
@SuppressWarnings("unchecked")
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));
return jo;

View file

@ -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);
}
}