// Engines/Nwjs/NwjsModService.cs — NW.js ゲーム向けスクリプト改造注入・復元サービス
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using GameLauncher.Engines.Tkool;
using GameLauncher.Helpers;
using GameLauncher.Models;
namespace GameLauncher.Engines.Nwjs
{
///
/// NW.js エンジン製ゲーム(GameCreator 製・カスタム HTML5/JS 製・その他 NW.js 全般)に対して、
/// エントリ HTML への改造スクリプト(チートメニュー・倍速・全解放・F12開発者ツール)の注入および
/// バックアップからの完全復元を提供するサービスクラス。
///
public static class NwjsModService
{
#region □定数(Private)
///
/// 注入する改造 JavaScript ファイル名。
///
private const string ModScriptFileName = "nwjs_launcher_mod.js";
///
/// エントリ HTML に挿入する script タグ。
///
private const string ModScriptTag = "";
///
/// バックアップファイルの拡張子サフィックス。
///
private const string BackupFileSuffix = ".original_launcher_backup";
#endregion
#region □メソッド(Public)
///
/// 指定されたエントリ HTML ファイルに改造スクリプトタグが適用済みかどうかを判定します。
///
/// 判定対象の index.html 等のフルパス
/// 適用済みの場合は true、それ以外は false
public static bool CheckIsModded(string indexHtmlPath)
{
if (string.IsNullOrEmpty(indexHtmlPath) || !File.Exists(indexHtmlPath))
{
return false;
}
try
{
string content = File.ReadAllText(indexHtmlPath, Encoding.UTF8);
return content.Contains(NwjsModService.ModScriptFileName, StringComparison.OrdinalIgnoreCase);
}
catch
{
return false;
}
}
///
/// 選択されたプラグイン設定に基づいて NW.js ゲームへ改造スクリプトを生成・注入します。
///
/// 対象のゲームアイテム
/// エントリ HTML のパス
/// プラグイン設定一覧
public static void ApplyMod(TkoolGameItem game, string indexHtmlPath, IEnumerable plugins)
{
if (string.IsNullOrEmpty(indexHtmlPath) || !File.Exists(indexHtmlPath))
{
throw new FileNotFoundException("NW.js のエントリ HTML ファイルが見つかりません。", indexHtmlPath);
}
bool enableCheatMenu = false;
bool enableSpeedHack = false;
bool enableUnlockGallery = false;
bool enableDevTools = false;
if (plugins != null)
{
foreach (ModPluginOptionItem opt in plugins)
{
if (!opt.IsEnabled)
{
continue;
}
switch (opt.Id)
{
case "NwjsCheatMenu":
enableCheatMenu = true;
break;
case "NwjsSpeedHack":
enableSpeedHack = true;
break;
case "NwjsUnlockGallery":
enableUnlockGallery = true;
break;
case "NwjsDevTools":
enableDevTools = true;
break;
}
}
}
if (!enableCheatMenu && !enableSpeedHack && !enableUnlockGallery && !enableDevTools)
{
NwjsModService.RestoreOriginal(game, indexHtmlPath);
return;
}
string backupPath = NwjsModService.GetBackupFilePath(game, indexHtmlPath);
string legacyBackupPath = indexHtmlPath + NwjsModService.BackupFileSuffix;
if (!File.Exists(backupPath))
{
string? backupDir = Path.GetDirectoryName(backupPath);
if (!string.IsNullOrEmpty(backupDir) && !Directory.Exists(backupDir))
{
Directory.CreateDirectory(backupDir);
}
if (File.Exists(legacyBackupPath))
{
File.Copy(legacyBackupPath, backupPath, overwrite: true);
}
else
{
string currentHtml = File.ReadAllText(indexHtmlPath, Encoding.UTF8);
if (!currentHtml.Contains(NwjsModService.ModScriptFileName, StringComparison.OrdinalIgnoreCase))
{
File.Copy(indexHtmlPath, backupPath, overwrite: true);
}
}
}
if (File.Exists(legacyBackupPath))
{
try
{
File.Delete(legacyBackupPath);
}
catch
{
}
}
string baseHtml;
if (File.Exists(backupPath))
{
baseHtml = File.ReadAllText(backupPath, Encoding.UTF8);
}
else
{
baseHtml = File.ReadAllText(indexHtmlPath, Encoding.UTF8);
}
string htmlDir = Path.GetDirectoryName(indexHtmlPath) ?? game.RootDirectory;
string modJsPath = Path.Combine(htmlDir, NwjsModService.ModScriptFileName);
string jsContent = NwjsModService.GenerateModScriptContent(
enableCheatMenu,
enableSpeedHack,
enableUnlockGallery,
enableDevTools);
File.WriteAllText(modJsPath, jsContent, new UTF8Encoding(false));
if (!baseHtml.Contains(NwjsModService.ModScriptFileName, StringComparison.OrdinalIgnoreCase))
{
int headCloseIdx = baseHtml.IndexOf("", StringComparison.OrdinalIgnoreCase);
int bodyCloseIdx = baseHtml.IndexOf("