216 lines
8.8 KiB
C#
216 lines
8.8 KiB
C#
using System;
|
||
using System.Diagnostics;
|
||
using System.IO;
|
||
using System.Text;
|
||
using System.Text.Json;
|
||
using GameLauncher.Helpers;
|
||
using GameLauncher.Models;
|
||
|
||
namespace GameLauncher.Services
|
||
{
|
||
/// <summary>
|
||
/// ウディタ(WOLF RPGエディター)向けのプロキシDLL(メモリフック/スピードハック)
|
||
/// およびデバッグモード(テストプレイモード)管理サービス
|
||
/// </summary>
|
||
public class WolfModService
|
||
{
|
||
public const string ProxyDllName = "version.dll";
|
||
public const string ConfigFileName = "speedhack_config.json";
|
||
public const string GameExeBackupSuffix = ".original_launcher_backup";
|
||
|
||
/// <summary>
|
||
/// プロキシDLL(version.dll)がゲームフォルダ内に配置されているか判定します。
|
||
/// </summary>
|
||
public bool IsProxyDllInstalled(TkoolGameItem game)
|
||
{
|
||
if (string.IsNullOrEmpty(game.RootDirectory) || !Directory.Exists(game.RootDirectory))
|
||
return false;
|
||
|
||
var dllPath = Path.Combine(game.RootDirectory, ProxyDllName);
|
||
return File.Exists(dllPath);
|
||
}
|
||
|
||
/// <summary>
|
||
/// プロキシDLL(メモリフック/スピードハック)をゲームフォルダに適用します。
|
||
/// </summary>
|
||
/// <param name="game">対象のウディタゲーム</param>
|
||
/// <param name="baseSpeed">基本スピード倍率(1.0〜10.0)</param>
|
||
/// <param name="toggleSpeed">F6キー押下時のトグルスピード倍率(3.0など)</param>
|
||
/// <param name="holdSpeed">Ctrlキー押下時の早送り倍率(5.0など)</param>
|
||
public (bool success, string message) ApplyProxyDll(
|
||
TkoolGameItem game,
|
||
double baseSpeed = 1.0,
|
||
double toggleSpeed = 3.0,
|
||
double holdSpeed = 5.0)
|
||
{
|
||
var gameDir = game.RootDirectory;
|
||
if (!Directory.Exists(gameDir))
|
||
return (false, "ゲームディレクトリが見つかりません。");
|
||
|
||
try
|
||
{
|
||
// 1. プロキシDLL本体の探索 (Tools\speedhack-i686\version.dll)
|
||
var sourceDllPath = ResolveProxyDllPath();
|
||
if (string.IsNullOrEmpty(sourceDllPath) || !File.Exists(sourceDllPath))
|
||
{
|
||
return (false, "プロキシDLL(Tools\\speedhack-i686\\version.dll)が見つかりません。");
|
||
}
|
||
|
||
var targetDllPath = Path.Combine(gameDir, ProxyDllName);
|
||
var targetConfigPath = Path.Combine(gameDir, ConfigFileName);
|
||
|
||
// 2. 元々同名DLLが存在していた場合は Backups フォルダに退避
|
||
var backupDir = AppPathHelper.GetGameBackupDirectory(game.FolderName, game.RootDirectory);
|
||
var backupDllPath = Path.Combine(backupDir, ProxyDllName + GameExeBackupSuffix);
|
||
|
||
if (File.Exists(targetDllPath) && !File.Exists(backupDllPath))
|
||
{
|
||
File.Copy(targetDllPath, backupDllPath, overwrite: true);
|
||
}
|
||
|
||
// 3. プロキシDLLをコピー配置
|
||
File.Copy(sourceDllPath, targetDllPath, overwrite: true);
|
||
|
||
// 4. speedhack_config.json の生成・書き込み
|
||
var configContent = GenerateSpeedhackConfig(baseSpeed, toggleSpeed, holdSpeed);
|
||
File.WriteAllText(targetConfigPath, configContent, new UTF8Encoding(false));
|
||
|
||
game.WolfProxyDll = true;
|
||
game.IsModded = true;
|
||
return (true, $"プロキシDLL(version.dll)を配置しました。\n・[F6] キー: {toggleSpeed:0.#}倍速 トグル\n・[Ctrl] キー: {holdSpeed:0.#}倍速 早送り\n・通常速度: {baseSpeed:0.#}倍");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return (false, $"プロキシDLLの適用に失敗しました: {ex.Message}");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// プロキシDLLをゲームフォルダから削除し、初期状態に完全復元します。
|
||
/// </summary>
|
||
public (bool success, string message) RemoveProxyDll(TkoolGameItem game)
|
||
{
|
||
var gameDir = game.RootDirectory;
|
||
if (!Directory.Exists(gameDir))
|
||
return (false, "ゲームディレクトリが見つかりません。");
|
||
|
||
try
|
||
{
|
||
var targetDllPath = Path.Combine(gameDir, ProxyDllName);
|
||
var targetConfigPath = Path.Combine(gameDir, ConfigFileName);
|
||
|
||
var backupDir = AppPathHelper.GetGameBackupDirectory(game.FolderName, game.RootDirectory);
|
||
var backupDllPath = Path.Combine(backupDir, ProxyDllName + GameExeBackupSuffix);
|
||
|
||
// 1. プロキシDLLの削除またはオリジナル復元
|
||
if (File.Exists(backupDllPath))
|
||
{
|
||
File.Copy(backupDllPath, targetDllPath, overwrite: true);
|
||
try { File.Delete(backupDllPath); } catch { }
|
||
}
|
||
else if (File.Exists(targetDllPath))
|
||
{
|
||
File.Delete(targetDllPath);
|
||
}
|
||
|
||
// 2. 設定ファイルの削除
|
||
if (File.Exists(targetConfigPath))
|
||
{
|
||
try { File.Delete(targetConfigPath); } catch { }
|
||
}
|
||
|
||
game.WolfProxyDll = false;
|
||
game.IsModded = false;
|
||
return (true, "プロキシDLL(version.dll)を解除し、ゲームを元の状態に復元しました。");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return (false, $"プロキシDLLの解除に失敗しました: {ex.Message}");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 全ての改造(プロキシDLL、パッチ等)を解除し、ゲームを完全に元に戻します。
|
||
/// </summary>
|
||
public (bool success, string message) RestoreAllMods(TkoolGameItem game)
|
||
{
|
||
var sb = new StringBuilder();
|
||
|
||
// 1. プロキシDLLの解除
|
||
var (dllSuccess, dllMsg) = RemoveProxyDll(game);
|
||
sb.AppendLine(dllMsg);
|
||
|
||
// 2. Game.exe バックアップからの復元(存在する場合)
|
||
var backupDir = AppPathHelper.GetGameBackupDirectory(game.FolderName, game.RootDirectory);
|
||
var backupExePath = Path.Combine(backupDir, "Game.exe" + GameExeBackupSuffix);
|
||
var gameExePath = Path.Combine(game.RootDirectory, "Game.exe");
|
||
|
||
if (File.Exists(backupExePath))
|
||
{
|
||
try
|
||
{
|
||
File.Copy(backupExePath, gameExePath, overwrite: true);
|
||
sb.AppendLine("・Game.exe をオリジナルバックアップから復元しました。");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
sb.AppendLine($"・Game.exe の復元中にエラーが発生しました: {ex.Message}");
|
||
}
|
||
}
|
||
|
||
return (true, sb.ToString().Trim());
|
||
}
|
||
|
||
/// <summary>
|
||
/// speedhack-rs の version.dll の配置パスを探索・解決します。
|
||
/// </summary>
|
||
private string? ResolveProxyDllPath()
|
||
{
|
||
var exeDir = AppPathHelper.GetExeDirectory();
|
||
var candidates = new[]
|
||
{
|
||
Path.Combine(exeDir, "Tools", "speedhack-i686", ProxyDllName),
|
||
Path.Combine(exeDir, "Tools", ProxyDllName),
|
||
@"C:\VS2026\GameLauncher\Tools\speedhack-i686\version.dll",
|
||
@"Z:\自作ソフト\GameLauncher\Tools\speedhack-i686\version.dll"
|
||
};
|
||
|
||
foreach (var path in candidates)
|
||
{
|
||
if (File.Exists(path)) return path;
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// speedhack_config.json の JSON 文字列を生成します。
|
||
/// </summary>
|
||
private string GenerateSpeedhackConfig(double baseSpeed, double toggleSpeed, double holdSpeed)
|
||
{
|
||
var obj = new
|
||
{
|
||
console = false,
|
||
base_speed = baseSpeed,
|
||
speed_states = new object[]
|
||
{
|
||
new
|
||
{
|
||
keys = new[] { "F6" },
|
||
speed = toggleSpeed,
|
||
is_toggle = true
|
||
},
|
||
new
|
||
{
|
||
keys = new[] { "Control" },
|
||
speed = holdSpeed,
|
||
is_toggle = false
|
||
}
|
||
}
|
||
};
|
||
|
||
return JsonSerializer.Serialize(obj, new JsonSerializerOptions { WriteIndented = true });
|
||
}
|
||
}
|
||
}
|