Files
GameLauncher/Engines/Evb/EvbDetectionHelper.cs
T
armonica 908dc443eb fix(evb): PEセクションによるEVB判定の厳格化と誤検知の防止
- 誤検知の要因となっていたバイナリ文字列探索(ContainsEvbContentMarker)を廃止
- PEセクションテーブル(.enigma1, .enigma2)の検出のみで確実に判定するよう見直し
2026-10-14 09:57:33 +09:00

168 lines
6.0 KiB
C#

using System;
using System.IO;
using System.Linq;
namespace GameLauncher.Engines.Evb
{
/// <summary>
/// Detects Enigma Virtual Box (EVB) packed executables using reliable PE signatures
/// only. Heuristic checks such as file name or extension are intentionally avoided so
/// that ordinary (unpacked) games are never misclassified.
///
/// Detection strategy:
/// Parse the PE header and inspect the section table for the packer's sections
/// (".enigma1" / ".enigma2"). A present section is a decisive match; anything else
/// is treated as "not EVB" so plain (unpacked) games are never misclassified.
///
/// This class is engine-local and has no side effects.
/// </summary>
public static class EvbDetectionHelper
{
/// <summary>Section names produced by the packer.</summary>
private static readonly string[] EnigmaSections = { ".enigma1", ".enigma2" };
/// <summary>Returns true when the given executable is packed with EVB.</summary>
/// <remarks>
/// Reads the PE section table for the packer's section names. Any IO/format error
/// yields false (treated as "not confidently EVB").
/// </remarks>
public static bool IsEvbPacked(string exePath)
{
if (string.IsNullOrWhiteSpace(exePath) || !File.Exists(exePath))
{
return false;
}
try
{
// Decisive: the packer's own PE sections are the only reliable signal.
return HasEnigmaSection(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
{
return Directory.GetFiles(gameDirectory, "*.exe", SearchOption.TopDirectoryOnly)
.FirstOrDefault(IsEvbPacked);
}
catch
{
return null;
}
}
/// <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 and checks for the EVB packer's section names.
/// </summary>
private static bool HasEnigmaSection(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: skip machine + number of sections is at offset 2 from here.
fs.Seek(peOffset + 4, SeekOrigin.Begin);
ushort sectionCount = reader.ReadUInt16();
// Optional header size (end of the optional header) 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;
// Each section header is 40 bytes; the section name is the first 8 bytes.
for (int i = 0; i < sectionCount; i++)
{
long offset = sectionTable + (long)i * 40;
if (offset + 8 > fs.Length)
{
break;
}
fs.Seek(offset, SeekOrigin.Begin);
var nameBytes = reader.ReadBytes(8);
var name = System.Text.Encoding.ASCII.GetString(nameBytes).TrimEnd('\0').Trim();
foreach (var section in EnigmaSections)
{
if (string.Equals(name, section, StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
}
return false;
}
}
}