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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
use serde::{Deserialize, Serialize};
use serde_xml_rs;
use std::collections::HashMap;
use std::path::PathBuf;
use tracing;

pub mod json_handler;
pub use json_handler::*;

// when self.cmds[0].meta.kind == "passive" we need to return None.

pub fn traverse_versioned_balance_abilities(
    root_dir: impl Into<PathBuf>,
) -> std::io::Result<HashMap<(u32, String), VersionedBalanceUnit>> {
    let mut res: HashMap<(u32, String), VersionedBalanceUnit> = HashMap::new();
    let root_dir: PathBuf = root_dir.into();
    tracing::debug!(
        "Traversing versioned balance abilities in {}",
        root_dir.display()
    );
    if !root_dir.is_dir() {
        return Ok(res);
    }
    for versioned_dir in std::fs::read_dir(root_dir)? {
        let versioned_dir = versioned_dir?.path();
        if !versioned_dir.is_dir() {
            continue;
        }
        let versioned_balance_data_dir = versioned_dir.join("BalanceData");
        if !versioned_balance_data_dir.is_dir() {
            continue;
        }
        let protocol_version: u32 = match versioned_dir
            .file_name()
            .and_then(|s| s.to_str())
            .and_then(|s| s.parse().ok())
        {
            Some(v) => v,
            None => {
                tracing::warn!(
                    "Skipping versioned directory {:?} as it does not contain a number",
                    versioned_dir
                );
                continue;
            }
        };
        tracing::debug!(
            "Traversing versioned balance data directory: {:?}",
            versioned_balance_data_dir
        );
        for entry in std::fs::read_dir(versioned_balance_data_dir)? {
            let entry = entry?;
            let path = entry.path();
            if !path.is_file() {
                continue;
            }
            if let Some(ext) = path.extension()
                && ext == "xml"
                && path.is_file()
            {
                tracing::debug!("Processing XML file: {:?}", path);
                let files_content = std::fs::read_to_string(&path)?;
                let unit = match serde_xml_rs::from_str::<VersionedBalanceUnit>(&files_content) {
                    Ok(val) => val,
                    Err(err) => {
                        tracing::error!("Failed to parse XML file {:?}: {}", path, err);
                        continue;
                    }
                };
                res.insert((protocol_version, unit.id.clone()), unit);
            }
        }
    }
    tracing::info!("Processed {} total units", res.len(),);

    Ok(res)
}

/// The Meta has the name and the tooltip required.
/// <meta name="710" icon="btn-building-protoss-stargate" race="Prot" hotkey="734" tooltip="735" source="Liberty.SC2Mod" index="90"/>
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Meta {
    #[serde(rename = "@name")]
    pub name: String,
    #[serde(rename = "@tooltip")]
    pub tooltip: String,
    #[serde(rename = "@icon")]
    pub icon: Option<String>,
    #[serde(rename = "@race")]
    pub race: Option<String>,
    #[serde(rename = "@hotkey")]
    pub hotkey: Option<String>,
    #[serde(rename = "@source")]
    pub source: Option<String>,
    #[serde(rename = "@index")]
    pub index: Option<u32>,
    #[serde(rename = "@type")]
    pub kind: Option<String>,
}

/// The Life of a unit.
/// <life start="600" max="600"/>
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Life {
    #[serde(rename = "@start")]
    pub start: u32,
    #[serde(rename = "@max")]
    pub max: u32,
    #[serde(rename = "@regenRate")]
    pub regen_rate: Option<f32>,
}

/// The Shields of a unit.
/// <shields start="600" max="600" regenRate="2" delay="10"/>
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Shields {
    #[serde(rename = "@start")]
    pub start: u32,
    #[serde(rename = "@max")]
    pub max: u32,
    #[serde(rename = "@regenRate")]
    pub regen_rate: f32,
    #[serde(rename = "@delay", default)]
    pub delay: Option<f32>,
}

/// The Armor of a unit.
/// <armor start="1" max="1"/>
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Armor {
    #[serde(rename = "@start")]
    pub start: u32,
    #[serde(rename = "@max")]
    pub max: u32,
}

/// A short reference to a unit, rather than a full VersionedBalanceUnit reference (because
/// recursive ugh.)
/// <unit id="CyberneticsCore" name="679"/>
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename = "unit")]
pub struct UnitShortRef {
    #[serde(rename = "@id")]
    pub id: String,
    #[serde(rename = "@name")]
    pub name: String,
}

