Files
koring-launcher/electron/handlers/system.ts
T
dream_pep cd4578de5a feat: 全新 Korom 资源管理服务组件,优化内存占用
新增完整的程序本体资源管理与内存优化功能:
1. 新增背景图预处理服务,主进程将大图降采样至屏幕适配尺寸,避免全分辨率大图占用渲染进程大量内存
2. 实现资源注册表系统,支持引用计数、LRU自动逐出与释放回调
3. 新增资源与内存调试面板,可查看渲染进程JS堆、Electron进程内存与资源缓存状态
4. 修复进程内存信息API的单位与字段映射错误
5. 优化背景层缓存键避免过长dataURL,为Silk动画添加窗口隐藏时停帧逻辑减少GPU占用
6. 修复资源注册表类型适配问题并更新项目文档
2026-09-04 18:56:43 +08:00

115 lines
3.2 KiB
TypeScript

import electron from 'electron';
import { execSync } from 'child_process';
import * as os from 'os';
const { ipcMain, app, shell } = electron;
interface ProcessMemorySample {
type: string;
pid: number;
workingSetSize: number; // KB
peakWorkingSetSize: number; // KB
}
function getBiosId(): string {
if (process.platform !== 'win32') return 'N/A (non-Windows)';
try {
const output = execSync('powershell -NoProfile -Command "(Get-CimInstance Win32_BIOS).SerialNumber"', {
encoding: 'utf-8',
timeout: 5000,
});
return output.trim() || 'Unknown';
} catch {
return 'Unknown';
}
}
function getOsVersion(): string {
if (process.platform !== 'win32') return 'N/A (non-Windows)';
try {
const output = execSync('powershell -NoProfile -Command "[System.Environment]::OSVersion.VersionString"', {
encoding: 'utf-8',
timeout: 5000,
});
return output.trim() || 'Unknown';
} catch {
return 'Unknown';
}
}
function getOsName(): string {
if (process.platform !== 'win32') return 'N/A (non-Windows)';
try {
const output = execSync('powershell -NoProfile -Command "(Get-CimInstance Win32_OperatingSystem).Caption"', {
encoding: 'utf-8',
timeout: 5000,
});
return output.trim() || 'Unknown';
} catch {
return 'Unknown';
}
}
export function registerSystemHandlers() {
ipcMain.handle('system:info', () => {
try {
return {
success: true,
data: {
app_version: app.getVersion(),
bios_id: getBiosId(),
os_version: getOsVersion(),
os_name: getOsName(),
},
error: null,
};
} catch (e: unknown) {
return { success: false, data: null, error: String(e) };
}
});
// 进程内存快照(资源/内存调试面板使用;纯读取,无副作用)
ipcMain.handle('system:memory', async () => {
try {
const metrics: ProcessMemorySample[] = app.getAppMetrics().map((m) => ({
type: String(m.type),
pid: m.pid,
workingSetSize: m.memory?.workingSetSize ?? 0,
peakWorkingSetSize: m.memory?.peakWorkingSetSize ?? 0,
}));
let mainProcess: { workingSetSize: number; privateBytes: number } | null = null;
try {
const info = await process.getProcessMemoryInfo();
mainProcess = {
workingSetSize: info.residentSet ?? 0,
privateBytes: info.private ?? 0,
};
} catch {
// 个别平台不支持 getProcessMemoryInfo,忽略
}
return {
success: true,
data: {
app_version: app.getVersion(),
timestamp: Date.now(),
metrics,
mainProcess,
},
error: null,
};
} catch (e: unknown) {
return { success: false, data: null, error: String(e) };
}
});
// 在系统文件管理器中打开指定路径(用于"打开游戏目录"等操作)
ipcMain.handle('system:open-path', async (_event, payload: { path: string }) => {
try {
const error = await shell.openPath(payload.path);
return { success: !error, data: { error }, error: error || null };
} catch (e: unknown) {
return { success: false, data: null, error: String(e) };
}
});
}