2158 lines
95 KiB
C#
2158 lines
95 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;
|
|
|
|
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;
|
|
|
|
// 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
|
|
};
|
|
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;
|
|
|
|
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
|
|
};
|
|
|
|
// 既存キャッシュの自動修復: ウディタの誤判定を修正
|
|
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
|
|
});
|
|
}
|
|
}
|
|
|
|
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. 通常のティラノスクリプト探索
|
|
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"
|
|
};
|
|
}
|
|
|
|
// 3. 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
|
|
};
|
|
}
|
|
}
|
|
|
|
// 4. WOLF RPGエディター(ウディタ)ゲームの検出
|
|
var wolfGame = DetectWolfGameInDirectory(dirPath, folderName, folderModified);
|
|
if (wolfGame != null)
|
|
{
|
|
return wolfGame;
|
|
}
|
|
|
|
// 5. SRPG Studio ゲームの検出
|
|
var srpgGame = DetectSrpgGameInDirectory(dirPath, folderName, folderModified);
|
|
if (srpgGame != null)
|
|
{
|
|
return srpgGame;
|
|
}
|
|
|
|
// 6. ツクールVX / VX Ace (RGSS2 / RGSS3) ゲームの検出
|
|
var rgssGame = DetectRgssGameInDirectory(dirPath, folderName, folderModified);
|
|
if (rgssGame != null)
|
|
{
|
|
return rgssGame;
|
|
}
|
|
}
|
|
catch { }
|
|
|
|
return null;
|
|
}
|
|
|
|
#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ファイルを行単位で読み込みます。
|
|
/// </summary>
|
|
public static string[] ReadIniLinesSafe(string filePath)
|
|
{
|
|
try
|
|
{
|
|
if (!File.Exists(filePath)) return Array.Empty<string>();
|
|
|
|
byte[] bytes = File.ReadAllBytes(filePath);
|
|
if (bytes.Length == 0) return Array.Empty<string>();
|
|
|
|
// 1. UTF-8 BOM (EF BB BF)
|
|
if (bytes.Length >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF)
|
|
{
|
|
var text = System.Text.Encoding.UTF8.GetString(bytes, 3, bytes.Length - 3);
|
|
return text.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
|
|
}
|
|
|
|
// 2. UTF-16 LE BOM (FF FE)
|
|
if (bytes.Length >= 2 && bytes[0] == 0xFF && bytes[1] == 0xFE)
|
|
{
|
|
var text = System.Text.Encoding.Unicode.GetString(bytes, 2, bytes.Length - 2);
|
|
return text.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
|
|
}
|
|
|
|
// 3. Shift_JIS (CP932) でデコード(ツクールVX/VX Aceおよびウディタの標準)
|
|
System.Text.Encoding encoding;
|
|
try
|
|
{
|
|
encoding = System.Text.Encoding.GetEncoding(932);
|
|
}
|
|
catch
|
|
{
|
|
encoding = System.Text.Encoding.GetEncoding("shift_jis");
|
|
}
|
|
|
|
var decoded = encoding.GetString(bytes);
|
|
return decoded.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
|
|
}
|
|
catch
|
|
{
|
|
return Array.Empty<string>();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// キャッシュ内に文字化けして保存されたRGSSタイトルの修復を行います。
|
|
/// </summary>
|
|
public static void FixRgssTitleFromIni(TkoolGameItem tg)
|
|
{
|
|
try
|
|
{
|
|
string? iniPath = null;
|
|
var directIni = Path.Combine(tg.RootDirectory, "Game.ini");
|
|
if (File.Exists(directIni))
|
|
{
|
|
iniPath = directIni;
|
|
}
|
|
else
|
|
{
|
|
if (Directory.Exists(tg.RootDirectory))
|
|
{
|
|
foreach (var sub in Directory.GetDirectories(tg.RootDirectory))
|
|
{
|
|
var subIni = Path.Combine(sub, "Game.ini");
|
|
if (File.Exists(subIni))
|
|
{
|
|
iniPath = subIni;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (iniPath != null)
|
|
{
|
|
var lines = ReadIniLinesSafe(iniPath);
|
|
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) && !val.Contains('\uFFFD'))
|
|
{
|
|
tg.Title = val;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
#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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
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;
|
|
}
|
|
}
|
|
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
|
|
}
|
|
}
|