// Engines/Tkool/TkoolMzFixService.cs using System; using System.IO; using System.Text; using System.Text.RegularExpressions; namespace GameLauncher.Engines.Tkool { /// /// RPGツクールMZ専用のPixi.js・Spineエラー修復およびDevTools有効化サービス。 /// マップ移動時に発生する Pixi.js の "Cannot read property 'valid' of null" クラッシュ、 /// および Spine の "Animation not found" クラッシュを防止し、開発者ツール(DevTools)の制限を解除します。 /// public static class TkoolMzFixService { #region □定数(Private) private const string BackupSuffix = ".bak"; private const string SpineFixMarker = "/* GameLauncher_SpineFix */"; #endregion #region □メソッド(Public) /// /// 指定されたゲームディレクトリに対して、Pixi.js のエラー修復および DevTools 有効化を適用します。 /// /// ゲームのルートディレクトリ /// 処理成否およびメッセージ public static (bool Success, string Message) ApplyMzPixiFix(string rootDir) { if (string.IsNullOrEmpty(rootDir) || !Directory.Exists(rootDir)) { return (false, "ゲームディレクトリが存在しません。"); } int patchedCount = 0; var sb = new StringBuilder(); // 1. package.json の --disable-devtools 除去 var packageJsonPath = Path.Combine(rootDir, "package.json"); if (File.Exists(packageJsonPath)) { try { if (PatchPackageJson(packageJsonPath)) { patchedCount++; sb.Append("DevTools有効化, "); } } catch (Exception ex) { sb.Append($"package.json修復失敗({ex.Message}), "); } } // 2. js/libs/pixi.js の null安全パッチ適用 var pixiJsPath = Path.Combine(rootDir, "js", "libs", "pixi.js"); if (File.Exists(pixiJsPath)) { try { if (PatchPixiJs(pixiJsPath)) { patchedCount++; sb.Append("pixi.js安全化, "); } } catch (Exception ex) { sb.Append($"pixi.js修復失敗({ex.Message}), "); } } // 3. js/libs/pixi-picture.js の null安全パッチ適用 var pixiPicturePath = Path.Combine(rootDir, "js", "libs", "pixi-picture.js"); if (File.Exists(pixiPicturePath)) { try { if (PatchPixiPictureJs(pixiPicturePath)) { patchedCount++; sb.Append("pixi-picture.js安全化, "); } } catch (Exception ex) { sb.Append($"pixi-picture.js修復失敗({ex.Message}), "); } } if (patchedCount > 0) { var summary = sb.ToString().TrimEnd(' ', ','); return (true, $"ツクールMZ修復パッチを適用しました: [{summary}]"); } return (true, "ツクールMZ修復パッチは既に適用済みです。"); } /// /// バックアップファイルからオリジナル状態への復元を行います。 /// /// ゲームのルートディレクトリ /// 処理成否およびメッセージ public static (bool Success, string Message) RestoreMzPixiFix(string rootDir) { if (string.IsNullOrEmpty(rootDir) || !Directory.Exists(rootDir)) { return (false, "ゲームディレクトリが存在しません。"); } int restoredCount = 0; // 1. package.json の復元 var packageJsonPath = Path.Combine(rootDir, "package.json"); var packageJsonBak = packageJsonPath + BackupSuffix; if (File.Exists(packageJsonBak)) { try { File.Copy(packageJsonBak, packageJsonPath, true); restoredCount++; } catch { } } // 2. js/libs/pixi.js の復元 var pixiJsPath = Path.Combine(rootDir, "js", "libs", "pixi.js"); var pixiJsBak = pixiJsPath + BackupSuffix; if (File.Exists(pixiJsBak)) { try { File.Copy(pixiJsBak, pixiJsPath, true); restoredCount++; } catch { } } // 3. js/libs/pixi-picture.js の復元 var pixiPicturePath = Path.Combine(rootDir, "js", "libs", "pixi-picture.js"); var pixiPictureBak = pixiPicturePath + BackupSuffix; if (File.Exists(pixiPictureBak)) { try { File.Copy(pixiPictureBak, pixiPicturePath, true); restoredCount++; } catch { } } return (true, $"ツクールMZ修復パッチを元に戻しました(復元ファイル数: {restoredCount})"); } /// /// 指定されたゲームディレクトリに対して、Spineアニメーション未定義エラー(Animation not found 等)の修復パッチを適用します。 /// /// ゲームのルートディレクトリ /// 処理成否およびメッセージ public static (bool Success, string Message) ApplyMzSpineFix(string rootDir) { if (string.IsNullOrEmpty(rootDir) || !Directory.Exists(rootDir)) { return (false, "ゲームディレクトリが存在しません。"); } bool hasTargetFile = false; int patchedCount = 0; var sb = new StringBuilder(); // 1. js/libs/pixi-spine.js の未定義アニメーション安全化パッチ適用 var pixiSpinePath = Path.Combine(rootDir, "js", "libs", "pixi-spine.js"); if (File.Exists(pixiSpinePath)) { hasTargetFile = true; try { if (PatchPixiSpineJs(pixiSpinePath)) { patchedCount++; sb.Append("pixi-spine.js安全化, "); } } catch (Exception ex) { sb.Append($"pixi-spine.js修復失敗({ex.Message}), "); } } // 2. js/plugins/PictureSpine.js のスケルトン整合・null安全化パッチ適用 var pictureSpinePath = Path.Combine(rootDir, "js", "plugins", "PictureSpine.js"); if (File.Exists(pictureSpinePath)) { hasTargetFile = true; try { if (PatchPictureSpineJs(pictureSpinePath)) { patchedCount++; sb.Append("PictureSpine.js安全化, "); } } catch (Exception ex) { sb.Append($"PictureSpine.js修復失敗({ex.Message}), "); } } if (!hasTargetFile) { return (true, string.Empty); } if (patchedCount > 0) { var summary = sb.ToString().TrimEnd(' ', ','); return (true, $"ツクールMZ Spine修復パッチを適用しました: [{summary}]"); } return (true, "ツクールMZ Spine修復パッチは既に適用済みです。"); } /// /// Spine修復パッチのバックアップファイルからオリジナル状態への復元を行います。 /// /// ゲームのルートディレクトリ /// 処理成否およびメッセージ public static (bool Success, string Message) RestoreMzSpineFix(string rootDir) { if (string.IsNullOrEmpty(rootDir) || !Directory.Exists(rootDir)) { return (false, "ゲームディレクトリが存在しません。"); } int restoredCount = 0; var pixiSpinePath = Path.Combine(rootDir, "js", "libs", "pixi-spine.js"); var pixiSpineBak = pixiSpinePath + BackupSuffix; if (File.Exists(pixiSpineBak)) { try { File.Copy(pixiSpineBak, pixiSpinePath, true); restoredCount++; } catch { } } var pictureSpinePath = Path.Combine(rootDir, "js", "plugins", "PictureSpine.js"); var pictureSpineBak = pictureSpinePath + BackupSuffix; if (File.Exists(pictureSpineBak)) { try { File.Copy(pictureSpineBak, pictureSpinePath, true); restoredCount++; } catch { } } if (restoredCount == 0) { return (true, string.Empty); } return (true, $"ツクールMZ Spine修復パッチを元に戻しました(復元ファイル数: {restoredCount})"); } #endregion #region □メソッド(Private) private static bool PatchPackageJson(string path) { string content = File.ReadAllText(path, Encoding.UTF8); if (!content.Contains("--disable-devtools")) { return false; } var bakPath = path + BackupSuffix; if (!File.Exists(bakPath)) { File.Copy(path, bakPath, true); } string updated = content.Replace("--disable-devtools", string.Empty); updated = updated.Replace(" ", " "); File.WriteAllText(path, updated, new UTF8Encoding(true)); return true; } private static bool PatchPixiJs(string path) { string content = File.ReadAllText(path, Encoding.UTF8); bool modified = false; var bakPath = path + BackupSuffix; if (!File.Exists(bakPath)) { File.Copy(path, bakPath, true); } // A. AbstractBatchRenderer.prototype.render if (content.Contains("if (!element._texture.valid) {")) { content = content.Replace("if (!element._texture.valid) {", "if (!element || !element._texture || !element._texture.valid) {"); modified = true; } // B. SpriteMaskFilter.prototype.apply if (content.Contains("var maskSprite = this.maskSprite;\n\t var tex = maskSprite._texture;\n\t if (!tex.valid) {")) { content = content.Replace( "var maskSprite = this.maskSprite;\n\t var tex = maskSprite._texture;\n\t if (!tex.valid) {", "var maskSprite = this.maskSprite;\n\t var tex = maskSprite ? maskSprite._texture : null;\n\t if (!tex || !tex.valid) {"); modified = true; } else if (content.Contains("var maskSprite = this.maskSprite;\r\n\t var tex = maskSprite._texture;\r\n\t if (!tex.valid) {")) { content = content.Replace( "var maskSprite = this.maskSprite;\r\n\t var tex = maskSprite._texture;\r\n\t if (!tex.valid) {", "var maskSprite = this.maskSprite;\r\n\t var tex = maskSprite ? maskSprite._texture : null;\r\n\t if (!tex || !tex.valid) {"); modified = true; } // C. Texture constructor if (content.Contains("if (!baseTexture.valid) {\n\t baseTexture.once('loaded', _this.onBaseTextureUpdated, _this);")) { content = content.Replace( "if (!baseTexture.valid) {\n\t baseTexture.once('loaded', _this.onBaseTextureUpdated, _this);", "if (!baseTexture || !baseTexture.valid) {\n\t if (baseTexture) baseTexture.once('loaded', _this.onBaseTextureUpdated, _this);"); modified = true; } else if (content.Contains("if (!baseTexture.valid) {\r\n\t baseTexture.once('loaded', _this.onBaseTextureUpdated, _this);")) { content = content.Replace( "if (!baseTexture.valid) {\r\n\t baseTexture.once('loaded', _this.onBaseTextureUpdated, _this);", "if (!baseTexture || !baseTexture.valid) {\r\n\t if (baseTexture) baseTexture.once('loaded', _this.onBaseTextureUpdated, _this);"); modified = true; } // D. Texture.prototype.onBaseTextureUpdated if (content.Contains("if (!this.baseTexture.valid) {")) { content = content.Replace("if (!this.baseTexture.valid) {", "if (!this.baseTexture || !this.baseTexture.valid) {"); modified = true; } // E. Graphics if (content.Contains("if (fill && !fill.texture.baseTexture.valid)")) { content = content.Replace("if (fill && !fill.texture.baseTexture.valid)", "if (fill && (!fill.texture || !fill.texture.baseTexture || !fill.texture.baseTexture.valid))"); modified = true; } if (content.Contains("if (line && !line.texture.baseTexture.valid)")) { content = content.Replace("if (line && !line.texture.baseTexture.valid)", "if (line && (!line.texture || !line.texture.baseTexture || !line.texture.baseTexture.valid))"); modified = true; } // F. Sprite.prototype texture setter if (content.Contains("if (value.baseTexture.valid) {\n\t this._onTextureUpdate();\n\t }\n\t else {\n\t value.once('update', this._onTextureUpdate, this);")) { content = content.Replace( "if (value.baseTexture.valid) {\n\t this._onTextureUpdate();\n\t }\n\t else {\n\t value.once('update', this._onTextureUpdate, this);", "if (value.baseTexture && value.baseTexture.valid) {\n\t this._onTextureUpdate();\n\t }\n\t else if (value.baseTexture) {\n\t value.once('update', this._onTextureUpdate, this);"); modified = true; } else if (content.Contains("if (value.baseTexture.valid) {\r\n\t this._onTextureUpdate();\r\n\t }\r\n\t else {\r\n\t value.once('update', this._onTextureUpdate, this);")) { content = content.Replace( "if (value.baseTexture.valid) {\r\n\t this._onTextureUpdate();\r\n\t }\r\n\t else {\r\n\t value.once('update', this._onTextureUpdate, this);", "if (value.baseTexture && value.baseTexture.valid) {\r\n\t this._onTextureUpdate();\r\n\t }\r\n\t else if (value.baseTexture) {\r\n\t value.once('update', this._onTextureUpdate, this);"); modified = true; } // H. Texture constructor: baseTexture null safety (Cannot read property 'on' of null) if (content.Contains("if (baseTexture instanceof Texture) {\n\t baseTexture = baseTexture.baseTexture;\n\t }")) { content = content.Replace( "if (baseTexture instanceof Texture) {\n\t baseTexture = baseTexture.baseTexture;\n\t }", "if (baseTexture instanceof Texture) {\n\t baseTexture = baseTexture.baseTexture;\n\t }\n\t if (!baseTexture) {\n\t baseTexture = new BaseTexture();\n\t }"); modified = true; } else if (content.Contains("if (baseTexture instanceof Texture) {\r\n\t baseTexture = baseTexture.baseTexture;\r\n\t }")) { content = content.Replace( "if (baseTexture instanceof Texture) {\r\n\t baseTexture = baseTexture.baseTexture;\r\n\t }", "if (baseTexture instanceof Texture) {\r\n\t baseTexture = baseTexture.baseTexture;\r\n\t }\r\n\t if (!baseTexture) {\r\n\t baseTexture = new BaseTexture();\r\n\t }"); modified = true; } // I. Texture constructor: noFrame on update safety if (content.Contains("if (_this.noFrame) {\n\t baseTexture.on('update', _this.onBaseTextureUpdated, _this);\n\t }")) { content = content.Replace( "if (_this.noFrame) {\n\t baseTexture.on('update', _this.onBaseTextureUpdated, _this);\n\t }", "if (_this.noFrame && baseTexture) {\n\t baseTexture.on('update', _this.onBaseTextureUpdated, _this);\n\t }"); modified = true; } else if (content.Contains("if (_this.noFrame) {\r\n\t baseTexture.on('update', _this.onBaseTextureUpdated, _this);\r\n\t }")) { content = content.Replace( "if (_this.noFrame) {\r\n\t baseTexture.on('update', _this.onBaseTextureUpdated, _this);\r\n\t }", "if (_this.noFrame && baseTexture) {\r\n\t baseTexture.on('update', _this.onBaseTextureUpdated, _this);\r\n\t }"); modified = true; } // G. Mesh.prototype texture setter if (content.Contains("if (value.baseTexture.valid) {\n\t this.textureUpdated();\n\t }\n\t else {\n\t value.once('update', this.textureUpdated, this);")) { content = content.Replace( "if (value.baseTexture.valid) {\n\t this.textureUpdated();\n\t }\n\t else {\n\t value.once('update', this.textureUpdated, this);", "if (value && value.baseTexture && value.baseTexture.valid) {\n\t this.textureUpdated();\n\t }\n\t else if (value && value.baseTexture) {\n\t value.once('update', this.textureUpdated, this);"); modified = true; } else if (content.Contains("if (value.baseTexture.valid) {\r\n\t this.textureUpdated();\r\n\t }\r\n\t else {\r\n\t value.once('update', this.textureUpdated, this);")) { content = content.Replace( "if (value.baseTexture.valid) {\r\n\t this.textureUpdated();\r\n\t }\r\n\t else {\r\n\t value.once('update', this.textureUpdated, this);", "if (value && value.baseTexture && value.baseTexture.valid) {\r\n\t this.textureUpdated();\r\n\t }\r\n\t else if (value && value.baseTexture) {\r\n\t value.once('update', this.textureUpdated, this);"); modified = true; } if (modified) { File.WriteAllText(path, content, new UTF8Encoding(true)); } return modified; } private static bool PatchPixiPictureJs(string path) { string content = File.ReadAllText(path, Encoding.UTF8); if (!content.Contains("if (!sprite.texture.valid) {")) { return false; } var bakPath = path + BackupSuffix; if (!File.Exists(bakPath)) { File.Copy(path, bakPath, true); } string updated = content.Replace("if (!sprite.texture.valid) {", "if (!sprite || !sprite.texture.valid) {"); File.WriteAllText(path, updated, new UTF8Encoding(true)); return true; } private static bool PatchPixiSpineJs(string path) { string content = File.ReadAllText(path, Encoding.UTF8); if (content.Contains(SpineFixMarker)) { return false; } string updated = content; bool modified = false; const string setAnimReplacement = "var animation = this.data.skeletonData.findAnimation(animationName);\n" + " if (!animation) {\n" + " animation = typeof __glResolveSpineAnim === \"function\" ? __glResolveSpineAnim(this, trackIndex, animationName) : null;\n" + " if (!animation) {\n" + " if (typeof console !== \"undefined\" && console.warn) { console.warn(\"[GameLauncher SpineFix] Animation not found, using empty:\", animationName, \"track:\", trackIndex); }\n" + " return typeof this.setEmptyAnimation === \"function\" ? this.setEmptyAnimation(trackIndex, 0) : null;\n" + " }\n" + " }\n" + " return this.setAnimationWith(trackIndex, animation, loop);"; var setAnimRegex = new Regex( @"(?:var|let|const)\s+animation\s*=\s*this\.data\.skeletonData\.findAnimation\(animationName\);\s*if\s*\(\s*(?:animation\s*==\s*null|!animation)\s*\)\s*\{?\s*throw\s+new\s+Error\([""']Animation not found:\s*[""']\s*\+\s*animationName\);\s*\}?\s*return\s+this\.setAnimationWith\(trackIndex,\s*animation,\s*loop\);", RegexOptions.Singleline); string afterSet = setAnimRegex.Replace(updated, _ => setAnimReplacement); if (!string.Equals(afterSet, updated, StringComparison.Ordinal)) { updated = afterSet; modified = true; } const string addAnimReplacement = "var animation = this.data.skeletonData.findAnimation(animationName);\n" + " if (!animation) {\n" + " animation = typeof __glResolveSpineAnim === \"function\" ? __glResolveSpineAnim(this, trackIndex, animationName) : null;\n" + " if (!animation) {\n" + " if (typeof console !== \"undefined\" && console.warn) { console.warn(\"[GameLauncher SpineFix] Animation not found, using empty:\", animationName, \"track:\", trackIndex); }\n" + " return typeof this.addEmptyAnimation === \"function\" ? this.addEmptyAnimation(trackIndex, 0, delay) : null;\n" + " }\n" + " }\n" + " return this.addAnimationWith(trackIndex, animation, loop, delay);"; var addAnimRegex = new Regex( @"(?:var|let|const)\s+animation\s*=\s*this\.data\.skeletonData\.findAnimation\(animationName\);\s*if\s*\(\s*(?:animation\s*==\s*null|!animation)\s*\)\s*\{?\s*throw\s+new\s+Error\([""']Animation not found:\s*[""']\s*\+\s*animationName\);\s*\}?\s*return\s+this\.addAnimationWith\(trackIndex,\s*animation,\s*loop,\s*delay\);", RegexOptions.Singleline); string afterAdd = addAnimRegex.Replace(updated, _ => addAnimReplacement); if (!string.Equals(afterAdd, updated, StringComparison.Ordinal)) { updated = afterAdd; modified = true; } const string setMixReplacement = "var from = this.skeletonData.findAnimation(fromName);\n" + " if (!from) return;\n" + " var to = this.skeletonData.findAnimation(toName);\n" + " if (!to) return;\n" + " this.setMixWith(from, to, duration);"; var setMixRegex = new Regex( @"(?:var|let|const)\s+from\s*=\s*this\.skeletonData\.findAnimation\(fromName\);\s*if\s*\(\s*(?:from\s*==\s*null|!from)\s*\)\s*\{?\s*throw\s+new\s+Error\([""']Animation not found:\s*[""']\s*\+\s*fromName\);\s*\}?\s*(?:var|let|const)\s+to\s*=\s*this\.skeletonData\.findAnimation\(toName\);\s*if\s*\(\s*(?:to\s*==\s*null|!to)\s*\)\s*\{?\s*throw\s+new\s+Error\([""']Animation not found:\s*[""']\s*\+\s*toName\);\s*\}?\s*this\.setMixWith\(from,\s*to,\s*duration\);", RegexOptions.Singleline); string afterMix = setMixRegex.Replace(updated, _ => setMixReplacement); if (!string.Equals(afterMix, updated, StringComparison.Ordinal)) { updated = afterMix; modified = true; } if (!modified) { return false; } var bakPath = path + BackupSuffix; if (!File.Exists(bakPath)) { File.Copy(path, bakPath, true); } const string helperBlock = SpineFixMarker + "\n" + "function __glResolveSpineAnim(state, trackIndex, animationName) {\n" + " try {\n" + " var sd = state && state.data && state.data.skeletonData;\n" + " var anims = sd && sd.animations;\n" + " if (!Array.isArray(anims) || anims.length === 0) return null;\n" + " var req = String(animationName == null ? \"\" : animationName);\n" + " var alt = req.indexOf(\"chimpo\") >= 0 ? req.replace(/chimpo/g, \"chinpo\") : (req.indexOf(\"chinpo\") >= 0 ? req.replace(/chinpo/g, \"chimpo\") : \"\");\n" + " var find = function (fn) {\n" + " for (var i = 0; i < anims.length; i++) {\n" + " if (anims[i] && fn(anims[i])) return anims[i];\n" + " }\n" + " return null;\n" + " };\n" + " var found = find(function (a) { return a.name === req || (alt && a.name === alt); })\n" + " || find(function (a) { return typeof a.name === \"string\" && (a.name.endsWith(\"/\" + req) || (alt && a.name.endsWith(\"/\" + alt))); });\n" + " if (found) return found;\n" + " var isMainTrack = Number(trackIndex) === 0 || /idle/i.test(req);\n" + " if (isMainTrack) {\n" + " found = find(function (a) { return /(?:^|\\/)0_idle$/i.test(a.name); })\n" + " || find(function (a) { return /(?:^|\\/)idle$/i.test(a.name); })\n" + " || find(function (a) { return /idle/i.test(a.name); })\n" + " || find(function (a) { return /^(?:0\\/|0_move\\/|0_)/i.test(a.name); });\n" + " if (found) return found;\n" + " }\n" + " if (Number(trackIndex) === 1 || /^1_face/i.test(req)) {\n" + " found = find(function (a) { return /(?:^|\\/)1_face_normal$/i.test(a.name); })\n" + " || find(function (a) { return /(?:^|\\/)1_face_/i.test(a.name); });\n" + " if (found) return found;\n" + " }\n" + " found = find(function (a) { return a.name === \"none\"; });\n" + " if (found) return found;\n" + " if (Number(trackIndex) === 0) return anims[0];\n" + " } catch (e) {}\n" + " return null;\n" + "}\n"; string newline = content.Contains("\r\n") ? "\r\n" : "\n"; string normalizedHelper = helperBlock.Replace("\n", newline); updated = normalizedHelper + updated; File.WriteAllText(path, updated, new UTF8Encoding(true)); return true; } private static bool PatchPictureSpineJs(string path) { string content = File.ReadAllText(path, Encoding.UTF8); if (content.Contains(SpineFixMarker)) { return false; } string updated = content; bool modified = false; // 1. Game_Spine.setAnimation: velo_damage状態で通常立ち絵系アニメが指定された場合のスケルトン自動復旧 var setAnimRegex = new Regex( @"setAnimation\s*\(\s*id\s*,\s*animations\s*,\s*\.\.\.args\s*\)\s*\{\s*\[id,\s*animations,\s*args\]\s*=\s*convars\(\s*\[id,\s*animations,\s*args\]\s*\);", RegexOptions.Singleline); const string setAnimReplacement = "setAnimation(id, animations, ...args) {\n" + " " + SpineFixMarker + "\n" + " [id, animations, args] = convars([id, animations, args]);\n" + " if (Number(id) === 0 && typeof this._skeleton === 'string' && /^velo_damage_\\d+$/.test(this._skeleton)) {\n" + " let _firstAnim = Array.isArray(animations) ? animations[0] : animations;\n" + " let _animBase = typeof _firstAnim === 'string' ? _firstAnim.split(/\\s*\\/\\s*/)[0].trim() : '';\n" + " if (/^0_(?:idle|low|yoin|shy|talk|bind)/i.test(_animBase)) {\n" + " let _targetSkel = (typeof $gameParty !== 'undefined' && $gameParty && $gameParty.members && $gameParty.members()[0] && $gameParty.members()[0].hp === 0) ? 'velo_hunou' : 'velo_normal';\n" + " if (typeof Game_Spine !== 'undefined' && Game_Spine.fullName && Game_Spine.fullName(_targetSkel)) {\n" + " this._skeleton = _targetSkel + '_' + Date.now();\n" + " }\n" + " }\n" + " }"; string afterSetAnim = setAnimRegex.Replace(updated, _ => setAnimReplacement); if (!string.Equals(afterSetAnim, updated, StringComparison.Ordinal)) { updated = afterSetAnim; modified = true; } // 1.5. Sprite_Spine.updateSkeleton: スケルトン切り替え破棄前に未発火のswitchイベントを完了させ、イベントループ停止を防止 var updateSkelRegex = new Regex( @"updateSkeleton\s*\(\s*spine\s*\)\s*\{\s*if\s*\(\s*spine\s*\)\s*\{\s*if\s*\(\s*spine\.skeleton\s*!=\s*this\._skeleton\s*\)\s*\{\s*this\.init\(\s*\);", RegexOptions.Singleline); const string updateSkelReplacement = "updateSkeleton(spine) {\n" + " if (spine) {\n" + " if (spine.skeleton != this._skeleton) {\n" + " try {\n" + " if (this._data && this._data.state && Array.isArray(this._data.state.tracks) && typeof $gameSwitches !== 'undefined') {\n" + " for (let _glEntry of this._data.state.tracks) {\n" + " if (!_glEntry || !_glEntry.animation || !Array.isArray(_glEntry.animation.timelines)) continue;\n" + " for (let _glTl of _glEntry.animation.timelines) {\n" + " if (!_glTl || !Array.isArray(_glTl.events) || !_glTl.frames || typeof _glTl.frames.length !== 'number') continue;\n" + " for (let _glIdx = 0; _glIdx < _glTl.events.length; _glIdx++) {\n" + " if (_glTl.frames[_glIdx] < _glEntry.trackTime) continue;\n" + " let _glEv = _glTl.events[_glIdx];\n" + " let _glStr = _glEv && _glEv.stringValue;\n" + " if (!_glStr) continue;\n" + " for (let _glTok of String(_glStr).replace(/ +/, '').split(/,/)) {\n" + " if (_glTok.match(/^([!~]?)switch:(\\d+)$/i)) {\n" + " let _glType = RegExp.$1.replace('!', 'off').replace('~', 'reverse') || 'on';\n" + " let _glSwId = Number(RegExp.$2);\n" + " let _glVal = _glType == 'on' ? true : _glType == 'off' ? false : !$gameSwitches.value(_glSwId);\n" + " $gameSwitches.setValue(_glSwId, _glVal);\n" + " }\n" + " }\n" + " }\n" + " }\n" + " }\n" + " }\n" + " } catch (_glErr) {}\n" + " this.init();"; string afterUpdateSkel = updateSkelRegex.Replace(updated, _ => updateSkelReplacement); if (!string.Equals(afterUpdateSkel, updated, StringComparison.Ordinal)) { updated = afterUpdateSkel; modified = true; } // 2. updateSkin: findSkin null安全化 var skinRegex = new Regex( @"skin\.addSkin\(\s*this\._data\.skeleton\.data\.findSkin\(\s*name\s*\)\s*\);", RegexOptions.Singleline); const string skinReplacement = "let _glSkin = this._data.skeleton.data.findSkin(name);\n" + " if (_glSkin) skin.addSkin(_glSkin);"; string afterSkin = skinRegex.Replace(updated, _ => skinReplacement); if (!string.Equals(afterSkin, updated, StringComparison.Ordinal)) { updated = afterSkin; modified = true; } // 3. updateAnimation: entry null安全化 var entryRegex = new Regex( @"entry\.timeScale\s*=\s*animation\.timeScale;\s*entry\.alpha\s*=\s*animation\.alpha;\s*entry\.plainAlpha\s*=\s*animation\.alpha;\s*spine\.setPlayData\(\s*entry\s*\);", RegexOptions.Singleline); const string entryReplacement = "if (entry) {\n" + " entry.timeScale = animation.timeScale;\n" + " entry.alpha = animation.alpha;\n" + " entry.plainAlpha = animation.alpha;\n" + " spine.setPlayData(entry);\n" + " }"; string afterEntry = entryRegex.Replace(updated, _ => entryReplacement); if (!string.Equals(afterEntry, updated, StringComparison.Ordinal)) { updated = afterEntry; modified = true; } // 4. restoreAnimation: animations.find(...).duration および entry の null安全化 var restoreRegex = new Regex( @"data\.trackTime\s*%=\s*this\._data\.spineData\.animations\.find\(\s*animation\s*=>\s*animation\.name\s*==\s*data\.name\s*\)\.duration;", RegexOptions.Singleline); const string restoreReplacement = "let _glAnim = this._data.spineData.animations.find(animation => animation.name == data.name);\n" + " data.trackTime = (_glAnim && _glAnim.duration) ? (data.trackTime % _glAnim.duration) : 0;"; string afterRestore = restoreRegex.Replace(updated, _ => restoreReplacement); if (!string.Equals(afterRestore, updated, StringComparison.Ordinal)) { updated = afterRestore; modified = true; } var restoreEntryRegex = new Regex( @"(entry\s*=\s*this\._data\.state\.(?:setAnimation|addAnimation)\([^;]+\);)\s*(entry\.alpha\s*=)", RegexOptions.Singleline); string afterRestoreEntry = restoreEntryRegex.Replace(updated, "$1\n if (!entry) break;\n $2"); if (!string.Equals(afterRestoreEntry, updated, StringComparison.Ordinal)) { updated = afterRestoreEntry; modified = true; } if (!modified) { return false; } var bakPath = path + BackupSuffix; if (!File.Exists(bakPath)) { File.Copy(path, bakPath, true); } File.WriteAllText(path, updated, new UTF8Encoding(true)); return true; } #endregion } }