rm2k-lib 1.2.0

Reads and writes the RPG Maker 2000/2003 LCF binary formats (LDB, LMU, LMT, LSD).
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
//! Codecs for the handful of types that do not fit the chunked-struct shape.
//!
//! Fixed-layout structs with no chunk framing ([`Rect`], [`Equipment`], [`Parameters`]), and field
//! kinds too irregular for the schema macro ([`EventCommandList`], [`MoveCommandList`],
//! [`StrList`], [`EmptyBlock`], [`DatabaseVersion`]).
//!
//! [`EventCommandList`] and [`MoveCommandList`] own what the borrowing iterators in
//! [`crate::event`] walk, and document the framing rules. To only *read* commands, prefer those:
//! they work in the core tier and copy nothing.

use crate::{
    cursor::{Ctx, Cursor},
    diag::DiagKind,
    engine::Engine,
    event::{EventCommandIter, MoveCommandIter, move_code},
    field::{DbStr, LcfField, Packed},
    sink::Sink,
    wire,
};
use alloc::vec::Vec;

/// A chunk that exists but carries nothing.
///
/// Three unused 2003-only `Database` chunks (`commoneventD2`, `commoneventD3`, `classD1`). RPG
/// Maker writes them as a zero-length header, and files round-trip only if written back the same
/// way, hence a real type rather than omitting them from the schema.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct EmptyBlock;

impl<'a> LcfField<'a> for EmptyBlock {
    #[inline]
    fn field_default(_engine: Engine) -> Self {
        Self
    }
    #[inline]
    fn read(_data: &'a [u8], _ctx: &mut Ctx<'_>, _owner: &'static str, _id: u32) -> Self {
        Self
    }
    #[inline]
    fn lcf_size(&self, _engine: Engine) -> u32 {
        0
    }
    #[inline]
    fn write<S: Sink>(&self, _sink: &mut S, _engine: Engine) -> Result<(), S::Error> {
        Ok(())
    }
}

/// The database format version, which serialises unlike any other integer.
///
/// Two irregularities, both from `DatabaseVersionField` (`src/reader_struct.h`):
///
/// - A value of `0` writes an **empty** chunk - the header is emitted, the payload is not.
/// - Under RPG Maker 2003 the chunk is *always* written, even at its default. Under 2000 it is
///   written only when non-zero.
///
/// The second rule is why the schema tags this field `db_version` rather than `persist`.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct DatabaseVersion(pub i32);

impl<'a> LcfField<'a> for DatabaseVersion {
    #[inline]
    fn field_default(_engine: Engine) -> Self {
        Self(0)
    }

    #[inline]
    fn read(data: &'a [u8], ctx: &mut Ctx<'_>, owner: &'static str, id: u32) -> Self {
        if data.is_empty() {
            return Self(0);
        }
        Self(<i32 as LcfField>::read(data, ctx, owner, id))
    }

    #[inline]
    fn lcf_size(&self, _engine: Engine) -> u32 {
        if self.0 == 0 {
            0
        } else {
            wire::varint_len(self.0 as u32)
        }
    }

    #[inline]
    fn write<S: Sink>(&self, sink: &mut S, _engine: Engine) -> Result<(), S::Error> {
        if self.0 == 0 {
            return Ok(());
        }
        wire::write_varint(sink, self.0 as u32)
    }
}

/// A map-tree rectangle: four raw little-endian 32-bit values, no chunk framing.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct Rect {
    pub left: u32,
    pub top: u32,
    pub right: u32,
    pub bottom: u32,
}

impl<'a> LcfField<'a> for Rect {
    #[inline]
    fn field_default(_engine: Engine) -> Self {
        Self::default()
    }

    fn read(data: &'a [u8], ctx: &mut Ctx<'_>, owner: &'static str, id: u32) -> Self {
        let mut cur = Cursor::new(data);
        let mut out = Self::default();
        for slot in [&mut out.left, &mut out.top, &mut out.right, &mut out.bottom] {
            match cur.u32_le() {
                Some(v) => *slot = v,
                None => {
                    ctx.report(owner, id, cur.offset(), DiagKind::ShortChunk);
                    break;
                }
            }
        }
        out
    }

    #[inline]
    fn lcf_size(&self, _engine: Engine) -> u32 {
        16
    }

    fn write<S: Sink>(&self, sink: &mut S, _engine: Engine) -> Result<(), S::Error> {
        for value in [self.left, self.top, self.right, self.bottom] {
            sink.write(&value.to_le_bytes())?;
        }
        Ok(())
    }
}

