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

propnames

This commit is contained in:
Robin Appelman 2022-04-09 18:50:47 +02:00
commit b8d3540069
9 changed files with 5643 additions and 88 deletions

View file

@ -1,5 +1,6 @@
use crate::demo::message::packetentities::EntityId;
use crate::demo::packet::stringtable::{ExtraData, StringTableEntry};
use crate::demo::parser::analyser::UserId;
use crate::{ReadResult, Stream};
use bitbuffer::{BitRead, BitReadBuffer, BitReadStream, BitWrite, BitWriteStream, LittleEndian};
@ -24,7 +25,7 @@ struct RawPlayerInfo {
pub struct PlayerInfo {
#[size = 32]
pub name: String,
pub user_id: u32,
pub user_id: UserId,
#[size = 32]
pub steam_id: String,
pub extra: u32, // all my sources say these 4 bytes don't exist
@ -44,7 +45,7 @@ impl From<RawPlayerInfo> for PlayerInfo {
name: String::from_utf8_lossy(&raw.name_bytes)
.trim_end_matches('\0')
.to_string(),
user_id: raw.user_id,
user_id: raw.user_id.into(),
steam_id: raw.steam_id,
extra: raw.extra,
friends_id: raw.friends_id,

View file

@ -38,6 +38,18 @@ impl From<u32> for EntityId {
}
}
impl From<EntityId> for u32 {
fn from(id: EntityId) -> Self {
id.0
}
}
impl PartialEq<u32> for EntityId {
fn eq(&self, other: &u32) -> bool {
self.0 == *other
}
}
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(
BitRead, BitWrite, Clone, Copy, Debug, PartialEq, Eq, Serialize_repr, Deserialize_repr,

View file

@ -10,6 +10,7 @@ pub mod message;
pub mod packet;
pub mod parser;
pub mod sendprop;
mod sendprop_gen;
pub mod vector;
pub type Buffer<'a> = BitReadBuffer<'a, LittleEndian>;

View file

@ -3,9 +3,12 @@ use crate::demo::sendprop::{
RawSendPropDefinition, SendPropDefinition, SendPropFlag, SendPropIdentifier, SendPropType,
};
use crate::{Parse, ParseError, ParserState, Result, Stream};
use bitbuffer::{BitRead, BitWrite, BitWriteSized, BitWriteStream, LittleEndian};
use bitbuffer::{
BitRead, BitReadStream, BitWrite, BitWriteSized, BitWriteStream, Endianness, LittleEndian,
};
use parse_display::{Display, FromStr};
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::cmp::min;
use std::convert::TryFrom;
use std::iter::once;
@ -79,7 +82,6 @@ pub struct ServerClass {
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(
BitRead,
BitWrite,
PartialEq,
Eq,
@ -93,23 +95,29 @@ pub struct ServerClass {
Ord,
Default,
)]
pub struct SendTableName(String);
pub struct SendTableName(Cow<'static, str>);
impl SendTableName {
pub fn as_str(&self) -> &str {
self.0.as_str()
self.0.as_ref()
}
}
impl<E: Endianness> BitRead<'_, E> for SendTableName {
fn read(stream: &mut BitReadStream<'_, E>) -> bitbuffer::Result<Self> {
String::read(stream).map(SendTableName::from)
}
}
impl From<String> for SendTableName {
fn from(value: String) -> Self {
Self(value)
Self(Cow::Owned(value))
}
}
impl From<&str> for SendTableName {
fn from(value: &str) -> Self {
Self(value.into())
impl From<&'static str> for SendTableName {
fn from(value: &'static str) -> Self {
Self(Cow::Borrowed(value))
}
}

View file

@ -8,6 +8,7 @@ use crate::demo::packet::stringtable::StringTableEntry;
use crate::demo::parser::handler::{BorrowMessageHandler, MessageHandler};
use crate::demo::vector::Vector;
use crate::{ParserState, ReadResult, Stream};
use bitbuffer::{BitWrite, BitWriteStream, Endianness};
use num_enum::TryFromPrimitive;
use parse_display::{Display, FromStr};
use serde::de::Error;
@ -211,6 +212,12 @@ impl From<HashMap<Class, u8>> for ClassList {
)]
pub struct UserId(pub u8);
impl<E: Endianness> BitWrite<E> for UserId {
fn write(&self, stream: &mut BitWriteStream<E>) -> ReadResult<()> {
(self.0 as u32).write(stream)
}
}
impl From<u32> for UserId {
fn from(int: u32) -> Self {
UserId((int & 255) as u8)
@ -223,6 +230,24 @@ impl From<u16> for UserId {
}
}
impl From<UserId> for u8 {
fn from(id: UserId) -> Self {
id.0
}
}
impl From<UserId> for u32 {
fn from(id: UserId) -> Self {
id.0 as u32
}
}
impl PartialEq<u8> for UserId {
fn eq(&self, other: &u8) -> bool {
self.0 == *other
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Spawn {
pub user: UserId,

View file

@ -1,6 +1,9 @@
use bitbuffer::{BitRead, BitWrite, BitWriteSized, BitWriteStream, LittleEndian};
use bitbuffer::{
BitRead, BitReadStream, BitWrite, BitWriteSized, BitWriteStream, Endianness, LittleEndian,
};
use enumflags2::{bitflags, BitFlags};
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use crate::{ParseError, ReadResult, Result, Stream};
@ -10,52 +13,48 @@ use crate::consthash::ConstFnvHash;
use crate::demo::message::stringtable::log_base2;
use crate::demo::packet::datatable::SendTableName;
use crate::demo::parser::MalformedSendPropDefinitionError;
use crate::demo::sendprop_gen::get_prop_names;
use num_traits::Signed;
use parse_display::Display;
use std::cmp::min;
use std::convert::{TryFrom, TryInto};
use std::fmt;
use std::fmt::{self, Display, Formatter};
use std::hash::Hash;
use std::ops::BitOr;
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(
BitRead,
BitWrite,
PartialEq,
Eq,
Hash,
Debug,
Display,
Clone,
Serialize,
Deserialize,
Ord,
PartialOrd,
BitWrite, PartialEq, Eq, Hash, Debug, Display, Clone, Serialize, Deserialize, Ord, PartialOrd,
)]
pub struct SendPropName(String);
pub struct SendPropName(Cow<'static, str>);
impl SendPropName {
pub fn as_str(&self) -> &str {
self.0.as_str()
self.0.as_ref()
}
}
impl<E: Endianness> BitRead<'_, E> for SendPropName {
fn read(stream: &mut BitReadStream<'_, E>) -> bitbuffer::Result<Self> {
String::read(stream).map(SendPropName::from)
}
}
impl PartialEq<&str> for SendPropName {
fn eq(&self, other: &&str) -> bool {
self.0.as_str() == *other
self.as_str() == *other
}
}
impl From<String> for SendPropName {
fn from(value: String) -> Self {
Self(value)
Self(Cow::Owned(value))
}
}
impl From<&str> for SendPropName {
fn from(value: &str) -> Self {
value.to_string().into()
impl From<&'static str> for SendPropName {
fn from(value: &'static str) -> Self {
SendPropName(Cow::Borrowed(value))
}
}
@ -1105,9 +1104,7 @@ impl<'a> TryFrom<&'a SendPropValue> for &'a [SendPropValue] {
}
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(
Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq, Hash, Display, Serialize, Deserialize,
)]
#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct SendPropIdentifier(u64);
impl SendPropIdentifier {
@ -1115,6 +1112,14 @@ impl SendPropIdentifier {
let hasher = ConstFnvHash::new().push_string(table).push_string(prop);
SendPropIdentifier(hasher.finish())
}
pub fn table_name(&self) -> Option<SendTableName> {
get_prop_names(*self).map(|(table, _)| table.into())
}
pub fn prop_name(&self) -> Option<SendPropName> {
get_prop_names(*self).map(|(_, prop)| prop.into())
}
}
impl From<u64> for SendPropIdentifier {
@ -1123,6 +1128,21 @@ impl From<u64> for SendPropIdentifier {
}
}
impl From<SendPropIdentifier> for u64 {
fn from(identifier: SendPropIdentifier) -> Self {
identifier.0
}
}
impl Display for SendPropIdentifier {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match get_prop_names(*self) {
Some((table, prop)) => write!(f, "{}.{}", table, prop),
None => write!(f, "Prop name {} not known", self.0),
}
}
}
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Debug, Clone, Display, PartialEq, Serialize, Deserialize)]
#[display("{index} = {value}")]

5489
src/demo/sendprop_gen.rs Normal file

File diff suppressed because it is too large Load diff