using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using GameLauncher.Helpers;
namespace GameLauncher.Engines.Evb
{
///
/// Resolves the bundled evbunpack executable used to unpack Enigma Virtual Box (EVB)
/// packed applications.
///
/// Design notes (durable / reusable):
/// - The tool is bundled under `Engines\Evb\Tools\evbunpack\` (per-engine tools folder).
/// - Path resolution goes through so that deployed, production
/// and source-tree layouts are all supported without hard-coding this machine's paths.
/// - CLI argument construction is kept in one place so callers never duplicate the
/// evbunpack invocation contract.
///
/// The evbunpack CLI contract:
/// evbunpack [-pe {10_70,9_70,7_80}] [--legacy-fs] <input exe> <output folder>
/// It restores both the packed virtual file system and the original PE executable.
///
public static class EvbUnpackerToolProvider
{
/// Engine identifier passed to .
public const string EngineName = "Evb";
/// Bundled unpacker executable name.
public const string DefaultToolExeName = "evbunpack.exe";
/// Relative subfolder under Engines/Evb/Tools that holds the tool.
public const string ToolSubDirectory = "evbunpack";
///
/// Default PE rebuild variant used when the caller does not specify one.
/// 9_70 works for Enigma Virtual Box 9.70 and 10.70/11.00 in most cases;
/// callers may override via .
///
public const string DefaultPeVariant = "9_70";
/// Gets the engine tools directory (Engines/Evb/Tools).
public static string GetToolsDirectory()
{
return AppPathHelper.GetEngineToolsDirectory(EngineName);
}
/// Gets the bundled tool subfolder (Engines/Evb/Tools/evbunpack).
public static string GetToolDirectory()
{
return Path.Combine(GetToolsDirectory(), ToolSubDirectory);
}
///
/// Resolves the evbunpack.exe path, or null when the tool is missing.
/// Prefers the bundled subfolder, then falls back to a flat layout inside the
/// engine tools folder (and the shared probe).
///
public static string? GetToolPath()
{
try
{
var candidates = new[]
{
Path.Combine(GetToolDirectory(), DefaultToolExeName),
Path.Combine(GetToolsDirectory(), DefaultToolExeName),
};
var found = candidates.FirstOrDefault(File.Exists);
if (found != null)
{
return found;
}
// Fall back to the shared per-engine resolver (checks deployed + dev layouts).
return AppPathHelper.ResolveEngineTool(
EngineName,
Path.Combine(ToolSubDirectory, DefaultToolExeName));
}
catch
{
return null;
}
}
/// True when the unpacker executable exists and is usable.
public static bool IsAvailable() => GetToolPath() != null;
///
/// Builds the argument list for the unpacker CLI.
/// Emits the PE variant first, then the optional legacy switch, then the
/// input executable and the output folder. Values are raw (no quoting); callers
/// pass them through ProcessStartInfo.ArgumentList which handles escaping.
///
/// Path to the EVB-packed executable.
/// Folder that receives the unpacked files.
/// When true, adds --legacy-fs for packer version 7.80.
/// PE rebuild variant (10_70/9_70/7_80); defaults to .
public static IReadOnlyList BuildArguments(
string inputExePath,
string outputDirectory,
bool legacyFileSystem = false,
string? peVariant = null)
{
var args = new List();
if (legacyFileSystem)
{
args.Add("--legacy-fs");
}
var variant = string.IsNullOrWhiteSpace(peVariant) ? DefaultPeVariant : peVariant.Trim();
args.Add("-pe");
args.Add(variant);
args.Add(inputExePath);
args.Add(outputDirectory);
return args;
}
}
}