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