whiteoutlib 0.1.3

Read and write Blizzard game assets from Rust: models (MDX, M2, M3), textures (BLP, DDS, PNG, JPEG, BMP, TGA, TIFF, GIF) and archives (CASC, MPQ).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2026 Fernando Sahmkow

#include "mdl_parser.h"
#include "mdl_tokenizer.h"

#include <cstdlib>
#include <cstring>

namespace whiteout {
namespace mdx {

namespace {

// ─── Token stream helper ─────────────────────────────────────────────────────

class TokenStream {
public:
    explicit TokenStream(std::vector<MdlToken> tokens) : m_tokens(std::move(tokens)), m_pos(0) {}

    const MdlToken& peek() const {
        return m_tokens[m_pos];
    }
    const MdlToken& peek(std::size_t ahead) const {
        auto idx = m_pos + ahead;
        return idx < m_tokens.size() ? m_tokens[idx] : m_tokens.back();
    }
    const MdlToken& advance() {
        if (m_pos + 1 < m_tokens.size())
            return m_tokens[m_pos++];
        return m_tokens[m_pos]; // stay on EOF sentinel
    }
    bool atEnd() const {
        return m_tokens[m_pos].type == MdlTokenType::EndOfFile;
    }

    bool check(MdlTokenType type) const {
        return peek().type == type;
    }
    bool match(MdlTokenType type) {
        if (check(type)) {
            advance();
            return true;
        }
        return false;
    }

    // Consume a token of the given type, or record an error and return false.
    bool expect(MdlTokenType type, std::vector<MdlParseError>& errors, const char* context) {
        if (check(type)) {
            advance();
            return true;
        }
        auto& t = peek();
        errors.push_back(
            {std::string("expected ") + context + " but got '" + std::string(t.text) + "'", t.line,
             t.column});
        return false;
    }

    std::size_t pos() const {
        return m_pos;
    }
    void setPos(std::size_t p) {
        m_pos = p;
    }

private:
    std::vector<MdlToken> m_tokens;
    std::size_t m_pos;
};

// ─── Numeric conversion ─────────────────────────────────────────────────────

static f64 toNumber(std::string_view text) {
    // std::from_chars for double is not universally available on all MSVC
    // versions for all locales, so we use strtod via a null-terminated copy.
    // The text is typically short (< 30 chars).
    char buf[64];
    auto len = text.size() < 63 ? text.size() : 63;
    std::memcpy(buf, text.data(), len);
    buf[len] = '\0';
    return std::strtod(buf, nullptr);
}

static i32 toInt(std::string_view text) {
    char buf[32];
    auto len = text.size() < 31 ? text.size() : 31;
    std::memcpy(buf, text.data(), len);
    buf[len] = '\0';
    return static_cast<i32>(std::strtoll(buf, nullptr, 10));
}

// ─── Parser core ─────────────────────────────────────────────────────────────

class ParserImpl {
public:
    explicit ParserImpl(TokenStream& ts, std::vector<MdlParseError>& errors)
        : m_ts(ts), m_errors(errors) {}

    // Parse all top-level blocks.
    std::vector<MdlNode> parseDocument() {
        std::vector<MdlNode> roots;
        while (!m_ts.atEnd()) {
            if (m_ts.check(MdlTokenType::Identifier)) {
                roots.push_back(parseBlock());
            } else {
                // Skip unexpected tokens at top level.
                auto& t = m_ts.advance();
                m_errors.push_back({"unexpected token at top level: '" + std::string(t.text) + "'",
                                    t.line, t.column});
            }
        }
        return roots;
    }

private:
    TokenStream& m_ts;
    std::vector<MdlParseError>& m_errors;

    // ── Block parsing ────────────────────────────────────────────────────

