/*: * @target MZ * @plugindesc Spine処理およびイベント進行の診断ログを save/spine_debug_trace.log へ出力します(調査完了後に削除・無効化可能)。 * @author GameLauncher */ (() => { 'use strict'; let fs = null; let path = null; try { fs = require('fs'); path = require('path'); } catch (e) { return; } function resolveLogPath() { const candidates = []; try { if (typeof StorageManager !== 'undefined' && typeof StorageManager.fileDirectoryPath === 'function') { candidates.push(StorageManager.fileDirectoryPath()); } } catch (e) {} try { if (typeof process !== 'undefined' && typeof process.cwd === 'function') { candidates.push(path.join(process.cwd(), 'save')); } } catch (e) {} for (const dir of candidates) { if (!dir) continue; try { if (!fs.existsSync(dir)) { fs.mkdirSync(dir, { recursive: true }); } return path.join(dir, 'spine_debug_trace.log'); } catch (e) {} } return null; } const logFilePath = resolveLogPath(); if (!logFilePath) return; try { if (fs.existsSync(logFilePath) && fs.statSync(logFilePath).size > 5 * 1024 * 1024) { fs.renameSync(logFilePath, logFilePath + '.old'); } } catch (e) {} function frame() { return (typeof Graphics !== 'undefined' && typeof Graphics.frameCount === 'number') ? Graphics.frameCount : -1; } function writeLog(tag, msg, detail) { try { const ts = new Date().toISOString(); let line = `[${ts}][F:${frame()}][${tag}] ${msg}`; if (detail !== undefined) { try { line += ' | ' + (typeof detail === 'string' ? detail : JSON.stringify(detail)); } catch (e) { line += ' | [unserializable]'; } } fs.appendFileSync(logFilePath, line + '\r\n', 'utf8'); } catch (e) {} } writeLog('INIT', 'SpineDebugTrace 起動', { logFilePath }); // グローバルエラー・警告フック window.addEventListener('error', (ev) => { const err = ev && ev.error ? ev.error : ev; writeLog('WINDOW_ERROR', ev && ev.message ? ev.message : 'unknown', { file: ev && ev.filename, line: ev && ev.lineno, stack: err && err.stack ? String(err.stack) : '' }); }); window.addEventListener('unhandledrejection', (ev) => { const r = ev && ev.reason; writeLog('UNHANDLED_REJECTION', r && r.message ? r.message : String(r), { stack: r && r.stack ? String(r.stack) : '' }); }); const _origWarn = console.warn; console.warn = function (...args) { try { const text = args.map(a => typeof a === 'string' ? a : String(a)).join(' '); if (/SpineFix|Animation not found|GameLauncher|Error|Spine/i.test(text)) { writeLog('CONSOLE_WARN', text); } } catch (e) {} return _origWarn.apply(this, args); }; const _origError = console.error; console.error = function (...args) { try { const text = args.map(a => typeof a === 'string' ? a : (a && a.stack ? a.stack : String(a))).join(' '); writeLog('CONSOLE_ERROR', text); } catch (e) {} return _origError.apply(this, args); }; // SceneManager.catchException フック if (typeof SceneManager !== 'undefined' && SceneManager.catchException) { const _catchEx = SceneManager.catchException; SceneManager.catchException = function (e) { writeLog('SCENE_EXCEPTION', e && e.message ? e.message : String(e), { stack: e && e.stack ? String(e.stack) : '' }); return _catchEx.apply(this, arguments); }; } // スイッチ・変数監視 const watchSwitches = new Set([8, 10, 11, 15, 20, 21, 50, 51, 58, 521]); const watchVars = new Set([51, 54, 55, 100, 101, 102]); const _swSetValue = Game_Switches.prototype.setValue; Game_Switches.prototype.setValue = function (switchId, value) { const prev = this.value(switchId); const res = _swSetValue.apply(this, arguments); const next = this.value(switchId); if (watchSwitches.has(Number(switchId)) && prev !== next) { writeLog('SWITCH', `S[${switchId}] ${prev} -> ${next}`); } return res; }; const _varSetValue = Game_Variables.prototype.setValue; Game_Variables.prototype.setValue = function (variableId, value) { const prev = this.value(variableId); const res = _varSetValue.apply(this, arguments); const next = this.value(variableId); if (watchVars.has(Number(variableId)) && prev !== next) { writeLog('VAR', `V[${variableId}] ${JSON.stringify(prev)} -> ${JSON.stringify(next)}`); } return res; }; // BattleManager フェーズ監視 let _lastBattlePhase = ''; const _bmUpdate = BattleManager.update; BattleManager.update = function () { if (this._phase !== _lastBattlePhase) { _lastBattlePhase = this._phase; writeLog('BATTLE_PHASE', `phase=${this._phase}`, { busy: this.isBusy ? this.isBusy() : null, troopRunning: typeof $gameTroop !== 'undefined' && $gameTroop && $gameTroop._interpreter ? $gameTroop._interpreter.isRunning() : null }); } return _bmUpdate.apply(this, arguments); }; // Game_Interpreter 監視(セットアップ・コマンド実行・例外・停滞スナップショット) const _giSetup = Game_Interpreter.prototype.setup; Game_Interpreter.prototype.setup = function (list, eventId) { const res = _giSetup.apply(this, arguments); writeLog('INTERP_SETUP', `depth=${this._depth} eventId=${eventId || this._eventId} len=${list ? list.length : 0}`); return res; }; const _giTerminate = Game_Interpreter.prototype.terminate; Game_Interpreter.prototype.terminate = function () { writeLog('INTERP_END', `depth=${this._depth} eventId=${this._eventId}`); return _giTerminate.apply(this, arguments); }; const _giExec = Game_Interpreter.prototype.executeCommand; Game_Interpreter.prototype.executeCommand = function () { const cmd = this.currentCommand(); const idx = this._index; try { if (cmd) { if (cmd.code === 117) { writeLog('INTERP_CE', `depth=${this._depth} idx=${idx} call CE[${cmd.parameters[0]}]`); } else if (cmd.code === 355) { const line = String(cmd.parameters[0] || ''); if (/spine|setSkeleton|setAnimation|setSkin|PluginManager/i.test(line)) { writeLog('INTERP_SCRIPT', `depth=${this._depth} idx=${idx}`, line); } } } return _giExec.apply(this, arguments); } catch (e) { writeLog('INTERP_ERROR', `depth=${this._depth} idx=${idx} code=${cmd ? cmd.code : -1} err=${e && e.message}`, { stack: e && e.stack ? String(e.stack) : '' }); throw e; } }; // 停滞監視(バトル中のインタープリタや並列イベントが30フレーム以上止まっているように見える場合のスナップショット) let _lastSnapshotFrame = -999; const _sbUpdate = Scene_Base.prototype.update; Scene_Base.prototype.update = function () { const res = _sbUpdate.apply(this, arguments); try { const f = frame(); if (f - _lastSnapshotFrame >= 60 && typeof $gameSwitches !== 'undefined' && $gameSwitches) { const s15 = $gameSwitches.value(15); const s20 = $gameSwitches.value(20); const s51 = $gameSwitches.value(51); const troopInterp = typeof $gameTroop !== 'undefined' && $gameTroop && $gameTroop._interpreter; const running = troopInterp && troopInterp.isRunning(); if (s15 || s20 || s51 || running) { _lastSnapshotFrame = f; const cmd = running ? troopInterp.currentCommand() : null; let pic1Spine = null; try { const p1 = $gameScreen && $gameScreen.picture(1); if (p1 && p1._spine) { pic1Spine = { skel: p1._spine._skeleton, track0: p1._spine._track && p1._spine._track[0] ? p1._spine._track[0].list.map(x => x.name) : null, play0: p1._spine._playData && p1._spine._playData[0] ? p1._spine._playData[0].map(x => x.name + ':' + x.state + '@' + Number(x.trackTime).toFixed(2)) : null }; } } catch (e) {} writeLog('SNAPSHOT', `phase=${BattleManager._phase} troopRun=${running} idx=${troopInterp ? troopInterp._index : -1} code=${cmd ? cmd.code : -1}`, { S15: s15, S20: s20, S51: s51, V54: $gameVariables ? $gameVariables.value(54) : null, V55: $gameVariables ? $gameVariables.value(55) : null, V102: $gameVariables ? $gameVariables.value(102) : null, pic1Spine }); } } } catch (e) {} return res; }; // PictureSpine フック (Game_Spine & Sprite_Spine) function hookPictureSpine() { if (typeof Game_Spine !== 'undefined' && !Game_Spine.__glTraceHooked) { Game_Spine.__glTraceHooked = true; const _gsSetSkel = Game_Spine.prototype.setSkeleton; Game_Spine.prototype.setSkeleton = function (name) { const before = this._skeleton; const res = _gsSetSkel.apply(this, arguments); writeLog('GS_SETSKEL', `name=${name} (${before} -> ${this._skeleton})`); return res; }; const _gsSetAnim = Game_Spine.prototype.setAnimation; Game_Spine.prototype.setAnimation = function (id, animations, ...args) { const before = this._skeleton; const res = _gsSetAnim.apply(this, arguments); writeLog('GS_SETANIM', `track=${id} anim=${JSON.stringify(animations)} args=${JSON.stringify(args)} skel=${before}->${this._skeleton}`); return res; }; } if (typeof Sprite_Spine !== 'undefined' && !Sprite_Spine.__glTraceHooked) { Sprite_Spine.__glTraceHooked = true; const _ssUpdate = Sprite_Spine.prototype.update; Sprite_Spine.prototype.update = function () { try { return _ssUpdate.apply(this, arguments); } catch (e) { writeLog('SS_UPDATE_ERROR', e && e.message ? e.message : String(e), { picId: this._pictureId, skel: this._skeleton, stack: e && e.stack ? String(e.stack) : '' }); throw e; } }; const _ssUpdSkel = Sprite_Spine.prototype.updateSkeleton; Sprite_Spine.prototype.updateSkeleton = function (spine) { const prevSkel = this._skeleton; const nextSkel = spine ? spine.skeleton : null; const res = _ssUpdSkel.apply(this, arguments); if (nextSkel && nextSkel !== prevSkel) { writeLog('SS_UPDSKEL', `pic=${this._pictureId} ${prevSkel} -> ${this._skeleton} (target=${nextSkel}, hasData=${!!this._data})`); } return res; }; const _ssUpdAnim = Sprite_Spine.prototype.updateAnimation; Sprite_Spine.prototype.updateAnimation = function (spine) { const changed = spine && spine.track !== this._track; const res = _ssUpdAnim.apply(this, arguments); if (changed) { try { const tracks = this._data && this._data.state ? this._data.state.tracks.map(t => t && t.animation ? t.animation.name : null) : null; writeLog('SS_UPDANIM', `pic=${this._pictureId} skel=${this._skeleton}`, { tracks }); } catch (e) {} } return res; }; const _ssOnEvent = Sprite_Spine.prototype.onEvent; Sprite_Spine.prototype.onEvent = function (entry, event) { writeLog('SS_EVENT', `pic=${this._pictureId} anim=${entry && entry.animation ? entry.animation.name : '?'} ev=${event && event.data ? event.data.name : '?'} str=${event ? event.stringValue : ''}`); return _ssOnEvent.apply(this, arguments); }; const _ssOnComplete = Sprite_Spine.prototype.onComplete; Sprite_Spine.prototype.onComplete = function (entry) { if (entry && !entry.loop) { writeLog('SS_COMPLETE', `pic=${this._pictureId} track=${entry.trackIndex} anim=${entry.animation ? entry.animation.name : '?'}`); } return _ssOnComplete.apply(this, arguments); }; } } const _smInit = SceneManager.initialize; SceneManager.initialize = function () { const res = _smInit.apply(this, arguments); hookPictureSpine(); return res; }; hookPictureSpine(); })();