using System; using System.Collections.Generic; using System.IO; using System.Text; using GameLauncher.Engines.Unity; using GameLauncher.Models; namespace GameLauncher.Services.Codecs { /// /// YU-RIS エンジン形式のセーブデータ(YSSD シグネチャ)用コーデック /// public class YurisSaveCodec : ISaveDataCodec { /// /// コーデックの表示名を取得します。 /// public string CodecName => "YU-RIS セーブデータ (YSSD) コーデック"; /// /// パスワード入力が必要かどうかを取得します。 /// public bool RequiresPassword => false; /// /// パスワード入力ダイアログに表示する案内メッセージを取得します。 /// public string? PasswordPromptMessage => null; /// /// 指定されたセーブファイルが本コーデックで処理可能かを判定します。 /// /// 対象のUnityゲーム情報 /// 対象のセーブファイル情報 /// 処理可能な場合は true、それ以外は false public bool CanHandle(UnityGameItem game, SaveFileEntry file) { if (!File.Exists(file.FilePath)) return false; try { using var fs = File.OpenRead(file.FilePath); if (fs.Length < 16) return false; var magic = new byte[4]; fs.ReadExactly(magic); // "YSSD" (0x59 0x53 0x53 0x44) return magic[0] == 0x59 && magic[1] == 0x53 && magic[2] == 0x53 && magic[3] == 0x44; } catch { return false; } } /// /// セーブファイルからプロパティ一覧を読み込みます。 /// /// セーブファイルの絶対パス /// 任意の暗号化パスワード /// 展開されたセーブデータプロパティのリスト public List LoadProperties(string filePath, string? optionalPassword = null) { var list = new List(); if (!File.Exists(filePath)) return list; byte[] bytes = File.ReadAllBytes(filePath); if (bytes.Length < 16) return list; int headerSize = BitConverter.ToInt32(bytes, 4); int version = BitConverter.ToInt32(bytes, 8); list.Add(new SaveDataPropertyEntry { Key = "Signature", Category = "ヘッダー情報", Type = "String", EditValue = "YSSD (YU-RIS Save Data)", Description = "セーブ識別子" }); list.Add(new SaveDataPropertyEntry { Key = "HeaderSize", Category = "ヘッダー情報", Type = "Int32", EditValue = headerSize.ToString(), Description = "ヘッダーサイズ" }); list.Add(new SaveDataPropertyEntry { Key = "SaveVersion", Category = "ヘッダー情報", Type = "Int32", EditValue = version.ToString(), Description = "セーブバージョン番号" }); // 16バイト以降の変数領域から、4バイト整数および文字列をスキャン int offset = 16; int varIndex = 0; while (offset + 4 <= bytes.Length && varIndex < 100) { int val = BitConverter.ToInt32(bytes, offset); list.Add(new SaveDataPropertyEntry { Key = $"Var_{varIndex:D3} (0x{offset:X4})", Category = "ゲーム変数 / フラグ", Type = "Int32", EditValue = val.ToString(), Description = $"オフセット 0x{offset:X4} の4バイト値", RawData = offset }); offset += 4; varIndex++; } return list; } /// /// 編集後のプロパティ一覧をセーブファイルへ書き戻して保存します。 /// /// 保存先セーブファイルの絶対パス /// 編集されたプロパティ一覧 /// 任意の暗号化パスワード /// 保存に成功した場合は true、それ以外は false public bool SaveProperties(string filePath, List properties, string? optionalPassword = null) { try { byte[] bytes = File.ReadAllBytes(filePath); foreach (var prop in properties) { if (prop.RawData is int offset && offset + 4 <= bytes.Length) { if (int.TryParse(prop.EditValue, out int val)) { var b = BitConverter.GetBytes(val); Array.Copy(b, 0, bytes, offset, 4); } } } if (File.Exists(filePath)) { File.Copy(filePath, filePath + ".bak", overwrite: true); } File.WriteAllBytes(filePath, bytes); return true; } catch { return false; } } } }