mirror of
https://codeberg.org/demostf/parser.git
synced 2026-06-03 18:24:05 +02:00
make user info parsing reusable
This commit is contained in:
parent
746176ceb5
commit
5ea5983a74
6 changed files with 68 additions and 32 deletions
3
src/demo/data/mod.rs
Normal file
3
src/demo/data/mod.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
mod userinfo;
|
||||
|
||||
pub use userinfo::UserInfo;
|
||||
40
src/demo/data/userinfo.rs
Normal file
40
src/demo/data/userinfo.rs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
use crate::demo::message::packetentities::EntityId;
|
||||
use crate::demo::parser::gamestateanalyser::UserId;
|
||||
use crate::{ReadResult, Stream};
|
||||
|
||||
pub struct UserInfo {
|
||||
pub steam_id: String,
|
||||
pub user_id: UserId,
|
||||
pub entity_id: EntityId,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
impl UserInfo {
|
||||
pub fn parse_from_string_table(
|
||||
text: Option<&str>,
|
||||
data: Option<Stream>,
|
||||
) -> ReadResult<Option<Self>> {
|
||||
if let Some(mut data) = data {
|
||||
let name: String = data
|
||||
.read_sized(32)
|
||||
.unwrap_or_else(|_| "Malformed Name".into());
|
||||
let user_id: UserId = data.read::<u32>()?.into();
|
||||
let steam_id: String = data.read()?;
|
||||
|
||||
match text
|
||||
.map(|text| text.parse::<u32>().map(|id| (id + 1).into()))
|
||||
.unwrap_or_else(|| Ok((user_id.0 as u32).into()))
|
||||
{
|
||||
Ok(entity_id) if !steam_id.is_empty() => Ok(Some(UserInfo {
|
||||
steam_id,
|
||||
user_id,
|
||||
entity_id,
|
||||
name,
|
||||
})),
|
||||
_ => Ok(None),
|
||||
}
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
use crate::ReadResult;
|
||||
use bitbuffer::{BitError, BitReadBuffer, BitReadStream, LittleEndian};
|
||||
|
||||
pub mod data;
|
||||
pub mod gameevent_gen;
|
||||
pub mod gamevent;
|
||||
pub mod header;
|
||||
|
|
|
|||
|
|
@ -175,7 +175,7 @@ impl From<HashMap<Class, u8>> for ClassList {
|
|||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
|
||||
pub struct UserId(u8);
|
||||
pub struct UserId(pub u8);
|
||||
|
||||
impl From<u32> for UserId {
|
||||
fn from(int: u32) -> Self {
|
||||
|
|
@ -220,6 +220,19 @@ pub struct UserInfo {
|
|||
pub team: Team,
|
||||
}
|
||||
|
||||
impl From<crate::demo::data::UserInfo> for UserInfo {
|
||||
fn from(info: crate::demo::data::UserInfo) -> Self {
|
||||
UserInfo {
|
||||
classes: ClassList::default(),
|
||||
name: info.name,
|
||||
user_id: info.user_id,
|
||||
steam_id: info.steam_id,
|
||||
entity_id: info.entity_id,
|
||||
team: Team::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for UserInfo {
|
||||
fn eq(&self, other: &UserInfo) -> bool {
|
||||
self.classes == other.classes
|
||||
|
|
@ -379,35 +392,14 @@ impl Analyser {
|
|||
}
|
||||
|
||||
fn parse_user_info(&mut self, text: Option<&str>, data: Option<Stream>) -> ReadResult<()> {
|
||||
if let Some(mut data) = data {
|
||||
let name: String = data
|
||||
.read_sized(32)
|
||||
.unwrap_or_else(|_| "Malformed Name".into());
|
||||
let user_id: UserId = data.read::<u32>()?.into();
|
||||
let steam_id: String = data.read()?;
|
||||
|
||||
match text
|
||||
.map(|text| text.parse::<u32>().map(|id| (id + 1).into()))
|
||||
.unwrap_or_else(|| Ok((user_id.0 as u32).into()))
|
||||
{
|
||||
Ok(entity_id) if !steam_id.is_empty() => {
|
||||
self.state
|
||||
.users
|
||||
.entry(user_id)
|
||||
.and_modify(|info| {
|
||||
info.entity_id = entity_id;
|
||||
})
|
||||
.or_insert_with(|| UserInfo {
|
||||
classes: ClassList::default(),
|
||||
team: Team::Other,
|
||||
steam_id,
|
||||
user_id,
|
||||
name,
|
||||
entity_id,
|
||||
});
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
if let Some(user_info) = crate::demo::data::UserInfo::parse_from_string_table(text, data)? {
|
||||
self.state
|
||||
.users
|
||||
.entry(user_info.user_id)
|
||||
.and_modify(|info| {
|
||||
info.entity_id = user_info.entity_id;
|
||||
})
|
||||
.or_insert_with(|| user_info.into());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue