~quf/tocs

de8cfd70f5e9e8c9d423f1716cc1b77fdbe7b568 — Lukas Himbert 5 months ago 1d5e308
remove println!
2 files changed, 0 insertions(+), 21 deletions(-)

M tocs/src/script/deser.rs
M tocs/src/script/ser.rs
M tocs/src/script/deser.rs => tocs/src/script/deser.rs +0 -19
@@ 48,8 48,6 @@ pub fn parse(data: &[u8]) -> Option<Script> {
    let unk2 = reader.read_chunk::<4>()?;
    let script_name = reader.read_c_ascii_str().ok()?;

    println!("offset after script name = 0x{:x}, table list offset = 0x{:?}", reader.offset(), table_list_offset);

    // check padding between end of script name and start of table name offsets
    let name_padding = {
        let script_padding = data.get(reader.offset()..usize::try_from(table_list_offset).unwrap())?;


@@ 86,24 84,19 @@ pub fn parse(data: &[u8]) -> Option<Script> {
    let mut table_name_offsets = std::vec::Vec::with_capacity(n_tables);
    let mut table_data_offsets = std::vec::Vec::with_capacity(n_tables);

    println!("before data offsets 0x{:x}", reader.offset());
    for _ in 0..n_tables {
        let off = reader.read_u32_le()?;
        ensure!(off % 4 == 0);
        table_data_offsets.push(off);
    }
    println!("after data offsets 0x{:x}", reader.offset());
    for _ in 0..n_tables {
        table_name_offsets.push(reader.read_u16_le()?);
    }

    println!("after name offsets 0x{:x}", reader.offset());

    // read table names and contents, record alignments
    let mut tables = std::vec::Vec::with_capacity(n_tables);
    let mut alignment = std::collections::BTreeMap::new();
    for i in 0..n_tables {
        println!("start {i} (name offset 0x{:x})", table_name_offsets[i]);
        let name = reader.clone_at_offset(table_name_offsets[i].try_into().unwrap()).read_c_ascii_str().ok()?;

        // we assume the table data is always sorted. it seems to work out.


@@ 111,19 104,7 @@ pub fn parse(data: &[u8]) -> Option<Script> {
        let data_end = table_data_offsets.get(i + 1).copied().map(|n| usize::try_from(n).unwrap()).unwrap_or(data.len());
        let table_data_including_terminator = data.get(data_start..data_end)?;

        println!(
            "data {}; next one: {}, offset 0x{:x}",
            name.to_str(),
            reader
                .clone_at_offset(table_name_offsets[i].try_into().unwrap())
                .read_c_ascii_str()
                .ok()
                .map(|s| s.to_str())
                .unwrap_or(""),
            table_data_offsets.get(i + 1).map(|x| *x as usize).unwrap_or(data.len())
        );
        let table_data = remove_end_padding_from_entry_data(table_data_including_terminator)?;
        //println!("foo {i}");

        let table = match name.to_str() {
            "ActionTable" => ScriptEntry::ActionTable(read_action_table_entries(table_data)?),

M tocs/src/script/ser.rs => tocs/src/script/ser.rs +0 -2
@@ 25,7 25,6 @@ pub fn serialize(script: &Script) -> Option<std::vec::Vec<u8>> {
        ScriptNamePadding::Once => 33 + script.name.len_with_padding().get() + padding_before_table_list,
        ScriptNamePadding::Twice => 37 + script.name.len_with_padding().get() + padding_before_table_list,
    };
    println!("ser table_list_offset = 0x{table_list_offset:x}");
    writer.write_u32_le(table_list_offset.try_into().unwrap());

    // redundant stuff


@@ 38,7 37,6 @@ pub fn serialize(script: &Script) -> Option<std::vec::Vec<u8>> {

    // calculate table data offsets and name offsets
    let table_names_offset: usize = table_list_offset + 6 * script.entries.len();
    dbg!(table_names_offset);
    let table_names_end_offset = table_names_offset + script.entries.iter().map(|ent| ent.name_len_with_padding()).sum::<usize>();
    let table_data_padding = if table_names_end_offset % 4 == 0 { 0 } else { 4 - table_names_end_offset % 4 };
    let table_data_offset = table_names_end_offset + table_data_padding;