    MdlNode parseBlock() {
        MdlNode node;
        node.name = std::string(m_ts.advance().text); // consume block name

        // Collect header parameters (strings and numbers) before '{'
        while (!m_ts.atEnd() && !m_ts.check(MdlTokenType::OpenBrace)) {
            if (m_ts.check(MdlTokenType::String)) {
                node.headerParams.push_back(MdlValue{std::string(m_ts.advance().text)});
            } else if (m_ts.check(MdlTokenType::Number)) {
                node.headerParams.push_back(MdlValue{toNumber(m_ts.advance().text)});
            } else {
                break; // unexpected token
            }
        }

        if (!m_ts.expect(MdlTokenType::OpenBrace, m_errors, "'{'")) {
            return node;
        }

        parseBlockBody(node);

        m_ts.expect(MdlTokenType::CloseBrace, m_errors, "'}'");
        return node;
    }

    void parseBlockBody(MdlNode& node) {
        while (!m_ts.atEnd() && !m_ts.check(MdlTokenType::CloseBrace)) {
            if (!m_ts.check(MdlTokenType::Identifier)) {
                // Could be a raw value line (e.g. in VertexGroup: "0,")
                // or a brace-delimited array element (e.g. in Vertices: "{ 1, 2, 3 },")
                if (m_ts.check(MdlTokenType::Number) || m_ts.check(MdlTokenType::OpenBrace)) {
                    // Treat as an anonymous property with values
                    node.children.push_back(parseAnonymousValues());
                    continue;
                }
                auto& t = m_ts.advance();
                m_errors.push_back({"unexpected token in block body: '" + std::string(t.text) + "'",
                                    t.line, t.column});
                continue;
            }

            // We have an identifier. Determine what follows:
            //  1. "static" keyword prefix  → property
            //  2. IDENT '{'                → sub-block (e.g. Layer, Anim, Target, Bitmap)
            //  3. IDENT STRING|NUMBER+ '{' → sub-block with header params
            //  4. IDENT NUMBER '{' IDENT ','  → animation track (count + interp)
            //  5. IDENT NUMBER '{'         → could be track (EventTrack) or block (Vertices N {)
            //  6. IDENT value ','          → property
            //  7. IDENT ','                → bare flag

            auto& first = m_ts.peek();

            // Handle "static" prefix
            if (first.text == "static") {
                m_ts.advance(); // consume "static"
                node.children.push_back(parseProperty(true));
                continue;
            }

            // Look ahead to classify
            auto& second = m_ts.peek(1);

            // IDENT ','  → bare flag
            if (second.type == MdlTokenType::Comma) {
                MdlProperty prop;
                prop.name = std::string(m_ts.advance().text);
                m_ts.advance(); // consume comma
                node.children.push_back(std::move(prop));
                continue;
            }

            // IDENT '{' → either sub-block or property with vector value
            // Disambiguate by peeking inside the brace:
            //   - First token is Number → property (e.g. Alpha {255, 200, 0},)
            //   - First token is Identifier → sub-block (e.g. Layer { FilterMode None, })
            //   - First token is OpenBrace → sub-block with vector data (e.g. Vertices { {1,2,3},
            //   })
            //   - First token is CloseBrace → empty sub-block
            if (second.type == MdlTokenType::OpenBrace) {
                auto& insideBrace = m_ts.peek(2);
                if (insideBrace.type == MdlTokenType::Number ||
                    insideBrace.type == MdlTokenType::String) {
                    // Property with vector value: Alpha {255, 200, 0},
                    node.children.push_back(parseProperty(false));
                } else {
                    // Sub-block: Layer { ... }, SegmentColor { Color { ... } ... }
                    node.children.push_back(parseBlock());
                    m_ts.match(MdlTokenType::Comma); // trailing comma after block
                }
                continue;
            }

            // IDENT NUMBER ... → could be track, block, or property
            if (second.type == MdlTokenType::Number) {
                if (tryParseAsTrackOrBlock(node)) {
                    continue;
                }
                // Fall through to property
            }

            // IDENT STRING ... → block or property
            if (second.type == MdlTokenType::String) {
                auto& third = m_ts.peek(2);
                if (third.type == MdlTokenType::OpenBrace || third.type == MdlTokenType::Number) {
                    // sub-block with string param: Anim "Stand 1" { ... }
                    // or: Bone "name" { ... } — third could be number then '{'
                    node.children.push_back(parseBlock());
                    m_ts.match(MdlTokenType::Comma); // trailing comma after block
                    continue;
                }
                // Otherwise property: Image "foo.blp",
                node.children.push_back(parseProperty(false));
                continue;
            }

            // IDENT IDENT ... → property (e.g. FilterMode None, or GeosetId Multiple,)
            if (second.type == MdlTokenType::Identifier) {
                node.children.push_back(parseProperty(false));
                continue;
            }

            // Fallback: treat as property
            node.children.push_back(parseProperty(false));
        }
    }

