mirror of
https://codeberg.org/demostf/parser.git
synced 2026-06-03 10:14:06 +02:00
make gameevent parsing more backwards compatible
This commit is contained in:
parent
8ff5e5e6df
commit
ab87b34de8
6 changed files with 16415 additions and 4409 deletions
File diff suppressed because it is too large
Load diff
|
|
@ -15,6 +15,12 @@ pub struct GameEventDefinition {
|
|||
pub entries: Vec<GameEventEntry>,
|
||||
}
|
||||
|
||||
impl GameEventDefinition {
|
||||
pub fn get_entry(&self, name: &str) -> Option<&GameEventEntry> {
|
||||
self.entries.iter().find(|entry| entry.name == name)
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq<GameEventDefinition> for GameEventDefinition {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.id.eq(&other.id)
|
||||
|
|
@ -56,6 +62,21 @@ pub enum GameEventValueType {
|
|||
Local = 7,
|
||||
}
|
||||
|
||||
impl GameEventValueType {
|
||||
pub fn default_value(&self) -> GameEventValue {
|
||||
match self {
|
||||
GameEventValueType::None => GameEventValue::Local,
|
||||
GameEventValueType::String => GameEventValue::String(Default::default()),
|
||||
GameEventValueType::Float => GameEventValue::Float(Default::default()),
|
||||
GameEventValueType::Long => GameEventValue::Long(Default::default()),
|
||||
GameEventValueType::Short => GameEventValue::Short(Default::default()),
|
||||
GameEventValueType::Byte => GameEventValue::Byte(Default::default()),
|
||||
GameEventValueType::Boolean => GameEventValue::Boolean(Default::default()),
|
||||
GameEventValueType::Local => GameEventValue::Local,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub enum GameEventValue {
|
||||
|
|
@ -68,6 +89,42 @@ pub enum GameEventValue {
|
|||
Local,
|
||||
}
|
||||
|
||||
impl From<MaybeUtf8String> for GameEventValue {
|
||||
fn from(value: MaybeUtf8String) -> Self {
|
||||
GameEventValue::String(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<f32> for GameEventValue {
|
||||
fn from(value: f32) -> Self {
|
||||
GameEventValue::Float(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u32> for GameEventValue {
|
||||
fn from(value: u32) -> Self {
|
||||
GameEventValue::Long(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u16> for GameEventValue {
|
||||
fn from(value: u16) -> Self {
|
||||
GameEventValue::Short(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u8> for GameEventValue {
|
||||
fn from(value: u8) -> Self {
|
||||
GameEventValue::Byte(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<bool> for GameEventValue {
|
||||
fn from(value: bool) -> Self {
|
||||
GameEventValue::Boolean(value)
|
||||
}
|
||||
}
|
||||
|
||||
fn read_event_value(stream: &mut Stream, definition: &GameEventEntry) -> Result<GameEventValue> {
|
||||
Ok(match definition.kind {
|
||||
GameEventValueType::String => GameEventValue::String(stream.read()?),
|
||||
|
|
|
|||
|
|
@ -52,14 +52,17 @@ impl Parse<'_> for GameEventMessage {
|
|||
}
|
||||
|
||||
impl Encode for GameEventMessage {
|
||||
fn encode(
|
||||
&self,
|
||||
stream: &mut BitWriteStream<LittleEndian>,
|
||||
_state: &ParserState,
|
||||
) -> Result<()> {
|
||||
fn encode(&self, stream: &mut BitWriteStream<LittleEndian>, state: &ParserState) -> Result<()> {
|
||||
let definition = state
|
||||
.event_definitions
|
||||
.iter()
|
||||
.find(|def| def.event_type == self.event_type)
|
||||
.ok_or_else(|| {
|
||||
ParseError::MalformedGameEvent(GameEventError::UnknownType(self.event_type_id))
|
||||
})?;
|
||||
Ok(stream.reserve_length(11, |stream| {
|
||||
self.event_type_id.write(stream)?;
|
||||
self.event.write(stream)
|
||||
self.event.write(stream, definition)
|
||||
})?)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -63,6 +63,12 @@ pub enum ParseError {
|
|||
name: &'static str,
|
||||
found_type: GameEventValueType,
|
||||
},
|
||||
#[error(
|
||||
display = "Game event of type {} does not contain a {} value",
|
||||
ty,
|
||||
field
|
||||
)]
|
||||
MissingGameEventValue { ty: &'static str, field: String },
|
||||
#[error(display = "An entity with an unknown server class({}) was read", _0)]
|
||||
UnknownServerClass(ClassId),
|
||||
#[error(display = "Unknown send table: {}", _0)]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue