From 2dba254528151b979cd94f256aa08d3d815ed661 Mon Sep 17 00:00:00 2001 From: NotRyken <127091011+NotRyken@users.noreply.github.com> Date: Thu, 2 Jan 2025 09:46:28 +0800 Subject: [PATCH 1/3] Fix chat log serialization --- gradle.properties | 2 +- .../advancedchatlog/util/LogChatMessageSerializer.java | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/gradle.properties b/gradle.properties index e64c090..a9b414c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,7 +6,7 @@ yarn_mappings=1.21.3+build.2 loader_version=0.15.11 fabric_api_version=0.112.1+1.21.3 -mod_version=1.2.5 +mod_version=1.2.6 maven_group=com.arematics archives_base_name=AdvancedChatLog diff --git a/src/main/java/io/github/darkkronicle/advancedchatlog/util/LogChatMessageSerializer.java b/src/main/java/io/github/darkkronicle/advancedchatlog/util/LogChatMessageSerializer.java index 49fa34d..a6c4d10 100644 --- a/src/main/java/io/github/darkkronicle/advancedchatlog/util/LogChatMessageSerializer.java +++ b/src/main/java/io/github/darkkronicle/advancedchatlog/util/LogChatMessageSerializer.java @@ -72,13 +72,14 @@ public LogChatMessage load(JsonObject obj) { @Override public JsonObject save(LogChatMessage message) { + RegistryWrapper.WrapperLookup wrapperLookup = BuiltinRegistries.createWrapperLookup(); JsonObject json = new JsonObject(); ChatMessage chat = message.getMessage(); LocalDateTime dateTime = LocalDateTime.of(message.getDate(), chat.getTime()); json.addProperty("time", formatter.format(dateTime)); json.addProperty("stacks", chat.getStacks()); - json.add("display", GSON.toJsonTree(chat.getDisplayText())); - json.add("original", GSON.toJsonTree(chat.getOriginalText())); + json.addProperty("display", Text.Serialization.toJsonString(chat.getDisplayText(), wrapperLookup)); + json.addProperty("original", Text.Serialization.toJsonString(chat.getOriginalText(), wrapperLookup)); return json; } } From cecb19eb7cade47742d3bcb117a8260fe0c0131e Mon Sep 17 00:00:00 2001 From: NotRyken <127091011+NotRyken@users.noreply.github.com> Date: Thu, 2 Jan 2025 09:48:37 +0800 Subject: [PATCH 2/3] Update license headers --- .../github/darkkronicle/advancedchatlog/gui/ChatLogScreen.java | 3 +-- .../advancedchatlog/util/LogChatMessageSerializer.java | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/github/darkkronicle/advancedchatlog/gui/ChatLogScreen.java b/src/main/java/io/github/darkkronicle/advancedchatlog/gui/ChatLogScreen.java index 6d846da..86ba622 100644 --- a/src/main/java/io/github/darkkronicle/advancedchatlog/gui/ChatLogScreen.java +++ b/src/main/java/io/github/darkkronicle/advancedchatlog/gui/ChatLogScreen.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021-2024 DarkKronicle + * Copyright (C) 2021-2025 DarkKronicle * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this @@ -32,7 +32,6 @@ import net.fabricmc.api.Environment; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.DrawContext; -import net.minecraft.client.util.math.MatrixStack; import net.minecraft.text.Style; import net.minecraft.text.Text; import net.minecraft.text.TextColor; diff --git a/src/main/java/io/github/darkkronicle/advancedchatlog/util/LogChatMessageSerializer.java b/src/main/java/io/github/darkkronicle/advancedchatlog/util/LogChatMessageSerializer.java index a6c4d10..8c6bb2c 100644 --- a/src/main/java/io/github/darkkronicle/advancedchatlog/util/LogChatMessageSerializer.java +++ b/src/main/java/io/github/darkkronicle/advancedchatlog/util/LogChatMessageSerializer.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021-2024 DarkKronicle + * Copyright (C) 2021-2025 DarkKronicle * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this From f673433ff152d7d89deaa02417442e3bcf90ad47 Mon Sep 17 00:00:00 2001 From: NotRyken <127091011+NotRyken@users.noreply.github.com> Date: Fri, 7 Feb 2025 16:34:50 +0800 Subject: [PATCH 3/3] Fix chat log serialization (again) --- gradle.properties | 2 +- .../config/ChatLogConfigStorage.java | 10 +++- .../util/LogChatMessageSerializer.java | 58 ++++++++++++++----- 3 files changed, 54 insertions(+), 16 deletions(-) diff --git a/gradle.properties b/gradle.properties index a9b414c..ab3fc6b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,7 +6,7 @@ yarn_mappings=1.21.3+build.2 loader_version=0.15.11 fabric_api_version=0.112.1+1.21.3 -mod_version=1.2.6 +mod_version=1.2.7 maven_group=com.arematics archives_base_name=AdvancedChatLog diff --git a/src/main/java/io/github/darkkronicle/advancedchatlog/config/ChatLogConfigStorage.java b/src/main/java/io/github/darkkronicle/advancedchatlog/config/ChatLogConfigStorage.java index c174ded..9aa20f1 100644 --- a/src/main/java/io/github/darkkronicle/advancedchatlog/config/ChatLogConfigStorage.java +++ b/src/main/java/io/github/darkkronicle/advancedchatlog/config/ChatLogConfigStorage.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021 DarkKronicle + * Copyright (C) 2021-2025 DarkKronicle * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this @@ -11,6 +11,7 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; +import com.mojang.logging.LogUtils; import fi.dy.masa.malilib.config.IConfigBase; import fi.dy.masa.malilib.config.IConfigHandler; import fi.dy.masa.malilib.config.options.*; @@ -28,6 +29,7 @@ import java.util.List; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; +import net.minecraft.util.Util; @Environment(EnvType.CLIENT) public class ChatLogConfigStorage implements IConfigHandler { @@ -205,11 +207,17 @@ public static void saveFromFile() { @Override public void load() { + LogUtils.getLogger().info("[AdvancedChatLog] Loading..."); + long time = Util.getMeasuringTimeMs(); loadFromFile(); + LogUtils.getLogger().info("[AdvancedChatLog] Load completed in {}ms", Util.getMeasuringTimeMs() - time); } @Override public void save() { + LogUtils.getLogger().info("[AdvancedChatLog] Saving..."); + long time = Util.getMeasuringTimeMs(); saveFromFile(); + LogUtils.getLogger().info("[AdvancedChatLog] Save completed in {}ms", Util.getMeasuringTimeMs() - time); } } diff --git a/src/main/java/io/github/darkkronicle/advancedchatlog/util/LogChatMessageSerializer.java b/src/main/java/io/github/darkkronicle/advancedchatlog/util/LogChatMessageSerializer.java index 8c6bb2c..67a6583 100644 --- a/src/main/java/io/github/darkkronicle/advancedchatlog/util/LogChatMessageSerializer.java +++ b/src/main/java/io/github/darkkronicle/advancedchatlog/util/LogChatMessageSerializer.java @@ -7,11 +7,11 @@ */ package io.github.darkkronicle.advancedchatlog.util; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; import io.github.darkkronicle.advancedchatcore.chat.ChatMessage; import io.github.darkkronicle.advancedchatcore.interfaces.IJsonSave; +import io.github.darkkronicle.advancedchatlog.AdvancedChatLog; import io.github.darkkronicle.advancedchatlog.config.ChatLogConfigStorage; import java.time.LocalDate; import java.time.LocalDateTime; @@ -19,14 +19,13 @@ import java.time.format.DateTimeFormatter; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; -import net.minecraft.registry.BuiltinRegistries; -import net.minecraft.registry.RegistryWrapper; +import net.minecraft.registry.DynamicRegistryManager; import net.minecraft.text.Style; import net.minecraft.text.Text; @Environment(EnvType.CLIENT) public class LogChatMessageSerializer implements IJsonSave { - private static final Gson GSON = (new GsonBuilder()).disableHtmlEscaping().create(); + private final Text.Serializer serializer = new Text.Serializer(DynamicRegistryManager.EMPTY); private final DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; public LogChatMessageSerializer() {} @@ -51,15 +50,30 @@ private Text transfer(Text text) { return base; } + private Style forceCleanStyle(Style style) { + style = style.withClickEvent(null); + style = style.withHoverEvent(null); + style = style.withInsertion(null); + return style; + } + + private Text forceTransfer(Text text) { + // Using the built in serializer LiteralText is required + Text base = Text.empty(); + for (Text t : text.getSiblings()) { + Text newT = Text.literal(t.getString()).fillStyle(forceCleanStyle(t.getStyle())); + base.getSiblings().add(newT); + } + return base; + } + @Override public LogChatMessage load(JsonObject obj) { - RegistryWrapper.WrapperLookup wrapperLookup = BuiltinRegistries.createWrapperLookup(); LocalDateTime dateTime = LocalDateTime.from(formatter.parse(obj.get("time").getAsString())); LocalDate date = dateTime.toLocalDate(); LocalTime time = dateTime.toLocalTime(); - - Text display = Text.Serialization.fromJson(obj.get("display").getAsString(), wrapperLookup); - Text original = Text.Serialization.fromJson(obj.get("original").getAsString(), wrapperLookup); + Text display = serializer.deserialize(obj.get("display"), Text.class, null); + Text original = serializer.deserialize(obj.get("original"), Text.class, null); int stacks = obj.get("stacks").getAsByte(); ChatMessage message = ChatMessage.builder() @@ -72,14 +86,30 @@ public LogChatMessage load(JsonObject obj) { @Override public JsonObject save(LogChatMessage message) { - RegistryWrapper.WrapperLookup wrapperLookup = BuiltinRegistries.createWrapperLookup(); JsonObject json = new JsonObject(); ChatMessage chat = message.getMessage(); LocalDateTime dateTime = LocalDateTime.of(message.getDate(), chat.getTime()); - json.addProperty("time", formatter.format(dateTime)); - json.addProperty("stacks", chat.getStacks()); - json.addProperty("display", Text.Serialization.toJsonString(chat.getDisplayText(), wrapperLookup)); - json.addProperty("original", Text.Serialization.toJsonString(chat.getOriginalText(), wrapperLookup)); + try { + json.addProperty("time", formatter.format(dateTime)); + json.addProperty("stacks", chat.getStacks()); + json.add("display", serializer.serialize(transfer(chat.getDisplayText()), Text.class, null)); + json.add("original", serializer.serialize(transfer(chat.getOriginalText()), Text.class, null)); + } catch (JsonParseException e) { + try { + AdvancedChatLog.LOGGER.warn("[AdvancedChatLog] Save Error 1: {}", e.getMessage()); + AdvancedChatLog.LOGGER.warn("Original Message:"); + AdvancedChatLog.LOGGER.warn(chat.getOriginalText().getString()); + AdvancedChatLog.LOGGER.warn(chat.getOriginalText().toString()); + json = new JsonObject(); + json.addProperty("time", formatter.format(dateTime)); + json.addProperty("stacks", chat.getStacks()); + json.add("display", serializer.serialize(forceTransfer(chat.getDisplayText()), Text.class, null)); + json.add("original", serializer.serialize(forceTransfer(chat.getOriginalText()), Text.class, null)); + } catch (Exception e2) { + AdvancedChatLog.LOGGER.warn("[AdvancedChatLog] Save Error 2", e2); + return new JsonObject(); + } + } return json; } }