    // Attempt to parse "IDENT NUMBER ..." as either an animation track or a
    // sub-block with numeric header params. Returns true if successfully parsed.
    bool tryParseAsTrackOrBlock(MdlNode& parent) {
        // Pattern: IDENT NUMBER '{'
        // Could be:
        //   - Animation track:  Translation 402 { Linear, ... }
        //   - Data block:       Vertices 2085 { ... }
        //   - Event track:      EventTrack 1 { 0, ... }
        //
        // Disambiguation: peek inside the brace to see if first child is
        // an interp keyword or a number followed by colon (keyframe).

        // Check that we have IDENT NUMBER+ '{'
        std::size_t lookahead = 1; // position 0 = IDENT
        while (m_ts.peek(lookahead).type == MdlTokenType::Number) {
            ++lookahead;
        }

        // Optional engine HD-texture slot suffix between the count and the
        // '{': `TextureID 3 <= 2 { ... }`. Skip it so the brace is still found.
        if (m_ts.peek(lookahead).type == MdlTokenType::LessEqual &&
            m_ts.peek(lookahead + 1).type == MdlTokenType::Number) {
            lookahead += 2;
        }

        if (m_ts.peek(lookahead).type != MdlTokenType::OpenBrace) {
            // Not a block/track — must be a property: IDENT NUMBER ','
            parent.children.push_back(parseProperty(false));
            return true;
        }

        // Now peek past the '{' to see what's inside
        auto insideIdx = lookahead + 1;
        auto& insideToken = m_ts.peek(insideIdx);

        // Check for interpolation keyword → animation track
        bool isTrack = false;
        if (insideToken.type == MdlTokenType::Identifier) {
            auto txt = insideToken.text;
            if (txt == "Linear" || txt == "Hermite" || txt == "Bezier" || txt == "DontInterp") {
                isTrack = true;
            }
        }

        // Check for bare number followed by colon → keyframes (EventTrack-like).
        // A bare `NUMBER,` body is shared by EventTrack and by data blocks such
        // as `SkinWeights N { 1, 2, ... }` (engine dialect); only EventTrack is
        // actually a track, so gate the comma case on the block name.
        if (!isTrack && insideToken.type == MdlTokenType::Number) {
            auto& afterNum = m_ts.peek(insideIdx + 1);
            if (afterNum.type == MdlTokenType::Colon) {
                isTrack = true;
            } else if (afterNum.type == MdlTokenType::Comma ||
                       afterNum.type == MdlTokenType::CloseBrace) {
                isTrack = (m_ts.peek().text == "EventTrack");
            }
        }

        if (isTrack) {
            parent.children.push_back(parseAnimTrack());
            m_ts.match(MdlTokenType::Comma); // trailing comma after track
            return true;
        }

        // It's a data block (e.g. Vertices 2085 { ... })
        parent.children.push_back(parseBlock());
        m_ts.match(MdlTokenType::Comma); // trailing comma after block
        return true;
    }

