s2protocol 3.5.3

A parser for Starcraft II - Replay format, exports to different target formats
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//! Arrow Specific handling of data.

#[cfg(feature = "dep_arrow")]
use arrow::{
    array::Array, array::ArrayRef, datatypes::DataType, datatypes::Schema,
    record_batch::RecordBatch,
};
#[cfg(feature = "dep_arrow")]
use arrow_convert::{field::ArrowField, serialize::TryIntoArrow};
use init_data::InitData;
#[cfg(feature = "dep_arrow")]
use rayon::prelude::*;

use crate::cli::get_matching_files;
use crate::details::{PlayerLobbyDetails, PlayerLobbyDetailsFlatRow};
use crate::game_events::VersionedBalanceUnit;
use crate::tracker_events;
use crate::*;
use clap::Subcommand;
use std::path::PathBuf;
pub mod ipc_writer;
use ipc_writer::*;

/// The supported Arrow IPC types
#[derive(Debug, Subcommand, Clone)]
pub enum ArrowIpcTypes {
    /// Writes the [`crate::init_data::UserInitDataFlatRow`] flat row to an Arrow IPC file
    UserInitData,
    /// Writes the [`crate::details::PlayerLobbyDetailsFlatRow`] flat row to an Arrow IPC file
    Details,
    /// Writes the [`crate::tracker_events::PlayerStatsEvent`] to an Arrow IPC file
    Stats,
    /// Writes the [`crate::tracker_events::UpgradeEvent`] to an Arrow IPC file
    Upgrades,
    /// Writes the [`crate::tracker_events::UnitBornEvent`] to an Arrow IPC file
    UnitBorn,
    /// Writes the [`crate::tracker_events::UnitDiedEvent`] to an Arrow IPC file
    UnitDied,
    /// Writes the [`crate::message_events::MessageEvent`] to an Arrow IPC file
    MessageEvents,
    /// Writes the [`crate::game_events::CmdTargetPointEventFlatRow`] to an Arrow IPC file
    CmdTargetPoint,
    /// Writes the [`crate::game_events::CmdTargetUnitEventFlatRow`] to an Arrow IPC file
    CmdTargetUnit,
    /// Writes all the implemented flat row types to Arrow IPC files inside the output directory
    All,
}

impl ArrowIpcTypes {
    /// Returns the schema for the chosen output type
    pub fn schema(&self) -> Schema {
        match self {
            Self::UserInitData => {
                if let DataType::Struct(fields) = init_data::UserInitDataFlatRow::data_type() {
                    Schema::new(fields.clone())
                } else {
                    panic!("Invalid schema, expected struct");
                }
            }
            Self::Details => {
                if let DataType::Struct(fields) = details::PlayerLobbyDetailsFlatRow::data_type() {
                    Schema::new(fields.clone())
                } else {
                    panic!("Invalid schema, expected struct");
                }
            }
            Self::Stats => {
                if let DataType::Struct(fields) = tracker_events::PlayerStatsFlatRow::data_type() {
                    Schema::new(fields.clone())
                } else {
                    panic!("Invalid schema, expected struct");
                }
            }
            Self::Upgrades => {
                if let DataType::Struct(fields) = tracker_events::UpgradeEventFlatRow::data_type() {
                    Schema::new(fields.clone())
                } else {
                    panic!("Invalid schema, expected struct");
                }
            }
            Self::UnitBorn => {
                if let DataType::Struct(fields) = tracker_events::UnitBornEventFlatRow::data_type()
                {
                    Schema::new(fields.clone())
                } else {
                    panic!("Invalid schema, expected struct");
                }
            }
            Self::UnitDied => {
                if let DataType::Struct(fields) = tracker_events::UnitDiedEventFlatRow::data_type()
                {
                    Schema::new(fields.clone())
                } else {
                    panic!("Invalid schema, expected struct");
                }
            }
            Self::CmdTargetPoint => {
                if let DataType::Struct(fields) =
                    game_events::CmdTargetPointEventFlatRow::data_type()
                {
                    Schema::new(fields.clone())
                } else {
                    panic!("Invalid schema, expected struct");
                }
            }
            Self::CmdTargetUnit => {
                if let DataType::Struct(fields) =
                    game_events::CmdTargetUnitEventFlatRow::data_type()
                {
                    Schema::new(fields.clone())
                } else {
                    panic!("Invalid schema, expected struct");
                }
            }
            _ => unimplemented!(),
        }
    }

