Skip to main content

rm2k/
raw.rs

1//! Codecs for the handful of types that do not fit the chunked-struct shape.
2//!
3//! Fixed-layout structs with no chunk framing ([`Rect`], [`Equipment`], [`Parameters`]), and field
4//! kinds too irregular for the schema macro ([`EventCommandList`], [`MoveCommandList`],
5//! [`StrList`], [`EmptyBlock`], [`DatabaseVersion`]).
6//!
7//! [`EventCommandList`] and [`MoveCommandList`] own what the borrowing iterators in
8//! [`crate::event`] walk, and document the framing rules. To only *read* commands, prefer those:
9//! they work in the core tier and copy nothing.
10
11use crate::{
12    cursor::{Ctx, Cursor},
13    diag::DiagKind,
14    engine::Engine,
15    event::{EventCommandIter, MoveCommandIter, move_code},
16    field::{DbStr, LcfField, Packed},
17    sink::Sink,
18    wire,
19};
20use alloc::vec::Vec;
21
22/// A chunk that exists but carries nothing.
23///
24/// Three unused 2003-only `Database` chunks (`commoneventD2`, `commoneventD3`, `classD1`). RPG
25/// Maker writes them as a zero-length header, and files round-trip only if written back the same
26/// way, hence a real type rather than omitting them from the schema.
27#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
28pub struct EmptyBlock;
29
30impl<'a> LcfField<'a> for EmptyBlock {
31    #[inline]
32    fn field_default(_engine: Engine) -> Self {
33        Self
34    }
35    #[inline]
36    fn read(_data: &'a [u8], _ctx: &mut Ctx<'_>, _owner: &'static str, _id: u32) -> Self {
37        Self
38    }
39    #[inline]
40    fn lcf_size(&self, _engine: Engine) -> u32 {
41        0
42    }
43    #[inline]
44    fn write<S: Sink>(&self, _sink: &mut S, _engine: Engine) -> Result<(), S::Error> {
45        Ok(())
46    }
47}
48
49/// The database format version, which serialises unlike any other integer.
50///
51/// Two irregularities, both from `DatabaseVersionField` (`src/reader_struct.h`):
52///
53/// - A value of `0` writes an **empty** chunk - the header is emitted, the payload is not.
54/// - Under RPG Maker 2003 the chunk is *always* written, even at its default. Under 2000 it is
55///   written only when non-zero.
56///
57/// The second rule is why the schema tags this field `db_version` rather than `persist`.
58#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
59pub struct DatabaseVersion(pub i32);
60
61impl<'a> LcfField<'a> for DatabaseVersion {
62    #[inline]
63    fn field_default(_engine: Engine) -> Self {
64        Self(0)
65    }
66
67    #[inline]
68    fn read(data: &'a [u8], ctx: &mut Ctx<'_>, owner: &'static str, id: u32) -> Self {
69        if data.is_empty() {
70            return Self(0);
71        }
72        Self(<i32 as LcfField>::read(data, ctx, owner, id))
73    }
74
75    #[inline]
76    fn lcf_size(&self, _engine: Engine) -> u32 {
77        if self.0 == 0 {
78            0
79        } else {
80            wire::varint_len(self.0 as u32)
81        }
82    }
83
84    #[inline]
85    fn write<S: Sink>(&self, sink: &mut S, _engine: Engine) -> Result<(), S::Error> {
86        if self.0 == 0 {
87            return Ok(());
88        }
89        wire::write_varint(sink, self.0 as u32)
90    }
91}
92
93/// A map-tree rectangle: four raw little-endian 32-bit values, no chunk framing.
94#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
95pub struct Rect {
96    pub left: u32,
97    pub top: u32,
98    pub right: u32,
99    pub bottom: u32,
100}
101
102impl<'a> LcfField<'a> for Rect {
103    #[inline]
104    fn field_default(_engine: Engine) -> Self {
105        Self::default()
106    }
107
108    fn read(data: &'a [u8], ctx: &mut Ctx<'_>, owner: &'static str, id: u32) -> Self {
109        let mut cur = Cursor::new(data);
110        let mut out = Self::default();
111        for slot in [&mut out.left, &mut out.top, &mut out.right, &mut out.bottom] {
112            match cur.u32_le() {
113                Some(v) => *slot = v,
114                None => {
115                    ctx.report(owner, id, cur.offset(), DiagKind::ShortChunk);
116                    break;
117                }
118            }
119        }
120        out
121    }
122
123    #[inline]
124    fn lcf_size(&self, _engine: Engine) -> u32 {
125        16
126    }
127
128    fn write<S: Sink>(&self, sink: &mut S, _engine: Engine) -> Result<(), S::Error> {
129        for value in [self.left, self.top, self.right, self.bottom] {
130            sink.write(&value.to_le_bytes())?;
131        }
132        Ok(())
133    }
134}
135
136/// An actor's starting equipment: five raw little-endian 16-bit item ids.
137///
138/// liblcf refuses to parse this at all unless the payload is exactly ten bytes, skipping the
139/// chunk and leaving every id zero, and this is reproduced.
140#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
141pub struct Equipment {
142    pub weapon_id: i16,
143    pub shield_id: i16,
144    pub armor_id: i16,
145    pub helmet_id: i16,
146    pub accessory_id: i16,
147}
148
149impl Equipment {
150    pub const SIZE: u32 = 10;
151}
152
153impl<'a> LcfField<'a> for Equipment {
154    #[inline]
155    fn field_default(_engine: Engine) -> Self {
156        Self::default()
157    }
158
159    fn read(data: &'a [u8], ctx: &mut Ctx<'_>, owner: &'static str, id: u32) -> Self {
160        let mut out = Self::default();
161        if data.len() != Self::SIZE as usize {
162            ctx.report(owner, id, 0, DiagKind::ShortChunk);
163            return out;
164        }
165        let mut cur = Cursor::new(data);
166        for slot in [
167            &mut out.weapon_id,
168            &mut out.shield_id,
169            &mut out.armor_id,
170            &mut out.helmet_id,
171            &mut out.accessory_id,
172        ] {
173            *slot = cur.i16_le().unwrap_or(0);
174        }
175        out
176    }
177
178    #[inline]
179    fn lcf_size(&self, _engine: Engine) -> u32 {
180        Self::SIZE
181    }
182
183    fn write<S: Sink>(&self, sink: &mut S, _engine: Engine) -> Result<(), S::Error> {
184        for value in [
185            self.weapon_id,
186            self.shield_id,
187            self.armor_id,
188            self.helmet_id,
189            self.accessory_id,
190        ] {
191            sink.write(&value.to_le_bytes())?;
192        }
193        Ok(())
194    }
195}
196
197/// Six parallel stat curves, stored end to end with no separators.
198///
199/// The payload is split six ways: each curve gets `len / 6` bytes. The encoding gives no curve
200/// length, so a payload not a multiple of twelve silently loses its tail, as in liblcf.
201#[derive(Clone, Debug, Default, Eq, PartialEq)]
202pub struct Parameters {
203    pub maxhp: Packed<i16>,
204    pub maxsp: Packed<i16>,
205    pub attack: Packed<i16>,
206    pub defense: Packed<i16>,
207    pub spirit: Packed<i16>,
208    pub agility: Packed<i16>,
209}
210
211impl Parameters {
212    /// The six curves, in wire order.
213    const fn curves(&self) -> [&Packed<i16>; 6] {
214        [
215            &self.maxhp,
216            &self.maxsp,
217            &self.attack,
218            &self.defense,
219            &self.spirit,
220            &self.agility,
221        ]
222    }
223}
224
225impl<'a> LcfField<'a> for Parameters {
226    #[inline]
227    fn field_default(_engine: Engine) -> Self {
228        Self::default()
229    }
230
231    fn read(data: &'a [u8], ctx: &mut Ctx<'_>, owner: &'static str, id: u32) -> Self {
232        let per = data.len() / 6;
233        if data.len() % 6 != 0 {
234            ctx.report(owner, id, 0, DiagKind::RaggedArray);
235        }
236        let mut out = Self::default();
237        for (index, slot) in [
238            &mut out.maxhp,
239            &mut out.maxsp,
240            &mut out.attack,
241            &mut out.defense,
242            &mut out.spirit,
243            &mut out.agility,
244        ]
245        .into_iter()
246        .enumerate()
247        {
248            let start = index * per;
249            let chunk = data.get(start..start + per).unwrap_or(&[]);
250            *slot = <Packed<i16> as LcfField>::read(chunk, ctx, owner, id);
251        }
252        out
253    }
254
255    fn lcf_size(&self, engine: Engine) -> u32 {
256        self.curves().iter().map(|c| c.lcf_size(engine)).sum()
257    }
258
259    fn write<S: Sink>(&self, sink: &mut S, engine: Engine) -> Result<(), S::Error> {
260        for curve in self.curves() {
261            curve.write(sink, engine)?;
262        }
263        Ok(())
264    }
265}
266
267/// One event command, owning its string and parameters.
268///
269/// The borrowing counterpart is [`EventCommandRef`](crate::event::EventCommandRef), which needs
270/// no allocator.
271#[derive(Clone, Debug, Default, Eq, PartialEq)]
272pub struct EventCommand<'a> {
273    /// The command code.
274    pub code: i32,
275    /// Nesting depth inside conditional branches and loops.
276    pub indent: i32,
277    /// The string operand, verbatim.
278    pub string: DbStr<'a>,
279    /// The integer operands.
280    pub parameters: Vec<i32>,
281}
282
283impl EventCommand<'_> {
284    fn wire_len(&self) -> u32 {
285        crate::event::event_command_len(self.code, self.indent, self.string.len(), &self.parameters)
286    }
287
288    fn write_one<S: Sink>(&self, sink: &mut S) -> Result<(), S::Error> {
289        wire::write_varint(sink, self.code as u32)?;
290        wire::write_varint(sink, self.indent as u32)?;
291        wire::write_varint(sink, self.string.len() as u32)?;
292        sink.write(self.string.as_bytes())?;
293        wire::write_varint(sink, self.parameters.len() as u32)?;
294        for &param in &self.parameters {
295            wire::write_varint(sink, param as u32)?;
296        }
297        Ok(())
298    }
299}
300
301/// A command list, terminated on the wire by four zero bytes.
302#[derive(Clone, Debug, Default, Eq, PartialEq)]
303pub struct EventCommandList<'a>(pub Vec<EventCommand<'a>>);
304
305impl<'a> core::ops::Deref for EventCommandList<'a> {
306    type Target = Vec<EventCommand<'a>>;
307    #[inline]
308    fn deref(&self) -> &Self::Target {
309        &self.0
310    }
311}
312
313impl<'a> LcfField<'a> for EventCommandList<'a> {
314    #[inline]
315    fn field_default(_engine: Engine) -> Self {
316        Self(Vec::new())
317    }
318
319    fn read(data: &'a [u8], ctx: &mut Ctx<'_>, _owner: &'static str, _id: u32) -> Self {
320        let mut out = Vec::new();
321        let mut iter = EventCommandIter::new(data, 0);
322        while let Some(cmd) = iter.next(ctx) {
323            out.push(EventCommand {
324                code: cmd.code,
325                indent: cmd.indent,
326                string: DbStr::from_bytes(cmd.string),
327                parameters: cmd.params().collect(),
328            });
329        }
330        Self(out)
331    }
332
333    fn lcf_size(&self, _engine: Engine) -> u32 {
334        // Four zero bytes terminate the list.
335        self.0.iter().map(EventCommand::wire_len).sum::<u32>() + 4
336    }
337
338    fn write<S: Sink>(&self, sink: &mut S, _engine: Engine) -> Result<(), S::Error> {
339        for cmd in &self.0 {
340            cmd.write_one(sink)?;
341        }
342        sink.write(&[0, 0, 0, 0])
343    }
344
345    #[inline]
346    fn element_count(&self) -> u32 {
347        self.0.len() as u32
348    }
349}
350
351/// One move-route command, owning its string.
352#[derive(Clone, Debug, Default, Eq, PartialEq)]
353pub struct MoveCommand<'a> {
354    /// The command code. See [`move_code`].
355    pub code: i32,
356    /// The string operand, verbatim; empty for codes that carry none.
357    pub string: DbStr<'a>,
358    /// First integer operand.
359    pub parameter_a: i32,
360    /// Second integer operand.
361    pub parameter_b: i32,
362    /// Third integer operand.
363    pub parameter_c: i32,
364}
365
366impl MoveCommand<'_> {
367    /// How many integer operands this command's code carries.
368    const fn int_count(&self) -> usize {
369        match self.code {
370            move_code::SWITCH_ON | move_code::SWITCH_OFF | move_code::CHANGE_GRAPHIC => 1,
371            move_code::PLAY_SOUND_EFFECT => 3,
372            _ => 0,
373        }
374    }
375
376    /// Whether this command's code carries a string operand.
377    const fn has_string(&self) -> bool {
378        matches!(self.code, move_code::CHANGE_GRAPHIC | move_code::PLAY_SOUND_EFFECT)
379    }
380
381    fn wire_len(&self) -> u32 {
382        let mut total = wire::varint_len(self.code as u32);
383        if self.has_string() {
384            total += wire::varint_len(self.string.len() as u32) + self.string.len() as u32;
385        }
386        for value in [self.parameter_a, self.parameter_b, self.parameter_c]
387            .into_iter()
388            .take(self.int_count())
389        {
390            total += wire::varint_len(value as u32);
391        }
392        total
393    }
394
395    fn write_one<S: Sink>(&self, sink: &mut S) -> Result<(), S::Error> {
396        wire::write_varint(sink, self.code as u32)?;
397        if self.has_string() {
398            wire::write_varint(sink, self.string.len() as u32)?;
399            sink.write(self.string.as_bytes())?;
400        }
401        for value in [self.parameter_a, self.parameter_b, self.parameter_c]
402            .into_iter()
403            .take(self.int_count())
404        {
405            wire::write_varint(sink, value as u32)?;
406        }
407        Ok(())
408    }
409}
410
411/// A move route's commands, running to the end of the chunk with no count and no terminator.
412#[derive(Clone, Debug, Default, Eq, PartialEq)]
413pub struct MoveCommandList<'a>(pub Vec<MoveCommand<'a>>);
414
415impl<'a> core::ops::Deref for MoveCommandList<'a> {
416    type Target = Vec<MoveCommand<'a>>;
417    #[inline]
418    fn deref(&self) -> &Self::Target {
419        &self.0
420    }
421}
422
423impl<'a> LcfField<'a> for MoveCommandList<'a> {
424    #[inline]
425    fn field_default(_engine: Engine) -> Self {
426        Self(Vec::new())
427    }
428
429    fn read(data: &'a [u8], ctx: &mut Ctx<'_>, _owner: &'static str, _id: u32) -> Self {
430        let mut out = Vec::new();
431        let mut iter = MoveCommandIter::new(data, 0);
432        while let Some(cmd) = iter.next(ctx) {
433            out.push(MoveCommand {
434                code: cmd.code,
435                string: DbStr::from_bytes(cmd.string),
436                parameter_a: cmd.parameter_a,
437                parameter_b: cmd.parameter_b,
438                parameter_c: cmd.parameter_c,
439            });
440        }
441        Self(out)
442    }
443
444    fn lcf_size(&self, _engine: Engine) -> u32 {
445        self.0.iter().map(MoveCommand::wire_len).sum()
446    }
447
448    fn write<S: Sink>(&self, sink: &mut S, _engine: Engine) -> Result<(), S::Error> {
449        for cmd in &self.0 {
450            cmd.write_one(sink)?;
451        }
452        Ok(())
453    }
454
455    #[inline]
456    fn element_count(&self) -> u32 {
457        self.0.len() as u32
458    }
459}
460
461/// A sparse list of strings, run-length encoded over its empty entries.
462///
463/// The oddest encoding in the format, and the only reason a 64-bit compressed integer exists.
464/// Each entry is a length followed by that many bytes, but a length **above `u32::MAX`** is the
465/// sentinel `0x8_0000_0000 - gap`, meaning "skip `gap` empty entries". Used by one field: an
466/// EasyRPG extension to `SaveSystem`.
467///
468/// It does not save space on short runs, and liblcf uses it anyway. The sentinel always needs all
469/// five bytes while each empty entry it replaces costs one, so a run shorter than six *grows*.
470/// Reproduced regardless: the goal is byte-identical output.
471///
472/// Loading stops once the load's string-list entry budget runs out, reporting `EntryBudgetExhausted`.
473#[derive(Clone, Debug, Default, Eq, PartialEq)]
474pub struct StrList<'a>(pub Vec<DbStr<'a>>);
475
476impl<'a> core::ops::Deref for StrList<'a> {
477    type Target = Vec<DbStr<'a>>;
478    #[inline]
479    fn deref(&self) -> &Self::Target {
480        &self.0
481    }
482}
483
484/// The sentinel base a gap marker is subtracted from.
485const GAP_BASE: u64 = 0x8_0000_0000;
486
487impl<'a> LcfField<'a> for StrList<'a> {
488    #[inline]
489    fn field_default(_engine: Engine) -> Self {
490        Self(Vec::new())
491    }
492
493    fn read(data: &'a [u8], ctx: &mut Ctx<'_>, owner: &'static str, id: u32) -> Self {
494        let mut out: Vec<DbStr<'a>> = Vec::new();
495        let mut cur = Cursor::new(data);
496
497        while !cur.is_empty() {
498            let Some(marker) = cur.varint64() else {
499                ctx.report(owner, id, cur.offset(), DiagKind::ShortChunk);
500                break;
501            };
502
503            if marker.value > u64::from(u32::MAX) {
504                // A gap: extend with empty entries rather than storing them. The run's length is not compared
505                // with the chunk's: the writer compresses runs, so a long run can be longer than its compressed
506                // chunk. A marker at or past `GAP_BASE` names an empty run, which the writer never emits.
507                let gap = GAP_BASE.saturating_sub(marker.value);
508                if gap == 0 {
509                    ctx.report(owner, id, cur.offset(), DiagKind::BadVarint);
510                    break;
511                }
512                if !ctx.take_entries(gap) {
513                    ctx.report(owner, id, cur.offset(), DiagKind::EntryBudgetExhausted);
514                    break;
515                }
516                // `take_entries` succeeded, so `gap` is at most `ENTRY_BUDGET`, which fits any `usize`.
517                out.resize(out.len() + gap as usize, DbStr::default());
518                continue;
519            }
520
521            // Charged before the bytes are read: any failure below ends the loop, so at most one unit is wasted.
522            if !ctx.take_entries(1) {
523                ctx.report(owner, id, cur.offset(), DiagKind::EntryBudgetExhausted);
524                break;
525            }
526            let Some(bytes) = cur.take(marker.value as usize) else {
527                ctx.report(owner, id, cur.offset(), DiagKind::ShortChunk);
528                break;
529            };
530            out.push(DbStr::from_bytes(bytes));
531        }
532
533        Self(out)
534    }
535
536    fn lcf_size(&self, _engine: Engine) -> u32 {
537        let mut total = 0u32;
538        let mut gap = 0u64;
539        for entry in &self.0 {
540            if entry.is_empty() {
541                gap += 1;
542                continue;
543            }
544            if gap > 0 {
545                total += wire::varint64_len(GAP_BASE - gap);
546                gap = 0;
547            }
548            let len = entry.len() as u32;
549            total += wire::varint_len(len) + len;
550        }
551        // A trailing run of empties is dropped, exactly as liblcf's writer drops it.
552        total
553    }
554
555    fn write<S: Sink>(&self, sink: &mut S, _engine: Engine) -> Result<(), S::Error> {
556        let mut gap = 0u64;
557        for entry in &self.0 {
558            if entry.is_empty() {
559                gap += 1;
560                continue;
561            }
562            if gap > 0 {
563                wire::write_varint64(sink, GAP_BASE - gap)?;
564                gap = 0;
565            }
566            wire::write_varint(sink, entry.len() as u32)?;
567            sink.write(entry.as_bytes())?;
568        }
569        Ok(())
570    }
571
572    #[inline]
573    fn element_count(&self) -> u32 {
574        self.0.len() as u32
575    }
576}