using System.Collections.Concurrent; using System.Reflection; using EvilEngine.Conversion; using EvilEngine.Formats; using EvilEngine.Formats.Images; using EvilEngine.Formats.Textures; using EvilEngine.Mounts; namespace EvilEngine.Host.Tests; public sealed class GarrysModImageTests(ITestOutputHelper output) { private static readonly string Installed = typeof(GarrysModImageTests).Assembly .GetCustomAttributes() .Single(attribute => attribute.Key == "AdapterOutputRoot").Value!; private static readonly string[] Extensions = [".png", ".jpg", ".jpeg"]; private static (EngineHost Host, Mount Mount) Open() { var path = Environment.GetEnvironmentVariable("EVIL_GMOD"); Assert.SkipWhen(path is null, "Set EVIL_GMOD to a Garry's Mod install to convert its images."); var host = new EngineHost(); host.Adapters.LoadAll([Installed]); var mount = host.Mounts.Open(Assert.Single(host.Mounts.Probe(path!))); host.Conversion.AddNamespace(ContentNamespace.For(mount)); return (host, mount); } [Fact] public void ConvertsEveryPngAndJpegInARealInstall() { var (_, mount) = Open(); var images = mount.Files.EnumerateAll() .Select(entry => entry.Path) .Where(path => Extensions.Any(extension => path.Value.EndsWith(extension, StringComparison.OrdinalIgnoreCase))) .ToArray(); var failures = new ConcurrentBag(); var misnamed = new ConcurrentBag(); var kinds = new ConcurrentDictionary(); Parallel.ForEach(images, path => { if (!mount.Files.TryOpen(path, out var stream)) { failures.Add($"{path}: can't be opened"); return; } byte[] bytes; using (stream) { using var buffer = new MemoryStream(); stream.CopyTo(buffer); bytes = buffer.ToArray(); } try { var image = ImageTexture.Decode(bytes, path.Value); var texture = TextureFile.Read(TextureFile.Write(ImageTexture.From(image)), path.Value); Assert.Equal(image.Pixels, texture.Levels[0]); kinds.AddOrUpdate( $"{Path.GetExtension(path.Value).ToLowerInvariant()} {image.Channels}×{image.BitDepth}", 1, (_, count) => count + 1); } catch (Exception error) when (error is ImageFormatException or TextureFormatException) { (error.Message.Contains("whatever its name says") ? misnamed : failures).Add(error.Message); } }); foreach (var (kind, count) in kinds.OrderByDescending(pair => pair.Value)) output.WriteLine($"{count} × {kind}"); output.WriteLine( $"{images.Length} images, {failures.Count} failed, {misnamed.Count} another format under an image's name"); foreach (var failure in failures.Concat(misnamed).Order(StringComparer.Ordinal).Take(100)) output.WriteLine(failure); Assert.NotEmpty(images); Assert.Empty(failures); } [Fact] public void ConvertsASpawnMenuIconThroughThePipeline() { var (host, _) = Open(); var result = host.Conversion.Get(ContentKind.Texture, ContentId.Parse("gmod:materials/icon16/folder")); Assert.True(result.Succeeded, string.Join('\n', result.Diagnostics)); Assert.Equal("image", result.Content!.Converter); var texture = TextureFile.Read(result.Content.Data, "folder"); Assert.Equal((TextureFormat.R8G8B8A8Unorm, 16, 16, 5), (texture.Format, texture.Width, texture.Height, texture.Levels.Count)); } }