using System; using System.Collections.Generic; using System.IO; using System.Text; using GameLauncher.Models; using GameLauncher.Engines.Unity; 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; 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; } 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; } } } }