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; 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) { InitializeComponent(); _game = game; _saveService = saveService; lblGameTitle.Text = _game.Title; lblGamePath.Text = _game.RootDirectory; txtInfoBackupDir.Text = AppPathHelper.GetGameBackupDirectory(_game.FolderName, _game.RootDirectory); LoadSaveFileList(); } /// /// セーブファイル一覧の読み込みとコンボボックス設定 /// private void LoadSaveFileList() { try { var files = _saveService.GetSaveFiles(_game.RootDirectory); cboSaveFiles.Items.Clear(); foreach (var f in files) { cboSaveFiles.Items.Add(Path.GetFileName(f)); } if (cboSaveFiles.Items.Count > 0) { cboSaveFiles.SelectedIndex = 0; } else { lblStatus.Text = "セーブデータ(*.sav)が見つかりませんでした。"; } } catch (Exception ex) { lblStatus.Text = $"セーブファイル列挙エラー: {ex.Message}"; } } private void cboSaveFiles_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (cboSaveFiles.SelectedItem == null) return; var fileName = cboSaveFiles.SelectedItem.ToString(); if (string.IsNullOrEmpty(fileName)) return; LoadSaveFile(fileName); } /// /// 指定されたセーブファイルの読み込み・解析 /// private void LoadSaveFile(string fileName) { try { var files = _saveService.GetSaveFiles(_game.RootDirectory); var fullPath = files.FirstOrDefault(f => string.Equals(Path.GetFileName(f), fileName, StringComparison.OrdinalIgnoreCase)); if (string.IsNullOrEmpty(fullPath) || !File.Exists(fullPath)) { lblStatus.Text = $"ファイルが存在しません: {fileName}"; return; } _currentSaveData = _saveService.LoadSave(fullPath); _allVariables = [.. _currentSaveData.Variables]; _allStrings = [.. _currentSaveData.Strings]; // メタ情報表示 txtInfoTitle.Text = string.IsNullOrEmpty(_currentSaveData.GameTitle) ? "(不明)" : _currentSaveData.GameTitle; txtInfoVersion.Text = $"0x{_currentSaveData.FileVersion:X4}"; txtInfoEncoding.Text = _currentSaveData.IsUtf8 ? "UTF-8" : "Shift-JIS (CP932)"; txtInfoSize.Text = $"{_currentSaveData.RawDecryptedBytes.Length:N0} バイト"; ApplyFilter(); lblStatus.Text = $"'{fileName}' を正常に読み込みました。"; lblSaveCount.Text = $"変数項目: {_allVariables.Count}件, 文字列項目: {_allStrings.Count}件"; } catch (Exception ex) { MessageBox.Show($"セーブデータの読み込みに失敗しました:\n{ex.Message}", "読み込みエラー", MessageBoxButton.OK, MessageBoxImage.Error); lblStatus.Text = $"読み込みエラー: {ex.Message}"; } } private void btnReload_Click(object sender, RoutedEventArgs e) { if (cboSaveFiles.SelectedItem != null) { LoadSaveFile(cboSaveFiles.SelectedItem.ToString()!); } else { LoadSaveFileList(); } } /// /// フィルターの適用 /// private void ApplyFilter() { var filter = txtFilter.Text?.Trim() ?? string.Empty; if (string.IsNullOrEmpty(filter)) { dgVariables.ItemsSource = _allVariables; dgStrings.ItemsSource = _allStrings; } else { dgVariables.ItemsSource = _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(); dgStrings.ItemsSource = _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) { ApplyFilter(); } /// /// 数値ピンポイント検索(所持金・ステータス等) /// private void btnSearchValue_Click(object sender, RoutedEventArgs e) { if (!int.TryParse(txtTargetValue.Text.Trim(), out int target)) { MessageBox.Show("検索する数値を半角数字で入力してください。(例: 現在の所持金やステータス)", "入力エラー", MessageBoxButton.OK, MessageBoxImage.Information); return; } var matches = _allVariables.Where(v => v.Value == target).ToList(); if (matches.Count == 0) { MessageBox.Show($"値が {target:N0} と一致する変数が見つかりませんでした。", "検索結果", MessageBoxButton.OK, MessageBoxImage.Information); return; } dgVariables.ItemsSource = matches; lblStatus.Text = $"値 {target:N0} に一致する候補が {matches.Count} 件見つかりました。"; } /// /// 検索された数値を一括で 9,999,999 に設定 /// private void btnSetMaxTarget_Click(object sender, RoutedEventArgs e) { if (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; } dgVariables.Items.Refresh(); 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; dgVariables.Items.Refresh(); lblStatus.Text = $"{item.DisplayName} の値を 9,999,999 に設定しました。"; } } private void dgVariables_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) { lblStatus.Text = "値を編集しました。「上書き保存」を押すとバックアップを作成して保存されます。"; } /// /// セーブデータの上書き保存(Backupsへの完全自動退避付き) /// private void btnSave_Click(object sender, RoutedEventArgs e) { if (_currentSaveData == null) { MessageBox.Show("保存対象のセーブデータがありません。", "エラー", MessageBoxButton.OK, MessageBoxImage.Warning); return; } try { // 変数リストの最新値をモデルへ反映 _currentSaveData.Variables = _allVariables; // 保存実行 (自動バックアップ) string backupPath = _saveService.SaveToFile(_currentSaveData, _game.FolderName, _game.RootDirectory); var msg = $"セーブデータ '{_currentSaveData.FileName}' を安全に上書き保存しました!\n\n【バックアップ保存先】\n{backupPath}"; MessageBox.Show(msg, "保存完了", MessageBoxButton.OK, MessageBoxImage.Information); lblStatus.Text = $"保存完了 (バックアップ: {Path.GetFileName(backupPath)})"; } catch (Exception ex) { MessageBox.Show($"保存中にエラーが発生しました:\n{ex.Message}", "保存エラー", MessageBoxButton.OK, MessageBoxImage.Error); lblStatus.Text = $"保存エラー: {ex.Message}"; } } /// /// JSON エクスポート /// private void btnExportJson_Click(object sender, RoutedEventArgs e) { if (_currentSaveData == null) return; var sfd = new SaveFileDialog { Title = "セーブデータを JSON としてエクスポート", Filter = "JSON ファイル (*.json)|*.json", FileName = $"{Path.GetFileNameWithoutExtension(_currentSaveData.FileName)}_export.json" }; if (sfd.ShowDialog() == true) { try { _saveService.ExportToJson(_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 (_currentSaveData == null) return; var ofd = new OpenFileDialog { Title = "編集済み JSON ファイルを選択してください", Filter = "JSON ファイル (*.json)|*.json" }; if (ofd.ShowDialog() == true) { try { _saveService.ImportFromJson(_currentSaveData, ofd.FileName); _allVariables = [.. _currentSaveData.Variables]; 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(_game.FolderName, _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); } } } }