    // ── Property parsing ─────────────────────────────────────────────────

    MdlProperty parseProperty(bool isStatic) {
        MdlProperty prop;
        prop.isStatic = isStatic;
        prop.name = std::string(m_ts.advance().text); // consume name

        // Collect values until comma or end of block.
        // Break early if the next token is an identifier or open-brace,
        // which indicates we've left the property's value context and
        // reached the next entry in the parent block.
        while (!m_ts.atEnd() && !m_ts.check(MdlTokenType::Comma) &&
               !m_ts.check(MdlTokenType::CloseBrace) && !m_ts.check(MdlTokenType::LessEqual)) {
            prop.values.push_back(parseValue());
            if (m_ts.check(MdlTokenType::Identifier) || m_ts.check(MdlTokenType::OpenBrace) ||
                m_ts.check(MdlTokenType::Number)) {
                break;
            }
        }

        // Engine MDL HD-texture slot suffix: "TextureID 5 <= 1,"
        if (m_ts.check(MdlTokenType::LessEqual)) {
            m_ts.advance(); // consume "<="
            if (m_ts.check(MdlTokenType::Number)) {
                prop.slot = static_cast<i32>(toNumber(m_ts.advance().text));
            } else {
                auto& t = m_ts.peek();
                m_errors.push_back(
                    {"expected slot number after '<=', got '" + std::string(t.text) + "'", t.line,
                     t.column});
            }
        }

        m_ts.match(MdlTokenType::Comma); // consume optional trailing comma
        return prop;
    }

    // Parse anonymous values (numbers or vectors without a leading identifier).
    // Used inside data blocks like VertexGroup { 0, 0, ... } or Vertices { { 1,2,3 }, ... }
    MdlProperty parseAnonymousValues() {
        MdlProperty prop;
        prop.name = ""; // anonymous

        // Collect values until we hit something that starts a new entry
        prop.values.push_back(parseValue());
        m_ts.match(MdlTokenType::Comma);

        return prop;
    }

    // ── Value parsing ────────────────────────────────────────────────────

    MdlValue parseValue() {
        if (m_ts.check(MdlTokenType::Number)) {
            return MdlValue{toNumber(m_ts.advance().text)};
        }
        if (m_ts.check(MdlTokenType::String)) {
            return MdlValue{std::string(m_ts.advance().text)};
        }
        if (m_ts.check(MdlTokenType::Identifier)) {
            // Identifiers as values (e.g. "None", "Multiple", "Linear", filter modes)
            return MdlValue{std::string(m_ts.advance().text)};
        }
        if (m_ts.check(MdlTokenType::OpenBrace)) {
            return parseValueArray();
        }

        // Error recovery
        auto& t = m_ts.advance();
        m_errors.push_back(
            {"unexpected token in value: '" + std::string(t.text) + "'", t.line, t.column});
        return MdlValue{0.0};
    }

    // Parse { val, val, ... }
    MdlValue parseValueArray() {
        m_ts.advance(); // consume '{'
        MdlValue::Array arr;

        while (!m_ts.atEnd() && !m_ts.check(MdlTokenType::CloseBrace)) {
            arr.push_back(parseValue());
            m_ts.match(MdlTokenType::Comma); // optional comma between elements
        }

        m_ts.expect(MdlTokenType::CloseBrace, m_errors, "'}'");
        return MdlValue{std::move(arr)};
    }

    // ── Animation track parsing ──────────────────────────────────────────