/// An actor's starting equipment: five raw little-endian 16-bit item ids.
///
/// liblcf refuses to parse this at all unless the payload is exactly ten bytes, skipping the
/// chunk and leaving every id zero, and this is reproduced.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct Equipment {
    pub weapon_id: i16,
    pub shield_id: i16,
    pub armor_id: i16,
    pub helmet_id: i16,
    pub accessory_id: i16,
}

impl Equipment {
    pub const SIZE: u32 = 10;
}

impl<'a> LcfField<'a> for Equipment {
    #[inline]
    fn field_default(_engine: Engine) -> Self {
        Self::default()
    }

    fn read(data: &'a [u8], ctx: &mut Ctx<'_>, owner: &'static str, id: u32) -> Self {
        let mut out = Self::default();
        if data.len() != Self::SIZE as usize {
            ctx.report(owner, id, 0, DiagKind::ShortChunk);
            return out;
        }
        let mut cur = Cursor::new(data);
        for slot in [
            &mut out.weapon_id,
            &mut out.shield_id,
            &mut out.armor_id,
            &mut out.helmet_id,
            &mut out.accessory_id,
        ] {
            *slot = cur.i16_le().unwrap_or(0);
        }
        out
    }

    #[inline]
    fn lcf_size(&self, _engine: Engine) -> u32 {
        Self::SIZE
    }

    fn write<S: Sink>(&self, sink: &mut S, _engine: Engine) -> Result<(), S::Error> {
        for value in [
            self.weapon_id,
            self.shield_id,
            self.armor_id,
            self.helmet_id,
            self.accessory_id,
        ] {
            sink.write(&value.to_le_bytes())?;
        }
        Ok(())
    }
}

/// Six parallel stat curves, stored end to end with no separators.
///
/// The payload is split six ways: each curve gets `len / 6` bytes. The encoding gives no curve
/// length, so a payload not a multiple of twelve silently loses its tail, as in liblcf.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Parameters {
    pub maxhp: Packed<i16>,
    pub maxsp: Packed<i16>,
    pub attack: Packed<i16>,
    pub defense: Packed<i16>,
    pub spirit: Packed<i16>,
    pub agility: Packed<i16>,
}

impl Parameters {
    /// The six curves, in wire order.
    const fn curves(&self) -> [&Packed<i16>; 6] {
        [
            &self.maxhp,
            &self.maxsp,
            &self.attack,
            &self.defense,
            &self.spirit,
            &self.agility,
        ]
    }
}

impl<'a> LcfField<'a> for Parameters {
    #[inline]
    fn field_default(_engine: Engine) -> Self {
        Self::default()
    }

    fn read(data: &'a [u8], ctx: &mut Ctx<'_>, owner: &'static str, id: u32) -> Self {
        let per = data.len() / 6;
        if data.len() % 6 != 0 {
            ctx.report(owner, id, 0, DiagKind::RaggedArray);
        }
        let mut out = Self::default();
        for (index, slot) in [
            &mut out.maxhp,
            &mut out.maxsp,
            &mut out.attack,
            &mut out.defense,
            &mut out.spirit,
            &mut out.agility,
        ]
        .into_iter()
        .enumerate()
        {
            let start = index * per;
            let chunk = data.get(start..start + per).unwrap_or(&[]);
            *slot = <Packed<i16> as LcfField>::read(chunk, ctx, owner, id);
        }
        out
    }

    fn lcf_size(&self, engine: Engine) -> u32 {
        self.curves().iter().map(|c| c.lcf_size(engine)).sum()
    }

    fn write<S: Sink>(&self, sink: &mut S, engine: Engine) -> Result<(), S::Error> {
        for curve in self.curves() {
            curve.write(sink, engine)?;
        }
        Ok(())
    }
}

/// One event command, owning its string and parameters.
///
/// The borrowing counterpart is [`EventCommandRef`](crate::event::EventCommandRef), which needs
/// no allocator.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct EventCommand<'a> {
    /// The command code.
    pub code: i32,
    /// Nesting depth inside conditional branches and loops.
    pub indent: i32,
    /// The string operand, verbatim.
    pub string: DbStr<'a>,
    /// The integer operands.
    pub parameters: Vec<i32>,
}

impl EventCommand<'_> {
    fn wire_len(&self) -> u32 {
        crate::event::event_command_len(self.code, self.indent, self.string.len(), &self.parameters)
    }

    fn write_one<S: Sink>(&self, sink: &mut S) -> Result<(), S::Error> {
        wire::write_varint(sink, self.code as u32)?;
        wire::write_varint(sink, self.indent as u32)?;
        wire::write_varint(sink, self.string.len() as u32)?;
        sink.write(self.string.as_bytes())?;
        wire::write_varint(sink, self.parameters.len() as u32)?;
        for &param in &self.parameters {
            wire::write_varint(sink, param as u32)?;
        }
        Ok(())
    }
}

