1217 lines
51 KiB
C#
1217 lines
51 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; }
|
|
|
|
// 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();
|
|
|
|
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
|
|
};
|
|
if (entry.PluginSettings != null)
|
|
{
|
|
item.PluginSettings = new Dictionary<string, bool>(entry.PluginSettings, StringComparer.OrdinalIgnoreCase);
|
|
}
|
|
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
|
|
};
|
|
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
|
|
});
|
|
}
|
|
}
|
|
|
|
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)
|
|
{
|
|
// ASAR形式ゲームの実在検証
|
|
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 (!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.IsAsar && !tg.IsAsarExtracted)
|
|
{
|
|
tg.IsModded = false;
|
|
}
|
|
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
|
|
};
|
|
}
|
|
}
|
|
}
|
|
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
|
|
}
|
|
}
|