using System.Text.RegularExpressions; using EvilEngine.Adapters.Pc; using EvilEngine.Adapters.Steam; using EvilEngine.Adapters.Unity; using EvilEngine.Mounts; namespace EvilEngine.Adapters.BattleBit; public sealed partial class BattleBitGame : IGameAdapter { public const string AppId = "671860"; public const string Company = "BattleBitDevTeam"; public const string Product = "BattleBit"; public static readonly VirtualPath Data = "BattleBit_Data"; public static readonly VirtualPath Executable = "BattleBit.exe"; public static readonly VirtualPath GameAssembly = "GameAssembly.dll"; private static readonly Platform Pc = PcPlatform.Platform; private static readonly VirtualPath Metadata = Data.Join("il2cpp_data/Metadata/global-metadata.dat"); private static readonly VirtualPath Spawnables = Data.Join("resources.assets"); private static readonly string[] ModLoader = ["BepInEx", "winhttp.dll", "doorstop_config.ini"]; private static readonly HashSet Unparted = ["Intro", "MainMenu", "LoadingScreen"]; public string Game => BattleBit.Game; public IReadOnlyList Platforms { get; } = [Pc]; public MountProbe? Probe(IFileSource tree) { var player = new PlayerData(tree, Data); if (PlayerData.ReadAppInfo(tree, player.AppInfo) != (Company, Product) || !tree.Exists(player.GlobalGameManagers) || !tree.Exists(Executable)) return null; var evidence = new List { $"{player.AppInfo} names {Company}'s {Product}", $"{Data} is Unity's player data", $"{Executable} is the windows player build" }; if (LinkTime(tree) is { } linked) evidence.Add($"{GameAssembly} was linked {linked:yyyy-MM-dd HH:mm:ss} UTC"); var version = player.ReadVersion(out var why); if (version is null || version.Company != Company || version.Product != Product) { evidence.Add(version is null ? $"{player.GlobalGameManagers} names no version: {why}" : $"{player.GlobalGameManagers} names {version.Company}'s {version.Product}, not the game app.info names"); return new MountProbe(new MountId(Game, Pc.Id, "unknown"), ProbeStatus.UnknownVariant, tree) { Evidence = evidence, Externals = SteamClaims.Externals }; } evidence.Add($"{player.GlobalGameManagers} names version {version.Version}"); return new MountProbe(new MountId(Game, Pc.Id, version.Version), ProbeStatus.Known, tree) { Evidence = evidence, Externals = SteamClaims.Externals }; } public MountPlan Open(MountProbe probe, Externals externals) { var tree = probe.Tree; var player = new PlayerData(tree, Data); var notes = new List(); var claims = SteamClaims.For(tree, externals, AppId, notes) .ToDictionary(pair => pair.Key.Value, pair => pair.Value, StringComparer.OrdinalIgnoreCase); foreach (var marker in ModLoader.Where(marker => tree.Exists(marker) || tree.DirectoryExists(marker))) notes.Add($"{marker} is BepInEx's, so the install is modded, and its mods aren't read."); var spawnables = Close(player, Spawnables, notes); var parts = new List { Part("spawnables", spawnables, claims) }; var scenes = Scenes(tree, player, out var why); if (scenes is null) notes.Add($"{player.GlobalGameManagers}'s scene list doesn't read, so the mount has no map parts: {why}."); else for (var index = 0; index < scenes.Count; index++) { var name = BuildSettings.SceneName(scenes[index]); if (MapScene().Match(scenes[index]) is { Success: true } map && map.Groups[1].Value == name) parts.Add(Part($"map/{name}", [.. Close(player, Data.Join(BuildSettings.LevelFile(index)), notes), .. spawnables], claims)); else if (!Unparted.Contains(name)) notes.Add($"The scene list names {scenes[index]} as {BuildSettings.LevelFile(index)}, " + "which is neither a map nor a scene the adapter knows, so it's in no part."); } return new MountPlan(Pc.CaseRule) { Layers = [new LayerPlan("install", "base", new LayerSource.Tree(tree))], BuildFiles = [.. new[] { player.AppInfo, player.GlobalGameManagers, GameAssembly, Metadata }.Where(tree.Exists)], Parts = parts, Notes = [.. notes.Distinct()] }; } private static IReadOnlyList Close(PlayerData player, VirtualPath file, List notes) { return [.. player.Close(file, notes), player.GlobalGameManagers]; } private static IReadOnlyList? Scenes(IFileSource tree, PlayerData player, out string why) { try { using var access = tree.OpenAccess(player.GlobalGameManagers); var scenes = BuildSettings.ReadScenes(SerializedFile.Read(access), access, out var reason); why = reason ?? ""; return scenes; } catch (Exception error) when (error is InvalidDataException or EndOfStreamException or FileNotFoundException) { why = $"{player.GlobalGameManagers} can't be read: {error.Message}"; return null; } } private static PartPlan Part(string name, IEnumerable files, Dictionary claims) { VirtualPath[] defining = [.. files.Distinct().Order()]; return new PartPlan(name, defining) { Sha1Claims = true, Claims = defining.Where(path => claims.ContainsKey(path.Value)) .ToDictionary(path => path, path => claims[path.Value]) }; } private static DateTime? LinkTime(IFileSource tree) { if (!tree.TryOpen(GameAssembly, out var stream)) return null; using (stream) { var header = new byte[4096]; var read = stream.ReadAtLeast(header, header.Length, false); if (read < 0x40 || header[0] != 'M' || header[1] != 'Z') return null; var pe = BitConverter.ToInt32(header, 0x3C); if (pe < 0 || pe + 12 > read || header[pe] != 'P' || header[pe + 1] != 'E' || header[pe + 2] != 0 || header[pe + 3] != 0) return null; return DateTime.UnixEpoch.AddSeconds(BitConverter.ToUInt32(header, pe + 8)); } } [GeneratedRegex(@"^Assets/BattleBitRemastered/Maps/([^/]+)/[^/]+\.unity$")] private static partial Regex MapScene(); }