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

make user info parsing reusable

This commit is contained in:
Robin Appelman 2021-07-11 17:32:20 +02:00
commit 5ea5983a74
6 changed files with 68 additions and 32 deletions

2
Cargo.lock generated
View file

@ -634,7 +634,7 @@ dependencies = [
[[package]]
name = "tf-demo-parser"
version = "0.2.5"
version = "0.2.6"
dependencies = [
"better-panic",
"bitbuffer",

View file

@ -1,7 +1,7 @@
[package]
name = "tf-demo-parser"
description = "parser for tf2 demo files"
version = "0.2.5"
version = "0.2.6"
authors = ["Robin Appelman <robin@icewind.nl>"]
edition = "2018"
license = "MIT OR Apache-2.0"

3
src/demo/data/mod.rs Normal file
View file

@ -0,0 +1,3 @@
mod userinfo;
pub use userinfo::UserInfo;

40
src/demo/data/userinfo.rs Normal file
View 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)
}
}
}

View file

@ -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;

View file

@ -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(())