~quf/tocs

3ebb9dcf2d26bebac010cedf95592e7b501ee7de — Lukas Himbert 5 months ago ba77f7d
parse AlgoTable
2 files changed, 24 insertions(+), 8 deletions(-)

M tocs/src/script/deser.rs
M tocs/src/script/ser.rs
M tocs/src/script/deser.rs => tocs/src/script/deser.rs +2 -6
@@ 127,7 127,7 @@ pub fn parse(data: &[u8]) -> Option<Script> {

        let table = match name.to_str() {
            "ActionTable" => ScriptEntry::ActionTable(read_action_table_entries(table_data)?),
            //"AlgoTable" => ScriptEntry::ActionTable(read_action_table_entries(table_data)?),
            "AlgoTable" => ScriptEntry::AlgoTable(read_algo_table_entries(table_data)?),
            _ => ScriptEntry::Generic {
                name: name.to_owned(),
                data: table_data.to_owned(),


@@ 236,7 236,7 @@ pub fn read_algo_table_entries(data: &[u8]) -> Option<std::vec::Vec<AlgoTableEnt
    let mut reader = MemReader::new(data);
    let mut entries = std::vec::Vec::with_capacity(data.len() / 32);

    while reader.offset() + 32 < data.len() {
    while !reader.at_end() {
        let action_or_magic_id = reader.read_i16_le()?;
        let condition = reader.read_byte()?;
        let chance = reader.read_byte()?;


@@ 261,9 261,5 @@ pub fn read_algo_table_entries(data: &[u8]) -> Option<std::vec::Vec<AlgoTableEnt
        });
    }

    // check final padding
    ensure!(reader.read_byte()? == 1);
    ensure!(reader.rest().iter().copied().all(|b| b == 0));

    Some(entries)
}

M tocs/src/script/ser.rs => tocs/src/script/ser.rs +22 -2
@@ 192,8 192,28 @@ impl ActionTableEntry {
}

impl AlgoTableEntry {
    fn serialize_data(&self, vec: &mut MemWriter) -> Option<()> {
        todo!()
    fn serialize_data(&self, writer: &mut MemWriter) -> Option<()> {
        let Self {
            action_or_magic_id,
            condition,
            chance,
            use_limit,
            target_type,
            unk_byte_1,
            unk_byte_2,
            params,
        } = self;
        writer.write_i16_le(*action_or_magic_id);
        writer.write_u8(*condition);
        writer.write_u8(*chance);
        writer.write_u8(*use_limit);
        writer.write_u8(*target_type);
        writer.write_u8(*unk_byte_1);
        writer.write_u8(*unk_byte_2);
        for x in params {
            writer.write_u32_le(*x);
        }
        Some(())
    }
}