using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using GameLauncher.Helpers;
using GameLauncher.Models;
namespace GameLauncher.Services
{
///
/// ティラノスクリプトゲームの個別改造設定オプション
///
public class TyranoModOptions
{
public bool UnlockAllCg { get; set; } = true;
public bool FastMessage { get; set; } = true;
public bool EnableDevTools { get; set; } = true;
public bool ShowCheatMenu { get; set; } = true;
}
///
/// ティラノスクリプト(TyranoScript)ゲームのMOD適用・解除・管理を行うサービス
///
public class TyranoModService
{
public const string ModScriptFileName = "tyrano_launcher_mod.js";
public const string IndexBackupSuffix = ".original_launcher_backup";
///
/// index.html に改造スクリプトが適用されているかどうか判定します。
///
public static bool CheckIsModded(string indexPath)
{
if (string.IsNullOrEmpty(indexPath))
return false;
string extPath = AsarArchiveService.ToExtendedPath(indexPath);
if (!File.Exists(extPath))
return false;
try
{
string content = File.ReadAllText(extPath);
return content.Contains(ModScriptFileName);
}
catch
{
return false;
}
}
///
/// ティラノスクリプトゲームに改造スクリプトを適用します。
///
public (bool Success, string Message) ApplyMod(TkoolGameItem game, Dictionary? pluginSettings)
{
var rawIndexPath = game.PluginsJsPath; // ティラノの場合は index.html パス
if (string.IsNullOrEmpty(rawIndexPath))
{
return (false, "index.html が見つかりません。ASARを解凍してください。");
}
var indexPath = AsarArchiveService.ToExtendedPath(rawIndexPath);
if (!File.Exists(indexPath))
{
return (false, "index.html が見つかりません。ASARを解凍してください。");
}
try
{
var dir = Path.GetDirectoryName(indexPath)!;
var gameBackupDir = AppPathHelper.GetGameBackupDirectory(game.FolderName, game.RootDirectory);
var backupPath = Path.Combine(gameBackupDir, "index.html" + IndexBackupSuffix);
var legacyBackupPath = indexPath + IndexBackupSuffix;
// 1. バックアップの作成(存在しない場合、Backups フォルダに保存)
if (!File.Exists(backupPath))
{
if (File.Exists(legacyBackupPath))
{
File.Copy(legacyBackupPath, backupPath, true);
}
else
{
string originalContent = File.ReadAllText(indexPath, Encoding.UTF8);
// 既にタグが含まれていた場合は除去したものをバックアップとする
string cleanContent = RemoveScriptTag(originalContent);
File.WriteAllText(backupPath, cleanContent, new UTF8Encoding(false));
}
}
// 2. 有効化する機能の判定
bool unlockCg = game.IsPluginEnabled("TyranoUnlockAllCg", pluginSettings, fallbackVal: true);
bool fastMsg = game.IsPluginEnabled("TyranoFastMessage", pluginSettings, fallbackVal: true);
bool devTools = game.IsPluginEnabled("TyranoDevTools", pluginSettings, fallbackVal: true);
bool cheatMenu = game.IsPluginEnabled("TyranoCheatMenu", pluginSettings, fallbackVal: true);
// 3. tyrano_launcher_mod.js を生成して配置
string modJsContent = BuildModScript(unlockCg, fastMsg, devTools, cheatMenu);
string modJsPath = Path.Combine(dir, ModScriptFileName);
File.WriteAllText(modJsPath, modJsContent, new UTF8Encoding(false));
// 4. index.html にスクリプトタグを注入
string baseContent = File.ReadAllText(backupPath, Encoding.UTF8);
string injectedContent = InjectScriptTag(baseContent);
File.WriteAllText(indexPath, injectedContent, new UTF8Encoding(false));
game.IsModded = true;
return (true, "ティラノスクリプト用改造を正常に適用しました。(全CG解放/高速化/DevTools/チートメニュー)");
}
catch (Exception ex)
{
return (false, $"ティラノ改造適用中にエラーが発生しました: {ex.Message}");
}
}
///
/// ティラノスクリプトゲームの改造を解除し、元のオリジナル状態に復元します。
///
public (bool Success, string Message) RestoreOriginal(TkoolGameItem game)
{
var rawIndexPath = game.PluginsJsPath;
if (string.IsNullOrEmpty(rawIndexPath))
{
return (false, "index.html のパスが設定されていません。");
}
var indexPath = AsarArchiveService.ToExtendedPath(rawIndexPath);
try
{
var dir = Path.GetDirectoryName(indexPath)!;
var gameBackupDir = AppPathHelper.GetGameBackupDirectory(game.FolderName, game.RootDirectory);
var backupPath = Path.Combine(gameBackupDir, "index.html" + IndexBackupSuffix);
var legacyBackupPath = indexPath + IndexBackupSuffix;
var modJsPath = Path.Combine(dir, ModScriptFileName);
// 1. index.html の復元 (Backups フォルダを優先)
if (File.Exists(backupPath))
{
File.Copy(backupPath, indexPath, true);
}
else if (File.Exists(legacyBackupPath))
{
File.Copy(legacyBackupPath, indexPath, true);
try { File.Copy(legacyBackupPath, backupPath, true); } catch { }
}
else if (File.Exists(indexPath))
{
string content = File.ReadAllText(indexPath, Encoding.UTF8);
string clean = RemoveScriptTag(content);
File.WriteAllText(indexPath, clean, new UTF8Encoding(false));
}
// ゲームフォルダ内の旧バックアップがあれば削除
if (File.Exists(legacyBackupPath))
{
try { File.Delete(legacyBackupPath); } catch { }
}
// 2. tyrano_launcher_mod.js の削除
if (File.Exists(modJsPath))
{
try { File.Delete(modJsPath); } catch { }
}
game.IsModded = false;
return (true, "ティラノスクリプトの改造を解除し、オリジナル状態に復元しました。");
}
catch (Exception ex)
{
return (false, $"復元中にエラーが発生しました: {ex.Message}");
}
}
private static string InjectScriptTag(string html)
{
string tag = $"";
if (html.Contains(ModScriptFileName))
{
return html;
}
if (html.Contains("