    /// Writes a snapshot of the replay collection.
    /// A snapshot is a collection of generated files that work together.
    /// The consistency of the files is not yet implemented.
    /// But the files should have been generated at around the same time.
    /// If one file lags behind, it may be from an incomplete data generation.
    /// i.e. this function is called but it errors in the middle and no retries/fixes are done.
    /// Two things todo:
    /// First delete all the files in the snapshot directory.
    /// Add a snashopt generation timestamp and when reads are done, they are checked for
    /// very basic timestamp write consistency.
    #[tracing::instrument(level = "debug")]
    pub fn handle_write_snapshot(
        sources: Vec<InitData>,
        output: PathBuf,
        unit_abilities: &HashMap<(u32, String), VersionedBalanceUnit>,
        serially: bool,
    ) -> Result<(), Box<dyn std::error::Error>> {
        if !output.is_dir() {
            panic!("Output must be a directory for types 'all'");
        }
        // output must be a directory, for this directory we will create the following files:
        // details.ipc
        // stats.ipc
        // upgrades.ipc
        // unit_born.ipc
        // unit_cmd_target_point.ipc
        // unit_cmd_target_unit.ipc
        Self::Details.handle_details_ipc_cmd(sources.clone(), output.join("details.ipc"))?;
        Self::Stats.handle_tracker_events(
            sources.clone(),
            output.join("stats.ipc"),
            unit_abilities,
            serially,
        )?;
        Self::Upgrades.handle_tracker_events(
            sources.clone(),
            output.join("upgrades.ipc"),
            unit_abilities,
            serially,
        )?;
        Self::UnitBorn.handle_tracker_events(
            sources.clone(),
            output.join("unit_born.ipc"),
            unit_abilities,
            serially,
        )?;
        Self::UnitDied.handle_tracker_events(
            sources.clone(),
            output.join("unit_died.ipc"),
            unit_abilities,
            serially,
        )?;
        Self::CmdTargetPoint.handle_game_events(
            sources.clone(),
            output.join("cmd_target_point.ipc"),
            unit_abilities,
            serially,
        )?;
        Self::CmdTargetUnit.handle_game_events(
            sources.clone(),
            output.join("cmd_target_unit.ipc"),
            unit_abilities,
            serially,
        )?;
        Ok(())
    }

    /// Creates a new Arrow IPC file with the tracker events data
    /// This seems to be small enough to not need to be chunked and is done in parallel
    /// This requires 1.5GB of RAM for 3600 files, so maybe not good for real players.
    #[tracing::instrument(level = "debug")]
    pub fn handle_tracker_events(
        &self,
        sources: Vec<InitData>,
        output: PathBuf,
        versioned_abilities: &HashMap<(u32, String), VersionedBalanceUnit>,
        serially: bool,
    ) -> Result<(), Box<dyn std::error::Error>> {
        tracing::info!("Processing TrackerEvents IPC write request: {:?}", self);
        let writer = open_arrow_mutex_writer(output, self.schema())?;

        // XXX: Ok this is really very ugly/embarrasing, gotta find a way to switch between serial and parallel processing
        let total_records = if serially {
            sources
                .iter()
                .filter_map(|source| {
                    let event_iterator =
                        SC2EventIterator::new(source, versioned_abilities.clone()).ok()?;
                    let (res, batch_len): (ArrayRef, usize) = match self {
                        Self::Stats => {
                            let batch = event_iterator.collect_into_player_stats_flat_rows();
                            (batch.try_into_arrow().ok()?, batch.len())
                        }
                        Self::Upgrades => {
                            let batch = event_iterator.collect_into_upgrades_flat_rows();
                            (batch.try_into_arrow().ok()?, batch.len())
                        }
                        Self::UnitBorn => {
                            let batch = event_iterator.collect_into_unit_born_flat_rows();
                            (batch.try_into_arrow().ok()?, batch.len())
                        }
                        Self::UnitDied => {
                            let batch = event_iterator.collect_into_unit_died_flat_rows();
                            (batch.try_into_arrow().ok()?, batch.len())
                        }
                        _ => unimplemented!(),
                    };
                    write_to_arrow_mutex_writer(&writer, res, batch_len)
                })
                .sum::<usize>()
        } else {
            sources
                .par_iter()
                .filter_map(|source| {
                    let event_iterator =
                        SC2EventIterator::new(source, versioned_abilities.clone()).ok()?;
                    let (res, batch_len): (ArrayRef, usize) = match self {
                        Self::Stats => {
                            let batch = event_iterator.collect_into_player_stats_flat_rows();
                            (batch.try_into_arrow().ok()?, batch.len())
                        }
                        Self::Upgrades => {
                            let batch = event_iterator.collect_into_upgrades_flat_rows();
                            (batch.try_into_arrow().ok()?, batch.len())
                        }
                        Self::UnitBorn => {
                            let batch = event_iterator.collect_into_unit_born_flat_rows();
                            (batch.try_into_arrow().ok()?, batch.len())
                        }
                        Self::UnitDied => {
                            let batch = event_iterator.collect_into_unit_died_flat_rows();
                            (batch.try_into_arrow().ok()?, batch.len())
                        }
                        _ => unimplemented!(),
                    };
                    write_to_arrow_mutex_writer(&writer, res, batch_len)
                })
                .sum::<usize>()
        };
        tracing::info!("Loaded {} records", total_records);
        close_arrow_mutex_writer(writer)
    }

