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
2
Cargo.lock
generated
2
Cargo.lock
generated
|
|
@ -634,7 +634,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tf-demo-parser"
|
name = "tf-demo-parser"
|
||||||
version = "0.2.5"
|
version = "0.2.6"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"better-panic",
|
"better-panic",
|
||||||
"bitbuffer",
|
"bitbuffer",
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
[package]
|
[package]
|
||||||
name = "tf-demo-parser"
|
name = "tf-demo-parser"
|
||||||
description = "parser for tf2 demo files"
|
description = "parser for tf2 demo files"
|
||||||
version = "0.2.5"
|
version = "0.2.6"
|
||||||
authors = ["Robin Appelman <robin@icewind.nl>"]
|
authors = ["Robin Appelman <robin@icewind.nl>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
license = "MIT OR Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
|
|
|
||||||
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 crate::ReadResult;
|
||||||
use bitbuffer::{BitError, BitReadBuffer, BitReadStream, LittleEndian};
|
use bitbuffer::{BitError, BitReadBuffer, BitReadStream, LittleEndian};
|
||||||
|
|
||||||
|
pub mod data;
|
||||||
pub mod gameevent_gen;
|
pub mod gameevent_gen;
|
||||||
pub mod gamevent;
|
pub mod gamevent;
|
||||||
pub mod header;
|
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)]
|
#[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 {
|
impl From<u32> for UserId {
|
||||||
fn from(int: u32) -> Self {
|
fn from(int: u32) -> Self {
|
||||||
|
|
@ -220,6 +220,19 @@ pub struct UserInfo {
|
||||||
pub team: Team,
|
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 {
|
impl PartialEq for UserInfo {
|
||||||
fn eq(&self, other: &UserInfo) -> bool {
|
fn eq(&self, other: &UserInfo) -> bool {
|
||||||
self.classes == other.classes
|
self.classes == other.classes
|
||||||
|
|
@ -379,35 +392,14 @@ impl Analyser {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_user_info(&mut self, text: Option<&str>, data: Option<Stream>) -> ReadResult<()> {
|
fn parse_user_info(&mut self, text: Option<&str>, data: Option<Stream>) -> ReadResult<()> {
|
||||||
if let Some(mut data) = data {
|
if let Some(user_info) = crate::demo::data::UserInfo::parse_from_string_table(text, data)? {
|
||||||
let name: String = data
|
self.state
|
||||||
.read_sized(32)
|
.users
|
||||||
.unwrap_or_else(|_| "Malformed Name".into());
|
.entry(user_info.user_id)
|
||||||
let user_id: UserId = data.read::<u32>()?.into();
|
.and_modify(|info| {
|
||||||
let steam_id: String = data.read()?;
|
info.entity_id = user_info.entity_id;
|
||||||
|
})
|
||||||
match text
|
.or_insert_with(|| user_info.into());
|
||||||
.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,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue