0.2.0-dev Download ZIP
317 lines ·15.5 KB Raw
| 1 | using System.Diagnostics; |
| 2 | using EvilEngine.Adapters.Dbpf; |
| 3 | using EvilEngine.Adapters.Tests.Support; |
| 4 | using EvilEngine.Adapters.TheSims4; |
| 5 | using EvilEngine.Mounts; |
| 6 | |
| 7 | namespace EvilEngine.Adapters.Tests; |
| 8 | |
| 9 | public sealed class TheSims4Tests |
| 10 | { |
| 11 | private const string InstallPath = "/library/steamapps/common/The Sims 4"; |
| 12 | |
| 13 | private const uint Cas = 0x034AEECB; |
| 14 | |
| 15 | private const uint Tuning = 0x545AC67A; |
| 16 | |
| 17 | private const string DefaultIni = """ |
| 18 | [Config] |
| 19 | lotfadeduration = 0.5 |
| 20 | |
| 21 | [PlumbBob] |
| 22 | playeractivecolor = 0.43, 0.78, 0.12, 1.00 # green |
| 23 | |
| 24 | [Version] |
| 25 | cdsversion = 48.0 |
| 26 | gameversion = 1.129.39.1030 |
| 27 | codedatetime = 2026_10_14-14_34_08 |
| 28 | """; |
| 29 | |
| 30 | private const string ClientConfig = """ |
| 31 | Priority -15 |
| 32 | PackedFile ClipHeader.package |
| 33 | Priority -20 |
| 34 | PackedFile ClientDeltaBuild0.package |
| 35 | Priority -30 |
| 36 | PackedFile ClientFullBuild0.package |
| 37 | PackedFile Strings_*.package |
| 38 | Select CONSOLETRAY |
| 39 | PackedFile ConsoleTray.package |
| 40 | End |
| 41 | Priority -40 |
| 42 | PackedFile UI*.package |
| 43 | PackedFile magalog.package |
| 44 | """; |
| 45 | |
| 46 | private const string SimulationConfig = """ |
| 47 | Priority -20 |
| 48 | PackedFile SimulationDeltaBuild0.package |
| 49 | Priority -30 |
| 50 | PackedFile SimulationFullBuild0.package |
| 51 | """; |
| 52 | |
| 53 | private const string PackClient = """ |
| 54 | Priority -20 |
| 55 | PackedFile ClientDeltaBuild*.package |
| 56 | Priority -30 |
| 57 | PackedFile ClientFullBuild*.package |
| 58 | Priority -40 |
| 59 | PackedFile magalog.package |
| 60 | """; |
| 61 | |
| 62 | private const string PackSimulation = """ |
| 63 | Priority -20 |
| 64 | PackedFile SimulationDeltaBuild*.package |
| 65 | Priority -30 |
| 66 | PackedFile SimulationFullBuild*.package |
| 67 | Priority -40 |
| 68 | PackedFile magalog.package |
| 69 | """; |
| 70 | |
| 71 | private const string InstallerData = """ |
| 72 | <?xml version='1.0' encoding='utf-8'?> |
| 73 | <DiPManifest version="4.0"> |
| 74 | <gameTitles> |
| 75 | <gameTitle locale="en_US">The Sims 4 My First Pet Stuff</gameTitle> |
| 76 | <gameTitle locale="fr_FR">Les Sims 4 Kit d'Objets Premier animal de compagnie</gameTitle> |
| 77 | </gameTitles> |
| 78 | </DiPManifest> |
| 79 | """; |
| 80 | |
| 81 | private const int ExpectedClient = 866_133; |
| 82 | |
| 83 | private const int ExpectedSimulation = 138_881; |
| 84 | |
| 85 | private static string Key(uint type, ulong instance) |
| 86 | { |
| 87 | return new ResourceKey(type, 0, instance).ToString(); |
| 88 | } |
| 89 | |
| 90 | private static MemorySource Install(string? version = "1.129.39.1030") |
| 91 | { |
| 92 | var install = new MemorySource(InstallPath, CaseRule.Insensitive) |
| 93 | .Add("Game/Bin/Default.ini", |
| 94 | version is null |
| 95 | ? "[Version]\ncodeversion = 1.200.000.721\n" |
| 96 | : DefaultIni.Replace("1.129.39.1030", version)) |
| 97 | .Add("Game/Bin/TS4_x64.exe", "MZ") |
| 98 | .Add("Data/Client/Resource.cfg", ClientConfig) |
| 99 | .Add("Data/Client/Resource_LE.cfg", "Priority -30\nPackedFile ClientFullBuild0.package\n") |
| 100 | .Add("Data/Client/ClientFullBuild0.package", new DbpfWriter().Add(Cas, 0, 1, "full chair") |
| 101 | .Add(Cas, 0, 2, "full lamp") |
| 102 | .Add(Cas, 0, 3, "full sofa").Add(Tuning, 0, 9, "client tuning").Build()) |
| 103 | .Add("Data/Client/ClientDeltaBuild0.package", new DbpfWriter().Add(Cas, 0, 1, "delta chair") |
| 104 | .Delete(Cas, 0, 2) |
| 105 | .Add(Cas, 0, 7, "moved from the pack").Build()) |
| 106 | .Add("Data/Client/ClipHeader.package", new DbpfWriter().Add(0x6B20C4F3, 0, 1, "clip").Build()) |
| 107 | .Add("Data/Client/Strings_ENG_US.package", new DbpfWriter().Add(0x220557DA, 0, 1, "strings").Build()) |
| 108 | .Add("Data/Client/UI.package", new DbpfWriter().Add(0x62E94D38, 0, 1, "ui").Build()) |
| 109 | .Add("Data/Client/magalog.package", new DbpfWriter().Add(Cas, 0, 3, "magalog sofa").Build()) |
| 110 | .Add("Data/Client/tmp.txt", "") |
| 111 | .Add("Data/Simulation/Resource.cfg", SimulationConfig) |
| 112 | .Add("Data/Simulation/SimulationFullBuild0.package", |
| 113 | new DbpfWriter().Add(Tuning, 0, 9, "simulation tuning").Build()) |
| 114 | .Add("Data/Simulation/SimulationDeltaBuild0.package", |
| 115 | new DbpfWriter().Add(Tuning, 0, 10, "new tuning").Build()) |
| 116 | .Add("Data/Simulation/Gameplay/base.zip", "PK") |
| 117 | .Add("Data/Shared/Worlds/Areas/SO_DesertPark_01.world", "world") |
| 118 | .Add("SP14/ResourceClient.cfg", PackClient) |
| 119 | .Add("SP14/ResourceSimulation.cfg", PackSimulation) |
| 120 | .Add("SP14/ClientFullBuild0.package", new DbpfWriter().Add(Cas, 0, 5, "pet bed").Add(Cas, 0, 7, "pack copy") |
| 121 | .Add(Cas, 0, 8, "pet bowl").Build()) |
| 122 | .Add("SP14/SimulationFullBuild0.package", new DbpfWriter().Add(Tuning, 0, 11, "pet tuning").Build()) |
| 123 | .Add("SP14/magalog.package", new DbpfWriter().Add(0x0, 0, 1, "pack magalog").Build()) |
| 124 | .Add("SP14/Version.ini", "[Version]\npackversion = 26.0.20.1020\n") |
| 125 | .Add("Delta/SP14/ResourceClient.cfg", PackClient) |
| 126 | .Add("Delta/SP14/ResourceSimulation.cfg", PackSimulation) |
| 127 | .Add("Delta/SP14/ClientDeltaBuild0.package", |
| 128 | new DbpfWriter().Delete(Cas, 0, 7).Add(Cas, 0, 8, "pet bowl patched").Build()) |
| 129 | .Add("Delta/SP14/Stray.package", new DbpfWriter().Build()) |
| 130 | .Add("Delta/EP01/ResourceClient.cfg", PackClient) |
| 131 | .Add("Delta/EP01/ClientDeltaBuild0.package", new DbpfWriter().Delete(Cas, 0, 1).Build()) |
| 132 | .Add("Delta/GP01/ResourceClient.cfg", PackClient) |
| 133 | .Add("__Installer/DLC/SP14/__Installer/installerdata.xml", InstallerData); |
| 134 | return install; |
| 135 | } |
| 136 | |
| 137 | private static MountRegistry Registry() |
| 138 | { |
| 139 | var registry = new MountRegistry(); |
| 140 | registry.Add(new DbpfContainer()); |
| 141 | registry.Add(new TheSims4Game()); |
| 142 | return registry; |
| 143 | } |
| 144 | |
| 145 | private static Mount Open(MemorySource install) |
| 146 | { |
| 147 | var registry = Registry(); |
| 148 | var probe = registry.Probe(install, install.Location).Single(); |
| 149 | return registry.Open(probe, new TheSims4Game().Open(probe.Probe, Externals.None), Externals.None); |
| 150 | } |
| 151 | |
| 152 | [Fact] |
| 153 | public void ProbesTheGameVersionFromDefaultIni() |
| 154 | { |
| 155 | var probe = Assert.Single(Registry().Probe(Install(), InstallPath)); |
| 156 | |
| 157 | Assert.Equal(new MountId("the-sims-4", "pc", "1.129.39.1030"), probe.Id); |
| 158 | Assert.Equal(ProbeStatus.Known, probe.Probe.Status); |
| 159 | Assert.Contains("Game/Bin/Default.ini names game version 1.129.39.1030", probe.Probe.Evidence); |
| 160 | Assert.Contains(probe.Probe.Externals, external => external.Name == "steamapps"); |
| 161 | } |
| 162 | |
| 163 | [Fact] |
| 164 | public void ProbesAnUnknownBuildWithoutAGameVersion() |
| 165 | { |
| 166 | var probe = Assert.Single(Registry().Probe(Install(null), InstallPath)); |
| 167 | |
| 168 | Assert.Equal(new MountId("the-sims-4", "pc", "unknown"), probe.Id); |
| 169 | Assert.Equal(ProbeStatus.UnknownVariant, probe.Probe.Status); |
| 170 | } |
| 171 | |
| 172 | [Fact] |
| 173 | public void DeclinesAnotherGame() |
| 174 | { |
| 175 | var install = Install(); |
| 176 | install.Remove("Data/Simulation/Resource.cfg"); |
| 177 | |
| 178 | Assert.Empty(Registry().Probe(install, InstallPath)); |
| 179 | } |
| 180 | |
| 181 | [Fact] |
| 182 | public void LaysEveryPackageByItsPriorityWithDeletionsHidingWhatsBeneath() |
| 183 | { |
| 184 | var mount = Open(Install()); |
| 185 | |
| 186 | Assert.Equal("delta chair", mount.Files.ReadAllText($"client/{Key(Cas, 1)}")); |
| 187 | Assert.False(mount.Files.Exists($"client/{Key(Cas, 2)}")); |
| 188 | Assert.Equal("full sofa", mount.Files.ReadAllText($"client/{Key(Cas, 3)}")); |
| 189 | Assert.Equal("clip", mount.Files.ReadAllText($"client/{Key(0x6B20C4F3, 1)}")); |
| 190 | Assert.Equal("ui", mount.Files.ReadAllText($"client/{Key(0x62E94D38, 1)}")); |
| 191 | Assert.Equal("client/Data/Client/ClientDeltaBuild0.package (-20)", |
| 192 | mount.Origin($"client/{Key(Cas, 1)}")!.Layer.Name); |
| 193 | Assert.Equal( |
| 194 | [ |
| 195 | "client/Data/Client/ClipHeader.package (-15)", "client/Data/Client/ClientDeltaBuild0.package (-20)", |
| 196 | "client/Delta/SP14/ClientDeltaBuild0.package (-20)", "client/Data/Client/ClientFullBuild0.package (-30)", |
| 197 | "client/Data/Client/Strings_ENG_US.package (-30)", "client/SP14/ClientFullBuild0.package (-30)", |
| 198 | "client/Data/Client/UI.package (-40)", "client/Data/Client/magalog.package (-40)", |
| 199 | "client/SP14/magalog.package (-40)", |
| 200 | "simulation/Data/Simulation/SimulationDeltaBuild0.package (-20)", |
| 201 | "simulation/Data/Simulation/SimulationFullBuild0.package (-30)", |
| 202 | "simulation/SP14/SimulationFullBuild0.package (-30)", |
| 203 | "simulation/SP14/magalog.package (-40)", "install" |
| 204 | ], mount.Layers.Select(layer => layer.Name)); |
| 205 | } |
| 206 | |
| 207 | [Fact] |
| 208 | public void KeepsTheClientAndSimulationApart() |
| 209 | { |
| 210 | var mount = Open(Install()); |
| 211 | |
| 212 | Assert.Equal("client tuning", mount.Files.ReadAllText($"client/{Key(Tuning, 9)}")); |
| 213 | Assert.Equal("simulation tuning", mount.Files.ReadAllText($"simulation/{Key(Tuning, 9)}")); |
| 214 | Assert.Equal("new tuning", mount.Files.ReadAllText($"simulation/{Key(Tuning, 10)}")); |
| 215 | Assert.False(mount.Files.Exists($"client/{Key(Tuning, 10)}")); |
| 216 | Assert.Equal("pack magalog", mount.Files.ReadAllText($"simulation/{Key(0, 1)}")); |
| 217 | Assert.Equal("pack magalog", mount.Files.ReadAllText($"client/{Key(0, 1)}")); |
| 218 | } |
| 219 | |
| 220 | [Fact] |
| 221 | public void LaysAnInstalledPackAndItsUpdateWithTheBaseGameFirstAtEqualPriority() |
| 222 | { |
| 223 | var mount = Open(Install()); |
| 224 | |
| 225 | Assert.Equal("pet bed", mount.Files.ReadAllText($"client/{Key(Cas, 5)}")); |
| 226 | Assert.Equal("pet bowl patched", mount.Files.ReadAllText($"client/{Key(Cas, 8)}")); |
| 227 | Assert.Equal("moved from the pack", mount.Files.ReadAllText($"client/{Key(Cas, 7)}")); |
| 228 | Assert.Equal("pet tuning", mount.Files.ReadAllText($"simulation/{Key(Tuning, 11)}")); |
| 229 | Assert.DoesNotContain(mount.Layers, layer => layer.Name.Contains("EP01")); |
| 230 | Assert.Contains(mount.Notes, |
| 231 | note => note.StartsWith("Delta holds updates for 2 packs this copy doesn't have (EP01, GP01)")); |
| 232 | Assert.Contains("SP14 (The Sims 4 My First Pet Stuff) is installed and laid as part pack/SP14.", mount.Notes); |
| 233 | } |
| 234 | |
| 235 | [Fact] |
| 236 | public void NotesWhatTheConfigsLeaveOut() |
| 237 | { |
| 238 | var mount = Open(Install()); |
| 239 | |
| 240 | Assert.Contains(mount.Notes, note => note.Contains("CONSOLETRAY selection")); |
| 241 | Assert.Contains("Delta/SP14/Stray.package isn't named by its folder's resource configs, so it isn't laid.", |
| 242 | mount.Notes); |
| 243 | Assert.Contains("Data/Client/Resource_LE.cfg isn't read, as no executable that reads it has been seen.", |
| 244 | mount.Notes); |
| 245 | Assert.Contains("Data/Client/Resource.cfg line 2 names ClipHeader.package, which isn't in Data/Client.", |
| 246 | Open(InstallWithout("Data/Client/ClipHeader.package")).Notes); |
| 247 | } |
| 248 | |
| 249 | private static MemorySource InstallWithout(string path) |
| 250 | { |
| 251 | var install = Install(); |
| 252 | install.Remove(path); |
| 253 | return install; |
| 254 | } |
| 255 | |
| 256 | [Fact] |
| 257 | public void NamesTheBaseGameAndEachPackAsParts() |
| 258 | { |
| 259 | var mount = Open(Install()); |
| 260 | |
| 261 | Assert.Equal(["base-game", "pack/SP14"], mount.Parts.Select(part => part.Name)); |
| 262 | Assert.Equal( |
| 263 | [ |
| 264 | "Data/Client/ClientDeltaBuild0.package", "Data/Client/ClientFullBuild0.package", |
| 265 | "Data/Client/ClipHeader.package", |
| 266 | "Data/Client/Resource.cfg", "Data/Client/Strings_ENG_US.package", "Data/Client/UI.package", |
| 267 | "Data/Client/magalog.package", "Data/Shared/Worlds/Areas/SO_DesertPark_01.world", |
| 268 | "Data/Simulation/Gameplay/base.zip", |
| 269 | "Data/Simulation/Resource.cfg", "Data/Simulation/SimulationDeltaBuild0.package", |
| 270 | "Data/Simulation/SimulationFullBuild0.package" |
| 271 | ], mount.Part("base-game")!.DefiningFiles.Select(path => path.Value)); |
| 272 | Assert.Equal( |
| 273 | [ |
| 274 | "Delta/SP14/ClientDeltaBuild0.package", "Delta/SP14/ResourceClient.cfg", |
| 275 | "Delta/SP14/ResourceSimulation.cfg", |
| 276 | "SP14/ClientFullBuild0.package", "SP14/ResourceClient.cfg", "SP14/ResourceSimulation.cfg", |
| 277 | "SP14/SimulationFullBuild0.package", "SP14/magalog.package" |
| 278 | ], mount.Part("pack/SP14")!.DefiningFiles.Select(path => path.Value)); |
| 279 | } |
| 280 | |
| 281 | [Fact] |
| 282 | public void ReadsARealInstall() |
| 283 | { |
| 284 | var install = Environment.GetEnvironmentVariable("EVIL_SIMS4"); |
| 285 | Assert.SkipWhen(install is null, "Set EVIL_SIMS4 to a The Sims 4 install to read it."); |
| 286 | var registry = Registry(); |
| 287 | var probe = Assert.Single(registry.Probe(install!)); |
| 288 | Assert.Equal(new MountId("the-sims-4", "pc", "1.129.39.1030"), probe.Id); |
| 289 | |
| 290 | var timer = Stopwatch.StartNew(); |
| 291 | var mount = registry.Open(probe); |
| 292 | var opened = timer.Elapsed; |
| 293 | var client = mount.Files.Enumerate("client", true).Count(); |
| 294 | var simulation = mount.Files.Enumerate("simulation", true).Count(); |
| 295 | var merged = timer.Elapsed; |
| 296 | |
| 297 | Assert.Equal(["base-game", "pack/SP14"], mount.Parts.Select(part => part.Name)); |
| 298 | Assert.All(mount.Parts, part => Assert.Equal(part.DefiningFiles.Count, part.Claims.Count)); |
| 299 | Assert.DoesNotContain(mount.Notes, |
| 300 | note => note.Contains("hash from their bytes") || note.Contains("hashes from")); |
| 301 | Assert.Equal(34 + 9 + 1, mount.Layers.Count); |
| 302 | Assert.Equal(ExpectedClient, client); |
| 303 | Assert.Equal(ExpectedSimulation, simulation); |
| 304 | |
| 305 | var decoded = 0; |
| 306 | foreach (var entry in mount.Files.Enumerate("client", true).Concat(mount.Files.Enumerate("simulation", true)) |
| 307 | .Where((_, at) => at % 997 == 0)) |
| 308 | { |
| 309 | using var stream = mount.Files.Open(entry.Path); |
| 310 | Assert.Equal(entry.Length, stream.CopyToCount()); |
| 311 | decoded++; |
| 312 | } |
| 313 | |
| 314 | TestContext.Current.TestOutputHelper?.WriteLine( |
| 315 | $"Opened in {opened.TotalMilliseconds:F0} ms, merged {client} client and {simulation} simulation resources by {merged.TotalMilliseconds:F0} ms, decoded {decoded}; {GC.GetTotalMemory(false) >> 20} MB held."); |
| 316 | } |
| 317 | } |