~quf/tocs

7eae8753cc5bde302bf7adc8d9eb69542fae210c — Lukas Himbert 5 months ago a28de09
WeaponAttTable
4 files changed, 58 insertions(+), 5 deletions(-)

M sqlite_conv/src/main.rs
M tocs/src/script/deser.rs
M tocs/src/script/mod.rs
M tocs/src/script/ser.rs
M sqlite_conv/src/main.rs => sqlite_conv/src/main.rs +24 -3
@@ 295,14 295,28 @@ fn scripts_to_new_db<P: AsRef<std::path::Path>>(base_path: P, dbpath: P) {
    tx.execute(
        "create table SummonTable (
    file_name text not null,
    unk1 int,
    unk2 int,
    monster text,
    unk1 int not null,
    unk2 int not null,
    monster text not null,
    foreign key(file_name) references scripts(file_name)
) strict;",
        [],
    )
    .unwrap();

    tx.execute(
        "create table WeaponAttTable (
    file_name text not null,
    slash int not null,
    thrust int not null,
    pierce int not null,
    strike int not null,
    foreign key(file_name) references scripts(file_name)
) strict;",
        [],
    )
    .unwrap();

    tx.execute(
        "create table generic_entry (
    file_name text not null,


@@ 470,6 484,13 @@ fn insert_script_entry_data(tx: &rusqlite::Transaction, script_name: &str, entry
                .unwrap();
            }
        }
        ScriptEntry::WeaponAttTable(att) => {
            tx.execute(
                "insert into WeaponAttTable values (?, ?, ?, ?, ?)",
                [&script_name as &dyn rusqlite::types::ToSql, &att.slash as _, &att.thrust as _, &att.pierce as _, &att.strike as _],
            )
            .unwrap();
        }
        ScriptEntry::Generic { name, data } => {
            tx.execute(
                "insert into generic_entry values (?, ?, ?)",

M tocs/src/script/deser.rs => tocs/src/script/deser.rs +9 -1
@@ 1,4 1,4 @@
use super::{ActionTableEntry, AlgoTableEntry, Effect, Script, ScriptEntry, ScriptNamePadding, SummonTableEntry};
use super::{ActionTableEntry, AlgoTableEntry, Effect, Script, ScriptEntry, ScriptNamePadding, SummonTableEntry, WeaponAtt};

use crate::io::MemReader;



@@ 112,6 112,7 @@ pub fn parse(data: &[u8]) -> Option<Script> {
            "ActionTable" => ScriptEntry::ActionTable(read_action_table_entries(table_data)?),
            "AlgoTable" => ScriptEntry::AlgoTable(read_algo_table_entries(table_data)?),
            "SummonTable" => ScriptEntry::SummonTable(read_summon_table_entries(table_data)?),
            "WeaponAttTable" => ScriptEntry::WeaponAttTable(read_weapon_att_table(table_data)?),
            _ => ScriptEntry::Generic {
                name: name.to_owned(),
                data: table_data.to_owned(),


@@ 276,3 277,10 @@ pub fn read_summon_table_entries(data: &[u8]) -> Option<std::vec::Vec<SummonTabl

    Some(entries)
}

pub fn read_weapon_att_table(data: &[u8]) -> Option<WeaponAtt> {
    match data {
        &[slash, thrust, pierce, strike] => Some(WeaponAtt { slash, thrust, pierce, strike }),
        _ => None,
    }
}

M tocs/src/script/mod.rs => tocs/src/script/mod.rs +11 -0
@@ 29,6 29,7 @@ pub enum ScriptEntry {
    ActionTable(std::vec::Vec<ActionTableEntry>),
    AlgoTable(std::vec::Vec<AlgoTableEntry>),
    SummonTable(std::vec::Vec<SummonTableEntry>),
    WeaponAttTable(WeaponAtt),
    Generic { name: PaddedAsciiString<1>, data: std::vec::Vec<u8> },
}



@@ 38,6 39,7 @@ impl ScriptEntry {
            ScriptEntry::ActionTable { .. } => 12,
            ScriptEntry::AlgoTable { .. } => 10,
            ScriptEntry::SummonTable { .. } => 12,
            ScriptEntry::WeaponAttTable { .. } => 15,
            ScriptEntry::Generic { name, .. } => name.len_with_padding().get(),
        }
    }


@@ 48,6 50,7 @@ impl ScriptEntry {
            ScriptEntry::ActionTable { .. } => PaddedAsciiStr::new("ActionTable").unwrap(),
            ScriptEntry::AlgoTable { .. } => PaddedAsciiStr::new("AlgoTable").unwrap(),
            ScriptEntry::SummonTable { .. } => PaddedAsciiStr::new("SummonTable").unwrap(),
            ScriptEntry::WeaponAttTable { .. } => PaddedAsciiStr::new("WeaponAttTable").unwrap(),
            ScriptEntry::Generic { name, .. } => name.as_padded_ascii_str(),
        }
    }


@@ 107,3 110,11 @@ pub struct SummonTableEntry {
    pub unk2: u16,
    pub monster: PaddedAsciiString<32>,
}

#[derive(Debug, Clone)]
pub struct WeaponAtt {
    pub slash: u8,
    pub thrust: u8,
    pub pierce: u8,
    pub strike: u8,
}

M tocs/src/script/ser.rs => tocs/src/script/ser.rs +14 -1
@@ 1,4 1,4 @@
use super::{ActionTableEntry, AlgoTableEntry, Script, ScriptEntry, ScriptNamePadding, SummonTableEntry};
use super::{ActionTableEntry, AlgoTableEntry, Script, ScriptEntry, ScriptNamePadding, SummonTableEntry, WeaponAtt};
use crate::io::MemWriter;

pub fn serialize(script: &Script) -> Option<std::vec::Vec<u8>> {


@@ 125,6 125,7 @@ impl ScriptEntry {
            ScriptEntry::ActionTable(entries) => entries.iter().try_for_each(|entry| entry.serialize_data(writer))?,
            ScriptEntry::AlgoTable(entries) => entries.iter().try_for_each(|entry| entry.serialize_data(writer))?,
            ScriptEntry::SummonTable(entries) => entries.iter().try_for_each(|entry| entry.serialize_data(writer))?,
            ScriptEntry::WeaponAttTable(att) => att.serialize_data(writer)?,
            ScriptEntry::Generic { name: _, data } => {
                writer.write_data(data.as_slice());
            }


@@ 225,12 226,24 @@ impl SummonTableEntry {
    }
}

impl WeaponAtt {
    fn serialize_data(&self, writer: &mut MemWriter) -> Option<()> {
        let Self { slash, thrust, pierce, strike } = self;
        writer.write_u8(*slash);
        writer.write_u8(*thrust);
        writer.write_u8(*pierce);
        writer.write_u8(*strike);
        Some(())
    }
}

impl ScriptEntry {
    fn data_len(&self) -> usize {
        match self {
            ScriptEntry::ActionTable(entries) => 212 * entries.len(),
            ScriptEntry::AlgoTable(entries) => 32 * entries.len(),
            ScriptEntry::SummonTable(entries) => 36 * entries.len(),
            ScriptEntry::WeaponAttTable(_) => 4,
            ScriptEntry::Generic { name: _, data } => data.len(),
        }
    }