Files

164 lines
5.3 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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) 适配器:无校准的导入模型自动量包围盒并持久化
patch('renderer/src/pet/adapters/live2d.ts', [
[
` lookAt(x: number, y: number): void {`,
` private autoCalibrated = false
/** 导入模型没有校准值时,渲染一帧量出内容包围盒,自动校准并写回皮肤清单 */
tryAutoCalibrate(): void {
if (this.autoCalibrated || this.ctx.skin.avatar.calibration) return
try {
const renderer = this.app.renderer
const gl = renderer.gl
try {
this.model.internalModel?.motionManager?.stopAllMotions?.()
} catch {
/* 忽略 */
}
renderer.render(this.app.stage)
const res = renderer.resolution || 1
const w = Math.floor(renderer.width * res)
const h = Math.floor(renderer.height * res)
const buf = new Uint8Array(w * h * 4)
gl.bindFramebuffer(gl.FRAMEBUFFER, null)
gl.readPixels(0, 0, w, h, gl.RGBA, gl.UNSIGNED_BYTE, buf)
let minX = 1e9
let minY = 1e9
let maxX = -1
let maxY = -1
for (let y = 0; y < h; y++) {
const row = (h - 1 - y) * w * 4
for (let x = 0; x < w; x++) {
if (buf[row + x * 4 + 3] > 16) {
if (x < minX) minX = x
if (x > maxX) maxX = x
if (y < minY) minY = y
if (y > maxY) maxY = y
}
}
}
if (!Number.isFinite(minX) || maxX <= minX || maxY <= minY) return
const sx0 = minX / res
const sx1 = maxX / res
const sy0 = minY / res
const sy1 = maxY / res
const s0 = this.model.scale.x || 1
const lx0 = (sx0 - this.model.x) / s0
const lx1 = (sx1 - this.model.x) / s0
const ly0 = (sy0 - this.model.y) / s0
const ly1 = (sy1 - this.model.y) / s0
const H = Math.max(1, ly1 - ly0)
const refHeight = 470
const cal = {
scale: (0.92 * refHeight) / H,
centerX: (lx0 + lx1) / 2,
bottom: ly1,
refHeight
}
this.ctx.skin.avatar.calibration = cal
this.autoCalibrated = true
this.layout()
console.log('[live2d] 自动校准完成', JSON.stringify(cal))
try {
;(window as unknown as { pet?: { saveCalibration?: (id: string, c: unknown) => void } }).pet?.saveCalibration?.(
this.ctx.skin.id,
cal
)
} catch {
/* 忽略 */
}
} catch (err) {
console.warn('[live2d] 自动校准失败', err)
}
}
lookAt(x: number, y: number): void {`
],
[
` void adapter.play(skin.motions?.idle ?? 'Idle', true)` // 在 App 里,这里不改
]
])
// App:挂载后触发自动校准
patch('renderer/src/App.tsx', [
[
` void adapter.play(skin.motions?.idle ?? 'Idle', true)
loadedSkinRef.current = cfgRef.current.currentSkin || 'xiaona'`,
` void adapter.play(skin.motions?.idle ?? 'Idle', true)
loadedSkinRef.current = cfgRef.current.currentSkin || 'xiaona'
if (!skin.avatar.calibration) {
window.setTimeout(() => adapterRef.current?.tryAutoCalibrate?.(), 700)
}`
]
])
// 接口声明
patch('renderer/src/pet/types.ts', [
[
` /** Live2D:视线看向画布内某点(CSS 坐标,相对画布左上角) */
lookAt?(x: number, y: number): void`,
` /** Live2D:视线看向画布内某点(CSS 坐标,相对画布左上角) */
lookAt?(x: number, y: number): void
/** 无校准值的导入模型:自动测量包围盒 */
tryAutoCalibrate?(): void`
]
])
// main:校准持久化 IPC
patch('main/index.ts', [
[
` ipcMain.handle('pet:skins:rename', (_e, id: string, name: string) => {`,
` ipcMain.handle('pet:skin:calibrate', (_e, id: string, cal: unknown) => {
const p = path.join(userSkins(), sanitizeId(String(id)), 'skin.json')
try {
const m = JSON.parse(fs.readFileSync(p, 'utf8'))
m.avatar = { ...(m.avatar || {}), calibration: cal }
fs.writeFileSync(p, JSON.stringify(m, null, 2), 'utf8')
console.log('[skin] 校准已保存:', String(id))
} catch (err) {
console.error('[skin] 校准保存失败', err)
}
})
ipcMain.handle('pet:skins:rename', (_e, id: string, name: string) => {`
]
])
// preload / api:saveCalibration
patch('preload/index.ts', [
[
" importPhotos: () => Promise<PhotoMeta[]>",
" importPhotos: () => Promise<PhotoMeta[]>\n saveCalibration: (id: string, cal: unknown) => void"
],
[
" importPhotos: () => ipcRenderer.invoke('pet:photos:import'),",
" importPhotos: () => ipcRenderer.invoke('pet:photos:import'),\n saveCalibration: (id: string, cal: unknown) => ipcRenderer.invoke('pet:skin:calibrate', id, cal),"
]
])
patch('renderer/src/api.ts', [
[
" importPhotos: () => Promise<PhotoMeta[]>",
" importPhotos: () => Promise<PhotoMeta[]>\n saveCalibration: (id: string, cal: unknown) => void"
]
])
console.log('auto-calibrate patched')