機能追加: Bakin・NW.jsエンジンおよびセーブ編集対応、ES3パスワード解決機能とRPGツクールMZ修正サービスの追加
This commit is contained in:
1 parent
f50976a6c0
commit
152abae1f8
33 files changed
+11356
-175
No files matched your search
@@ -0,0 +1,214 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using GameLauncher.Helpers;
|
||||
|
||||
namespace GameLauncher.Services.Codecs.Es3
|
||||
{
|
||||
/// <summary>
|
||||
/// ES3 のパスワード特定結果を「ゲーム単位」で永続化する学習キャッシュ。
|
||||
///
|
||||
/// 一度解決したパスワードを保存しておくことで、次回以降は自動適用できます。
|
||||
/// 保存先は exe 直下の <c>es3-passwords.json</c> で、キャッシュ破棄やブックマークとは独立に管理します。
|
||||
/// </summary>
|
||||
public sealed class Es3PasswordStore
|
||||
{
|
||||
/// <summary>永続化ファイル名。</summary>
|
||||
private const string FileName = "es3-passwords.json";
|
||||
|
||||
/// <summary>既定のシングルトンインスタンス。</summary>
|
||||
public static Es3PasswordStore Instance { get; } = new();
|
||||
|
||||
private readonly object _syncRoot = new();
|
||||
private readonly string _filePath;
|
||||
private Dictionary<string, string>? _entries;
|
||||
|
||||
/// <summary>
|
||||
/// exe 直下の既定パスを用いて <see cref="Es3PasswordStore"/> を初期化します。
|
||||
/// </summary>
|
||||
public Es3PasswordStore()
|
||||
: this(Path.Combine(AppPathHelper.GetExeDirectory(), FileName))
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 任意のファイルパスを指定して <see cref="Es3PasswordStore"/> を初期化します(テスト用途を含む)。
|
||||
/// </summary>
|
||||
/// <param name="filePath">永続化ファイルの絶対パス</param>
|
||||
public Es3PasswordStore(string filePath)
|
||||
{
|
||||
this._filePath = filePath;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 永続化ファイルのパスを取得します。
|
||||
/// </summary>
|
||||
public string FilePath => this._filePath;
|
||||
|
||||
/// <summary>
|
||||
/// 指定キーに対応する学習済みパスワードを取得します。
|
||||
/// </summary>
|
||||
/// <param name="key">ゲーム単位のキー(通常はゲームのルートディレクトリ)</param>
|
||||
/// <returns>保存済みパスワード。未登録の場合は null</returns>
|
||||
public string? TryGet(string key)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(key))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
lock (this._syncRoot)
|
||||
{
|
||||
var entries = this.LoadEntries();
|
||||
return entries.TryGetValue(Normalize(key), out var password) ? password : null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 学習済みパスワードを保存します。
|
||||
/// </summary>
|
||||
/// <param name="key">ゲーム単位のキー</param>
|
||||
/// <param name="password">保存するパスワード(空文字も有効な値として保存します)</param>
|
||||
public void Set(string key, string password)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(key))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
lock (this._syncRoot)
|
||||
{
|
||||
var entries = this.LoadEntries();
|
||||
entries[Normalize(key)] = password ?? string.Empty;
|
||||
this.SaveEntries(entries);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 指定キーの学習済みパスワードを削除します。
|
||||
/// </summary>
|
||||
/// <param name="key">ゲーム単位のキー</param>
|
||||
/// <returns>削除できた場合 true</returns>
|
||||
public bool Remove(string key)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(key))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
lock (this._syncRoot)
|
||||
{
|
||||
var entries = this.LoadEntries();
|
||||
if (!entries.Remove(Normalize(key)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
this.SaveEntries(entries);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 保存済みエントリのスナップショットを取得します(表示・診断用)。
|
||||
/// </summary>
|
||||
/// <returns>キーとパスワードの読み取り専用ディクショナリ</returns>
|
||||
public IReadOnlyDictionary<string, string> Snapshot()
|
||||
{
|
||||
lock (this._syncRoot)
|
||||
{
|
||||
return new Dictionary<string, string>(this.LoadEntries(), StringComparer.OrdinalIgnoreCase);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 永続化ファイルを読み込みます。壊れている場合は空のディクショナリを返します。
|
||||
/// </summary>
|
||||
/// <returns>読み込んだエントリ</returns>
|
||||
private Dictionary<string, string> LoadEntries()
|
||||
{
|
||||
if (this._entries != null)
|
||||
{
|
||||
return this._entries;
|
||||
}
|
||||
|
||||
var result = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
try
|
||||
{
|
||||
if (File.Exists(this._filePath))
|
||||
{
|
||||
var json = File.ReadAllText(this._filePath, Encoding.UTF8);
|
||||
if (!string.IsNullOrWhiteSpace(json))
|
||||
{
|
||||
var loaded = JsonSerializer.Deserialize<Dictionary<string, string>>(json);
|
||||
if (loaded != null)
|
||||
{
|
||||
foreach (var kvp in loaded)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(kvp.Key))
|
||||
{
|
||||
result[Normalize(kvp.Key)] = kvp.Value ?? string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// 破損ファイルは無視し、空の状態から再構築する
|
||||
result = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
this._entries = result;
|
||||
return this._entries;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// エントリを永続化ファイルへ書き出します。
|
||||
/// </summary>
|
||||
/// <param name="entries">書き出すエントリ</param>
|
||||
private void SaveEntries(Dictionary<string, string> entries)
|
||||
{
|
||||
try
|
||||
{
|
||||
var directory = Path.GetDirectoryName(this._filePath);
|
||||
if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
|
||||
{
|
||||
Directory.CreateDirectory(directory);
|
||||
}
|
||||
|
||||
var options = new JsonSerializerOptions { WriteIndented = true };
|
||||
var json = JsonSerializer.Serialize(entries, options);
|
||||
|
||||
// BOM なし UTF-8 で保存する
|
||||
File.WriteAllText(this._filePath, json, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
|
||||
}
|
||||
catch
|
||||
{
|
||||
// 保存失敗は致命的ではないため握りつぶす(次回再度解決を試みる)
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// キーを比較用に正規化します(パス区切りと大文字小文字の揺れを吸収)。
|
||||
/// </summary>
|
||||
/// <param name="key">元のキー</param>
|
||||
/// <returns>正規化済みキー</returns>
|
||||
private static string Normalize(string key)
|
||||
{
|
||||
var trimmed = key.Trim();
|
||||
try
|
||||
{
|
||||
return Path.GetFullPath(trimmed).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return trimmed;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user