- 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/ の再帰配置に対応
2513 lines
113 KiB
C#
2513 lines
113 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Diagnostics;
|
||
using System.Drawing;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Text.Encodings.Web;
|
||
using System.Text.Json;
|
||
using System.Text.RegularExpressions;
|
||
using System.Threading;
|
||
using System.Threading.Tasks;
|
||
using System.Windows;
|
||
using System.Windows.Interop;
|
||
using System.Windows.Media;
|
||
using System.Windows.Media.Imaging;
|
||
using Microsoft.Win32;
|
||
using GameLauncher.Helpers;
|
||
using GameLauncher.Models;
|
||
using GameLauncher.Engines.Rgss;
|
||
using GameLauncher.Engines.Tkool;
|
||
using GameLauncher.Engines.Tyrano;
|
||
using GameLauncher.Engines.Wolf;
|
||
using GameLauncher.Engines.Unity;
|
||
using GameLauncher.Engines.Nwjs;
|
||
|
||
namespace GameLauncher.Services
|
||
{
|
||
/// <summary>
|
||
/// キャッシュ保存用の統合ゲームデータDTO
|
||
/// </summary>
|
||
public class UnifiedGameCacheEntry
|
||
{
|
||
// 共通
|
||
public int EngineKind { get; set; } // 0: Tkool, 1: Tyrano, 2: Unity
|
||
public string Title { get; set; } = string.Empty;
|
||
public string FolderName { get; set; } = string.Empty;
|
||
public string RootDirectory { get; set; } = string.Empty;
|
||
public string ExecutablePath { get; set; } = string.Empty;
|
||
public DateTime? LastLaunched { get; set; }
|
||
public DateTime? FolderLastModified { get; set; }
|
||
|
||
// ツクール / ティラノ専用
|
||
public string PluginsJsPath { get; set; } = string.Empty;
|
||
public string PluginsDirPath { get; set; } = string.Empty;
|
||
public int TkoolEngineType { get; set; }
|
||
public bool HasCustomApplyMod { get; set; }
|
||
public bool ApplyMod { get; set; } = true;
|
||
public Dictionary<string, bool>? PluginSettings { get; set; }
|
||
public bool IsAsar { get; set; }
|
||
public bool IsAsarExtracted { get; set; }
|
||
public string? AsarPath { get; set; }
|
||
public string? AsarExtractedDirPath { get; set; }
|
||
public string? RelativePluginsJsInAsar { get; set; }
|
||
public bool HasRgssArchive { get; set; }
|
||
public bool IsRgssExtracted { get; set; }
|
||
public string? RgssArchivePath { get; set; }
|
||
public bool HasWolfArchive { get; set; }
|
||
public bool IsWolfExtracted { get; set; }
|
||
public bool WolfProxyDll { get; set; }
|
||
public double WolfSpeedMultiplier { get; set; } = 3.0;
|
||
public bool HasSrpgArchive { get; set; }
|
||
public bool IsSrpgExtracted { get; set; }
|
||
public string? SrpgArchivePath { get; set; }
|
||
public bool SrpgProxyDll { get; set; }
|
||
public double SrpgSpeedMultiplier { get; set; } = 3.0;
|
||
|
||
// 単体exe梱包(FOSSIL / boxed)専用
|
||
public bool IsFossilSingleExe { get; set; }
|
||
public bool IsBoxedExe { get; set; }
|
||
public bool IsStandaloneExeExtracted { get; set; }
|
||
public string? StandaloneExtractedDirPath { get; set; }
|
||
public string? StandaloneBackupPath { get; set; }
|
||
public string? BoxedPayloadEntryPath { get; set; }
|
||
public bool CanExtractViaExternalTool { get; set; }
|
||
|
||
// Unity専用
|
||
public string DataDirectoryPath { get; set; } = string.Empty;
|
||
public string CompanyName { get; set; } = string.Empty;
|
||
public string ProductName { get; set; } = string.Empty;
|
||
public int UnityBackendType { get; set; }
|
||
public string UnityVersion { get; set; } = string.Empty;
|
||
public bool HasSaveData { get; set; }
|
||
}
|
||
|
||
/// <summary>
|
||
/// ツクール・ティラノおよびUnityゲームの自動検出・解析・キャッシュ管理を行う統合スキャンサービス
|
||
/// </summary>
|
||
public class GameScannerService
|
||
{
|
||
/// <summary>検索対象のデフォルトルートディレクトリ一覧</summary>
|
||
public static readonly string[] DefaultTargetDirectories =
|
||
[
|
||
@"C:\Users\uduki\Downloads\Brave\@Game\",
|
||
@"Z:\game\R18同人\"
|
||
];
|
||
|
||
private readonly string _cacheFilePath;
|
||
private readonly AsarArchiveService _asarService = new();
|
||
|
||
static GameScannerService()
|
||
{
|
||
try
|
||
{
|
||
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
|
||
}
|
||
catch { }
|
||
}
|
||
|
||
public GameScannerService()
|
||
{
|
||
var baseDir = AppPathHelper.GetExeDirectory();
|
||
_cacheFilePath = Path.Combine(baseDir, "games_cache.json");
|
||
}
|
||
|
||
/// <summary>
|
||
/// キャッシュから全ゲームを瞬時にロードします。
|
||
/// 初回起動時、もし本アプリのキャッシュが無ければ既存の TkoolLauncher や UnityLauncher のキャッシュから自動インポートします。
|
||
/// </summary>
|
||
public List<GameItemBase> LoadCachedGamesFast()
|
||
{
|
||
var list = new List<GameItemBase>();
|
||
|
||
// 1. 本アプリのキャッシュが存在すればロード
|
||
if (File.Exists(_cacheFilePath))
|
||
{
|
||
try
|
||
{
|
||
string json = File.ReadAllText(_cacheFilePath);
|
||
var entries = JsonSerializer.Deserialize<List<UnifiedGameCacheEntry>>(json);
|
||
if (entries != null)
|
||
{
|
||
foreach (var entry in entries)
|
||
{
|
||
if (entry.EngineKind == (int)GameEngineKind.Unity)
|
||
{
|
||
list.Add(new UnityGameItem
|
||
{
|
||
Title = entry.Title,
|
||
FolderName = entry.FolderName,
|
||
RootDirectory = entry.RootDirectory,
|
||
ExecutablePath = entry.ExecutablePath,
|
||
DataDirectoryPath = entry.DataDirectoryPath,
|
||
CompanyName = entry.CompanyName,
|
||
ProductName = entry.ProductName,
|
||
BackendType = (UnityBackendType)entry.UnityBackendType,
|
||
UnityVersion = entry.UnityVersion,
|
||
LastLaunched = entry.LastLaunched,
|
||
FolderLastModified = entry.FolderLastModified,
|
||
HasSaveData = entry.HasSaveData
|
||
});
|
||
}
|
||
else
|
||
{
|
||
var item = new TkoolGameItem
|
||
{
|
||
Title = entry.Title,
|
||
FolderName = entry.FolderName,
|
||
RootDirectory = entry.RootDirectory,
|
||
ExecutablePath = entry.ExecutablePath,
|
||
PluginsJsPath = entry.PluginsJsPath,
|
||
PluginsDirPath = entry.PluginsDirPath,
|
||
EngineType = (TkoolEngineType)entry.TkoolEngineType,
|
||
HasCustomApplyMod = entry.HasCustomApplyMod,
|
||
ApplyMod = entry.ApplyMod,
|
||
LastLaunched = entry.LastLaunched,
|
||
FolderLastModified = entry.FolderLastModified,
|
||
IsModded = false,
|
||
IsAsar = entry.IsAsar,
|
||
IsAsarExtracted = entry.IsAsarExtracted,
|
||
AsarPath = entry.AsarPath,
|
||
AsarExtractedDirPath = entry.AsarExtractedDirPath,
|
||
RelativePluginsJsInAsar = entry.RelativePluginsJsInAsar,
|
||
HasRgssArchive = entry.HasRgssArchive,
|
||
IsRgssExtracted = entry.IsRgssExtracted,
|
||
RgssArchivePath = entry.RgssArchivePath,
|
||
HasWolfArchive = entry.HasWolfArchive,
|
||
IsWolfExtracted = entry.IsWolfExtracted,
|
||
WolfProxyDll = entry.WolfProxyDll,
|
||
WolfSpeedMultiplier = entry.WolfSpeedMultiplier > 0 ? entry.WolfSpeedMultiplier : 3.0,
|
||
HasSrpgArchive = entry.HasSrpgArchive,
|
||
IsSrpgExtracted = entry.IsSrpgExtracted,
|
||
SrpgArchivePath = entry.SrpgArchivePath,
|
||
SrpgProxyDll = entry.SrpgProxyDll,
|
||
SrpgSpeedMultiplier = entry.SrpgSpeedMultiplier > 0 ? entry.SrpgSpeedMultiplier : 3.0,
|
||
IsFossilSingleExe = entry.IsFossilSingleExe,
|
||
IsBoxedExe = entry.IsBoxedExe,
|
||
IsStandaloneExeExtracted = entry.IsStandaloneExeExtracted,
|
||
StandaloneExtractedDirPath = entry.StandaloneExtractedDirPath,
|
||
StandaloneBackupPath = entry.StandaloneBackupPath,
|
||
BoxedPayloadEntryPath = entry.BoxedPayloadEntryPath
|
||
};
|
||
if (entry.PluginSettings != null)
|
||
{
|
||
item.PluginSettings = new Dictionary<string, bool>(entry.PluginSettings, StringComparer.OrdinalIgnoreCase);
|
||
}
|
||
|
||
// 既存キャッシュの自動修復: ウディタの誤判定を修正
|
||
if (item.EngineType is TkoolEngineType.VX or TkoolEngineType.VXAce && HasWolfGame(item.RootDirectory, out var wolfDir))
|
||
{
|
||
item.EngineType = TkoolEngineType.Wolf;
|
||
item.HasRgssArchive = false;
|
||
item.IsRgssExtracted = false;
|
||
item.RgssArchivePath = null;
|
||
item.PluginsJsPath = string.Empty;
|
||
item.PluginsDirPath = string.Empty;
|
||
item.HasWolfArchive = File.Exists(Path.Combine(wolfDir, "Data.wolf")) ||
|
||
(Directory.Exists(wolfDir) && Directory.GetFiles(wolfDir, "*.wolf", SearchOption.TopDirectoryOnly).Length > 0);
|
||
item.IsWolfExtracted = Directory.Exists(Path.Combine(wolfDir, "Data"));
|
||
item.WolfProxyDll = File.Exists(Path.Combine(wolfDir, WolfModService.ProxyDllName));
|
||
item.IsModded = item.WolfProxyDll;
|
||
}
|
||
|
||
// 既存キャッシュの自動修復: SRPG Studio の誤判定 (VX/VXAce) を修正
|
||
if (item.EngineType is TkoolEngineType.VX or TkoolEngineType.VXAce && HasSrpgStudioGame(item.RootDirectory, out var srpgDir))
|
||
{
|
||
item.EngineType = TkoolEngineType.SrpgStudio;
|
||
item.HasRgssArchive = false;
|
||
item.IsRgssExtracted = false;
|
||
item.RgssArchivePath = null;
|
||
item.PluginsJsPath = string.Empty;
|
||
item.PluginsDirPath = string.Empty;
|
||
item.HasSrpgArchive = File.Exists(Path.Combine(srpgDir, "data.dts"));
|
||
item.IsSrpgExtracted = Directory.Exists(Path.Combine(srpgDir, "data_extracted")) || (Directory.Exists(Path.Combine(srpgDir, "Script")) && Directory.Exists(Path.Combine(srpgDir, "Plugin")));
|
||
item.SrpgArchivePath = item.HasSrpgArchive ? Path.Combine(srpgDir, "data.dts") : null;
|
||
item.SrpgProxyDll = File.Exists(Path.Combine(srpgDir, SrpgModService.ProxyDllName));
|
||
item.IsModded = item.SrpgProxyDll || SrpgModService.IsModScriptApplied(srpgDir);
|
||
}
|
||
|
||
// 既存キャッシュの自動修復: 文字化けタイトルの復元
|
||
if (item.EngineType is TkoolEngineType.VX or TkoolEngineType.VXAce)
|
||
{
|
||
if (item.Title.Contains('\uFFFD') || item.Title.Contains("◆G◆"))
|
||
{
|
||
FixRgssTitleFromIni(item);
|
||
}
|
||
}
|
||
|
||
list.Add(item);
|
||
}
|
||
}
|
||
return list;
|
||
}
|
||
}
|
||
catch { }
|
||
}
|
||
|
||
// 2. 本アプリのキャッシュが無い場合:旧 TkoolLauncher と UnityLauncher のキャッシュを自動インポート
|
||
try
|
||
{
|
||
bool imported = false;
|
||
|
||
// TkoolLauncher キャッシュの読み込み
|
||
var oldTkoolCaches = new[]
|
||
{
|
||
Path.Combine(AppPathHelper.GetExeDirectory(), "..", "TkoolLauncher", "games_cache.json"),
|
||
@"Z:\自作ソフト\TkoolLauncher\games_cache.json",
|
||
@"c:\VS2026\TkoolLauncher\bin\Release\net10.0-windows\win-x64\games_cache.json"
|
||
};
|
||
|
||
foreach (var path in oldTkoolCaches)
|
||
{
|
||
if (File.Exists(path))
|
||
{
|
||
try
|
||
{
|
||
string tJson = File.ReadAllText(path);
|
||
using var doc = JsonDocument.Parse(tJson);
|
||
foreach (var element in doc.RootElement.EnumerateArray())
|
||
{
|
||
var title = element.GetProperty("Title").GetString() ?? "";
|
||
var folderName = element.GetProperty("FolderName").GetString() ?? "";
|
||
var rootDir = element.GetProperty("RootDirectory").GetString() ?? "";
|
||
var exePath = element.GetProperty("ExecutablePath").GetString() ?? "";
|
||
var pluginsJs = element.GetProperty("PluginsJsPath").GetString() ?? "";
|
||
var pluginsDir = element.GetProperty("PluginsDirPath").GetString() ?? "";
|
||
var engType = element.GetProperty("EngineType").GetInt32();
|
||
var applyMod = element.TryGetProperty("ApplyMod", out var am) && am.GetBoolean();
|
||
var hasCustom = element.TryGetProperty("HasCustomApplyMod", out var hc) && hc.GetBoolean();
|
||
DateTime? lastLaunched = element.TryGetProperty("LastLaunched", out var ll) && ll.ValueKind == JsonValueKind.String && DateTime.TryParse(ll.GetString(), out var dtL) ? dtL : null;
|
||
DateTime? folderMod = element.TryGetProperty("FolderLastModified", out var fm) && fm.ValueKind == JsonValueKind.String && DateTime.TryParse(fm.GetString(), out var dtM) ? dtM : null;
|
||
|
||
bool isAsar = element.TryGetProperty("IsAsar", out var ia) && ia.GetBoolean();
|
||
bool isAsarExt = element.TryGetProperty("IsAsarExtracted", out var iae) && iae.GetBoolean();
|
||
string? asarP = element.TryGetProperty("AsarPath", out var ap) ? ap.GetString() : null;
|
||
string? asarExtP = element.TryGetProperty("AsarExtractedDirPath", out var aep) ? aep.GetString() : null;
|
||
string? relJs = element.TryGetProperty("RelativePluginsJsInAsar", out var rj) ? rj.GetString() : null;
|
||
|
||
bool isFossilSingleExe = element.TryGetProperty("IsFossilSingleExe", out var ifs) && ifs.GetBoolean();
|
||
bool isBoxedExe = element.TryGetProperty("IsBoxedExe", out var ibx) && ibx.GetBoolean();
|
||
bool isStandaloneExeExtracted = element.TryGetProperty("IsStandaloneExeExtracted", out var isx) && isx.GetBoolean();
|
||
string? standaloneExtDir = element.TryGetProperty("StandaloneExtractedDirPath", out var sed) ? sed.GetString() : null;
|
||
string? standaloneBackup = element.TryGetProperty("StandaloneBackupPath", out var sbp) ? sbp.GetString() : null;
|
||
string? boxedEntry = element.TryGetProperty("BoxedPayloadEntryPath", out var bep) ? bep.GetString() : null;
|
||
bool canExtractViaExternalTool = element.TryGetProperty("CanExtractViaExternalTool", out var cet) && cet.GetBoolean();
|
||
|
||
var item = new TkoolGameItem
|
||
{
|
||
Title = title,
|
||
FolderName = folderName,
|
||
RootDirectory = rootDir,
|
||
ExecutablePath = exePath,
|
||
PluginsJsPath = pluginsJs,
|
||
PluginsDirPath = pluginsDir,
|
||
EngineType = (TkoolEngineType)engType,
|
||
HasCustomApplyMod = hasCustom,
|
||
ApplyMod = applyMod,
|
||
LastLaunched = lastLaunched,
|
||
FolderLastModified = folderMod,
|
||
IsAsar = isAsar,
|
||
IsAsarExtracted = isAsarExt,
|
||
AsarPath = asarP,
|
||
AsarExtractedDirPath = asarExtP,
|
||
RelativePluginsJsInAsar = relJs,
|
||
IsFossilSingleExe = isFossilSingleExe,
|
||
IsBoxedExe = isBoxedExe,
|
||
IsStandaloneExeExtracted = isStandaloneExeExtracted,
|
||
StandaloneExtractedDirPath = standaloneExtDir,
|
||
StandaloneBackupPath = standaloneBackup,
|
||
BoxedPayloadEntryPath = boxedEntry,
|
||
CanExtractViaExternalTool = canExtractViaExternalTool
|
||
};
|
||
|
||
// 既存キャッシュの自動修復: ウディタの誤判定を修正
|
||
if (item.EngineType is TkoolEngineType.VX or TkoolEngineType.VXAce && HasWolfGame(item.RootDirectory, out _))
|
||
{
|
||
item.EngineType = TkoolEngineType.Wolf;
|
||
item.HasRgssArchive = false;
|
||
item.IsRgssExtracted = false;
|
||
item.RgssArchivePath = null;
|
||
item.PluginsJsPath = string.Empty;
|
||
item.PluginsDirPath = string.Empty;
|
||
}
|
||
|
||
// 既存キャッシュの自動修復: 文字化けタイトルの復元
|
||
if (item.EngineType is TkoolEngineType.VX or TkoolEngineType.VXAce)
|
||
{
|
||
if (item.Title.Contains('\uFFFD') || item.Title.Contains("◆G◆"))
|
||
{
|
||
FixRgssTitleFromIni(item);
|
||
}
|
||
}
|
||
|
||
list.Add(item);
|
||
imported = true;
|
||
}
|
||
break;
|
||
}
|
||
catch { }
|
||
}
|
||
}
|
||
|
||
// UnityLauncher キャッシュの読み込み
|
||
var oldUnityCaches = new[]
|
||
{
|
||
Path.Combine(AppPathHelper.GetExeDirectory(), "..", "UnityLauncher", "games_cache.json"),
|
||
@"Z:\自作ソフト\UnityLauncher\games_cache.json",
|
||
@"c:\VS2026\UnityLauncher\bin\Release\net10.0-windows\win-x64\games_cache.json"
|
||
};
|
||
|
||
foreach (var path in oldUnityCaches)
|
||
{
|
||
if (File.Exists(path))
|
||
{
|
||
try
|
||
{
|
||
string uJson = File.ReadAllText(path);
|
||
using var doc = JsonDocument.Parse(uJson);
|
||
foreach (var element in doc.RootElement.EnumerateArray())
|
||
{
|
||
var title = element.GetProperty("Title").GetString() ?? "";
|
||
var folderName = element.GetProperty("FolderName").GetString() ?? "";
|
||
var rootDir = element.GetProperty("RootDirectory").GetString() ?? "";
|
||
var exePath = element.GetProperty("ExecutablePath").GetString() ?? "";
|
||
var dataDir = element.GetProperty("DataDirectoryPath").GetString() ?? "";
|
||
var comp = element.GetProperty("CompanyName").GetString() ?? "";
|
||
var prod = element.GetProperty("ProductName").GetString() ?? "";
|
||
var backend = element.GetProperty("BackendType").GetInt32();
|
||
var uVer = element.GetProperty("UnityVersion").GetString() ?? "";
|
||
DateTime? lastLaunched = element.TryGetProperty("LastLaunched", out var ll) && ll.ValueKind == JsonValueKind.String && DateTime.TryParse(ll.GetString(), out var dtL) ? dtL : null;
|
||
DateTime? folderMod = element.TryGetProperty("FolderLastModified", out var fm) && fm.ValueKind == JsonValueKind.String && DateTime.TryParse(fm.GetString(), out var dtM) ? dtM : null;
|
||
bool hasSave = element.TryGetProperty("HasSaveData", out var hs) && hs.GetBoolean();
|
||
|
||
var item = new UnityGameItem
|
||
{
|
||
Title = title,
|
||
FolderName = folderName,
|
||
RootDirectory = rootDir,
|
||
ExecutablePath = exePath,
|
||
DataDirectoryPath = dataDir,
|
||
CompanyName = comp,
|
||
ProductName = prod,
|
||
BackendType = (UnityBackendType)backend,
|
||
UnityVersion = uVer,
|
||
LastLaunched = lastLaunched,
|
||
FolderLastModified = folderMod,
|
||
HasSaveData = hasSave
|
||
};
|
||
list.Add(item);
|
||
imported = true;
|
||
}
|
||
break;
|
||
}
|
||
catch { }
|
||
}
|
||
}
|
||
|
||
if (imported)
|
||
{
|
||
SaveCache(list);
|
||
}
|
||
}
|
||
catch { }
|
||
|
||
return list;
|
||
}
|
||
|
||
/// <summary>
|
||
/// ゲーム一覧をローカルキャッシュに保存します(exeと同じフォルダに出力)。
|
||
/// </summary>
|
||
public void SaveCache(IEnumerable<GameItemBase> games)
|
||
{
|
||
try
|
||
{
|
||
var entries = new List<UnifiedGameCacheEntry>();
|
||
|
||
foreach (var g in games)
|
||
{
|
||
if (g is UnityGameItem ug)
|
||
{
|
||
entries.Add(new UnifiedGameCacheEntry
|
||
{
|
||
EngineKind = (int)GameEngineKind.Unity,
|
||
Title = ug.Title,
|
||
FolderName = ug.FolderName,
|
||
RootDirectory = ug.RootDirectory,
|
||
ExecutablePath = ug.ExecutablePath,
|
||
LastLaunched = ug.LastLaunched,
|
||
FolderLastModified = ug.FolderLastModified,
|
||
DataDirectoryPath = ug.DataDirectoryPath,
|
||
CompanyName = ug.CompanyName,
|
||
ProductName = ug.ProductName,
|
||
UnityBackendType = (int)ug.BackendType,
|
||
UnityVersion = ug.UnityVersion,
|
||
HasSaveData = ug.HasSaveData
|
||
});
|
||
}
|
||
else if (g is TkoolGameItem tg)
|
||
{
|
||
entries.Add(new UnifiedGameCacheEntry
|
||
{
|
||
EngineKind = (int)tg.EngineKind,
|
||
Title = tg.Title,
|
||
FolderName = tg.FolderName,
|
||
RootDirectory = tg.RootDirectory,
|
||
ExecutablePath = tg.ExecutablePath,
|
||
LastLaunched = tg.LastLaunched,
|
||
FolderLastModified = tg.FolderLastModified,
|
||
PluginsJsPath = tg.PluginsJsPath,
|
||
PluginsDirPath = tg.PluginsDirPath,
|
||
TkoolEngineType = (int)tg.EngineType,
|
||
HasCustomApplyMod = tg.HasCustomApplyMod,
|
||
ApplyMod = tg.ApplyMod,
|
||
PluginSettings = tg.PluginSettings.Count > 0 ? tg.PluginSettings : null,
|
||
IsAsar = tg.IsAsar,
|
||
IsAsarExtracted = tg.IsAsarExtracted,
|
||
AsarPath = tg.AsarPath,
|
||
AsarExtractedDirPath = tg.AsarExtractedDirPath,
|
||
RelativePluginsJsInAsar = tg.RelativePluginsJsInAsar,
|
||
HasRgssArchive = tg.HasRgssArchive,
|
||
IsRgssExtracted = tg.IsRgssExtracted,
|
||
RgssArchivePath = tg.RgssArchivePath,
|
||
HasWolfArchive = tg.HasWolfArchive,
|
||
IsWolfExtracted = tg.IsWolfExtracted,
|
||
WolfProxyDll = tg.WolfProxyDll,
|
||
WolfSpeedMultiplier = tg.WolfSpeedMultiplier,
|
||
HasSrpgArchive = tg.HasSrpgArchive,
|
||
IsSrpgExtracted = tg.IsSrpgExtracted,
|
||
SrpgArchivePath = tg.SrpgArchivePath,
|
||
SrpgProxyDll = tg.SrpgProxyDll,
|
||
SrpgSpeedMultiplier = tg.SrpgSpeedMultiplier,
|
||
IsFossilSingleExe = tg.IsFossilSingleExe,
|
||
IsBoxedExe = tg.IsBoxedExe,
|
||
IsStandaloneExeExtracted = tg.IsStandaloneExeExtracted,
|
||
StandaloneExtractedDirPath = tg.StandaloneExtractedDirPath,
|
||
StandaloneBackupPath = tg.StandaloneBackupPath,
|
||
BoxedPayloadEntryPath = tg.BoxedPayloadEntryPath,
|
||
CanExtractViaExternalTool = tg.CanExtractViaExternalTool
|
||
});
|
||
}
|
||
}
|
||
|
||
var options = new JsonSerializerOptions
|
||
{
|
||
WriteIndented = true,
|
||
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
|
||
};
|
||
|
||
string json = JsonSerializer.Serialize(entries, options);
|
||
File.WriteAllText(_cacheFilePath, json);
|
||
}
|
||
catch { }
|
||
}
|
||
|
||
/// <summary>
|
||
/// キャッシュから読み込んだゲームのアイコン取得や改造状態の確認、実在検証をバックグラウンドで行います。
|
||
/// </summary>
|
||
public async Task VerifyAndEnrichGamesAsync(
|
||
List<GameItemBase> games,
|
||
Action<GameItemBase> onGameUpdated,
|
||
Action<GameItemBase> onGameRemoved,
|
||
CancellationToken ct = default)
|
||
{
|
||
await Task.Run(() =>
|
||
{
|
||
var copyList = games.ToList();
|
||
bool cacheNeedsSave = false;
|
||
|
||
foreach (var game in copyList)
|
||
{
|
||
if (ct.IsCancellationRequested) break;
|
||
|
||
// 実在検証
|
||
if (!Directory.Exists(game.RootDirectory))
|
||
{
|
||
onGameRemoved(game);
|
||
cacheNeedsSave = true;
|
||
continue;
|
||
}
|
||
|
||
if (game is UnityGameItem ug)
|
||
{
|
||
if (!File.Exists(ug.ExecutablePath))
|
||
{
|
||
onGameRemoved(ug);
|
||
cacheNeedsSave = true;
|
||
continue;
|
||
}
|
||
|
||
// フォルダ更新日時
|
||
if (!ug.FolderLastModified.HasValue)
|
||
{
|
||
try
|
||
{
|
||
ug.FolderLastModified = Directory.GetLastWriteTime(ug.RootDirectory);
|
||
cacheNeedsSave = true;
|
||
}
|
||
catch { }
|
||
}
|
||
|
||
// セーブデータ有無確認
|
||
bool hasSave = CheckHasSaveData(ug);
|
||
if (ug.HasSaveData != hasSave)
|
||
{
|
||
ug.HasSaveData = hasSave;
|
||
cacheNeedsSave = true;
|
||
}
|
||
|
||
// アイコン読み込み
|
||
if (ug.IconImage == null)
|
||
{
|
||
var icon = LoadIcon(ug.ExecutablePath, ug.RootDirectory);
|
||
if (icon != null)
|
||
{
|
||
ug.IconImage = icon;
|
||
}
|
||
}
|
||
|
||
onGameUpdated(ug);
|
||
}
|
||
else if (game is TkoolGameItem tg)
|
||
{
|
||
// 既存キャッシュの自動修復: ウディタの誤判定を修正
|
||
if (tg.EngineType is TkoolEngineType.VX or TkoolEngineType.VXAce && HasWolfGame(tg.RootDirectory, out _))
|
||
{
|
||
tg.EngineType = TkoolEngineType.Wolf;
|
||
tg.HasRgssArchive = false;
|
||
tg.IsRgssExtracted = false;
|
||
tg.RgssArchivePath = null;
|
||
tg.PluginsJsPath = string.Empty;
|
||
tg.PluginsDirPath = string.Empty;
|
||
cacheNeedsSave = true;
|
||
onGameUpdated(tg);
|
||
}
|
||
|
||
// 既存キャッシュの自動修復: 文字化けタイトルの復元
|
||
if (tg.EngineType is TkoolEngineType.VX or TkoolEngineType.VXAce &&
|
||
(tg.Title.Contains('\uFFFD') || tg.Title.Contains("◆G◆")))
|
||
{
|
||
var oldTitle = tg.Title;
|
||
FixRgssTitleFromIni(tg);
|
||
if (tg.Title != oldTitle)
|
||
{
|
||
cacheNeedsSave = true;
|
||
onGameUpdated(tg);
|
||
}
|
||
}
|
||
|
||
// 実在検証
|
||
if (tg.IsWolf)
|
||
{
|
||
if (!File.Exists(tg.ExecutablePath))
|
||
{
|
||
onGameRemoved(tg);
|
||
cacheNeedsSave = true;
|
||
continue;
|
||
}
|
||
|
||
// アーカイブ・解凍状態・プロキシDLLの最新同期
|
||
var wolfDir = tg.RootDirectory;
|
||
bool hasWolfArchive = File.Exists(Path.Combine(wolfDir, "Data.wolf")) ||
|
||
(Directory.Exists(wolfDir) && Directory.GetFiles(wolfDir, "*.wolf", SearchOption.TopDirectoryOnly).Length > 0);
|
||
bool isWolfExtracted = Directory.Exists(Path.Combine(wolfDir, "Data"));
|
||
bool isProxyDll = File.Exists(Path.Combine(wolfDir, WolfModService.ProxyDllName));
|
||
|
||
if (tg.HasWolfArchive != hasWolfArchive || tg.IsWolfExtracted != isWolfExtracted || tg.WolfProxyDll != isProxyDll)
|
||
{
|
||
tg.HasWolfArchive = hasWolfArchive;
|
||
tg.IsWolfExtracted = isWolfExtracted;
|
||
tg.WolfProxyDll = isProxyDll;
|
||
tg.IsModded = isProxyDll;
|
||
cacheNeedsSave = true;
|
||
}
|
||
}
|
||
else if (tg.IsSrpgStudio)
|
||
{
|
||
if (!File.Exists(tg.ExecutablePath))
|
||
{
|
||
onGameRemoved(tg);
|
||
cacheNeedsSave = true;
|
||
continue;
|
||
}
|
||
|
||
// アーカイブ・解凍状態・プロキシDLL・Modスクリプトの最新同期
|
||
var srpgDir = tg.RootDirectory;
|
||
bool hasSrpgArchive = File.Exists(Path.Combine(srpgDir, "data.dts"));
|
||
bool isSrpgExtracted = Directory.Exists(Path.Combine(srpgDir, "data_extracted")) || (Directory.Exists(Path.Combine(srpgDir, "Script")) && Directory.Exists(Path.Combine(srpgDir, "Plugin")));
|
||
bool isProxyDll = File.Exists(Path.Combine(srpgDir, SrpgModService.ProxyDllName));
|
||
bool isModScript = SrpgModService.IsModScriptApplied(srpgDir);
|
||
|
||
if (tg.HasSrpgArchive != hasSrpgArchive || tg.IsSrpgExtracted != isSrpgExtracted || tg.SrpgProxyDll != isProxyDll)
|
||
{
|
||
tg.HasSrpgArchive = hasSrpgArchive;
|
||
tg.IsSrpgExtracted = isSrpgExtracted;
|
||
tg.SrpgProxyDll = isProxyDll;
|
||
tg.IsModded = isProxyDll || isModScript;
|
||
cacheNeedsSave = true;
|
||
}
|
||
}
|
||
else if (tg.IsAsar)
|
||
{
|
||
var asarExists = !string.IsNullOrEmpty(tg.AsarPath) &&
|
||
(File.Exists(tg.AsarPath) || File.Exists(tg.AsarPath + AsarArchiveService.AsarBackupSuffix));
|
||
|
||
if (!asarExists)
|
||
{
|
||
onGameRemoved(tg);
|
||
cacheNeedsSave = true;
|
||
continue;
|
||
}
|
||
|
||
// 解凍状態の整合性確認
|
||
bool dirExtracted = !string.IsNullOrEmpty(tg.AsarExtractedDirPath) && Directory.Exists(tg.AsarExtractedDirPath);
|
||
bool jsExists = !string.IsNullOrEmpty(tg.PluginsJsPath) && File.Exists(tg.PluginsJsPath);
|
||
|
||
if (tg.IsAsarExtracted && (!dirExtracted || !jsExists))
|
||
{
|
||
tg.IsAsarExtracted = false;
|
||
cacheNeedsSave = true;
|
||
onGameUpdated(tg);
|
||
}
|
||
else if (!tg.IsAsarExtracted && dirExtracted && jsExists)
|
||
{
|
||
tg.IsAsarExtracted = true;
|
||
cacheNeedsSave = true;
|
||
onGameUpdated(tg);
|
||
}
|
||
}
|
||
else if (tg.IsRgss)
|
||
{
|
||
var archiveExists = !string.IsNullOrEmpty(tg.RgssArchivePath) && File.Exists(tg.RgssArchivePath);
|
||
var scriptExists = !string.IsNullOrEmpty(tg.PluginsJsPath) && File.Exists(tg.PluginsJsPath);
|
||
|
||
if (!archiveExists && !scriptExists)
|
||
{
|
||
onGameRemoved(tg);
|
||
cacheNeedsSave = true;
|
||
continue;
|
||
}
|
||
|
||
if (tg.IsRgssExtracted != scriptExists)
|
||
{
|
||
tg.IsRgssExtracted = scriptExists;
|
||
cacheNeedsSave = true;
|
||
onGameUpdated(tg);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (!File.Exists(tg.PluginsJsPath))
|
||
{
|
||
onGameRemoved(tg);
|
||
cacheNeedsSave = true;
|
||
continue;
|
||
}
|
||
}
|
||
|
||
// フォルダ更新日時
|
||
if (!tg.FolderLastModified.HasValue)
|
||
{
|
||
try
|
||
{
|
||
tg.FolderLastModified = Directory.GetLastWriteTime(tg.RootDirectory);
|
||
cacheNeedsSave = true;
|
||
onGameUpdated(tg);
|
||
}
|
||
catch { }
|
||
}
|
||
|
||
// 改造状態の確認
|
||
if (tg.IsWolf)
|
||
{
|
||
tg.IsModded = false;
|
||
}
|
||
else if (tg.IsAsar && !tg.IsAsarExtracted)
|
||
{
|
||
tg.IsModded = false;
|
||
}
|
||
else if (tg.IsRgss)
|
||
{
|
||
tg.IsModded = !string.IsNullOrEmpty(tg.PluginsJsPath) && File.Exists(tg.PluginsJsPath) && RgssModService.CheckIsModded(tg.PluginsJsPath);
|
||
}
|
||
else if (File.Exists(tg.PluginsJsPath))
|
||
{
|
||
if (tg.EngineType == TkoolEngineType.Tyrano)
|
||
{
|
||
tg.IsModded = TyranoModService.CheckIsModded(tg.PluginsJsPath);
|
||
}
|
||
else
|
||
{
|
||
tg.IsModded = TkoolModService.CheckIsModded(tg.PluginsJsPath);
|
||
}
|
||
}
|
||
|
||
// アイコン読み込み
|
||
if (tg.IconImage == null)
|
||
{
|
||
var icon = LoadIcon(tg.ExecutablePath, tg.RootDirectory);
|
||
if (icon != null)
|
||
{
|
||
tg.IconImage = icon;
|
||
onGameUpdated(tg);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
if (cacheNeedsSave)
|
||
{
|
||
SaveCache(copyList.Where(g => Directory.Exists(g.RootDirectory)));
|
||
}
|
||
}, ct);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 指定されたフォルダ一覧からツクール・ティラノ・Unityゲームを非同期で走査・検出します。
|
||
/// </summary>
|
||
public async Task ScanDirectoriesAsync(
|
||
IEnumerable<string> targetPaths,
|
||
IProgress<GameItemBase> progress,
|
||
HashSet<string> existingPaths,
|
||
CancellationToken ct = default)
|
||
{
|
||
await Task.Run(() =>
|
||
{
|
||
foreach (var targetPath in targetPaths)
|
||
{
|
||
if (ct.IsCancellationRequested) break;
|
||
if (!Directory.Exists(targetPath)) continue;
|
||
|
||
IEnumerable<string> subDirs;
|
||
try
|
||
{
|
||
subDirs = Directory.EnumerateDirectories(targetPath);
|
||
}
|
||
catch
|
||
{
|
||
continue;
|
||
}
|
||
|
||
foreach (var dir in subDirs)
|
||
{
|
||
if (ct.IsCancellationRequested) break;
|
||
|
||
// 既に検出済みのパスならスキップ
|
||
if (existingPaths.Contains(dir))
|
||
continue;
|
||
|
||
// ディレクトリ直下のゲームを検出
|
||
var game = DetectGameInDirectory(dir);
|
||
if (game != null)
|
||
{
|
||
existingPaths.Add(dir);
|
||
progress.Report(game);
|
||
continue;
|
||
}
|
||
|
||
// 1階層下も探索(アーカイブ解凍等でサブフォルダが入っているケース)
|
||
try
|
||
{
|
||
var childDirs = Directory.EnumerateDirectories(dir);
|
||
foreach (var childDir in childDirs)
|
||
{
|
||
if (ct.IsCancellationRequested) break;
|
||
if (existingPaths.Contains(childDir)) continue;
|
||
|
||
var childGame = DetectGameInDirectory(childDir);
|
||
if (childGame != null)
|
||
{
|
||
existingPaths.Add(childDir);
|
||
progress.Report(childGame);
|
||
}
|
||
}
|
||
}
|
||
catch { }
|
||
}
|
||
}
|
||
}, ct);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 1つのディレクトリ内にツクール・ティラノまたはUnityゲームが存在するか判定し、対応するアイテムを生成します。
|
||
/// </summary>
|
||
public GameItemBase? DetectGameInDirectory(string dirPath)
|
||
{
|
||
try
|
||
{
|
||
if (!Directory.Exists(dirPath))
|
||
return null;
|
||
|
||
// 1. まずツクール/ティラノ判定
|
||
var tkoolGame = DetectTkoolGameInDirectory(dirPath);
|
||
if (tkoolGame != null)
|
||
{
|
||
return tkoolGame;
|
||
}
|
||
|
||
// 2. 次にUnity判定
|
||
var unityGame = DetectUnityGameInDirectory(dirPath);
|
||
if (unityGame != null)
|
||
{
|
||
return unityGame;
|
||
}
|
||
}
|
||
catch { }
|
||
|
||
return null;
|
||
}
|
||
|
||
#region ツクール / ティラノ 検出ロジック
|
||
|
||
public TkoolGameItem? DetectTkoolGameInDirectory(string dirPath)
|
||
{
|
||
try
|
||
{
|
||
var folderName = Path.GetFileName(dirPath.TrimEnd('\\', '/'));
|
||
DateTime? folderModified = null;
|
||
try { folderModified = Directory.GetLastWriteTime(dirPath); } catch { }
|
||
|
||
// 1. 通常の plugins.js 探索
|
||
var pluginsJs = FindPluginsJs(dirPath, maxDepth: 2);
|
||
if (pluginsJs != null)
|
||
{
|
||
var pluginsDir = Path.Combine(Path.GetDirectoryName(pluginsJs)!, "plugins");
|
||
var jsDir = Path.GetDirectoryName(pluginsJs)!;
|
||
|
||
var engineType = DetectEngineType(jsDir);
|
||
var title = ExtractTkoolTitle(dirPath, jsDir);
|
||
var exePath = FindTkoolExecutable(dirPath, jsDir);
|
||
var icon = LoadIcon(exePath, dirPath);
|
||
var isModded = TkoolModService.CheckIsModded(pluginsJs);
|
||
|
||
bool isAsarPkg = AsarArchiveService.FindAsarPackage(dirPath, out var aPath, out bool isExt, out var extDir);
|
||
|
||
return new TkoolGameItem
|
||
{
|
||
Title = string.IsNullOrWhiteSpace(title) ? folderName : title,
|
||
FolderName = folderName,
|
||
RootDirectory = dirPath,
|
||
ExecutablePath = exePath ?? string.Empty,
|
||
PluginsJsPath = pluginsJs,
|
||
PluginsDirPath = pluginsDir,
|
||
EngineType = engineType,
|
||
IsModded = isModded,
|
||
ApplyMod = true,
|
||
IconImage = icon,
|
||
FolderLastModified = folderModified,
|
||
IsAsar = isAsarPkg,
|
||
IsAsarExtracted = isAsarPkg && isExt,
|
||
AsarPath = aPath,
|
||
AsarExtractedDirPath = extDir
|
||
};
|
||
}
|
||
|
||
// 2. boxed(nw.js埋め込み: *boxed*.exe + 埋め込みpackage.nw または on-disk www\)のツクール探索
|
||
var boxedGame = DetectBoxedNwGameInDirectory(dirPath, folderName, folderModified);
|
||
if (boxedGame != null)
|
||
{
|
||
return boxedGame;
|
||
}
|
||
|
||
// 3. FOSSIL単体exe同梱のツクールMZ探索 (js/plugins.jsがexe内に埋め込まれている形態)
|
||
var fossilMzGame = DetectFossilMZGameInDirectory(dirPath, folderName, folderModified);
|
||
if (fossilMzGame != null)
|
||
{
|
||
return fossilMzGame;
|
||
}
|
||
|
||
// 3. 通常のティラノスクリプト探索
|
||
var tyranoIndex = FindTyranoIndexHtml(dirPath);
|
||
if (tyranoIndex != null)
|
||
{
|
||
var exePath = FindTkoolExecutable(dirPath, Path.GetDirectoryName(tyranoIndex)!);
|
||
var icon = LoadIcon(exePath, dirPath);
|
||
var isModded = TyranoModService.CheckIsModded(tyranoIndex);
|
||
bool isAsarPkg = AsarArchiveService.FindAsarPackage(dirPath, out var aPath, out bool isExt, out var extDir);
|
||
|
||
return new TkoolGameItem
|
||
{
|
||
Title = folderName,
|
||
FolderName = folderName,
|
||
RootDirectory = dirPath,
|
||
ExecutablePath = exePath ?? string.Empty,
|
||
PluginsJsPath = tyranoIndex,
|
||
PluginsDirPath = Path.GetDirectoryName(tyranoIndex)!,
|
||
EngineType = TkoolEngineType.Tyrano,
|
||
IsModded = isModded,
|
||
ApplyMod = true,
|
||
IconImage = icon,
|
||
FolderLastModified = folderModified,
|
||
IsAsar = isAsarPkg,
|
||
IsAsarExtracted = isAsarPkg && isExt,
|
||
AsarPath = aPath,
|
||
AsarExtractedDirPath = extDir,
|
||
RelativePluginsJsInAsar = "index.html"
|
||
};
|
||
}
|
||
|
||
// 4. app.asar パッケージであるか判定
|
||
if (AsarArchiveService.FindAsarPackage(dirPath, out var asarPath, out bool isExtracted, out var extractedDirPath))
|
||
{
|
||
var inspect = _asarService.InspectAsar(asarPath!);
|
||
if (inspect.IsValid && (inspect.IsTkoolGame || inspect.IsTyranoGame))
|
||
{
|
||
var relJs = inspect.RelativePluginsJsPath ?? (inspect.IsTyranoGame ? "index.html" : "project/js/plugins.js");
|
||
var plannedJsPath = Path.Combine(extractedDirPath!, relJs.Replace('/', Path.DirectorySeparatorChar));
|
||
var plannedPluginsDir = Path.Combine(Path.GetDirectoryName(plannedJsPath)!, "plugins");
|
||
|
||
var exePath = FindTkoolExecutable(dirPath, extractedDirPath ?? dirPath);
|
||
var icon = LoadIcon(exePath, dirPath);
|
||
|
||
bool isModded = false;
|
||
if (isExtracted && File.Exists(plannedJsPath))
|
||
{
|
||
isModded = inspect.IsTyranoGame
|
||
? TyranoModService.CheckIsModded(plannedJsPath)
|
||
: TkoolModService.CheckIsModded(plannedJsPath);
|
||
}
|
||
|
||
var title = !string.IsNullOrWhiteSpace(inspect.Title) ? inspect.Title : folderName;
|
||
|
||
return new TkoolGameItem
|
||
{
|
||
Title = title,
|
||
FolderName = folderName,
|
||
RootDirectory = dirPath,
|
||
ExecutablePath = exePath ?? string.Empty,
|
||
PluginsJsPath = isExtracted ? plannedJsPath : string.Empty,
|
||
PluginsDirPath = isExtracted ? plannedPluginsDir : string.Empty,
|
||
EngineType = inspect.EngineType,
|
||
IsModded = isModded,
|
||
ApplyMod = true,
|
||
IconImage = icon,
|
||
FolderLastModified = folderModified,
|
||
IsAsar = true,
|
||
IsAsarExtracted = isExtracted,
|
||
AsarPath = asarPath,
|
||
AsarExtractedDirPath = extractedDirPath,
|
||
RelativePluginsJsInAsar = relJs
|
||
};
|
||
}
|
||
}
|
||
|
||
// 5. WOLF RPGエディター(ウディタ)ゲームの検出
|
||
var wolfGame = DetectWolfGameInDirectory(dirPath, folderName, folderModified);
|
||
if (wolfGame != null)
|
||
{
|
||
return wolfGame;
|
||
}
|
||
|
||
// 6. SRPG Studio ゲームの検出
|
||
var srpgGame = DetectSrpgGameInDirectory(dirPath, folderName, folderModified);
|
||
if (srpgGame != null)
|
||
{
|
||
return srpgGame;
|
||
}
|
||
|
||
// 7. ツクールVX / VX Ace (RGSS2 / RGSS3) ゲームの検出
|
||
var rgssGame = DetectRgssGameInDirectory(dirPath, folderName, folderModified);
|
||
if (rgssGame != null)
|
||
{
|
||
return rgssGame;
|
||
}
|
||
}
|
||
catch { }
|
||
|
||
return null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 単体exe梱包されたツクールMZゲームを判定し、TkoolGameItemを生成します。
|
||
/// 対象とするexe形態は次の2種で、いずれも js/plugins.js がディスク上に存在しません。
|
||
/// - FOSSIL形態: エントリHTMLが FOSSILindex.html、本体は Game.exe(data/System.json はディスク上)
|
||
/// - boxed形態: ゲーム本体を exe に内包(例: Game_boxed.exe)。ディスク上にゲーム実体が無い
|
||
/// 誤検出を避けるため、以下の条件が全て揃った場合のみ検出します。
|
||
/// - 候補exe(Game.exe / *boxed*.exe / *.exe)が存在する
|
||
/// - js/plugins.js および www/js/plugins.js がディスク上に存在しない
|
||
/// - FOSSILindex.html が存在する、又は data/System.json が存在する、又は boxed 命名のexeである
|
||
/// 本形態はゲーム本体をexeへ埋め込むため、他のツクールゲームには一切影響しません。
|
||
/// </summary>
|
||
public TkoolGameItem? DetectFossilMZGameInDirectory(string dirPath, string folderName, DateTime? folderModified)
|
||
{
|
||
try
|
||
{
|
||
// 0. ディスク上に plugins.js が存在する場合は通常のMV/MZ検出に任せる
|
||
if (File.Exists(Path.Combine(dirPath, "js", "plugins.js")) ||
|
||
File.Exists(Path.Combine(dirPath, "www", "js", "plugins.js")))
|
||
{
|
||
return null;
|
||
}
|
||
|
||
// 1. 候補exeの解決(Game.exe を最優先、次に *boxed*.exe、最後に任意のexe)
|
||
var exePath = ResolveStandaloneEntryExecutable(dirPath);
|
||
if (exePath == null)
|
||
{
|
||
return null;
|
||
}
|
||
|
||
// 2. 判定マーカーの確認
|
||
bool hasFossilHtml = File.Exists(Path.Combine(dirPath, "FOSSILindex.html"));
|
||
var systemJson = Path.Combine(dirPath, "data", "System.json");
|
||
bool hasSystemJson = File.Exists(systemJson);
|
||
bool isBoxedExe = Path.GetFileName(exePath).IndexOf("boxed", StringComparison.OrdinalIgnoreCase) >= 0;
|
||
|
||
if (!hasFossilHtml && !hasSystemJson && !isBoxedExe)
|
||
{
|
||
return null;
|
||
}
|
||
|
||
var icon = LoadIcon(exePath, dirPath);
|
||
string? title = null;
|
||
string? pluginsJsInContainer = null;
|
||
string? embeddedPluginsPath = null;
|
||
|
||
// 3. boxed 命名exeは内包コンテナ(package.nw)からplugins.js位置を推定
|
||
if (isBoxedExe)
|
||
{
|
||
TryProbeBoxedEmbeddedPayload(exePath, out pluginsJsInContainer, out embeddedPluginsPath);
|
||
}
|
||
|
||
// 4. タイトル解決: data/System.json の gameTitle を優先、無ければフォルダ名
|
||
if (hasSystemJson)
|
||
{
|
||
try
|
||
{
|
||
var json = File.ReadAllText(systemJson);
|
||
var match = Regex.Match(json, @"""gameTitle""\s*:\s*""([^""]*)\""");
|
||
if (match.Success && !string.IsNullOrWhiteSpace(match.Groups[1].Value))
|
||
{
|
||
title = Regex.Unescape(match.Groups[1].Value);
|
||
}
|
||
}
|
||
catch { }
|
||
}
|
||
|
||
// 5. boxed命名exeで data/System.json がディスク上に無い場合は内包コンテナを注入対象とする
|
||
bool isBoxed = isBoxedExe && !hasSystemJson;
|
||
|
||
return new TkoolGameItem
|
||
{
|
||
Title = string.IsNullOrWhiteSpace(title) ? folderName : title!,
|
||
FolderName = folderName,
|
||
RootDirectory = dirPath,
|
||
ExecutablePath = exePath,
|
||
PluginsJsPath = embeddedPluginsPath ?? string.Empty,
|
||
PluginsDirPath = string.Empty,
|
||
EngineType = TkoolEngineType.MZ,
|
||
IsModded = false,
|
||
// ディスク上にJS実体が無い単体exe形態は、そのままではプラグイン注入できないため初期は改造OFF
|
||
ApplyMod = false,
|
||
IconImage = icon,
|
||
FolderLastModified = folderModified,
|
||
IsAsar = false,
|
||
// 形態フラグ(他ゲームへ影響しないための識別子)
|
||
IsFossilSingleExe = hasFossilHtml || !isBoxed,
|
||
IsBoxedExe = isBoxed,
|
||
BoxedPayloadEntryPath = pluginsJsInContainer
|
||
};
|
||
}
|
||
catch { }
|
||
|
||
return null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 単体exe形態の候補となる実行ファイルを解決します。
|
||
/// 優先順: Game.exe → *boxed*.exe → それ以外の *.exe(unins* を除く)。
|
||
/// </summary>
|
||
private static string? ResolveStandaloneEntryExecutable(string dirPath)
|
||
{
|
||
try
|
||
{
|
||
var gameExe = Path.Combine(dirPath, "Game.exe");
|
||
if (File.Exists(gameExe))
|
||
{
|
||
return gameExe;
|
||
}
|
||
|
||
var exes = Directory.GetFiles(dirPath, "*.exe", SearchOption.TopDirectoryOnly);
|
||
if (exes.Length == 0)
|
||
{
|
||
return null;
|
||
}
|
||
|
||
// *boxed*.exe を優先
|
||
foreach (var exe in exes)
|
||
{
|
||
var name = Path.GetFileName(exe);
|
||
if (name.IndexOf("boxed", StringComparison.OrdinalIgnoreCase) >= 0)
|
||
{
|
||
return exe;
|
||
}
|
||
}
|
||
|
||
// unins* を除外した最初のexe
|
||
foreach (var exe in exes)
|
||
{
|
||
var name = Path.GetFileName(exe);
|
||
if (name.StartsWith("unins", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
continue;
|
||
}
|
||
return exe;
|
||
}
|
||
}
|
||
catch { }
|
||
|
||
return null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// boxed命名exeの内包コンテナ(package.nw相当)を限定部分読みで探索し、
|
||
/// タイトル情報(package.json)と js/plugins.js 相当のエントリ有無を推定します。
|
||
/// 全走査は行いません(1.14GB級exeを対象に含むため)。
|
||
/// </summary>
|
||
private static void TryProbeBoxedEmbeddedPayload(string exePath, out string? packageJsonEntryName, out string? embeddedPluginsJsPath)
|
||
{
|
||
packageJsonEntryName = null;
|
||
embeddedPluginsJsPath = null;
|
||
|
||
try
|
||
{
|
||
const int windowSize = 4 * 1024 * 1024; // 4MB限定ウィンドウ
|
||
using var fs = new FileStream(exePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
||
long size = fs.Length;
|
||
|
||
// package.nw マーカーは exe の中間域に置かれることが多いため、中央付近から数点だけ走査する
|
||
long[] probes = new[]
|
||
{
|
||
Math.Max(0, size / 2 - windowSize / 2),
|
||
Math.Max(0, size / 4),
|
||
Math.Max(0, size * 3 / 4),
|
||
};
|
||
|
||
foreach (var start in probes)
|
||
{
|
||
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);
|
||
|
||
// ASCII/UTF8 の "package.json" を探す
|
||
var text = System.Text.Encoding.ASCII.GetString(buf, 0, read);
|
||
int idx = text.IndexOf("package.json", StringComparison.Ordinal);
|
||
if (idx >= 0)
|
||
{
|
||
packageJsonEntryName = "package.json";
|
||
// 同一ウィンドウ内に plugins.js があれば注入対象として記録
|
||
if (text.IndexOf("plugins.js", StringComparison.Ordinal) >= 0)
|
||
{
|
||
embeddedPluginsJsPath = "js/plugins.js";
|
||
}
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
catch { }
|
||
}
|
||
|
||
/// <summary>
|
||
/// nw.js埋め込み(*boxed*.exe + 埋め込みpackage.nw または on-disk www\)のツクールMZゲームを検出します。
|
||
/// 全走査は行わず、exe内のpackage.nwマーカー周辺の限定読みで埋め込みコンテナ有無を判定します。
|
||
/// boxed命名exeが存在する場合のみ発火するため、他のツクールゲームには一切影響しません。
|
||
/// </summary>
|
||
public TkoolGameItem? DetectBoxedNwGameInDirectory(string dirPath, string folderName, DateTime? folderModified)
|
||
{
|
||
try
|
||
{
|
||
// (a) ディスク上の www\js\plugins.js を優先(通常検出で拾えない露出パターンを補完)
|
||
var onDiskPlugins = FindPluginsJsUnderWww(dirPath);
|
||
|
||
// (b) *boxed*.exe の存在確認(Game.exe固定を一般化)
|
||
var boxedExe = Directory.EnumerateFiles(dirPath, "*.exe", SearchOption.TopDirectoryOnly)
|
||
.FirstOrDefault(f => Path.GetFileName(f).Contains("boxed", StringComparison.OrdinalIgnoreCase));
|
||
|
||
// boxed命名exeが無ければここでは扱わない(厳密に追加的)
|
||
if (boxedExe == null)
|
||
{
|
||
return null;
|
||
}
|
||
|
||
// (c) 埋め込みpackage.nwマーカーの有無(限定部分読み)
|
||
bool hasEmbeddedPkg = ContainsPackageNwMarker(boxedExe);
|
||
|
||
// (d) boxed命名exeが存在すれば、マーカー未確定でも外部アンパッカーでの展開を試行できるため一覧に表示する。
|
||
// (マーカーは中盤の限られた窓でしか確認しないため、非標準コンテナでは拾えない場合がある)
|
||
// 展開可否は実行時(ExtractPackageNw)に確定し、不可なら「起動のみ」にフォールバックする。
|
||
bool canTryExternalTool = true;
|
||
if (!hasEmbeddedPkg && onDiskPlugins == null && !canTryExternalTool)
|
||
{
|
||
return null;
|
||
}
|
||
|
||
var dataDir = Path.Combine(dirPath, "data");
|
||
var engine = DetectEngineTypeByDataJson(dirPath);
|
||
var title = ExtractTkoolTitle(dirPath, dataDir);
|
||
var icon = LoadIcon(boxedExe, dirPath);
|
||
|
||
return new TkoolGameItem
|
||
{
|
||
Title = string.IsNullOrWhiteSpace(title) ? folderName : title,
|
||
FolderName = folderName,
|
||
RootDirectory = dirPath,
|
||
ExecutablePath = boxedExe,
|
||
PluginsJsPath = onDiskPlugins ?? string.Empty,
|
||
PluginsDirPath = onDiskPlugins != null ? Path.Combine(Path.GetDirectoryName(onDiskPlugins)!, "plugins") : string.Empty,
|
||
EngineType = engine,
|
||
IsModded = false,
|
||
// on-disk露出時のみその場で注入可。埋め込みのみの場合はAI展開後に注入可となる
|
||
ApplyMod = onDiskPlugins != null,
|
||
IconImage = icon,
|
||
FolderLastModified = folderModified,
|
||
IsAsar = false,
|
||
IsFossilSingleExe = false,
|
||
IsBoxedExe = true,
|
||
BoxedPayloadEntryPath = hasEmbeddedPkg ? "js/plugins.js" : null,
|
||
// boxed命名exeが存在するため、外部アンパッカーでの展開を試行できる可能性がある
|
||
CanExtractViaExternalTool = canTryExternalTool
|
||
};
|
||
}
|
||
catch { }
|
||
|
||
return null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// ディスク上に露出した www\js\plugins.js(または www\www\js\plugins.js)を探索します。
|
||
/// 通常の FindPluginsJs は深さ2で js\・www\js\ を見ますが、boxed形態の
|
||
/// www 直下露出を取りこぼさないための補完です。
|
||
/// </summary>
|
||
private static string? FindPluginsJsUnderWww(string dirPath)
|
||
{
|
||
try
|
||
{
|
||
var candidates = new[]
|
||
{
|
||
Path.Combine(dirPath, "www", "js", "plugins.js"),
|
||
Path.Combine(dirPath, "www", "www", "js", "plugins.js"),
|
||
};
|
||
foreach (var c in candidates)
|
||
{
|
||
if (File.Exists(c))
|
||
{
|
||
return c;
|
||
}
|
||
}
|
||
}
|
||
catch { }
|
||
|
||
return null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// exe内に nw.js の埋め込みコンテナ(package.nw)マーカーが存在するかを
|
||
/// 限定部分読み(4MB窓×数点)で判定します。全走査は行いません(1.14GB級exe対策)。
|
||
/// </summary>
|
||
private 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>
|
||
/// data\System.json の有無と内容からエンジン種別(MZ/MV)を判定します。
|
||
/// jsディレクトリが無い単体exe梱包形態でも動作します。
|
||
/// </summary>
|
||
private static TkoolEngineType DetectEngineTypeByDataJson(string dirPath)
|
||
{
|
||
try
|
||
{
|
||
var systemJson = Path.Combine(dirPath, "data", "System.json");
|
||
if (File.Exists(systemJson))
|
||
{
|
||
var json = File.ReadAllText(systemJson);
|
||
// MZ固有のキー(例: "hasEncryptedImages" は両者にあるが、"optDisplayTp"等はMZのみ)
|
||
if (json.Contains("\"optDisplayTp\"") ||
|
||
json.Contains("\"optExtraExp\"") ||
|
||
json.Contains("\"optAntiLag\""))
|
||
{
|
||
return TkoolEngineType.MZ;
|
||
}
|
||
return TkoolEngineType.MZ;
|
||
}
|
||
}
|
||
catch { }
|
||
|
||
return TkoolEngineType.MZ;
|
||
}
|
||
|
||
#region WOLF RPGエディター(ウディタ)検出ロジック
|
||
|
||
/// <summary>
|
||
/// 指定されたディレクトリがウディタのゲームであるかを判定し、TkoolGameItemを生成します。
|
||
/// </summary>
|
||
public TkoolGameItem? DetectWolfGameInDirectory(string dirPath, string folderName, DateTime? folderModified)
|
||
{
|
||
try
|
||
{
|
||
if (!HasWolfGame(dirPath, out var targetDir))
|
||
{
|
||
return null;
|
||
}
|
||
|
||
// 実行ファイルの探索 (Game.exe を最優先)
|
||
string? exePath = null;
|
||
var directExe = Path.Combine(targetDir, "Game.exe");
|
||
if (File.Exists(directExe))
|
||
{
|
||
exePath = directExe;
|
||
}
|
||
else
|
||
{
|
||
var rootDirectExe = Path.Combine(dirPath, "Game.exe");
|
||
if (File.Exists(rootDirectExe))
|
||
{
|
||
exePath = rootDirectExe;
|
||
}
|
||
else
|
||
{
|
||
var exes = Directory.GetFiles(targetDir, "*.exe", SearchOption.TopDirectoryOnly);
|
||
foreach (var exe in exes)
|
||
{
|
||
var name = Path.GetFileName(exe);
|
||
if (!name.Contains("unins", StringComparison.OrdinalIgnoreCase) &&
|
||
!name.Contains("config", StringComparison.OrdinalIgnoreCase) &&
|
||
!name.Contains("setup", StringComparison.OrdinalIgnoreCase) &&
|
||
!name.Contains("patch", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
exePath = exe;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
if (exePath == null)
|
||
{
|
||
return null;
|
||
}
|
||
|
||
var icon = LoadIcon(exePath, dirPath);
|
||
|
||
// タイトル取得 (フォルダ名をデフォルトとし、Game.iniにTitle指定があれば優先)
|
||
string title = folderName;
|
||
var ini = Path.Combine(targetDir, "Game.ini");
|
||
if (File.Exists(ini))
|
||
{
|
||
var lines = ReadIniLinesSafe(ini);
|
||
foreach (var line in lines)
|
||
{
|
||
var trim = line.Trim();
|
||
if (trim.StartsWith("Title=", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
var val = trim.Substring(6).Trim();
|
||
if (!string.IsNullOrWhiteSpace(val))
|
||
{
|
||
title = val;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// アーカイブおよび解凍状態、プロキシDLL配置の確認
|
||
bool hasWolfArchive = File.Exists(Path.Combine(targetDir, "Data.wolf")) ||
|
||
(Directory.Exists(targetDir) && Directory.GetFiles(targetDir, "*.wolf", SearchOption.TopDirectoryOnly).Length > 0);
|
||
bool isWolfExtracted = Directory.Exists(Path.Combine(targetDir, "Data"));
|
||
bool isProxyDll = File.Exists(Path.Combine(targetDir, WolfModService.ProxyDllName));
|
||
|
||
return new TkoolGameItem
|
||
{
|
||
Title = title,
|
||
FolderName = folderName,
|
||
RootDirectory = dirPath,
|
||
ExecutablePath = exePath,
|
||
PluginsJsPath = string.Empty,
|
||
PluginsDirPath = string.Empty,
|
||
EngineType = TkoolEngineType.Wolf,
|
||
IsModded = isProxyDll,
|
||
ApplyMod = false,
|
||
WolfProxyDll = isProxyDll,
|
||
HasWolfArchive = hasWolfArchive,
|
||
IsWolfExtracted = isWolfExtracted,
|
||
IconImage = icon,
|
||
FolderLastModified = folderModified,
|
||
HasRgssArchive = false,
|
||
IsRgssExtracted = false,
|
||
RgssArchivePath = null,
|
||
IsAsar = false
|
||
};
|
||
}
|
||
catch { }
|
||
|
||
return null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 指定されたディレクトリまたはその直下にウディタのファイルが存在するかチェックします。
|
||
/// </summary>
|
||
public static bool HasWolfGame(string dir, out string wolfDir)
|
||
{
|
||
wolfDir = dir;
|
||
if (IsWolfDirectory(dir)) return true;
|
||
try
|
||
{
|
||
if (Directory.Exists(dir))
|
||
{
|
||
foreach (var sub in Directory.GetDirectories(dir))
|
||
{
|
||
if (IsWolfDirectory(sub))
|
||
{
|
||
wolfDir = sub;
|
||
return true;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
catch { }
|
||
return false;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 単一のディレクトリ内にウディタの特徴(*.wolf、ウディタ用Game.ini、Game.dat等)が存在するか判定します。
|
||
/// </summary>
|
||
public static bool IsWolfDirectory(string dir)
|
||
{
|
||
try
|
||
{
|
||
if (!Directory.Exists(dir)) return false;
|
||
|
||
// 1. *.wolf ファイルの存在 (Data.wolf, Data\*.wolf 等)
|
||
if (Directory.GetFiles(dir, "*.wolf", SearchOption.TopDirectoryOnly).Length > 0)
|
||
return true;
|
||
|
||
var dataDir = Path.Combine(dir, "Data");
|
||
if (Directory.Exists(dataDir))
|
||
{
|
||
if (Directory.GetFiles(dataDir, "*.wolf", SearchOption.TopDirectoryOnly).Length > 0)
|
||
return true;
|
||
|
||
if (Directory.Exists(Path.Combine(dataDir, "BasicData")) || Directory.Exists(Path.Combine(dataDir, "System")))
|
||
return true;
|
||
}
|
||
|
||
// 2. Game.dat または GuruguruSMF4.dll
|
||
if (File.Exists(Path.Combine(dir, "Game.dat")) || File.Exists(Path.Combine(dir, "GuruguruSMF4.dll")))
|
||
return true;
|
||
|
||
// 3. Game.ini のウディタ固有設定 ([DEFAULT], SoftModeFlag, SEandBGM)
|
||
var ini = Path.Combine(dir, "Game.ini");
|
||
if (File.Exists(ini))
|
||
{
|
||
var lines = ReadIniLinesSafe(ini);
|
||
foreach (var line in lines)
|
||
{
|
||
var trim = line.Trim();
|
||
if (trim.StartsWith("SoftModeFlag=", StringComparison.OrdinalIgnoreCase) ||
|
||
trim.StartsWith("SEandBGM=", StringComparison.OrdinalIgnoreCase) ||
|
||
trim.Equals("[DEFAULT]", StringComparison.OrdinalIgnoreCase) ||
|
||
trim.StartsWith("WindowModeFlag=", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
return true;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
catch { }
|
||
|
||
return false;
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region SRPG Studio 検出ロジック
|
||
|
||
/// <summary>
|
||
/// 指定されたディレクトリがSRPG Studioのゲームであるかを判定し、TkoolGameItemを生成します。
|
||
/// </summary>
|
||
public TkoolGameItem? DetectSrpgGameInDirectory(string dirPath, string folderName, DateTime? folderModified)
|
||
{
|
||
try
|
||
{
|
||
if (!HasSrpgStudioGame(dirPath, out var targetDir))
|
||
{
|
||
return null;
|
||
}
|
||
|
||
// 実行ファイルの探索 (Game.exe, game.exe を最優先)
|
||
string? exePath = null;
|
||
var directExe = Path.Combine(targetDir, "Game.exe");
|
||
if (File.Exists(directExe))
|
||
{
|
||
exePath = directExe;
|
||
}
|
||
else
|
||
{
|
||
directExe = Path.Combine(targetDir, "game.exe");
|
||
if (File.Exists(directExe))
|
||
{
|
||
exePath = directExe;
|
||
}
|
||
else
|
||
{
|
||
var rootDirectExe = Path.Combine(dirPath, "Game.exe");
|
||
if (File.Exists(rootDirectExe))
|
||
{
|
||
exePath = rootDirectExe;
|
||
}
|
||
else
|
||
{
|
||
var exes = Directory.GetFiles(targetDir, "*.exe", SearchOption.TopDirectoryOnly);
|
||
foreach (var exe in exes)
|
||
{
|
||
var name = Path.GetFileName(exe);
|
||
if (!name.Contains("unins", StringComparison.OrdinalIgnoreCase) &&
|
||
!name.Contains("config", StringComparison.OrdinalIgnoreCase) &&
|
||
!name.Contains("setup", StringComparison.OrdinalIgnoreCase) &&
|
||
!name.Contains("patch", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
exePath = exe;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
if (exePath == null)
|
||
{
|
||
return null;
|
||
}
|
||
|
||
var icon = LoadIcon(exePath, dirPath);
|
||
|
||
// タイトル取得 (フォルダ名をデフォルトとし、Game.iniにTitle指定があれば優先)
|
||
string title = folderName;
|
||
var ini = Path.Combine(targetDir, "Game.ini");
|
||
if (!File.Exists(ini)) ini = Path.Combine(targetDir, "game.ini");
|
||
if (File.Exists(ini))
|
||
{
|
||
var lines = ReadIniLinesSafe(ini);
|
||
foreach (var line in lines)
|
||
{
|
||
var trim = line.Trim();
|
||
if (trim.StartsWith("Title=", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
var val = trim.Substring(6).Trim();
|
||
if (!string.IsNullOrWhiteSpace(val))
|
||
{
|
||
title = val;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// アーカイブおよび解凍状態、プロキシDLL配置、Modスクリプトの確認
|
||
bool hasSrpgArchive = File.Exists(Path.Combine(targetDir, SrpgArchiveService.ArchiveFileName));
|
||
bool isSrpgExtracted = Directory.Exists(Path.Combine(targetDir, SrpgArchiveService.ExtractedDirName)) ||
|
||
(Directory.Exists(Path.Combine(targetDir, "Script")) && Directory.Exists(Path.Combine(targetDir, "Plugin")));
|
||
bool isProxyDll = File.Exists(Path.Combine(targetDir, SrpgModService.ProxyDllName));
|
||
bool isModScript = SrpgModService.IsModScriptApplied(targetDir);
|
||
|
||
return new TkoolGameItem
|
||
{
|
||
Title = title,
|
||
FolderName = folderName,
|
||
RootDirectory = dirPath,
|
||
ExecutablePath = exePath,
|
||
PluginsJsPath = string.Empty,
|
||
PluginsDirPath = string.Empty,
|
||
EngineType = TkoolEngineType.SrpgStudio,
|
||
IsModded = isProxyDll || isModScript,
|
||
ApplyMod = false,
|
||
SrpgProxyDll = isProxyDll,
|
||
HasSrpgArchive = hasSrpgArchive,
|
||
IsSrpgExtracted = isSrpgExtracted,
|
||
SrpgArchivePath = hasSrpgArchive ? Path.Combine(targetDir, SrpgArchiveService.ArchiveFileName) : null,
|
||
IconImage = icon,
|
||
FolderLastModified = folderModified,
|
||
HasRgssArchive = false,
|
||
IsRgssExtracted = false,
|
||
RgssArchivePath = null,
|
||
IsAsar = false,
|
||
HasWolfArchive = false,
|
||
IsWolfExtracted = false
|
||
};
|
||
}
|
||
catch { }
|
||
|
||
return null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 指定されたディレクトリまたはその直下にSRPG Studioのファイルが存在するかチェックします。
|
||
/// </summary>
|
||
public static bool HasSrpgStudioGame(string dir, out string srpgDir)
|
||
{
|
||
srpgDir = dir;
|
||
if (IsSrpgStudioDirectory(dir)) return true;
|
||
try
|
||
{
|
||
if (Directory.Exists(dir))
|
||
{
|
||
foreach (var sub in Directory.GetDirectories(dir))
|
||
{
|
||
if (IsSrpgStudioDirectory(sub))
|
||
{
|
||
srpgDir = sub;
|
||
return true;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
catch { }
|
||
return false;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 単一のディレクトリ内にSRPG Studioの特徴(data.dts、runtime.rts、固有設定のgame.ini等)が存在するか判定します。
|
||
/// </summary>
|
||
public static bool IsSrpgStudioDirectory(string dir)
|
||
{
|
||
try
|
||
{
|
||
if (!Directory.Exists(dir)) return false;
|
||
|
||
// 1. data.dts または runtime.rts または environment.evs の存在
|
||
if (File.Exists(Path.Combine(dir, "data.dts")) ||
|
||
File.Exists(Path.Combine(dir, "runtime.rts")) ||
|
||
File.Exists(Path.Combine(dir, "environment.evs")))
|
||
{
|
||
return true;
|
||
}
|
||
|
||
// 2. 解凍済みフォルダ (project.srpgs または project.dat) の存在
|
||
if (File.Exists(Path.Combine(dir, "project.srpgs")) ||
|
||
File.Exists(Path.Combine(dir, "project.dat")) ||
|
||
File.Exists(Path.Combine(dir, "data_extracted", "project.srpgs")))
|
||
{
|
||
return true;
|
||
}
|
||
|
||
// 3. game.ini / Game.ini のSRPG Studio固有設定
|
||
var ini = Path.Combine(dir, "game.ini");
|
||
if (!File.Exists(ini)) ini = Path.Combine(dir, "Game.ini");
|
||
if (File.Exists(ini))
|
||
{
|
||
var lines = ReadIniLinesSafe(ini);
|
||
foreach (var line in lines)
|
||
{
|
||
var trim = line.Trim();
|
||
if (trim.StartsWith("HighPerf=", StringComparison.OrdinalIgnoreCase) ||
|
||
trim.StartsWith("HardwareAcceleration=", StringComparison.OrdinalIgnoreCase) ||
|
||
trim.StartsWith("GamePadDInput=", StringComparison.OrdinalIgnoreCase) ||
|
||
trim.StartsWith("NonActiveMusicPlay=", StringComparison.OrdinalIgnoreCase) ||
|
||
trim.StartsWith("ScriptErrorTerminate=", StringComparison.OrdinalIgnoreCase) ||
|
||
trim.Equals("[keyboard]", StringComparison.OrdinalIgnoreCase) ||
|
||
trim.Equals("[gamepad]", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
return true;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
catch { }
|
||
|
||
return false;
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region エンコーディング安全INI読み込み & タイトル修復
|
||
|
||
/// <summary>
|
||
/// BOMの有無やShift_JIS (CP932) を自動判定して安全にINIファイルを行単位で読み込みます。
|
||
/// 実装は Engines/Rgss/Games/IiniTitleRepair に分離済み(RGSS個別対応)。
|
||
/// </summary>
|
||
public static string[] ReadIniLinesSafe(string filePath)
|
||
=> GameLauncher.Engines.Rgss.Games.IiniTitleRepair.GameIniTitleRepair.ReadIniLinesSafe(filePath);
|
||
|
||
/// <summary>
|
||
/// キャッシュ内に文字化けして保存されたRGSSタイトルの修復を行います。
|
||
/// 実装は Engines/Rgss/Games/IiniTitleRepair に分離済み(RGSS個別対応)。
|
||
/// </summary>
|
||
public static void FixRgssTitleFromIni(TkoolGameItem tg)
|
||
=> GameLauncher.Engines.Rgss.Games.IiniTitleRepair.GameIniTitleRepair.Fix(tg);
|
||
|
||
#endregion
|
||
|
||
private TkoolGameItem? DetectRgssGameInDirectory(string dirPath, string folderName, DateTime? folderModified)
|
||
{
|
||
try
|
||
{
|
||
// 1. ウディタおよび SRPG Studio の場合は絶対に RGSS にしない
|
||
if (IsWolfDirectory(dirPath) || IsSrpgStudioDirectory(dirPath))
|
||
{
|
||
return null;
|
||
}
|
||
|
||
string? gameIniPath = null;
|
||
string searchDir = dirPath;
|
||
|
||
var directIni = Path.Combine(dirPath, "Game.ini");
|
||
if (File.Exists(directIni))
|
||
{
|
||
gameIniPath = directIni;
|
||
}
|
||
else
|
||
{
|
||
try
|
||
{
|
||
foreach (var sub in Directory.GetDirectories(dirPath))
|
||
{
|
||
if (IsWolfDirectory(sub) || IsSrpgStudioDirectory(sub)) continue;
|
||
var subIni = Path.Combine(sub, "Game.ini");
|
||
if (File.Exists(subIni))
|
||
{
|
||
gameIniPath = subIni;
|
||
searchDir = sub;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
catch { }
|
||
}
|
||
|
||
// 2. アーカイブおよびスクリプト実ファイルの探索
|
||
string? archivePath = null;
|
||
string? scriptsPath = null;
|
||
TkoolEngineType engineType = TkoolEngineType.Unknown;
|
||
string? title = null;
|
||
bool isRgssIni = false;
|
||
|
||
if (gameIniPath != null)
|
||
{
|
||
try
|
||
{
|
||
var lines = ReadIniLinesSafe(gameIniPath);
|
||
foreach (var line in lines)
|
||
{
|
||
var trim = line.Trim();
|
||
if (trim.Equals("[Game]", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
isRgssIni = true;
|
||
}
|
||
else if (trim.StartsWith("Title=", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
var val = trim.Substring(6).Trim();
|
||
if (!string.IsNullOrEmpty(val)) title = val;
|
||
}
|
||
else if (trim.StartsWith("RTP=", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
var val = trim.Substring(4).Trim();
|
||
if (val.Contains("RPGVXAce", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
engineType = TkoolEngineType.VXAce;
|
||
isRgssIni = true;
|
||
}
|
||
else if (val.Contains("RPGVX", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
engineType = TkoolEngineType.VX;
|
||
isRgssIni = true;
|
||
}
|
||
}
|
||
else if (trim.StartsWith("Library=", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
var val = trim.Substring(8).Trim();
|
||
if (val.Contains("RGSS3", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
engineType = TkoolEngineType.VXAce;
|
||
isRgssIni = true;
|
||
}
|
||
else if (val.Contains("RGSS2", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
engineType = TkoolEngineType.VX;
|
||
isRgssIni = true;
|
||
}
|
||
else if (val.Contains("RGSS", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
isRgssIni = true;
|
||
}
|
||
}
|
||
else if (trim.StartsWith("Scripts=", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
var val = trim.Substring(8).Trim();
|
||
if (val.EndsWith(".rvdata2", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
engineType = TkoolEngineType.VXAce;
|
||
isRgssIni = true;
|
||
}
|
||
else if (val.EndsWith(".rvdata", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
engineType = TkoolEngineType.VX;
|
||
isRgssIni = true;
|
||
}
|
||
}
|
||
else if (trim.StartsWith("SoftModeFlag=", StringComparison.OrdinalIgnoreCase) ||
|
||
trim.StartsWith("SEandBGM=", StringComparison.OrdinalIgnoreCase) ||
|
||
trim.Equals("[DEFAULT]", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
// ウディタのiniであることが判明した場合は直ちに除外
|
||
return null;
|
||
}
|
||
}
|
||
}
|
||
catch { }
|
||
}
|
||
|
||
// アーカイブファイルの探索
|
||
var rgss3aFiles = Directory.Exists(searchDir) ? Directory.GetFiles(searchDir, "*.rgss3a", SearchOption.TopDirectoryOnly) : Array.Empty<string>();
|
||
var rgss2aFiles = Directory.Exists(searchDir) ? Directory.GetFiles(searchDir, "*.rgss2a", SearchOption.TopDirectoryOnly) : Array.Empty<string>();
|
||
|
||
if (rgss3aFiles.Length > 0)
|
||
{
|
||
archivePath = rgss3aFiles[0];
|
||
if (engineType == TkoolEngineType.Unknown) engineType = TkoolEngineType.VXAce;
|
||
}
|
||
else if (rgss2aFiles.Length > 0)
|
||
{
|
||
archivePath = rgss2aFiles[0];
|
||
if (engineType == TkoolEngineType.Unknown) engineType = TkoolEngineType.VX;
|
||
}
|
||
|
||
// スクリプト実ファイルの探索 (Data\Scripts.rvdata2 または rvdata)
|
||
var rvdata2Path = Path.Combine(searchDir, "Data", "Scripts.rvdata2");
|
||
var rvdataPath = Path.Combine(searchDir, "Data", "Scripts.rvdata");
|
||
|
||
if (File.Exists(rvdata2Path))
|
||
{
|
||
scriptsPath = rvdata2Path;
|
||
if (engineType == TkoolEngineType.Unknown) engineType = TkoolEngineType.VXAce;
|
||
}
|
||
else if (File.Exists(rvdataPath))
|
||
{
|
||
scriptsPath = rvdataPath;
|
||
if (engineType == TkoolEngineType.Unknown) engineType = TkoolEngineType.VX;
|
||
}
|
||
|
||
// RGSS系でない場合はスキップ(アーカイブなし、スクリプトなし、RGSS用Game.iniでもない場合は除外)
|
||
if (archivePath == null && scriptsPath == null && !isRgssIni)
|
||
{
|
||
return null;
|
||
}
|
||
|
||
if (engineType == TkoolEngineType.Unknown)
|
||
{
|
||
engineType = TkoolEngineType.VXAce;
|
||
}
|
||
|
||
// 実行ファイルの探索 (Game.exe、または searchDir 内の exe)
|
||
var exePath = FindRgssExecutable(searchDir, dirPath);
|
||
if (exePath == null)
|
||
{
|
||
return null;
|
||
}
|
||
|
||
var icon = LoadIcon(exePath, dirPath);
|
||
bool hasArchive = archivePath != null;
|
||
bool isExtracted = scriptsPath != null;
|
||
bool isModded = isExtracted && RgssModService.CheckIsModded(scriptsPath!);
|
||
|
||
var plannedScriptName = engineType == TkoolEngineType.VXAce ? "Scripts.rvdata2" : "Scripts.rvdata";
|
||
var plannedScriptPath = Path.Combine(searchDir, "Data", plannedScriptName);
|
||
var plannedDataDir = Path.Combine(searchDir, "Data");
|
||
|
||
return new TkoolGameItem
|
||
{
|
||
Title = !string.IsNullOrWhiteSpace(title) ? title : folderName,
|
||
FolderName = folderName,
|
||
RootDirectory = dirPath,
|
||
ExecutablePath = exePath,
|
||
PluginsJsPath = isExtracted ? scriptsPath! : plannedScriptPath,
|
||
PluginsDirPath = plannedDataDir,
|
||
EngineType = engineType,
|
||
IsModded = isModded,
|
||
ApplyMod = true,
|
||
IconImage = icon,
|
||
FolderLastModified = folderModified,
|
||
HasRgssArchive = hasArchive,
|
||
IsRgssExtracted = isExtracted,
|
||
RgssArchivePath = archivePath
|
||
};
|
||
}
|
||
catch { }
|
||
|
||
return null;
|
||
}
|
||
|
||
private static string? FindRgssExecutable(string searchDir, string rootDir)
|
||
{
|
||
try
|
||
{
|
||
var gameExe = Path.Combine(searchDir, "Game.exe");
|
||
if (File.Exists(gameExe)) return gameExe;
|
||
|
||
var rootGameExe = Path.Combine(rootDir, "Game.exe");
|
||
if (File.Exists(rootGameExe)) return rootGameExe;
|
||
|
||
var exes = Directory.GetFiles(searchDir, "*.exe", SearchOption.TopDirectoryOnly);
|
||
foreach (var exe in exes)
|
||
{
|
||
var name = Path.GetFileName(exe);
|
||
if (!name.Contains("unins", StringComparison.OrdinalIgnoreCase) &&
|
||
!name.Contains("extractor", StringComparison.OrdinalIgnoreCase) &&
|
||
!name.Contains("patch", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
return exe;
|
||
}
|
||
}
|
||
}
|
||
catch { }
|
||
|
||
return null;
|
||
}
|
||
|
||
private string? FindPluginsJs(string currentDir, int maxDepth, int currentDepth = 0)
|
||
{
|
||
if (currentDepth > maxDepth) return null;
|
||
|
||
try
|
||
{
|
||
var candidate = Path.Combine(currentDir, "js", "plugins.js");
|
||
if (File.Exists(candidate)) return candidate;
|
||
|
||
var candidateWww = Path.Combine(currentDir, "www", "js", "plugins.js");
|
||
if (File.Exists(candidateWww)) return candidateWww;
|
||
|
||
var subDirs = Directory.GetDirectories(currentDir);
|
||
foreach (var dir in subDirs)
|
||
{
|
||
var dirName = Path.GetFileName(dir);
|
||
if (dirName.Equals("node_modules", StringComparison.OrdinalIgnoreCase) ||
|
||
dirName.Equals(".git", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
continue;
|
||
}
|
||
|
||
var result = FindPluginsJs(dir, maxDepth, currentDepth + 1);
|
||
if (result != null) return result;
|
||
}
|
||
}
|
||
catch { }
|
||
|
||
return null;
|
||
}
|
||
|
||
private string? FindTyranoIndexHtml(string dirPath)
|
||
{
|
||
try
|
||
{
|
||
var directIndex = Path.Combine(dirPath, "index.html");
|
||
if (File.Exists(directIndex) && CheckIsTyranoDirectory(dirPath))
|
||
{
|
||
return directIndex;
|
||
}
|
||
|
||
// FOSSILで梱包されたティラノスクリプトは index.html が FOSSILindex.html にリネームされる
|
||
var directFossilIndex = Path.Combine(dirPath, "FOSSILindex.html");
|
||
if (File.Exists(directFossilIndex) && CheckIsTyranoDirectory(dirPath))
|
||
{
|
||
return directFossilIndex;
|
||
}
|
||
|
||
var subDirs = Directory.GetDirectories(dirPath);
|
||
foreach (var sub in subDirs)
|
||
{
|
||
var subName = Path.GetFileName(sub);
|
||
if (subName.Equals("node_modules", StringComparison.OrdinalIgnoreCase) ||
|
||
subName.Equals("resources", StringComparison.OrdinalIgnoreCase) ||
|
||
subName.Equals(".git", StringComparison.OrdinalIgnoreCase))
|
||
continue;
|
||
|
||
var subIndex = Path.Combine(sub, "index.html");
|
||
if (File.Exists(subIndex) && CheckIsTyranoDirectory(sub))
|
||
{
|
||
return subIndex;
|
||
}
|
||
|
||
var subFossilIndex = Path.Combine(sub, "FOSSILindex.html");
|
||
if (File.Exists(subFossilIndex) && CheckIsTyranoDirectory(sub))
|
||
{
|
||
return subFossilIndex;
|
||
}
|
||
}
|
||
}
|
||
catch { }
|
||
|
||
return null;
|
||
}
|
||
|
||
private bool CheckIsTyranoDirectory(string dir)
|
||
{
|
||
try
|
||
{
|
||
if (Directory.Exists(Path.Combine(dir, "tyrano")) || Directory.Exists(Path.Combine(dir, "data", "system")))
|
||
return true;
|
||
|
||
var indexPath = Path.Combine(dir, "index.html");
|
||
if (File.Exists(indexPath))
|
||
{
|
||
var html = File.ReadAllText(indexPath);
|
||
if (html.Contains("tyrano", StringComparison.OrdinalIgnoreCase))
|
||
return true;
|
||
}
|
||
|
||
// FOSSIL梱包のティラノスクリプト用エントリ
|
||
var fossilIndexPath = Path.Combine(dir, "FOSSILindex.html");
|
||
if (File.Exists(fossilIndexPath))
|
||
{
|
||
var html = File.ReadAllText(fossilIndexPath);
|
||
if (html.Contains("tyrano", StringComparison.OrdinalIgnoreCase))
|
||
return true;
|
||
}
|
||
}
|
||
catch { }
|
||
|
||
return false;
|
||
}
|
||
|
||
private TkoolEngineType DetectEngineType(string jsDir)
|
||
{
|
||
try
|
||
{
|
||
if (File.Exists(Path.Combine(jsDir, "rmmz_core.js")) ||
|
||
File.Exists(Path.Combine(jsDir, "rmmz_managers.js")))
|
||
{
|
||
return TkoolEngineType.MZ;
|
||
}
|
||
|
||
if (File.Exists(Path.Combine(jsDir, "rpg_core.js")) ||
|
||
File.Exists(Path.Combine(jsDir, "rpg_managers.js")))
|
||
{
|
||
return TkoolEngineType.MV;
|
||
}
|
||
}
|
||
catch { }
|
||
|
||
return TkoolEngineType.MV;
|
||
}
|
||
|
||
private string ExtractTkoolTitle(string rootDir, string jsDir)
|
||
{
|
||
try
|
||
{
|
||
var parentDir = Path.GetDirectoryName(jsDir);
|
||
if (parentDir != null)
|
||
{
|
||
var systemJson = Path.Combine(parentDir, "data", "System.json");
|
||
if (File.Exists(systemJson))
|
||
{
|
||
var json = File.ReadAllText(systemJson);
|
||
var match = Regex.Match(json, @"""gameTitle""\s*:\s*""([^""]+)""");
|
||
if (match.Success && !string.IsNullOrWhiteSpace(match.Groups[1].Value))
|
||
{
|
||
return Regex.Unescape(match.Groups[1].Value);
|
||
}
|
||
}
|
||
}
|
||
|
||
var pkgJson = Path.Combine(rootDir, "package.json");
|
||
if (File.Exists(pkgJson))
|
||
{
|
||
var json = File.ReadAllText(pkgJson);
|
||
var match = Regex.Match(json, @"""name""\s*:\s*""([^""]+)""");
|
||
if (match.Success && !string.IsNullOrWhiteSpace(match.Groups[1].Value))
|
||
{
|
||
return match.Groups[1].Value;
|
||
}
|
||
}
|
||
}
|
||
catch { }
|
||
|
||
return string.Empty;
|
||
}
|
||
|
||
private string? FindTkoolExecutable(string rootDir, string jsDir)
|
||
{
|
||
try
|
||
{
|
||
var candidates = new[] { "Game.exe", "nw.exe" };
|
||
foreach (var cand in candidates)
|
||
{
|
||
var p = Path.Combine(rootDir, cand);
|
||
if (File.Exists(p)) return p;
|
||
}
|
||
|
||
var exes = Directory.GetFiles(rootDir, "*.exe", SearchOption.TopDirectoryOnly)
|
||
.Where(f => !Path.GetFileName(f).StartsWith("unins", StringComparison.OrdinalIgnoreCase))
|
||
.ToList();
|
||
|
||
if (exes.Count > 0) return exes[0];
|
||
|
||
var parent = Path.GetDirectoryName(jsDir);
|
||
if (parent != null && parent != rootDir)
|
||
{
|
||
var pExes = Directory.GetFiles(parent, "*.exe", SearchOption.TopDirectoryOnly)
|
||
.Where(f => !Path.GetFileName(f).StartsWith("unins", StringComparison.OrdinalIgnoreCase))
|
||
.ToList();
|
||
if (pExes.Count > 0) return pExes[0];
|
||
}
|
||
}
|
||
catch { }
|
||
|
||
return null;
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region Unity 検出ロジック
|
||
|
||
public UnityGameItem? DetectUnityGameInDirectory(string dirPath)
|
||
{
|
||
try
|
||
{
|
||
// 1. UnityPlayer.dll の存在確認
|
||
var unityPlayerDll = Path.Combine(dirPath, "UnityPlayer.dll");
|
||
bool hasPlayerDll = File.Exists(unityPlayerDll);
|
||
|
||
// 2. [Game]_Data フォルダの存在確認
|
||
string? dataDir = null;
|
||
var subDirs = Directory.GetDirectories(dirPath);
|
||
foreach (var sub in subDirs)
|
||
{
|
||
var name = Path.GetFileName(sub);
|
||
if (name.EndsWith("_Data", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
dataDir = sub;
|
||
break;
|
||
}
|
||
}
|
||
|
||
// UnityPlayer.dll または *_Data のいずれも存在しない場合は Unity ゲームではない
|
||
if (!hasPlayerDll && dataDir == null)
|
||
{
|
||
return null;
|
||
}
|
||
|
||
// 3. 実行可能ファイル(.exe)の探索
|
||
var exePath = FindUnityExecutable(dirPath, dataDir);
|
||
if (exePath == null)
|
||
{
|
||
return null;
|
||
}
|
||
|
||
// 4. app.info から会社名・製品名の抽出
|
||
string companyName = string.Empty;
|
||
string productName = string.Empty;
|
||
if (dataDir != null)
|
||
{
|
||
var appInfoPath = Path.Combine(dataDir, "app.info");
|
||
if (File.Exists(appInfoPath))
|
||
{
|
||
var lines = File.ReadAllLines(appInfoPath);
|
||
if (lines.Length >= 1) companyName = lines[0].Trim();
|
||
if (lines.Length >= 2) productName = lines[1].Trim();
|
||
}
|
||
}
|
||
|
||
// 5. バックエンド(IL2CPP / Mono)の判定
|
||
var backend = DetectUnityBackendType(dirPath, dataDir);
|
||
|
||
// 6. Unityバージョンの取得
|
||
var unityVersion = DetectUnityVersion(dirPath, dataDir, unityPlayerDll);
|
||
|
||
// 7. タイトルの決定
|
||
var folderName = Path.GetFileName(dirPath.TrimEnd('\\', '/'));
|
||
var title = DetermineUnityTitle(dirPath, exePath, productName, folderName);
|
||
|
||
// 8. アイコン読み込み
|
||
var icon = LoadIcon(exePath, dirPath);
|
||
|
||
// 9. 更新日時
|
||
DateTime? folderModified = null;
|
||
try { folderModified = Directory.GetLastWriteTime(dirPath); } catch { }
|
||
|
||
var game = new UnityGameItem
|
||
{
|
||
Title = title,
|
||
FolderName = folderName,
|
||
RootDirectory = dirPath,
|
||
ExecutablePath = exePath,
|
||
DataDirectoryPath = dataDir ?? string.Empty,
|
||
CompanyName = companyName,
|
||
ProductName = productName,
|
||
BackendType = backend,
|
||
UnityVersion = unityVersion,
|
||
IconImage = icon,
|
||
FolderLastModified = folderModified
|
||
};
|
||
|
||
game.HasSaveData = CheckHasSaveData(game);
|
||
|
||
return game;
|
||
}
|
||
catch
|
||
{
|
||
return null;
|
||
}
|
||
}
|
||
|
||
private string? FindUnityExecutable(string gameRoot, string? dataDir)
|
||
{
|
||
try
|
||
{
|
||
if (dataDir != null)
|
||
{
|
||
var dataName = Path.GetFileName(dataDir);
|
||
if (dataName.EndsWith("_Data", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
var baseName = dataName[..^5];
|
||
var candidate = Path.Combine(gameRoot, baseName + ".exe");
|
||
if (File.Exists(candidate)) return candidate;
|
||
}
|
||
}
|
||
|
||
var standardGameExe = Path.Combine(gameRoot, "Game.exe");
|
||
if (File.Exists(standardGameExe)) return standardGameExe;
|
||
|
||
var exeFiles = Directory.GetFiles(gameRoot, "*.exe", SearchOption.TopDirectoryOnly)
|
||
.Where(f => !Regex.IsMatch(Path.GetFileName(f), @"(unins|setup|crash|handler|UnityCrash|設定|アンインストール)", RegexOptions.IgnoreCase))
|
||
.ToList();
|
||
|
||
if (exeFiles.Count > 0)
|
||
{
|
||
return exeFiles[0];
|
||
}
|
||
}
|
||
catch { }
|
||
|
||
return null;
|
||
}
|
||
|
||
private UnityBackendType DetectUnityBackendType(string gameRoot, string? dataDir)
|
||
{
|
||
try
|
||
{
|
||
if (File.Exists(Path.Combine(gameRoot, "GameAssembly.dll")) ||
|
||
File.Exists(Path.Combine(gameRoot, "UserAssembly.dll")))
|
||
{
|
||
return UnityBackendType.IL2CPP;
|
||
}
|
||
|
||
if (dataDir != null && Directory.Exists(Path.Combine(dataDir, "il2cpp_data")))
|
||
{
|
||
return UnityBackendType.IL2CPP;
|
||
}
|
||
|
||
if (Directory.Exists(Path.Combine(gameRoot, "MonoBleedingEdge")) ||
|
||
Directory.Exists(Path.Combine(gameRoot, "Mono")))
|
||
{
|
||
return UnityBackendType.Mono;
|
||
}
|
||
|
||
if (dataDir != null && Directory.Exists(Path.Combine(dataDir, "Managed")))
|
||
{
|
||
return UnityBackendType.Mono;
|
||
}
|
||
}
|
||
catch { }
|
||
|
||
return UnityBackendType.Unknown;
|
||
}
|
||
|
||
private string DetectUnityVersion(string gameRoot, string? dataDir, string unityPlayerDll)
|
||
{
|
||
if (File.Exists(unityPlayerDll))
|
||
{
|
||
try
|
||
{
|
||
var vi = FileVersionInfo.GetVersionInfo(unityPlayerDll);
|
||
if (!string.IsNullOrEmpty(vi.FileVersion))
|
||
{
|
||
var match = Regex.Match(vi.FileVersion, @"^(\d+\.\d+\.\d+)");
|
||
if (match.Success) return match.Groups[1].Value;
|
||
return vi.FileVersion;
|
||
}
|
||
}
|
||
catch { }
|
||
}
|
||
|
||
if (dataDir != null)
|
||
{
|
||
var ggm = Path.Combine(dataDir, "globalgamemanagers");
|
||
if (File.Exists(ggm))
|
||
{
|
||
try
|
||
{
|
||
using var fs = File.OpenRead(ggm);
|
||
var buffer = new byte[64];
|
||
int read = fs.Read(buffer, 0, buffer.Length);
|
||
var text = System.Text.Encoding.ASCII.GetString(buffer, 0, read);
|
||
var match = Regex.Match(text, @"(\d+\.\d+\.\d+[a-z0-9]*)");
|
||
if (match.Success) return match.Groups[1].Value;
|
||
}
|
||
catch { }
|
||
}
|
||
}
|
||
|
||
return string.Empty;
|
||
}
|
||
|
||
private string DetermineUnityTitle(string gameRoot, string exePath, string productName, string folderName)
|
||
{
|
||
if (!string.IsNullOrWhiteSpace(productName))
|
||
{
|
||
return productName;
|
||
}
|
||
|
||
try
|
||
{
|
||
var vi = FileVersionInfo.GetVersionInfo(exePath);
|
||
if (!string.IsNullOrWhiteSpace(vi.FileDescription) &&
|
||
!vi.FileDescription.Equals("Unity", StringComparison.OrdinalIgnoreCase) &&
|
||
!vi.FileDescription.Equals("Unity Player", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
return vi.FileDescription.Trim();
|
||
}
|
||
|
||
if (!string.IsNullOrWhiteSpace(vi.ProductName) &&
|
||
!vi.ProductName.Equals("Unity", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
return vi.ProductName.Trim();
|
||
}
|
||
}
|
||
catch { }
|
||
|
||
return folderName;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Unityゲームのセーブデータが実在するかどうかチェック
|
||
/// </summary>
|
||
public bool CheckHasSaveData(UnityGameItem game)
|
||
{
|
||
try
|
||
{
|
||
// 1. レジストリ (PlayerPrefs)
|
||
if (!string.IsNullOrEmpty(game.RegistryPath))
|
||
{
|
||
using var key = Registry.CurrentUser.OpenSubKey(game.RegistryPath);
|
||
if (key != null && (key.ValueCount > 0 || key.SubKeyCount > 0))
|
||
{
|
||
return true;
|
||
}
|
||
}
|
||
|
||
// 2. LocalLow ディレクトリ
|
||
if (!string.IsNullOrEmpty(game.LocalLowPath) && Directory.Exists(game.LocalLowPath))
|
||
{
|
||
var files = Directory.GetFiles(game.LocalLowPath, "*", SearchOption.AllDirectories);
|
||
if (files.Length > 0) return true;
|
||
}
|
||
|
||
// 3. ゲーム本体フォルダ内のセーブファイル
|
||
if (Directory.Exists(game.RootDirectory))
|
||
{
|
||
var saveFiles = Directory.GetFiles(game.RootDirectory, "*.*", SearchOption.AllDirectories)
|
||
.Where(f =>
|
||
{
|
||
var fn = Path.GetFileName(f);
|
||
return fn.StartsWith("saveslot", StringComparison.OrdinalIgnoreCase) ||
|
||
fn.EndsWith(".sav", StringComparison.OrdinalIgnoreCase) ||
|
||
fn.EndsWith(".save", StringComparison.OrdinalIgnoreCase) ||
|
||
(fn.Contains("save", StringComparison.OrdinalIgnoreCase) && fn.EndsWith(".dat", StringComparison.OrdinalIgnoreCase));
|
||
});
|
||
|
||
if (saveFiles.Any()) return true;
|
||
}
|
||
}
|
||
catch { }
|
||
|
||
return false;
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 共通アイコン取得
|
||
|
||
public static ImageSource? LoadIcon(string? exePath, string? fallbackDir = null)
|
||
{
|
||
if (string.IsNullOrEmpty(exePath) || !File.Exists(exePath))
|
||
{
|
||
if (!string.IsNullOrEmpty(fallbackDir) && Directory.Exists(fallbackDir))
|
||
{
|
||
var png = Path.Combine(fallbackDir, "icon.png");
|
||
if (File.Exists(png))
|
||
{
|
||
try
|
||
{
|
||
var bmp = new BitmapImage();
|
||
bmp.BeginInit();
|
||
bmp.UriSource = new Uri(png);
|
||
bmp.CacheOption = BitmapCacheOption.OnLoad;
|
||
bmp.EndInit();
|
||
bmp.Freeze();
|
||
return bmp;
|
||
}
|
||
catch { }
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
|
||
try
|
||
{
|
||
using var sysIcon = Icon.ExtractAssociatedIcon(exePath);
|
||
if (sysIcon != null)
|
||
{
|
||
var bitmapSource = Imaging.CreateBitmapSourceFromHIcon(
|
||
sysIcon.Handle,
|
||
Int32Rect.Empty,
|
||
BitmapSizeOptions.FromEmptyOptions());
|
||
bitmapSource.Freeze();
|
||
return bitmapSource;
|
||
}
|
||
}
|
||
catch { }
|
||
|
||
return null;
|
||
}
|
||
|
||
#endregion
|
||
}
|
||
}
|