using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Windows; using System.Windows.Controls; using Microsoft.Win32; using GameLauncher.Helpers; using GameLauncher.Models; using GameLauncher.Services; using GameLauncher.Engines.Wolf; using GameLauncher.Engines.Tkool; namespace GameLauncher { /// /// ウディタ用セーブデータエディタの画面ロジック /// public partial class WolfSaveEditorDialog : Window { private readonly TkoolGameItem _game; private readonly WolfSaveService _saveService; private WolfSaveData? _currentSaveData; private List _allVariables = []; private List _allStrings = []; public WolfSaveEditorDialog(TkoolGameItem game, WolfSaveService saveService) { this.InitializeComponent(); this._game = game; this._saveService = saveService; this.lblGameTitle.Text = this._game.Title; this.lblGamePath.Text = this._game.RootDirectory; this.txtInfoBackupDir.Text = AppPathHelper.GetGameBackupDirectory(this._game.FolderName, this._game.RootDirectory); this.LoadSaveFileList(); } /// /// セーブファイル一覧の読み込みとコンボボックス設定 /// private void LoadSaveFileList() { try { var files = this._saveService.GetSaveFiles(this._game.RootDirectory); this.cboSaveFiles.Items.Clear(); foreach (var f in files) { this.cboSaveFiles.Items.Add(Path.GetFileName(f)); } if (this.cboSaveFiles.Items.Count > 0) { this.cboSaveFiles.SelectedIndex = 0; } else { this.lblStatus.Text = "セーブデータ(*.sav)が見つかりませんでした。"; } } catch (Exception ex) { this.lblStatus.Text = $"セーブファイル列挙エラー: {ex.Message}"; } } private void cboSaveFiles_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (this.cboSaveFiles.SelectedItem == null) return; var fileName = this.cboSaveFiles.SelectedItem.ToString(); if (string.IsNullOrEmpty(fileName)) return; this.LoadSaveFile(fileName); } /// /// 指定されたセーブファイルの読み込み・解析 /// private void LoadSaveFile(string fileName) { try { var files = this._saveService.GetSaveFiles(this._game.RootDirectory); var fullPath = files.FirstOrDefault(f => string.Equals(Path.GetFileName(f), fileName, StringComparison.OrdinalIgnoreCase)); if (string.IsNullOrEmpty(fullPath) || !File.Exists(fullPath)) { this.lblStatus.Text = $"ファイルが存在しません: {fileName}"; return; } this._currentSaveData = this._saveService.LoadSave(fullPath); this._allVariables = [.. this._currentSaveData.Variables]; this._allStrings = [.. this._currentSaveData.Strings]; // メタ情報表示 this.txtInfoTitle.Text = string.IsNullOrEmpty(this._currentSaveData.GameTitle) ? "(不明)" : this._currentSaveData.GameTitle; this.txtInfoVersion.Text = $"0x{this._currentSaveData.FileVersion:X4}"; this.txtInfoEncoding.Text = this._currentSaveData.IsUtf8 ? "UTF-8" : "Shift-JIS (CP932)"; this.txtInfoSize.Text = $"{this._currentSaveData.RawDecryptedBytes.Length:N0} バイト"; this.ApplyFilter(); this.lblStatus.Text = $"'{fileName}' を正常に読み込みました。"; this.lblSaveCount.Text = $"変数項目: {this._allVariables.Count}件, 文字列項目: {this._allStrings.Count}件"; } catch (Exception ex) { MessageBox.Show($"セーブデータの読み込みに失敗しました:\n{ex.Message}", "読み込みエラー", MessageBoxButton.OK, MessageBoxImage.Error); this.lblStatus.Text = $"読み込みエラー: {ex.Message}"; } } private void btnReload_Click(object sender, RoutedEventArgs e) { if (this.cboSaveFiles.SelectedItem != null) { this.LoadSaveFile(this.cboSaveFiles.SelectedItem.ToString()!); } else { this.LoadSaveFileList(); } } /// /// フィルターの適用 /// private void ApplyFilter() { var filter = this.txtFilter.Text?.Trim() ?? string.Empty; if (string.IsNullOrEmpty(filter)) { this.dgVariables.ItemsSource = this._allVariables; this.dgStrings.ItemsSource = this._allStrings; } else { this.dgVariables.ItemsSource = this._allVariables .Where(v => v.DisplayName.Contains(filter, StringComparison.OrdinalIgnoreCase) || v.Category.Contains(filter, StringComparison.OrdinalIgnoreCase) || v.Value.ToString().Contains(filter, StringComparison.OrdinalIgnoreCase) || v.Offset.ToString("X").Contains(filter, StringComparison.OrdinalIgnoreCase)) .ToList(); this.dgStrings.ItemsSource = this._allStrings .Where(s => s.Value.Contains(filter, StringComparison.OrdinalIgnoreCase) || s.Offset.ToString("X").Contains(filter, StringComparison.OrdinalIgnoreCase)) .ToList(); } } private void txtFilter_TextChanged(object sender, TextChangedEventArgs e) { this.ApplyFilter(); } /// /// 数値ピンポイント検索(所持金・ステータス等) /// private void btnSearchValue_Click(object sender, RoutedEventArgs e) { if (!int.TryParse(this.txtTargetValue.Text.Trim(), out int target)) { MessageBox.Show("検索する数値を半角数字で入力してください。(例: 現在の所持金やステータス)", "入力エラー", MessageBoxButton.OK, MessageBoxImage.Information); return; } var matches = this._allVariables.Where(v => v.Value == target).ToList(); if (matches.Count == 0) { MessageBox.Show($"値が {target:N0} と一致する変数が見つかりませんでした。", "検索結果", MessageBoxButton.OK, MessageBoxImage.Information); return; } this.dgVariables.ItemsSource = matches; this.lblStatus.Text = $"値 {target:N0} に一致する候補が {matches.Count} 件見つかりました。"; } /// /// 検索された数値を一括で 9,999,999 に設定 /// private void btnSetMaxTarget_Click(object sender, RoutedEventArgs e) { if (this.dgVariables.ItemsSource is not IEnumerable currentList) return; var list = currentList.ToList(); if (list.Count == 0) return; var result = MessageBox.Show( $"現在一覧に表示されている {list.Count} 件の数値をすべて 9,999,999 に変更しますか?\n(所持金やステータスの一括MAX化に便利です)", "一括変更の確認", MessageBoxButton.YesNo, MessageBoxImage.Question); if (result == MessageBoxResult.Yes) { foreach (var item in list) { item.Value = 9999999; } this.dgVariables.Items.Refresh(); this.lblStatus.Text = $"{list.Count} 件の値を 9,999,999 に変更しました。保存ボタンを押すと反映されます。"; } } /// /// 単一行のクイックMAX化 /// private void btnQuickMax_Click(object sender, RoutedEventArgs e) { if (sender is Button btn && btn.Tag is WolfVariableItem item) { item.Value = 9999999; this.dgVariables.Items.Refresh(); this.lblStatus.Text = $"{item.DisplayName} の値を 9,999,999 に設定しました。"; } } private void dgVariables_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) { this.lblStatus.Text = "値を編集しました。「上書き保存」を押すとバックアップを作成して保存されます。"; } /// /// セーブデータの上書き保存(Backupsへの完全自動退避付き) /// private void btnSave_Click(object sender, RoutedEventArgs e) { if (this._currentSaveData == null) { MessageBox.Show("保存対象のセーブデータがありません。", "エラー", MessageBoxButton.OK, MessageBoxImage.Warning); return; } try { // 変数リストの最新値をモデルへ反映 this._currentSaveData.Variables = this._allVariables; // 保存実行 (自動バックアップ) string backupPath = this._saveService.SaveToFile(this._currentSaveData, this._game.FolderName, this._game.RootDirectory); var msg = $"セーブデータ '{this._currentSaveData.FileName}' を安全に上書き保存しました!\n\n【バックアップ保存先】\n{backupPath}"; MessageBox.Show(msg, "保存完了", MessageBoxButton.OK, MessageBoxImage.Information); this.lblStatus.Text = $"保存完了 (バックアップ: {Path.GetFileName(backupPath)})"; } catch (Exception ex) { MessageBox.Show($"保存中にエラーが発生しました:\n{ex.Message}", "保存エラー", MessageBoxButton.OK, MessageBoxImage.Error); this.lblStatus.Text = $"保存エラー: {ex.Message}"; } } /// /// JSON エクスポート /// private void btnExportJson_Click(object sender, RoutedEventArgs e) { if (this._currentSaveData == null) return; var sfd = new SaveFileDialog { Title = "セーブデータを JSON としてエクスポート", Filter = "JSON ファイル (*.json)|*.json", FileName = $"{Path.GetFileNameWithoutExtension(this._currentSaveData.FileName)}_export.json" }; if (sfd.ShowDialog() == true) { try { this._saveService.ExportToJson(this._currentSaveData, sfd.FileName); MessageBox.Show($"JSON ファイルをエクスポートしました:\n{sfd.FileName}", "エクスポート完了", MessageBoxButton.OK, MessageBoxImage.Information); } catch (Exception ex) { MessageBox.Show($"エクスポートに失敗しました:\n{ex.Message}", "エラー", MessageBoxButton.OK, MessageBoxImage.Error); } } } /// /// JSON インポート /// private void btnImportJson_Click(object sender, RoutedEventArgs e) { if (this._currentSaveData == null) return; var ofd = new OpenFileDialog { Title = "編集済み JSON ファイルを選択してください", Filter = "JSON ファイル (*.json)|*.json" }; if (ofd.ShowDialog() == true) { try { this._saveService.ImportFromJson(this._currentSaveData, ofd.FileName); this._allVariables = [.. this._currentSaveData.Variables]; this.ApplyFilter(); MessageBox.Show("JSON データを正常に取り込みました。「上書き保存」を押すとファイルへ反映されます。", "インポート完了", MessageBoxButton.OK, MessageBoxImage.Information); } catch (Exception ex) { MessageBox.Show($"インポートに失敗しました:\n{ex.Message}", "エラー", MessageBoxButton.OK, MessageBoxImage.Error); } } } /// /// バックアップフォルダを開く /// private void btnOpenBackups_Click(object sender, RoutedEventArgs e) { try { var dir = AppPathHelper.GetGameBackupDirectory(this._game.FolderName, this._game.RootDirectory); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } Process.Start(new ProcessStartInfo { FileName = dir, UseShellExecute = true }); } catch (Exception ex) { MessageBox.Show($"バックアップフォルダを開けませんでした:\n{ex.Message}", "エラー", MessageBoxButton.OK, MessageBoxImage.Warning); } } } }