From d8fe269327a1a51f2588a3573a4764613da16388 Mon Sep 17 00:00:00 2001 From: Trumeet Date: Tue, 26 Jul 2022 19:06:11 -0700 Subject: Move the mod to mod/ --- .../moe/ymc/acron/serialization/Serializer.java | 60 ++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 mod/src/main/java/moe/ymc/acron/serialization/Serializer.java (limited to 'mod/src/main/java/moe/ymc/acron/serialization/Serializer.java') diff --git a/mod/src/main/java/moe/ymc/acron/serialization/Serializer.java b/mod/src/main/java/moe/ymc/acron/serialization/Serializer.java new file mode 100644 index 0000000..8091c25 --- /dev/null +++ b/mod/src/main/java/moe/ymc/acron/serialization/Serializer.java @@ -0,0 +1,60 @@ +package moe.ymc.acron.serialization; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonParseException; +import com.google.gson.typeadapters.RuntimeTypeAdapterFactory; +import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; +import moe.ymc.acron.c2s.ReqCmd; +import moe.ymc.acron.c2s.ReqSetConfig; +import moe.ymc.acron.c2s.Request; +import moe.ymc.acron.common.Vec2f; +import moe.ymc.acron.common.Vec3d; +import moe.ymc.acron.s2c.Event; +import moe.ymc.acron.s2c.event.*; +import moe.ymc.acron.s2c.response.EventCmdOut; +import moe.ymc.acron.s2c.response.EventCmdRes; +import moe.ymc.acron.s2c.response.EventError; +import moe.ymc.acron.s2c.response.EventOk; +import org.jetbrains.annotations.NotNull; + +public class Serializer { + @NotNull + public static Request deserialize(@NotNull TextWebSocketFrame frame) + throws JsonParseException, IllegalArgumentException, IllegalStateException { + final String text = frame.text(); + final RuntimeTypeAdapterFactory adapter = + RuntimeTypeAdapterFactory.of(Request.class, "type") + .registerSubtype(ReqSetConfig.class, "set_config") + .registerSubtype(ReqCmd.class, "cmd"); + final Gson gson = new GsonBuilder() + .registerTypeAdapter(ReqSetConfig.class, new ReqSetConfig.ReqSetConfigDeserializer()) + .registerTypeAdapter(Vec3d.class, new Vec3d.Vec3dDeserializer()) + .registerTypeAdapter(Vec2f.class, new Vec2f.Vec2fDeserializer()) + .registerTypeAdapter(ReqCmd.class, new ReqCmd.ReqCmdDeserializer()) + .registerTypeAdapterFactory(adapter) + .create(); + final Request request = gson.fromJson(text, Request.class); + request.validate(); + return request; + } + + @NotNull + public static TextWebSocketFrame serialize(@NotNull Event message) { + final RuntimeTypeAdapterFactory adapter = + RuntimeTypeAdapterFactory.of(Event.class, "type") + .registerSubtype(EventDisconnected.class, "disconnect") + .registerSubtype(EventPlayerMessage.class, "message") + .registerSubtype(EventPlayerJoined.class, "join") + .registerSubtype(EventEntityDeath.class, "death") + .registerSubtype(EventCmdOut.class, "cmd_out") + .registerSubtype(EventCmdRes.class, "cmd_result") + .registerSubtype(EventLagging.class, "lagging") + .registerSubtype(EventError.class, "error") + .registerSubtype(EventOk.class, "ok"); + final Gson gson = new GsonBuilder() + .registerTypeAdapterFactory(adapter) + .create(); + return new TextWebSocketFrame(gson.toJson(message, message.getClass())); + } +} -- cgit v1.2.3