Сериализация HashedDir в json

This commit is contained in:
Gravit 2018-10-02 20:25:07 +07:00
parent 788eb253f4
commit f27ec8cb7f
5 changed files with 63 additions and 10 deletions

View file

@ -330,7 +330,7 @@ public static void main(String... args) throws Throwable {
// Updates and profiles
private volatile List<SignedObjectHolder<ClientProfile>> profilesList;
private volatile Map<String, SignedObjectHolder<HashedDir>> updatesDirMap;
public volatile Map<String, SignedObjectHolder<HashedDir>> updatesDirMap;
public LaunchServer(Path dir, boolean portable) throws IOException, InvalidKeySpecException {
//setScriptBindings();

View file

@ -3,6 +3,7 @@
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.command.Command;
import ru.gravit.launchserver.socket.NettyServerSocketHandler;
import ru.gravit.launchserver.socket.websocket.WebSocketService;
import ru.gravit.utils.helper.CommonHelper;
public class TestCommand extends Command {

View file

@ -7,9 +7,11 @@
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.group.ChannelGroup;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import ru.gravit.launcher.hasher.HashedEntry;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.socket.Client;
import ru.gravit.launchserver.socket.websocket.json.EchoResponse;
import ru.gravit.launchserver.socket.websocket.json.HashedEntryAdapter;
import ru.gravit.launchserver.socket.websocket.json.JsonResponseAdapter;
import ru.gravit.launchserver.socket.websocket.json.JsonResponseInterface;
import ru.gravit.launchserver.socket.websocket.json.auth.AuthResponse;
@ -28,6 +30,7 @@ public WebSocketService(ChannelGroup channels, LaunchServer server, GsonBuilder
this.gsonBuiler = gson;
this.
gsonBuiler.registerTypeAdapter(JsonResponseInterface.class,new JsonResponseAdapter(this));
gsonBuiler.registerTypeAdapter(HashedEntry.class,new HashedEntryAdapter(this));
this.gson = gsonBuiler.create();
}

View file

@ -0,0 +1,44 @@
package ru.gravit.launchserver.socket.websocket.json;
import com.google.gson.*;
import ru.gravit.launcher.hasher.HashedDir;
import ru.gravit.launcher.hasher.HashedEntry;
import ru.gravit.launcher.hasher.HashedFile;
import ru.gravit.launchserver.socket.websocket.WebSocketService;
import java.lang.reflect.Type;
public class HashedEntryAdapter implements JsonSerializer<HashedEntry>, JsonDeserializer<HashedEntry> {
private final WebSocketService service;
private static final String PROP_NAME = "type";
public HashedEntryAdapter(WebSocketService service) {
this.service = service;
}
@Override
public HashedEntry deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString();
Class cls = null;
if(typename.equals("dir")) cls = HashedDir.class;
if(typename.equals("file")) cls = HashedFile.class;
return (HashedEntry) context.deserialize(json, cls);
}
@Override
public JsonElement serialize(HashedEntry src, Type typeOfSrc, JsonSerializationContext context) {
// note : won't work, you must delegate this
JsonObject jo = context.serialize(src).getAsJsonObject();
HashedEntry.Type type = src.getType();
if(type == HashedEntry.Type.DIR)
jo.add(PROP_NAME, new JsonPrimitive("dir"));
if(type == HashedEntry.Type.FILE)
jo.add(PROP_NAME, new JsonPrimitive("file"));
return jo;
}
}

View file

@ -14,6 +14,9 @@ public class LauncherResponse implements JsonResponseInterface {
public Version version;
public String hash;
public int launcher_type;
//REPLACED TO REAL URL
public static final String JAR_URL = "http://localhost:9752/Launcher.jar";
public static final String EXE_URL = "http://localhost:9752/Launcher.exe";
@Override
public String getType() {
return "launcherUpdate";
@ -25,24 +28,24 @@ public void execute(WebSocketService service, ChannelHandlerContext ctx, Client
if(launcher_type == 1) // JAR
{
byte[] hash = LaunchServer.server.launcherBinary.getHash();
if(hash == null) service.sendObjectAndClose(ctx, new Result(true));
if(Arrays.equals(bytes, hash)) //REPLACE REAL HASH
if(hash == null) service.sendObjectAndClose(ctx, new Result(true,JAR_URL));
if(Arrays.equals(bytes, hash))
{
service.sendObject(ctx, new Result(false));
service.sendObject(ctx, new Result(false,JAR_URL));
} else
{
service.sendObjectAndClose(ctx, new Result(true));
service.sendObjectAndClose(ctx, new Result(true,JAR_URL));
}
} else if(launcher_type == 2) //EXE
{
byte[] hash = LaunchServer.server.launcherEXEBinary.getHash();
if(hash == null) service.sendObjectAndClose(ctx, new Result(true));
if(Arrays.equals(bytes, hash)) //REPLACE REAL HASH
if(hash == null) service.sendObjectAndClose(ctx, new Result(true,EXE_URL));
if(Arrays.equals(bytes, hash))
{
service.sendObject(ctx, new Result(false));
service.sendObject(ctx, new Result(false,EXE_URL));
} else
{
service.sendObjectAndClose(ctx, new Result(true));
service.sendObjectAndClose(ctx, new Result(true,EXE_URL));
}
} else service.sendObject(ctx, new WebSocketService.ErrorResult("Request launcher type error"));
@ -51,9 +54,11 @@ public class Result
{
public String type = "success";
public String requesttype = "launcherUpdate";
public String url;
public Result(boolean needUpdate) {
public Result(boolean needUpdate,String url) {
this.needUpdate = needUpdate;
this.url = url;
}
public boolean needUpdate;