Skip to content

Commit eee504c

Browse files
committed
Add WASM support for reading ShieldBattery data.
1 parent b44c256 commit eee504c

File tree

5 files changed

+71
-12
lines changed

5 files changed

+71
-12
lines changed

Cargo.lock

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

broodrep-wasm/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ js-sys = "0.3"
1818
serde = { version = "1.0", features = ["derive"] }
1919
serde-wasm-bindgen = "0.6"
2020
tsify = { version = "0.5", default-features = false, features = ["js"] }
21+
uuid = { version = "1.18", default-features = false, features = [
22+
"js",
23+
"serde",
24+
] }
2125
wasm-bindgen = "0.2"
2226

2327
[dependencies.web-sys]

broodrep-wasm/src/lib.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@ use js_sys::Uint8Array;
22
use serde::{Deserialize, Serialize};
33
use std::io::Cursor;
44
use tsify::Tsify;
5+
use uuid::Uuid;
56
use wasm_bindgen::prelude::*;
67

8+
#[wasm_bindgen(typescript_custom_section)]
9+
const TS_APPEND_CONTENT: &'static str = r#"
10+
export type Uuid = string;
11+
"#;
12+
713
/// Decompression configuration options. These settings help prevent zip bomb attacks and excessive
814
/// resource usage.
915
#[derive(Clone, Debug, Default, Tsify, Serialize, Deserialize)]
@@ -305,6 +311,33 @@ impl From<ReplaySection> for broodrep::ReplaySection {
305311
}
306312
}
307313

314+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Tsify, Serialize, Deserialize)]
315+
#[serde(rename_all = "camelCase")]
316+
#[tsify(into_wasm_abi)]
317+
pub struct ShieldBatteryData {
318+
pub starcraft_exe_build: u32,
319+
pub shieldbattery_version: String,
320+
pub team_game_main_players: [u8; 4],
321+
pub starting_races: [u8; 12],
322+
pub game_id: Uuid,
323+
pub user_ids: [u32; 8],
324+
pub game_logic_version: Option<u16>,
325+
}
326+
327+
impl From<broodrep::ShieldBatteryData> for ShieldBatteryData {
328+
fn from(data: broodrep::ShieldBatteryData) -> Self {
329+
ShieldBatteryData {
330+
starcraft_exe_build: data.starcraft_exe_build(),
331+
shieldbattery_version: data.shieldbattery_version().to_string(),
332+
team_game_main_players: data.team_game_main_players(),
333+
starting_races: data.starting_races(),
334+
game_id: Uuid::from_u128(data.game_id()),
335+
user_ids: data.user_ids(),
336+
game_logic_version: data.game_logic_version(),
337+
}
338+
}
339+
}
340+
308341
/// A parsed StarCraft replay. Only the header will be parsed eagerly, other sections may be
309342
/// processed on demand.
310343
///
@@ -370,6 +403,16 @@ impl Replay {
370403
.get_raw_section(section_id.to_le_bytes().into())
371404
.map_err(|e| JsValue::from_str(&e.to_string()))
372405
}
406+
407+
/// Returns the parsed ShieldBattery section, or `undefined` if not present in the replay.
408+
#[wasm_bindgen(js_name = getShieldBatterySection)]
409+
pub fn get_shieldbattery_section(&mut self) -> Result<Option<ShieldBatteryData>, JsValue> {
410+
Ok(self
411+
.replay
412+
.get_shieldbattery_section()
413+
.map_err(|e| JsValue::from_str(&e.to_string()))?
414+
.map(Into::into))
415+
}
373416
}
374417

375418
/// Parse a StarCraft replay from a Uint8Array (synchronously).

broodrep/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,10 +1080,10 @@ mod tests {
10801080

10811081
assert_eq!(data.starcraft_exe_build(), 13515);
10821082
assert_eq!(data.shieldbattery_version(), "10.1.0");
1083-
assert_eq!(data.team_game_main_players(), &[0, 0, 0, 0]);
1084-
assert_eq!(data.starting_races(), &[2, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0]);
1083+
assert_eq!(data.team_game_main_players(), [0, 0, 0, 0]);
1084+
assert_eq!(data.starting_races(), [2, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0]);
10851085
assert_eq!(data.game_id(), 56542772156747381282200559102402795521);
1086-
assert_eq!(data.user_ids(), &[101, 112, 1, 113, 0, 0, 0, 0]);
1086+
assert_eq!(data.user_ids(), [101, 112, 1, 113, 0, 0, 0, 0]);
10871087
assert_eq!(data.game_logic_version(), Some(3));
10881088
}
10891089

broodrep/src/shieldbattery.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,18 @@ impl ShieldBatteryData {
5353
}
5454

5555
/// Which players were the "main" players in a team game (e.g. Team Melee).
56-
pub fn team_game_main_players(&self) -> &[u8; 4] {
56+
pub fn team_game_main_players(&self) -> [u8; 4] {
5757
match self {
58-
ShieldBatteryData::Version0(data) => &data.team_game_main_players,
59-
ShieldBatteryData::Version1(data, _) => &data.team_game_main_players,
58+
ShieldBatteryData::Version0(data) => data.team_game_main_players,
59+
ShieldBatteryData::Version1(data, _) => data.team_game_main_players,
6060
}
6161
}
6262

6363
/// The starting race for each player in the game.
64-
pub fn starting_races(&self) -> &[u8; 12] {
64+
pub fn starting_races(&self) -> [u8; 12] {
6565
match self {
66-
ShieldBatteryData::Version0(data) => &data.starting_races,
67-
ShieldBatteryData::Version1(data, _) => &data.starting_races,
66+
ShieldBatteryData::Version0(data) => data.starting_races,
67+
ShieldBatteryData::Version1(data, _) => data.starting_races,
6868
}
6969
}
7070

@@ -78,10 +78,10 @@ impl ShieldBatteryData {
7878

7979
/// The ShieldBattery user IDs of the players ingame, in the same order as the players in the
8080
/// replay header.
81-
pub fn user_ids(&self) -> &[u32; 8] {
81+
pub fn user_ids(&self) -> [u32; 8] {
8282
match self {
83-
ShieldBatteryData::Version0(data) => &data.user_ids,
84-
ShieldBatteryData::Version1(data, _) => &data.user_ids,
83+
ShieldBatteryData::Version0(data) => data.user_ids,
84+
ShieldBatteryData::Version1(data, _) => data.user_ids,
8585
}
8686
}
8787

0 commit comments

Comments
 (0)