acdream had no application icon on either executable. Two marks now ship, built from the game's own material rather than drawn freehand: * Client - the retail mosswart head. Not an illustration of one: the actual creature mesh (Setup 0x02000B4F part 14, skin atlas 0x05001E11, ClothingBase 0x10000344) read out of client_portal.dat through acdream's own GfxObjMesh/SetupMesh port, then smoothed, lit and graded. Palette values are sampled from that texture, including the mustard belly the Mosswart lore calls a "foul yellow". * Launcher - a forged ring enclosing a barbed crescent, rebuilt from measurements of the retail wordmark and the acclient.exe icon resource. An original construction in the same visual language, not a copy of the trademarked logo. Its warm field matches the retail client icon. Three techniques carry the render quality, all in tools/IconForge: * PN-triangle tessellation (smooth.py). The retail head is 104 triangles and renders faceted. Each triangle becomes a cubic Bezier patch built from its own corner positions and normals, so the silhouette genuinely rounds rather than merely shading smoothly - and it needs no mesh connectivity, which matters because UV seams would otherwise pull apart. Normals are welded across coincident positions first, but only within a crease angle, so ear fins and tusk edges stay sharp. * Matcaps (ring.py). A Lambert rasterizer cannot produce chrome, because chrome is almost entirely reflection and there is nothing here to reflect. Sampling a lit-sphere image by the camera-space normal is the standard stand-in for an environment map. * Distance-transform bevelling (chisel.py). Flat shapes become chiselled metal by treating distance-to-edge as height. The height field is blurred before differentiating; without that the medial axis of each stroke shows through as a hatched ridge. Two facts worth recording, both discovered the hard way. Creature Setups define no upright pose in PlacementFrames, so the exporter must be handed the weenie's MotionTable id or all 17 parts stack on the origin. And a mosswart's eyes sit on the sides of the skull like a frog's, so a dead-on frontal turns them edge-on and the face stops reading as a mosswart at all; the hero angle is az 266 / el 32. Wiring: <ApplicationIcon> gives each executable its PE icon. The client's runtime window icon is embedded rather than copied beside the binary - a window icon has no sensible fallback if the file goes missing, and embedding survives single-file publish. WindowIconLoaderTests guards the resource names, which are coupled to LogicalName in the csproj by string alone and would otherwise fail only as a silently icon-less window. Both halves of the pipeline are deterministic and reproduce the committed PNGs byte-for-byte, so an accidental edit shows up as a diff. Solution builds clean; 14,378 tests pass on the standard hermetic lane filter, 0 failures. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
164 lines
5.1 KiB
C#
164 lines
5.1 KiB
C#
// Dump the surfaces a geometry export references, as PNGs named the way
|
|
// tools/IconForge's loader globs for them: <0xID>_<W>x<H>.png.
|
|
//
|
|
// PNG is hand-rolled on ZLibStream so this tool needs no image package; the
|
|
// same approach is used by tools/IconExtract.
|
|
using System.IO.Compression;
|
|
using System.Text;
|
|
using AcDream.Core.Textures;
|
|
using DatReaderWriter;
|
|
using DatReaderWriter.DBObjs;
|
|
|
|
namespace MosswartArt;
|
|
|
|
internal static class Textures
|
|
{
|
|
/// <summary>
|
|
/// Resolve each id to pixels and write it out. Ids may be Surface (0x08),
|
|
/// SurfaceTexture (0x05) or RenderSurface (0x06/0x07); the chain is walked
|
|
/// down to pixels either way.
|
|
/// </summary>
|
|
public static int Dump(DatCollection dats, IEnumerable<uint> ids, string outDir)
|
|
{
|
|
Directory.CreateDirectory(outDir);
|
|
int written = 0;
|
|
|
|
foreach (uint id in ids.Distinct().OrderBy(v => v))
|
|
{
|
|
RenderSurface? rs = Resolve(dats, id);
|
|
if (rs is null || rs.Width <= 0 || rs.Height <= 0)
|
|
{
|
|
Console.Error.WriteLine($" 0x{id:X8} UNRESOLVED");
|
|
continue;
|
|
}
|
|
|
|
Palette? palette = null;
|
|
if (rs.DefaultPaletteId != 0
|
|
&& dats.TryGet<Palette>(rs.DefaultPaletteId, out var pal) && pal is not null)
|
|
{
|
|
palette = pal;
|
|
}
|
|
|
|
var decoded = SurfaceDecoder.DecodeRenderSurface(rs, palette);
|
|
if (decoded.Rgba8 is null || decoded.Rgba8.Length < decoded.Width * decoded.Height * 4)
|
|
{
|
|
Console.Error.WriteLine($" 0x{id:X8} DECODE FAILED");
|
|
continue;
|
|
}
|
|
|
|
string path = Path.Combine(
|
|
outDir, $"0x{id:X8}_{decoded.Width}x{decoded.Height}.png");
|
|
WritePng(path, decoded.Rgba8, decoded.Width, decoded.Height);
|
|
Console.WriteLine($" 0x{id:X8} {decoded.Width}x{decoded.Height} -> {Path.GetFileName(path)}");
|
|
written++;
|
|
}
|
|
|
|
return written;
|
|
}
|
|
|
|
// DatReaderWriter's TryGet<T> does not validate the file type -- it just
|
|
// deserializes the bytes as T -- so dispatch on the id range rather than
|
|
// trying each type in turn.
|
|
private static RenderSurface? Resolve(DatCollection dats, uint id)
|
|
{
|
|
uint lookupId = id;
|
|
uint type = id >> 24;
|
|
|
|
if (type == 0x08)
|
|
{
|
|
if (dats.TryGet<Surface>(id, out var surface) && surface is not null)
|
|
lookupId = (uint)surface.OrigTextureId;
|
|
type = lookupId >> 24;
|
|
}
|
|
|
|
if (type == 0x05)
|
|
{
|
|
if (dats.TryGet<SurfaceTexture>(lookupId, out var st) && st is not null
|
|
&& st.Textures.Count > 0
|
|
&& dats.TryGet<RenderSurface>((uint)st.Textures[0], out var inner))
|
|
{
|
|
return inner;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
if (type is 0x06 or 0x07)
|
|
return dats.TryGet<RenderSurface>(lookupId, out var direct) ? direct : null;
|
|
|
|
return null;
|
|
}
|
|
|
|
private static void WritePng(string path, byte[] rgba, int w, int h)
|
|
{
|
|
using var fs = File.Create(path);
|
|
fs.Write(new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A });
|
|
|
|
var ihdr = new byte[13];
|
|
WriteBE(ihdr, 0, (uint)w);
|
|
WriteBE(ihdr, 4, (uint)h);
|
|
ihdr[8] = 8; // bit depth
|
|
ihdr[9] = 6; // colour type: RGBA
|
|
WriteChunk(fs, "IHDR", ihdr);
|
|
|
|
using var ms = new MemoryStream();
|
|
using (var z = new ZLibStream(ms, CompressionLevel.Optimal, leaveOpen: true))
|
|
{
|
|
var row = new byte[1 + w * 4];
|
|
for (int y = 0; y < h; y++)
|
|
{
|
|
row[0] = 0; // filter: none
|
|
Array.Copy(rgba, y * w * 4, row, 1, w * 4);
|
|
z.Write(row, 0, row.Length);
|
|
}
|
|
}
|
|
WriteChunk(fs, "IDAT", ms.ToArray());
|
|
WriteChunk(fs, "IEND", Array.Empty<byte>());
|
|
}
|
|
|
|
private static void WriteBE(byte[] b, int o, uint v)
|
|
{
|
|
b[o] = (byte)(v >> 24);
|
|
b[o + 1] = (byte)(v >> 16);
|
|
b[o + 2] = (byte)(v >> 8);
|
|
b[o + 3] = (byte)v;
|
|
}
|
|
|
|
private static void WriteChunk(Stream s, string type, byte[] data)
|
|
{
|
|
Span<byte> len = stackalloc byte[4];
|
|
WriteBE(len, (uint)data.Length);
|
|
s.Write(len);
|
|
|
|
byte[] t = Encoding.ASCII.GetBytes(type);
|
|
s.Write(t);
|
|
s.Write(data);
|
|
|
|
uint crc = Crc32(t, data);
|
|
Span<byte> c = stackalloc byte[4];
|
|
WriteBE(c, crc);
|
|
s.Write(c);
|
|
}
|
|
|
|
private static void WriteBE(Span<byte> b, uint v)
|
|
{
|
|
b[0] = (byte)(v >> 24);
|
|
b[1] = (byte)(v >> 16);
|
|
b[2] = (byte)(v >> 8);
|
|
b[3] = (byte)v;
|
|
}
|
|
|
|
private static uint Crc32(byte[] a, byte[] b)
|
|
{
|
|
uint crc = 0xFFFFFFFFu;
|
|
foreach (byte[] arr in new[] { a, b })
|
|
{
|
|
foreach (byte by in arr)
|
|
{
|
|
crc ^= by;
|
|
for (int k = 0; k < 8; k++)
|
|
crc = (crc & 1) != 0 ? (crc >> 1) ^ 0xEDB88320u : crc >> 1;
|
|
}
|
|
}
|
|
return crc ^ 0xFFFFFFFFu;
|
|
}
|
|
}
|