1
0
Fork 0
mirror of https://codeberg.org/demostf/parser.git synced 2026-06-03 10:14:06 +02:00

use vec for event definitions

This commit is contained in:
Robin Appelman 2019-08-28 16:00:47 +02:00
commit b6f7ebb15a
4 changed files with 14 additions and 19 deletions

View file

@ -35,8 +35,8 @@ impl Parse for GameEventMessage {
fn parse(stream: &mut Stream, state: &ParserState) -> Result<Self> {
let length: u16 = stream.read_sized(11)?;
let mut data = stream.read_bits(length as usize)?;
let event_type = data.read()?;
let raw_event = match state.event_definitions.get(&event_type) {
let event_type: GameEventTypeId = data.read()?;
let raw_event = match state.event_definitions.get(usize::from(event_type)) {
Some(definition) => {
let mut values: Vec<GameEventValue> = Vec::with_capacity(definition.entries.len());
for entry in &definition.entries {
@ -66,24 +66,18 @@ impl ParseBitSkip for GameEventMessage {
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct GameEventTypeId(u16);
#[derive(BitRead, Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct GameEventTypeId(#[size = 9] u16);
impl BitRead<LittleEndian> for GameEventTypeId {
fn read(stream: &mut Stream) -> ReadResult<Self> {
Ok(GameEventTypeId(stream.read_int(9)?))
}
}
impl From<GameEventTypeId> for u16 {
impl From<GameEventTypeId> for usize {
fn from(id: GameEventTypeId) -> Self {
id.0
id.0 as usize
}
}
#[derive(Debug)]
pub struct GameEventListMessage {
pub event_list: HashMap<GameEventTypeId, GameEventDefinition>,
pub event_list: Vec<GameEventDefinition>,
}
impl BitRead<LittleEndian> for GameEventDefinition {
@ -116,8 +110,7 @@ impl BitRead<LittleEndian> for GameEventListMessage {
let count: u16 = stream.read_sized(9)?;
let length: u32 = stream.read_sized(20)?;
let mut data = stream.read_bits(length as usize)?;
let event_list_vec: Vec<GameEventDefinition> = data.read_sized(count as usize)?;
let event_list = HashMap::from_iter(event_list_vec.into_iter().map(|def| (def.id, def)));
let event_list: Vec<GameEventDefinition> = data.read_sized(count as usize)?;
Ok(GameEventListMessage { event_list })
}

View file

@ -15,7 +15,9 @@ use std::num::ParseIntError;
use std::rc::Rc;
use std::str::FromStr;
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Display)]
#[derive(
Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Display, Ord, PartialOrd,
)]
pub struct EntityId(u32);
impl From<u32> for EntityId {

View file

@ -13,7 +13,7 @@ use std::ops::Deref;
use std::rc::Rc;
use std::str::FromStr;
#[derive(BitRead, Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(BitRead, Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub struct ClassId(u16);
impl FromStr for ClassId {

View file

@ -24,7 +24,7 @@ pub struct DemoMeta {
pub struct ParserState {
pub static_baselines: HashMap<ClassId, StaticBaseline>,
pub parsed_static_baselines: RefCell<HashMap<ClassId, Vec<SendProp>>>,
pub event_definitions: HashMap<GameEventTypeId, GameEventDefinition>,
pub event_definitions: Vec<GameEventDefinition>,
pub string_tables: Vec<StringTableMeta>,
pub entity_classes: HashMap<EntityId, Rc<ServerClass>>,
pub send_tables: HashMap<SendTableName, SendTable>,
@ -55,7 +55,7 @@ impl ParserState {
ParserState {
static_baselines: HashMap::new(),
parsed_static_baselines: RefCell::new(HashMap::new()),
event_definitions: HashMap::new(),
event_definitions: Vec::new(),
string_tables: Vec::new(),
entity_classes: HashMap::new(),
send_tables: HashMap::new(),