|
| 1 | +// ============================================================================= |
| 2 | +// IW4 (MW2 PS3) zone reader — ported from Jacob Schroeder's FastFile |
| 3 | +// https://github.com/jacob-schroeder/FastFile |
| 4 | +// Ports: FastFile.Logic/Assets/Readers/FontReader.cs and |
| 5 | +// FastFile.Models/Assets/Fonts/FontAsset.cs (FontAsset + FontGlyph). |
| 6 | +// |
| 7 | +// Font_s is a 24-byte root (name + pixelHeight + glyphCount + material + glowMaterial + |
| 8 | +// glyphs pointers); the name, glyph table, and materials all live in other (LARGE/shared) |
| 9 | +// blocks, i.e. Offset pointers. The local inline engine resolves only inline (-1) pointers, |
| 10 | +// so for a typical font nothing is consumed past the 24-byte root and the walk simply |
| 11 | +// advances to the next asset. This is what lets the body walk step past the fonts that sit |
| 12 | +// right before the big localize block in zones like code_post_gfx_mp.ff. |
| 13 | +// ============================================================================= |
| 14 | + |
| 15 | +namespace FastFileLib.Iw4; |
| 16 | + |
| 17 | +public sealed class FontAsset : BaseAsset |
| 18 | +{ |
| 19 | + public const int RootSize = 0x18; // 24 |
| 20 | + public const int GlyphSize = 0x18; // 24 |
| 21 | + |
| 22 | + public FontAsset() : base(XAssetType.Font) { } |
| 23 | + |
| 24 | + public ZonePointer<string>? NamePtr { get; set; } |
| 25 | + public string Name => NamePtr is { IsResolved: true } ? NamePtr.Result ?? string.Empty : string.Empty; |
| 26 | + public int PixelHeight { get; set; } |
| 27 | + public int GlyphCount { get; set; } |
| 28 | + public ZonePointer<Material>? Material { get; set; } |
| 29 | + public ZonePointer<Material>? GlowMaterial { get; set; } |
| 30 | + public ZonePointer<FontGlyph[]>? Glyphs { get; set; } |
| 31 | + |
| 32 | + public override string? GetDisplayName => string.IsNullOrWhiteSpace(Name) ? Type.ToString() : Name; |
| 33 | +} |
| 34 | + |
| 35 | +public sealed class FontGlyph |
| 36 | +{ |
| 37 | + public ushort Letter; |
| 38 | + public byte X0, Y0, Dx, PixelWidth, PixelHeight, Padding; |
| 39 | + public float S0, T0, S1, T1; |
| 40 | +} |
| 41 | + |
| 42 | +internal static class FontReader |
| 43 | +{ |
| 44 | + public static FontAsset Read(ref ZoneReadContext context) |
| 45 | + { |
| 46 | + var asset = new FontAsset |
| 47 | + { |
| 48 | + Offset = context.Position, |
| 49 | + NamePtr = GenericReader.ReadStringPointer(ref context), |
| 50 | + PixelHeight = context.ReadInt32(), |
| 51 | + GlyphCount = context.ReadInt32(), |
| 52 | + Material = MaterialReader.ReadMaterialPointer(ref context), |
| 53 | + GlowMaterial = MaterialReader.ReadMaterialPointer(ref context), |
| 54 | + }; |
| 55 | + |
| 56 | + asset.Glyphs = context.ReadPointer<FontGlyph[]>( |
| 57 | + (ref ZoneReadContext pointerContext, ZonePointer<FontGlyph[]> pointer) => |
| 58 | + { |
| 59 | + var glyphs = new FontGlyph[Math.Max(0, asset.GlyphCount)]; |
| 60 | + for (var i = 0; i < glyphs.Length; i++) |
| 61 | + glyphs[i] = ReadGlyph(ref pointerContext); |
| 62 | + pointer.SetResult(glyphs); |
| 63 | + }); |
| 64 | + |
| 65 | + return asset; |
| 66 | + } |
| 67 | + |
| 68 | + private static FontGlyph ReadGlyph(ref ZoneReadContext context) => new() |
| 69 | + { |
| 70 | + Letter = context.ReadUInt16(), |
| 71 | + X0 = context.ReadByte(), |
| 72 | + Y0 = context.ReadByte(), |
| 73 | + Dx = context.ReadByte(), |
| 74 | + PixelWidth = context.ReadByte(), |
| 75 | + PixelHeight = context.ReadByte(), |
| 76 | + Padding = context.ReadByte(), |
| 77 | + S0 = context.ReadFloat(), |
| 78 | + T0 = context.ReadFloat(), |
| 79 | + S1 = context.ReadFloat(), |
| 80 | + T1 = context.ReadFloat(), |
| 81 | + }; |
| 82 | +} |
0 commit comments