docs(codecs): セーブデータコーデック基盤へのXMLドキュメントコメント付与および実装スタイルの整理
This commit is contained in:
1 parent
922f669191
commit
ee7b37323c
4 files changed
+175
-42
No files matched your search
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
@@ -7,8 +7,8 @@ using System.Text;
|
||||
using System.Text.Encodings.Web;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using GameLauncher.Models;
|
||||
using GameLauncher.Engines.Unity;
|
||||
using GameLauncher.Models;
|
||||
|
||||
namespace GameLauncher.Services.Codecs
|
||||
{
|
||||
@@ -17,10 +17,19 @@ namespace GameLauncher.Services.Codecs
|
||||
/// </summary>
|
||||
public class EasySave3Codec : ISaveDataCodec
|
||||
{
|
||||
/// <summary>
|
||||
/// コーデックの表示名を取得します。
|
||||
/// </summary>
|
||||
public string CodecName => "Easy Save 3 (ES3) コーデック";
|
||||
|
||||
/// <summary>
|
||||
/// パスワード入力が必要かどうかを取得します。
|
||||
/// </summary>
|
||||
public bool RequiresPassword => false;
|
||||
|
||||
/// <summary>
|
||||
/// パスワード入力ダイアログに表示する案内メッセージを取得します。
|
||||
/// </summary>
|
||||
public string? PasswordPromptMessage => "暗号化パスワードが設定されている場合は入力してください(未設定・既定の場合は空欄):";
|
||||
|
||||
// キャッシュ用の最新平文JSONノード
|
||||
@@ -28,6 +37,12 @@ namespace GameLauncher.Services.Codecs
|
||||
private bool _wasEncrypted;
|
||||
private string? _usedPassword;
|
||||
|
||||
/// <summary>
|
||||
/// 指定されたセーブファイルが本コーデックで処理可能かを判定します。
|
||||
/// </summary>
|
||||
/// <param name="game">対象のUnityゲーム情報</param>
|
||||
/// <param name="file">対象のセーブファイル情報</param>
|
||||
/// <returns>処理可能な場合は true、それ以外は false</returns>
|
||||
public bool CanHandle(UnityGameItem game, SaveFileEntry file)
|
||||
{
|
||||
var ext = Path.GetExtension(file.FilePath).ToLowerInvariant();
|
||||
@@ -43,6 +58,12 @@ namespace GameLauncher.Services.Codecs
|
||||
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>();
|
||||
@@ -52,8 +73,8 @@ namespace GameLauncher.Services.Codecs
|
||||
if (fileBytes.Length == 0) return list;
|
||||
|
||||
string? jsonText = null;
|
||||
_wasEncrypted = false;
|
||||
_usedPassword = null;
|
||||
this._wasEncrypted = false;
|
||||
this._usedPassword = null;
|
||||
|
||||
// 1. まず平文 JSON または Gzip 解凍を試みる
|
||||
try
|
||||
@@ -91,12 +112,12 @@ namespace GameLauncher.Services.Codecs
|
||||
|
||||
foreach (var pwd in candidatePasswords)
|
||||
{
|
||||
var decrypted = TryDecryptAes(fileBytes, pwd);
|
||||
var decrypted = this.TryDecryptAes(fileBytes, pwd);
|
||||
if (!string.IsNullOrEmpty(decrypted))
|
||||
{
|
||||
jsonText = decrypted;
|
||||
_wasEncrypted = true;
|
||||
_usedPassword = pwd;
|
||||
this._wasEncrypted = true;
|
||||
this._usedPassword = pwd;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -105,30 +126,36 @@ namespace GameLauncher.Services.Codecs
|
||||
if (string.IsNullOrEmpty(jsonText))
|
||||
{
|
||||
// JSON展開ができなかった場合、バイナリキー・バリュー形式として先頭・重要オフセットを抽出
|
||||
return ExtractBinaryPropertiesFallback(fileBytes);
|
||||
return this.ExtractBinaryPropertiesFallback(fileBytes);
|
||||
}
|
||||
|
||||
// JSON からプロパティを再帰的に展開
|
||||
try
|
||||
{
|
||||
_cachedRootNode = JsonNode.Parse(jsonText);
|
||||
if (_cachedRootNode is JsonObject rootObj)
|
||||
this._cachedRootNode = JsonNode.Parse(jsonText);
|
||||
if (this._cachedRootNode is JsonObject rootObj)
|
||||
{
|
||||
FlattenJsonObject(rootObj, "", list);
|
||||
this.FlattenJsonObject(rootObj, "", list);
|
||||
}
|
||||
else if (_cachedRootNode is JsonArray rootArr)
|
||||
else if (this._cachedRootNode is JsonArray rootArr)
|
||||
{
|
||||
FlattenJsonArray(rootArr, "Array", list);
|
||||
this.FlattenJsonArray(rootArr, "Array", list);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
return ExtractBinaryPropertiesFallback(fileBytes);
|
||||
return this.ExtractBinaryPropertiesFallback(fileBytes);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// JSONオブジェクトを再帰的に走査し、プロパティ一覧に追加します。
|
||||
/// </summary>
|
||||
/// <param name="obj">対象のJSONオブジェクト</param>
|
||||
/// <param name="prefix">現在のキー階層プレフィックス</param>
|
||||
/// <param name="list">追加先のプロパティリスト</param>
|
||||
private void FlattenJsonObject(JsonObject obj, string prefix, List<SaveDataPropertyEntry> list)
|
||||
{
|
||||
foreach (var kvp in obj)
|
||||
@@ -181,14 +208,14 @@ namespace GameLauncher.Services.Codecs
|
||||
}
|
||||
else
|
||||
{
|
||||
FlattenJsonObject(childObj, key, list);
|
||||
this.FlattenJsonObject(childObj, key, list);
|
||||
}
|
||||
}
|
||||
else if (node is JsonArray childArr)
|
||||
{
|
||||
if (childArr.Count <= 20)
|
||||
{
|
||||
FlattenJsonArray(childArr, key, list);
|
||||
this.FlattenJsonArray(childArr, key, list);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -206,6 +233,12 @@ namespace GameLauncher.Services.Codecs
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// JSON配列を再帰的に走査し、プロパティ一覧に追加します。
|
||||
/// </summary>
|
||||
/// <param name="arr">対象のJSON配列</param>
|
||||
/// <param name="prefix">現在のキー階層プレフィックス</param>
|
||||
/// <param name="list">追加先のプロパティリスト</param>
|
||||
private void FlattenJsonArray(JsonArray arr, string prefix, List<SaveDataPropertyEntry> list)
|
||||
{
|
||||
for (int i = 0; i < arr.Count; i++)
|
||||
@@ -225,11 +258,16 @@ namespace GameLauncher.Services.Codecs
|
||||
}
|
||||
else if (node is JsonObject childObj)
|
||||
{
|
||||
FlattenJsonObject(childObj, key, list);
|
||||
this.FlattenJsonObject(childObj, key, list);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// JSON展開に失敗した場合のフォールバックとしてバイナリ値を抽出します。
|
||||
/// </summary>
|
||||
/// <param name="bytes">ファイル全体のバイナリデータ</param>
|
||||
/// <returns>バイナリ情報プロパティのリスト</returns>
|
||||
private List<SaveDataPropertyEntry> ExtractBinaryPropertiesFallback(byte[] bytes)
|
||||
{
|
||||
var list = new List<SaveDataPropertyEntry>();
|
||||
@@ -261,18 +299,25 @@ namespace GameLauncher.Services.Codecs
|
||||
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
|
||||
{
|
||||
if (_cachedRootNode != null)
|
||||
if (this._cachedRootNode != null)
|
||||
{
|
||||
// JSON の値を更新
|
||||
foreach (var prop in properties)
|
||||
{
|
||||
if (prop.RawData is string keyPath)
|
||||
{
|
||||
UpdateJsonNodeByPath(_cachedRootNode, keyPath, prop.EditValue, prop.Type);
|
||||
this.UpdateJsonNodeByPath(this._cachedRootNode, keyPath, prop.EditValue, prop.Type);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -281,15 +326,15 @@ namespace GameLauncher.Services.Codecs
|
||||
WriteIndented = true,
|
||||
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
|
||||
};
|
||||
string updatedJson = _cachedRootNode.ToJsonString(options);
|
||||
string updatedJson = this._cachedRootNode.ToJsonString(options);
|
||||
|
||||
byte[] outputBytes = Encoding.UTF8.GetBytes(updatedJson);
|
||||
|
||||
// もともと暗号化されていた場合は再暗号化
|
||||
if (_wasEncrypted)
|
||||
if (this._wasEncrypted)
|
||||
{
|
||||
string pwd = !string.IsNullOrEmpty(optionalPassword) ? optionalPassword : (_usedPassword ?? "Moodkie");
|
||||
outputBytes = EncryptAes(outputBytes, pwd);
|
||||
string pwd = !string.IsNullOrEmpty(optionalPassword) ? optionalPassword : (this._usedPassword ?? "Moodkie");
|
||||
outputBytes = this.EncryptAes(outputBytes, pwd);
|
||||
}
|
||||
|
||||
// バックアップ作成
|
||||
@@ -331,6 +376,13 @@ namespace GameLauncher.Services.Codecs
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// キーパスに基づいてJSONノードの値を更新します。
|
||||
/// </summary>
|
||||
/// <param name="root">ルートのJSONノード</param>
|
||||
/// <param name="path">ドット区切りのキーパス</param>
|
||||
/// <param name="newValue">更新後の値文字列</param>
|
||||
/// <param name="type">値の型名</param>
|
||||
private void UpdateJsonNodeByPath(JsonNode root, string path, string newValue, string type)
|
||||
{
|
||||
try
|
||||
@@ -358,16 +410,22 @@ namespace GameLauncher.Services.Codecs
|
||||
string lastKey = parts[^1];
|
||||
if (current is JsonObject finalObj)
|
||||
{
|
||||
finalObj[lastKey] = ParseJsonValue(newValue, type);
|
||||
finalObj[lastKey] = this.ParseJsonValue(newValue, type);
|
||||
}
|
||||
else if (current is JsonArray finalArr && int.TryParse(lastKey, out int finalIdx) && finalIdx < finalArr.Count)
|
||||
{
|
||||
finalArr[finalIdx] = ParseJsonValue(newValue, type);
|
||||
finalArr[finalIdx] = this.ParseJsonValue(newValue, type);
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 文字列値と型名から対応するJSONノードを生成します。
|
||||
/// </summary>
|
||||
/// <param name="value">値文字列</param>
|
||||
/// <param name="type">型名</param>
|
||||
/// <returns>生成されたJSONノード</returns>
|
||||
private JsonNode? ParseJsonValue(string value, string type)
|
||||
{
|
||||
if (type == "Int" && int.TryParse(value, out int i)) return JsonValue.Create(i);
|
||||
@@ -389,6 +447,12 @@ namespace GameLauncher.Services.Codecs
|
||||
return JsonValue.Create(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// AESアルゴリズムによるセーブデータの復号を試みます。
|
||||
/// </summary>
|
||||
/// <param name="data">暗号化バイナリデータ</param>
|
||||
/// <param name="password">復号パスワード</param>
|
||||
/// <returns>復号された平文文字列。失敗時は null</returns>
|
||||
private string? TryDecryptAes(byte[] data, string password)
|
||||
{
|
||||
try
|
||||
@@ -424,6 +488,12 @@ namespace GameLauncher.Services.Codecs
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 平文バイト配列をAESアルゴリズムで暗号化します。
|
||||
/// </summary>
|
||||
/// <param name="plainBytes">平文バイナリデータ</param>
|
||||
/// <param name="password">暗号化パスワード</param>
|
||||
/// <returns>暗号化されたバイナリデータ</returns>
|
||||
private byte[] EncryptAes(byte[] plainBytes, string password)
|
||||
{
|
||||
byte[] rawUtf8 = Encoding.UTF8.GetBytes(password);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
using GameLauncher.Models;
|
||||
using System.Collections.Generic;
|
||||
using GameLauncher.Engines.Unity;
|
||||
using GameLauncher.Models;
|
||||
|
||||
namespace GameLauncher.Services.Codecs
|
||||
{
|
||||
@@ -9,22 +9,44 @@ namespace GameLauncher.Services.Codecs
|
||||
/// </summary>
|
||||
public interface ISaveDataCodec
|
||||
{
|
||||
/// <summary>コーデックの識別名称(例: "Easy Save 3 (ES3)", "YU-RIS セーブデータ")</summary>
|
||||
/// <summary>
|
||||
/// コーデックの識別名称(例: "Easy Save 3 (ES3)", "YU-RIS セーブデータ")を取得します。
|
||||
/// </summary>
|
||||
string CodecName { get; }
|
||||
|
||||
/// <summary>このコーデックで指定ファイルを処理可能か判定</summary>
|
||||
/// <summary>
|
||||
/// このコーデックで指定ファイルを処理可能か判定します。
|
||||
/// </summary>
|
||||
/// <param name="game">対象のUnityゲーム情報</param>
|
||||
/// <param name="file">対象のセーブファイル情報</param>
|
||||
/// <returns>処理可能な場合は true、それ以外は false</returns>
|
||||
bool CanHandle(UnityGameItem game, SaveFileEntry file);
|
||||
|
||||
/// <summary>セーブファイルからGUI編集用プロパティ一覧をロード・解析</summary>
|
||||
/// <summary>
|
||||
/// セーブファイルからGUI編集用プロパティ一覧をロード・解析します。
|
||||
/// </summary>
|
||||
/// <param name="filePath">セーブファイルの絶対パス</param>
|
||||
/// <param name="optionalPassword">任意の暗号化パスワード</param>
|
||||
/// <returns>展開されたセーブデータプロパティのリスト</returns>
|
||||
List<SaveDataPropertyEntry> LoadProperties(string filePath, string? optionalPassword = null);
|
||||
|
||||
/// <summary>GUI編集されたプロパティ一覧をファイルへ再エンコード・保存</summary>
|
||||
/// <summary>
|
||||
/// GUI編集されたプロパティ一覧をファイルへ再エンコード・保存します。
|
||||
/// </summary>
|
||||
/// <param name="filePath">保存先セーブファイルの絶対パス</param>
|
||||
/// <param name="properties">編集されたプロパティ一覧</param>
|
||||
/// <param name="optionalPassword">任意の暗号化パスワード</param>
|
||||
/// <returns>保存に成功した場合は true、それ以外は false</returns>
|
||||
bool SaveProperties(string filePath, List<SaveDataPropertyEntry> properties, string? optionalPassword = null);
|
||||
|
||||
/// <summary>復号にパスワードやキーが必要かどうか</summary>
|
||||
/// <summary>
|
||||
/// 復号にパスワードやキーが必要かどうかを取得します。
|
||||
/// </summary>
|
||||
bool RequiresPassword { get; }
|
||||
|
||||
/// <summary>パスワード入力時の案内メッセージ</summary>
|
||||
/// <summary>
|
||||
/// パスワード入力時の案内メッセージを取得します。
|
||||
/// </summary>
|
||||
string? PasswordPromptMessage { get; }
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using GameLauncher.Models;
|
||||
using GameLauncher.Engines.Unity;
|
||||
using GameLauncher.Models;
|
||||
|
||||
namespace GameLauncher.Services.Codecs
|
||||
{
|
||||
@@ -12,12 +12,27 @@ namespace GameLauncher.Services.Codecs
|
||||
/// </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;
|
||||
@@ -39,6 +54,12 @@ namespace GameLauncher.Services.Codecs
|
||||
}
|
||||
}
|
||||
|
||||
/// <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>();
|
||||
@@ -101,6 +122,13 @@ namespace GameLauncher.Services.Codecs
|
||||
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
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using GameLauncher.Engines.Unity;
|
||||
using GameLauncher.Models;
|
||||
using GameLauncher.Services.Codecs;
|
||||
using GameLauncher.Engines.Unity;
|
||||
using GameLauncher.Engines.Wolf;
|
||||
|
||||
namespace GameLauncher.Services
|
||||
{
|
||||
@@ -15,23 +14,32 @@ namespace GameLauncher.Services
|
||||
/// </summary>
|
||||
public class SaveDataCodecManager
|
||||
{
|
||||
/// <summary>
|
||||
/// シングルトンインスタンスを取得します。
|
||||
/// </summary>
|
||||
public static readonly SaveDataCodecManager Instance = new();
|
||||
|
||||
private readonly List<ISaveDataCodec> _codecs = [];
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="SaveDataCodecManager"/> クラスの新しいインスタンスを初期化します。
|
||||
/// </summary>
|
||||
public SaveDataCodecManager()
|
||||
{
|
||||
// コーデックの登録
|
||||
_codecs.Add(new EasySave3Codec());
|
||||
_codecs.Add(new YurisSaveCodec());
|
||||
this._codecs.Add(new EasySave3Codec());
|
||||
this._codecs.Add(new YurisSaveCodec());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 指定されたゲームおよびセーブファイルに対応するコーデックを検索します。
|
||||
/// </summary>
|
||||
/// <param name="game">対象のUnityゲーム情報</param>
|
||||
/// <param name="file">対象のセーブファイル情報</param>
|
||||
/// <returns>対応するコーデック。見つからない場合は null</returns>
|
||||
public ISaveDataCodec? FindCodec(UnityGameItem game, SaveFileEntry file)
|
||||
{
|
||||
foreach (var codec in _codecs)
|
||||
foreach (var codec in this._codecs)
|
||||
{
|
||||
if (codec.CanHandle(game, file))
|
||||
{
|
||||
@@ -44,6 +52,9 @@ namespace GameLauncher.Services
|
||||
/// <summary>
|
||||
/// 未対応セーブデータについて、Antigravity IDE に依頼できる安全なプロンプトを自動生成します。
|
||||
/// </summary>
|
||||
/// <param name="game">対象のUnityゲーム情報</param>
|
||||
/// <param name="file">対象のセーブファイル情報</param>
|
||||
/// <returns>生成された依頼プロンプト文字列</returns>
|
||||
public string GenerateAntigravityPrompt(UnityGameItem game, SaveFileEntry file)
|
||||
{
|
||||
// ポリシー配慮: 会社名・タイトル名からセンシティブ表現を除去/安全な別名に変換
|
||||
@@ -98,8 +109,10 @@ namespace GameLauncher.Services
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// AIプロンプト向けに文字列を安全・中立的な表現にサニタイズ
|
||||
/// AIプロンプト向けに文字列を安全・中立的な表現にサニタイズします。
|
||||
/// </summary>
|
||||
/// <param name="input">元の文字列</param>
|
||||
/// <returns>サニタイズされた安全な文字列</returns>
|
||||
private static string SanitizeForPrompt(string input)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(input)) return "Game";
|
||||
|
||||
Reference in new issue
Block a user