refactor: reorganize engine-based structure and add agent rules
- Engines/ 配下へエンジン別に再編 (Common/Rgss/Tkool/Tyrano/Wolf/Unity/Nwjs) - ゲーム個別処理を Engines/<Engine>/Games/<GameName>/ へ分離し、共通処理から明示呼び出し - 外部ツールを Engines/<Engine>/Tools/ へ移動し、AppPathHelper 経由で解決 - 旧 Tools/ 直下参照と不要な参照ファイル/バックアップを削除 - エージェント規則 AGENTS.md を追加し、README から参照 - publish.ps1 を Engines/<Engine>/Tools/ の再帰配置に対応
This commit is contained in:
1 parent
0d11039ee9
commit
8cea2a147d
47 files changed
+2976
-176
No files matched your search
@@ -0,0 +1,722 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
using GameLauncher.Models;
|
||||
using GameLauncher.Engines.Unity;
|
||||
|
||||
namespace GameLauncher.Engines.Tkool
|
||||
{
|
||||
/// <summary>
|
||||
/// 各プラグイン改造の個別有効/無効オプション(互換用)
|
||||
/// </summary>
|
||||
public class ModPluginOptions : System.ComponentModel.INotifyPropertyChanged
|
||||
{
|
||||
private bool _speedUp = true;
|
||||
private bool _antigravityGameTweaks = true;
|
||||
private bool _dropItem99 = true;
|
||||
private bool _turnStartFullRecover = true;
|
||||
private bool _turnStartFullRecoverHp = true;
|
||||
private bool _turnStartFullRecoverMp = true;
|
||||
private bool _turnStartFullRecoverTp = true;
|
||||
private bool _maxGainGold = true;
|
||||
private bool _maxGainExp = true;
|
||||
private bool _instantMessage = true;
|
||||
|
||||
public bool SpeedUp
|
||||
{
|
||||
get => _speedUp;
|
||||
set { if (_speedUp != value) { _speedUp = value; OnPropertyChanged(); } }
|
||||
}
|
||||
|
||||
public bool AntigravityGameTweaks
|
||||
{
|
||||
get => _antigravityGameTweaks;
|
||||
set { if (_antigravityGameTweaks != value) { _antigravityGameTweaks = value; OnPropertyChanged(); } }
|
||||
}
|
||||
|
||||
public bool DropItem99
|
||||
{
|
||||
get => _dropItem99;
|
||||
set { if (_dropItem99 != value) { _dropItem99 = value; OnPropertyChanged(); } }
|
||||
}
|
||||
|
||||
public bool TurnStartFullRecover
|
||||
{
|
||||
get => _turnStartFullRecover;
|
||||
set { if (_turnStartFullRecover != value) { _turnStartFullRecover = value; OnPropertyChanged(); } }
|
||||
}
|
||||
|
||||
public bool TurnStartFullRecoverHp
|
||||
{
|
||||
get => _turnStartFullRecoverHp;
|
||||
set { if (_turnStartFullRecoverHp != value) { _turnStartFullRecoverHp = value; OnPropertyChanged(); } }
|
||||
}
|
||||
|
||||
public bool TurnStartFullRecoverMp
|
||||
{
|
||||
get => _turnStartFullRecoverMp;
|
||||
set { if (_turnStartFullRecoverMp != value) { _turnStartFullRecoverMp = value; OnPropertyChanged(); } }
|
||||
}
|
||||
|
||||
public bool TurnStartFullRecoverTp
|
||||
{
|
||||
get => _turnStartFullRecoverTp;
|
||||
set { if (_turnStartFullRecoverTp != value) { _turnStartFullRecoverTp = value; OnPropertyChanged(); } }
|
||||
}
|
||||
|
||||
public bool MaxGainGold
|
||||
{
|
||||
get => _maxGainGold;
|
||||
set { if (_maxGainGold != value) { _maxGainGold = value; OnPropertyChanged(); } }
|
||||
}
|
||||
|
||||
public bool MaxGainExp
|
||||
{
|
||||
get => _maxGainExp;
|
||||
set { if (_maxGainExp != value) { _maxGainExp = value; OnPropertyChanged(); } }
|
||||
}
|
||||
|
||||
public bool InstantMessage
|
||||
{
|
||||
get => _instantMessage;
|
||||
set { if (_instantMessage != value) { _instantMessage = value; OnPropertyChanged(); } }
|
||||
}
|
||||
|
||||
public event System.ComponentModel.PropertyChangedEventHandler? PropertyChanged;
|
||||
protected void OnPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string? name = null)
|
||||
{
|
||||
PropertyChanged?.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(name));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ツクールおよびティラノスクリプトゲームのデータモデル
|
||||
/// </summary>
|
||||
public class TkoolGameItem : GameItemBase
|
||||
{
|
||||
private string _pluginsJsPath = string.Empty;
|
||||
private string _pluginsDirPath = string.Empty;
|
||||
private TkoolEngineType _engineType = TkoolEngineType.Unknown;
|
||||
private bool _isModded;
|
||||
private bool _applyMod = true;
|
||||
private bool _hasCustomApplyMod;
|
||||
private bool _isInitializingApplyMod;
|
||||
private ModPluginOptions _modOptions = new();
|
||||
|
||||
// app.asar 関連プロパティ
|
||||
private bool _isAsar;
|
||||
private bool _isAsarExtracted;
|
||||
private string? _asarPath;
|
||||
private string? _asarExtractedDirPath;
|
||||
private string? _relativePluginsJsInAsar;
|
||||
|
||||
// ================= 単体exe梱包(FOSSIL / boxed)関連プロパティ =================
|
||||
// js/plugins.js がディスク上に無く、exeへ埋め込まれている形態を識別します。
|
||||
private bool _isFossilSingleExe;
|
||||
private bool _isBoxedExe;
|
||||
private bool _isStandaloneExeExtracted;
|
||||
private string? _standaloneExtractedDirPath;
|
||||
private string? _standaloneBackupPath;
|
||||
private string? _boxedPayloadEntryPath;
|
||||
private bool _canExtractViaExternalTool;
|
||||
|
||||
/// <summary>FOSSIL形態(FOSSILindex.html + 単体Game.exe、data/System.json はディスク上)かどうか</summary>
|
||||
public bool IsFossilSingleExe
|
||||
{
|
||||
get => _isFossilSingleExe;
|
||||
set { if (_isFossilSingleExe != value) { _isFossilSingleExe = value; OnPropertyChanged(); } }
|
||||
}
|
||||
|
||||
/// <summary>boxed形態(ゲーム本体をexeへ内包、例: Game_boxed.exe)かどうか</summary>
|
||||
public bool IsBoxedExe
|
||||
{
|
||||
get => _isBoxedExe;
|
||||
set { if (_isBoxedExe != value) { _isBoxedExe = value; OnPropertyChanged(); } }
|
||||
}
|
||||
|
||||
/// <summary>いずれかの単体exe梱包形態(FOSSIL / boxed)かどうか</summary>
|
||||
public bool IsStandaloneExePackage => IsFossilSingleExe || IsBoxedExe;
|
||||
|
||||
/// <summary>単体exe梱包ゲームを _extracted へ展開済みかどうか</summary>
|
||||
public bool IsStandaloneExeExtracted
|
||||
{
|
||||
get => _isStandaloneExeExtracted;
|
||||
set { if (_isStandaloneExeExtracted != value) { _isStandaloneExeExtracted = value; OnPropertyChanged(); } }
|
||||
}
|
||||
|
||||
/// <summary>展開先ディレクトリ(ゲームフォルダ内 _extracted)</summary>
|
||||
public string? StandaloneExtractedDirPath
|
||||
{
|
||||
get => _standaloneExtractedDirPath;
|
||||
set { if (_standaloneExtractedDirPath != value) { _standaloneExtractedDirPath = value; OnPropertyChanged(); } }
|
||||
}
|
||||
|
||||
/// <summary>exe内コンテナ改変時のバックアップパス(*.glbak)</summary>
|
||||
public string? StandaloneBackupPath
|
||||
{
|
||||
get => _standaloneBackupPath;
|
||||
set { if (_standaloneBackupPath != value) { _standaloneBackupPath = value; OnPropertyChanged(); } }
|
||||
}
|
||||
|
||||
/// <summary>boxed exe内で注入対象とするコンテナ内エントリ(例: js/plugins.js)</summary>
|
||||
public string? BoxedPayloadEntryPath
|
||||
{
|
||||
get => _boxedPayloadEntryPath;
|
||||
set { if (_boxedPayloadEntryPath != value) { _boxedPayloadEntryPath = value; OnPropertyChanged(); } }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 外部アンパッカー(Tools\7zr.exe 等)による展開を試行できる可能性があるか。
|
||||
/// 標準ZIP署名が未確定でも、boxed命名exeが存在する場合に true となり「展開試行可」を示す。
|
||||
/// 実際に展開可能かは実行時(ExtractPackageNw)に確定する。
|
||||
/// </summary>
|
||||
public bool CanExtractViaExternalTool
|
||||
{
|
||||
get => _canExtractViaExternalTool;
|
||||
set { if (_canExtractViaExternalTool != value) { _canExtractViaExternalTool = value; OnPropertyChanged(); } }
|
||||
}
|
||||
|
||||
public override GameEngineKind EngineKind => EngineType switch
|
||||
{
|
||||
TkoolEngineType.Tyrano => GameEngineKind.Tyrano,
|
||||
TkoolEngineType.Wolf => GameEngineKind.Wolf,
|
||||
TkoolEngineType.SrpgStudio => GameEngineKind.SrpgStudio,
|
||||
_ => GameEngineKind.Tkool
|
||||
};
|
||||
|
||||
public string PluginsJsPath
|
||||
{
|
||||
get => _pluginsJsPath;
|
||||
set { if (_pluginsJsPath != value) { _pluginsJsPath = value; OnPropertyChanged(); } }
|
||||
}
|
||||
|
||||
public string PluginsDirPath
|
||||
{
|
||||
get => _pluginsDirPath;
|
||||
set { if (_pluginsDirPath != value) { _pluginsDirPath = value; OnPropertyChanged(); } }
|
||||
}
|
||||
|
||||
public override bool IsMV => EngineType == TkoolEngineType.MV;
|
||||
public override bool IsMZ => EngineType == TkoolEngineType.MZ;
|
||||
public override bool IsTyrano => EngineType == TkoolEngineType.Tyrano;
|
||||
public override bool IsVX => EngineType == TkoolEngineType.VX;
|
||||
public override bool IsVXAce => EngineType == TkoolEngineType.VXAce;
|
||||
public override bool IsWolf => EngineType == TkoolEngineType.Wolf;
|
||||
public override bool IsSrpgStudio => EngineType == TkoolEngineType.SrpgStudio;
|
||||
public override bool IsRgss => EngineType is TkoolEngineType.VX or TkoolEngineType.VXAce;
|
||||
|
||||
/// <summary>
|
||||
/// 「プリンセスシナジー」(ツクールMZ製・専用改造対象ゲーム)かどうか。
|
||||
/// タイトルまたはフォルダ名に "プリンセスシナジー" を含み、エンジンがMZの場合のみ true。
|
||||
/// これにより当該機能が他ゲームに表示・適用されるのを防ぎます。
|
||||
/// </summary>
|
||||
public override bool IsPrincessSynergy =>
|
||||
!string.IsNullOrEmpty(Title) && Title.Contains("プリンセスシナジー");
|
||||
|
||||
// SRPG Studio アーカイブ関連プロパティ
|
||||
private bool _hasSrpgArchive;
|
||||
private bool _isSrpgExtracted;
|
||||
private string? _srpgArchivePath;
|
||||
|
||||
public override bool HasSrpgArchive
|
||||
{
|
||||
get => _hasSrpgArchive;
|
||||
set
|
||||
{
|
||||
if (_hasSrpgArchive != value)
|
||||
{
|
||||
_hasSrpgArchive = value;
|
||||
OnPropertyChanged();
|
||||
OnPropertyChanged(nameof(SrpgBadgeVisibility));
|
||||
OnPropertyChanged(nameof(SrpgBadgeText));
|
||||
OnPropertyChanged(nameof(SrpgBadgeBackground));
|
||||
OnPropertyChanged(nameof(SrpgDetailStatusText));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override bool IsSrpgExtracted
|
||||
{
|
||||
get => _isSrpgExtracted;
|
||||
set
|
||||
{
|
||||
if (_isSrpgExtracted != value)
|
||||
{
|
||||
_isSrpgExtracted = value;
|
||||
OnPropertyChanged();
|
||||
OnPropertyChanged(nameof(SrpgBadgeText));
|
||||
OnPropertyChanged(nameof(SrpgBadgeBackground));
|
||||
OnPropertyChanged(nameof(SrpgDetailStatusText));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public string? SrpgArchivePath
|
||||
{
|
||||
get => _srpgArchivePath;
|
||||
set { if (_srpgArchivePath != value) { _srpgArchivePath = value; OnPropertyChanged(); } }
|
||||
}
|
||||
|
||||
public override Visibility SrpgBadgeVisibility => HasSrpgArchive ? Visibility.Visible : Visibility.Collapsed;
|
||||
|
||||
public override string SrpgBadgeText => !HasSrpgArchive ? string.Empty : (IsSrpgExtracted ? "SRPG (抽出済)" : "SRPG (暗号化)");
|
||||
|
||||
public override Brush SrpgBadgeBackground => IsSrpgExtracted
|
||||
? new SolidColorBrush(Color.FromRgb(0x00, 0x89, 0x7B))
|
||||
: new SolidColorBrush(Color.FromRgb(0x09, 0x84, 0xE3));
|
||||
|
||||
public override string SrpgDetailStatusText => !HasSrpgArchive
|
||||
? string.Empty
|
||||
: (IsSrpgExtracted ? "📦 SRPGアーカイブ(スクリプト/リソース抽出済み・改造可能)" : "🔒 SRPG暗号化アーカイブ(ボタンで抽出・改造・再構築可能)");
|
||||
|
||||
// RGSS (VX / VX Ace) アーカイブ関連プロパティ
|
||||
private bool _hasRgssArchive;
|
||||
private bool _isRgssExtracted;
|
||||
private string? _rgssArchivePath;
|
||||
|
||||
public override bool HasRgssArchive
|
||||
{
|
||||
get => _hasRgssArchive;
|
||||
set
|
||||
{
|
||||
if (_hasRgssArchive != value)
|
||||
{
|
||||
_hasRgssArchive = value;
|
||||
OnPropertyChanged();
|
||||
OnPropertyChanged(nameof(RgssBadgeVisibility));
|
||||
OnPropertyChanged(nameof(RgssBadgeText));
|
||||
OnPropertyChanged(nameof(RgssBadgeBackground));
|
||||
OnPropertyChanged(nameof(RgssDetailStatusText));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override bool IsRgssExtracted
|
||||
{
|
||||
get => _isRgssExtracted;
|
||||
set
|
||||
{
|
||||
if (_isRgssExtracted != value)
|
||||
{
|
||||
_isRgssExtracted = value;
|
||||
OnPropertyChanged();
|
||||
OnPropertyChanged(nameof(RgssBadgeText));
|
||||
OnPropertyChanged(nameof(RgssBadgeBackground));
|
||||
OnPropertyChanged(nameof(RgssDetailStatusText));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public string? RgssArchivePath
|
||||
{
|
||||
get => _rgssArchivePath;
|
||||
set { if (_rgssArchivePath != value) { _rgssArchivePath = value; OnPropertyChanged(); } }
|
||||
}
|
||||
|
||||
public override Visibility RgssBadgeVisibility => HasRgssArchive ? Visibility.Visible : Visibility.Collapsed;
|
||||
|
||||
public override string RgssBadgeText => !HasRgssArchive ? string.Empty : (IsRgssExtracted ? "RGSS (抽出済)" : "RGSS (暗号化)");
|
||||
|
||||
public override Brush RgssBadgeBackground => IsRgssExtracted
|
||||
? new SolidColorBrush(Color.FromRgb(0x00, 0x89, 0x7B))
|
||||
: new SolidColorBrush(Color.FromRgb(0xD3, 0x54, 0x00));
|
||||
|
||||
public override string RgssDetailStatusText => !HasRgssArchive
|
||||
? string.Empty
|
||||
: (IsRgssExtracted ? "📦 RGSSアーカイブ(スクリプト抽出済み・改造可能)" : "🔒 RGSS暗号化アーカイブ(起動時またはボタンでスクリプト抽出可能)");
|
||||
|
||||
public TkoolEngineType EngineType
|
||||
{
|
||||
get => _engineType;
|
||||
set
|
||||
{
|
||||
if (_engineType != value)
|
||||
{
|
||||
_engineType = value;
|
||||
OnPropertyChanged();
|
||||
OnPropertyChanged(nameof(EngineKind));
|
||||
OnPropertyChanged(nameof(EngineDisplayName));
|
||||
OnPropertyChanged(nameof(IsMV));
|
||||
OnPropertyChanged(nameof(IsMZ));
|
||||
OnPropertyChanged(nameof(IsTyrano));
|
||||
OnPropertyChanged(nameof(IsVX));
|
||||
OnPropertyChanged(nameof(IsVXAce));
|
||||
OnPropertyChanged(nameof(IsWolf));
|
||||
OnPropertyChanged(nameof(IsSrpgStudio));
|
||||
OnPropertyChanged(nameof(IsRgss));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ================= WOLF RPGエディター関連プロパティ =================
|
||||
private bool _wolfProxyDll = false;
|
||||
private double _wolfSpeedMultiplier = 3.0;
|
||||
private bool _hasWolfArchive = false;
|
||||
private bool _isWolfExtracted = false;
|
||||
|
||||
/// <summary>プロキシDLL(version.dll 倍速/メモリフック)を適用するか</summary>
|
||||
public bool WolfProxyDll
|
||||
{
|
||||
get => _wolfProxyDll;
|
||||
set { if (_wolfProxyDll != value) { _wolfProxyDll = value; OnPropertyChanged(); } }
|
||||
}
|
||||
|
||||
/// <summary>プロキシDLLのスピード倍率</summary>
|
||||
public double WolfSpeedMultiplier
|
||||
{
|
||||
get => _wolfSpeedMultiplier;
|
||||
set { if (_wolfSpeedMultiplier != value) { _wolfSpeedMultiplier = value; OnPropertyChanged(); } }
|
||||
}
|
||||
|
||||
/// <summary>Data.wolf アーカイブが存在するか</summary>
|
||||
public override bool HasWolfArchive
|
||||
{
|
||||
get => _hasWolfArchive;
|
||||
set
|
||||
{
|
||||
if (_hasWolfArchive != value)
|
||||
{
|
||||
_hasWolfArchive = value;
|
||||
OnPropertyChanged();
|
||||
OnPropertyChanged(nameof(WolfArchiveBadgeVisibility));
|
||||
OnPropertyChanged(nameof(WolfArchiveBadgeText));
|
||||
OnPropertyChanged(nameof(WolfArchiveBadgeBackground));
|
||||
OnPropertyChanged(nameof(WolfArchiveDetailStatusText));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Data.wolf が解凍済み(Dataフォルダが存在)か</summary>
|
||||
public override bool IsWolfExtracted
|
||||
{
|
||||
get => _isWolfExtracted;
|
||||
set
|
||||
{
|
||||
if (_isWolfExtracted != value)
|
||||
{
|
||||
_isWolfExtracted = value;
|
||||
OnPropertyChanged();
|
||||
OnPropertyChanged(nameof(WolfArchiveBadgeVisibility));
|
||||
OnPropertyChanged(nameof(WolfArchiveBadgeText));
|
||||
OnPropertyChanged(nameof(WolfArchiveBadgeBackground));
|
||||
OnPropertyChanged(nameof(WolfArchiveDetailStatusText));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Visibility WolfArchiveBadgeVisibility => IsWolf && HasWolfArchive ? Visibility.Visible : Visibility.Collapsed;
|
||||
public string WolfArchiveBadgeText => IsWolfExtracted ? "Data解凍済" : "Data.wolf保護";
|
||||
public string WolfArchiveBadgeBackground => IsWolfExtracted ? "#00B894" : "#636E72";
|
||||
public string WolfArchiveDetailStatusText => IsWolfExtracted
|
||||
? "Data フォルダに解凍・抽出済みです。ウディタは Data フォルダのマップ・イベント・データベース等を優先して読み込みます。直接ファイルを改造できます。"
|
||||
: "Data.wolf アーカイブで暗号化・保護されています。下の「Data.wolf を解凍」を実行すると、全データを展開して直接編集・改造可能になります。";
|
||||
|
||||
// ================= SRPG Studio関連プロパティ =================
|
||||
private bool _srpgProxyDll = false;
|
||||
private double _srpgSpeedMultiplier = 3.0;
|
||||
|
||||
/// <summary>SRPG Studio プロキシDLL(version.dll 倍速機能)を適用するか</summary>
|
||||
public bool SrpgProxyDll
|
||||
{
|
||||
get => _srpgProxyDll;
|
||||
set { if (_srpgProxyDll != value) { _srpgProxyDll = value; OnPropertyChanged(); } }
|
||||
}
|
||||
|
||||
/// <summary>SRPG Studio プロキシDLLのスピード倍率</summary>
|
||||
public double SrpgSpeedMultiplier
|
||||
{
|
||||
get => _srpgSpeedMultiplier;
|
||||
set { if (_srpgSpeedMultiplier != value) { _srpgSpeedMultiplier = value; OnPropertyChanged(); } }
|
||||
}
|
||||
|
||||
/// <summary>SRPG Studio プラグイン改造オプション(PluginOptionsのエイリアス)</summary>
|
||||
public ObservableCollection<ModPluginOptionItem> SrpgModOptions => PluginOptions;
|
||||
|
||||
public override bool IsModded
|
||||
{
|
||||
get => _isModded;
|
||||
set { if (_isModded != value) { _isModded = value; OnPropertyChanged(); } }
|
||||
}
|
||||
|
||||
public bool HasCustomApplyMod
|
||||
{
|
||||
get => _hasCustomApplyMod;
|
||||
set { if (_hasCustomApplyMod != value) { _hasCustomApplyMod = value; OnPropertyChanged(); } }
|
||||
}
|
||||
|
||||
public override bool ApplyMod
|
||||
{
|
||||
get => _applyMod;
|
||||
set
|
||||
{
|
||||
if (_applyMod != value)
|
||||
{
|
||||
_applyMod = value;
|
||||
if (!_isInitializingApplyMod)
|
||||
{
|
||||
HasCustomApplyMod = true;
|
||||
}
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void SetDefaultApplyMod(bool defaultVal)
|
||||
{
|
||||
if (!_hasCustomApplyMod)
|
||||
{
|
||||
_isInitializingApplyMod = true;
|
||||
_applyMod = defaultVal;
|
||||
_isInitializingApplyMod = false;
|
||||
OnPropertyChanged(nameof(ApplyMod));
|
||||
}
|
||||
}
|
||||
|
||||
public override bool IsAsar
|
||||
{
|
||||
get => _isAsar;
|
||||
set
|
||||
{
|
||||
if (_isAsar != value)
|
||||
{
|
||||
_isAsar = value;
|
||||
OnPropertyChanged();
|
||||
OnPropertyChanged(nameof(AsarBadgeVisibility));
|
||||
OnPropertyChanged(nameof(AsarBadgeText));
|
||||
OnPropertyChanged(nameof(AsarBadgeBackground));
|
||||
OnPropertyChanged(nameof(AsarDetailStatusText));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override bool IsAsarExtracted
|
||||
{
|
||||
get => _isAsarExtracted;
|
||||
set
|
||||
{
|
||||
if (_isAsarExtracted != value)
|
||||
{
|
||||
_isAsarExtracted = value;
|
||||
OnPropertyChanged();
|
||||
OnPropertyChanged(nameof(AsarBadgeText));
|
||||
OnPropertyChanged(nameof(AsarBadgeBackground));
|
||||
OnPropertyChanged(nameof(AsarDetailStatusText));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public string? AsarPath
|
||||
{
|
||||
get => _asarPath;
|
||||
set { if (_asarPath != value) { _asarPath = value; OnPropertyChanged(); } }
|
||||
}
|
||||
|
||||
public string? AsarExtractedDirPath
|
||||
{
|
||||
get => _asarExtractedDirPath;
|
||||
set { if (_asarExtractedDirPath != value) { _asarExtractedDirPath = value; OnPropertyChanged(); } }
|
||||
}
|
||||
|
||||
public string? RelativePluginsJsInAsar
|
||||
{
|
||||
get => _relativePluginsJsInAsar;
|
||||
set { if (_relativePluginsJsInAsar != value) { _relativePluginsJsInAsar = value; OnPropertyChanged(); } }
|
||||
}
|
||||
|
||||
public override Visibility AsarBadgeVisibility => IsAsar ? Visibility.Visible : Visibility.Collapsed;
|
||||
|
||||
public override string AsarBadgeText => !IsAsar ? string.Empty : (IsAsarExtracted ? "ASAR (解凍済)" : "ASAR (圧縮)");
|
||||
|
||||
public override Brush AsarBadgeBackground => IsAsarExtracted
|
||||
? new SolidColorBrush(Color.FromRgb(0x00, 0x89, 0x7B))
|
||||
: new SolidColorBrush(Color.FromRgb(0x5E, 0x35, 0xB1));
|
||||
|
||||
public override string AsarDetailStatusText => !IsAsar
|
||||
? string.Empty
|
||||
: (IsAsarExtracted ? "📦 app.asar 形式(解凍済み・改造可能)" : "📦 app.asar 形式(圧縮中・起動時またはボタンで解凍可能)");
|
||||
|
||||
private Dictionary<string, bool> _pluginSettings = new(StringComparer.OrdinalIgnoreCase);
|
||||
private ObservableCollection<ModPluginOptionItem> _pluginOptions = [];
|
||||
|
||||
public Dictionary<string, bool> PluginSettings
|
||||
{
|
||||
get => _pluginSettings;
|
||||
set { if (_pluginSettings != value) { _pluginSettings = value; OnPropertyChanged(); } }
|
||||
}
|
||||
|
||||
public override ObservableCollection<ModPluginOptionItem> PluginOptions
|
||||
{
|
||||
get => _pluginOptions;
|
||||
set { if (_pluginOptions != value) { _pluginOptions = value; OnPropertyChanged(); } }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// プリンセスシナジー専用: HP常時MAX(プラグイン "AlwaysMaxHp")の有効状態。
|
||||
/// PluginSettings に直接マッピングされ、既存の改造適用パイプラインで反映されます。
|
||||
/// </summary>
|
||||
public bool PrincessAlwaysMaxHp
|
||||
{
|
||||
get => IsPluginEnabled("AlwaysMaxHp", fallbackVal: false);
|
||||
set => SetPrincessPluginSetting("AlwaysMaxHp", value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// プリンセスシナジー専用: MP常時MAX(プラグイン "AlwaysMaxMp")の有効状態。
|
||||
/// </summary>
|
||||
public bool PrincessAlwaysMaxMp
|
||||
{
|
||||
get => IsPluginEnabled("AlwaysMaxMp", fallbackVal: false);
|
||||
set => SetPrincessPluginSetting("AlwaysMaxMp", value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// プラグイン設定を更新し、関連プロパティの変更通知を発行します。
|
||||
/// </summary>
|
||||
private void SetPrincessPluginSetting(string pluginId, bool value)
|
||||
{
|
||||
if (_pluginSettings.TryGetValue(pluginId, out var cur) && cur == value)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_pluginSettings[pluginId] = value;
|
||||
OnPropertyChanged(pluginId == "AlwaysMaxHp" ? nameof(PrincessAlwaysMaxHp) : nameof(PrincessAlwaysMaxMp));
|
||||
// 連動して独自の改造設定であることを示す
|
||||
HasCustomApplyMod = true;
|
||||
}
|
||||
|
||||
public bool IsPluginEnabled(string pluginId, Dictionary<string, bool>? defaultSettings = null, bool fallbackVal = true)
|
||||
{
|
||||
if (_pluginSettings.TryGetValue(pluginId, out var val))
|
||||
{
|
||||
return val;
|
||||
}
|
||||
|
||||
if (pluginId is "BattleTurnRecoverHp" or "BattleTurnRecoverMp" or "BattleTurnRecoverTp")
|
||||
{
|
||||
if (_pluginSettings.TryGetValue("BattleTurnRecover", out var legacyVal))
|
||||
{
|
||||
return legacyVal;
|
||||
}
|
||||
}
|
||||
|
||||
if (defaultSettings != null && defaultSettings.TryGetValue(pluginId, out var defVal))
|
||||
{
|
||||
return defVal;
|
||||
}
|
||||
|
||||
if (pluginId is "BattleTurnRecoverHp" or "BattleTurnRecoverMp" or "BattleTurnRecoverTp")
|
||||
{
|
||||
if (defaultSettings != null && defaultSettings.TryGetValue("BattleTurnRecover", out var legacyDefVal))
|
||||
{
|
||||
return legacyDefVal;
|
||||
}
|
||||
}
|
||||
|
||||
return fallbackVal;
|
||||
}
|
||||
|
||||
public static readonly ModPluginDefinition[] DefaultTyranoPlugins =
|
||||
[
|
||||
new() { Id = "TyranoUnlockAllCg", Name = "🔓 全CG・全回想の全開放", Description = "システム変数(sf)のCG・回想フラグを全開放し、最初からCG・シーン鑑賞を可能にします。" },
|
||||
new() { Id = "TyranoFastMessage", Name = "⚡ メッセージ超高速化・演出ウェイト短縮", Description = "文字送り速度を最大化し、[wait]演出の停止時間を短縮して快適にスキップ可能にします。" },
|
||||
new() { Id = "TyranoDevTools", Name = "🛠 開発者ツール (F12) の有効化", Description = "F12キーでブラウザ・アプリの開発者ツールコンソールを開けるようにします。" },
|
||||
new() { Id = "TyranoCheatMenu", Name = "🎛 画面上変数(f)チートエディタ表示", Description = "画面右上にボタンを配置し、ゲーム内変数・フラグの閲覧と直接書き換えができるメニューを表示します。" }
|
||||
];
|
||||
|
||||
/// <summary>ツクールVX / VX Ace用スクリプト改造定義一覧</summary>
|
||||
public static readonly ModPluginDefinition[] DefaultRgssPlugins =
|
||||
[
|
||||
new() { Id = "RgssDashSpeedUp", Name = "⚡ 移動速度高速化&常時オートダッシュ", Description = "キャラクターの移動速度を高速化し、Shiftキーを押さなくても常時ダッシュ状態にします。" },
|
||||
new() { Id = "RgssItemGainMax", Name = "🎁 アイテム入手時 99個固定化", Description = "宝箱・イベント・戦闘等で通常アイテムを入手した際、入手個数を99個にします。" },
|
||||
new() { Id = "RgssItemNoConsume", Name = "♾️ アイテム消費なし", Description = "アイテムを使用しても所持個数を減少しないようにします。" },
|
||||
new() { Id = "RgssGoldGainMax", Name = "💰 獲得ゴールド最大化 (999,999G)", Description = "戦闘勝利時の獲得ゴールドを999,999Gにします。" },
|
||||
new() { Id = "RgssExpGainMax", Name = "📈 獲得経験値最大化 (999,999Exp)", Description = "戦闘勝利時の獲得経験値を999,999Expにします。" },
|
||||
new() { Id = "RgssBattleTurnRecoverHp", Name = "💖 ターン開始時 全回復 (HP)", Description = "戦闘中の毎ターン開始時にパーティ全員のHPを全回復します。" },
|
||||
new() { Id = "RgssBattleTurnRecoverMp", Name = "🔷 ターン開始時 全回復 (MP)", Description = "戦闘中の毎ターン開始時にパーティ全員のMPを全回復します。" },
|
||||
new() { Id = "RgssBattleTurnRecoverTp", Name = "⚡ ターン開始時 全回復 (TP)", Description = "戦闘中の毎ターン開始時にパーティ全員のTPを最大(100)にします。(VX Ace)" },
|
||||
new() { Id = "RgssMessageInstant", Name = "⏩ メッセージ瞬間表示・高速スキップ", Description = "会話ウィンドウの文字送りウェイトをカットし、瞬間表示します。" },
|
||||
new() { Id = "RgssCommandMemory", Name = "🧠 コマンド位置記憶 (ツクールMV風)", Description = "メニュー画面や戦闘中のアクターコマンドのカーソル選択位置を記憶して次回遷移時に復元します。" },
|
||||
new() { Id = "RgssWindowResizeFull", Name = "🖥 ウィンドウをディスプレイに合わせて整数倍拡大 (スクリプト注入)", Description = "ゲーム起動時に論理解像度をディスプレイに収まる最大の整数倍(1x/2x/3x...)へ拡大し、ウィンドウを追従させます。VX AceはGraphics.resize_screen、VX(RGSS2)はWin32APIフォールバックで拡大します。解除で完全復元されます。" },
|
||||
new() { Id = "RgssWindowResizeExternal", Name = "🪟 外部ツールでウィンドウ拡大 (Win32 API / Sizer等)", Description = "ゲーム側は無改造のまま、ランチャーがゲームウィンドウを検出してディスプレイに収まる最大の整数倍へ拡大します。外部ツールパス設定があればそちらを優先起動します。(既定OFF)" }
|
||||
];
|
||||
|
||||
/// <summary>SRPG Studio用スクリプト改造定義一覧</summary>
|
||||
public static readonly ModPluginDefinition[] DefaultSrpgPlugins =
|
||||
[
|
||||
new() { Id = "SrpgExpBoost", Name = "📈 獲得経験値最大化 (即LvUP / 100Exp固定)", Description = "戦闘やアイテム使用時の獲得経験値を最大(100Exp)にして毎回即座にレベルアップします。" },
|
||||
new() { Id = "SrpgMaxGold", Name = "💰 所持金最大化 (999,999G)", Description = "自軍ターン開始時および戦闘勝利時に所持金を常に最大(999,999G)に設定します。" },
|
||||
new() { Id = "SrpgInfDurability", Name = "♾️ 武器・アイテム耐久度無限", Description = "武器やアイテムを使用しても耐久度が減少しないようにします。" },
|
||||
new() { Id = "SrpgTurnRecoverHp", Name = "💖 自軍ターン開始時 全味方HP全回復", Description = "プレイヤーのターン開始時に生存している全味方ユニットのHPを満タンまで回復します。" },
|
||||
new() { Id = "SrpgMoveBoost", Name = "👟 味方移動力大幅強化 (+5)", Description = "自軍味方ユニットの基本移動力を+5加算し、広大なマップでも自在に行動できるようにします。" },
|
||||
new() { Id = "SrpgItemAllDrop", Name = "🎁 敵撃破時アイテム100%ドロップ", Description = "敵ユニットが所持しているドロップ可能アイテムのドロップ率を100%にします。" }
|
||||
];
|
||||
|
||||
public void SyncPluginOptions(IEnumerable<ModPluginDefinition> availablePlugins, Dictionary<string, bool>? defaultSettings = null)
|
||||
{
|
||||
_pluginOptions.Clear();
|
||||
if (EngineType == TkoolEngineType.Wolf)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
IEnumerable<ModPluginDefinition> targetDefs;
|
||||
if (EngineType == TkoolEngineType.Tyrano)
|
||||
{
|
||||
targetDefs = DefaultTyranoPlugins;
|
||||
}
|
||||
else if (IsRgss)
|
||||
{
|
||||
targetDefs = DefaultRgssPlugins;
|
||||
}
|
||||
else if (IsSrpgStudio)
|
||||
{
|
||||
targetDefs = DefaultSrpgPlugins;
|
||||
}
|
||||
else
|
||||
{
|
||||
targetDefs = availablePlugins;
|
||||
}
|
||||
|
||||
foreach (var def in targetDefs)
|
||||
{
|
||||
bool isEnabled = IsPluginEnabled(def.Id, defaultSettings, fallbackVal: true);
|
||||
var item = new ModPluginOptionItem
|
||||
{
|
||||
Definition = def,
|
||||
IsEnabled = isEnabled
|
||||
};
|
||||
item.PropertyChanged += (s, e) =>
|
||||
{
|
||||
if (e.PropertyName == nameof(ModPluginOptionItem.IsEnabled))
|
||||
{
|
||||
_pluginSettings[def.Id] = item.IsEnabled;
|
||||
}
|
||||
};
|
||||
_pluginOptions.Add(item);
|
||||
}
|
||||
}
|
||||
|
||||
public ModPluginOptions ModOptions
|
||||
{
|
||||
get => _modOptions;
|
||||
set { if (_modOptions != value) { _modOptions = value; OnPropertyChanged(); } }
|
||||
}
|
||||
|
||||
public string EngineDisplayName => EngineType switch
|
||||
{
|
||||
TkoolEngineType.MV => "RPGツクールMV",
|
||||
TkoolEngineType.MZ => "RPGツクールMZ",
|
||||
TkoolEngineType.Tyrano => "ティラノスクリプト",
|
||||
TkoolEngineType.VX => "RPGツクールVX",
|
||||
TkoolEngineType.VXAce => "RPGツクールVX Ace",
|
||||
TkoolEngineType.Wolf => "WOLF RPGエディター",
|
||||
TkoolEngineType.SrpgStudio => "SRPG Studio",
|
||||
_ => "ツクール/ティラノ"
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,208 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace GameLauncher.Engines.Tkool
|
||||
{
|
||||
/// <summary>
|
||||
/// 対象ゲームの plugins.js に対する可逆なJSプラグイン注入(mod)を行うサービス。
|
||||
///
|
||||
/// 設計方針:
|
||||
/// - ゲーム専用改造は他ゲームへ一切影響を与えない(対象ゲームフォルダ内でのみ完結)。
|
||||
/// - 注入前に plugins.js を *.glbak へバックアップし、marker 区間のみを書き換える。
|
||||
/// - Restore は *.glbak から復元し、配置した mod JS を削除して完全可逆にする。
|
||||
/// - exe内 package.nw 直接書換は最終手段(必須バックアップ)。
|
||||
/// </summary>
|
||||
public sealed class TkoolInjectionService
|
||||
{
|
||||
/// <summary>注入マーカー(この区間のみを除去すれば完全に元へ戻せる)</summary>
|
||||
public const string BeginMarker = "/* === GAMELAUNCHER_MOD_BEGIN === */";
|
||||
public const string EndMarker = "/* === GAMELAUNCHER_MOD_END === */";
|
||||
|
||||
/// <summary>注入する改造プラグインファイル名</summary>
|
||||
public const string ModPluginId = "GameLauncher_Mod";
|
||||
public const string ModPluginFileName = "GameLauncher_Mod.js";
|
||||
|
||||
/// <summary>exe内コンテナ改変時のバックアップ拡張子</summary>
|
||||
public const string ExeBackupSuffix = ".glbak";
|
||||
|
||||
/// <summary>
|
||||
/// plugins.js に marker が存在する場合、改造注入済みと判定します。
|
||||
/// </summary>
|
||||
public static bool CheckIsModded(string pluginsJsPath)
|
||||
{
|
||||
if (string.IsNullOrEmpty(pluginsJsPath) || !File.Exists(pluginsJsPath))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var content = File.ReadAllText(pluginsJsPath, Encoding.UTF8);
|
||||
// パス破損の影響を避けるため、対称な EndMarker(先頭/末尾共通)で判定
|
||||
return content.Contains(BeginMarker) || content.Contains(ModPluginId);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// modプラグインエントリを plugins.js へ marker 付きで追記し、
|
||||
/// mod JS を js/plugins/ へ配置します。バックアップ(*.glbak)は必須。
|
||||
/// </summary>
|
||||
public bool Apply(string pluginsJsPath, string pluginsDir, string modSourcePath)
|
||||
{
|
||||
if (string.IsNullOrEmpty(pluginsJsPath) || !File.Exists(pluginsJsPath))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(modSourcePath) || !File.Exists(modSourcePath))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var backupPath = pluginsJsPath + ExeBackupSuffix;
|
||||
|
||||
// 1. バックアップ(未作成時のみ)。既に marker がある場合はクリーン化して保持
|
||||
if (!File.Exists(backupPath))
|
||||
{
|
||||
var original = File.ReadAllText(pluginsJsPath, Encoding.UTF8);
|
||||
File.WriteAllText(backupPath, RemoveInjection(original), new UTF8Encoding(false));
|
||||
}
|
||||
|
||||
// 2. mod JS を plugins ディレクトリへ配置
|
||||
if (string.IsNullOrEmpty(pluginsDir))
|
||||
{
|
||||
pluginsDir = Path.Combine(Path.GetDirectoryName(pluginsJsPath)!, "plugins");
|
||||
}
|
||||
Directory.CreateDirectory(pluginsDir);
|
||||
File.Copy(modSourcePath, Path.Combine(pluginsDir, ModPluginFileName), overwrite: true);
|
||||
|
||||
// 3. バックアップ(クリーン状態)を元に marker 区間を注入
|
||||
var baseContent = File.ReadAllText(backupPath, Encoding.UTF8);
|
||||
var injected = InjectEntry(RemoveInjection(baseContent));
|
||||
File.WriteAllText(pluginsJsPath, injected, new UTF8Encoding(false));
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// *.glbak から plugins.js を復元し、配置した mod JS を削除します(完全可逆)。
|
||||
/// </summary>
|
||||
public bool Restore(string pluginsJsPath, string pluginsDir)
|
||||
{
|
||||
if (string.IsNullOrEmpty(pluginsJsPath) || !File.Exists(pluginsJsPath))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var backupPath = pluginsJsPath + ExeBackupSuffix;
|
||||
|
||||
if (File.Exists(backupPath))
|
||||
{
|
||||
var backupContent = File.ReadAllText(backupPath, Encoding.UTF8);
|
||||
File.WriteAllText(pluginsJsPath, backupContent, new UTF8Encoding(false));
|
||||
try { File.Delete(backupPath); } catch { }
|
||||
}
|
||||
else
|
||||
{
|
||||
// バックアップが無くても現状から marker 区間を除去
|
||||
var current = File.ReadAllText(pluginsJsPath, Encoding.UTF8);
|
||||
File.WriteAllText(pluginsJsPath, RemoveInjection(current), new UTF8Encoding(false));
|
||||
}
|
||||
|
||||
// 配置した mod JS を削除(ゴミを残さない)
|
||||
if (!string.IsNullOrEmpty(pluginsDir))
|
||||
{
|
||||
var modPlugin = Path.Combine(pluginsDir, ModPluginFileName);
|
||||
if (File.Exists(modPlugin))
|
||||
{
|
||||
try { File.Delete(modPlugin); } catch { }
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// plugins.js の配列へ mod プラグインのエントリを marker 付きで追加します。
|
||||
/// </summary>
|
||||
private static string InjectEntry(string content)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(content))
|
||||
{
|
||||
return content;
|
||||
}
|
||||
|
||||
int arrayStart = content.IndexOf('[');
|
||||
int arrayEnd = content.LastIndexOf(']');
|
||||
if (arrayStart == -1 || arrayEnd == -1 || arrayEnd <= arrayStart)
|
||||
{
|
||||
return content;
|
||||
}
|
||||
|
||||
var entry = $"{BeginMarker}{{\"name\":\"{ModPluginId}\",\"status\":true,\"description\":\"GameLauncher mod\",\"parameters\":{{}}}}{EndMarker}";
|
||||
|
||||
var prefix = content.Substring(0, arrayStart + 1);
|
||||
var inner = content.Substring(arrayStart + 1, arrayEnd - arrayStart - 1);
|
||||
var suffix = content.Substring(arrayEnd);
|
||||
|
||||
var trimmedInner = inner.Trim();
|
||||
var newInner = string.IsNullOrEmpty(trimmedInner)
|
||||
? "\n" + entry + "\n"
|
||||
: inner.TrimEnd() + ",\n" + entry + "\n";
|
||||
|
||||
return prefix + newInner + suffix;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// marker 区間(BEGIN..END)を完全に除去します。これにより注入前の状態へ復元できます。
|
||||
/// </summary>
|
||||
private static string RemoveInjection(string content)
|
||||
{
|
||||
if (string.IsNullOrEmpty(content))
|
||||
{
|
||||
return content;
|
||||
}
|
||||
|
||||
string prev;
|
||||
do
|
||||
{
|
||||
prev = content;
|
||||
int begin = content.IndexOf(BeginMarker, StringComparison.Ordinal);
|
||||
if (begin < 0) break;
|
||||
|
||||
int end = content.IndexOf(EndMarker, begin, StringComparison.Ordinal);
|
||||
if (end < 0) break;
|
||||
end += EndMarker.Length;
|
||||
|
||||
// 直前のカンマ・余分な空白も除去対象に含める
|
||||
int cutStart = begin;
|
||||
int scan = begin - 1;
|
||||
while (scan >= 0 && char.IsWhiteSpace(content[scan])) scan--;
|
||||
if (scan >= 0 && content[scan] == ',') cutStart = scan;
|
||||
|
||||
content = content.Substring(0, cutStart) + content.Substring(end);
|
||||
}
|
||||
while (content != prev);
|
||||
|
||||
return content;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,482 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using GameLauncher.Helpers;
|
||||
using GameLauncher.Models;
|
||||
|
||||
namespace GameLauncher.Engines.Tkool
|
||||
{
|
||||
/// <summary>
|
||||
/// ツクールスクリプト改造プラグインの動的検出・適用・復元・管理を行うサービス
|
||||
/// </summary>
|
||||
public class TkoolModService
|
||||
{
|
||||
/// <summary>バックアップファイルのサフィックス</summary>
|
||||
public const string BackupSuffix = ".original_launcher_backup";
|
||||
|
||||
/// <summary>
|
||||
/// 過去のバージョンで使用されていたレガシープラグイン名一覧(完全削除・復元互換用)
|
||||
/// 以前改造されたゲームの検知、復元時のクリーンアップ、新改造適用時の旧ゴミ削除に使用します。
|
||||
/// </summary>
|
||||
public static readonly string[] LegacyPluginNames =
|
||||
[
|
||||
"SpeedUp",
|
||||
"Antigravity_GameTweaks",
|
||||
"DropItem99",
|
||||
"TurnStartFullRecover",
|
||||
"BattleTurnRecover",
|
||||
"MaxGainGold",
|
||||
"MaxGainExp",
|
||||
"InstantMessage"
|
||||
];
|
||||
|
||||
private List<ModPluginDefinition> _availablePlugins = [];
|
||||
|
||||
/// <summary>現在認識されている利用可能なプラグイン一覧</summary>
|
||||
public IReadOnlyList<ModPluginDefinition> AvailablePlugins => _availablePlugins;
|
||||
|
||||
public TkoolModService()
|
||||
{
|
||||
LoadAvailablePlugins();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// exe と同一階層にある plugins フォルダの物理パスを取得します。
|
||||
/// </summary>
|
||||
public static string GetPluginsDirectory()
|
||||
{
|
||||
// 1. 実行ファイルと同じ物理ディレクトリ
|
||||
var exeDir = AppPathHelper.GetExeDirectory();
|
||||
var primary = Path.Combine(exeDir, "plugins");
|
||||
if (Directory.Exists(primary))
|
||||
{
|
||||
return primary;
|
||||
}
|
||||
|
||||
// 2. 本番配置フォルダ(Z:\自作ソフト\GameLauncher\plugins)
|
||||
const string prodPath = @"Z:\自作ソフト\GameLauncher\plugins";
|
||||
if (Directory.Exists(prodPath))
|
||||
{
|
||||
return prodPath;
|
||||
}
|
||||
|
||||
// 3. AppContext.BaseDirectory 配下のフォールバック
|
||||
var baseDir = AppContext.BaseDirectory;
|
||||
var cand1 = Path.Combine(baseDir, "plugins");
|
||||
if (Directory.Exists(cand1)) return cand1;
|
||||
|
||||
return primary;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// exe と同階層の plugins フォルダから動的にプラグイン一覧を読み込みます。
|
||||
/// </summary>
|
||||
public List<ModPluginDefinition> LoadAvailablePlugins()
|
||||
{
|
||||
var list = new List<ModPluginDefinition>();
|
||||
var pluginsDir = GetPluginsDirectory();
|
||||
|
||||
if (Directory.Exists(pluginsDir))
|
||||
{
|
||||
foreach (var file in Directory.EnumerateFiles(pluginsDir, "*.js"))
|
||||
{
|
||||
try
|
||||
{
|
||||
list.Add(ModPluginDefinition.FromFile(file));
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
|
||||
_availablePlugins = list;
|
||||
return list;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 現在有効なプラグイン名と旧レガシープラグイン名のすべてを合算した除去対象プラグイン名セットを取得します。
|
||||
/// </summary>
|
||||
public static HashSet<string> GetAllManagedPluginNames(IEnumerable<ModPluginDefinition>? currentPlugins = null)
|
||||
{
|
||||
var set = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
// 過去のレガシープラグイン名をすべて含める
|
||||
foreach (var leg in LegacyPluginNames)
|
||||
{
|
||||
set.Add(leg);
|
||||
}
|
||||
|
||||
// 現在のプラグイン群を含める
|
||||
if (currentPlugins != null)
|
||||
{
|
||||
foreach (var p in currentPlugins)
|
||||
{
|
||||
set.Add(p.Id);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var dir = GetPluginsDirectory();
|
||||
if (Directory.Exists(dir))
|
||||
{
|
||||
foreach (var file in Directory.EnumerateFiles(dir, "*.js"))
|
||||
{
|
||||
set.Add(Path.GetFileNameWithoutExtension(file));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 指定されたゲームに対して、現在のチェック状態に応じた改造の適用または復元を行います。
|
||||
/// </summary>
|
||||
public (bool Success, string Message) ApplyOrRestoreMod(TkoolGameItem game, Dictionary<string, bool>? defaultSettings = null)
|
||||
{
|
||||
if (string.IsNullOrEmpty(game.PluginsJsPath) || !File.Exists(game.PluginsJsPath))
|
||||
{
|
||||
return (false, "plugins.js が見つかりません。");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (game.ApplyMod)
|
||||
{
|
||||
return ApplyMod(game, defaultSettings);
|
||||
}
|
||||
else
|
||||
{
|
||||
return RestoreOriginal(game);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return (false, $"スクリプト処理中にエラーが発生しました: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 改造プラグインを適用します。
|
||||
/// </summary>
|
||||
public (bool Success, string Message) ApplyMod(TkoolGameItem game, Dictionary<string, bool>? defaultSettings = null)
|
||||
{
|
||||
var pluginsJsPath = game.PluginsJsPath;
|
||||
var pluginsDir = game.PluginsDirPath;
|
||||
|
||||
if (string.IsNullOrEmpty(pluginsDir))
|
||||
{
|
||||
pluginsDir = Path.Combine(Path.GetDirectoryName(pluginsJsPath)!, "plugins");
|
||||
game.PluginsDirPath = pluginsDir;
|
||||
}
|
||||
|
||||
if (!Directory.Exists(pluginsDir))
|
||||
{
|
||||
Directory.CreateDirectory(pluginsDir);
|
||||
}
|
||||
|
||||
var gameBackupDir = AppPathHelper.GetGameBackupDirectory(game.FolderName, game.RootDirectory);
|
||||
var backupPath = Path.Combine(gameBackupDir, "plugins.js" + BackupSuffix);
|
||||
var legacyBackupPath = pluginsJsPath + BackupSuffix;
|
||||
|
||||
// 1. バックアップが存在しない場合、バックアップを作成する (Backups フォルダに保存)
|
||||
if (!File.Exists(backupPath))
|
||||
{
|
||||
if (File.Exists(legacyBackupPath))
|
||||
{
|
||||
// 既存のゲーム内バックアップがあれば Backups フォルダへコピー
|
||||
File.Copy(legacyBackupPath, backupPath, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
string currentContent = File.ReadAllText(pluginsJsPath, Encoding.UTF8);
|
||||
// 既に改造プラグイン(新旧問わず)が埋め込まれている場合はクリーンアップしたものをバックアップとして保持
|
||||
string cleanContent = CleanManagedPluginsFromContent(currentContent);
|
||||
File.WriteAllText(backupPath, cleanContent, new UTF8Encoding(false));
|
||||
}
|
||||
}
|
||||
|
||||
// 2. バックアップ(クリーン状態)を元に編集を開始
|
||||
string baseContent = File.ReadAllText(backupPath, Encoding.UTF8);
|
||||
|
||||
// 最新の動的プラグイン一覧を取得
|
||||
if (_availablePlugins.Count == 0)
|
||||
{
|
||||
LoadAvailablePlugins();
|
||||
}
|
||||
|
||||
// 有効化対象のプラグインを取得
|
||||
var pluginsToEnable = _availablePlugins
|
||||
.Where(p => game.IsPluginEnabled(p.Id, defaultSettings, fallbackVal: true))
|
||||
.ToList();
|
||||
|
||||
// 3. ゲーム側の plugins ディレクトリから不要な旧プラグインや無効プラグインを安全に掃除
|
||||
var allManaged = GetAllManagedPluginNames(_availablePlugins);
|
||||
var enabledIds = new HashSet<string>(pluginsToEnable.Select(p => p.Id), StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
foreach (var pluginId in allManaged)
|
||||
{
|
||||
// 有効対象ではないプラグインファイル(旧プラグインや今回OFFにしたプラグイン)を削除
|
||||
if (!enabledIds.Contains(pluginId))
|
||||
{
|
||||
var targetFile = Path.Combine(pluginsDir, $"{pluginId}.js");
|
||||
if (File.Exists(targetFile))
|
||||
{
|
||||
try { File.Delete(targetFile); } catch { }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 有効対象のプラグインファイルをゲーム側へ配置
|
||||
foreach (var plugin in pluginsToEnable)
|
||||
{
|
||||
CopyPluginToGame(plugin, pluginsDir);
|
||||
}
|
||||
|
||||
// 5. plugins.js をパース・再構築して有効プラグインを追加
|
||||
var pluginNamesToAdd = pluginsToEnable.Select(p => p.Id).ToList();
|
||||
string updatedContent = RebuildPluginsJs(baseContent, pluginNamesToAdd);
|
||||
|
||||
// 6. 保存
|
||||
File.WriteAllText(pluginsJsPath, updatedContent, new UTF8Encoding(false));
|
||||
game.IsModded = true;
|
||||
|
||||
return (true, $"ツクールスクリプト改造を正常に適用しました。(有効プラグイン: {pluginsToEnable.Count}個)");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 改造を解除し、ゲームを改造前のオリジナル状態に復元します。
|
||||
/// 旧レガシープラグインも含めて完全にクリーンアップします。
|
||||
/// </summary>
|
||||
public (bool Success, string Message) RestoreOriginal(TkoolGameItem game)
|
||||
{
|
||||
var pluginsJsPath = game.PluginsJsPath;
|
||||
var pluginsDir = game.PluginsDirPath;
|
||||
var gameBackupDir = AppPathHelper.GetGameBackupDirectory(game.FolderName, game.RootDirectory);
|
||||
var backupPath = Path.Combine(gameBackupDir, "plugins.js" + BackupSuffix);
|
||||
var legacyBackupPath = pluginsJsPath + BackupSuffix;
|
||||
|
||||
// 1. バックアップが存在する場合はバックアップから復元(再構築を通すことで壊れたバックアップも修復)
|
||||
if (File.Exists(backupPath))
|
||||
{
|
||||
string backupContent = File.ReadAllText(backupPath, Encoding.UTF8);
|
||||
string cleanContent = RebuildPluginsJs(backupContent, null);
|
||||
File.WriteAllText(pluginsJsPath, cleanContent, new UTF8Encoding(false));
|
||||
}
|
||||
else if (File.Exists(legacyBackupPath))
|
||||
{
|
||||
string backupContent = File.ReadAllText(legacyBackupPath, Encoding.UTF8);
|
||||
string cleanContent = RebuildPluginsJs(backupContent, null);
|
||||
File.WriteAllText(pluginsJsPath, cleanContent, new UTF8Encoding(false));
|
||||
// Backups フォルダへもコピー
|
||||
try { File.Copy(legacyBackupPath, backupPath, true); } catch { }
|
||||
}
|
||||
else
|
||||
{
|
||||
// バックアップがなくても、現在の plugins.js から新旧すべての管理プラグインを綺麗に除去
|
||||
string currentContent = File.ReadAllText(pluginsJsPath, Encoding.UTF8);
|
||||
string cleanContent = RebuildPluginsJs(currentContent, null);
|
||||
File.WriteAllText(pluginsJsPath, cleanContent, new UTF8Encoding(false));
|
||||
}
|
||||
|
||||
// 2. ゲーム側の plugins フォルダから新旧すべてのプラグインファイルを安全に削除(ゴミを残さない)
|
||||
if (!string.IsNullOrEmpty(pluginsDir) && Directory.Exists(pluginsDir))
|
||||
{
|
||||
var allManaged = GetAllManagedPluginNames(_availablePlugins);
|
||||
foreach (var pluginName in allManaged)
|
||||
{
|
||||
var targetFile = Path.Combine(pluginsDir, $"{pluginName}.js");
|
||||
if (File.Exists(targetFile))
|
||||
{
|
||||
try { File.Delete(targetFile); } catch { /* 削除不可時は無視 */ }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. ゲームフォルダ内の旧バックアップファイルがあれば削除してBackupsフォルダに一本化
|
||||
if (File.Exists(legacyBackupPath))
|
||||
{
|
||||
try { File.Delete(legacyBackupPath); } catch { }
|
||||
}
|
||||
|
||||
game.IsModded = false;
|
||||
return (true, "ゲームを改造前のオリジナル状態に正常に復元しました。(旧プラグインも完全削除済み)");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// plugins.js のテキストから新旧すべての管理対象プラグインの登録記述を除去します。
|
||||
/// </summary>
|
||||
public static string CleanManagedPluginsFromContent(string content)
|
||||
{
|
||||
return RebuildPluginsJs(content, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// plugins.js の内容を構文レベルで解析し、新旧管理対象プラグインの完全除去および新規追加を行って
|
||||
/// 正常な JavaScript 配列構文として再構築します。
|
||||
/// </summary>
|
||||
public static string RebuildPluginsJs(string content, IReadOnlyList<string>? pluginsToAdd = null)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(content))
|
||||
{
|
||||
return content;
|
||||
}
|
||||
|
||||
int arrayStart = content.IndexOf('[');
|
||||
int arrayEnd = content.LastIndexOf(']');
|
||||
if (arrayStart == -1 || arrayEnd == -1 || arrayEnd <= arrayStart)
|
||||
{
|
||||
return content;
|
||||
}
|
||||
|
||||
string prefix = content.Substring(0, arrayStart + 1);
|
||||
string inner = content.Substring(arrayStart + 1, arrayEnd - arrayStart - 1);
|
||||
string suffix = content.Substring(arrayEnd);
|
||||
|
||||
// 新旧すべてのプラグイン名を除去対象とする
|
||||
var managedSet = GetAllManagedPluginNames();
|
||||
var keptObjects = new List<string>();
|
||||
|
||||
int i = 0;
|
||||
while (i < inner.Length)
|
||||
{
|
||||
// 次のオブジェクト開始 '{' を探す
|
||||
while (i < inner.Length && inner[i] != '{')
|
||||
{
|
||||
i++;
|
||||
}
|
||||
if (i >= inner.Length) break;
|
||||
|
||||
int objStart = i;
|
||||
int depth = 0;
|
||||
bool inString = false;
|
||||
bool isEscaped = false;
|
||||
|
||||
while (i < inner.Length)
|
||||
{
|
||||
char c = inner[i];
|
||||
if (isEscaped)
|
||||
{
|
||||
isEscaped = false;
|
||||
}
|
||||
else if (c == '\\')
|
||||
{
|
||||
if (inString) isEscaped = true;
|
||||
}
|
||||
else if (c == '"')
|
||||
{
|
||||
inString = !inString;
|
||||
}
|
||||
else if (!inString)
|
||||
{
|
||||
if (c == '{') depth++;
|
||||
else if (c == '}')
|
||||
{
|
||||
depth--;
|
||||
if (depth == 0)
|
||||
{
|
||||
i++; // '}' を含める
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
if (depth == 0)
|
||||
{
|
||||
string objText = inner.Substring(objStart, i - objStart).Trim();
|
||||
// このオブジェクトが新旧管理対象プラグインかどうか判定
|
||||
var match = Regex.Match(objText, @"""name""\s*:\s*""([^""]+)""");
|
||||
if (match.Success && managedSet.Contains(match.Groups[1].Value))
|
||||
{
|
||||
// 管理対象プラグインのため除外
|
||||
continue;
|
||||
}
|
||||
keptObjects.Add(objText);
|
||||
}
|
||||
}
|
||||
|
||||
// 追加対象のプラグインを登録
|
||||
if (pluginsToAdd != null)
|
||||
{
|
||||
foreach (var pluginName in pluginsToAdd)
|
||||
{
|
||||
keptObjects.Add($"{{\"name\":\"{pluginName}\",\"status\":true,\"description\":\"\",\"parameters\":{{}}}}");
|
||||
}
|
||||
}
|
||||
|
||||
// 綺麗な JavaScript 配列として再構築
|
||||
var sb = new StringBuilder();
|
||||
sb.Append(prefix.TrimEnd()).AppendLine();
|
||||
for (int k = 0; k < keptObjects.Count; k++)
|
||||
{
|
||||
sb.Append(keptObjects[k]);
|
||||
if (k < keptObjects.Count - 1)
|
||||
{
|
||||
sb.Append(",");
|
||||
}
|
||||
sb.AppendLine();
|
||||
}
|
||||
sb.Append(suffix.TrimStart());
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// プラグインファイルをゲーム側の plugins ディレクトリへコピーします。
|
||||
/// </summary>
|
||||
private void CopyPluginToGame(ModPluginDefinition plugin, string targetDir)
|
||||
{
|
||||
var destinationPath = Path.Combine(targetDir, plugin.FileName);
|
||||
|
||||
// 1. 指定ファイルの物理パスからコピー
|
||||
if (!string.IsNullOrEmpty(plugin.FilePath) && File.Exists(plugin.FilePath))
|
||||
{
|
||||
File.Copy(plugin.FilePath, destinationPath, true);
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. plugins ディレクトリからのコピーを試行
|
||||
var localPluginPath = Path.Combine(GetPluginsDirectory(), plugin.FileName);
|
||||
if (File.Exists(localPluginPath))
|
||||
{
|
||||
File.Copy(localPluginPath, destinationPath, true);
|
||||
return;
|
||||
}
|
||||
|
||||
throw new FileNotFoundException($"プラグインファイルが見つかりません: {plugin.FileName} (参照先: {GetPluginsDirectory()})");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 現在 plugins.js に改造プラグイン(新旧問わず)が記述されているか判定します。
|
||||
/// </summary>
|
||||
public static bool CheckIsModded(string pluginsJsPath)
|
||||
{
|
||||
if (string.IsNullOrEmpty(pluginsJsPath) || !File.Exists(pluginsJsPath))
|
||||
return false;
|
||||
|
||||
try
|
||||
{
|
||||
string content = File.ReadAllText(pluginsJsPath);
|
||||
var allManaged = GetAllManagedPluginNames();
|
||||
foreach (var pluginName in allManaged)
|
||||
{
|
||||
if (content.Contains($"\"name\":\"{pluginName}\""))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// 読み取り失敗時は false
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,249 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using GameLauncher.Engines.Nwjs;
|
||||
|
||||
namespace GameLauncher.Engines.Tkool
|
||||
{
|
||||
/// <summary>
|
||||
/// nw.js boxed(exe埋め込み package.nw)等の単体exe梱包ツクールゲームの解析と
|
||||
/// ゲームフォルダ内 `_extracted` への冪等展開を行うオーケストレーター。
|
||||
///
|
||||
/// 役割分担(恒久・再利用可能を目的とした汎用設計):
|
||||
/// - 本サービス = 抽出の「進行役(オーケストレーター)」。
|
||||
/// - <see cref="NwjsPackageUnpacker"/> = 組み込みの埋め込みZIPコンテナ検出・展開。
|
||||
/// - <see cref="NwjsUnpackerToolProvider"/> = 外部アンパッカーの取得・呼び出し。
|
||||
///
|
||||
/// 展開戦略(順に試行):
|
||||
/// 1. 組み込みZIP展開: exe内の標準ZIPコンテナ(package.nw)を検出して展開。
|
||||
/// 2. 外部ツール委譲: Tools\ 配下の外部アンパッカー(未配置なら自動取得)へ委譲。
|
||||
///
|
||||
/// 設計方針:
|
||||
/// - 1.14GB級exeを対象に含むため、組み込み検出は限定/逐次走査で行う。
|
||||
/// - 展開はゲームフォルダ内 `_extracted` のみ。他ゲームへは一切影響しない。
|
||||
/// - 冪等: 展開済み(plugins.js 存在)なら再展開しない。
|
||||
/// - 既存公開API(GetExtractedDir / ContainsPackageNwMarker / IsExtractionComplete /
|
||||
/// ExeBackupSuffix / ExtractPackageNw)のシグネチャは維持し、後方互換を壊さない。
|
||||
/// </summary>
|
||||
public sealed class TkoolPackagedExtractionService
|
||||
{
|
||||
/// <summary>ゲームフォルダ内の展開先フォルダ名(Asar/SRPG の慣習に合わせる)</summary>
|
||||
public const string ExtractedDirName = NwjsPackageUnpacker.ExtractedDirName;
|
||||
|
||||
/// <summary>exe内コンテナ改変時のバックアップ拡張子</summary>
|
||||
public const string ExeBackupSuffix = ".glbak";
|
||||
|
||||
/// <summary>展開戦略の種別。将来の新形式追加を見据え、列挙型で差し替え可能にする。</summary>
|
||||
public enum UnpackStrategy
|
||||
{
|
||||
/// <summary>組み込みの埋め込みZIPコンテナ検出・展開</summary>
|
||||
BuiltInZip = 0,
|
||||
|
||||
/// <summary>外部アンパッカーへの委譲</summary>
|
||||
ExternalTool = 1,
|
||||
}
|
||||
|
||||
/// <summary>ゲームフォルダ内の展開先ディレクトリ(物理パス)</summary>
|
||||
public static string GetExtractedDir(string gameRootDir)
|
||||
=> NwjsPackageUnpacker.GetExtractedDir(gameRootDir);
|
||||
|
||||
/// <summary>
|
||||
/// exeに package.nw マーカーが含まれるかを限定部分読み(4MB窓×数点)で判定します。
|
||||
/// 全走査は行いません(1.14GB級exe対策)。
|
||||
/// </summary>
|
||||
public static bool ContainsPackageNwMarker(string exePath)
|
||||
{
|
||||
try
|
||||
{
|
||||
const int windowSize = 4 * 1024 * 1024;
|
||||
using var fs = new FileStream(exePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
||||
long size = fs.Length;
|
||||
if (size < 1024)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
double[] fractions = { 0.25, 0.5, 0.75, 0.9 };
|
||||
foreach (var frac in fractions)
|
||||
{
|
||||
long start = (long)(size * frac);
|
||||
int readLen = (int)Math.Min(windowSize, size - start);
|
||||
if (readLen <= 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var buf = new byte[readLen];
|
||||
fs.Seek(start, SeekOrigin.Begin);
|
||||
int read = fs.Read(buf, 0, buf.Length);
|
||||
var text = System.Text.Encoding.ASCII.GetString(buf, 0, read);
|
||||
|
||||
if (text.IndexOf("package.nw", StringComparison.Ordinal) >= 0 ||
|
||||
text.IndexOf("package.json", StringComparison.Ordinal) >= 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// exe内ZIPのローカルヘッダ(PK\x03\x04)offsetを限定ウィンドウで検出します。
|
||||
/// exe先頭の自己展開部を除いた中盤以降のみを対象にし、全走査を避けます。
|
||||
/// </summary>
|
||||
public static long? FindEmbeddedZipOffset(string exePath, long searchStart = 0, int windowBytes = 8 * 1024 * 1024)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var fs = new FileStream(exePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
||||
long size = fs.Length;
|
||||
|
||||
// searchStart が 0 の場合は中盤〜後半(package.nw は後半に置かれる傾向)のみを走査
|
||||
double[] fractions = searchStart > 0 ? new[] { (double)searchStart / Math.Max(1, size) } : new[] { 0.5, 0.65, 0.8, 0.9 };
|
||||
foreach (var frac in fractions)
|
||||
{
|
||||
long start = (long)(size * frac);
|
||||
int readLen = (int)Math.Min(windowBytes, size - start);
|
||||
if (readLen <= 4)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var buf = new byte[readLen];
|
||||
fs.Seek(start, SeekOrigin.Begin);
|
||||
int read = fs.Read(buf, 0, buf.Length);
|
||||
|
||||
for (int i = 0; i < read - 3; i++)
|
||||
{
|
||||
if (buf[i] == 0x50 && buf[i + 1] == 0x4B && buf[i + 2] == 0x03 && buf[i + 3] == 0x04)
|
||||
{
|
||||
return start + i;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 既存 `_extracted` が完全か(plugins.js が存在するか)を判定します。
|
||||
/// </summary>
|
||||
public static bool IsExtractionComplete(string extractedDir, out string? pluginsJsPath)
|
||||
=> NwjsPackageUnpacker.IsExtractionComplete(extractedDir, out pluginsJsPath, out _);
|
||||
|
||||
/// <summary>
|
||||
/// exe内ZIP(package.nw 等)をゲームフォルダ内 `_extracted` へ冪等に全展開し、
|
||||
/// 展開後の js/plugins.js(相対パス)を返します。展開できなければ null。
|
||||
///
|
||||
/// 展開戦略を順に試行します: 組み込みZIP展開 → 外部ツール委譲。
|
||||
/// 後方互換のため同期シグネチャを維持します(外部ツールの自動取得は内部で同期待機)。
|
||||
/// </summary>
|
||||
public string? ExtractPackageNw(string exePath, string gameRoot, out string extractedDir, IProgress<string>? progress = null)
|
||||
{
|
||||
extractedDir = GetExtractedDir(gameRoot);
|
||||
|
||||
try
|
||||
{
|
||||
if (!File.Exists(exePath))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// 冪等: 展開済みならスキップ
|
||||
if (Directory.Exists(extractedDir) && IsExtractionComplete(extractedDir, out _))
|
||||
{
|
||||
progress?.Report("既に展開済みです(_extracted を再利用)。");
|
||||
return NwjsPackageUnpacker.IsExtractionComplete(extractedDir, out _, out var rel) ? rel : "js/plugins.js";
|
||||
}
|
||||
|
||||
// 戦略1: 組み込みZIP展開
|
||||
progress?.Report("展開戦略: 組み込みZIP展開を試行します。");
|
||||
var relBuiltin = NwjsPackageUnpacker.ExtractEmbeddedZip(exePath, extractedDir, progress);
|
||||
if (relBuiltin != null)
|
||||
{
|
||||
return relBuiltin;
|
||||
}
|
||||
|
||||
// 戦略2: 外部ツール委譲
|
||||
progress?.Report("展開戦略: 外部アンパッカーへの委譲を試行します。");
|
||||
var relExternal = ExtractViaExternalTool(exePath, extractedDir, progress);
|
||||
if (relExternal != null)
|
||||
{
|
||||
return relExternal;
|
||||
}
|
||||
|
||||
progress?.Report("展開できませんでした(起動のみ対応)。");
|
||||
return null;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
progress?.Report($"展開処理中にエラーが発生しました: {ex.Message}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 外部アンパッカー(未配置なら自動取得)で exe 内コンテナを `_extracted` へ展開します。
|
||||
/// 失敗(ネット未接続等)時は例外を投げず null を返し、呼び出し側で「起動のみ」へフォールバックさせます。
|
||||
/// </summary>
|
||||
private static string? ExtractViaExternalTool(string exePath, string extractedDir, IProgress<string>? progress)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 外部ツールの可用性確保(初回は自動ダウンロード)。同期待機で取得。
|
||||
var available = NwjsUnpackerToolProvider.IsToolAvailable()
|
||||
|| Task.Run(() => NwjsUnpackerToolProvider.EnsureToolAvailableAsync(progress)).GetAwaiter().GetResult();
|
||||
|
||||
if (!available)
|
||||
{
|
||||
progress?.Report("外部アンパッカーを取得できませんでした(ネット未接続等)。起動のみ対応へフォールバックします。");
|
||||
return null;
|
||||
}
|
||||
|
||||
var tool = NwjsUnpackerToolProvider.GetToolPath();
|
||||
if (tool == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// 展開先を一旦クリーンにする(部分展開の混在を避ける)
|
||||
try
|
||||
{
|
||||
if (Directory.Exists(extractedDir))
|
||||
{
|
||||
Directory.Delete(extractedDir, recursive: true);
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
|
||||
if (!NwjsUnpackerToolProvider.ExtractWithExternalTool(tool, exePath, extractedDir, progress))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// 展開結果から plugins.js を解決
|
||||
var plugins = NwjsPackageUnpacker.ResolvePluginsJs(extractedDir);
|
||||
if (plugins == null)
|
||||
{
|
||||
progress?.Report("展開は完了しましたが、js/plugins.js が見つかりませんでした。");
|
||||
return null;
|
||||
}
|
||||
|
||||
var rel = Path.GetRelativePath(extractedDir, plugins).Replace(Path.DirectorySeparatorChar, '/');
|
||||
progress?.Report($"外部アンパッカーでの展開が完了しました: {rel}");
|
||||
return rel;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
progress?.Report($"外部アンパッカー委譲でエラーが発生しました: {ex.Message}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user