    MdlAnimTrack parseAnimTrack() {
        MdlAnimTrack track;
        track.name = std::string(m_ts.advance().text); // consume track name

        // Parse count
        if (m_ts.check(MdlTokenType::Number)) {
            track.count = static_cast<u32>(toNumber(m_ts.advance().text));
        }

        // Optional engine HD-texture slot suffix: `TextureID 3 <= 2 { ... }`.
        if (m_ts.check(MdlTokenType::LessEqual)) {
            m_ts.advance(); // consume "<="
            if (m_ts.check(MdlTokenType::Number)) {
                track.slot = static_cast<i32>(toNumber(m_ts.advance().text));
            } else {
                auto& t = m_ts.peek();
                m_errors.push_back(
                    {"expected slot number after '<=', got '" + std::string(t.text) + "'", t.line,
                     t.column});
            }
        }

        m_ts.expect(MdlTokenType::OpenBrace, m_errors, "'{'");

        // Check for interpolation type keyword
        if (m_ts.check(MdlTokenType::Identifier)) {
            auto txt = m_ts.peek().text;
            if (txt == "Linear" || txt == "Hermite" || txt == "Bezier" || txt == "DontInterp") {
                track.interpolation = std::string(m_ts.advance().text);
                m_ts.match(MdlTokenType::Comma);
            }
        }

        // Optional: GlobalSeqId N,
        if (m_ts.check(MdlTokenType::Identifier) && m_ts.peek().text == "GlobalSeqId") {
            m_ts.advance();
            if (m_ts.check(MdlTokenType::Number)) {
                track.globalSequenceId = static_cast<u32>(toNumber(m_ts.advance().text));
            }
            m_ts.match(MdlTokenType::Comma);
        }

        // Parse keyframes
        while (!m_ts.atEnd() && !m_ts.check(MdlTokenType::CloseBrace)) {
            if (m_ts.check(MdlTokenType::Number)) {
                auto& nextAfterNum = m_ts.peek(1);
                if (nextAfterNum.type == MdlTokenType::Colon) {
                    // Standard keyframe: TIME: value,
                    track.keyframes.push_back(parseKeyframe(track.interpolation == "Hermite" ||
                                                            track.interpolation == "Bezier"));
                } else {
                    // EventTrack-style bare value: TIME,
                    MdlKeyframe kf;
                    kf.time = toInt(m_ts.advance().text);
                    kf.value = MdlValue{static_cast<f64>(kf.time)};
                    m_ts.match(MdlTokenType::Comma);
                    track.keyframes.push_back(std::move(kf));
                }
            } else {
                // Unexpected token in track body
                auto& t = m_ts.advance();
                m_errors.push_back(
                    {"unexpected token in animation track: '" + std::string(t.text) + "'", t.line,
                     t.column});
            }
        }

        m_ts.expect(MdlTokenType::CloseBrace, m_errors, "'}'");
        return track;
    }

    MdlKeyframe parseKeyframe(bool expectTangents) {
        MdlKeyframe kf;
        kf.time = toInt(m_ts.advance().text); // consume timestamp
        m_ts.expect(MdlTokenType::Colon, m_errors, "':'");
        kf.value = parseValue();
        m_ts.match(MdlTokenType::Comma);

        // Check for InTan / OutTan (Hermite/Bezier)
        if (expectTangents && m_ts.check(MdlTokenType::Identifier) && m_ts.peek().text == "InTan") {
            m_ts.advance(); // consume "InTan"
            kf.inTan = parseValue();
            m_ts.match(MdlTokenType::Comma);

            if (m_ts.check(MdlTokenType::Identifier) && m_ts.peek().text == "OutTan") {
                m_ts.advance(); // consume "OutTan"
                kf.outTan = parseValue();
                m_ts.match(MdlTokenType::Comma);
            }
            kf.hasTangents = true;
        }

        return kf;
    }
};

} // anonymous namespace

// ─── Public API ──────────────────────────────────────────────────────────────

MdlDocument MdlParser::parse(std::string_view source) {
    auto tokens = MdlTokenizer::tokenize(source);
    TokenStream ts(std::move(tokens));

    MdlDocument doc;
    ParserImpl parser(ts, doc.errors);
    doc.roots = parser.parseDocument();
    return doc;
}

} // namespace mdx
} // namespace whiteout