using System;
using System.IO;
namespace GameLauncher.Engines.Common
{
///
/// 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.
///
public static class EngineDetectionHelper
{
/// Canonical engine name for RPG Maker (RGSS based) games.
public const string EngineRgss = "Rgss";
/// Canonical engine name for RPG Maker MV/MZ (NW.js based) games.
public const string EngineTkool = "Tkool";
/// Canonical engine name for tyrano scripts games.
public const string EngineTyrano = "Tyrano";
/// Canonical engine name for Wolf RPG games.
public const string EngineWolf = "Wolf";
/// Canonical engine name for Unity games.
public const string EngineUnity = "Unity";
/// Canonical engine name for generic NW.js packaged games.
public const string EngineNwjs = "Nwjs";
/// Canonical engine name for Enigma Virtual Box packed applications.
public const string EngineEvb = "Evb";
/// Canonical engine name used when detection fails.
public const string EngineUnknown = "Unknown";
/// Resolves the canonical engine name from a raw detection string.
///
/// Handles case differences and common aliases (e.g. "RPGMaker", "RGSS3",
/// "MV", "MZ", "WolfRPG", "NW.js") so callers always compare canonical names.
///
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;
}
/// Re-validates and repairs engine misdetection for a scanned game.
///
/// 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.
///
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;
}
///
/// Infers the engine purely from the game directory layout.
/// Returns when no decisive evidence is found.
///
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;
}
var packageJson = Path.Combine(gameDirectory, "package.json");
if (File.Exists(packageJson) && Directory.Exists(Path.Combine(gameDirectory, "nwjs")))
{
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;
}
/// Case-insensitive equality against any of the provided aliases.
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;
}
}
}