Switch to using lazy parser
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -695,7 +695,7 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "csdemo"
|
name = "csdemo"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
source = "git+https://github.com/Lol3rrr/csdemo.git#a0eab7c68f14e4e846823d0468ef196cf4c85fa5"
|
source = "git+https://github.com/Lol3rrr/csdemo.git#4b1cb505667fd024174ba91fa19f9dd7a410fd30"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitter",
|
"bitter",
|
||||||
"phf",
|
"phf",
|
||||||
|
|||||||
@@ -33,13 +33,10 @@ pub struct PlayerStats {
|
|||||||
|
|
||||||
pub fn parse(buf: &[u8]) -> Result<EndOfGame, ()> {
|
pub fn parse(buf: &[u8]) -> Result<EndOfGame, ()> {
|
||||||
let tmp = csdemo::Container::parse(buf).map_err(|e| ())?;
|
let tmp = csdemo::Container::parse(buf).map_err(|e| ())?;
|
||||||
let output = csdemo::parser::parse(
|
let output = csdemo::lazyparser::LazyParser::new(tmp);
|
||||||
csdemo::FrameIterator::parse(tmp.inner),
|
|
||||||
csdemo::parser::EntityFilter::all(),
|
|
||||||
)
|
|
||||||
.map_err(|e| ())?;
|
|
||||||
|
|
||||||
let header = &output.header;
|
let header = output.file_header().ok_or(())?;
|
||||||
|
let player_info = output.player_info();
|
||||||
|
|
||||||
let mut player_stats = std::collections::HashMap::<_, PlayerStats>::new();
|
let mut player_stats = std::collections::HashMap::<_, PlayerStats>::new();
|
||||||
let mut pawn_to_player =
|
let mut pawn_to_player =
|
||||||
@@ -47,7 +44,7 @@ pub fn parse(buf: &[u8]) -> Result<EndOfGame, ()> {
|
|||||||
|
|
||||||
let mut track = false;
|
let mut track = false;
|
||||||
let mut player_life = std::collections::HashMap::<_, u8>::new();
|
let mut player_life = std::collections::HashMap::<_, u8>::new();
|
||||||
for event in output.events.iter() {
|
for event in output.events().filter_map(|e| e.ok()) {
|
||||||
match event {
|
match event {
|
||||||
csdemo::DemoEvent::GameEvent(gevent) => {
|
csdemo::DemoEvent::GameEvent(gevent) => {
|
||||||
match gevent.as_ref() {
|
match gevent.as_ref() {
|
||||||
@@ -84,15 +81,10 @@ pub fn parse(buf: &[u8]) -> Result<EndOfGame, ()> {
|
|||||||
track = false;
|
track = false;
|
||||||
}
|
}
|
||||||
csdemo::game_event::GameEvent::PlayerDeath(pdeath) if track => {
|
csdemo::game_event::GameEvent::PlayerDeath(pdeath) if track => {
|
||||||
player_death(pdeath, &output.player_info, &mut player_stats);
|
player_death(pdeath, &player_info, &mut player_stats);
|
||||||
}
|
}
|
||||||
csdemo::game_event::GameEvent::PlayerHurt(phurt) if track => {
|
csdemo::game_event::GameEvent::PlayerHurt(phurt) if track => {
|
||||||
player_hurt(
|
player_hurt(phurt, &player_info, &mut player_stats, &mut player_life);
|
||||||
phurt,
|
|
||||||
&output.player_info,
|
|
||||||
&mut player_stats,
|
|
||||||
&mut player_life,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
};
|
};
|
||||||
@@ -104,8 +96,7 @@ pub fn parse(buf: &[u8]) -> Result<EndOfGame, ()> {
|
|||||||
let mut teams = std::collections::HashMap::<i32, TeamInfo>::new();
|
let mut teams = std::collections::HashMap::<i32, TeamInfo>::new();
|
||||||
|
|
||||||
let mut entity_to_team = std::collections::HashMap::new();
|
let mut entity_to_team = std::collections::HashMap::new();
|
||||||
for tick_state in output.entity_states.ticks {
|
for (tick, state) in output.entities().filter_map(|e| e.ok()) {
|
||||||
for state in tick_state.states {
|
|
||||||
let team = match csdemo::structured::ccsteam::CCSTeam::try_from(&state) {
|
let team = match csdemo::structured::ccsteam::CCSTeam::try_from(&state) {
|
||||||
Ok(t) => t,
|
Ok(t) => t,
|
||||||
Err(_) => continue,
|
Err(_) => continue,
|
||||||
@@ -130,7 +121,7 @@ pub fn parse(buf: &[u8]) -> Result<EndOfGame, ()> {
|
|||||||
|
|
||||||
let team_number = player_ids
|
let team_number = player_ids
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|p| output.player_info.get(*p).map(|p| p.team))
|
.filter_map(|p| player_info.get(*p).map(|p| p.team))
|
||||||
.next()
|
.next()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
@@ -147,12 +138,11 @@ pub fn parse(buf: &[u8]) -> Result<EndOfGame, ()> {
|
|||||||
team_entry.end_score = score as usize;
|
team_entry.end_score = score as usize;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
let mut players: Vec<_> = player_stats
|
let mut players: Vec<_> = player_stats
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter_map(|(id, stats)| {
|
.filter_map(|(id, stats)| {
|
||||||
let player = output.player_info.get(&id)?;
|
let player = player_info.get(&id)?;
|
||||||
|
|
||||||
Some((
|
Some((
|
||||||
PlayerInfo {
|
PlayerInfo {
|
||||||
|
|||||||
@@ -79,16 +79,13 @@ impl From<u32> for PawnID {
|
|||||||
|
|
||||||
pub fn parse(config: &Config, buf: &[u8]) -> Result<HeatMapOutput, ()> {
|
pub fn parse(config: &Config, buf: &[u8]) -> Result<HeatMapOutput, ()> {
|
||||||
let tmp = csdemo::Container::parse(buf).map_err(|e| ())?;
|
let tmp = csdemo::Container::parse(buf).map_err(|e| ())?;
|
||||||
let output = csdemo::parser::parse(
|
|
||||||
csdemo::FrameIterator::parse(tmp.inner),
|
let output = csdemo::lazyparser::LazyParser::new(tmp);
|
||||||
csdemo::parser::EntityFilter::all(),
|
|
||||||
)
|
|
||||||
.map_err(|e| ())?;
|
|
||||||
|
|
||||||
let pawn_ids = {
|
let pawn_ids = {
|
||||||
let mut tmp = std::collections::HashMap::<PawnID, _>::new();
|
let mut tmp = std::collections::HashMap::<PawnID, _>::new();
|
||||||
|
|
||||||
for event in output.events.iter() {
|
for event in output.events().filter_map(|e| e.ok()) {
|
||||||
let entry = match event {
|
let entry = match event {
|
||||||
csdemo::DemoEvent::GameEvent(ge) => match ge.as_ref() {
|
csdemo::DemoEvent::GameEvent(ge) => match ge.as_ref() {
|
||||||
csdemo::game_event::GameEvent::PlayerSpawn(pspawn) => {
|
csdemo::game_event::GameEvent::PlayerSpawn(pspawn) => {
|
||||||
@@ -120,12 +117,12 @@ pub fn parse(config: &Config, buf: &[u8]) -> Result<HeatMapOutput, ()> {
|
|||||||
let mut player_cells = std::collections::HashMap::new();
|
let mut player_cells = std::collections::HashMap::new();
|
||||||
|
|
||||||
let mut heatmaps = std::collections::HashMap::new();
|
let mut heatmaps = std::collections::HashMap::new();
|
||||||
for tick_state in output.entity_states.ticks.iter() {
|
for (tick, state) in output.entities().filter_map(|s| s.ok()) {
|
||||||
let _tracing_guard = tracing::debug_span!("Tick", tick=?tick_state.tick).entered();
|
let _tracing_guard = tracing::debug_span!("Tick", ?tick).entered();
|
||||||
|
|
||||||
process_tick(
|
process_tick(
|
||||||
config,
|
config,
|
||||||
tick_state,
|
&state,
|
||||||
&pawn_ids,
|
&pawn_ids,
|
||||||
&mut teams,
|
&mut teams,
|
||||||
&mut player_lifestate,
|
&mut player_lifestate,
|
||||||
@@ -139,7 +136,7 @@ pub fn parse(config: &Config, buf: &[u8]) -> Result<HeatMapOutput, ()> {
|
|||||||
|
|
||||||
Ok(HeatMapOutput {
|
Ok(HeatMapOutput {
|
||||||
player_heatmaps: heatmaps,
|
player_heatmaps: heatmaps,
|
||||||
player_info: output.player_info,
|
player_info: output.player_info(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -147,7 +144,7 @@ pub const MAX_COORD: f32 = (1 << 14) as f32;
|
|||||||
|
|
||||||
fn process_tick(
|
fn process_tick(
|
||||||
config: &Config,
|
config: &Config,
|
||||||
tick_state: &csdemo::parser::EntityTickStates,
|
entity_state: &csdemo::parser::entities::EntityState,
|
||||||
pawn_ids: &std::collections::HashMap<PawnID, csdemo::UserId>,
|
pawn_ids: &std::collections::HashMap<PawnID, csdemo::UserId>,
|
||||||
teams: &mut std::collections::HashMap<PawnID, String>,
|
teams: &mut std::collections::HashMap<PawnID, String>,
|
||||||
player_lifestate: &mut std::collections::HashMap<csdemo::UserId, u32>,
|
player_lifestate: &mut std::collections::HashMap<csdemo::UserId, u32>,
|
||||||
@@ -155,11 +152,10 @@ fn process_tick(
|
|||||||
player_cells: &mut std::collections::HashMap<csdemo::UserId, (u32, u32, u32)>,
|
player_cells: &mut std::collections::HashMap<csdemo::UserId, (u32, u32, u32)>,
|
||||||
heatmaps: &mut std::collections::HashMap<(csdemo::UserId, String), HeatMap>,
|
heatmaps: &mut std::collections::HashMap<(csdemo::UserId, String), HeatMap>,
|
||||||
) {
|
) {
|
||||||
for entity_state in tick_state
|
if !matches!(entity_state.class.as_ref(), "CCSPlayerPawn" | "CCSTeam") {
|
||||||
.states
|
return;
|
||||||
.iter()
|
}
|
||||||
.filter(|s| matches!(s.class.as_ref(), "CCSPlayerPawn" | "CCSTeam"))
|
|
||||||
{
|
|
||||||
if entity_state.class.as_ref() == "CCSTeam" {
|
if entity_state.class.as_ref() == "CCSTeam" {
|
||||||
let raw_team_name = match entity_state
|
let raw_team_name = match entity_state
|
||||||
.get_prop("CCSTeam.m_szTeamname")
|
.get_prop("CCSTeam.m_szTeamname")
|
||||||
@@ -170,7 +166,7 @@ fn process_tick(
|
|||||||
.flatten()
|
.flatten()
|
||||||
{
|
{
|
||||||
Some(n) => n,
|
Some(n) => n,
|
||||||
None => continue,
|
None => return,
|
||||||
};
|
};
|
||||||
|
|
||||||
for prop in entity_state
|
for prop in entity_state
|
||||||
@@ -182,17 +178,17 @@ fn process_tick(
|
|||||||
teams.insert(prop, raw_team_name.clone());
|
teams.insert(prop, raw_team_name.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
continue;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let pawn_id = PawnID::from(entity_state.id);
|
let pawn_id = PawnID::from(entity_state.id);
|
||||||
let user_id = match pawn_ids.get(&pawn_id).cloned() {
|
let user_id = match pawn_ids.get(&pawn_id).cloned() {
|
||||||
Some(id) => id,
|
Some(id) => id,
|
||||||
None => continue,
|
None => return,
|
||||||
};
|
};
|
||||||
let team = match teams.get(&pawn_id).cloned() {
|
let team = match teams.get(&pawn_id).cloned() {
|
||||||
Some(t) => t,
|
Some(t) => t,
|
||||||
None => continue,
|
None => return,
|
||||||
};
|
};
|
||||||
|
|
||||||
let _inner_guard = tracing::trace_span!("Entity", entity_id=?entity_state.id).entered();
|
let _inner_guard = tracing::trace_span!("Entity", entity_id=?entity_state.id).entered();
|
||||||
@@ -300,7 +296,7 @@ fn process_tick(
|
|||||||
|
|
||||||
// 0 means alive
|
// 0 means alive
|
||||||
if lifestate != 0 {
|
if lifestate != 0 {
|
||||||
continue;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// tracing::trace!("Coord (X, Y, Z): {:?} -> {:?}", (x_coord, y_coord, z_coord), (x_cell, y_cell));
|
// tracing::trace!("Coord (X, Y, Z): {:?} -> {:?}", (x_coord, y_coord, z_coord), (x_cell, y_cell));
|
||||||
@@ -309,7 +305,6 @@ fn process_tick(
|
|||||||
.entry((user_id.clone(), team))
|
.entry((user_id.clone(), team))
|
||||||
.or_insert(HeatMap::new(config.cell_size));
|
.or_insert(HeatMap::new(config.cell_size));
|
||||||
heatmap.increment(x_cell, y_cell);
|
heatmap.increment(x_cell, y_cell);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl core::fmt::Display for HeatMap {
|
impl core::fmt::Display for HeatMap {
|
||||||
|
|||||||
@@ -70,15 +70,13 @@ pub struct PerRound {
|
|||||||
|
|
||||||
pub fn parse(buf: &[u8]) -> Result<PerRound, ()> {
|
pub fn parse(buf: &[u8]) -> Result<PerRound, ()> {
|
||||||
let tmp = csdemo::Container::parse(buf).map_err(|e| ())?;
|
let tmp = csdemo::Container::parse(buf).map_err(|e| ())?;
|
||||||
let output = csdemo::parser::parse(
|
|
||||||
csdemo::FrameIterator::parse(tmp.inner),
|
let output = csdemo::lazyparser::LazyParser::new(tmp);
|
||||||
csdemo::parser::EntityFilter::all(),
|
|
||||||
)
|
let player_info = output.player_info();
|
||||||
.map_err(|e| ())?;
|
|
||||||
|
|
||||||
let mut rounds: Vec<Round> = Vec::new();
|
let mut rounds: Vec<Round> = Vec::new();
|
||||||
for tick in output.entity_states.ticks.iter() {
|
for (tick, state) in output.entities().filter_map(|e| e.ok()) {
|
||||||
for state in tick.states.iter() {
|
|
||||||
let round_start_count = state
|
let round_start_count = state
|
||||||
.get_prop("CCSGameRulesProxy.CCSGameRules.m_nRoundStartCount")
|
.get_prop("CCSGameRulesProxy.CCSGameRules.m_nRoundStartCount")
|
||||||
.map(|v| v.value.as_u32())
|
.map(|v| v.value.as_u32())
|
||||||
@@ -87,7 +85,7 @@ pub fn parse(buf: &[u8]) -> Result<PerRound, ()> {
|
|||||||
if rounds.len() < (round_start_count - 1) as usize {
|
if rounds.len() < (round_start_count - 1) as usize {
|
||||||
rounds.push(Round {
|
rounds.push(Round {
|
||||||
winreason: WinReason::StillInProgress,
|
winreason: WinReason::StillInProgress,
|
||||||
start: tick.tick,
|
start: tick,
|
||||||
end: u32::MAX,
|
end: u32::MAX,
|
||||||
events: Vec::new(),
|
events: Vec::new(),
|
||||||
});
|
});
|
||||||
@@ -100,7 +98,7 @@ pub fn parse(buf: &[u8]) -> Result<PerRound, ()> {
|
|||||||
.flatten();
|
.flatten();
|
||||||
if let Some(round_end_count) = round_end_count {
|
if let Some(round_end_count) = round_end_count {
|
||||||
if rounds.len() == (round_end_count - 1) as usize {
|
if rounds.len() == (round_end_count - 1) as usize {
|
||||||
rounds.last_mut().unwrap().end = tick.tick;
|
rounds.last_mut().unwrap().end = tick;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,13 +115,12 @@ pub fn parse(buf: &[u8]) -> Result<PerRound, ()> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
let mut rounds_iter = rounds.iter_mut();
|
let mut rounds_iter = rounds.iter_mut();
|
||||||
|
|
||||||
let mut current_tick = 0;
|
let mut current_tick = 0;
|
||||||
let mut current_round = rounds_iter.next().unwrap();
|
let mut current_round = rounds_iter.next().unwrap();
|
||||||
'events: for event in output.events.iter() {
|
'events: for event in output.events().filter_map(|e| e.ok()) {
|
||||||
match event {
|
match event {
|
||||||
csdemo::DemoEvent::Tick(tick) => {
|
csdemo::DemoEvent::Tick(tick) => {
|
||||||
current_tick = tick.tick();
|
current_tick = tick.tick();
|
||||||
@@ -154,8 +151,8 @@ pub fn parse(buf: &[u8]) -> Result<PerRound, ()> {
|
|||||||
None => died.clone(),
|
None => died.clone(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let died_player = output.player_info.get(&died).unwrap();
|
let died_player = player_info.get(&died).unwrap();
|
||||||
let attacker_player = output.player_info.get(&attacker).unwrap();
|
let attacker_player = player_info.get(&attacker).unwrap();
|
||||||
|
|
||||||
RoundEvent::Kill {
|
RoundEvent::Kill {
|
||||||
attacker: attacker_player.xuid,
|
attacker: attacker_player.xuid,
|
||||||
|
|||||||
Reference in New Issue
Block a user