/// A longer reference to a unit, rather than a full VersionedBalanceUnit reference (because
/// recursive ugh.)
/// <unit id="CyberneticsCore" name="679">
/// <meta name="736" icon="btn-unit-protoss-phoenix" hotkey="737" type="command" tooltip="738"/>
/// <cost minerals="150" vespene="100" supply="2" time="35"/>
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename = "unit")]
pub struct UnitRef {
    #[serde(rename = "@id")]
    pub id: String,
    #[serde(rename = "@name")]
    pub name: Option<String>,
    // This may be a reference to a unit that is being trained by an ability "id"
    #[serde(rename = "@ability")]
    pub ability_id: Option<u16>,
    // This may be a reference to a command index within the ability
    #[serde(rename = "@index")]
    pub command_index: Option<i64>,
    pub meta: Option<Meta>,
    pub cost: Option<Cost>,
    #[serde(default)]
    pub requires: RequiresTag,
}

/// The Cost of a unit.
/// <cost minerals="150" vespene="150" time="60"/>
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Cost {
    #[serde(rename = "@minerals")]
    pub minerals: Option<u32>,
    #[serde(rename = "@vespene")]
    pub vespene: Option<u32>,
    #[serde(rename = "@supply")]
    pub supply: Option<f32>,
    #[serde(rename = "@time")]
    pub time: Option<f32>,
    #[serde(rename = "@energy")]
    pub energy: Option<u32>,
    #[serde(rename = "@cooldown")]
    pub cooldown: Option<f32>,
    #[serde(rename = "@life")]
    pub life: Option<u32>,
}

/// The Movement of a unit.
/// <movement type="Ground" speed="2.9531" acceleration="1000" deceleration="0" turnRate="999.8437"/>
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Movement {
    #[serde(rename = "@type")]
    pub kind: String,
    #[serde(rename = "@speed")]
    pub speed: f32,
    #[serde(rename = "@acceleration")]
    pub acceleration: f32,
    #[serde(rename = "@deceleration")]
    pub deceleration: f32,
    #[serde(rename = "@turnRate", default)]
    pub turn_rate: Option<f32>,
}

/// The ShieldArmor of a unit.
/// <shieldArmor start="0" max="3"/>
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename = "shieldArmor")]
pub struct ShieldArmor {
    #[serde(rename = "@start")]
    pub start: u32,
    #[serde(rename = "@max")]
    pub max: u32,
}

/// The Score of a unit.
/// <score build="300" kill="300"/>
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Score {
    #[serde(rename = "@build")]
    pub build: Option<u32>,
    #[serde(rename = "@kill")]
    pub kill: Option<u32>,
}

/// Miscellaneous properties of a unit.
/// <misc radius="1.75" cargoSize="0" footprint="Footprint3x3Contour" sightRadius="9" supply="0"/>
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Misc {
    #[serde(rename = "@radius")]
    pub radius: Option<f32>,
    #[serde(rename = "@cargoSize")]
    pub cargo_size: Option<u32>,
    #[serde(rename = "@footprint")]
    pub footprint: Option<String>,
    #[serde(rename = "@sightRadius")]
    pub sight_radius: Option<f32>,
    #[serde(rename = "@supply")]
    pub supply: Option<f32>,
    #[serde(rename = "@range")]
    pub range: Option<f32>,
    #[serde(rename = "@speed")]
    pub speed: Option<f32>,
    #[serde(rename = "@targets")]
    pub targets: Option<String>,
    #[serde(rename = "@count")]
    pub count: Option<u32>,
    #[serde(rename = "@cargoCapacity")]
    pub cargo_capacity: Option<u32>,
}

/// The Producer of a unit.
/// <producer id="Probe" name="438"/>
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Producer {
    #[serde(rename = "@id")]
    pub id: String,
    #[serde(rename = "@name")]
    pub name: String,
}

/// A bonus effect of a Weapon.
/// <bonus damage="10" max="10" type="Armored"/>
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EffectBonus {
    #[serde(rename = "@damage")]
    pub damage: u32,
    #[serde(rename = "@max")]
    pub max: u32,
    #[serde(rename = "@type")]
    pub kind: Option<String>,
}