/// A command list, terminated on the wire by four zero bytes.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct EventCommandList<'a>(pub Vec<EventCommand<'a>>);

impl<'a> core::ops::Deref for EventCommandList<'a> {
    type Target = Vec<EventCommand<'a>>;
    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<'a> LcfField<'a> for EventCommandList<'a> {
    #[inline]
    fn field_default(_engine: Engine) -> Self {
        Self(Vec::new())
    }

    fn read(data: &'a [u8], ctx: &mut Ctx<'_>, _owner: &'static str, _id: u32) -> Self {
        let mut out = Vec::new();
        let mut iter = EventCommandIter::new(data, 0);
        while let Some(cmd) = iter.next(ctx) {
            out.push(EventCommand {
                code: cmd.code,
                indent: cmd.indent,
                string: DbStr::from_bytes(cmd.string),
                parameters: cmd.params().collect(),
            });
        }
        Self(out)
    }

    fn lcf_size(&self, _engine: Engine) -> u32 {
        // Four zero bytes terminate the list.
        self.0.iter().map(EventCommand::wire_len).sum::<u32>() + 4
    }

    fn write<S: Sink>(&self, sink: &mut S, _engine: Engine) -> Result<(), S::Error> {
        for cmd in &self.0 {
            cmd.write_one(sink)?;
        }
        sink.write(&[0, 0, 0, 0])
    }

    #[inline]
    fn element_count(&self) -> u32 {
        self.0.len() as u32
    }
}

/// One move-route command, owning its string.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct MoveCommand<'a> {
    /// The command code. See [`move_code`].
    pub code: i32,
    /// The string operand, verbatim; empty for codes that carry none.
    pub string: DbStr<'a>,
    /// First integer operand.
    pub parameter_a: i32,
    /// Second integer operand.
    pub parameter_b: i32,
    /// Third integer operand.
    pub parameter_c: i32,
}

impl MoveCommand<'_> {
    /// How many integer operands this command's code carries.
    const fn int_count(&self) -> usize {
        match self.code {
            move_code::SWITCH_ON | move_code::SWITCH_OFF | move_code::CHANGE_GRAPHIC => 1,
            move_code::PLAY_SOUND_EFFECT => 3,
            _ => 0,
        }
    }

    /// Whether this command's code carries a string operand.
    const fn has_string(&self) -> bool {
        matches!(self.code, move_code::CHANGE_GRAPHIC | move_code::PLAY_SOUND_EFFECT)
    }

    fn wire_len(&self) -> u32 {
        let mut total = wire::varint_len(self.code as u32);
        if self.has_string() {
            total += wire::varint_len(self.string.len() as u32) + self.string.len() as u32;
        }
        for value in [self.parameter_a, self.parameter_b, self.parameter_c]
            .into_iter()
            .take(self.int_count())
        {
            total += wire::varint_len(value as u32);
        }
        total
    }

    fn write_one<S: Sink>(&self, sink: &mut S) -> Result<(), S::Error> {
        wire::write_varint(sink, self.code as u32)?;
        if self.has_string() {
            wire::write_varint(sink, self.string.len() as u32)?;
            sink.write(self.string.as_bytes())?;
        }
        for value in [self.parameter_a, self.parameter_b, self.parameter_c]
            .into_iter()
            .take(self.int_count())
        {
            wire::write_varint(sink, value as u32)?;
        }
        Ok(())
    }
}

/// A move route's commands, running to the end of the chunk with no count and no terminator.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct MoveCommandList<'a>(pub Vec<MoveCommand<'a>>);

impl<'a> core::ops::Deref for MoveCommandList<'a> {
    type Target = Vec<MoveCommand<'a>>;
    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<'a> LcfField<'a> for MoveCommandList<'a> {
    #[inline]
    fn field_default(_engine: Engine) -> Self {
        Self(Vec::new())
    }

    fn read(data: &'a [u8], ctx: &mut Ctx<'_>, _owner: &'static str, _id: u32) -> Self {
        let mut out = Vec::new();
        let mut iter = MoveCommandIter::new(data, 0);
        while let Some(cmd) = iter.next(ctx) {
            out.push(MoveCommand {
                code: cmd.code,
                string: DbStr::from_bytes(cmd.string),
                parameter_a: cmd.parameter_a,
                parameter_b: cmd.parameter_b,
                parameter_c: cmd.parameter_c,
            });
        }
        Self(out)
    }