    /// Creates a new Arrow IPC file with the game events data
    /// This requires 1.5GB of RAM for 3600 files, so maybe not good for real players.
    #[tracing::instrument(level = "debug")]
    pub fn handle_game_events(
        &self,
        sources: Vec<InitData>,
        output: PathBuf,
        versioned_abilities: &HashMap<(u32, String), VersionedBalanceUnit>,
        serially: bool,
    ) -> Result<(), Box<dyn std::error::Error>> {
        tracing::info!("Processing GameEvents IPC write request: {:?}", self);
        let writer = open_arrow_mutex_writer(output, self.schema())?;

        let total_records = if serially {
            sources
                .iter()
                .filter_map(|source| {
                    let event_iterator =
                        SC2EventIterator::new(source, versioned_abilities.clone()).ok()?;
                    let (res, batch_len): (ArrayRef, usize) = match self {
                        Self::CmdTargetPoint => {
                            let batch =
                                event_iterator.collect_into_game_cmd_target_points_flat_rows();
                            (batch.try_into_arrow().ok()?, batch.len())
                        }
                        Self::CmdTargetUnit => {
                            let batch =
                                event_iterator.collect_into_game_cmd_target_units_flat_rows();
                            (batch.try_into_arrow().ok()?, batch.len())
                        }
                        e => unimplemented!("{:?}", e),
                    };
                    write_to_arrow_mutex_writer(&writer, res, batch_len)
                })
                .sum::<usize>()
        } else {
            sources
                .par_iter()
                .filter_map(|source| {
                    let event_iterator =
                        SC2EventIterator::new(source, versioned_abilities.clone()).ok()?;
                    let (res, batch_len): (ArrayRef, usize) = match self {
                        Self::CmdTargetPoint => {
                            let batch =
                                event_iterator.collect_into_game_cmd_target_points_flat_rows();
                            (batch.try_into_arrow().ok()?, batch.len())
                        }
                        Self::CmdTargetUnit => {
                            let batch =
                                event_iterator.collect_into_game_cmd_target_units_flat_rows();
                            (batch.try_into_arrow().ok()?, batch.len())
                        }
                        e => unimplemented!("{:?}", e),
                    };
                    write_to_arrow_mutex_writer(&writer, res, batch_len)
                })
                .sum::<usize>()
        };
        tracing::info!("Loaded {} records", total_records);
        close_arrow_mutex_writer(writer)
    }

