hldemo/
types.rs

1//! Types for objects which demos consist of.
2
3use errors::*;
4use parse;
5
6/// A Goldsource demo.
7#[derive(Debug, PartialEq)]
8pub struct Demo<'a> {
9    pub header: Header<'a>,
10    pub directory: Directory<'a>,
11}
12
13impl<'a> Demo<'a> {
14    /// Parses a demo.
15    ///
16    /// # Examples
17    ///
18    /// ```no_run
19    /// # use std::error::Error;
20    /// #
21    /// # fn try_main() -> Result<(), Box<Error>> {
22    /// extern crate hldemo;
23    ///
24    /// use std::fs::File;
25    /// use std::io::Read;
26    ///
27    /// let mut bytes = Vec::new();
28    /// let mut f = File::open("demo.dem")?;
29    /// f.read_to_end(&mut bytes);
30    ///
31    /// let demo = hldemo::Demo::parse(&bytes)?;
32    /// #
33    /// #     Ok(())
34    /// # }
35    /// #
36    /// # fn main() {
37    /// #     try_main().unwrap();
38    /// # }
39    /// ```
40    pub fn parse(input: &[u8]) -> Result<Demo> {
41        iresult_into_result(parse::demo(input))
42    }
43
44    /// Parses a demo's header and directory, without parsing frame data.
45    ///
46    /// Parsing frames usually takes a long time, so this function can be used when the frame data
47    /// isn't needed.
48    ///
49    /// # Examples
50    ///
51    /// ```no_run
52    /// # use std::error::Error;
53    /// #
54    /// # fn try_main() -> Result<(), Box<Error>> {
55    /// extern crate hldemo;
56    ///
57    /// use std::fs::File;
58    /// use std::io::Read;
59    ///
60    /// let mut bytes = Vec::new();
61    /// let mut f = File::open("demo.dem")?;
62    /// f.read_to_end(&mut bytes);
63    ///
64    /// let demo = hldemo::Demo::parse_without_frames(&bytes)?;
65    /// #
66    /// #     Ok(())
67    /// # }
68    /// #
69    /// # fn main() {
70    /// #     try_main().unwrap();
71    /// # }
72    /// ```
73    pub fn parse_without_frames(input: &[u8]) -> Result<Demo> {
74        iresult_into_result(parse::demo_without_frames(input))
75    }
76}
77
78#[derive(Debug, PartialEq, Eq)]
79pub struct Header<'a> {
80    pub demo_protocol: i32,
81    pub net_protocol: i32,
82    pub map_name: &'a [u8],
83    pub game_dir: &'a [u8],
84    pub map_crc: i32,
85    pub directory_offset: i32,
86}
87
88#[derive(Debug, PartialEq)]
89pub struct Directory<'a> {
90    pub entries: Vec<DirectoryEntry<'a>>,
91}
92
93#[derive(Debug, PartialEq)]
94pub struct DirectoryEntry<'a> {
95    pub entry_type: i32,
96    pub description: &'a [u8],
97    pub flags: i32,
98    pub cd_track: i32,
99    pub track_time: f32,
100    pub frame_count: i32,
101    pub offset: i32,
102    pub file_length: i32,
103
104    pub frames: Vec<Frame<'a>>,
105}
106
107#[derive(Debug, PartialEq)]
108pub struct Frame<'a> {
109    pub time: f32,
110    pub frame: i32,
111    pub data: FrameData<'a>,
112}
113
114#[derive(Debug, PartialEq)]
115pub enum FrameData<'a> {
116    DemoStart,
117    ConsoleCommand(ConsoleCommandData<'a>),
118    ClientData(ClientDataData),
119    NextSection,
120    Event(EventData),
121    WeaponAnim(WeaponAnimData),
122    Sound(SoundData<'a>),
123    DemoBuffer(DemoBufferData<'a>),
124    NetMsg(NetMsgData<'a>),
125}
126
127#[derive(Debug, PartialEq, Eq)]
128pub struct ConsoleCommandData<'a> {
129    pub command: &'a [u8],
130}
131
132#[derive(Debug, PartialEq)]
133pub struct ClientDataData {
134    pub origin: [f32; 3],
135    pub viewangles: [f32; 3],
136    pub weapon_bits: i32,
137    pub fov: f32,
138}
139
140#[derive(Debug, PartialEq)]
141pub struct EventData {
142    pub flags: i32,
143    pub index: i32,
144    pub delay: f32,
145    pub args: EventArgs,
146}
147
148#[derive(Debug, PartialEq)]
149pub struct EventArgs {
150    pub flags: i32,
151    pub entity_index: i32,
152    pub origin: [f32; 3],
153    pub angles: [f32; 3],
154    pub velocity: [f32; 3],
155    pub ducking: i32,
156    pub fparam1: f32,
157    pub fparam2: f32,
158    pub iparam1: i32,
159    pub iparam2: i32,
160    pub bparam1: i32,
161    pub bparam2: i32,
162}
163
164#[derive(Debug, PartialEq, Eq)]
165pub struct WeaponAnimData {
166    pub anim: i32,
167    pub body: i32,
168}
169
170#[derive(Debug, PartialEq)]
171pub struct SoundData<'a> {
172    pub channel: i32,
173    pub sample: &'a [u8],
174    pub attenuation: f32,
175    pub volume: f32,
176    pub flags: i32,
177    pub pitch: i32,
178}
179
180#[derive(Debug, PartialEq, Eq)]
181pub struct DemoBufferData<'a> {
182    pub buffer: &'a [u8],
183}
184
185#[derive(Debug, PartialEq)]
186pub struct NetMsgData<'a> {
187    pub info: NetMsgInfo<'a>,
188    pub incoming_sequence: i32,
189    pub incoming_acknowledged: i32,
190    pub incoming_reliable_acknowledged: i32,
191    pub incoming_reliable_sequence: i32,
192    pub outgoing_sequence: i32,
193    pub reliable_sequence: i32,
194    pub last_reliable_sequence: i32,
195    pub msg: &'a [u8],
196}
197
198#[derive(Debug, PartialEq)]
199pub struct NetMsgInfo<'a> {
200    pub timestamp: f32,
201    pub ref_params: RefParams,
202    pub usercmd: UserCmd,
203    pub movevars: MoveVars<'a>,
204    pub view: [f32; 3],
205    pub viewmodel: i32,
206}
207
208#[derive(Debug, PartialEq)]
209pub struct RefParams {
210    pub vieworg: [f32; 3],
211    pub viewangles: [f32; 3],
212    pub forward: [f32; 3],
213    pub right: [f32; 3],
214    pub up: [f32; 3],
215    pub frametime: f32,
216    pub time: f32,
217    pub intermission: i32,
218    pub paused: i32,
219    pub spectator: i32,
220    pub onground: i32,
221    pub waterlevel: i32,
222    pub simvel: [f32; 3],
223    pub simorg: [f32; 3],
224    pub viewheight: [f32; 3],
225    pub idealpitch: f32,
226    pub cl_viewangles: [f32; 3],
227    pub health: i32,
228    pub crosshairangle: [f32; 3],
229    pub viewsize: f32,
230    pub punchangle: [f32; 3],
231    pub maxclients: i32,
232    pub viewentity: i32,
233    pub playernum: i32,
234    pub max_entities: i32,
235    pub demoplayback: i32,
236    pub hardware: i32,
237    pub smoothing: i32,
238    pub ptr_cmd: i32,
239    pub ptr_movevars: i32,
240    pub viewport: [i32; 4usize],
241    pub next_view: i32,
242    pub only_client_draw: i32,
243}
244
245#[derive(Debug, PartialEq)]
246pub struct UserCmd {
247    pub lerp_msec: i16,
248    pub msec: u8,
249    pub viewangles: [f32; 3],
250    pub forwardmove: f32,
251    pub sidemove: f32,
252    pub upmove: f32,
253    pub lightlevel: i8,
254    pub buttons: u16,
255    pub impulse: i8,
256    pub weaponselect: i8,
257    pub impact_index: i32,
258    pub impact_position: [f32; 3],
259}
260
261#[derive(Debug, PartialEq)]
262pub struct MoveVars<'a> {
263    pub gravity: f32,
264    pub stopspeed: f32,
265    pub maxspeed: f32,
266    pub spectatormaxspeed: f32,
267    pub accelerate: f32,
268    pub airaccelerate: f32,
269    pub wateraccelerate: f32,
270    pub friction: f32,
271    pub edgefriction: f32,
272    pub waterfriction: f32,
273    pub entgravity: f32,
274    pub bounce: f32,
275    pub stepsize: f32,
276    pub maxvelocity: f32,
277    pub zmax: f32,
278    pub wave_height: f32,
279    pub footsteps: i32,
280    pub sky_name: &'a [u8],
281    pub rollangle: f32,
282    pub rollspeed: f32,
283    pub skycolor_r: f32,
284    pub skycolor_g: f32,
285    pub skycolor_b: f32,
286    pub skyvec_x: f32,
287    pub skyvec_y: f32,
288    pub skyvec_z: f32,
289}