285 lines
11 KiB
C#
285 lines
11 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
|
|
namespace GameLauncher.Engines.Evb
|
|
{
|
|
/// <summary>
|
|
/// Detects Enigma Virtual Box (EVB) packed executables that are actually compressed
|
|
/// with EVB and unpackable by <c>evbunpack</c>.
|
|
///
|
|
/// Detection strategy:
|
|
/// 1. Parse the PE header and inspect the section table for the packer's
|
|
/// <c>.enigma1</c> section (required by <c>evbunpack</c> to restore the PE).
|
|
/// 2. Verify the presence of the EVB virtual filesystem pack header signature
|
|
/// (<c>"EVB\0"</c> / <c>0x45, 0x56, 0x42, 0x00</c>) inside the Enigma section
|
|
/// data or PE overlay (required by <c>evbunpack</c> to extract the VFS, and
|
|
/// distinguishes Enigma Virtual Box from commercial Enigma Protector DRM which
|
|
/// cannot be unpacked).
|
|
///
|
|
/// This class is engine-local and has no side effects.
|
|
/// </summary>
|
|
public static class EvbDetectionHelper
|
|
{
|
|
/// <summary>Primary section name required by evbunpack.</summary>
|
|
private const string EnigmaPrimarySection = ".enigma1";
|
|
|
|
/// <summary>Secondary section name produced by the packer.</summary>
|
|
private const string EnigmaSecondarySection = ".enigma2";
|
|
|
|
/// <summary>EVB virtual filesystem header magic ("EVB\0").</summary>
|
|
private static readonly byte[] EvbMagic = { 0x45, 0x56, 0x42, 0x00 };
|
|
|
|
/// <summary>Returns true when the given executable is packed with EVB and can be unpacked.</summary>
|
|
/// <remarks>
|
|
/// Reads the PE section table for <c>.enigma1</c> and verifies the <c>EVB\0</c>
|
|
/// virtual filesystem header. Any IO/format error yields false.
|
|
/// </remarks>
|
|
public static bool IsEvbPacked(string exePath)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(exePath) || !File.Exists(exePath))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
try
|
|
{
|
|
return HasExtractableEnigmaPackage(exePath);
|
|
}
|
|
catch
|
|
{
|
|
// Unparseable executable -> never claim EVB.
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Scans a game directory for an EVB packed executable.
|
|
/// Returns the first decisive match, or null when none is found.
|
|
/// </summary>
|
|
public static string? FindEvbPackedExecutable(string gameDirectory)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(gameDirectory) || !Directory.Exists(gameDirectory))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
try
|
|
{
|
|
var exes = Directory.GetFiles(gameDirectory, "*.exe", SearchOption.TopDirectoryOnly)
|
|
.Where(f => !IsExcludedUtilityExe(Path.GetFileName(f)))
|
|
.OrderBy(f => string.Equals(Path.GetFileName(f), "Game.exe", StringComparison.OrdinalIgnoreCase) ? 0 : 1);
|
|
|
|
return exes.FirstOrDefault(IsEvbPacked);
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private static bool IsExcludedUtilityExe(string fileName)
|
|
{
|
|
if (string.IsNullOrEmpty(fileName)) return true;
|
|
return fileName.StartsWith("unins", StringComparison.OrdinalIgnoreCase) ||
|
|
fileName.StartsWith("UnityCrashHandler", StringComparison.OrdinalIgnoreCase) ||
|
|
fileName.Equals("notification_helper.exe", StringComparison.OrdinalIgnoreCase) ||
|
|
fileName.Equals("crashpad_handler.exe", StringComparison.OrdinalIgnoreCase);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns true when the game directory already holds a completed EVB extraction.
|
|
///
|
|
/// Once a game is unpacked, the original packed exe is renamed to "*.glbak" and the
|
|
/// unpacked files (including the real exe) live inside the game-local "_evb_extracted"
|
|
/// folder. At that point <see cref="FindEvbPackedExecutable"/> can no longer find a
|
|
/// top-level "*.exe", so this extra signal is required to keep recognizing the folder
|
|
/// as an EVB game (otherwise launch and icon resolution break after unpacking).
|
|
///
|
|
/// Only a completed extraction (completion marker present) is accepted, so a normal
|
|
/// unpacked game without any EVB artifacts is never misclassified.
|
|
/// </summary>
|
|
public static bool IsExtracted(string gameDirectory)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(gameDirectory) || !Directory.Exists(gameDirectory))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
try
|
|
{
|
|
return EvbUnpackService.IsExtractionComplete(gameDirectory, out _);
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Parses the PE section table, verifies the <c>.enigma1</c> section, and checks
|
|
/// for the <c>EVB\0</c> virtual filesystem header required by <c>evbunpack</c>.
|
|
/// </summary>
|
|
private static bool HasExtractableEnigmaPackage(string exePath)
|
|
{
|
|
using var fs = new FileStream(exePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
|
using var reader = new BinaryReader(fs);
|
|
|
|
if (fs.Length < 0x40)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// DOS header: check the "MZ" signature and locate the PE header offset.
|
|
if (reader.ReadUInt16() != 0x5A4D)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
fs.Seek(0x3C, SeekOrigin.Begin);
|
|
int peOffset = reader.ReadInt32();
|
|
if (peOffset <= 0 || peOffset + 24 > fs.Length)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
fs.Seek(peOffset, SeekOrigin.Begin);
|
|
if (reader.ReadUInt32() != 0x00004550) // "PE\0\0"
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// COFF header: number of sections is at offset 6 from PE signature (peOffset + 6).
|
|
fs.Seek(peOffset + 6, SeekOrigin.Begin);
|
|
ushort sectionCount = reader.ReadUInt16();
|
|
if (sectionCount == 0 || sectionCount > 96)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// Optional header size is at offset 20 in the COFF header.
|
|
fs.Seek(peOffset + 20, SeekOrigin.Begin);
|
|
ushort optionalHeaderSize = reader.ReadUInt16();
|
|
|
|
// Section table starts right after the optional header.
|
|
long sectionTable = peOffset + 24 + optionalHeaderSize;
|
|
|
|
bool hasEnigma1 = false;
|
|
var enigmaRanges = new List<(long Offset, long Size)>();
|
|
long maxSectionEnd = 0;
|
|
|
|
// Each section header is 40 bytes.
|
|
for (int i = 0; i < sectionCount; i++)
|
|
{
|
|
long offset = sectionTable + (long)i * 40;
|
|
if (offset + 40 > fs.Length)
|
|
{
|
|
break;
|
|
}
|
|
|
|
fs.Seek(offset, SeekOrigin.Begin);
|
|
var nameBytes = reader.ReadBytes(8);
|
|
var name = System.Text.Encoding.ASCII.GetString(nameBytes).TrimEnd('\0').Trim();
|
|
|
|
fs.Seek(offset + 16, SeekOrigin.Begin);
|
|
uint rawSize = reader.ReadUInt32();
|
|
uint rawPtr = reader.ReadUInt32();
|
|
|
|
if (rawPtr > 0 && rawSize > 0)
|
|
{
|
|
long end = (long)rawPtr + rawSize;
|
|
if (end > maxSectionEnd && end <= fs.Length)
|
|
{
|
|
maxSectionEnd = end;
|
|
}
|
|
}
|
|
|
|
if (string.Equals(name, EnigmaPrimarySection, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
hasEnigma1 = true;
|
|
if (rawPtr > 0 && rawSize > 0 && rawPtr < fs.Length)
|
|
{
|
|
enigmaRanges.Insert(0, (rawPtr, Math.Min((long)rawSize, fs.Length - rawPtr)));
|
|
}
|
|
}
|
|
else if (string.Equals(name, EnigmaSecondarySection, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
if (rawPtr > 0 && rawSize > 0 && rawPtr < fs.Length)
|
|
{
|
|
enigmaRanges.Add((rawPtr, Math.Min((long)rawSize, fs.Length - rawPtr)));
|
|
}
|
|
}
|
|
}
|
|
|
|
// evbunpack requires the .enigma1 section to unpack and rebuild the executable.
|
|
if (!hasEnigma1)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// Also include PE overlay if any bytes exist after the last section.
|
|
if (maxSectionEnd > 0 && maxSectionEnd + 64 <= fs.Length)
|
|
{
|
|
enigmaRanges.Add((maxSectionEnd, fs.Length - maxSectionEnd));
|
|
}
|
|
|
|
// Verify that the EVB virtual filesystem signature ("EVB\0") exists in the
|
|
// Enigma sections or overlay so that Enigma Protector DRM executables (which
|
|
// have .enigma1/.enigma2 but no unpackable EVB VFS) are not misclassified.
|
|
foreach (var (rangeOffset, rangeSize) in enigmaRanges)
|
|
{
|
|
if (ContainsEvbMagicInRange(fs, rangeOffset, rangeSize))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Searches for the "EVB\0" pack header signature within the given file range
|
|
/// (scanning up to the first 16 MB of the range in 64 KB chunks).
|
|
/// </summary>
|
|
private static bool ContainsEvbMagicInRange(FileStream fs, long startOffset, long length)
|
|
{
|
|
const int chunkSize = 64 * 1024;
|
|
long maxScan = Math.Min(length, 16L * 1024 * 1024);
|
|
if (maxScan < EvbMagic.Length)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
var buffer = new byte[chunkSize + EvbMagic.Length];
|
|
long scanned = 0;
|
|
|
|
while (scanned < maxScan)
|
|
{
|
|
long readPos = startOffset + scanned;
|
|
int toRead = (int)Math.Min(buffer.Length, (startOffset + maxScan) - readPos);
|
|
if (toRead < EvbMagic.Length)
|
|
{
|
|
break;
|
|
}
|
|
|
|
fs.Seek(readPos, SeekOrigin.Begin);
|
|
int read = fs.Read(buffer, 0, toRead);
|
|
if (read < EvbMagic.Length)
|
|
{
|
|
break;
|
|
}
|
|
|
|
if (buffer.AsSpan(0, read).IndexOf(EvbMagic) >= 0)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
scanned += chunkSize;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
}
|