0.2.0-dev Download ZIP
197 lines ·7.4 KB Raw
| 1 | using System.Globalization; |
| 2 | using System.Text; |
| 3 | using EvilEngine.Conversion; |
| 4 | using EvilEngine.Formats; |
| 5 | using EvilEngine.Formats.Maps; |
| 6 | using EvilEngine.Formats.Materials; |
| 7 | using EvilEngine.Formats.Models; |
| 8 | using EvilEngine.Formats.Textures; |
| 9 | |
| 10 | namespace EvilEngine.Adapters.Quake.Maps; |
| 11 | |
| 12 | public abstract class QuakeMapConverter(ContentKind produces, string id = "quake.bsp") : IConverter |
| 13 | { |
| 14 | public string Id => id; |
| 15 | |
| 16 | public virtual int Version => 1; |
| 17 | |
| 18 | public ContentKind Produces => produces; |
| 19 | |
| 20 | public bool Accepts(ConversionContext context) |
| 21 | { |
| 22 | return QuakeMap.Split(context, context.Id.Name) is { } split && Wants(split.Part); |
| 23 | } |
| 24 | |
| 25 | public void Convert(ConversionContext context, Stream output) |
| 26 | { |
| 27 | var (name, part) = QuakeMap.Split(context, context.Id.Name) ?? |
| 28 | throw new InvalidOperationException($"{context.Id} isn't a map's."); |
| 29 | Convert(new QuakeMap(context, name), part, output); |
| 30 | } |
| 31 | |
| 32 | protected abstract bool Wants(string part); |
| 33 | |
| 34 | protected abstract void Convert(QuakeMap map, string part, Stream output); |
| 35 | |
| 36 | protected static void Write(Stream output, string text) |
| 37 | { |
| 38 | output.Adopt(Encoding.UTF8.GetBytes(text)); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | public sealed class QuakeManifestConverter() : QuakeMapConverter(ContentKind.Map) |
| 43 | { |
| 44 | public const string Cell = "cells/0"; |
| 45 | |
| 46 | protected override bool Wants(string part) |
| 47 | { |
| 48 | return part.Length == 0; |
| 49 | } |
| 50 | |
| 51 | protected override void Convert(QuakeMap map, string part, Stream output) |
| 52 | { |
| 53 | var bsp = map.Bsp; |
| 54 | var world = bsp.Models[0]; |
| 55 | var cell = new CellReference(map.Reference(ContentKind.Cell, Cell), QuakeSpace.Point(world.Min), |
| 56 | QuakeSpace.Point(world.Max)); |
| 57 | var lightmap = map.Lightmaps.Width > 0 |
| 58 | ? new MapLightmap(map.Reference(ContentKind.Texture, "lightmaps/ldr"), null) |
| 59 | : null; |
| 60 | var sky = QuakeSkyConverter.SkyTexture(map) is null ? (ContentId?)null : map.Reference(ContentKind.Sky, "sky"); |
| 61 | foreach (var extra in bsp.Extra) |
| 62 | map.Context.Warn($"{bsp.File} carries the BSPX lump {extra}, which isn't read."); |
| 63 | if (bsp.Entities.FirstOrDefault() is { } world0) |
| 64 | { |
| 65 | if (world0["sky"] is { Length: > 0 } skybox) |
| 66 | map.Context.Warn( |
| 67 | $"{bsp.File} names the skybox gfx/env/{skybox}, which waits on a TGA reader, so its sky is its sky texture."); |
| 68 | if (world0["fog"] is { Length: > 0 } fog) |
| 69 | map.Context.Warn($"{bsp.File} sets fog '{fog}', which waits on an exponential squared fog mode."); |
| 70 | } |
| 71 | |
| 72 | Write(output, MapText.WriteManifest(new MapManifest(sky, [cell], MapCell.Empty) { Lightmap = lightmap })); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | public sealed class QuakeCellConverter() : QuakeMapConverter(ContentKind.Cell) |
| 77 | { |
| 78 | public override int Version => 4; |
| 79 | |
| 80 | protected override bool Wants(string part) |
| 81 | { |
| 82 | return part == QuakeManifestConverter.Cell; |
| 83 | } |
| 84 | |
| 85 | protected override void Convert(QuakeMap map, string part, Stream output) |
| 86 | { |
| 87 | Write(output, MapText.WriteCell(QuakeMapCell.Build(map))); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | public sealed class QuakeModelConverter() : QuakeMapConverter(ContentKind.Model) |
| 92 | { |
| 93 | protected override bool Wants(string part) |
| 94 | { |
| 95 | return part.StartsWith("models/", StringComparison.Ordinal); |
| 96 | } |
| 97 | |
| 98 | protected override void Convert(QuakeMap map, string part, Stream output) |
| 99 | { |
| 100 | var index = int.Parse(part.AsSpan(7), NumberStyles.None, CultureInfo.InvariantCulture); |
| 101 | if (index >= map.Bsp.Models.Count) |
| 102 | throw new QuakeMapException(map.Bsp.File, $"has {map.Bsp.Models.Count} models, so no model {index}."); |
| 103 | output.Adopt(ModelFile.Write(QuakeMeshes.Build(map, map.ModelFaces(index)))); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | public sealed class QuakeLightmapConverter() : QuakeMapConverter(ContentKind.Texture) |
| 108 | { |
| 109 | protected override bool Wants(string part) |
| 110 | { |
| 111 | return part == "lightmaps/ldr"; |
| 112 | } |
| 113 | |
| 114 | protected override void Convert(QuakeMap map, string part, Stream output) |
| 115 | { |
| 116 | var lightmaps = map.Lightmaps; |
| 117 | if (lightmaps.Width == 0) |
| 118 | throw new QuakeMapException(map.Bsp.File, "has no lit faces, so no lightmap page."); |
| 119 | var grey = QuakeLightmaps.Bytes(map) == 1; |
| 120 | var texture = Texture.Flat(grey ? TextureFormat.R8Unorm : TextureFormat.R8G8B8Unorm, lightmaps.Width, |
| 121 | lightmaps.Height, lightmaps.Page(map)) with |
| 122 | { |
| 123 | Swizzle = grey ? "rrr1" : "rgba", |
| 124 | Sampling = new TextureSampling(TextureWrap.Clamp, TextureWrap.Clamp, TextureWrap.Clamp, |
| 125 | Mips: TextureMipFilter.None) |
| 126 | }; |
| 127 | output.Adopt(TextureFile.Write(texture)); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | public sealed class QuakeTextureConverter() : QuakeMapConverter(ContentKind.Texture, "quake.miptex") |
| 132 | { |
| 133 | protected override bool Wants(string part) |
| 134 | { |
| 135 | return part.StartsWith("textures/", StringComparison.Ordinal); |
| 136 | } |
| 137 | |
| 138 | protected override void Convert(QuakeMap map, string part, Stream output) |
| 139 | { |
| 140 | var segments = part.Split('/'); |
| 141 | var name = segments[1]; |
| 142 | var alternate = segments.Contains(QuakeTextures.Alternate); |
| 143 | var fullbright = segments[^1] == QuakeTextures.Fullbright; |
| 144 | var indices = segments[^1] == QuakeTextures.Indices; |
| 145 | if (QuakeTextures.Animated(name) && name[1] != '0') |
| 146 | throw new QuakeMapException(map.Bsp.File, |
| 147 | $"names its animated textures by their first frame, not '{name}'."); |
| 148 | var set = QuakeTextures.Set(map, name, alternate) ?? |
| 149 | throw new QuakeMapException(map.Bsp.File, |
| 150 | $"carries no texture '{name}'{(alternate ? " with alternate frames" : "")}."); |
| 151 | if (set.Id != $"textures/{name}{(alternate ? "/alternate" : "")}") |
| 152 | throw new QuakeMapException(map.Bsp.File, $"names texture '{name}' as {set.Id}."); |
| 153 | if (fullbright && !QuakeTextures.HasFullbright(map, set)) |
| 154 | throw new QuakeMapException(map.Bsp.File, $"has no fullbright texels in '{name}'."); |
| 155 | if (indices && !map.Software) |
| 156 | throw new QuakeMapException(map.Bsp.File, $"draws '{name}' in colour, so it has no index texture."); |
| 157 | output.Adopt(TextureFile.Write(indices |
| 158 | ? QuakeTextures.BuildIndices(set) |
| 159 | : QuakeTextures.Build(map, set, fullbright))); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | public sealed class QuakeMaterialConverter() : QuakeMapConverter(ContentKind.Material) |
| 164 | { |
| 165 | protected override bool Wants(string part) |
| 166 | { |
| 167 | return part.StartsWith("materials/", StringComparison.Ordinal); |
| 168 | } |
| 169 | |
| 170 | protected override void Convert(QuakeMap map, string part, Stream output) |
| 171 | { |
| 172 | var name = part["materials/".Length..]; |
| 173 | Write(output, MaterialText.Write(name.EndsWith("/" + QuakeTextures.Dome, StringComparison.Ordinal) |
| 174 | ? QuakeTextures.BuildDome(map, name[..^(QuakeTextures.Dome.Length + 1)]) |
| 175 | : QuakeTextures.BuildMaterial(map, name))); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | public sealed class QuakeSkyConverter() : QuakeMapConverter(ContentKind.Sky) |
| 180 | { |
| 181 | public static string? SkyTexture(QuakeMap map) |
| 182 | { |
| 183 | return map.Data.SkyTexture; |
| 184 | } |
| 185 | |
| 186 | protected override bool Wants(string part) |
| 187 | { |
| 188 | return part == "sky"; |
| 189 | } |
| 190 | |
| 191 | protected override void Convert(QuakeMap map, string part, Stream output) |
| 192 | { |
| 193 | var name = SkyTexture(map) ?? throw new QuakeMapException(map.Bsp.File, "draws no sky."); |
| 194 | var material = map.Reference(ContentKind.Material, $"materials/{name}/{QuakeTextures.Dome}"); |
| 195 | Write(output, SkyText.Write(new Sky([new SkyLayer.Dome(material)]))); |
| 196 | } |
| 197 | } |