125批:msg.json 主文件为模板时从 .bak/最新时间戳备份恢复真实记录,修复轮转备份漏昨日消息
Auto Release / build (push) Successful in 2m35s
Auto Release / build (push) Successful in 2m35s
This commit is contained in:
@@ -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 已存在且非空(模板初始状态忽略),按启动时刻复制为
|
||||
|
||||
Reference in New Issue
Block a user