Skip to content
0.2.0-dev Download ZIP
156 lines ·6.6 KB Raw
1using System.Text.RegularExpressions;
2using EvilEngine.Adapters.Pc;
3using EvilEngine.Adapters.Steam;
4using EvilEngine.Adapters.Unity;
5using EvilEngine.Mounts;
6
7namespace EvilEngine.Adapters.BattleBit;
8
9public sealed partial class BattleBitGame : IGameAdapter
10{
11 public const string AppId = "671860";
12
13 public const string Company = "BattleBitDevTeam";
14
15 public const string Product = "BattleBit";
16
17 public static readonly VirtualPath Data = "BattleBit_Data";
18
19 public static readonly VirtualPath Executable = "BattleBit.exe";
20
21 public static readonly VirtualPath GameAssembly = "GameAssembly.dll";
22
23 private static readonly Platform Pc = PcPlatform.Platform;
24
25 private static readonly VirtualPath Metadata = Data.Join("il2cpp_data/Metadata/global-metadata.dat");
26
27 private static readonly VirtualPath Spawnables = Data.Join("resources.assets");
28
29 private static readonly string[] ModLoader = ["BepInEx", "winhttp.dll", "doorstop_config.ini"];
30
31 private static readonly HashSet<string> Unparted = ["Intro", "MainMenu", "LoadingScreen"];
32
33 public string Game => BattleBit.Game;
34
35 public IReadOnlyList<Platform> Platforms { get; } = [Pc];
36
37 public MountProbe? Probe(IFileSource tree)
38 {
39 var player = new PlayerData(tree, Data);
40 if (PlayerData.ReadAppInfo(tree, player.AppInfo) != (Company, Product) ||
41 !tree.Exists(player.GlobalGameManagers) ||
42 !tree.Exists(Executable))
43 return null;
44
45 var evidence = new List<string>
46 {
47 $"{player.AppInfo} names {Company}'s {Product}", $"{Data} is Unity's player data",
48 $"{Executable} is the windows player build"
49 };
50 if (LinkTime(tree) is { } linked)
51 evidence.Add($"{GameAssembly} was linked {linked:yyyy-MM-dd HH:mm:ss} UTC");
52 var version = player.ReadVersion(out var why);
53 if (version is null || version.Company != Company || version.Product != Product)
54 {
55 evidence.Add(version is null
56 ? $"{player.GlobalGameManagers} names no version: {why}"
57 : $"{player.GlobalGameManagers} names {version.Company}'s {version.Product}, not the game app.info names");
58 return new MountProbe(new MountId(Game, Pc.Id, "unknown"), ProbeStatus.UnknownVariant, tree)
59 { Evidence = evidence, Externals = SteamClaims.Externals };
60 }
61
62 evidence.Add($"{player.GlobalGameManagers} names version {version.Version}");
63 return new MountProbe(new MountId(Game, Pc.Id, version.Version), ProbeStatus.Known, tree)
64 { Evidence = evidence, Externals = SteamClaims.Externals };
65 }
66
67 public MountPlan Open(MountProbe probe, Externals externals)
68 {
69 var tree = probe.Tree;
70 var player = new PlayerData(tree, Data);
71 var notes = new List<string>();
72 var claims = SteamClaims.For(tree, externals, AppId, notes)
73 .ToDictionary(pair => pair.Key.Value, pair => pair.Value, StringComparer.OrdinalIgnoreCase);
74 foreach (var marker in ModLoader.Where(marker => tree.Exists(marker) || tree.DirectoryExists(marker)))
75 notes.Add($"{marker} is BepInEx's, so the install is modded, and its mods aren't read.");
76
77 var spawnables = Close(player, Spawnables, notes);
78 var parts = new List<PartPlan> { Part("spawnables", spawnables, claims) };
79 var scenes = Scenes(tree, player, out var why);
80 if (scenes is null)
81 notes.Add($"{player.GlobalGameManagers}'s scene list doesn't read, so the mount has no map parts: {why}.");
82 else
83 for (var index = 0; index < scenes.Count; index++)
84 {
85 var name = BuildSettings.SceneName(scenes[index]);
86 if (MapScene().Match(scenes[index]) is { Success: true } map && map.Groups[1].Value == name)
87 parts.Add(Part($"map/{name}",
88 [.. Close(player, Data.Join(BuildSettings.LevelFile(index)), notes), .. spawnables], claims));
89 else if (!Unparted.Contains(name))
90 notes.Add($"The scene list names {scenes[index]} as {BuildSettings.LevelFile(index)}, " +
91 "which is neither a map nor a scene the adapter knows, so it's in no part.");
92 }
93
94 return new MountPlan(Pc.CaseRule)
95 {
96 Layers = [new LayerPlan("install", "base", new LayerSource.Tree(tree))],
97 BuildFiles =
98 [.. new[] { player.AppInfo, player.GlobalGameManagers, GameAssembly, Metadata }.Where(tree.Exists)],
99 Parts = parts,
100 Notes = [.. notes.Distinct()]
101 };
102 }
103
104 private static IReadOnlyList<VirtualPath> Close(PlayerData player, VirtualPath file, List<string> notes)
105 {
106 return [.. player.Close(file, notes), player.GlobalGameManagers];
107 }
108
109 private static IReadOnlyList<string>? Scenes(IFileSource tree, PlayerData player, out string why)
110 {
111 try
112 {
113 using var access = tree.OpenAccess(player.GlobalGameManagers);
114 var scenes = BuildSettings.ReadScenes(SerializedFile.Read(access), access, out var reason);
115 why = reason ?? "";
116 return scenes;
117 }
118 catch (Exception error) when (error is InvalidDataException or EndOfStreamException or FileNotFoundException)
119 {
120 why = $"{player.GlobalGameManagers} can't be read: {error.Message}";
121 return null;
122 }
123 }
124
125 private static PartPlan Part(string name, IEnumerable<VirtualPath> files, Dictionary<string, string> claims)
126 {
127 VirtualPath[] defining = [.. files.Distinct().Order()];
128 return new PartPlan(name, defining)
129 {
130 Sha1Claims = true,
131 Claims = defining.Where(path => claims.ContainsKey(path.Value))
132 .ToDictionary(path => path, path => claims[path.Value])
133 };
134 }
135
136 private static DateTime? LinkTime(IFileSource tree)
137 {
138 if (!tree.TryOpen(GameAssembly, out var stream))
139 return null;
140 using (stream)
141 {
142 var header = new byte[4096];
143 var read = stream.ReadAtLeast(header, header.Length, false);
144 if (read < 0x40 || header[0] != 'M' || header[1] != 'Z')
145 return null;
146 var pe = BitConverter.ToInt32(header, 0x3C);
147 if (pe < 0 || pe + 12 > read || header[pe] != 'P' || header[pe + 1] != 'E' || header[pe + 2] != 0 ||
148 header[pe + 3] != 0)
149 return null;
150 return DateTime.UnixEpoch.AddSeconds(BitConverter.ToUInt32(header, pe + 8));
151 }
152 }
153
154 [GeneratedRegex(@"^Assets/BattleBitRemastered/Maps/([^/]+)/[^/]+\.unity$")]
155 private static partial Regex MapScene();
156}