0.2.0-dev Download ZIP
247 lines ·11.6 KB Raw
| 1 | using System.Collections.Concurrent; |
| 2 | using System.Reflection; |
| 3 | using System.Text; |
| 4 | using System.Text.RegularExpressions; |
| 5 | using EvilEngine.Conversion; |
| 6 | using EvilEngine.Formats; |
| 7 | using EvilEngine.Formats.Maps; |
| 8 | using EvilEngine.Formats.Records; |
| 9 | using EvilEngine.Formats.Sounds; |
| 10 | using EvilEngine.Host.Collision; |
| 11 | using EvilEngine.Host.Movement; |
| 12 | using EvilEngine.Host.Sessions; |
| 13 | using EvilEngine.Host.Steps; |
| 14 | using EvilEngine.Mounts; |
| 15 | |
| 16 | namespace EvilEngine.Host.Tests; |
| 17 | |
| 18 | public sealed class SourceSoundTests(ITestOutputHelper output) |
| 19 | { |
| 20 | private static readonly string Installed = typeof(SourceSoundTests).Assembly |
| 21 | .GetCustomAttributes<AssemblyMetadataAttribute>() |
| 22 | .Single(attribute => attribute.Key == "AdapterOutputRoot").Value!; |
| 23 | |
| 24 | private static (EngineHost Host, Mount Mount) Open(IConversionCache? cache = null) |
| 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 sounds."); |
| 28 | var host = new EngineHost(cache); |
| 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, mount); |
| 33 | } |
| 34 | |
| 35 | private RecordSet Set(EngineHost host, string id) |
| 36 | { |
| 37 | var result = host.Conversion.Get(ContentKind.Record, ContentId.Parse(id)); |
| 38 | Assert.True(result.Succeeded, string.Join('\n', result.Diagnostics)); |
| 39 | var kinds = result.Diagnostics |
| 40 | .Select(note => Regex.Replace(Regex.Replace(note, @"^[^ ]+ ", ""), @"'[^']*'", "'…'")) |
| 41 | .GroupBy(kind => kind).OrderByDescending(group => group.Count()); |
| 42 | output.WriteLine($"{id}: {result.Diagnostics.Count} notes"); |
| 43 | foreach (var kind in kinds.Take(40)) |
| 44 | output.WriteLine($"{kind.Count(),6} × {kind.Key}"); |
| 45 | foreach (var note in result.Diagnostics.Take(60)) |
| 46 | output.WriteLine($" {note}"); |
| 47 | return RecordText.Read("gmod", Encoding.UTF8.GetString(result.Content!.Data.Span), id); |
| 48 | } |
| 49 | |
| 50 | [Fact] |
| 51 | public void ConvertsEverySoundscriptAndSoundAdd() |
| 52 | { |
| 53 | var (host, _) = Open(); |
| 54 | var set = Set(host, "gmod:records/sounds"); |
| 55 | |
| 56 | var step = Assert.Single(set.Sounds, sound => sound.Id == ContentId.Parse("gmod:concrete.stepleft")); |
| 57 | output.WriteLine($"{set.Sounds.Count} sounds; {set.Sounds.Count(sound => sound.Loop == true)} loop; " + |
| 58 | string.Join(", ", |
| 59 | set.Sounds.GroupBy(sound => sound.Branch) |
| 60 | .Select(group => $"{group.Key} {group.Count()}"))); |
| 61 | output.WriteLine($"concrete.stepleft: {string.Join(", ", step.Files.Select(file => file.Content))}, " + |
| 62 | $"volume {step.Volume}, pitch {step.Pitch}, level {step.Level}, channel {step.Channel}"); |
| 63 | Assert.Equal( |
| 64 | [ |
| 65 | ContentId.Parse("gmod:sound/player/footsteps/concrete1.wav"), |
| 66 | ContentId.Parse("gmod:sound/player/footsteps/concrete3.wav") |
| 67 | ], |
| 68 | step.Files.Select(file => file.Content)); |
| 69 | Assert.Equal(new SoundRange(75), step.Level); |
| 70 | Assert.True(set.Sounds.Count > 5000); |
| 71 | } |
| 72 | |
| 73 | [Fact] |
| 74 | public void ConvertsEverySoundscapeInTheManifest() |
| 75 | { |
| 76 | var (host, _) = Open(); |
| 77 | var set = Set(host, "gmod:records/soundscapes"); |
| 78 | |
| 79 | var outside = |
| 80 | Assert.Single(set.Soundscapes, record => record.Id == ContentId.Parse("gmod:construct.outside")); |
| 81 | output.WriteLine($"{set.Soundscapes.Count} soundscapes, {set.Soundscapes.Sum(record => record.Loops.Count)} " + |
| 82 | $"loops, {set.Soundscapes.Sum(record => record.Randoms.Count)} random sets, " + |
| 83 | $"{set.Soundscapes.Sum(record => record.Includes.Count)} includes"); |
| 84 | Assert.Equal( |
| 85 | [ |
| 86 | new SoundscapeLoop(new SoundFile(ContentId.Parse("gmod:sound/ambient/construct_tone.wav")) |
| 87 | { Flags = SoundFlags.Omni }) |
| 88 | { Volume = new SoundRange(0.15), Pitch = new SoundRange(110), Level = new SoundRange(30) }, |
| 89 | new SoundscapeLoop(new SoundFile(ContentId.Parse("gmod:sound/ambient/forest_day.wav"))) |
| 90 | { Volume = new SoundRange(0.25), Level = new SoundRange(30) } |
| 91 | ], outside.Loops); |
| 92 | Assert.Equal("1", outside.Games["gmod"]["dsp"]); |
| 93 | output.WriteLine(RecordText.Write(RecordSet.Empty with |
| 94 | { |
| 95 | Soundscapes = set.Soundscapes.Where(record => |
| 96 | record.Id.Name is "construct.lake" or "e3_techdemo_06" or "d1_canals_junkyard").ToArray() |
| 97 | })); |
| 98 | var names = set.Soundscapes.Select(record => record.Id).ToHashSet(); |
| 99 | var unresolved = set.Soundscapes.SelectMany(record => |
| 100 | record.Includes.Where(include => !names.Contains(include.Soundscape)) |
| 101 | .Select(include => $"{record.Id} -> {include.Soundscape}")) |
| 102 | .ToArray(); |
| 103 | output.WriteLine($"unresolved includes: {string.Join(", ", unresolved)}"); |
| 104 | Assert.True(set.Soundscapes.Count > 800, $"{set.Soundscapes.Count} soundscapes"); |
| 105 | Assert.True(unresolved.Length <= 2, string.Join(", ", unresolved)); |
| 106 | } |
| 107 | |
| 108 | [Fact] |
| 109 | public void ConvertsEverySurface() |
| 110 | { |
| 111 | var (host, _) = Open(); |
| 112 | var set = Set(host, "gmod:records/surfaces"); |
| 113 | |
| 114 | var concrete = Assert.Single(set.Surfaces, surface => surface.Id == ContentId.Parse("gmod:surfaces/concrete")); |
| 115 | output.WriteLine($"{set.Surfaces.Count} surfaces: " + |
| 116 | string.Join(", ", |
| 117 | set.Surfaces.GroupBy(surface => surface.Class) |
| 118 | .Select(group => $"{group.Key} {group.Count()}"))); |
| 119 | Assert.Equal(ContentId.Parse("gmod:concrete.stepleft"), concrete.Sounds["step.left"]); |
| 120 | Assert.Equal(ContentId.Parse("gmod:concrete.stepright"), concrete.Sounds["step.right"]); |
| 121 | Assert.Equal("concrete", concrete.Class); |
| 122 | var sounds = Set(host, "gmod:records/sounds").Sounds.Select(sound => sound.Id).ToHashSet(); |
| 123 | var unresolved = set.Surfaces.SelectMany(surface => surface.Sounds.Where(pair => !sounds.Contains(pair.Value)) |
| 124 | .Select(pair => $"{surface.Id} {pair.Key} {pair.Value}")).ToArray(); |
| 125 | output.WriteLine($"{unresolved.Length} surface sounds name no sound record"); |
| 126 | foreach (var line in unresolved.Take(40)) |
| 127 | output.WriteLine($" {line}"); |
| 128 | Assert.All(set.Surfaces, surface => |
| 129 | { |
| 130 | Assert.True(surface.Sounds.ContainsKey("step.left"), $"{surface.Id} has no step.left"); |
| 131 | Assert.Contains(surface.Sounds["step.left"], sounds); |
| 132 | }); |
| 133 | } |
| 134 | |
| 135 | [Fact] |
| 136 | public void ConvertsEveryWaveUnderSound() |
| 137 | { |
| 138 | var (host, mount) = Open(new NoCache()); |
| 139 | var waves = mount.Files.EnumerateAll() |
| 140 | .Select(entry => entry.Path.Value) |
| 141 | .Where(path => path.StartsWith("sound/", StringComparison.OrdinalIgnoreCase) && SoundContent.IsSound(path)) |
| 142 | .Order(StringComparer.Ordinal) |
| 143 | .ToArray(); |
| 144 | var failures = new ConcurrentBag<string>(); |
| 145 | var kinds = new ConcurrentDictionary<string, int>(); |
| 146 | var sources = new ConcurrentDictionary<string, int>(); |
| 147 | Parallel.ForEach(waves, new ParallelOptions { MaxDegreeOfParallelism = 8 }, path => |
| 148 | { |
| 149 | var result = host.Conversion.Get(ContentKind.Sound, ContentId.Create("gmod", path)); |
| 150 | if (!result.Succeeded) |
| 151 | { |
| 152 | failures.Add($"{path}: {string.Join(" / ", result.Diagnostics)}"); |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | var data = result.Content!.Data; |
| 157 | var kind = SoundContent.Extension(path)!; |
| 158 | if (kind == "wav") |
| 159 | { |
| 160 | var wave = WaveFile.Read(data, path); |
| 161 | var rewritten = mount.Files.TryOpenAccess(VirtualPath.Parse(path), out var original) && |
| 162 | original.Length != data.Length; |
| 163 | original?.Dispose(); |
| 164 | if (rewritten && mount.Files.TryOpen(VirtualPath.Parse(path), out var stream)) |
| 165 | { |
| 166 | using var copy = new MemoryStream(); |
| 167 | using (stream) |
| 168 | { |
| 169 | stream.CopyTo(copy); |
| 170 | } |
| 171 | |
| 172 | var source = WaveFile.Read(copy.ToArray(), path); |
| 173 | sources.AddOrUpdate( |
| 174 | $"from format {source.Format} {source.BitsPerSample}-bit, {source.Cues.Count} cues{(source.HasCue ? "" : ", no cue")}", |
| 175 | 1, (_, count) => count + 1); |
| 176 | } |
| 177 | |
| 178 | kind = |
| 179 | $"wav {(wave.Format == WaveFormats.Pcm ? "pcm" : wave.Format.ToString())} {wave.BitsPerSample}-bit " + |
| 180 | $"{wave.Channels}ch{(wave.Loop is null ? "" : " loop")}{(wave.HasCue ? " cue" : "")}{(rewritten ? " rewritten" : "")}"; |
| 181 | } |
| 182 | |
| 183 | kinds.AddOrUpdate(kind, 1, (_, count) => count + 1); |
| 184 | }); |
| 185 | |
| 186 | output.WriteLine($"{waves.Length} sounds, {failures.Count} failed"); |
| 187 | foreach (var (kind, count) in kinds.OrderByDescending(pair => pair.Value)) |
| 188 | output.WriteLine($"{count,6} × {kind}"); |
| 189 | foreach (var (kind, count) in sources.OrderByDescending(pair => pair.Value)) |
| 190 | output.WriteLine($"{count,6} × rewritten {kind}"); |
| 191 | foreach (var failure in failures.Order(StringComparer.Ordinal).Take(100)) |
| 192 | output.WriteLine(failure); |
| 193 | Assert.True(waves.Length > 5000); |
| 194 | Assert.Empty(failures); |
| 195 | Assert.DoesNotContain(kinds.Keys, kind => kind.EndsWith(" cue")); |
| 196 | } |
| 197 | |
| 198 | [Fact] |
| 199 | public void WalksABodyOverGmConstructsConcreteWithItsStepSounds() |
| 200 | { |
| 201 | var (host, mount) = Open(); |
| 202 | var registry = LocalRegistry.Build([mount], host.Conversion); |
| 203 | var cell = MapText.ReadCell(Encoding.UTF8.GetString(host.Conversion.Get(ContentKind.Cell, |
| 204 | ContentId.Parse("gmod:maps/gm_construct/cells/0")).Content!.Data.Span), "cell"); |
| 205 | var space = HullSpace.Build(MapColliders.Build([cell], host.Conversion.Get, (_, _) => { }), |
| 206 | HullSpace.BlocksPlayers); |
| 207 | var spawn = cell.Spawns[0]; |
| 208 | var server = new ServerSession(new SessionStart(ContentId.Parse("gmod:gm_construct"), space, [spawn], -1000) |
| 209 | { |
| 210 | StepSounds = StepSounds.From(registry, host.Conversion) |
| 211 | }); |
| 212 | var heard = new List<SoundEvent>(); |
| 213 | var bot = server.AddBot(brain: _ => new MoveCommand(1, 0, 0, 0, spawn.Transform.Angles.Y, MoveButtons.None)); |
| 214 | bot.Arrived = snapshot => heard.AddRange(snapshot.Sounds); |
| 215 | for (var tick = 0; tick < 200; tick++) |
| 216 | server.Tick(); |
| 217 | |
| 218 | output.WriteLine($"{registry.Sounds.Count} sounds, {registry.Surfaces.Count} surfaces in the registry"); |
| 219 | foreach (var sound in heard) |
| 220 | output.WriteLine($"{sound.Sound} at {sound.Position} volume {sound.Volume}"); |
| 221 | Assert.True(heard.Count >= 3, $"{heard.Count} step sounds"); |
| 222 | Assert.Equal([ContentId.Parse("gmod:concrete.stepright"), ContentId.Parse("gmod:concrete.stepleft")], |
| 223 | heard.Take(2).Select(sound => sound.Sound)); |
| 224 | Assert.All(heard, sound => Assert.NotNull(registry.Sound(sound.Sound))); |
| 225 | } |
| 226 | |
| 227 | private sealed class NoCache : IConversionCache |
| 228 | { |
| 229 | public ConvertedContent? Find(ContentKind kind, ContentId id) |
| 230 | { |
| 231 | return null; |
| 232 | } |
| 233 | |
| 234 | public void Store(ConvertedContent content) |
| 235 | { |
| 236 | } |
| 237 | |
| 238 | public void Forget(ContentKind kind, ContentId id) |
| 239 | { |
| 240 | } |
| 241 | |
| 242 | public int Invalidate(string @namespace, IReadOnlyCollection<VirtualPath> paths, CaseRule caseRule) |
| 243 | { |
| 244 | return 0; |
| 245 | } |
| 246 | } |
| 247 | } |