feat: SodiumPet 小钠桌宠 v0.1.0(Live2D 桌宠 / 经济任务 / 音乐 / 成就 / 编辑器 / MOD)
This commit is contained in:
@@ -0,0 +1,259 @@
|
||||
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:profiles + skins + flavor
|
||||
patch('main/index.ts', [
|
||||
// 存储改为可变实例(按角色存档目录)
|
||||
[
|
||||
`const store = new StateStore(path.join(HOME, 'data'))
|
||||
const media = new MediaStore(path.join(HOME, 'data'))
|
||||
const kb = new KnowledgeBase(path.join(HOME, 'data'))`,
|
||||
`function sanitizeId(name: string): string {
|
||||
const s = String(name).replace(/[\\\\/:*?"<>|]/g, '_').trim()
|
||||
return s || 'default'
|
||||
}
|
||||
|
||||
function profileRoot(): string {
|
||||
return path.join(HOME, 'data', 'profiles', profileId)
|
||||
}
|
||||
|
||||
function readProfileFromConfig(): string {
|
||||
try {
|
||||
const raw = fs.readFileSync(path.join(HOME, 'data', 'config.json'), 'utf8')
|
||||
const cfg = JSON.parse(raw) as { profile?: string }
|
||||
return sanitizeId(cfg.profile || '')
|
||||
} catch {
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
// 旧版数据迁移进 profiles/default
|
||||
function migrateLegacyData(): void {
|
||||
const baseDir = path.join(HOME, 'data')
|
||||
const profilesDir = path.join(baseDir, 'profiles')
|
||||
if (fs.existsSync(profilesDir)) return
|
||||
if (!fs.existsSync(path.join(baseDir, 'save.json'))) return
|
||||
const target = path.join(profilesDir, 'default')
|
||||
fs.mkdirSync(target, { recursive: true })
|
||||
for (const item of ['save.json', 'photos.json', 'files.json']) {
|
||||
const src = path.join(baseDir, item)
|
||||
if (fs.existsSync(src)) fs.renameSync(src, path.join(target, item))
|
||||
}
|
||||
for (const dir of ['photos', 'files', 'knowledge', 'workspace']) {
|
||||
const src = path.join(baseDir, dir)
|
||||
if (fs.existsSync(src)) fs.renameSync(src, path.join(target, dir))
|
||||
}
|
||||
console.log('[profile] 旧数据已迁移到 profiles/default')
|
||||
}
|
||||
|
||||
migrateLegacyData()
|
||||
const initialProfile = readProfileFromConfig()
|
||||
let profileId = sanitizeId(initialProfile)
|
||||
let store = new StateStore(profileRoot())
|
||||
let media = new MediaStore(profileRoot())
|
||||
let kb = new KnowledgeBase(profileRoot())
|
||||
fs.mkdirSync(path.join(HOME, 'data', 'profiles'), { recursive: true })
|
||||
store.setConfig({ profile: profileId })`
|
||||
],
|
||||
// 广播封装 + 切换
|
||||
[
|
||||
` store.subscribe((s) => {
|
||||
for (const w of BrowserWindow.getAllWindows()) {
|
||||
if (!w.isDestroyed()) w.webContents.send('pet:state:update', s)
|
||||
}
|
||||
})
|
||||
store.start()`,
|
||||
` bindStoreBroadcast()
|
||||
store.start()`
|
||||
],
|
||||
[
|
||||
`function positionMenu(): void {`,
|
||||
`function bindStoreBroadcast(): void {
|
||||
store.subscribe((s) => {
|
||||
for (const w of BrowserWindow.getAllWindows()) {
|
||||
if (!w.isDestroyed()) w.webContents.send('pet:state:update', s)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function listProfiles(): Array<{ id: string; active: boolean }> {
|
||||
const dir = path.join(HOME, 'data', 'profiles')
|
||||
fs.mkdirSync(dir, { recursive: true })
|
||||
return fs
|
||||
.readdirSync(dir, { withFileTypes: true })
|
||||
.filter((d) => d.isDirectory())
|
||||
.map((d) => ({ id: d.name, active: d.name === profileId }))
|
||||
}
|
||||
|
||||
function loadProfile(id: string): void {
|
||||
profileId = sanitizeId(id)
|
||||
store.dispose()
|
||||
store = new StateStore(profileRoot())
|
||||
media = new MediaStore(profileRoot())
|
||||
kb = new KnowledgeBase(profileRoot())
|
||||
bindStoreBroadcast()
|
||||
}
|
||||
|
||||
function listSkins(): Array<{ id: string; name: string; active: boolean }> {
|
||||
const root = userSkins()
|
||||
const cur = store.getConfig().currentSkin || 'xiaona'
|
||||
return fs
|
||||
.readdirSync(root, { withFileTypes: true })
|
||||
.filter((d) => d.isDirectory())
|
||||
.map((d) => {
|
||||
let name = d.name
|
||||
try {
|
||||
const m = JSON.parse(fs.readFileSync(path.join(root, d.name, 'skin.json'), 'utf8'))
|
||||
if (m.name) name = m.name
|
||||
} catch {
|
||||
/* 用目录名 */
|
||||
}
|
||||
return { id: d.name, name, active: cur === d.name }
|
||||
})
|
||||
}
|
||||
|
||||
function currentFlavor(): string {
|
||||
try {
|
||||
const root = userSkins()
|
||||
const m = JSON.parse(
|
||||
fs.readFileSync(path.join(root, store.getConfig().currentSkin || 'xiaona', 'skin.json'), 'utf8')
|
||||
)
|
||||
return m.flavor === 'normal' ? 'normal' : 'chemistry'
|
||||
} catch {
|
||||
return 'chemistry'
|
||||
}
|
||||
}
|
||||
|
||||
function positionMenu(): void {`
|
||||
],
|
||||
// IPC:profiles + skins
|
||||
[
|
||||
` ipcMain.handle('pet:ws:list', () => {`,
|
||||
` ipcMain.handle('pet:profiles:list', () => listProfiles())
|
||||
ipcMain.handle('pet:profiles:create', (_e, name: string) => {
|
||||
const id = sanitizeId(String(name))
|
||||
fs.mkdirSync(path.join(HOME, 'data', 'profiles', id), { recursive: true })
|
||||
return listProfiles()
|
||||
})
|
||||
ipcMain.handle('pet:profiles:switch', (_e, id: string) => {
|
||||
const target = path.join(HOME, 'data', 'profiles', sanitizeId(String(id)))
|
||||
if (!fs.existsSync(target)) return listProfiles()
|
||||
store.setConfig({ profile: sanitizeId(String(id)) })
|
||||
loadProfile(String(id))
|
||||
store.start()
|
||||
for (const w of BrowserWindow.getAllWindows()) {
|
||||
if (!w.isDestroyed()) w.webContents.send('pet:state:update', store.snapshot())
|
||||
}
|
||||
broadcastMedia()
|
||||
chatService.reset()
|
||||
return listProfiles()
|
||||
})
|
||||
|
||||
ipcMain.handle('pet:skins:list', () => listSkins())
|
||||
ipcMain.handle('pet:skins:import', async () => {
|
||||
const res = await dialog.showOpenDialog({
|
||||
title: '选择 Live2D 模型文件夹(需包含 .model3.json)',
|
||||
properties: ['openDirectory']
|
||||
})
|
||||
if (res.canceled || !res.filePaths.length) return listSkins()
|
||||
const src = res.filePaths[0]
|
||||
const modelFile = fs.readdirSync(src).find((f) => f.endsWith('.model3.json'))
|
||||
if (!modelFile) return listSkins()
|
||||
const id = sanitizeId(path.basename(src))
|
||||
const target = path.join(userSkins(), id)
|
||||
fs.mkdirSync(target, { recursive: true })
|
||||
for (const f of fs.readdirSync(src)) {
|
||||
const full = path.join(src, f)
|
||||
if (fs.statSync(full).isDirectory()) {
|
||||
fs.cpSync(full, path.join(target, f), { recursive: true })
|
||||
} else if (/\\.(moc3|model3\\.json|physics3\\.json|cdi3\\.json|motion3\\.json|exp3\\.json|png)$/i.test(f)) {
|
||||
fs.copyFileSync(full, path.join(target, f))
|
||||
}
|
||||
}
|
||||
const skin = {
|
||||
id,
|
||||
name: id,
|
||||
version: '1.0.0',
|
||||
flavor: 'normal',
|
||||
avatar: { type: 'live2d', entry: modelFile, scale: 0.4 },
|
||||
motions: {},
|
||||
expressions: {}
|
||||
}
|
||||
fs.writeFileSync(path.join(target, 'skin.json'), JSON.stringify(skin, null, 2), 'utf8')
|
||||
return listSkins()
|
||||
})
|
||||
ipcMain.handle('pet:skins:switch', (_e, id: string) => {
|
||||
if (!listSkins().some((s) => s.id === String(id))) return listSkins()
|
||||
store.setConfig({ currentSkin: String(id) })
|
||||
applyConfig(store.getConfig())
|
||||
chatService.reset()
|
||||
return listSkins()
|
||||
})
|
||||
|
||||
ipcMain.handle('pet:ws:list', () => {`
|
||||
],
|
||||
// 聊天与视觉请求带 flavor
|
||||
[
|
||||
` } else {
|
||||
const kbCtx = kb.search(t)
|
||||
reply = await chatService.askStream(t, (chunk) => bubbleStreamChunk(chunk), kbCtx)
|
||||
}`,
|
||||
` } else {
|
||||
const kbCtx = kb.search(t)
|
||||
reply = await chatService.askStream(
|
||||
t,
|
||||
(chunk) => bubbleStreamChunk(chunk),
|
||||
kbCtx,
|
||||
'',
|
||||
currentFlavor()
|
||||
)
|
||||
}`
|
||||
],
|
||||
[
|
||||
` reply = await chatService.askStream(
|
||||
'请用小钠的语气、五句话以内描述你看到的内容,如果图里有文字也读出来',
|
||||
(chunk) => bubbleStreamChunk(chunk),
|
||||
'',
|
||||
dataUrl
|
||||
)`,
|
||||
` reply = await chatService.askStream(
|
||||
'请用桌宠的语气、五句话以内描述你看到的内容,如果图里有文字也读出来',
|
||||
(chunk) => bubbleStreamChunk(chunk),
|
||||
'',
|
||||
dataUrl,
|
||||
currentFlavor()
|
||||
)`
|
||||
]
|
||||
])
|
||||
|
||||
// ---- preload / api
|
||||
patch('preload/index.ts', [
|
||||
[
|
||||
" getWs: () => Promise<Array<{ name: string; size: number }>>",
|
||||
" getWs: () => Promise<Array<{ name: string; size: number }>>\n getProfiles: () => Promise<Array<{ id: string; active: boolean }>>\n createProfile: (name: string) => Promise<Array<{ id: string; active: boolean }>>\n switchProfile: (id: string) => Promise<Array<{ id: string; active: boolean }>>\n getSkins: () => Promise<Array<{ id: string; name: string; active: boolean }>>\n importSkin: () => Promise<Array<{ id: string; name: string; active: boolean }>>\n switchSkin: (id: string) => Promise<Array<{ id: string; name: string; active: boolean }>>"
|
||||
],
|
||||
[
|
||||
" openWs: () => ipcRenderer.invoke('pet:ws:open'),",
|
||||
" openWs: () => ipcRenderer.invoke('pet:ws:open'),\n getProfiles: () => ipcRenderer.invoke('pet:profiles:list'),\n createProfile: (name: string) => ipcRenderer.invoke('pet:profiles:create', name),\n switchProfile: (id: string) => ipcRenderer.invoke('pet:profiles:switch', id),\n getSkins: () => ipcRenderer.invoke('pet:skins:list'),\n importSkin: () => ipcRenderer.invoke('pet:skins:import'),\n switchSkin: (id: string) => ipcRenderer.invoke('pet:skins:switch', id),"
|
||||
]
|
||||
])
|
||||
patch('renderer/src/api.ts', [
|
||||
[
|
||||
" getWs: () => Promise<Array<{ name: string; size: number }>>",
|
||||
" getWs: () => Promise<Array<{ name: string; size: number }>>\n getProfiles: () => Promise<Array<{ id: string; active: boolean }>>\n createProfile: (name: string) => Promise<Array<{ id: string; active: boolean }>>\n switchProfile: (id: string) => Promise<Array<{ id: string; active: boolean }>>\n getSkins: () => Promise<Array<{ id: string; name: string; active: boolean }>>\n importSkin: () => Promise<Array<{ id: string; name: string; active: boolean }>>\n switchSkin: (id: string) => Promise<Array<{ id: string; name: string; active: boolean }>>"
|
||||
]
|
||||
])
|
||||
|
||||
console.log('profiles/skins patched')
|
||||
Reference in New Issue
Block a user