/// The area of an effect
/// <area radius="2" fraction="0.33"/>
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EffectArea {
    #[serde(rename = "@radius")]
    pub radius: f32,
    #[serde(rename = "@fraction")]
    pub fraction: Option<f32>,
}

/// An Effect of a Weapon.
/// <effect id="ParticleBeam" index="535" radius="-1" damage="5" max="5" death="Normal" kind="Melee"/>
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Effect {
    #[serde(rename = "@id")]
    pub id: String,
    #[serde(rename = "@index")]
    pub index: u32,
    #[serde(rename = "@radius")]
    pub radius: f32,
    #[serde(rename = "@damage")]
    pub damage: u32,
    #[serde(rename = "@max")]
    pub max: u32,
    #[serde(rename = "@death")]
    pub death: String,
    #[serde(rename = "@kind")]
    pub kind: String,
    pub bonus: Option<EffectBonus>,
}

/// A Weapon of a unit.
/// <weapon id="ParticleBeam" index="16">
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Weapon {
    #[serde(rename = "@id")]
    pub id: String,
    #[serde(rename = "@index")]
    pub index: u32,
    pub meta: Meta,
    pub misc: Option<Misc>,
    pub score: Option<Effect>,
}

/// An Attribute of a unit.
/// <attribute type="Armored"/>
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Attribute {
    #[serde(rename = "@type")]
    pub kind: Option<String>,
}

/// A Command of an Ability.
/// <command id="Cancel" index="0">
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Command {
    /// The "passive" command has no id.
    #[serde(rename = "@id", default)]
    pub id: Option<String>,
    /// i64 matches the events in the SC2Replayfiles.
    #[serde(rename = "@index")]
    pub index: i64,
    pub meta: Meta,
}

/// An Ability of a unit.
/// <ability id="BuildInProgress" index="129">
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Ability {
    #[serde(rename = "@id")]
    pub id: String,
    /// u16 to match the event type in the SC2Replay files.
    #[serde(rename = "@index")]
    pub index: Option<u16>,
    #[serde(rename = "command", default)]
    pub commands: Vec<Command>,
}

/// A short reference to an Upgrade.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename = "upgrade")]
pub struct UpgradeRef {
    #[serde(rename = "@id", default)]
    pub id: String,
    #[serde(rename = "@name")]
    pub name: String,
}

/// A requirement for an UpgradeLevel.
/// May be a unit and a previous upgrade for example.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UpgradeLevelRequirement {
    #[serde(rename = "unit", default)]
    pub unit: Vec<UnitShortRef>,
    #[serde(rename = "upgrade", default)]
    pub upgrade: Vec<UpgradeRef>,
}

/// A level of an Upgrade.
/// <level id="ProtossShieldsLevel1" index="48">
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename = "level")]
pub struct UpgradeLevel {
    #[serde(rename = "@id", default)]
    pub id: String,
    #[serde(rename = "@index", default)]
    pub index: u32,
    pub meta: Meta,
    #[serde(default)]
    pub requires: Vec<UpgradeLevelRequirement>,
    pub cost: Cost,
}

/// An Upgrade of a unit.
/// <upgrade id="ProtossShields">
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Upgrade {
    #[serde(rename = "@id")]
    pub id: String,
    #[serde(rename = "level", default)]
    pub levels: Vec<UpgradeLevel>,
}

/// A building may start an upgrade.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResearchUpgrade {
    #[serde(rename = "@id")]
    pub id: String,
    #[serde(rename = "@ability")]
    pub ability: u16,
    #[serde(rename = "@index")]
    pub index: i64,
    pub meta: Meta,
    pub cost: Cost,
    // we can add here the <requires> that could have a <unit (i.e. a Hatchenry) and an <upgrade>,
    // i.e. previous level.
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename = "researches")]
pub struct ResearchesTag {
    #[serde(rename = "upgrade", default)]
    pub inner: Vec<ResearchUpgrade>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename = "requires")]
pub struct RequiresTag {
    #[serde(rename = "unit", default)]
    pub inner: Vec<UnitShortRef>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename = "attributes")]
pub struct AttributesTag {
    #[serde(rename = "attribute", default)]
    pub inner: Vec<Attribute>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename = "strengths")]
pub struct StrengthsTag {
    #[serde(rename = "unit", default)]
    pub inner: Vec<UnitShortRef>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename = "weaknesses")]
