Skip to content

Commit 5d021aa

Browse files
authored
Use Offset from protocol spec when generating code (#16)
Previously unused property. Required for proper (de)serialization of MapSign entities.
1 parent a011b87 commit 5d021aa

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Core library for writing Endless Online applications using the Go programming la
99
The library may be referenced in a project via go get:
1010

1111
```
12-
go get github.com/ethanmoffat/eolib-go@v2.0.0
12+
go get github.com/ethanmoffat/eolib-go@v2.0.1
1313
```
1414

1515
### Sample code

internal/codegen/struct.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package codegen
22

33
import (
44
"fmt"
5+
"math"
56
"path"
67
"strconv"
78
"unicode"
@@ -743,6 +744,16 @@ func getSerializeForInstruction(instruction xml.ProtocolInstruction, methodType
743744
instructionCode = jen.Int().Call(instructionCode)
744745
}
745746

747+
if instruction.Offset != nil {
748+
var op string
749+
if *instruction.Offset < 0 {
750+
op = "+"
751+
} else {
752+
op = "-"
753+
}
754+
instructionCode = instructionCode.Op(op).Lit(int(math.Abs(float64(*instruction.Offset))))
755+
}
756+
746757
serializeCode := jen.If(
747758
jen.Id("err").Op("=").Id("writer").Dot("Add"+methodType.String()).Call(
748759
instructionCode,
@@ -792,6 +803,15 @@ func getDeserializeForInstruction(instruction xml.ProtocolInstruction, methodTyp
792803
}
793804

794805
readerGetCode := jen.Id("reader").Dot("Get" + methodType.String()).Call(lengthExpr)
806+
if instruction.Offset != nil {
807+
var op string
808+
if *instruction.Offset < 0 {
809+
op = "-"
810+
} else {
811+
op = "+"
812+
}
813+
readerGetCode = readerGetCode.Op(op).Lit(int(math.Abs(float64(*instruction.Offset))))
814+
}
795815

796816
var retCodes []jen.Code
797817
var assignRHS, assignLHS *jen.Statement

pkg/eolib/protocol/map/structs_generated.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ func (s *MapSign) Serialize(writer *data.EoWriter) (err error) {
225225
return
226226
}
227227
// StringDataLength : length : short
228-
if err = writer.AddShort(s.StringDataLength); err != nil {
228+
if err = writer.AddShort(s.StringDataLength + 1); err != nil {
229229
return
230230
}
231231
// StringData : field : encoded_string
@@ -248,7 +248,7 @@ func (s *MapSign) Deserialize(reader *data.EoReader) (err error) {
248248
return
249249
}
250250
// StringDataLength : length : short
251-
s.StringDataLength = reader.GetShort()
251+
s.StringDataLength = reader.GetShort() - 1
252252
// StringData : field : encoded_string
253253
if s.StringData, err = reader.GetFixedEncodedString(s.StringDataLength); err != nil {
254254
return

0 commit comments

Comments
 (0)