Skip to content

Commit

Permalink
Add ShieldBattery struct and partly parse its section
Browse files Browse the repository at this point in the history
  • Loading branch information
icza committed Mar 22, 2023
1 parent 105f50e commit adea5d3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
3 changes: 3 additions & 0 deletions rep/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ type Replay struct {
// Computed contains data that is computed / derived from other parts of the
// replay.
Computed *Computed

// ShieldBattery holds info if game was played on ShieldBattery
ShieldBattery *ShieldBattery `json:",omitempty"`
}

// Compute creates and computes the Computed field.
Expand Down
10 changes: 10 additions & 0 deletions rep/shieldbattery.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// This file contains the types describing info parsed from the ShieldBattery custom section.

package rep

// ShieldBattery models the data parsed from the ShieldBattery custom section.
type ShieldBattery struct {
StarCraftExeBuild uint32
ShieldBatteryVersion string
GameID string
}
31 changes: 28 additions & 3 deletions repparser/repparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ func parse(dec repdecoder.Decoder, cfg Config) (*rep.Replay, error) {
return nil, fmt.Errorf("Decoder.NewSection() error: %w", err)
}

fmt.Println("icza ", sectionCounter)
var s *Section
var size int32
if sectionCounter < len(Sections) {
Expand Down Expand Up @@ -252,7 +251,6 @@ func parse(dec repdecoder.Decoder, cfg Config) (*rep.Replay, error) {
}

if s == nil {
fmt.Println("icza sectionID ", sectionID)
s = ModernSections[sectionID]
if s == nil {
// Unknown section, just skip it:
Expand Down Expand Up @@ -1038,7 +1036,34 @@ func parsePlayerColors(data []byte, r *rep.Replay, cfg Config) error {

// parseShieldBatterySection processes the ShieldBattery data.
func parseShieldBatterySection(data []byte, r *rep.Replay, cfg Config) error {
// TODO
// info source:
// https://github.com/ShieldBattery/ShieldBattery/blob/master/game/src/replay.rs#L62-L80
// https://github.com/ShieldBattery/ShieldBattery/blob/master/app/replays/parse-shieldbattery-replay.ts

if len(data) < 0x56 {
// 0x56 bytes is the size of SB's first version of the section.
return nil // Unknown format
}

bo := binary.LittleEndian // ByteOrder reader: little-endian

sb := new(rep.ShieldBattery)
r.ShieldBattery = sb

formatVersion := bo.Uint16(data)

sb.StarCraftExeBuild = bo.Uint32(data[0x01:])
sb.ShieldBatteryVersion, _ = cString(data[0x06:0x16])

// 0x16 - 0x1a: team_game_main_players
// 0x1a - 0x26: starting_races

gameID := data[0x26:0x36]
sb.GameID = fmt.Sprintf("%x-%x-%x-%x-%x", gameID[:4], gameID[4:6], gameID[6:8], gameID[8:10], gameID[10:])

if formatVersion >= 0x01 {
// 0x56 - 0x58: game_logic_version
}

return nil
}
Expand Down

0 comments on commit adea5d3

Please sign in to comment.