133 lines
4.7 KiB
JavaScript
133 lines
4.7 KiB
JavaScript
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')
|
|
}
|
|
|
|
// 1) 协议:pet://data/... 暴露 data 目录(只读)
|
|
patch('main/index.ts', [
|
|
[
|
|
` } else if (url.host === 'photos') {
|
|
abs = path.join(media.photoDir, rel)
|
|
root = media.photoDir
|
|
} else {`,
|
|
` } else if (url.host === 'photos') {
|
|
abs = path.join(media.photoDir, rel)
|
|
root = media.photoDir
|
|
} else if (url.host === 'data') {
|
|
abs = path.join(HOME, 'data', rel)
|
|
root = path.join(HOME, 'data')
|
|
} else {`
|
|
],
|
|
// 2) 启动时加载 catalog.json 覆盖内置目录
|
|
[
|
|
"import { ITEMS, itemById, levelOf } from '../shared/petState'",
|
|
"import { ITEMS, itemById, levelOf } from '../shared/petState'\nimport { applyCatalog, snapshotCatalog } from '../shared/catalog'"
|
|
],
|
|
[
|
|
`migrateLegacyData()`,
|
|
`migrateLegacyData()
|
|
|
|
// 载入用户编辑过的物品 / 活动目录
|
|
try {
|
|
const catalogPath = path.join(HOME, 'data', 'catalog.json')
|
|
if (fs.existsSync(catalogPath)) {
|
|
applyCatalog(JSON.parse(fs.readFileSync(catalogPath, 'utf8')))
|
|
console.log('[catalog] 已加载用户目录')
|
|
}
|
|
} catch (err) {
|
|
console.error('[catalog] 读取失败', err)
|
|
}`
|
|
],
|
|
// 3) 保存 IPC
|
|
[
|
|
` ipcMain.handle('pet:birthday:set', (_e, day: string) => store.setBirthday(String(day)))`,
|
|
` ipcMain.handle('pet:birthday:set', (_e, day: string) => store.setBirthday(String(day)))
|
|
ipcMain.handle('pet:catalog:get', () => snapshotCatalog())
|
|
ipcMain.handle('pet:catalog:save', (_e, data: unknown) => {
|
|
try {
|
|
const catalogPath = path.join(HOME, 'data', 'catalog.json')
|
|
fs.writeFileSync(catalogPath, JSON.stringify(data, null, 2), 'utf8')
|
|
applyCatalog(data as never)
|
|
for (const w of BrowserWindow.getAllWindows()) {
|
|
if (!w.isDestroyed()) w.webContents.send('pet:catalog:changed')
|
|
}
|
|
return { ok: true, message: '目录已保存' }
|
|
} catch (err) {
|
|
console.error('[catalog] 保存失败', err)
|
|
return { ok: false, message: '保存失败:' + String(err) }
|
|
}
|
|
})
|
|
ipcMain.on('pet:editor:open', () => createEditorWindow())`
|
|
],
|
|
// 4) 编辑器窗口声明
|
|
[
|
|
"let detailWindow: BrowserWindow | null = null",
|
|
"let detailWindow: BrowserWindow | null = null\nlet editorWindow: BrowserWindow | null = null"
|
|
],
|
|
[
|
|
`function createDetailWindow(): void {`,
|
|
`function createEditorWindow(): void {
|
|
if (editorWindow && !editorWindow.isDestroyed()) {
|
|
editorWindow.show()
|
|
editorWindow.focus()
|
|
return
|
|
}
|
|
editorWindow = new BrowserWindow({
|
|
width: 780,
|
|
height: 560,
|
|
minWidth: 640,
|
|
minHeight: 420,
|
|
show: false,
|
|
frame: false,
|
|
resizable: true,
|
|
backgroundColor: '#0d1017',
|
|
webPreferences: {
|
|
preload: path.join(__dirname, '../preload/index.js'),
|
|
contextIsolation: true,
|
|
nodeIntegration: false,
|
|
sandbox: false
|
|
}
|
|
})
|
|
editorWindow.loadFile(path.join(__dirname, '../renderer/index.html'), {
|
|
query: { view: 'editor' }
|
|
})
|
|
editorWindow.once('ready-to-show', () => editorWindow?.show())
|
|
editorWindow.on('closed', () => {
|
|
editorWindow = null
|
|
})
|
|
}
|
|
|
|
function createDetailWindow(): void {`
|
|
]
|
|
])
|
|
|
|
// preload / api
|
|
patch('preload/index.ts', [
|
|
[
|
|
" openDetail: () => void",
|
|
" openDetail: () => void\n openEditor: () => void\n getCatalog: () => Promise<{ items: unknown[]; jobs: unknown[] }>\n saveCatalog: (data: unknown) => Promise<ActionResult>\n onCatalogChanged: (cb: () => void) => () => void"
|
|
],
|
|
[
|
|
" openDetail: () => ipcRenderer.send('pet:detail:open'),",
|
|
" openDetail: () => ipcRenderer.send('pet:detail:open'),\n openEditor: () => ipcRenderer.send('pet:editor:open'),\n getCatalog: () => ipcRenderer.invoke('pet:catalog:get'),\n saveCatalog: (data: unknown) => ipcRenderer.invoke('pet:catalog:save', data),\n onCatalogChanged: (cb: () => void) => {\n const l = (): void => cb()\n ipcRenderer.on('pet:catalog:changed', l)\n return () => ipcRenderer.removeListener('pet:catalog:changed', l)\n },"
|
|
]
|
|
])
|
|
patch('renderer/src/api.ts', [
|
|
[
|
|
" openDetail: () => void",
|
|
" openDetail: () => void\n openEditor: () => void\n getCatalog: () => Promise<{ items: unknown[]; jobs: unknown[] }>\n saveCatalog: (data: unknown) => Promise<ActionResult>\n onCatalogChanged: (cb: () => void) => () => void"
|
|
]
|
|
])
|
|
|
|
console.log('editor main patched')
|