PCL-CE icon indicating copy to clipboard operation
PCL-CE copied to clipboard

[C#]: 本地资源联网信息缓存 LocalComp.json 写入时带 BOM,每次读取都失败并被重置

Open zhuzhi-09 opened this issue 2 weeks ago • 2 comments

检查项 / Checklist

  • [x] 我已在 Issues 页面 中搜索,确认了这一 Bug 未被提交过 / I have searched the Issues page and confirmed that this bug has not been reported yet.
  • [ ] 我确认只是全部选中而没有仔细确认就直接提交了这个 Issue,并且同意这个 Issue 可以直接被关闭 / I confirm that I checked all boxes without carefully reading them, and agree that this issue may be closed directly.
  • [x] 我确认正在使用当前更新分支的最新版 PCL CE,若不是,此 Issue 可能会被直接关闭(更新方式:启动器 设置-关于-软件更新 检查更新,或在 Releases 页面下载) / I confirm that I am using the latest PCL CE version on the current update channel. If not, this issue may be closed directly. To update, check for updates in Settings → About → Updates, or download the latest version from the Releases page.
  • [x] 我确认会详细描述我所遇到的问题,并附上日志和截图,而不是随便填点东西敷衍了事 / I will describe the problem in detail, attach logs and screenshots, and avoid submitting vague or incomplete information.
  • [x] 我确认已亲自复现并核实所报告的问题;如未能复现,我已在描述中明确说明未确认的部分。Issue 中的描述、日志和截图均为真实内容,未直接提交 AI 生成的未经核实内容 / I confirm that I have personally reproduced and verified the reported problem. If I could not reproduce it, I have clearly stated which parts remain unconfirmed in the description. The description, logs, and screenshots are genuine, and I have not submitted unverified AI-generated content.

AI 使用情况 / AI Usage

用于分析、整理或撰写,并已自行核实 / Used for analysis, drafting, or organization, verified by me

AI 工具与核实说明 / AI Tools and Verification

工具与模型 / Tools and models:Kimi K3.1 Pro(豆包 Work);Claude Code + Claude Opus 5.5 用途 / Purpose:Kimi K3.1 Pro 在复现 #3707 时发现缓存读取失败的日志;Claude Code 核实根因并整理本 Issue 内容 核实方式 / Verification:本人用 VS Code 打开测试环境中的 LocalComp.json,状态栏显示编码为 "UTF-8 with BOM";并对照了日志中的解析错误和文中列出的代码位置

描述 / Description

本地资源(模组 / 资源包等)的联网信息缓存 Temp\Cache\LocalComp.json 每次读取都会失败并被重置,导致这个缓存实际上从未生效。

原因:

  • 写入:LocalComp/Update.cs 的 _SaveUpdateCache 调用 Files.WriteAllText,默认编码是 Encoding.UTF8,会在文件开头写入 BOM(EF BB BF)。
  • 读取:LocalComp/Loader.cs 的 LoadLocalModInfoCache 调用 Files.ReadAllTextOrEmpty,BOM 被一起解码进字符串,GetJson 解析失败;异常被捕获后只记录一条 Debug 日志,缓存被重置为空。

影响:每次加载资源列表都要重新向 Modrinth / CurseForge 查询全部资源,原本"6 小时内不重复查询"的缓存不起作用,资源较多时加载更慢。

可能的修法:

  • 写入时使用不带 BOM 的编码(new UTF8Encoding(false));或
  • 在 Files.ReadAllTextOrEmpty 中去掉开头的 BOM,使所有读取方受益。

另外项目中还有约 19 处使用默认编码调用 Files.WriteAllText,可能存在同类问题。

测试版本:dev 009af6f6(Windows)。

重现步骤 / Steps to Reproduce

  1. 在任意实例中安装一个资源包或模组,进入"实例设置 → 资源包 / 模组"页面,等待联网信息加载完成。
  2. 查看 PCL 临时目录下的 Cache\LocalComp.json,文件开头为 EF BB BF(UTF-8 BOM)。
  3. 离开页面后重新进入(或重启启动器后进入),日志出现"读取本地 Mod 信息缓存失败,已重置",并再次联网查询全部资源。

日志与附件 / Logs & Attachments

[DBG] [Launcher] 读取本地 Mod 信息缓存失败,已重置 System.Exception: 格式化 JSON 失败:{"version":10,...} ---> System.Text.Json.JsonReaderException: '0xEF' is an invalid start of a value. LineNumber: 0 | BytePositionInLine: 0. at PCL.LocalComp.LoadLocalModInfoCache()

zhuzhi-09 avatar Nov 19 '26 17:11 zhuzhi-09

写错模型信息了 改了一下描述。

zhuzhi-09 avatar Nov 19 '26 17:11 zhuzhi-09

本身不是什么影响使用的问题,但是如果是大型整合包或者几十上百资源的实例会导致性能下降,在中低性能机器上可能出现显著卡顿,属于资源浪费的潜在问题。

zhuzhi-09 avatar Nov 19 '26 19:11 zhuzhi-09