From 7a922f97480d42ed6502256bd630761c6471d619 Mon Sep 17 00:00:00 2001 From: youpaishidifu <1@ypshidifu.cn> Date: Wed, 18 Nov 2026 10:13:33 +0800 Subject: [PATCH] =?UTF-8?q?125=E6=89=B9=EF=BC=9Amsg.json=20=E4=B8=BB?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=BA=E6=A8=A1=E6=9D=BF=E6=97=B6=E4=BB=8E?= =?UTF-8?q?=20.bak/=E6=9C=80=E6=96=B0=E6=97=B6=E9=97=B4=E6=88=B3=E5=A4=87?= =?UTF-8?q?=E4=BB=BD=E6=81=A2=E5=A4=8D=E7=9C=9F=E5=AE=9E=E8=AE=B0=E5=BD=95?= =?UTF-8?q?=EF=BC=8C=E4=BF=AE=E5=A4=8D=E8=BD=AE=E8=BD=AC=E5=A4=87=E4=BB=BD?= =?UTF-8?q?=E6=BC=8F=E6=98=A8=E6=97=A5=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/Sdf1_login/PlayerMessageManager.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/main/java/Sdf1_login/PlayerMessageManager.java b/src/main/java/Sdf1_login/PlayerMessageManager.java index 9933fa3..5adf685 100644 --- a/src/main/java/Sdf1_login/PlayerMessageManager.java +++ b/src/main/java/Sdf1_login/PlayerMessageManager.java @@ -62,6 +62,16 @@ public class PlayerMessageManager { // 旧逻辑只靠 justExtracted 判断,但文件已存在且内容就是模板(从未写过消息) // 时 justExtracted=false,把模板当"现有记录"备份 → 所有轮转备份全变成 198B 模板文件。 // 现在统一用 isTemplateContent() 判定:模板内容一律跳过备份。 + // ★ 第125批(2026-11-18):模板不能吞掉真实记录——若 msg.json 当前是模板,但存在含真实记录的 + // 备份(msg.json.bak 或最新时间戳 msg.json.*),先把真实记录恢复为 msg.json 主文件, + // 再走备份/加载。否则昨日真实记录滞留在 .bak、msg.json 主文件是模板 → 轮转备份漏掉真实记录。 + if (!justExtracted && isTemplateContent()) { + File real = restoreLatestRealBackup(); + if (real != null) { + plugin.getLogger().info("[消息记录] 主文件为模板,已从 " + real.getName() + + " 恢复真实记录,避免轮转备份丢失历史消息"); + } + } if (!justExtracted && !isTemplateContent()) backupOnStartupIfNeeded(); loadMessages(); } @@ -84,6 +94,42 @@ public class PlayerMessageManager { } } + /** + * ★ 第125批:若 msg.json 主文件是模板,查找含真实记录的备份并恢复为主文件。 + * 查找顺序:msg.json.bak(旧语义备份)→ 最新的时间戳备份 msg.json.*(按文件名时间戳倒序)。 + * 仅恢复「含非空 消息记录 数组」的备份。恢复成功返回该备份 File(调用方打日志),无则 null。 + */ + private File restoreLatestRealBackup() { + File[] candidates = new File[]{ new File(dataDir, "msg.json.bak") }; + File[] tsFiles = dataDir.listFiles((dir, name) -> + name.startsWith("msg.json.") && !name.equals("msg.json.bak")); + if (tsFiles != null && tsFiles.length > 0) { + // 按时间戳部分倒序(文件名形如 msg.json.202611180230,越大越新) + java.util.Arrays.sort(tsFiles, (a, b) -> b.getName().compareTo(a.getName())); + candidates = java.util.stream.Stream.concat( + java.util.stream.Stream.of(candidates[0]), + java.util.stream.Stream.of(tsFiles)).toArray(File[]::new); + } + for (File cand : candidates) { + if (cand == null || !cand.isFile()) continue; + try { + String content = Files.readString(cand.toPath(), StandardCharsets.UTF_8); + JsonObject obj = new Gson().fromJson(content, JsonObject.class); + if (obj == null) continue; + JsonArray arr = obj.getAsJsonArray("消息记录"); + if (arr != null && arr.size() > 0) { + Files.copy(cand.toPath(), msgFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + plugin.getLogger().info("[消息记录] 从备份 " + cand.getName() + + " 恢复 " + arr.size() + " 条真实记录到主文件(主文件原为模板)"); + return cand; + } + } catch (Exception e) { + plugin.getLogger().warning("[消息记录] 读取备份 " + cand.getName() + " 失败: " + e.getMessage()); + } + } + return null; + } + /** * ★ 第95批(2026-11-07) 重启备份。 * 若 msg.json 已存在且非空(模板初始状态忽略),按启动时刻复制为