From cd4578de5a8e2e7bfd922232d1069fdbecdffcdf Mon Sep 17 00:00:00 2001 From: dream_pep Date: Fri, 4 Sep 2026 18:56:43 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=85=A8=E6=96=B0=20Korom=20=E8=B5=84?= =?UTF-8?q?=E6=BA=90=E7=AE=A1=E7=90=86=E6=9C=8D=E5=8A=A1=E7=BB=84=E4=BB=B6?= =?UTF-8?q?=EF=BC=8C=E4=BC=98=E5=8C=96=E5=86=85=E5=AD=98=E5=8D=A0=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增完整的程序本体资源管理与内存优化功能: 1. 新增背景图预处理服务,主进程将大图降采样至屏幕适配尺寸,避免全分辨率大图占用渲染进程大量内存 2. 实现资源注册表系统,支持引用计数、LRU自动逐出与释放回调 3. 新增资源与内存调试面板,可查看渲染进程JS堆、Electron进程内存与资源缓存状态 4. 修复进程内存信息API的单位与字段映射错误 5. 优化背景层缓存键避免过长dataURL,为Silk动画添加窗口隐藏时停帧逻辑减少GPU占用 6. 修复资源注册表类型适配问题并更新项目文档 --- AGENTS.md | 7 + Koring.yml | 2 +- dsh-usage/usage-records.json | 2 +- electron/core/background-image.ts | 109 ++++++++ electron/handlers/background.ts | 11 + electron/handlers/system.ts | 4 +- electron/preload.ts | 31 ++- src/App.tsx | 2 + src/api/system.ts | 6 +- src/components/background/BackgroundLayer.tsx | 3 +- src/components/silk/Silk.tsx | 12 +- src/pages/debug/index.tsx | 10 +- src/pages/debug/resource-debug.tsx | 262 ++++++++++++++++++ src/resources/registry.ts | 3 +- src/stores/routeStore.ts | 3 +- 15 files changed, 452 insertions(+), 15 deletions(-) create mode 100644 electron/core/background-image.ts create mode 100644 src/pages/debug/resource-debug.tsx diff --git a/AGENTS.md b/AGENTS.md index 31ff137..d19a473 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -66,6 +66,7 @@ build/ ← generated by switch-icon.js (gitignored) - `electron/config.ts`: YAML config management (sparse save) - `electron/auth.ts`: Auth data persistence (JSON file) - `electron/core/`: @xmcl/* integrations (auth, installer, launcher, modrinth, instance) +- `electron/core/background-image.ts`: 背景图处理服务(自选背景降采样/重编码,程序本体资源管理) - `electron/handlers/`: IPC handlers (config, auth, install, launch, mods, instance, background, task, system, window) ## Frontend notes @@ -73,6 +74,12 @@ build/ ← generated by switch-icon.js (gitignored) - `src/api/ipc.ts`: Core IPC utilities (invoke, onIpcEvent) - `src/api/*.ts`: API modules wrapping IPC calls - `src/stores/`: Zustand state management +- `src/resources/`: 启动器程序本体「资源管理」子系统(与游戏无关): + - `registry.ts` 资源注册表服务(acquire/release、引用计数、预算 + LRU 逐出、onRelease 释放回调) + - `store.ts` 注册表 → zustand 镜像(调试面板消费) + - `image.ts` 图片解码管线(按显示尺寸降采样)、`hooks.ts`/`ManagedImage.tsx` 复用组件(供列表缩略图) + - 当前接线点:`BackgroundLayer` 把当前背景登记为 `background` 类资源;主进程 `background:prepare` 在进渲染端前压小大图 + - 监控入口:debug 页「资源与内存」(`debug-resource`) - `src/hooks/useTheme.ts`: Dark mode sync with Electron theme - `src/components/system/WindowControls.tsx`: Custom window controls (min/max/close), uses ` + + + + + {snapshot.entries.length === 0 ? ( +

暂无缓存条目(自定义背景图生效后此处可见 background:current:*)

+ ) : ( +
+ + + + + + + + + + + {snapshot.entries.slice(0, 40).map((e) => ( + + + + + + + ))} + +
Key类型估算引用
+ {e.key} + {KIND_LABELS[e.kind] ?? e.kind}{fmtMB(e.bytes)}{e.refs}
+
+ )} + + + + + + + + + + + ); +} diff --git a/src/resources/registry.ts b/src/resources/registry.ts index 38f9c1d..eb023f5 100644 --- a/src/resources/registry.ts +++ b/src/resources/registry.ts @@ -99,9 +99,10 @@ class ResourceRegistry { payload: null, inFlight: null, cache: opts.cache ?? true, - onRelease: opts.onRelease, }; this.entries.set(key, entry); + // 用桥接闭包把调用方的 (payload: T) => void 适配为内部 (payload: unknown) => void + entry.onRelease = opts.onRelease ? (payload: unknown): void => opts.onRelease?.(payload as T) : undefined; const run = async (): Promise => { let value: T | null = null; diff --git a/src/stores/routeStore.ts b/src/stores/routeStore.ts index 63e8654..08d1939 100644 --- a/src/stores/routeStore.ts +++ b/src/stores/routeStore.ts @@ -32,8 +32,8 @@ export type RouteKey = | "debug-version-card" | "debug-update" | "debug-task" + | "debug-resource" | "debug-crash"; - export type TitleBarMode = "default" | "sub" | "window" | "oobe"; export type TransitionDirection = "forward" | "backward"; @@ -82,6 +82,7 @@ export const allRoutes: RouteItem[] = [ { key: "debug-version-card", label: "版本卡片调试", path: "/debug/version-card", hidden: true }, { key: "debug-update", label: "更新功能测试", path: "/debug/update", hidden: true }, { key: "debug-task", label: "任务队列调试", path: "/debug/task", hidden: true }, + { key: "debug-resource", label: "资源与内存", path: "/debug/resource", hidden: true }, ]; const topLevelKeys = new Set(routes.map((r) => r.key));