Skip to content
0.2.0-dev Download ZIP
97 lines ·3.8 KB Raw
1using System.Collections.Concurrent;
2using System.Reflection;
3using EvilEngine.Conversion;
4using EvilEngine.Formats;
5using EvilEngine.Formats.Images;
6using EvilEngine.Formats.Textures;
7using EvilEngine.Mounts;
8
9namespace EvilEngine.Host.Tests;
10
11public sealed class GarrysModImageTests(ITestOutputHelper output)
12{
13 private static readonly string Installed = typeof(GarrysModImageTests).Assembly
14 .GetCustomAttributes<AssemblyMetadataAttribute>()
15 .Single(attribute => attribute.Key == "AdapterOutputRoot").Value!;
16
17 private static readonly string[] Extensions = [".png", ".jpg", ".jpeg"];
18
19 private static (EngineHost Host, Mount Mount) Open()
20 {
21 var path = Environment.GetEnvironmentVariable("EVIL_GMOD");
22 Assert.SkipWhen(path is null, "Set EVIL_GMOD to a Garry's Mod install to convert its images.");
23 var host = new EngineHost();
24 host.Adapters.LoadAll([Installed]);
25 var mount = host.Mounts.Open(Assert.Single(host.Mounts.Probe(path!)));
26 host.Conversion.AddNamespace(ContentNamespace.For(mount));
27 return (host, mount);
28 }
29
30 [Fact]
31 public void ConvertsEveryPngAndJpegInARealInstall()
32 {
33 var (_, mount) = Open();
34 var images = mount.Files.EnumerateAll()
35 .Select(entry => entry.Path)
36 .Where(path =>
37 Extensions.Any(extension => path.Value.EndsWith(extension, StringComparison.OrdinalIgnoreCase)))
38 .ToArray();
39 var failures = new ConcurrentBag<string>();
40 var misnamed = new ConcurrentBag<string>();
41 var kinds = new ConcurrentDictionary<string, int>();
42
43 Parallel.ForEach(images, path =>
44 {
45 if (!mount.Files.TryOpen(path, out var stream))
46 {
47 failures.Add($"{path}: can't be opened");
48 return;
49 }
50
51 byte[] bytes;
52 using (stream)
53 {
54 using var buffer = new MemoryStream();
55 stream.CopyTo(buffer);
56 bytes = buffer.ToArray();
57 }
58
59 try
60 {
61 var image = ImageTexture.Decode(bytes, path.Value);
62 var texture = TextureFile.Read(TextureFile.Write(ImageTexture.From(image)), path.Value);
63 Assert.Equal(image.Pixels, texture.Levels[0]);
64 kinds.AddOrUpdate(
65 $"{Path.GetExtension(path.Value).ToLowerInvariant()} {image.Channels}×{image.BitDepth}", 1,
66 (_, count) => count + 1);
67 }
68 catch (Exception error) when (error is ImageFormatException or TextureFormatException)
69 {
70 (error.Message.Contains("whatever its name says") ? misnamed : failures).Add(error.Message);
71 }
72 });
73
74 foreach (var (kind, count) in kinds.OrderByDescending(pair => pair.Value))
75 output.WriteLine($"{count} × {kind}");
76 output.WriteLine(
77 $"{images.Length} images, {failures.Count} failed, {misnamed.Count} another format under an image's name");
78 foreach (var failure in failures.Concat(misnamed).Order(StringComparer.Ordinal).Take(100))
79 output.WriteLine(failure);
80 Assert.NotEmpty(images);
81 Assert.Empty(failures);
82 }
83
84 [Fact]
85 public void ConvertsASpawnMenuIconThroughThePipeline()
86 {
87 var (host, _) = Open();
88
89 var result = host.Conversion.Get(ContentKind.Texture, ContentId.Parse("gmod:materials/icon16/folder"));
90
91 Assert.True(result.Succeeded, string.Join('\n', result.Diagnostics));
92 Assert.Equal("image", result.Content!.Converter);
93 var texture = TextureFile.Read(result.Content.Data, "folder");
94 Assert.Equal((TextureFormat.R8G8B8A8Unorm, 16, 16, 5),
95 (texture.Format, texture.Width, texture.Height, texture.Levels.Count));
96 }
97}