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);
|
||||
|
||||
Reference in new issue
Block a user