Files
GameLauncher/Services/WolfArchiveService.cs
T

218 lines
8.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using GameLauncher.Helpers;
using GameLauncher.Models;
namespace GameLauncher.Services
{
/// <summary>
/// ウディタ(WOLF RPGエディター)の Data.wolf アーカイブの解凍・抽出・状態管理サービス
/// </summary>
public class WolfArchiveService
{
/// <summary>
/// 指定されたゲームフォルダ内に Data.wolf または .wolf アーカイブが存在するか判定します。
/// </summary>
public bool HasWolfArchive(string gameDirectory)
{
if (string.IsNullOrEmpty(gameDirectory) || !Directory.Exists(gameDirectory))
return false;
var dataWolf = Path.Combine(gameDirectory, "Data.wolf");
if (File.Exists(dataWolf)) return true;
try
{
return Directory.EnumerateFiles(gameDirectory, "*.wolf", SearchOption.TopDirectoryOnly).Any();
}
catch
{
return false;
}
}
/// <summary>
/// 指定されたゲームフォルダ内に Data.wolf の解凍先フォルダ(Data フォルダ)が存在するか判定します。
/// </summary>
public bool IsExtracted(string gameDirectory)
{
if (string.IsNullOrEmpty(gameDirectory) || !Directory.Exists(gameDirectory))
return false;
var dataDir = Path.Combine(gameDirectory, "Data");
return Directory.Exists(dataDir);
}
/// <summary>
/// 有効な Wolf 解凍ツールの実行ファイルパスを取得します。
/// </summary>
public string? ResolveExtractorPath(string? customPath)
{
// 1. ユーザー指定パス
if (!string.IsNullOrWhiteSpace(customPath) && File.Exists(customPath))
{
return customPath;
}
// 2. ランチャー直下の Tools\UberWolfCli.exe
var exeDir = AppPathHelper.GetExeDirectory();
var candidates = new[]
{
Path.Combine(exeDir, "Tools", "UberWolfCli.exe"),
Path.Combine(exeDir, "Tools", "UberWolf.exe"),
Path.Combine(exeDir, "Tools", "WolfDec.exe"),
@"C:\VS2026\GameLauncher\Tools\UberWolfCli.exe",
@"Z:\自作ソフト\GameLauncher\Tools\UberWolfCli.exe"
};
foreach (var candidate in candidates)
{
if (File.Exists(candidate))
{
return candidate;
}
}
return null;
}
/// <summary>
/// Data.wolf を解凍・抽出します。
/// </summary>
public async Task<(bool success, string message)> ExtractArchiveAsync(
TkoolGameItem game,
string? customExtractorPath,
IProgress<string>? progress = null)
{
var gameDir = game.RootDirectory;
if (!Directory.Exists(gameDir))
{
return (false, "ゲームのルートディレクトリが見つかりません。");
}
var extractorPath = ResolveExtractorPath(customExtractorPath);
if (string.IsNullOrEmpty(extractorPath) || !File.Exists(extractorPath))
{
return (false, "Wolf RPG アーカイブ解凍ツール(UberWolfCli.exe / WolfDec.exe)が見つかりません。設定ダイアログからツールのパスを指定してください。");
}
// 解凍対象の探索 (Data.wolf または Game.exe)
var targetFile = Path.Combine(gameDir, "Data.wolf");
if (!File.Exists(targetFile))
{
var wolfFiles = Directory.GetFiles(gameDir, "*.wolf");
if (wolfFiles.Length > 0)
{
targetFile = wolfFiles[0];
}
else
{
targetFile = Path.Combine(gameDir, "Game.exe");
}
}
if (!File.Exists(targetFile))
{
return (false, "解凍対象の Data.wolf または Game.exe が見つかりません。");
}
progress?.Report($"[解凍開始] ツール: {Path.GetFileName(extractorPath)} 対象: {Path.GetFileName(targetFile)}");
try
{
// UberWolfCli.exe の場合は -o (上書き) -u (プロテクト解除) を渡す
var isUberWolf = Path.GetFileName(extractorPath).Contains("UberWolf", StringComparison.OrdinalIgnoreCase);
var arguments = isUberWolf
? $"-o -u \"{targetFile}\""
: $"\"{targetFile}\"";
var psi = new ProcessStartInfo
{
FileName = extractorPath,
Arguments = arguments,
WorkingDirectory = gameDir,
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
};
using var process = new Process { StartInfo = psi };
var outputLog = new System.Text.StringBuilder();
process.OutputDataReceived += (_, e) =>
{
if (!string.IsNullOrEmpty(e.Data))
{
outputLog.AppendLine(e.Data);
progress?.Report(e.Data);
}
};
process.ErrorDataReceived += (_, e) =>
{
if (!string.IsNullOrEmpty(e.Data))
{
outputLog.AppendLine(e.Data);
progress?.Report($"[情報] {e.Data}");
}
};
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
await process.WaitForExitAsync();
var dataDir = Path.Combine(gameDir, "Data");
if (Directory.Exists(dataDir) && Directory.EnumerateFileSystemEntries(dataDir).Any())
{
progress?.Report($"[解凍完了] Data フォルダへの抽出に成功しました: {dataDir}");
return (true, "Data.wolf の解凍・抽出が完了しました。Data フォルダのファイルを直接編集・改造できます。");
}
else
{
return (false, $"解凍処理は終了しましたが、Data フォルダが確認できませんでした。\nログ: {outputLog}");
}
}
catch (Exception ex)
{
return (false, $"解凍処理中に例外が発生しました: {ex.Message}");
}
}
/// <summary>
/// 解凍された Data フォルダを Backups フォルダに退避し、元の Data.wolf 優先読み込み状態に復元します。
/// </summary>
public (bool success, string message) RestoreArchiveState(TkoolGameItem game)
{
var gameDir = game.RootDirectory;
var dataDir = Path.Combine(gameDir, "Data");
if (!Directory.Exists(dataDir))
{
return (true, "Data フォルダは存在しないため、既に元のアーカイブ読み込み状態です。");
}
try
{
// バックアップ保存先(Backups フォルダ)
var backupDir = AppPathHelper.GetGameBackupDirectory(game.FolderName, game.RootDirectory);
var timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss");
var backupDataDir = Path.Combine(backupDir, $"Data_Extracted_{timestamp}");
// Data フォルダを Backups へ移動(元に戻す)
Directory.Move(dataDir, backupDataDir);
return (true, $"解凍されていた Data フォルダを Backups フォルダへ退避しました。\n(退避先: {backupDataDir})\n元の Data.wolf アーカイブ優先読み込みに復元されました。");
}
catch (Exception ex)
{
return (false, $"復元処理中にエラーが発生しました: {ex.Message}");
}
}
}
}