mirror of
https://codeberg.org/demostf/parser.git
synced 2026-06-03 18:24:05 +02:00
read createstringtable entries as list directly
This commit is contained in:
parent
eb62b5960e
commit
f335dc2163
1 changed files with 108 additions and 68 deletions
|
|
@ -2,13 +2,13 @@ use std::collections::HashMap;
|
|||
|
||||
use arraydeque::{ArrayDeque, Wrapping};
|
||||
use bitstream_reader::{BitBuffer, BitRead, BitReadSized, BitStream, LittleEndian};
|
||||
use num_traits::{PrimInt, Unsigned};
|
||||
use snap::Decoder;
|
||||
|
||||
use crate::{Parse, ParseError, ParserState, ReadResult, Result, Stream};
|
||||
use crate::demo::packet::stringtable::{
|
||||
ExtraData, FixedUserdataSize, StringTable, StringTableEntry,
|
||||
};
|
||||
use crate::{Parse, ParseError, ParserState, ReadResult, Result, Stream};
|
||||
use num_traits::{PrimInt, Unsigned};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct CreateStringTableMessage {
|
||||
|
|
@ -78,20 +78,10 @@ impl Parse for CreateStringTableMessage {
|
|||
};
|
||||
|
||||
let entries =
|
||||
parse_string_table_entries(&mut table_data, &table_meta, entity_count, &Vec::new())?;
|
||||
let mut entries: Vec<(u16, StringTableEntry)> = entries.into_iter().collect();
|
||||
parse_string_table_list(&mut table_data, &table_meta, entity_count)?;
|
||||
|
||||
// verify that there are no holes in our indexes
|
||||
entries.sort_unstable_by(|(a, _), (b, _)| a.cmp(b));
|
||||
if entries.len() > 0 {
|
||||
let last_index = entries.last().map(|(index, _)| *index).unwrap_or(0u16) as usize;
|
||||
if last_index != entries.len() - 1 {
|
||||
panic!("there should be no holes when reading CreateStringTable message");
|
||||
}
|
||||
}
|
||||
let table_entries = entries.into_iter().map(|(_, entry)| entry).collect();
|
||||
let table = StringTable {
|
||||
entries: table_entries,
|
||||
entries,
|
||||
max_entries,
|
||||
fixed_userdata_size,
|
||||
client_entries: None,
|
||||
|
|
@ -118,7 +108,7 @@ impl Parse for UpdateStringTableMessage {
|
|||
let mut data = stream.read_bits(len)?;
|
||||
|
||||
let entries = match state.string_tables.get(table_id as usize) {
|
||||
Some(table) => parse_string_table_entries(
|
||||
Some(table) => parse_string_table_sparse_entries(
|
||||
&mut data,
|
||||
&table.get_table_meta(),
|
||||
changed,
|
||||
|
|
@ -131,7 +121,7 @@ impl Parse for UpdateStringTableMessage {
|
|||
}
|
||||
}
|
||||
|
||||
fn parse_string_table_entries(
|
||||
fn parse_string_table_sparse_entries(
|
||||
stream: &mut Stream,
|
||||
table_meta: &StringTableMeta,
|
||||
entry_count: u16,
|
||||
|
|
@ -152,6 +142,69 @@ fn parse_string_table_entries(
|
|||
|
||||
last_entry = index as i16;
|
||||
|
||||
let entry = read_table_entry(
|
||||
stream,
|
||||
table_meta,
|
||||
&history,
|
||||
existing_entries.get(index as usize),
|
||||
)?;
|
||||
// optimize: any way to get rid of the clone here?
|
||||
// `entries` always outlives `history` without reallocation
|
||||
let text = entry.text.clone();
|
||||
entries.insert(index, entry);
|
||||
// not 100% sure we should be pushing front here, and not appending
|
||||
history.push(text);
|
||||
|
||||
if history.len() > 32 {
|
||||
history.remove(0);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(entries)
|
||||
}
|
||||
|
||||
fn parse_string_table_list(
|
||||
stream: &mut Stream,
|
||||
table_meta: &StringTableMeta,
|
||||
entry_count: u16,
|
||||
) -> ReadResult<Vec<StringTableEntry>> {
|
||||
let entry_bits = log_base2(table_meta.max_entries);
|
||||
let mut entries = Vec::with_capacity(entry_count as usize);
|
||||
|
||||
let mut history: Vec<String> = Vec::new();
|
||||
|
||||
for i in 0..entry_count {
|
||||
if !stream.read::<bool>()? {
|
||||
panic!("there should be no holes when reading CreateStringTable message");
|
||||
};
|
||||
|
||||
let entry = read_table_entry(
|
||||
stream,
|
||||
table_meta,
|
||||
&history,
|
||||
None,
|
||||
)?;
|
||||
// optimize: any way to get rid of the clone here?
|
||||
// `entries` always outlives `history` without reallocation
|
||||
let text = entry.text.clone();
|
||||
entries.push(entry);
|
||||
// not 100% sure we should be pushing front here, and not appending
|
||||
history.push(text);
|
||||
|
||||
if history.len() > 32 {
|
||||
history.remove(0);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(entries)
|
||||
}
|
||||
|
||||
fn read_table_entry(
|
||||
stream: &mut Stream,
|
||||
table_meta: &StringTableMeta,
|
||||
history: &Vec<String>,
|
||||
existing_entry: Option<&StringTableEntry>,
|
||||
) -> ReadResult<StringTableEntry> {
|
||||
let value = if stream.read()? {
|
||||
// set value
|
||||
if stream.read()? {
|
||||
|
|
@ -192,7 +245,7 @@ fn parse_string_table_entries(
|
|||
data: stream,
|
||||
});
|
||||
|
||||
let entry = match existing_entries.get(index as usize) {
|
||||
Ok(match existing_entry {
|
||||
Some(existing_entry) => {
|
||||
let new_user_data = user_data.or_else(|| existing_entry.extra_data.clone());
|
||||
let new_value = value.unwrap_or_else(|| existing_entry.text.clone());
|
||||
|
|
@ -205,20 +258,7 @@ fn parse_string_table_entries(
|
|||
text: value.unwrap_or_default(),
|
||||
extra_data: user_data,
|
||||
},
|
||||
};
|
||||
// optimize: any way to get rid of the clone here?
|
||||
// `entries` always outlives `history` without reallocation
|
||||
let text = entry.text.clone();
|
||||
entries.insert(index, entry);
|
||||
// not 100% sure we should be pushing front here, and not appending
|
||||
history.push(text);
|
||||
|
||||
if history.len() > 32 {
|
||||
history.remove(0);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(entries)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn read_var_int(stream: &mut Stream) -> ReadResult<u32> {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue