// Engines/Bakin/BakinSaveEditorDialog.xaml.cs — RPG Developer Bakin セーブデータ改造エディタ ウィンドウ using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.IO; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using GameLauncher.Engines.Tkool; namespace GameLauncher.Engines.Bakin { /// /// RPG Developer Bakin (YUKRDATA .sgs) セーブデータの閲覧・編集・クイックチート・バックアップ復元を行うダイアログウィンドウ。 /// public partial class BakinSaveEditorDialog : Window { #region □定数(Private) private const string AllCategoriesLabel = "すべて表示"; #endregion #region □変数(Private) private readonly TkoolGameItem _game; private List _saveFiles = new(); private BakinSaveDocument? _currentDoc; private ICollectionView? _entriesView; private bool _suppressFilterEvents; #endregion #region □コンストラクタ /// /// の新しいインスタンスを初期化します。 /// /// 対象の Bakin ゲームアイテム public BakinSaveEditorDialog(TkoolGameItem game) { this.InitializeComponent(); this._game = game ?? throw new ArgumentNullException(nameof(game)); this.GameTitleText.Text = $"— {this._game.Title}"; this.LoadSaveFileList(); } #endregion #region □イベント private void SaveFileComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (this.SaveFileComboBox.SelectedItem is BakinSaveFileInfo selected) { this.LoadSelectedSaveFile(selected.FilePath); } } private void ReloadButton_Click(object sender, RoutedEventArgs e) { string? currentPath = (this.SaveFileComboBox.SelectedItem as BakinSaveFileInfo)?.FilePath; this.LoadSaveFileList(currentPath); } private void OpenFolderButton_Click(object sender, RoutedEventArgs e) { try { var saveDir = BakinDetector.ResolveSaveDirectory(this._game.RootDirectory); if (!string.IsNullOrEmpty(saveDir) && Directory.Exists(saveDir)) { Process.Start(new ProcessStartInfo { FileName = saveDir, UseShellExecute = true }); } else if (Directory.Exists(this._game.RootDirectory)) { Process.Start(new ProcessStartInfo { FileName = this._game.RootDirectory, UseShellExecute = true }); } } catch (Exception ex) { MessageBox.Show(this, $"フォルダを開けませんでした:\n{ex.Message}", "エラー", MessageBoxButton.OK, MessageBoxImage.Warning); } } private void QuickMaxMoney_Click(object sender, RoutedEventArgs e) { if (this._currentDoc == null) { return; } int updated = 0; foreach (var entry in this._currentDoc.Entries) { if (entry.InternalKey == "Party.Money") { entry.ValueText = "9999999"; updated++; } else if (entry.InternalKey == "Party.InventoryMax") { if (int.TryParse(entry.ValueText, out int curMax) && curMax < 999) { entry.ValueText = "999"; } } } this.EntriesDataGrid.Items.Refresh(); this.StatusTextBlock.Text = updated > 0 ? "💰 所持金を 9,999,999 に設定しました。「💾 変更をセーブデータに保存」を押すと確定します。" : "⚠️ このセーブファイルには所持金データが含まれていません(Save_00.sgs 等の通常セーブを選択してください)。"; } private void QuickMaxActionPoints_Click(object sender, RoutedEventArgs e) { if (this._currentDoc == null) { return; } int updated = 0; foreach (var entry in this._currentDoc.Entries) { if (entry.Category == BakinSaveService.CategoryVariables && (entry.DisplayKey.Contains("行動力", StringComparison.OrdinalIgnoreCase) || entry.DisplayKey.Contains("AP", StringComparison.OrdinalIgnoreCase) || entry.DisplayKey.Contains("スタミナ", StringComparison.OrdinalIgnoreCase))) { entry.ValueText = "999"; updated++; } } this.EntriesDataGrid.Items.Refresh(); this.StatusTextBlock.Text = updated > 0 ? $"⚡ 行動力・最大行動力関連の変数 ({updated} 件) を 999 に設定しました。「💾 変更をセーブデータに保存」を押すと確定します。" : "⚠️ 行動力関連の変数が見つかりませんでした。"; } private void QuickMaxPartyStats_Click(object sender, RoutedEventArgs e) { if (this._currentDoc == null) { return; } int updated = 0; foreach (var entry in this._currentDoc.Entries) { if (entry.Category != BakinSaveService.CategoryParty) { continue; } if (entry.InternalKey.EndsWith(".Level", StringComparison.Ordinal)) { entry.ValueText = "99"; updated++; } else if (entry.InternalKey.EndsWith(".Exp", StringComparison.Ordinal)) { entry.ValueText = "9999999"; updated++; } else if (entry.InternalKey.EndsWith(".HP", StringComparison.Ordinal) || entry.InternalKey.EndsWith(".MaxHP", StringComparison.Ordinal) || entry.InternalKey.EndsWith(".MP", StringComparison.Ordinal) || entry.InternalKey.EndsWith(".MaxMP", StringComparison.Ordinal)) { entry.ValueText = "9999"; updated++; } else if (entry.InternalKey.EndsWith(".Power", StringComparison.Ordinal) || entry.InternalKey.EndsWith(".Vitality", StringComparison.Ordinal) || entry.InternalKey.EndsWith(".Magic", StringComparison.Ordinal) || entry.InternalKey.EndsWith(".Speed", StringComparison.Ordinal)) { entry.ValueText = "999"; updated++; } } this.EntriesDataGrid.Items.Refresh(); this.StatusTextBlock.Text = updated > 0 ? $"💪 パーティ・控えメンバー全員の Lv99 / HP・MP 9999 / 各能力 999 ({updated} 項目) を設定しました。" : "⚠️ パーティメンバーのステータス項目が見つかりませんでした。"; } private void QuickMaxItems_Click(object sender, RoutedEventArgs e) { if (this._currentDoc == null) { return; } int itemUpdated = 0; int flagUpdated = 0; foreach (var entry in this._currentDoc.Entries) { if (entry.Category == BakinSaveService.CategoryItems) { entry.ValueText = "99"; itemUpdated++; } else if (entry.Category == BakinSaveService.CategoryVariables) { if (entry.InternalKey.Contains("(dbGetAllItems)", StringComparison.OrdinalIgnoreCase) || entry.InternalKey.Contains("(dbLearnAll)", StringComparison.OrdinalIgnoreCase)) { entry.ValueText = "1"; flagUpdated++; } else if (entry.InternalKey.Contains("(dbGetAllItemsAmount)", StringComparison.OrdinalIgnoreCase)) { entry.ValueText = "99"; flagUpdated++; } } } this.EntriesDataGrid.Items.Refresh(); this.StatusTextBlock.Text = $"🎒 所持アイテム ({itemUpdated} 枠) を ×99 に設定し、全アイテム/全レシピ習得変数 ({flagUpdated} 件) を有効化しました。"; } private void QuickUnlockAllGallery_Click(object sender, RoutedEventArgs e) { if (this._currentDoc == null) { return; } int updated = 0; foreach (var entry in this._currentDoc.Entries) { if (entry.ValueType != "Double" && entry.ValueType != "Int32") { continue; } if (entry.Category == BakinSaveService.CategoryCrossSave || entry.DisplayKey.Contains("動画", StringComparison.OrdinalIgnoreCase) || entry.DisplayKey.Contains("解放", StringComparison.OrdinalIgnoreCase) || entry.DisplayKey.Contains("回想", StringComparison.OrdinalIgnoreCase) || entry.DisplayKey.Contains("ギャラリー", StringComparison.OrdinalIgnoreCase) || entry.DisplayKey.Contains("CG", StringComparison.OrdinalIgnoreCase)) { entry.ValueText = "1"; updated++; } } this.EntriesDataGrid.Items.Refresh(); this.StatusTextBlock.Text = updated > 0 ? $"🔓 動画・回想・クロスセーブ解放フラグ ({updated} 件) をすべて ON(1) に設定しました(Save_xx.sgs にも同時反映されます)。" : "⚠️ 解放フラグ系の変数が見つかりませんでした。"; } private void FilterChanged(object sender, EventArgs e) { if (this._suppressFilterEvents || this._entriesView == null) { return; } this._entriesView.Refresh(); this.UpdateFilteredCountStatus(); } private void ApplyBulkValue_Click(object sender, RoutedEventArgs e) { if (this._entriesView == null) { return; } string bulkVal = this.BulkValueTextBox.Text.Trim(); if (string.IsNullOrEmpty(bulkVal)) { return; } int count = 0; foreach (var obj in this._entriesView) { if (obj is BakinSaveEntry entry) { entry.ValueText = bulkVal; count++; } } this.EntriesDataGrid.Items.Refresh(); this.StatusTextBlock.Text = $"✏️ 現在表示中の {count} 件の項目に「{bulkVal}」を一括設定しました。"; } private void SaveButton_Click(object sender, RoutedEventArgs e) { if (this._currentDoc == null) { return; } try { this.EntriesDataGrid.CommitEdit(DataGridEditingUnit.Cell, true); this.EntriesDataGrid.CommitEdit(DataGridEditingUnit.Row, true); string backupPath = BakinSaveService.SaveSaveData( this._currentDoc, this._game.FolderName, this._game.RootDirectory); this.EntriesDataGrid.Items.Refresh(); string fileName = Path.GetFileName(this._currentDoc.FilePath); this.StatusTextBlock.Text = $"✅ 「{fileName}」を保存しました!(バックアップ作成済み: {Path.GetFileName(backupPath)})"; MessageBox.Show( this, $"セーブデータ「{fileName}」への保存が完了しました!\n" + (string.IsNullOrEmpty(this._currentDoc.CompanionCrossSavePath) ? string.Empty : "(※クロスセーブ Save_xx.sgs の変更も同時に保存されました)\n") + $"\n自動バックアップ保存先:\n{backupPath}", "セーブデータ保存完了", MessageBoxButton.OK, MessageBoxImage.Information); } catch (Exception ex) { MessageBox.Show( this, $"セーブデータの保存中にエラーが発生しました:\n{ex.Message}", "保存エラー", MessageBoxButton.OK, MessageBoxImage.Error); } } private void RestoreBackupButton_Click(object sender, RoutedEventArgs e) { if (this._currentDoc == null) { return; } string fileName = Path.GetFileName(this._currentDoc.FilePath); var ask = MessageBox.Show( this, $"「{fileName}」を最新のバックアップから復元しますか?\n(現在の未保存の編集内容は破棄されます)", "バックアップ復元の確認", MessageBoxButton.YesNo, MessageBoxImage.Question); if (ask != MessageBoxResult.Yes) { return; } var (success, message) = BakinSaveService.RestoreLatestBackup( this._currentDoc.FilePath, this._game.FolderName, this._game.RootDirectory); if (success) { this.LoadSelectedSaveFile(this._currentDoc.FilePath); this.StatusTextBlock.Text = $"⏪ {message.Replace("\n", " ")}"; MessageBox.Show(this, message, "復元完了", MessageBoxButton.OK, MessageBoxImage.Information); } else { MessageBox.Show(this, message, "復元案内", MessageBoxButton.OK, MessageBoxImage.Warning); } } private void CloseButton_Click(object sender, RoutedEventArgs e) { this.Close(); } #endregion #region □メソッド(Private) private void LoadSaveFileList(string? preferFilePath = null) { this._saveFiles = BakinSaveService.FindSaveFiles(this._game.RootDirectory); this.SaveFileComboBox.ItemsSource = this._saveFiles; if (this._saveFiles.Count == 0) { this._currentDoc = null; this.EntriesDataGrid.ItemsSource = null; this.SaveButton.IsEnabled = false; this.RestoreBackupButton.IsEnabled = false; this.StatusTextBlock.Text = "⚠️ セーブデータ (data/savedata/Save_*.sgs) が見つかりません。一度ゲーム内でセーブを作成してください。"; return; } this.SaveButton.IsEnabled = true; this.RestoreBackupButton.IsEnabled = true; BakinSaveFileInfo? target = null; if (!string.IsNullOrEmpty(preferFilePath)) { target = this._saveFiles.FirstOrDefault(f => string.Equals(f.FilePath, preferFilePath, StringComparison.OrdinalIgnoreCase)); } // デフォルトでは Save_00.sgs 等の通常セーブを優先選択 target ??= this._saveFiles.FirstOrDefault(f => !f.IsCrossSave) ?? this._saveFiles[0]; this.SaveFileComboBox.SelectedItem = target; } private void LoadSelectedSaveFile(string filePath) { try { this._currentDoc = BakinSaveService.LoadSaveData(filePath); this._suppressFilterEvents = true; var categories = new List { BakinSaveEditorDialog.AllCategoriesLabel }; categories.AddRange(this._currentDoc.Entries.Select(e => e.Category).Distinct()); string? prevCat = this.CategoryFilterComboBox.SelectedItem as string; this.CategoryFilterComboBox.ItemsSource = categories; this.CategoryFilterComboBox.SelectedItem = (prevCat != null && categories.Contains(prevCat)) ? prevCat : BakinSaveEditorDialog.AllCategoriesLabel; this._suppressFilterEvents = false; this._entriesView = CollectionViewSource.GetDefaultView(this._currentDoc.Entries); this._entriesView.Filter = this.FilterEntry; this.EntriesDataGrid.ItemsSource = this._entriesView; this.UpdateFilteredCountStatus(); } catch (Exception ex) { this.StatusTextBlock.Text = $"❌ 読み込みエラー: {ex.Message}"; MessageBox.Show( this, $"セーブデータの読み込み中にエラーが発生しました:\n{ex.Message}", "読み込みエラー", MessageBoxButton.OK, MessageBoxImage.Error); } } private bool FilterEntry(object obj) { if (obj is not BakinSaveEntry entry) { return false; } // 1. カテゴリフィルタ if (this.CategoryFilterComboBox.SelectedItem is string cat && !string.Equals(cat, BakinSaveEditorDialog.AllCategoriesLabel, StringComparison.Ordinal)) { if (!string.Equals(entry.Category, cat, StringComparison.Ordinal)) { return false; } } // 2. キーワード検索フィルタ string kw = this.KeywordFilterTextBox.Text.Trim(); if (!string.IsNullOrEmpty(kw)) { bool matchDisplay = entry.DisplayKey.Contains(kw, StringComparison.OrdinalIgnoreCase); bool matchInternal = entry.InternalKey.Contains(kw, StringComparison.OrdinalIgnoreCase); bool matchCategory = entry.Category.Contains(kw, StringComparison.OrdinalIgnoreCase); bool matchValue = entry.ValueText.Contains(kw, StringComparison.OrdinalIgnoreCase); if (!matchDisplay && !matchInternal && !matchCategory && !matchValue) { return false; } } // 3. 現在値完全一致フィルタ string exact = this.ExactValueFilterTextBox.Text.Trim(); if (!string.IsNullOrEmpty(exact)) { if (!string.Equals(entry.ValueText.Trim(), exact, StringComparison.OrdinalIgnoreCase)) { return false; } } return true; } private void UpdateFilteredCountStatus() { if (this._currentDoc == null || this._entriesView == null) { return; } int total = this._currentDoc.Entries.Count; int visible = this._entriesView.Cast().Count(); string fileName = Path.GetFileName(this._currentDoc.FilePath); this.StatusTextBlock.Text = $"📂 読込完了: {fileName} — 全 {total} 項目中 {visible} 項目を表示中(ダブルクリックまたはクイック改造ボタンで編集できます)"; } #endregion } }