Skip to main content

source2_demo/
lib.rs

1#![doc = include_str!("../README.md")]
2#![warn(missing_docs)]
3#![allow(clippy::too_many_arguments)]
4
5#[cfg(test)]
6extern crate self as source2_demo;
7
8mod display;
9mod entity;
10pub mod error;
11mod event;
12mod macros;
13mod parser;
14mod stream;
15mod string_table;
16
17#[cfg(test)]
18mod tests;
19
20/// Reader types used by advanced parser integrations.
21///
22/// Most applications should construct parsers with [`Parser::from_slice`] or
23/// [`Parser::from_reader`]. These re-exports are primarily for wrappers that
24/// need to store a concrete parser type.
25pub mod reader {
26    pub(crate) use crate::stream::reader::{
27        BitsReader, FieldPathCodec, FieldReader, MessageReader, ReplayInfoReader,
28    };
29    pub use crate::stream::reader::{SeekableReader, SliceReader};
30}
31
32/// Demo rewriting APIs and lower-level bitstream writer utilities.
33///
34/// This module is the preferred import path for writing or rewriting demo
35/// files. It includes [`DemoWriter`](crate::writer::DemoWriter),
36/// [`DemoRewriter`](crate::writer::DemoRewriter), packet message helpers, and
37/// the bitstream writer utilities used by advanced rewrites.
38pub mod writer {
39    pub use crate::error::ParserError;
40    pub use crate::parser::{
41        rewrite_protobuf_message, DemoRewriter, DemoWriter, MessageRewrite, PacketMessage,
42        RewriteInterests,
43    };
44    pub use crate::stream::writer::{
45        write_demo_message, write_demo_message_with_compression, write_var_u32_to_buf,
46        write_var_u32_to_vec, write_var_u64_to_buf, write_var_u64_to_vec, BitsWriter,
47        BitstreamWriter, MessageWriter,
48    };
49    pub use crate::string_table::StringTableEntryUpdate;
50    pub use source2_demo_macros::{
51        replace_entity_field, rewrite_demo_message, rewrite_demo_string_tables, rewrite_field,
52        rewrite_packet_message, rewrite_packet_messages, rewrite_string_table_entry,
53        rewrite_svc_create_string_table, rewrite_svc_update_string_table, rewriter,
54        should_rewrite_entity, should_track_entity,
55    };
56}
57
58/// Protocol buffer definitions for Source 2 games.
59///
60/// This module re-exports all protobuf message types used by the parser.
61/// The available messages depend on which game feature is enabled.
62///
63/// # Examples
64///
65/// ```no_run
66/// use source2_demo::proto::*;
67///
68/// // Decode a protobuf message
69/// # let bytes: &[u8] = &[];
70/// let msg = CDemoFileInfo::decode(bytes)?;
71/// # Ok::<(), Box<dyn std::error::Error>>(())
72/// ```
73pub mod proto {
74    pub use source2_demo_protobufs::prost::Message;
75    pub use source2_demo_protobufs::*;
76}
77
78/// Prelude module for convenient reading/parsing imports.
79///
80/// Import this module to get access to all commonly used types and traits:
81///
82/// ```
83/// use source2_demo::prelude::*;
84/// ```
85///
86/// This includes:
87/// - Core types: [`Parser`], [`Context`], [`Entity`], [`Observer`]
88/// - Traits and macros: [`observer`], [`on_message`], [`property!`]
89/// - Protobuf enums: Message type enumerations for each game
90///
91/// Demo writing APIs are intentionally not included. Import them from
92/// [`crate::writer`] instead.
93pub mod prelude {
94    pub use crate::entity::field::FieldValue;
95    pub use crate::entity::{Entity, EntityEvents, EntityField};
96    pub use crate::error::ParserError;
97    pub use crate::event::{EventValue, GameEvent, GameEventList};
98    pub use crate::parser::{Context, DemoRunner, Interests, Observer, ObserverResult, Parser};
99    pub use crate::string_table::{StringTable, StringTableRow, StringTables};
100    pub use crate::{property, try_property};
101
102    pub use source2_demo_macros::*;
103
104    pub use source2_demo_protobufs::prost::Message;
105    pub use source2_demo_protobufs::EBaseGameEvents;
106    pub use source2_demo_protobufs::EBaseUserMessages;
107    pub use source2_demo_protobufs::EDemoCommands;
108    pub use source2_demo_protobufs::NetMessages;
109    pub use source2_demo_protobufs::SvcMessages;
110
111    #[cfg(feature = "dota")]
112    pub use crate::event::CombatLogEntry;
113    #[cfg(feature = "dota")]
114    pub use crate::proto::EDotaUserMessages;
115
116    #[cfg(feature = "deadlock")]
117    pub use crate::proto::CitadelUserMessageIds;
118    #[cfg(feature = "deadlock")]
119    pub use crate::proto::ECitadelGameEvents;
120
121    #[cfg(feature = "cs2")]
122    pub use crate::proto::ECsgoGameEvents;
123    #[cfg(feature = "cs2")]
124    pub use crate::proto::ECstrike15UserMessages;
125}
126
127// Re-export commonly used types at the crate root
128pub use crate::entity::field::{FieldRewriteResult, FieldValue, IntoFieldValue};
129pub use crate::entity::*;
130pub use crate::event::*;
131pub use crate::parser::{Context, DemoRunner, Interests, Observer, ObserverResult, Parser};
132pub use crate::string_table::{StringTable, StringTableRow, StringTables};
133pub use source2_demo_macros::*;
134
135/// Fast hash map using FxHash algorithm.
136///
137/// This type alias is used throughout the crate for better performance
138/// compared to the standard library's `HashMap`.
139pub type HashMap<K, V> = hashbrown::HashMap<K, V, rustc_hash::FxBuildHasher>;
140
141/// Fast hash set using FxHash algorithm.
142///
143/// This type alias is used throughout the crate for better performance
144/// compared to the standard library's `HashSet`.
145pub type HashSet<T> = hashbrown::HashSet<T, rustc_hash::FxBuildHasher>;
146
147#[cfg(feature = "dota")]
148pub use crate::event::CombatLogEntry;
149
150#[cfg(feature = "mimalloc")]
151use mimalloc::MiMalloc;
152#[cfg(feature = "mimalloc")]
153#[global_allocator]
154static GLOBAL: MiMalloc = MiMalloc;