Files
GameLauncher/Services/Codecs/YurisSaveCodec.cs
T

165 lines
5.9 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using GameLauncher.Engines.Unity;
using GameLauncher.Models;
namespace GameLauncher.Services.Codecs
{
/// <summary>
/// YU-RIS エンジン形式のセーブデータ(YSSD シグネチャ)用コーデック
/// </summary>
public class YurisSaveCodec : ISaveDataCodec
{
/// <summary>
/// コーデックの表示名を取得します。
/// </summary>
public string CodecName => "YU-RIS セーブデータ (YSSD) コーデック";
/// <summary>
/// パスワード入力が必要かどうかを取得します。
/// </summary>
public bool RequiresPassword => false;
/// <summary>
/// パスワード入力ダイアログに表示する案内メッセージを取得します。
/// </summary>
public string? PasswordPromptMessage => null;
/// <summary>
/// 指定されたセーブファイルが本コーデックで処理可能かを判定します。
/// </summary>
/// <param name="game">対象のUnityゲーム情報</param>
/// <param name="file">対象のセーブファイル情報</param>
/// <returns>処理可能な場合は true、それ以外は false</returns>
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;
}
}
/// <summary>
/// セーブファイルからプロパティ一覧を読み込みます。
/// </summary>
/// <param name="filePath">セーブファイルの絶対パス</param>
/// <param name="optionalPassword">任意の暗号化パスワード</param>
/// <returns>展開されたセーブデータプロパティのリスト</returns>
public List<SaveDataPropertyEntry> LoadProperties(string filePath, string? optionalPassword = null)
{
var list = new List<SaveDataPropertyEntry>();
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;
}
/// <summary>
/// 編集後のプロパティ一覧をセーブファイルへ書き戻して保存します。
/// </summary>
/// <param name="filePath">保存先セーブファイルの絶対パス</param>
/// <param name="properties">編集されたプロパティ一覧</param>
/// <param name="optionalPassword">任意の暗号化パスワード</param>
/// <returns>保存に成功した場合は true、それ以外は false</returns>
public bool SaveProperties(string filePath, List<SaveDataPropertyEntry> 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;
}
}
}
}