    /// Creates a new Arrow IPC file with the details data
    #[tracing::instrument(level = "debug")]
    pub fn handle_read_once_write_all(
        &self,
        sources: Vec<InitData>,
        output: PathBuf,
    ) -> Result<(), Box<dyn std::error::Error>> {
        tracing::info!("Processing Read Once Write All IPC request");
        // process the sources in parallel consuming into the batch variable

        let details_flaw_rows: Vec<PlayerLobbyDetailsFlatRow> = sources
            .iter()
            .flat_map(|source| {
                let res: Vec<PlayerLobbyDetails> = match source.try_into() {
                    Ok(details) => details,
                    Err(err) => {
                        tracing::error!("Error reading details: {:?}", err);
                        return vec![];
                    }
                };
                res.into_iter()
                    .map(|d| d.into())
                    .collect::<Vec<PlayerLobbyDetailsFlatRow>>()
            })
            .collect();
        let res: ArrayRef = details_flaw_rows.try_into_arrow()?;
        let chunk: RecordBatch = res
            .as_any()
            .downcast_ref::<arrow::array::StructArray>()
            .unwrap()
            .into();

        write_batches(output, Self::Details.schema(), chunk)?;
        Ok(())
    }
    /// Creates a new Arrow IPC file with the details data
    #[tracing::instrument(level = "debug")]
    pub fn handle_details_ipc_cmd(
        &self,
        sources: Vec<InitData>,
        output: PathBuf,
    ) -> Result<(), Box<dyn std::error::Error>> {
        tracing::info!("Processing Details IPC write request");
        // process the sources in parallel consuming into the batch variable

        let details_flaw_rows: Vec<PlayerLobbyDetailsFlatRow> = sources
            .iter()
            .flat_map(|source| {
                let res: Vec<PlayerLobbyDetails> = match source.try_into() {
                    Ok(details) => details,
                    Err(err) => {
                        tracing::error!("Error reading details: {:?}", err);
                        return vec![];
                    }
                };
                res.into_iter()
                    .map(|d| d.into())
                    .collect::<Vec<PlayerLobbyDetailsFlatRow>>()
            })
            .collect();
        let res: ArrayRef = details_flaw_rows.try_into_arrow()?;
        let chunk: RecordBatch = res
            .as_any()
            .downcast_ref::<arrow::array::StructArray>()
            .unwrap()
            .into();

        write_batches(output, Self::Details.schema(), chunk)?;
        Ok(())
    }

    /// Handles the Arrow IPC command variants
    #[tracing::instrument(level = "debug")]
    pub fn handle_arrow_ipc_cmd(
        source: PathBuf,
        output: PathBuf,
        cmd: &WriteArrowIpcProps,
        unit_abilities: &HashMap<(u32, String), VersionedBalanceUnit>,
        serially: bool,
    ) -> Result<(), Box<dyn std::error::Error>> {
        println!(
            "Processing Arrow write request with scan_max_files: {}, traverse_max_depth: {}, process_max_files: {}, min_version: {:?}, max_version: {:?}",
            cmd.scan_max_files,
            cmd.process_max_files,
            cmd.traverse_max_depth,
            cmd.min_version,
            cmd.max_version
        );
        let sources = get_matching_files(source, cmd.scan_max_files, cmd.traverse_max_depth)?;
        println!("Located {} matching files by extension", sources.len());
        let sources: Vec<InitData> = if !serially {
            sources
                .iter()
                .enumerate()
                .filter_map(|(idx, source)| {
                    InitData::try_from((source.clone(), u64::try_from(idx).unwrap())).ok()
                })
                .collect::<Vec<InitData>>()
        } else {
            sources
                .par_iter()
                .enumerate()
                .filter_map(|(idx, source)| {
                    InitData::try_from((source.clone(), u64::try_from(idx).unwrap())).ok()
                })
                .collect::<Vec<InitData>>()
        };
        let sources: Vec<InitData> = sources
            .into_iter()
            .filter(|source| {
                if let Some(min_version) = cmd.min_version
                    && source.version < min_version
                {
                    return false;
                }
                if let Some(max_version) = cmd.max_version
                    && source.version > max_version
                {
                    return false;
                }
                true
            })
            .take(cmd.process_max_files)
            .collect();
        if sources.is_empty() {
            panic!("No files found");
        } else {
            println!(
                "{} files have valid init data, processing...",
                sources.len()
            );
        }
        Self::handle_write_snapshot(sources, output, unit_abilities, serially)
    }
}