source2_demo/parser/context.rs
1//! Parser context containing the current replay state.
2//!
3//! The [`Context`] struct maintains all the state information about the replay
4//! as it's being parsed, including entities, string tables, game events, and
5//! the current tick.
6
7use crate::entity::field::*;
8use crate::entity::*;
9use crate::event::*;
10use crate::string_table::*;
11use crate::HashMap;
12use source2_demo_protobufs::CDemoFileInfo;
13use std::rc::Rc;
14
15/// Current replay state accessible to observers.
16///
17/// The context is passed to all observer callbacks and provides access to:
18/// - Entity state and properties
19/// - String tables
20/// - Game event definitions
21/// - Current tick and timing information
22/// - Replay metadata
23///
24/// # Examples
25///
26/// ## Accessing entities
27///
28/// ```no_run
29/// use source2_demo::prelude::*;
30///
31/// #[derive(Default)]
32/// struct HeroStats;
33///
34/// #[observer]
35/// #[uses_entities]
36/// impl HeroStats {
37/// #[on_tick_start]
38/// fn on_tick_start(&mut self, ctx: &Context) -> ObserverResult {
39/// for entity in ctx.entities().iter() {
40/// if entity.class().name().starts_with("CDOTA_Unit_Hero_") {
41/// let health: i32 = property!(entity, "m_iHealth");
42/// println!("{}: {}", entity.class().name(), health);
43/// }
44/// }
45/// Ok(())
46/// }
47/// }
48/// ```
49///
50/// ## Accessing string tables
51///
52/// ```no_run
53/// use source2_demo::prelude::*;
54///
55/// #[derive(Default)]
56/// struct TableReader;
57///
58/// #[observer]
59/// #[uses_string_tables]
60/// impl TableReader {
61/// #[on_tick_start]
62/// fn on_tick_start(&mut self, ctx: &Context) -> ObserverResult {
63/// if let Ok(table) = ctx.string_tables().get_by_name("ActiveModifiers") {
64/// println!("Active modifiers: {}", table.iter().count());
65/// }
66/// Ok(())
67/// }
68/// }
69/// ```
70pub struct Context {
71 pub(crate) classes: Classes,
72 pub(crate) entities: Entities,
73 pub(crate) string_tables: StringTables,
74 pub(crate) game_events: GameEventList,
75
76 pub(crate) tick: u32,
77 pub(crate) previous_tick: u32,
78 pub(crate) net_tick: u32,
79
80 pub(crate) game_build: u32,
81 pub(crate) replay_info: CDemoFileInfo,
82
83 pub(crate) baselines: BaselineContainer,
84 pub(crate) serializers: HashMap<Box<str>, Rc<Serializer>>,
85 pub(crate) last_full_packet_tick: u32,
86}
87
88impl Default for Context {
89 fn default() -> Self {
90 Context {
91 classes: Classes::default(),
92 entities: Entities::default(),
93 string_tables: StringTables::default(),
94 game_events: Default::default(),
95 tick: u32::MAX,
96 previous_tick: u32::MAX,
97 net_tick: u32::MAX,
98 game_build: 0,
99 replay_info: CDemoFileInfo::default(),
100 baselines: BaselineContainer::default(),
101 serializers: HashMap::default(),
102 last_full_packet_tick: u32::MAX,
103 }
104 }
105}
106
107impl Context {
108 pub(crate) fn new(replay_info: CDemoFileInfo) -> Self {
109 Context {
110 replay_info,
111 ..Default::default()
112 }
113 }
114}
115
116impl Context {
117 /// Returns a reference to the entity container.
118 ///
119 /// Use this to access all entities in the game and query their properties.
120 ///
121 /// # Examples
122 ///
123 /// ```no_run
124 /// use source2_demo::prelude::*;
125 ///
126 /// # fn example(ctx: &Context) -> anyhow::Result<()> {
127 /// // Get entity by index
128 /// let entity = ctx.entities().get_by_index(0)?;
129 ///
130 /// // Find entity by class name
131 /// let player_resource = ctx.entities().get_by_class_name("CDOTA_PlayerResource")?;
132 ///
133 /// // Iterate all entities
134 /// for entity in ctx.entities().iter() {
135 /// println!("{}", entity.class().name());
136 /// }
137 /// # Ok(())
138 /// # }
139 /// ```
140 pub fn classes(&self) -> &Classes {
141 &self.classes
142 }
143
144 /// Returns a reference to the entity container.
145 ///
146 /// Provides access to all game entities and their properties.
147 /// Requires `Interests::ENABLE_ENTITY` to be populated.
148 pub fn entities(&self) -> &Entities {
149 &self.entities
150 }
151
152 /// Returns a reference to the string tables.
153 ///
154 /// String tables contain game data like hero names, item names, etc.
155 /// Requires `Interests::ENABLE_STRINGTAB` to be populated.
156 pub fn string_tables(&self) -> &StringTables {
157 &self.string_tables
158 }
159
160 /// Returns a reference to the game event list.
161 ///
162 /// Contains definitions for all game events in the replay.
163 pub fn game_events(&self) -> &GameEventList {
164 &self.game_events
165 }
166
167 /// Returns the current tick number.
168 ///
169 /// The tick represents the current game simulation step.
170 /// Typically runs at 30 ticks per second.
171 pub fn tick(&self) -> u32 {
172 self.tick
173 }
174
175 /// Returns the current network tick.
176 ///
177 /// The network tick from the last processed packet.
178 pub fn net_tick(&self) -> u32 {
179 self.net_tick
180 }
181
182 /// Returns the game build number.
183 ///
184 /// Identifies the specific version of the game that created this replay.
185 pub fn game_build(&self) -> u32 {
186 self.game_build
187 }
188
189 /// Returns replay file metadata.
190 ///
191 /// Contains information about the replay including duration, map, and game info.
192 pub fn replay_info(&self) -> &CDemoFileInfo {
193 &self.replay_info
194 }
195}