0.2.0-dev Download ZIP
416 lines ·17.7 KB Raw
| 1 | using System.Security.Cryptography; |
| 2 | using EvilEngine.Adapters.BattleBit; |
| 3 | using EvilEngine.Adapters.LethalCompany; |
| 4 | using EvilEngine.Adapters.ScheduleI; |
| 5 | using EvilEngine.Adapters.SlimeRancher; |
| 6 | using EvilEngine.Adapters.Steam; |
| 7 | using EvilEngine.Adapters.Tests.Support; |
| 8 | using EvilEngine.Adapters.Unity; |
| 9 | using EvilEngine.Mounts; |
| 10 | |
| 11 | namespace EvilEngine.Adapters.Tests; |
| 12 | |
| 13 | public sealed class BattleBitTests |
| 14 | { |
| 15 | private const string Data = "BattleBit_Data"; |
| 16 | |
| 17 | private const string InstallPath = "/library/steamapps/common/BattleBit Remastered"; |
| 18 | |
| 19 | private const string Unity = "2019.4.40f1"; |
| 20 | |
| 21 | private const int Windows = 19; |
| 22 | |
| 23 | private const uint Linked = 1702878587; |
| 24 | |
| 25 | private static readonly string[] Scenes = |
| 26 | [ |
| 27 | "Assets/BattleBitRemastered/Scene/Intro.unity", "Assets/Scenes/MainMenu.unity", |
| 28 | "Assets/BattleBitRemastered/Maps/Dustydew/Dustydew.unity", |
| 29 | "Assets/BattleBitRemastered/Maps/Azagor/Azagor.unity", |
| 30 | "Assets/Scenes/LoadingScreen.unity" |
| 31 | ]; |
| 32 | |
| 33 | private static byte[] Code(uint linked = Linked) |
| 34 | { |
| 35 | var bytes = new byte[0x200]; |
| 36 | bytes[0] = (byte)'M'; |
| 37 | bytes[1] = (byte)'Z'; |
| 38 | BitConverter.GetBytes(0x80).CopyTo(bytes, 0x3C); |
| 39 | "PE\0\0"u8.CopyTo(bytes.AsSpan(0x80)); |
| 40 | BitConverter.GetBytes((ushort)0x8664).CopyTo(bytes, 0x84); |
| 41 | BitConverter.GetBytes(linked).CopyTo(bytes, 0x88); |
| 42 | return bytes; |
| 43 | } |
| 44 | |
| 45 | private static byte[] Serialized(params string[] externals) |
| 46 | { |
| 47 | var writer = new SerializedFileWriter(Unity, Windows).Object(1, new byte[16]); |
| 48 | foreach (var external in externals) |
| 49 | writer.External(external); |
| 50 | return writer.Build(); |
| 51 | } |
| 52 | |
| 53 | private static byte[] GlobalGameManagers(string unity = Unity, string product = "BattleBit", params string[] scenes) |
| 54 | { |
| 55 | return new SerializedFileWriter(unity, Windows) |
| 56 | .Object(11, new byte[12]) |
| 57 | .Object(129, SerializedFileWriter.PlayerSettings("BattleBitDevTeam", product, "3.0", 284)) |
| 58 | .Object(BuildSettings.Class, SerializedFileWriter.BuildSettings(scenes.Length > 0 ? scenes : Scenes)) |
| 59 | .External("globalgamemanagers.assets").External("resources.assets") |
| 60 | .Build(); |
| 61 | } |
| 62 | |
| 63 | private static MemorySource Install(string appInfo = "BattleBitDevTeam\nBattleBit", |
| 64 | byte[]? globalGameManagers = null, |
| 65 | string location = InstallPath) |
| 66 | { |
| 67 | return new MemorySource(location, CaseRule.Insensitive) |
| 68 | .Add("BattleBit.exe", "MZ") |
| 69 | .Add("BattleBitEAC.exe", "MZ") |
| 70 | .Add("GameAssembly.dll", Code()) |
| 71 | .Add("UnityPlayer.dll", "MZ") |
| 72 | .Add("English.lang", "Encoding=UTF-8") |
| 73 | .Add("EasyAntiCheat/Settings.json", "{}") |
| 74 | .Add($"{Data}/app.info", appInfo) |
| 75 | .Add($"{Data}/boot.config", "gfx-enable-gfx-jobs=1") |
| 76 | .Add($"{Data}/globalgamemanagers", globalGameManagers ?? GlobalGameManagers()) |
| 77 | .Add($"{Data}/globalgamemanagers.assets", Serialized("Library/unity default resources")) |
| 78 | .Add($"{Data}/globalgamemanagers.assets.resS", "video") |
| 79 | .Add($"{Data}/resources.assets", Serialized("globalgamemanagers.assets", "sharedassets0.assets")) |
| 80 | .Add($"{Data}/resources.assets.resS", "texels") |
| 81 | .Add($"{Data}/resources.resource", "FSB5") |
| 82 | .Add($"{Data}/sharedassets0.assets", Serialized("globalgamemanagers.assets")) |
| 83 | .Add($"{Data}/level0", Serialized("globalgamemanagers.assets", "sharedassets0.assets")) |
| 84 | .Add($"{Data}/level1", Serialized("sharedassets1.assets", "resources.assets")) |
| 85 | .Add($"{Data}/sharedassets1.assets", Serialized("resources.assets")) |
| 86 | .Add($"{Data}/level2", Serialized("sharedassets2.assets", "sharedassets1.assets", "resources.assets")) |
| 87 | .Add($"{Data}/level2.resS", "texels") |
| 88 | .Add($"{Data}/sharedassets2.assets", |
| 89 | Serialized("resources.assets", "Resources/unity_builtin_extra", "Library/unity default resources")) |
| 90 | .Add($"{Data}/sharedassets2.assets.resS", "texels") |
| 91 | .Add($"{Data}/sharedassets2.resource", "FSB5") |
| 92 | .Add($"{Data}/level3", Serialized("sharedassets3.assets", "sharedassets2.assets", "resources.assets")) |
| 93 | .Add($"{Data}/sharedassets3.assets", Serialized("sharedassets2.assets")) |
| 94 | .Add($"{Data}/level4", Serialized("sharedassets4.assets", "sharedassets2.assets")) |
| 95 | .Add($"{Data}/sharedassets4.assets", Serialized()) |
| 96 | .Add($"{Data}/Resources/unity default resources", Serialized()) |
| 97 | .Add($"{Data}/Resources/unity_builtin_extra", Serialized()) |
| 98 | .Add($"{Data}/il2cpp_data/Metadata/global-metadata.dat", "¯\u001b±ú") |
| 99 | .Add($"{Data}/Plugins/x86_64/steam_api64.dll", "MZ"); |
| 100 | } |
| 101 | |
| 102 | private static MountRegistry Registry() |
| 103 | { |
| 104 | var registry = new MountRegistry(); |
| 105 | registry.Add(new BattleBitGame()); |
| 106 | return registry; |
| 107 | } |
| 108 | |
| 109 | private static Mount Open(MemorySource install) |
| 110 | { |
| 111 | var registry = Registry(); |
| 112 | return registry.Open(Assert.Single(registry.Probe(install, install.Location))); |
| 113 | } |
| 114 | |
| 115 | private static string[] Files(Mount mount, string part) |
| 116 | { |
| 117 | return [.. mount.Part(part)!.DefiningFiles.Select(path => path.Value)]; |
| 118 | } |
| 119 | |
| 120 | private static Externals Steam(MemorySource install) |
| 121 | { |
| 122 | var depot = new DepotManifestWriter(671861, 9072711616476469134); |
| 123 | foreach (var entry in install.EnumerateAll()) |
| 124 | depot.File(entry.Path.Value.Replace('/', '\\'), install.ReadAllBytes(entry.Path)); |
| 125 | var steamapps = new MemorySource("/library/steamapps", CaseRule.Insensitive).Add("appmanifest_671860.acf", |
| 126 | """ |
| 127 | "AppState" |
| 128 | { |
| 129 | "appid" "671860" |
| 130 | "StateFlags" "4" |
| 131 | "installdir" "BattleBit Remastered" |
| 132 | "InstalledDepots" |
| 133 | { |
| 134 | "671861" |
| 135 | { |
| 136 | "manifest" "9072711616476469134" |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | """); |
| 141 | var depotcache = new MemorySource("/library/depotcache", CaseRule.Insensitive) |
| 142 | .Add("671861_9072711616476469134.manifest", depot.Build()); |
| 143 | return new Externals( |
| 144 | [ |
| 145 | new ResolvedExternal(SteamClaims.Steamapps, ExternalState.Resolved, ExternalFrom.Hint) |
| 146 | { Files = steamapps, Location = steamapps.Location }, |
| 147 | new ResolvedExternal(SteamClaims.Depotcache, ExternalState.Resolved, ExternalFrom.Hint) |
| 148 | { Files = depotcache, Location = depotcache.Location } |
| 149 | ]); |
| 150 | } |
| 151 | |
| 152 | [Fact] |
| 153 | public void ProbesTheVersionFromPlayerSettingsWithTheCodesLinkTimeAsEvidence() |
| 154 | { |
| 155 | var probe = Assert.Single(Registry().Probe(Install(), InstallPath)); |
| 156 | |
| 157 | Assert.Equal((new MountId("battlebit-remastered", "pc", "3.0"), ProbeStatus.Known), |
| 158 | (probe.Id, probe.Probe.Status)); |
| 159 | Assert.Equal( |
| 160 | [ |
| 161 | $"{Data}/app.info names BattleBitDevTeam's BattleBit", $"{Data} is Unity's player data", |
| 162 | "BattleBit.exe is the windows player build", "GameAssembly.dll was linked 2023-12-18 05:49:47 UTC", |
| 163 | $"{Data}/globalgamemanagers names version 3.0" |
| 164 | ], probe.Probe.Evidence); |
| 165 | Assert.Equal(["steamapps", "depotcache"], probe.Probe.Externals.Select(external => external.Name)); |
| 166 | } |
| 167 | |
| 168 | [Fact] |
| 169 | public void ProbesAUnityItHasNoLayoutForAsAnUnknownVariant() |
| 170 | { |
| 171 | var probe = Assert.Single(Registry().Probe(Install(globalGameManagers: GlobalGameManagers("2019.4.41f1")), |
| 172 | InstallPath)); |
| 173 | |
| 174 | Assert.Equal((new MountId("battlebit-remastered", "pc", "unknown"), ProbeStatus.UnknownVariant), |
| 175 | (probe.Id, probe.Probe.Status)); |
| 176 | Assert.Contains( |
| 177 | $"{Data}/globalgamemanagers names no version: PlayerSettings has no layout for Unity 2019.4.41f1", |
| 178 | probe.Probe.Evidence); |
| 179 | } |
| 180 | |
| 181 | [Fact] |
| 182 | public void ProbesSettingsNamingAnotherProductAsAnUnknownVariant() |
| 183 | { |
| 184 | var probe = Assert.Single(Registry().Probe(Install(globalGameManagers: GlobalGameManagers(product: "Other")), |
| 185 | InstallPath)); |
| 186 | |
| 187 | Assert.Equal(ProbeStatus.UnknownVariant, probe.Probe.Status); |
| 188 | Assert.Contains($"{Data}/globalgamemanagers names BattleBitDevTeam's Other, not the game app.info names", |
| 189 | probe.Probe.Evidence); |
| 190 | } |
| 191 | |
| 192 | [Fact] |
| 193 | public void DeclinesAnotherUnityGameATreeWithNoPlayerAndOneThatIsntUnity() |
| 194 | { |
| 195 | var withoutPlayer = Install(); |
| 196 | withoutPlayer.Remove("BattleBit.exe"); |
| 197 | |
| 198 | Assert.Empty(Registry().Probe(Install("ZeekerssRBLX\nLethal Company"), InstallPath)); |
| 199 | Assert.Empty(Registry().Probe(withoutPlayer, InstallPath)); |
| 200 | Assert.Empty(Registry().Probe(new MemorySource("other", CaseRule.Insensitive).Add("BattleBit.exe", "MZ"), |
| 201 | "other")); |
| 202 | } |
| 203 | |
| 204 | [Fact] |
| 205 | public void TheOtherUnityGameAdaptersDeclineIt() |
| 206 | { |
| 207 | Assert.Null(new SlimeRancherGame().Probe(Install())); |
| 208 | Assert.Null(new LethalCompanyGame().Probe(Install())); |
| 209 | Assert.Null(new ScheduleIGame().Probe(Install())); |
| 210 | } |
| 211 | |
| 212 | [Fact] |
| 213 | public void LayersTheInstallOnceWithTheCodeInItsBuildFiles() |
| 214 | { |
| 215 | var install = Install(); |
| 216 | var game = new BattleBitGame(); |
| 217 | |
| 218 | var plan = game.Open(game.Probe(install)!, Externals.None); |
| 219 | |
| 220 | var layer = Assert.Single(plan.Layers); |
| 221 | Assert.Equal(("install", "base"), (layer.Name, layer.Role)); |
| 222 | Assert.Equal( |
| 223 | [ |
| 224 | $"{Data}/app.info", $"{Data}/globalgamemanagers", "GameAssembly.dll", |
| 225 | $"{Data}/il2cpp_data/Metadata/global-metadata.dat" |
| 226 | ], |
| 227 | plan.BuildFiles.Select(path => path.Value)); |
| 228 | Assert.Empty(plan.Notes); |
| 229 | } |
| 230 | |
| 231 | [Fact] |
| 232 | public void ClosesSpawnablesOverTheFilesResourcesAssetsNames() |
| 233 | { |
| 234 | var spawnables = Open(Install()).Part("spawnables")!; |
| 235 | |
| 236 | Assert.Equal( |
| 237 | [ |
| 238 | $"{Data}/Resources/unity default resources", $"{Data}/globalgamemanagers", |
| 239 | $"{Data}/globalgamemanagers.assets", |
| 240 | $"{Data}/globalgamemanagers.assets.resS", $"{Data}/resources.assets", $"{Data}/resources.assets.resS", |
| 241 | $"{Data}/resources.resource", $"{Data}/sharedassets0.assets" |
| 242 | ], |
| 243 | spawnables.DefiningFiles.Select(path => path.Value)); |
| 244 | Assert.True(spawnables.Sha1Claims); |
| 245 | } |
| 246 | |
| 247 | [Fact] |
| 248 | public void GivesEachMapInTheSceneListAPartHoldingEverySpawnable() |
| 249 | { |
| 250 | var mount = Open(Install()); |
| 251 | var azagor = Files(mount, "map/Azagor"); |
| 252 | |
| 253 | Assert.Equal(["spawnables", "map/Dustydew", "map/Azagor"], mount.Parts.Select(part => part.Name)); |
| 254 | Assert.Equal( |
| 255 | [ |
| 256 | $"{Data}/Resources/unity default resources", $"{Data}/Resources/unity_builtin_extra", |
| 257 | $"{Data}/globalgamemanagers", |
| 258 | $"{Data}/globalgamemanagers.assets", $"{Data}/globalgamemanagers.assets.resS", $"{Data}/level3", |
| 259 | $"{Data}/resources.assets", $"{Data}/resources.assets.resS", $"{Data}/resources.resource", |
| 260 | $"{Data}/sharedassets0.assets", $"{Data}/sharedassets2.assets", $"{Data}/sharedassets2.assets.resS", |
| 261 | $"{Data}/sharedassets2.resource", $"{Data}/sharedassets3.assets" |
| 262 | ], azagor); |
| 263 | Assert.Contains($"{Data}/level2.resS", Files(mount, "map/Dustydew")); |
| 264 | Assert.Contains($"{Data}/sharedassets1.assets", Files(mount, "map/Dustydew")); |
| 265 | Assert.All(mount.Parts.Skip(1), part => Assert.Subset(part.DefiningFiles.ToHashSet(), |
| 266 | mount.Part("spawnables")!.DefiningFiles.ToHashSet())); |
| 267 | } |
| 268 | |
| 269 | [Fact] |
| 270 | public void FindsEachMapByTheSceneListRatherThanItsFileNumber() |
| 271 | { |
| 272 | var swapped = Install(globalGameManagers: GlobalGameManagers(scenes: |
| 273 | [ |
| 274 | "Assets/BattleBitRemastered/Scene/Intro.unity", "Assets/Scenes/MainMenu.unity", |
| 275 | "Assets/BattleBitRemastered/Maps/Azagor/Azagor.unity", |
| 276 | "Assets/BattleBitRemastered/Maps/Dustydew/Dustydew.unity", |
| 277 | "Assets/Scenes/LoadingScreen.unity" |
| 278 | ])); |
| 279 | |
| 280 | var mount = Open(swapped); |
| 281 | |
| 282 | Assert.Equal(["spawnables", "map/Azagor", "map/Dustydew"], mount.Parts.Select(part => part.Name)); |
| 283 | Assert.Contains($"{Data}/level2", Files(mount, "map/Azagor")); |
| 284 | Assert.Contains($"{Data}/level3", Files(mount, "map/Dustydew")); |
| 285 | } |
| 286 | |
| 287 | [Fact] |
| 288 | public void LeavesTheMenusCodeAntiCheatAndPlayerOutOfEveryPart() |
| 289 | { |
| 290 | var mount = Open(Install()); |
| 291 | |
| 292 | Assert.All(mount.Parts, part => Assert.DoesNotContain(part.DefiningFiles, path => |
| 293 | path.Value is $"{Data}/level0" or $"{Data}/level1" or $"{Data}/level4" or $"{Data}/sharedassets4.assets" or |
| 294 | $"{Data}/app.info" or $"{Data}/boot.config" || |
| 295 | path.Value.StartsWith($"{Data}/il2cpp_data/") || path.Value.StartsWith($"{Data}/Plugins/") || |
| 296 | !path.Value.StartsWith($"{Data}/"))); |
| 297 | } |
| 298 | |
| 299 | [Fact] |
| 300 | public void NotesASceneThatsNeitherAMapNorKnown() |
| 301 | { |
| 302 | var install = Install(globalGameManagers: GlobalGameManagers(scenes: |
| 303 | [ |
| 304 | "Assets/BattleBitRemastered/Scene/Intro.unity", "Assets/Scenes/MainMenu.unity", |
| 305 | "Assets/BattleBitRemastered/Maps/Dustydew/Dustydew.unity", |
| 306 | "Assets/BattleBitRemastered/Maps/Azagor/Lighting.unity", |
| 307 | "Assets/Scenes/Tutorial.unity" |
| 308 | ])); |
| 309 | |
| 310 | var mount = Open(install); |
| 311 | |
| 312 | Assert.Equal(["spawnables", "map/Dustydew"], mount.Parts.Select(part => part.Name)); |
| 313 | Assert.Equal( |
| 314 | [ |
| 315 | "The scene list names Assets/BattleBitRemastered/Maps/Azagor/Lighting.unity as level3, " + |
| 316 | "which is neither a map nor a scene the adapter knows, so it's in no part.", |
| 317 | "The scene list names Assets/Scenes/Tutorial.unity as level4, " + |
| 318 | "which is neither a map nor a scene the adapter knows, so it's in no part." |
| 319 | ], mount.Notes); |
| 320 | } |
| 321 | |
| 322 | [Fact] |
| 323 | public void NotesAFileATableNamesThatTheInstallDoesntHave() |
| 324 | { |
| 325 | var install = Install(); |
| 326 | install.Remove($"{Data}/sharedassets3.assets"); |
| 327 | |
| 328 | var mount = Open(install); |
| 329 | |
| 330 | Assert.Equal([$"{Data}/level3 names sharedassets3.assets, which isn't in the install."], mount.Notes); |
| 331 | Assert.DoesNotContain(Files(mount, "map/Azagor"), path => path.EndsWith("sharedassets3.assets")); |
| 332 | } |
| 333 | |
| 334 | [Fact] |
| 335 | public void NotesBepInExWithoutReadingItsMods() |
| 336 | { |
| 337 | var install = Install(); |
| 338 | install.Add("winhttp.dll", "MZ").Add("BepInEx/plugins/Mod/Mod.dll", "MZ"); |
| 339 | |
| 340 | var mount = Open(install); |
| 341 | |
| 342 | Assert.Equal( |
| 343 | [ |
| 344 | "BepInEx is BepInEx's, so the install is modded, and its mods aren't read.", |
| 345 | "winhttp.dll is BepInEx's, so the install is modded, and its mods aren't read." |
| 346 | ], mount.Notes); |
| 347 | Assert.All(mount.Parts, |
| 348 | part => Assert.DoesNotContain(part.DefiningFiles, path => path.Value.StartsWith("BepInEx/"))); |
| 349 | } |
| 350 | |
| 351 | [Fact] |
| 352 | public void ChangesTheBuildFingerprintWhenOnlyTheCodeIsRebuilt() |
| 353 | { |
| 354 | var rebuilt = Install(); |
| 355 | rebuilt.Add("GameAssembly.dll", Code(Linked + 3600)); |
| 356 | var remapped = Install(); |
| 357 | remapped.Add($"{Data}/il2cpp_data/Metadata/global-metadata.dat", "¯\u001b±ú\u0018"); |
| 358 | |
| 359 | var before = Open(Install()); |
| 360 | var after = Open(rebuilt); |
| 361 | var metadata = Open(remapped); |
| 362 | |
| 363 | Assert.Equal(before.Id, after.Id); |
| 364 | Assert.NotEqual(before.Build, after.Build); |
| 365 | Assert.NotEqual(before.Build, metadata.Build); |
| 366 | Assert.Equal(before.Part("map/Azagor")!.Fingerprint, after.Part("map/Azagor")!.Fingerprint); |
| 367 | } |
| 368 | |
| 369 | [Fact] |
| 370 | public void FingerprintsASteamCopyByItsClaimsAsACopyFromElsewhere() |
| 371 | { |
| 372 | var install = Install(); |
| 373 | var game = new BattleBitGame(); |
| 374 | |
| 375 | var steam = game.Open(game.Probe(install)!, Steam(install)); |
| 376 | var elsewhere = game.Open(game.Probe(Install(location: "/run/media/me/BattleBit Remastered"))!, Externals.None); |
| 377 | |
| 378 | Assert.All(steam.Parts, part => Assert.Equal(part.DefiningFiles.Count, part.Claims.Count)); |
| 379 | Assert.All(elsewhere.Parts, part => Assert.Empty(part.Claims)); |
| 380 | foreach (var (claimed, read) in steam.Parts.Zip(elsewhere.Parts)) |
| 381 | Assert.Equal(Fingerprint.Of(install, read.DefiningFiles, sha1Claims: true), |
| 382 | Fingerprint.Of(install, claimed.DefiningFiles, claims: claimed.Claims, sha1Claims: true)); |
| 383 | } |
| 384 | |
| 385 | [Fact] |
| 386 | public void MountsARealBattleBitInstall() |
| 387 | { |
| 388 | var install = Environment.GetEnvironmentVariable("EVIL_BATTLEBIT"); |
| 389 | Assert.SkipWhen(install is null, "Set EVIL_BATTLEBIT to a BattleBit Remastered install to read it."); |
| 390 | var registry = Registry(); |
| 391 | var probe = Assert.Single(registry.Probe(install!)); |
| 392 | Assert.Equal((new MountId("battlebit-remastered", "pc", "3.0"), ProbeStatus.Known), |
| 393 | (probe.Id, probe.Probe.Status)); |
| 394 | |
| 395 | var mount = registry.Open(probe); |
| 396 | |
| 397 | Assert.Empty(mount.Notes); |
| 398 | Assert.Equal(176, mount.Files.EnumerateAll().Count()); |
| 399 | Assert.Equal(10, mount.Part("spawnables")!.DefiningFiles.Count); |
| 400 | Assert.Equal(36, mount.Parts.Count); |
| 401 | Assert.All(mount.Parts.Skip(1), part => Assert.StartsWith("map/", part.Name)); |
| 402 | Assert.Equal(17, mount.Part("map/Dustydew")!.DefiningFiles.Count); |
| 403 | Assert.Equal(55, mount.Part("map/Polygon")!.DefiningFiles.Count); |
| 404 | Assert.All(mount.Parts, part => Assert.Equal(part.DefiningFiles.Count, part.Claims.Count)); |
| 405 | |
| 406 | var claims = mount.Parts.SelectMany(part => part.Claims).DistinctBy(pair => pair.Key).ToArray(); |
| 407 | Assert.Equal(102, claims.Length); |
| 408 | foreach (var (path, claim) in claims) |
| 409 | { |
| 410 | using var stream = mount.Files.Open(path); |
| 411 | Assert.Equal(claim, Convert.ToHexStringLower(SHA1.HashData(stream))); |
| 412 | } |
| 413 | |
| 414 | TestContext.Current.TestOutputHelper?.WriteLine($"Checked {claims.Length} claims against their files' SHA-1s."); |
| 415 | } |
| 416 | } |