9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-28 19:39:17 +00:00

Cleanup ChatImage protocol support & Further reduce worldgen allocation (#131)

* Cleanup protocol comment & Reduce worldgen allocations

* Cleanup protocol
This commit is contained in:
Kobe ⑧
2024-10-22 03:44:41 +08:00
committed by GitHub
parent e324f59a03
commit 5b0251fde8
3 changed files with 51 additions and 68 deletions

View File

@@ -27,10 +27,10 @@ index 9bff9c7fce4fec7687940f4212ac05d460ab2ab5..6f151c185850738a9f8a575f9c09e3c4
syncmaticaProtocol = config.getBoolean(getBasePath() + ".syncmatica-protocol", syncmaticaProtocol);
diff --git a/src/main/java/org/leavesmc/leaves/protocol/ChatImageProtocol.java b/src/main/java/org/leavesmc/leaves/protocol/ChatImageProtocol.java
new file mode 100644
index 0000000000000000000000000000000000000000..87ffe8a81a8bab7d20ff9551b105487d47616ee1
index 0000000000000000000000000000000000000000..5ef387ccfe19bb91bbcb926d44e7a01450035a1d
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/protocol/ChatImageProtocol.java
@@ -0,0 +1,160 @@
@@ -0,0 +1,140 @@
+package org.leavesmc.leaves.protocol;
+
+import com.google.common.collect.Lists;
@@ -83,9 +83,6 @@ index 0000000000000000000000000000000000000000..87ffe8a81a8bab7d20ff9551b105487d
+ }
+
+ public record DownloadFileChannelPacket(String message) implements LeavesCustomPayload<LeavesProtocolManager.LeavesPayload> {
+ /**
+ * 发送文件分块到客户端通道(Map)
+ */
+ private static final ResourceLocation DOWNLOAD_FILE_CHANNEL = ChatImageProtocol.id("download_file_channel");
+
+ @New
@@ -106,9 +103,6 @@ index 0000000000000000000000000000000000000000..87ffe8a81a8bab7d20ff9551b105487d
+ }
+
+ public record FileChannelPacket(String message) implements LeavesCustomPayload<LeavesProtocolManager.LeavesPayload> {
+ /**
+ * 客户端发送文件分块到服务器通道(Map)
+ */
+ private static final ResourceLocation FILE_CHANNEL = ChatImageProtocol.id("file_channel");
+
+ @New
@@ -130,55 +124,41 @@ index 0000000000000000000000000000000000000000..87ffe8a81a8bab7d20ff9551b105487d
+
+ @ProtocolHandler.PayloadReceiver(payload = FileChannelPacket.class, payloadId = "file_channel")
+ public static void serverFileChannelReceived(ServerPlayer player, String res) {
+ if (!ProtocolSupport.chatImageProtocol) return;
+ ChatImageIndex title = gson.fromJson(res, ChatImageIndex.class);
+ HashMap<Integer, String> blocks = SERVER_BLOCK_CACHE.containsKey(title.url) ? SERVER_BLOCK_CACHE.get(title.url) : new HashMap<>();
+ blocks.put(title.index, res);
+ SERVER_BLOCK_CACHE.put(title.url, blocks);
+ FILE_COUNT_MAP.put(title.url, title.total);
+ //System.out.println("[FileChannel->Server:" + title.index + "/" + title.total + "]" + title.url);
+ if (title.total == blocks.size()) {
+ if (USER_CACHE_MAP.containsKey(title.url)) {
+ // 通知之前请求但是没图片的客户端
+ List<String> names = USER_CACHE_MAP.get(title.url);
+ for (String uuid : names) {
+ ServerPlayer serverPlayer = player.server.getPlayerList().getPlayer(UUID.fromString(uuid));
+ if (serverPlayer != null) {
+ sendToPlayer(new FileInfoChannelPacket("true->" + title.url), serverPlayer);
+ }
+ //System.out.println("[echo to client(" + uuid + ")]" + title.url);
+ }
+ USER_CACHE_MAP.put(title.url, Lists.newArrayList());
+ }
+ //System.out.println("[FileChannel->Server]" + title.url);
+ }
+ }
+
+ @ProtocolHandler.PayloadReceiver(payload = FileInfoChannelPacket.class, payloadId = "file_info")
+ public static void serverFileInfoChannelReceived(ServerPlayer player, String url) {
+ if (!ProtocolSupport.chatImageProtocol) return;
+ if (SERVER_BLOCK_CACHE.containsKey(url) && FILE_COUNT_MAP.containsKey(url)) {
+ HashMap<Integer, String> list = SERVER_BLOCK_CACHE.get(url);
+ Integer total = FILE_COUNT_MAP.get(url);
+ if (total == list.size()) {
+ // 服务器存在缓存图片,直接发送给客户端
+ for (Map.Entry<Integer, String> entry : list.entrySet()) {
+ //System.out.println("[GetFileChannel->Client:" + entry.getKey() + "/" + (list.size() - 1) + "]" + url);
+ sendToPlayer(new DownloadFileChannelPacket(entry.getValue()), player);
+ }
+ //System.out.println("[GetFileChannel->Client]" + url);
+ return;
+ }
+ }
+ //通知客户端无文件
+ sendToPlayer(new FileInfoChannelPacket("null->" + url), player);
+ //System.out.println("[GetFileChannel]not found in server:" + url);
+ // 记录uuid,后续有文件了推送
+ List<String> names = USER_CACHE_MAP.containsKey(url) ? USER_CACHE_MAP.get(url) : Lists.newArrayList();
+ names.add(player.getStringUUID());
+ USER_CACHE_MAP.put(url, names);
+ //System.out.println("[GetFileChannel]记录uuid:" + player.getStringUUID());
+ //System.out.println("[not found in server]" + url);
+ }
+
+ @Contract("_ -> new")