const fs = require('fs') const base = 'C:/Users/ASUS/Documents/deepseek-harness/default-workspace/desktop-pet/src/' function patch(file, pairs) { const p = base + file let c = fs.readFileSync(p, 'utf8') for (const [from, to] of pairs) { if (!c.includes(from)) { console.log('MISS in', file, '::', from.slice(0, 70).replace(/\n/g, '\\n')) continue } c = c.replace(from, to) } fs.writeFileSync(p, c, 'utf8') } // main:mods 目录 + IPC + 启动应用 patch('main/index.ts', [ [ "import { applyCatalog, snapshotCatalog } from '../shared/catalog'", "import { applyCatalog, snapshotCatalog } from '../shared/catalog'\nimport { mergeCatalog, type ModFile } from '../shared/mod'" ], [ ` for (const item of ['save.json', 'photos.json', 'files.json']) {`, ` for (const item of ['save.json', 'photos.json', 'files.json']) {` ], // 启动时应用 mods(放在 catalog 载入之后) [ `const initialProfile = readProfileFromConfig()`, `// 应用 MOD(data/mods/*.json) try { const modsDir = path.join(HOME, 'data', 'mods') fs.mkdirSync(modsDir, { recursive: true }) let merged = snapshotCatalog() for (const f of fs.readdirSync(modsDir).filter((n) => n.endsWith('.json'))) { try { const mod = JSON.parse(fs.readFileSync(path.join(modsDir, f), 'utf8')) as ModFile merged = mergeCatalog({ items: merged.items ?? [], jobs: merged.jobs ?? [] }, mod) } catch (err) { console.error('[mod] 加载失败', f, err) } } applyCatalog(merged) console.log('[mod] 已应用,物品', ITEMS.length, '活动', merged.jobs?.length ?? 0) } catch (err) { console.error('[mod] 目录读取失败', err) } const initialProfile = readProfileFromConfig()` ], // IPC [ ` ipcMain.handle('pet:chat:presets', () => store.getAvailablePresets(currentFlavor()))`, ` ipcMain.handle('pet:mods:list', () => { const modsDir = path.join(HOME, 'data', 'mods') fs.mkdirSync(modsDir, { recursive: true }) return fs.readdirSync(modsDir).filter((n) => n.endsWith('.json')) }) ipcMain.handle('pet:mods:add', async () => { const res = await dialog.showOpenDialog({ title: '选择 MOD 文件(.json)', properties: ['openFile'], filters: [{ name: 'MOD', extensions: ['json'] }] }) if (res.canceled || !res.filePaths.length) return { ok: false, message: '已取消', mods: [] } try { const src = res.filePaths[0] const raw = JSON.parse(fs.readFileSync(src, 'utf8')) as ModFile if (!raw.items && !raw.jobs && !raw.removeItems && !raw.removeJobs) { return { ok: false, message: '这个 MOD 里没有物品或活动', mods: [] } } const modsDir = path.join(HOME, 'data', 'mods') fs.mkdirSync(modsDir, { recursive: true }) const name = path.basename(src) fs.copyFileSync(src, path.join(modsDir, name)) return { ok: true, message: '已添加 MOD:' + (raw.name || name) + '(重启后生效)', mods: fs.readdirSync(modsDir).filter((n) => n.endsWith('.json')) } } catch (err) { return { ok: false, message: 'MOD 解析失败:' + String(err), mods: [] } } }) ipcMain.handle('pet:mods:remove', (_e, name: string) => { const modsDir = path.join(HOME, 'data', 'mods') const target = path.join(modsDir, String(name)) if (target.startsWith(modsDir)) fs.rmSync(target, { force: true }) return fs.readdirSync(modsDir).filter((n) => n.endsWith('.json')) }) ipcMain.handle('pet:chat:presets', () => store.getAvailablePresets(currentFlavor()))` ] ]) // preload / api patch('preload/index.ts', [ [ " getChatPresets: () => Promise<{", ` getMods: () => Promise addMod: () => Promise<{ ok: boolean; message: string; mods: string[] }> removeMod: (name: string) => Promise getChatPresets: () => Promise<{` ], [ " getChatPresets: () => ipcRenderer.invoke('pet:chat:presets'),", " getMods: () => ipcRenderer.invoke('pet:mods:list'),\n addMod: () => ipcRenderer.invoke('pet:mods:add'),\n removeMod: (name: string) => ipcRenderer.invoke('pet:mods:remove', name),\n getChatPresets: () => ipcRenderer.invoke('pet:chat:presets')," ] ]) patch('renderer/src/api.ts', [ [ " getChatPresets: () => Promise<{", ` getMods: () => Promise addMod: () => Promise<{ ok: boolean; message: string; mods: string[] }> removeMod: (name: string) => Promise getChatPresets: () => Promise<{` ] ]) // renderer 启动:应用 mods patch('renderer/src/main.tsx', [ [ "import { applyCatalog } from '../../shared/catalog'", "import { applyCatalog } from '../../shared/catalog'\nimport { mergeCatalog, type ModFile } from '../../shared/mod'" ], [ ` try { const r = await fetch('pet://data/catalog.json') if (r.ok) applyCatalog(await r.json()) } catch { /* 没有自定义目录就用内置数据 */ }`, ` try { const r = await fetch('pet://data/catalog.json') if (r.ok) applyCatalog(await r.json()) } catch { /* 没有自定义目录就用内置数据 */ } // MOD:叠加在 catalog 之后 try { const mods: string[] = (await window.pet?.getMods?.()) ?? [] let base = { items: ITEMS, jobs: JOBS } for (const f of mods) { try { const res = await fetch('pet://data/mods/' + encodeURIComponent(f)) if (!res.ok) continue const mod = (await res.json()) as ModFile base = mergeCatalog(base, mod) } catch { /* 单个 mod 失败不影响启动 */ } } ITEMS.length = 0 for (const it of base.items) ITEMS.push(it) JOBS.length = 0 for (const j of base.jobs) JOBS.push(j) } catch { /* 忽略 */ }` ], [ "import { applyCatalog } from '../../shared/catalog'", "import { applyCatalog } from '../../shared/catalog'\nimport { ITEMS, JOBS } from '../../shared/petState'" ] ]) console.log('mod system patched')