第76批补2:loadBlacklistFromPath增量重载(指纹对比,无变化闭嘴)
Auto Release / build (push) Successful in 2m33s

This commit is contained in:
youpaishidifu
2026-11-04 09:28:26 +08:00
parent 680b4f42ea
commit 7268967281
+29 -20
View File
@@ -1831,38 +1831,47 @@ public class AIChatManager {
} }
try { try {
int totalCount = 0; int totalCount = 0;
// ★ 第76批:支持目录扫描——路径是目录则遍历所有 .json,是文件则当单文件读 // ★ 第76批补:增量重载——用指纹对比文件内容,无变化闭嘴、有变化才打印
// 复用 blacklistFileKeywords/blacklistFileHashes 映射
java.util.List<File> targetFiles = new java.util.ArrayList<>();
if (resolved.isDirectory()) { if (resolved.isDirectory()) {
File[] jsonFiles = resolved.listFiles((dir, name) -> name.endsWith(".json")); File[] jsonFiles = resolved.listFiles((dir, name) -> name.endsWith(".json"));
if (jsonFiles != null) { if (jsonFiles != null) {
for (File jsonFile : jsonFiles) { targetFiles.addAll(java.util.Arrays.asList(jsonFiles));
try {
String content = Files.readString(jsonFile.toPath(), StandardCharsets.UTF_8);
JsonArray arr = JsonParser.parseString(content).getAsJsonArray();
for (JsonElement el : arr) {
if (el.isJsonPrimitive()) {
blacklist.add(el.getAsString());
totalCount++;
}
}
plugin.getLogger().info("[AI聊天] 已加载黑名单文件: " + jsonFile.getName() + "(" + arr.size() + " 个关键词)");
} catch (Exception e) {
plugin.getLogger().warning("[AI聊天] 加载黑名单文件失败: " + jsonFile.getName() + " - " + e.getMessage());
}
}
} }
} else { } else {
// 单文件 targetFiles.add(resolved);
String content = Files.readString(resolved.toPath(), StandardCharsets.UTF_8); }
for (File jsonFile : targetFiles) {
String fileName = jsonFile.getName();
String content = Files.readString(jsonFile.toPath(), StandardCharsets.UTF_8);
String newHash = crc32(content);
String oldHash = blacklistFileHashes.get(fileName);
if (newHash.equals(oldHash)) {
continue; // 文件没变 → 内存不动,闭嘴
}
// 有变化或新文件:加载关键词
java.util.List<String> newKeywords = new java.util.ArrayList<>();
JsonArray arr = JsonParser.parseString(content).getAsJsonArray(); JsonArray arr = JsonParser.parseString(content).getAsJsonArray();
for (JsonElement el : arr) { for (JsonElement el : arr) {
if (el.isJsonPrimitive()) { if (el.isJsonPrimitive()) {
blacklist.add(el.getAsString()); newKeywords.add(el.getAsString());
totalCount++;
} }
} }
if (blacklistFileKeywords.containsKey(fileName)) {
blacklist.removeAll(blacklistFileKeywords.get(fileName));
plugin.getLogger().info("[AI聊天] 黑名单文件已更新: " + fileName + "(" + newKeywords.size() + " 个关键词)");
} else {
plugin.getLogger().info("[AI聊天] 已加载黑名单文件: " + fileName + "(" + newKeywords.size() + " 个关键词)");
} }
blacklist.addAll(newKeywords);
blacklistFileKeywords.put(fileName, newKeywords);
blacklistFileHashes.put(fileName, newHash);
totalCount += newKeywords.size();
}
if (totalCount > 0) {
plugin.getLogger().info("[AI聊天] 已从路径加载黑名单: " + rawPath + "(" + totalCount + " 个关键词)"); plugin.getLogger().info("[AI聊天] 已从路径加载黑名单: " + rawPath + "(" + totalCount + " 个关键词)");
}
} catch (Exception e) { } catch (Exception e) {
plugin.getLogger().warning("[AI聊天] 加载黑名单路径失败: " + rawPath + " - " + e.getMessage()); plugin.getLogger().warning("[AI聊天] 加载黑名单路径失败: " + rawPath + " - " + e.getMessage());
} }