@@ -38,48 +38,34 @@ func newBaseCommand(
3838// that can be used.
3939// =========================================================================
4040
41- func parseGameCommands (data * []byte , headerEndOffset int ) ([]RawGameCommand , error ) {
41+ func parseGameCommands (data * []byte , headerEndOffset int , commandCount int ) ([]RawGameCommand , error ) {
4242 offset := bytes .Index ((* data )[headerEndOffset :], FOOTER )
4343 // slog.Debug("Parsing command list", "offset", strconv.FormatInt(int64(headerEndOffset+offset), 16))
4444
4545 if offset == - 1 {
4646 return nil , FooterNotFoundError (offset )
4747 }
4848
49- firstFootEnd , err := findFooterOffset (data , headerEndOffset + offset )
50- if err != nil {
51- return nil , err
52- }
53- offset = firstFootEnd + 5
54- lastIndex := 1
49+ offset += headerEndOffset - 19
5550 commandList := make ([]RawGameCommand , 0 )
5651
57- for {
58- if offset == len (* data )- 1 {
59- // We've reached the end!
60- break
61- }
62- item , err := parseCommandList (data , offset , lastIndex )
52+ for i := 1 ; i <= commandCount ; i ++ {
53+ item , err := parseCommandList (data , offset , i )
6354 if err != nil {
6455 return commandList , err
6556 }
6657 // Add all the commands to the command list, flattening everything into a single list.
6758 commandList = append (commandList , item .commands ... )
68- if item .finalCommand {
69- // We've reached the end! Someone resigned.
70- break
71- }
72- lastIndex += 1
73- if item .entryIdx != lastIndex {
74- return commandList , fmt .Errorf ("entryIdx was not sequential, item.entryIdx=%v, lastIndex=%v" , item .entryIdx , lastIndex )
59+ if item .entryIdx != i {
60+ return commandList , fmt .Errorf ("entryIdx was not sequential, item.entryIdx=%v, lastIndex=%v" , item .entryIdx , i )
7561 }
7662 offset = item .offsetEnd
7763 }
7864
7965 return commandList , nil
8066}
8167
82- func findFooterOffset (data * []byte , offset int ) (int , error ) {
68+ func findFooterEndOffset (data * []byte , offset int ) (int , error ) {
8369 /*
8470 Each set of commands is followd by a "FOOTER" (footer is probably not the correct term) the demarcates the
8571 end of the command sequence and the beginning of the next. This function finds end of this footer and the
@@ -93,17 +79,21 @@ func findFooterOffset(data *[]byte, offset int) (int, error) {
9379 for i := 0 ; i < int (extraByteCount ); i ++ {
9480 extraByteNumbers [i ] = derefedData [offset + i ]
9581 }
82+ offset += int (extraByteCount )
9683 if extraByteCount > 0 {
9784 slog .Debug (fmt .Sprintf ("foot has %v extra bytes: %v" , extraByteCount , extraByteNumbers ))
9885 }
9986
10087 unk := derefedData [offset ]
101- if unk != 1 {
102- slog .Debug ("unk not equal to 1" , "unk" , unk )
103- return - 1 , UnkNotEqualTo1Error (offset )
88+ offset += 1
89+
90+ if unk == 0 {
91+ offset += 8
92+ } else if unk != 1 {
93+ slog .Debug ("unk not equal to 0 or 1" , "unk" , unk )
94+ return - 1 , UnkNotExpectedValueError (offset )
10495 }
10596
106- offset += 9
10797 oneFourthFooterLength := readUint16 (data , offset )
10898 offset += 4
10999 endOffset := offset + 4 * int (oneFourthFooterLength )
@@ -172,24 +162,7 @@ func parseCommandList(data *[]byte, offset int, lastCommandListIdx int) (Command
172162 }
173163 }
174164
175- // TODO: Remove this and modify this to work for more than 1v1 replays.
176- // Check if the last command is the resign command. Right now, the code panics because it cannot find a footer
177- // after the resign command is issued. I haven't tried running this on team games yet, but I imagine that
178- // it might work correctly. We'll need a smarter way to determine the end of the command stream.
179- for _ , cmd := range commands {
180- if cmd .CommandType () == 16 {
181- slog .Debug ("Resign command issued" , "cmd" , cmd )
182- // Resign command issued, return the command list
183- return CommandList {
184- - 1 ,
185- offset ,
186- true ,
187- commands ,
188- }, nil
189- }
190- }
191-
192- footerEndOffset , err := findFooterOffset (data , offset )
165+ footerEndOffset , err := findFooterEndOffset (data , offset )
193166 if err != nil {
194167 return CommandList {}, err
195168 }
@@ -209,7 +182,6 @@ func parseCommandList(data *[]byte, offset int, lastCommandListIdx int) (Command
209182 return CommandList {
210183 int (entryIdx ),
211184 offset ,
212- false ,
213185 commands ,
214186 }, nil
215187}
0 commit comments