Files
SodiumPet/tools/patch-p39-music-main.cjs

217 lines
6.1 KiB
JavaScript

const fs = require('fs')
const base = 'C:/Users/ASUS/Documents/deepseek-harness/desktop-pet/'
const petStatePath = 'C:/Users/ASUS/Documents/deepseek-harness/default-workspace/desktop-pet/src/shared/petState.ts'
// 1) 配置:歌词浮窗样式
let ps = fs.readFileSync(petStatePath, 'utf8')
if (!ps.includes('lyricBg')) {
ps = ps.replace(
` flavorMode: 'auto' | 'chemistry' | 'normal'`,
` flavorMode: 'auto' | 'chemistry' | 'normal'
/** 歌词浮窗:底色 / 文字色 / 描边色 / 字号 */
lyricBg: string
lyricColor: string
lyricOutline: string
lyricSize: number`
)
ps = ps.replace(
` flavorMode: 'auto',`,
` flavorMode: 'auto',
lyricBg: 'rgba(16,20,30,0.55)',
lyricColor: '#ffffff',
lyricOutline: '#000000',
lyricSize: 20,`
)
fs.writeFileSync(petStatePath, ps, 'utf8')
console.log('config lyric fields added')
}
const mainPath = 'C:/Users/ASUS/Documents/deepseek-harness/default-workspace/desktop-pet/src/main/index.ts'
let m = fs.readFileSync(mainPath, 'utf8')
function rep(from, to) {
if (!m.includes(from)) {
console.log('MISS ::', from.slice(0, 70).replace(/\n/g, '\\n'))
return
}
m = m.replace(from, to)
}
// 窗口声明
rep(
`let editorWindow: BrowserWindow | null = null`,
`let editorWindow: BrowserWindow | null = null
let musicWindow: BrowserWindow | null = null
let lyricWindow: BrowserWindow | null = null`
)
// 音乐目录与列表
rep(
`function createEditorWindow(): void {`,
`// ---------------------------------------------------------------- 本地音乐
const MUSIC_EXT = /\.(mp3|flac|wav|ogg|m4a|aac|opus)$/i
function musicConfigPath(): string {
return path.join(HOME, 'data', 'music.json')
}
function readMusicDir(): string {
try {
const cfg = JSON.parse(fs.readFileSync(musicConfigPath(), 'utf8')) as { dir?: string }
if (cfg.dir && fs.existsSync(cfg.dir)) return cfg.dir
} catch {
/* 忽略 */
}
return ''
}
function scanMusic(): { dir: string; tracks: Array<{ name: string; file: string }> } {
const dir = readMusicDir()
if (!dir) return { dir: '', tracks: [] }
const tracks: Array<{ name: string; file: string }> = []
const walk = (folder: string, rel: string): void => {
let entries: fs.Dirent[] = []
try {
entries = fs.readdirSync(folder, { withFileTypes: true })
} catch {
return
}
for (const e of entries) {
if (e.isDirectory()) walk(path.join(folder, e.name), rel + e.name + '/')
else if (MUSIC_EXT.test(e.name)) {
tracks.push({ name: e.name.replace(MUSIC_EXT, ''), file: rel + e.name })
}
}
}
walk(dir, '')
return { dir, tracks }
}
function createMusicWindow(): void {
if (musicWindow && !musicWindow.isDestroyed()) {
musicWindow.show()
musicWindow.focus()
return
}
musicWindow = new BrowserWindow({
width: 880,
height: 600,
minWidth: 700,
minHeight: 460,
show: false,
frame: false,
resizable: true,
backgroundColor: '#0d1017',
webPreferences: {
preload: path.join(__dirname, '../preload/index.js'),
contextIsolation: true,
nodeIntegration: false,
sandbox: false
}
})
musicWindow.loadFile(path.join(__dirname, '../renderer/index.html'), {
query: { view: 'music' }
})
musicWindow.once('ready-to-show', () => musicWindow?.show())
musicWindow.on('closed', () => {
musicWindow = null
})
}
function createLyricWindow(): void {
if (lyricWindow && !lyricWindow.isDestroyed()) return
lyricWindow = new BrowserWindow({
width: 560,
height: 110,
show: true,
frame: false,
transparent: true,
resizable: false,
alwaysOnTop: true,
skipTaskbar: true,
hasShadow: false,
webPreferences: {
preload: path.join(__dirname, '../preload/index.js'),
contextIsolation: true,
nodeIntegration: false,
sandbox: false
}
})
lyricWindow.setAlwaysOnTop(true, 'screen-saver')
lyricWindow.setIgnoreMouseEvents(false)
lyricWindow.loadFile(path.join(__dirname, '../renderer/index.html'), {
query: { view: 'lyric' }
})
lyricWindow.on('closed', () => {
lyricWindow = null
})
}
function createEditorWindow(): void {`
)
// IPC
rep(
` ipcMain.on('pet:storage:open', () => createStorageWindow())`,
` ipcMain.on('pet:storage:open', () => createStorageWindow())
ipcMain.on('pet:music:open', () => createMusicWindow())
ipcMain.on('pet:lyric:open', () => createLyricWindow())
ipcMain.on('pet:lyric:close', () => {
lyricWindow?.close()
})
ipcMain.handle('pet:music:choose', async () => {
const res = await dialog.showOpenDialog({
title: '选择本地音乐文件夹',
properties: ['openDirectory']
})
if (res.canceled || !res.filePaths.length) return scanMusic()
try {
fs.writeFileSync(
musicConfigPath(),
JSON.stringify({ dir: res.filePaths[0] }, null, 2),
'utf8'
)
} catch (err) {
console.error('[music] 保存目录失败', err)
}
const data = scanMusic()
for (const w of BrowserWindow.getAllWindows()) {
if (!w.isDestroyed()) w.webContents.send('pet:music:list', data)
}
return data
})
ipcMain.handle('pet:music:list', () => scanMusic())
ipcMain.handle('pet:music:lyric', (_e, file: string) => {
try {
const dir = readMusicDir()
if (!dir) return ''
const rel = String(file).replace(MUSIC_EXT, '.lrc')
const p = path.join(dir, rel)
if (!isInside(dir, p)) return ''
if (!fs.existsSync(p)) return ''
return fs.readFileSync(p, 'utf8')
} catch {
return ''
}
})
ipcMain.on('pet:music:now', (_e, payload: unknown) => {
for (const w of BrowserWindow.getAllWindows()) {
if (!w.isDestroyed()) w.webContents.send('pet:music:now', payload)
}
})`
)
// 协议 music host
rep(
` } else if (url.host === 'data') {`,
` } else if (url.host === 'music') {
const dir = readMusicDir()
if (!dir) return new Response('no music dir', { status: 404 })
abs = path.join(dir, rel)
root = dir
} else if (url.host === 'data') {`
)
fs.writeFileSync(mainPath, m, 'utf8')
console.log('music main patched')