    fn lcf_size(&self, _engine: Engine) -> u32 {
        self.0.iter().map(MoveCommand::wire_len).sum()
    }

    fn write<S: Sink>(&self, sink: &mut S, _engine: Engine) -> Result<(), S::Error> {
        for cmd in &self.0 {
            cmd.write_one(sink)?;
        }
        Ok(())
    }

    #[inline]
    fn element_count(&self) -> u32 {
        self.0.len() as u32
    }
}

/// A sparse list of strings, run-length encoded over its empty entries.
///
/// The oddest encoding in the format, and the only reason a 64-bit compressed integer exists.
/// Each entry is a length followed by that many bytes, but a length **above `u32::MAX`** is the
/// sentinel `0x8_0000_0000 - gap`, meaning "skip `gap` empty entries". Used by one field: an
/// EasyRPG extension to `SaveSystem`.
///
/// It does not save space on short runs, and liblcf uses it anyway. The sentinel always needs all
/// five bytes while each empty entry it replaces costs one, so a run shorter than six *grows*.
/// Reproduced regardless: the goal is byte-identical output.
///
/// Loading stops once the load's string-list entry budget runs out, reporting `EntryBudgetExhausted`.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct StrList<'a>(pub Vec<DbStr<'a>>);

impl<'a> core::ops::Deref for StrList<'a> {
    type Target = Vec<DbStr<'a>>;
    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

/// The sentinel base a gap marker is subtracted from.
const GAP_BASE: u64 = 0x8_0000_0000;

impl<'a> LcfField<'a> for StrList<'a> {
    #[inline]
    fn field_default(_engine: Engine) -> Self {
        Self(Vec::new())
    }

    fn read(data: &'a [u8], ctx: &mut Ctx<'_>, owner: &'static str, id: u32) -> Self {
        let mut out: Vec<DbStr<'a>> = Vec::new();
        let mut cur = Cursor::new(data);

        while !cur.is_empty() {
            let Some(marker) = cur.varint64() else {
                ctx.report(owner, id, cur.offset(), DiagKind::ShortChunk);
                break;
            };

            if marker.value > u64::from(u32::MAX) {
                // A gap: extend with empty entries rather than storing them. The run's length is not compared
                // with the chunk's: the writer compresses runs, so a long run can be longer than its compressed
                // chunk. A marker at or past `GAP_BASE` names an empty run, which the writer never emits.
                let gap = GAP_BASE.saturating_sub(marker.value);
                if gap == 0 {
                    ctx.report(owner, id, cur.offset(), DiagKind::BadVarint);
                    break;
                }
                if !ctx.take_entries(gap) {
                    ctx.report(owner, id, cur.offset(), DiagKind::EntryBudgetExhausted);
                    break;
                }
                // `take_entries` succeeded, so `gap` is at most `ENTRY_BUDGET`, which fits any `usize`.
                out.resize(out.len() + gap as usize, DbStr::default());
                continue;
            }

            // Charged before the bytes are read: any failure below ends the loop, so at most one unit is wasted.
            if !ctx.take_entries(1) {
                ctx.report(owner, id, cur.offset(), DiagKind::EntryBudgetExhausted);
                break;
            }
            let Some(bytes) = cur.take(marker.value as usize) else {
                ctx.report(owner, id, cur.offset(), DiagKind::ShortChunk);
                break;
            };
            out.push(DbStr::from_bytes(bytes));
        }

        Self(out)
    }

    fn lcf_size(&self, _engine: Engine) -> u32 {
        let mut total = 0u32;
        let mut gap = 0u64;
        for entry in &self.0 {
            if entry.is_empty() {
                gap += 1;
                continue;
            }
            if gap > 0 {
                total += wire::varint64_len(GAP_BASE - gap);
                gap = 0;
            }
            let len = entry.len() as u32;
            total += wire::varint_len(len) + len;
        }
        // A trailing run of empties is dropped, exactly as liblcf's writer drops it.
        total
    }

    fn write<S: Sink>(&self, sink: &mut S, _engine: Engine) -> Result<(), S::Error> {
        let mut gap = 0u64;
        for entry in &self.0 {
            if entry.is_empty() {
                gap += 1;
                continue;
            }
            if gap > 0 {
                wire::write_varint64(sink, GAP_BASE - gap)?;
                gap = 0;
            }
            wire::write_varint(sink, entry.len() as u32)?;
            sink.write(entry.as_bytes())?;
        }
        Ok(())
    }

    #[inline]
    fn element_count(&self) -> u32 {
        self.0.len() as u32
    }
}