pub struct WeaknessesTag {
    #[serde(rename = "unit", default)]
    pub inner: Vec<UnitShortRef>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename = "weapons")]
pub struct WeaponsTag {
    #[serde(rename = "weapon", default)]
    pub inner: Vec<Weapon>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename = "abilities")]
pub struct AbilitiesTag {
    #[serde(rename = "ability", default)]
    pub inner: Vec<Ability>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename = "trains")]
pub struct TrainsTag {
    #[serde(rename = "unit", default)]
    pub inner: Vec<UnitRef>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename = "builds")]
pub struct BuildsTag {
    #[serde(rename = "unit", default)]
    pub inner: Vec<UnitRef>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename = "upgrades")]
pub struct UpgradesTag {
    #[serde(rename = "upgrade", default)]
    pub inner: Vec<Upgrade>,
}

/// A <unit> definition that belongs to a specific protocol version.
/// They may be compatible with other versions but no idea yet, I wish we could find a dump of
/// previous versions or that we could get the Starcrat II Editor to load previous versions of the
/// games.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename = "unit")]
pub struct VersionedBalanceUnit {
    #[serde(rename = "@id")]
    pub id: String,
    #[serde(skip)]
    pub version: u32,
    pub meta: Meta,
    pub life: Life,
    #[serde(default)]
    pub shields: Option<Shields>,
    pub armor: Armor,
    #[serde(rename = "shieldArmor")]
    pub shield_armor: ShieldArmor,
    #[serde(default)]
    pub requires: RequiresTag,
    pub cost: Option<Cost>,
    #[serde(default)]
    pub researches: Option<ResearchesTag>,
    #[serde(default)]
    pub movement: Option<Movement>,
    pub score: Score,
    pub misc: Misc,
    #[serde(default)]
    pub producer: Option<Producer>,
    #[serde(default)]
    pub attributes: AttributesTag,
    #[serde(default)]
    pub strengths: StrengthsTag,
    #[serde(default)]
    pub weaknesses: WeaknessesTag,
    #[serde(default)]
    pub weapons: WeaponsTag,
    #[serde(default)]
    pub abilities: AbilitiesTag,
    #[serde(default)]
    pub builds: BuildsTag,
    #[serde(default)]
    pub trains: TrainsTag,
    #[serde(default)]
    pub upgrades: UpgradesTag,
}

pub type MultiVersionedBalanceUnits = HashMap<(u32, String), VersionedBalanceUnit>;
pub type VersionedBalanceUnits = HashMap<String, VersionedBalanceUnit>;

pub fn get_indexed_ability_command_name(
    balance_data_units: &VersionedBalanceUnits,
    unit_name: &str,
    ability_id: u16,
    command_index: i64,
) -> String {
    if let Some(balance_unit) = balance_data_units.get(unit_name) {
        for ability in &balance_unit.abilities.inner {
            match ability.index {
                None => continue,
                Some(idx) if idx != ability_id => continue,
                Some(_) => {
                    for command in &ability.commands {
                        if command_index == command.index {
                            return format!(
                                "{}.{}",
                                ability.id,
                                command.id.as_ref().unwrap_or(&"".to_string())
                            );
                        }
                    }
                }
            }
        }
        for train in &balance_unit.trains.inner {
            if let (Some(train_ability_id), Some(train_command_index)) =
                (train.ability_id, train.command_index)
                && ability_id == train_ability_id
                && command_index == train_command_index
            {
                return format!("Train.{}", train.id);
            }
        }
        for build in &balance_unit.builds.inner {
            if let (Some(build_ability_id), Some(build_command_index)) =
                (build.ability_id, build.command_index)
                && ability_id == build_ability_id
                && command_index == build_command_index
            {
                return format!("Build.{}", build.id);
            }
        }
        if let Some(balance_unit_researches) = &balance_unit.researches {
            for research in &balance_unit_researches.inner {
                if ability_id == research.ability && command_index == research.index {
                    return format!("Research.{}", research.id);
                }
            }
        }
        tracing::warn!(
            "Unable to correlate ability[{ability_id}:{command_index}] for {:?}",
            balance_unit
        );
    } else {
        tracing::warn!(
            "Balance ability[{ability_id}:{command_index}] not found for unit {}",
            unit_name
        );
    }
    Default::default()
}