Files
GameLauncher/Engines/Common/EngineDetectionHelper.cs

209 lines
7.5 KiB
C#

using System;
using System.IO;
namespace GameLauncher.Engines.Common
{
/// <summary>
/// Shared helpers for engine detection and misdetection repair logic,
/// consolidated from the duplicated branches previously spread across
/// GameScannerService. This is the single source of truth for engine names
/// and for re-validating a detected engine against the game directory content.
/// </summary>
public static class EngineDetectionHelper
{
/// <summary>Canonical engine name for RPG Maker (RGSS based) games.</summary>
public const string EngineRgss = "Rgss";
/// <summary>Canonical engine name for RPG Maker MV/MZ (NW.js based) games.</summary>
public const string EngineTkool = "Tkool";
/// <summary>Canonical engine name for tyrano scripts games.</summary>
public const string EngineTyrano = "Tyrano";
/// <summary>Canonical engine name for Wolf RPG games.</summary>
public const string EngineWolf = "Wolf";
/// <summary>Canonical engine name for Unity games.</summary>
public const string EngineUnity = "Unity";
/// <summary>Canonical engine name for generic NW.js packaged games.</summary>
public const string EngineNwjs = "Nwjs";
/// <summary>Canonical engine name for Enigma Virtual Box packed applications.</summary>
public const string EngineEvb = "Evb";
/// <summary>Canonical engine name used when detection fails.</summary>
public const string EngineUnknown = "Unknown";
/// <summary>Resolves the canonical engine name from a raw detection string.</summary>
/// <remarks>
/// Handles case differences and common aliases (e.g. "RPGMaker", "RGSS3",
/// "MV", "MZ", "WolfRPG", "NW.js") so callers always compare canonical names.
/// </remarks>
public static string NormalizeEngineName(string raw)
{
if (string.IsNullOrWhiteSpace(raw))
{
return EngineUnknown;
}
var value = raw.Trim();
// Case-insensitive comparison without allocating a lowercase copy each time.
if (Matches(value, "Rgss", "RGSS", "RPGMaker", "RPGMakerVX", "VX", "VXAce", "XP"))
{
return EngineRgss;
}
if (Matches(value, "Tkool", "RPGMakerMV", "RPGMakerMZ", "MV", "MZ"))
{
return EngineTkool;
}
if (Matches(value, "Tyrano", "Tyranoscript", "KAG", "TJS"))
{
return EngineTyrano;
}
if (Matches(value, "Wolf", "WolfRPG"))
{
return EngineWolf;
}
if (Matches(value, "Unity"))
{
return EngineUnity;
}
if (Matches(value, "Nwjs", "NWjs", "NW.js", "NWJS"))
{
return EngineNwjs;
}
if (Matches(value, "Evb", "Enigma", "EnigmaVB", "EnigmaVirtualBox"))
{
return EngineEvb;
}
return EngineUnknown;
}
/// <summary>Re-validates and repairs engine misdetection for a scanned game.</summary>
/// <remarks>
/// A single detection signal (extension or file layout) can be wrong, so the
/// canonical engine is re-checked against the game directory content. When the
/// detected engine contradicts the on-disk evidence, the evidence-based engine
/// is returned; otherwise the normalized detection is kept.
/// </remarks>
public static string RepairMisdetection(string gameDirectory, string detectedEngine)
{
var normalized = NormalizeEngineName(detectedEngine);
if (string.IsNullOrWhiteSpace(gameDirectory) || !Directory.Exists(gameDirectory))
{
return normalized;
}
var evidence = DetectFromDirectory(gameDirectory);
// Directory evidence has priority when it clearly identifies an engine.
if (evidence != EngineUnknown && evidence != normalized)
{
return evidence;
}
return normalized;
}
/// <summary>
/// Infers the engine purely from the game directory layout.
/// Returns <see cref="EngineUnknown"/> when no decisive evidence is found.
/// </summary>
private static string DetectFromDirectory(string gameDirectory)
{
// Enigma Virtual Box (EVB): a packed executable hides the real engine, so it
// must be detected before any other layout-based check. Detection uses the
// PE section signature only (see EvbDetectionHelper.IsEvbPacked).
if (Engines.Evb.EvbDetectionHelper.FindEvbPackedExecutable(gameDirectory) != null)
{
return EngineEvb;
}
// RPG Maker MV/MZ: www/ folder or a Game.exe next to nw.js package files.
var wwwDir = Path.Combine(gameDirectory, "www");
if (Directory.Exists(wwwDir))
{
return EngineTkool;
}
if (Engines.Nwjs.NwjsDetector.IsNwjsGame(gameDirectory))
{
return EngineNwjs;
}
// Wolf RPG: Data folder with a .wolf project file in the root.
if (Directory.Exists(Path.Combine(gameDirectory, "Data")))
{
var wolfProjects = Directory.GetFiles(gameDirectory, "*.wolf", SearchOption.TopDirectoryOnly);
if (wolfProjects.Length > 0)
{
return EngineWolf;
}
}
// RPG Maker RGSS: Game.ini or a Data folder containing .rvdata2/.rvdata files.
var gameIni = Path.Combine(gameDirectory, "Game.ini");
if (File.Exists(gameIni))
{
return EngineRgss;
}
var dataDir = Path.Combine(gameDirectory, "Data");
if (Directory.Exists(dataDir) &&
(Directory.GetFiles(dataDir, "*.rvdata2").Length > 0 ||
Directory.GetFiles(dataDir, "*.rvdata").Length > 0))
{
return EngineRgss;
}
// tyrano: a tyrano/ subfolder or a .tjs config in the root.
if (Directory.Exists(Path.Combine(gameDirectory, "tyrano")))
{
return EngineTyrano;
}
if (Directory.GetFiles(gameDirectory, "*.tjs", SearchOption.TopDirectoryOnly).Length > 0)
{
return EngineTyrano;
}
// Unity: an *_Data folder next to the executable (typical Unity layout).
foreach (var dir in Directory.GetDirectories(gameDirectory))
{
var name = Path.GetFileName(dir);
if (name.EndsWith("_Data", StringComparison.OrdinalIgnoreCase) &&
File.Exists(Path.Combine(dir, "globalgamemanagers")))
{
return EngineUnity;
}
}
return EngineUnknown;
}
/// <summary>Case-insensitive equality against any of the provided aliases.</summary>
private static bool Matches(string value, params string[] aliases)
{
foreach (var alias in aliases)
{
if (string.Equals(value, alias, StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
return false;
}
}
}