0.2.0-dev Download ZIP
496 lines ·24.0 KB Raw
| 1 | using System.Buffers.Binary; |
| 2 | using System.Collections.Concurrent; |
| 3 | using System.Numerics; |
| 4 | using System.Reflection; |
| 5 | using System.Text; |
| 6 | using EvilEngine.Conversion; |
| 7 | using EvilEngine.Formats; |
| 8 | using EvilEngine.Formats.Maps; |
| 9 | using EvilEngine.Formats.Materials; |
| 10 | using EvilEngine.Formats.Models; |
| 11 | using EvilEngine.Formats.Records; |
| 12 | using EvilEngine.Formats.Textures; |
| 13 | using EvilEngine.Host.Liquids; |
| 14 | using EvilEngine.Mounts; |
| 15 | |
| 16 | namespace EvilEngine.Host.Tests; |
| 17 | |
| 18 | public sealed class SourceMapTests(ITestOutputHelper output) |
| 19 | { |
| 20 | private static readonly string Installed = typeof(SourceMapTests).Assembly |
| 21 | .GetCustomAttributes<AssemblyMetadataAttribute>() |
| 22 | .Single(attribute => attribute.Key == "AdapterOutputRoot").Value!; |
| 23 | |
| 24 | private static EngineHost Open() |
| 25 | { |
| 26 | var path = Environment.GetEnvironmentVariable("EVIL_GMOD"); |
| 27 | Assert.SkipWhen(path is null, "Set EVIL_GMOD to a Garry's Mod install to convert its maps."); |
| 28 | var host = new EngineHost(); |
| 29 | host.Adapters.LoadAll([Installed]); |
| 30 | var mount = host.Mounts.Open(Assert.Single(host.Mounts.Probe(path!))); |
| 31 | host.Conversion.AddNamespace(ContentNamespace.For(mount)); |
| 32 | return host; |
| 33 | } |
| 34 | |
| 35 | private static ConvertedContent Convert(EngineHost host, ContentKind kind, string id) |
| 36 | { |
| 37 | var result = host.Conversion.Get(kind, ContentId.Parse(id)); |
| 38 | Assert.True(result.Succeeded, string.Join('\n', result.Diagnostics)); |
| 39 | return result.Content!; |
| 40 | } |
| 41 | |
| 42 | private static string Text(ConvertedContent content) |
| 43 | { |
| 44 | return Encoding.UTF8.GetString(content.Data.Span); |
| 45 | } |
| 46 | |
| 47 | private static (int Triangles, double Agreeing) Facing(Model model) |
| 48 | { |
| 49 | int triangles = 0, agreeing = 0; |
| 50 | foreach (var primitive in model.Meshes.SelectMany(mesh => mesh.Primitives)) |
| 51 | { |
| 52 | var p = primitive.Vertices.Positions; |
| 53 | var n = primitive.Vertices.Normals!; |
| 54 | for (var t = 0; t < primitive.Indices.Length; t += 3) |
| 55 | { |
| 56 | var (a, b, c) = (primitive.Indices[t], primitive.Indices[t + 1], primitive.Indices[t + 2]); |
| 57 | var face = Vector3.Cross(p[b] - p[a], p[c] - p[a]); |
| 58 | if (face.LengthSquared() < 1e-12f) |
| 59 | continue; |
| 60 | triangles++; |
| 61 | if (Vector3.Dot(face, n[a] + n[b] + n[c]) > 0) |
| 62 | agreeing++; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | return (triangles, agreeing / (double)triangles); |
| 67 | } |
| 68 | |
| 69 | [Fact] |
| 70 | public void ConvertsGmConstruct() |
| 71 | { |
| 72 | var host = Open(); |
| 73 | |
| 74 | var manifest = MapText.ReadManifest(Text(Convert(host, ContentKind.Map, "gmod:maps/gm_construct")), |
| 75 | "gm_construct"); |
| 76 | var cellContent = Convert(host, ContentKind.Cell, "gmod:maps/gm_construct/cells/0"); |
| 77 | var cell = MapText.ReadCell(Text(cellContent), "gm_construct/cells/0"); |
| 78 | |
| 79 | output.WriteLine(string.Join('\n', cellContent.Diagnostics)); |
| 80 | Assert.Equal("gmod:maps/gm_construct/cells/0", Assert.Single(manifest.Cells).Id.ToString()); |
| 81 | Assert.Equal(new MapLightmap(ContentId.Parse("gmod:maps/gm_construct/lightmaps/ldr"), |
| 82 | ContentId.Parse("gmod:maps/gm_construct/lightmaps/hdr")), manifest.Lightmap); |
| 83 | Assert.Equal(ContentId.Parse("gmod:maps/gm_construct/sky"), manifest.Sky); |
| 84 | var sky = SkyText.Read(Text(Convert(host, ContentKind.Sky, "gmod:maps/gm_construct/sky")), "sky"); |
| 85 | Assert.Equal(ContentId.Parse("gmod:maps/gm_construct/sky/dome"), |
| 86 | Assert.IsType<SkyLayer.Dome>(Assert.Single(sky.Layers)).Material); |
| 87 | var dome = MaterialText.Read(Text(Convert(host, ContentKind.Material, "gmod:maps/gm_construct/sky/dome")), |
| 88 | "dome"); |
| 89 | Assert.Equal("source:shaders/g_sky", dome.Shader.ToString()); |
| 90 | Assert.Equal("gmod:materials/skybox/clouds", dome.Textures["startexture"].ToString()); |
| 91 | Assert.NotNull(sky.Sun); |
| 92 | Assert.NotNull(sky.Fog); |
| 93 | Assert.Equal(16, manifest.Skybox!.Scale); |
| 94 | Assert.NotNull(manifest.Skybox.Fog!.Blend); |
| 95 | Assert.Equal(1227, cell.Entities.Count); |
| 96 | Assert.Equal("gmod:worldspawn", cell.Entities[0].Class.ToString()); |
| 97 | Assert.Contains(new KeyValuePair<string, string>("skyname", "painted"), cell.Entities[0].Properties); |
| 98 | Assert.Equal(33, cell.Spawns.Count); |
| 99 | Assert.All(cell.Spawns, |
| 100 | spawn => Assert.Equal((false, SpawnRole.Single, 0), (spawn.Start, spawn.Role, spawn.Teams.Count))); |
| 101 | Assert.All(cell.Spawns, spawn => Assert.Equal("gmod:info_player_start", |
| 102 | cell.Entities.Single(entity => entity.Id == spawn.Owner).Class.ToString())); |
| 103 | var props = cell.Models.Where(place => place.Model.Name.StartsWith("models/", StringComparison.Ordinal)) |
| 104 | .ToArray(); |
| 105 | Assert.Equal(157, props.Count(place => place.Skybox && place.Owner is null)); |
| 106 | Assert.Equal(182, props.Count(place => place.Owner is null)); |
| 107 | Assert.Contains(cell.Models, place => place.Model.ToString() == "gmod:maps/gm_construct/models/0" && |
| 108 | place.Lightmap && !place.Skybox); |
| 109 | Assert.Contains(cell.Models, place => place.Model.ToString() == "gmod:maps/gm_construct/skybox" && |
| 110 | place.Lightmap && place.Skybox); |
| 111 | var brush = cell.Entities.First(entity => entity.Class.ToString() == "gmod:func_brush"); |
| 112 | var placed = Assert.Single(cell.Models, place => place.Owner == brush.Id); |
| 113 | Assert.Equal("gmod:maps/gm_construct/models/1", placed.Model.ToString()); |
| 114 | Assert.Equal(new MapVector(-91.44, -1.3208, -20.4216), placed.Transform.Position); |
| 115 | Assert.Contains(cell.Solids, solid => solid.Owner == brush.Id); |
| 116 | var button = cell.Entities.Single(entity => entity.Class.ToString() == "gmod:func_button"); |
| 117 | Assert.DoesNotContain(cell.Models, place => place.Owner == button.Id); |
| 118 | Assert.Contains(cell.Solids, solid => solid.Owner == button.Id); |
| 119 | Assert.Contains(cell.Solids, solid => solid.Contents.Contains(SolidContents.Water)); |
| 120 | Assert.Contains(cell.Solids, solid => solid.Contents.Contains(SolidContents.Ladder)); |
| 121 | Assert.All(cell.Solids, solid => Assert.True(solid.Sides.Count >= 4)); |
| 122 | Assert.Contains(cell.Solids.SelectMany(solid => solid.Sides), |
| 123 | side => side.Material.Name.StartsWith("maps/gm_construct/pak/materials/maps/gm_construct/", |
| 124 | StringComparison.Ordinal)); |
| 125 | } |
| 126 | |
| 127 | [Fact] |
| 128 | public void NamesGmConstructsWaterAsALiquidFromItsSurfaceMaterial() |
| 129 | { |
| 130 | var host = Open(); |
| 131 | |
| 132 | var cell = MapText.ReadCell(Text(Convert(host, ContentKind.Cell, "gmod:maps/gm_construct/cells/0")), "cell"); |
| 133 | |
| 134 | var water = Assert.Single(cell.Solids, solid => solid.Contents.Contains(SolidContents.Water)); |
| 135 | const string id = |
| 136 | "gmod:maps/gm_construct/pak/materials/maps/gm_construct/gm_construct/water_13_1344_3584_32/liquid"; |
| 137 | Assert.Equal(new Liquid { Base = ContentId.Parse(id) }, water.Liquid); |
| 138 | Assert.Contains(new ContentReference(ContentKind.Record, ContentId.Parse(id)), |
| 139 | Convert(host, ContentKind.Cell, "gmod:maps/gm_construct/cells/0").References); |
| 140 | Assert.All(cell.Solids.Where(solid => SolidContents.Liquid(solid.Contents) is null), |
| 141 | solid => Assert.Null(solid.Liquid)); |
| 142 | var record = |
| 143 | Assert.Single(RecordText.Read("gmod", Text(Convert(host, ContentKind.Record, id)), "liquid").Liquids); |
| 144 | output.WriteLine(RecordText.Write(new RecordSet([]) { Liquids = [record] })); |
| 145 | Assert.Equal(ContentId.Parse(id), record.Id); |
| 146 | Assert.Null(record.Liquid.Class); |
| 147 | Assert.Equal(ContentId.Parse("gmod:surfaces/water"), record.Liquid.Surface); |
| 148 | var under = record.Liquid.Under!; |
| 149 | Assert.Equal(new MapFog(MapFogMode.Linear, new MapVector(0.009134061, 0.051269462, 0.064803271)) |
| 150 | { Start = -26.0096, End = 52.0192, Curve = 2 }, under.Fog); |
| 151 | Assert.Equal(ContentId.Parse("gmod:materials/effects/water_warp01"), under.Overlay); |
| 152 | Assert.Equal(ContentId.Parse("gmod:dsp/14"), under.Effect); |
| 153 | var resolved = EngineLiquids.Resolve(water.Liquid!, SolidContents.Liquid(water.Contents), _ => record.Liquid, |
| 154 | message => Assert.Fail(message)); |
| 155 | Assert.Equal("water", resolved.Class); |
| 156 | Assert.Equal(1000, resolved.Density); |
| 157 | |
| 158 | var space = LiquidSpace.Build([cell], found => found == record.Id ? record.Liquid : null, |
| 159 | message => Assert.Fail(message)); |
| 160 | var hit = space.At(new MapVector(4000 * 0.0254, -200 * 0.0254, 0))!; |
| 161 | Assert.Equal(40 * 0.0254, hit.Depth, 6); |
| 162 | Assert.Equal(-160 * 0.0254, Assert.Single(space.Surfaces).Height, 6); |
| 163 | Assert.Equal(record.Liquid.Under, hit.Liquid.Under); |
| 164 | Assert.Null(space.At(new MapVector(4000 * 0.0254, -150 * 0.0254, 0))); |
| 165 | Assert.Equal(LiquidLevel.Waist, |
| 166 | space.Hull(new MapVector(4000 * 0.0254, -200 * 0.0254, 0), 72 * 0.0254, 64 * 0.0254).Level); |
| 167 | } |
| 168 | |
| 169 | [Fact] |
| 170 | public void ConvertsGmConstructsWorldFacingOutWithItsLightmaps() |
| 171 | { |
| 172 | var host = Open(); |
| 173 | |
| 174 | var world = ModelFile.Read(Convert(host, ContentKind.Model, "gmod:maps/gm_construct/models/0").Data, "world"); |
| 175 | var sky = ModelFile.Read(Convert(host, ContentKind.Model, "gmod:maps/gm_construct/skybox").Data, "skybox"); |
| 176 | var ldr = TextureFile.Read(Convert(host, ContentKind.Texture, "gmod:maps/gm_construct/lightmaps/ldr").Data, |
| 177 | "ldr"); |
| 178 | var hdr = TextureFile.Read(Convert(host, ContentKind.Texture, "gmod:maps/gm_construct/lightmaps/hdr").Data, |
| 179 | "hdr"); |
| 180 | |
| 181 | foreach (var model in new[] { world, sky }) |
| 182 | { |
| 183 | var (triangles, agreeing) = Facing(model); |
| 184 | output.WriteLine($"{triangles} triangles, {agreeing:P2} facing their normals"); |
| 185 | Assert.True(agreeing > 0.99, $"Only {agreeing:P} of the triangles face the way their normals do."); |
| 186 | } |
| 187 | |
| 188 | Assert.Equal((ldr.Width, ldr.Height), (hdr.Width, hdr.Height)); |
| 189 | Assert.Equal(TextureHdr.SourceLightmap, hdr.Hdr); |
| 190 | output.WriteLine($"lightmap pages {ldr.Width}x{ldr.Height}"); |
| 191 | var primitives = world.Meshes.SelectMany(mesh => mesh.Primitives).ToArray(); |
| 192 | Assert.Contains(primitives, primitive => primitive.Vertices.Colors is not null); |
| 193 | var styles = primitives.SelectMany(primitive => primitive.Vertices.CustomAttributes) |
| 194 | .Where(attribute => attribute.Name == "_LIGHT_STYLES").SelectMany(attribute => attribute.Values) |
| 195 | .ToHashSet(); |
| 196 | Assert.Equal([-1, 0, 32, 33], styles.Order()); |
| 197 | Assert.All(primitives.SelectMany(primitive => primitive.Vertices.Uv1!), |
| 198 | uv => Assert.True(uv is { X: >= 0 and <= 1, Y: >= 0 and <= 1 }, $"Lightmap UV {uv} leaves the page.")); |
| 199 | } |
| 200 | |
| 201 | [Fact] |
| 202 | public void LightsEveryStaticPropOfGmConstructFromItsOwnVertexLight() |
| 203 | { |
| 204 | var host = Open(); |
| 205 | var cell = MapText.ReadCell(Text(Convert(host, ContentKind.Cell, "gmod:maps/gm_construct/cells/0")), "cell"); |
| 206 | var lit = cell.Models.Where(place => place.Light is not null).ToArray(); |
| 207 | |
| 208 | Assert.Equal(182, lit.Length); |
| 209 | foreach (var place in lit) |
| 210 | { |
| 211 | var texture = TextureFile.Read(Convert(host, ContentKind.Texture, place.Light!.Value.ToString()).Data, |
| 212 | "light"); |
| 213 | var model = ModelFile.Read(Convert(host, ContentKind.Model, place.Model.ToString()).Data, "model"); |
| 214 | var primitives = model.Meshes.SelectMany(mesh => mesh.Primitives).ToArray(); |
| 215 | Assert.Equal(primitives.Length, texture.Height); |
| 216 | Assert.Equal(primitives.Max(primitive => primitive.Vertices.Positions.Length), texture.Width); |
| 217 | } |
| 218 | |
| 219 | output.WriteLine($"{lit.Length} static props lit by their own per-vertex light."); |
| 220 | } |
| 221 | |
| 222 | [Fact] |
| 223 | public void AddsGmConstructsSwitchableSunToThePropsThatSeeItButNotTheRest() |
| 224 | { |
| 225 | var host = Open(); |
| 226 | |
| 227 | double Brightest(int prop) |
| 228 | { |
| 229 | var texture = TextureFile.Read( |
| 230 | Convert(host, ContentKind.Texture, $"gmod:maps/gm_construct/props/{prop}/light").Data, "light"); |
| 231 | var texels = texture.Image(0).Span; |
| 232 | var brightest = 0.0; |
| 233 | for (var at = 0; at < texels.Length; at += 2) |
| 234 | { |
| 235 | var value = (double)BinaryPrimitives.ReadHalfLittleEndian(texels[at..]); |
| 236 | Assert.True(double.IsFinite(value), $"prop {prop} holds {value} at byte {at}."); |
| 237 | if (at % 8 == 0) |
| 238 | brightest = Math.Max(brightest, value); |
| 239 | } |
| 240 | |
| 241 | return brightest; |
| 242 | } |
| 243 | |
| 244 | for (var prop = 0; prop < 182; prop++) |
| 245 | Brightest(prop); |
| 246 | |
| 247 | var sun = 1.5658; |
| 248 | Assert.InRange(Brightest(34), sun * 0.829, sun + 0.23); |
| 249 | Assert.InRange(Brightest(22), sun * 0.4, sun + 0.21); |
| 250 | Assert.InRange(Brightest(18), 0, 0.31); |
| 251 | Assert.InRange(Brightest(168), 0, 0.39); |
| 252 | } |
| 253 | |
| 254 | private static WorldLight[] WorldLights(string bsp) |
| 255 | { |
| 256 | var data = File.ReadAllBytes(bsp); |
| 257 | |
| 258 | float F32(int at) |
| 259 | { |
| 260 | return BinaryPrimitives.ReadSingleLittleEndian(data.AsSpan(at)); |
| 261 | } |
| 262 | |
| 263 | int I32(int at) |
| 264 | { |
| 265 | return BinaryPrimitives.ReadInt32LittleEndian(data.AsSpan(at)); |
| 266 | } |
| 267 | |
| 268 | Vector3 Vec3(int at) |
| 269 | { |
| 270 | return new Vector3(F32(at), F32(at + 4), F32(at + 8)); |
| 271 | } |
| 272 | |
| 273 | var (offset, length, version) = (I32(8 + 54 * 16), I32(8 + 54 * 16 + 4), I32(8 + 54 * 16 + 8)); |
| 274 | Assert.Equal(0, version); |
| 275 | return Enumerable.Range(0, length / 88).Select(i => offset + i * 88).Select(at => new WorldLight(Vec3(at), |
| 276 | Vec3(at + 12), Vec3(at + 24), I32(at + 40), F32(at + 52), F32(at + 60), F32(at + 64), F32(at + 68), |
| 277 | F32(at + 72))).ToArray(); |
| 278 | } |
| 279 | |
| 280 | [Fact] |
| 281 | public void ConvertsGmConstructsWorldLightsAsBakedLightsOwnedByTheirEntities() |
| 282 | { |
| 283 | var host = Open(); |
| 284 | var cell = MapText.ReadCell(Text(Convert(host, ContentKind.Cell, "gmod:maps/gm_construct/cells/0")), "cell"); |
| 285 | var bsp = WorldLights(Path.Combine(Environment.GetEnvironmentVariable("EVIL_GMOD")!, |
| 286 | "garrysmod/maps/gm_construct.bsp")); |
| 287 | |
| 288 | Assert.Equal(138, bsp.Length); |
| 289 | Assert.Equal(136, cell.Lights.Count); |
| 290 | Assert.All(cell.Lights, light => Assert.True(light.Baked)); |
| 291 | Assert.Equal(108, cell.Lights.Count(light => light.Kind == MapLightKind.Point)); |
| 292 | Assert.Equal(28, cell.Lights.Count(light => light.Kind == MapLightKind.Spot)); |
| 293 | foreach (var light in cell.Lights) |
| 294 | Assert.Equal(light.Kind == MapLightKind.Spot ? "gmod:light_spot" : "gmod:light", |
| 295 | cell.Entities.Single(entity => entity.Id == light.Owner).Class.ToString()); |
| 296 | Assert.Equal(cell.Lights.Count, cell.Lights.Select(light => light.Owner).Distinct().Count()); |
| 297 | |
| 298 | static MapVector Axes(Vector3 v) |
| 299 | { |
| 300 | return new MapVector(v.Y, v.Z, v.X); |
| 301 | } |
| 302 | |
| 303 | static MapVector Meters(Vector3 v) |
| 304 | { |
| 305 | return Axes(v * 0.0254f); |
| 306 | } |
| 307 | |
| 308 | void Check(WorldLight source) |
| 309 | { |
| 310 | var at = Meters(source.Origin); |
| 311 | var light = Assert.Single(cell.Lights, light => |
| 312 | Math.Abs(light.Transform.Position.X - at.X) + Math.Abs(light.Transform.Position.Y - at.Y) + |
| 313 | Math.Abs(light.Transform.Position.Z - at.Z) < 1e-4); |
| 314 | var owner = cell.Entities.Single(entity => entity.Id == light.Owner); |
| 315 | var origin = owner.Transform.Position; |
| 316 | Assert.True(Math.Abs(origin.X - at.X) + Math.Abs(origin.Y - at.Y) + Math.Abs(origin.Z - at.Z) < 1e-4); |
| 317 | |
| 318 | var reference = Math.Min(100, source.Radius > 0 ? source.Radius : 100); |
| 319 | var falloff = source.Constant + source.Linear * reference + source.Quadratic * reference * reference; |
| 320 | var meters = reference * 0.0254; |
| 321 | var colour = source.Intensity / falloff; |
| 322 | Assert.Equal(colour.X, light.Colour.X / (meters * meters), 4); |
| 323 | Assert.Equal(colour.Y, light.Colour.Y / (meters * meters), 4); |
| 324 | Assert.Equal(colour.Z, light.Colour.Z / (meters * meters), 4); |
| 325 | var brightest = Math.Max(light.Colour.X, Math.Max(light.Colour.Y, light.Colour.Z)); |
| 326 | var faded = Math.Sqrt(brightest * 255); |
| 327 | Assert.Equal(source.Radius > 0 ? Math.Min(faded, source.Radius * 0.0254) : faded, light.Range, 6); |
| 328 | |
| 329 | if (light.Kind != MapLightKind.Spot) |
| 330 | return; |
| 331 | Assert.Equal(Math.Min(89.9, Math.Acos(source.StopDot2) * 180 / Math.PI), light.Cone!.Value, 4); |
| 332 | var m = MapAngles.ToBasis(light.Transform.Angles); |
| 333 | var facing = Axes(source.Normal); |
| 334 | Assert.Equal(-facing.X, m[0, 2], 4); |
| 335 | Assert.Equal(-facing.Y, m[1, 2], 4); |
| 336 | Assert.Equal(-facing.Z, m[2, 2], 4); |
| 337 | } |
| 338 | |
| 339 | Check(bsp.First(light => light is { Type: 1, Quadratic: 1, Linear: 0, Radius: 0 })); |
| 340 | Check(bsp.First(light => light is { Type: 1, Linear: 1, Radius: 512 })); |
| 341 | Check(bsp.First(light => light is { Type: 1, Linear: 1, Quadratic: 2 })); |
| 342 | Check(bsp.First(light => light is { Type: 2, Quadratic: 1, Linear: 0, Radius: 0 })); |
| 343 | Check(bsp.First(light => light is { Type: 2, Linear: 1, Radius: 512 })); |
| 344 | var widest = bsp.Single(light => light is { Type: 2, Constant: > 0 }); |
| 345 | Check(widest); |
| 346 | Assert.Equal(89.9, cell.Lights.Max(light => light.Cone ?? 0)); |
| 347 | |
| 348 | output.WriteLine($"{cell.Lights.Count} of {bsp.Length} world lights converted, the rest the sky's."); |
| 349 | } |
| 350 | |
| 351 | [Fact] |
| 352 | public void PlacesGmConstructsSoundscapesEachNamingOneTheGameDefines() |
| 353 | { |
| 354 | var host = Open(); |
| 355 | |
| 356 | var cell = MapText.ReadCell(Text(Convert(host, ContentKind.Cell, "gmod:maps/gm_construct/cells/0")), |
| 357 | "gm_construct/cells/0"); |
| 358 | var defined = RecordText.Read("gmod", Text(Convert(host, ContentKind.Record, "gmod:records/soundscapes")), |
| 359 | "soundscapes").Soundscapes.Select(record => record.Id).ToHashSet(); |
| 360 | |
| 361 | var classes = cell.Entities.ToDictionary(entity => entity.Id, entity => entity.Class.Name); |
| 362 | foreach (var ambience in cell.Ambience) |
| 363 | output.WriteLine($"{ambience.Owner} {ambience.Soundscape} at {ambience.At} radius " + |
| 364 | $"{ambience.Radius?.ToString() ?? "none"}, {ambience.Positions.Count} positions" + |
| 365 | (ambience.Enabled ? "" : ", disabled")); |
| 366 | Assert.Equal(19, cell.Ambience.Count); |
| 367 | Assert.All(cell.Ambience, ambience => Assert.Equal("env_soundscape", classes[ambience.Owner!])); |
| 368 | Assert.All(cell.Ambience, ambience => Assert.Contains(ambience.Soundscape, defined)); |
| 369 | Assert.Equal(["gmod:construct.garage", "gmod:construct.inside", "gmod:construct.lake", "gmod:construct.outside"], |
| 370 | cell.Ambience.Select(ambience => ambience.Soundscape.ToString()).Distinct().Order()); |
| 371 | Assert.Contains(cell.Ambience, |
| 372 | ambience => ambience.Soundscape == ContentId.Parse("gmod:construct.outside") && ambience.Radius is null); |
| 373 | } |
| 374 | |
| 375 | [Fact] |
| 376 | public void ConvertsTheSameMapToTheSameBytes() |
| 377 | { |
| 378 | string[] ids = |
| 379 | [ |
| 380 | "map:gmod:maps/gm_construct", "cell:gmod:maps/gm_construct/cells/0", |
| 381 | "model:gmod:maps/gm_construct/models/0", |
| 382 | "texture:gmod:maps/gm_construct/lightmaps/hdr" |
| 383 | ]; |
| 384 | |
| 385 | string Hashes() |
| 386 | { |
| 387 | var host = Open(); |
| 388 | return string.Join(' ', ids.Select(id => |
| 389 | Convert(host, new ContentKind(id[..id.IndexOf(':')]), id[(id.IndexOf(':') + 1)..]).Hash)); |
| 390 | } |
| 391 | |
| 392 | Assert.Equal(Hashes(), Hashes()); |
| 393 | } |
| 394 | |
| 395 | [Fact] |
| 396 | public void ConvertsEverySkyInTheLibrary() |
| 397 | { |
| 398 | var path = Environment.GetEnvironmentVariable("EVIL_GMOD"); |
| 399 | Assert.SkipWhen(path is null, "Set EVIL_GMOD to a Garry's Mod install to convert its skies."); |
| 400 | var host = new EngineHost(); |
| 401 | host.Adapters.LoadAll([Installed]); |
| 402 | var mount = host.Mounts.Open(Assert.Single(host.Mounts.Probe(path!))); |
| 403 | host.Conversion.AddNamespace(ContentNamespace.For(mount)); |
| 404 | var names = mount.Files.EnumerateAll().Select(entry => entry.Path.Value) |
| 405 | .Where(file => file.StartsWith("materials/skybox/", StringComparison.OrdinalIgnoreCase) && |
| 406 | file.EndsWith("rt.vmt", StringComparison.OrdinalIgnoreCase)) |
| 407 | .Select(file => file["materials/skybox/".Length..^"rt.vmt".Length].ToLowerInvariant()).Distinct().Order() |
| 408 | .ToArray(); |
| 409 | var failures = new List<string>(); |
| 410 | var converted = 0; |
| 411 | foreach (var name in names) |
| 412 | { |
| 413 | var result = host.Conversion.Get(ContentKind.Sky, ContentId.Create("gmod", $"skies/{name}")); |
| 414 | if (result.Status == ConversionStatus.NoConverter) |
| 415 | continue; |
| 416 | if (!result.Succeeded) |
| 417 | { |
| 418 | failures.Add(result.Diagnostics[^1]); |
| 419 | continue; |
| 420 | } |
| 421 | |
| 422 | converted++; |
| 423 | foreach (var reference in result.Content!.References) |
| 424 | if (host.Conversion.Get(reference.Kind, reference.Id) is { Succeeded: false } face) |
| 425 | failures.Add(face.Diagnostics[^1]); |
| 426 | } |
| 427 | |
| 428 | output.WriteLine($"{converted} of {names.Length} skies with a right face convert, {failures.Count} failures"); |
| 429 | foreach (var failure in failures.Take(20)) |
| 430 | output.WriteLine(failure); |
| 431 | Assert.True(converted > 30); |
| 432 | Assert.Empty(failures); |
| 433 | } |
| 434 | |
| 435 | [Theory] |
| 436 | [InlineData("gm_construct")] |
| 437 | [InlineData("gm_flatgrass")] |
| 438 | [InlineData("gm_bigcity_dusk")] |
| 439 | [InlineData("meaningless_life")] |
| 440 | public void ConvertsEverythingAMapReferences(string name) |
| 441 | { |
| 442 | var host = Open(); |
| 443 | Assert.SkipWhen(host.Conversion.Get(ContentKind.Map, ContentId.Parse($"gmod:maps/{name}")).Status == |
| 444 | ConversionStatus.NoConverter, $"maps/{name}.bsp isn't installed."); |
| 445 | var seen = new ConcurrentDictionary<ContentReference, byte>(); |
| 446 | var failures = new ConcurrentBag<string>(); |
| 447 | var missing = new ConcurrentBag<string>(); |
| 448 | var counts = new ConcurrentDictionary<string, int>(); |
| 449 | var warnings = new ConcurrentDictionary<string, int>(); |
| 450 | var frontier = new List<ContentReference> { new(ContentKind.Map, ContentId.Parse($"gmod:maps/{name}")) }; |
| 451 | while (frontier.Count > 0) |
| 452 | { |
| 453 | var next = new ConcurrentBag<ContentReference>(); |
| 454 | Parallel.ForEach(frontier.Where(reference => seen.TryAdd(reference, 0)), reference => |
| 455 | { |
| 456 | var result = host.Conversion.Get(reference.Kind, reference.Id); |
| 457 | counts.AddOrUpdate($"{reference.Kind} {result.Status}", 1, (_, count) => count + 1); |
| 458 | if (!result.Succeeded) |
| 459 | { |
| 460 | if (result.Status != ConversionStatus.NoNamespace) |
| 461 | (result.Status == ConversionStatus.NoConverter || |
| 462 | result.Diagnostics[^1].Contains("which isn't in its namespace") |
| 463 | ? missing |
| 464 | : failures).Add($"{reference.Kind} {reference.Id}: {result.Diagnostics[^1]}"); |
| 465 | return; |
| 466 | } |
| 467 | |
| 468 | foreach (var warning in result.Diagnostics) |
| 469 | warnings.AddOrUpdate(warning, 1, (_, count) => count + 1); |
| 470 | foreach (var child in result.Content!.References) |
| 471 | next.Add(child); |
| 472 | }); |
| 473 | frontier = next.Distinct().ToList(); |
| 474 | } |
| 475 | |
| 476 | foreach (var (kind, count) in counts.OrderBy(pair => pair.Key, StringComparer.Ordinal)) |
| 477 | output.WriteLine($"{count} × {kind}"); |
| 478 | foreach (var (warning, count) in warnings.OrderByDescending(pair => pair.Value).Take(40)) |
| 479 | output.WriteLine($"{count} × {warning}"); |
| 480 | output.WriteLine($"{failures.Count} failed, {missing.Count} missing from the install"); |
| 481 | foreach (var failure in failures.Concat(missing).Order(StringComparer.Ordinal).Take(100)) |
| 482 | output.WriteLine(failure); |
| 483 | Assert.Empty(failures); |
| 484 | } |
| 485 | |
| 486 | private sealed record WorldLight( |
| 487 | Vector3 Origin, |
| 488 | Vector3 Intensity, |
| 489 | Vector3 Normal, |
| 490 | int Type, |
| 491 | float StopDot2, |
| 492 | float Radius, |
| 493 | float Constant, |
| 494 | float Linear, |
| 495 | float Quadratic); |
| 496 | } |