diff --git a/d2common/d2fileformats/d2tbl/text_dictionary.go b/d2common/d2fileformats/d2tbl/text_dictionary.go index b0b8e51b..a31c67c6 100644 --- a/d2common/d2fileformats/d2tbl/text_dictionary.go +++ b/d2common/d2fileformats/d2tbl/text_dictionary.go @@ -185,7 +185,12 @@ func (td *TextDictionary) Marshal() []byte { sw.PushUint16(0) - sw.PushInt32(int32(len(*td))) + keys := make([]string, 0) + for key := range *td { + keys = append(keys, key) + } + + sw.PushInt32(int32(len(keys))) // version (always 0) sw.PushBytes(0) @@ -205,7 +210,8 @@ func (td *TextDictionary) Marshal() []byte { // dataPos is a position, when we're placing data stream dataPos := len(sw.GetBytes()) + 17*len(*td) - for key, value := range *td { + for _, key := range keys { + value := (*td)[key] // non-zero if record is used (for us, every record is used ;-) sw.PushBytes(1) @@ -218,7 +224,13 @@ func (td *TextDictionary) Marshal() []byte { sw.PushUint32(0) sw.PushUint32(uint32(dataPos)) - dataPos += len(key) + 1 + + if key[0] == '#' { + // 1 for X, and 1 for separator + dataPos += 2 + } else { + dataPos += len(key) + 1 + } sw.PushUint32(uint32(dataPos)) dataPos += len(value) + 1 @@ -227,7 +239,13 @@ func (td *TextDictionary) Marshal() []byte { } // data stream: put all data in appropriate order - for key, value := range *td { + for _, key := range keys { + value := (*td)[key] + + if key[0] == '#' { + key = "x" + } + for _, i := range key { sw.PushBytes(byte(i)) } diff --git a/d2common/d2fileformats/d2tbl/text_dictionary_test.go b/d2common/d2fileformats/d2tbl/text_dictionary_test.go index 26188cd5..aea1f7d2 100644 --- a/d2common/d2fileformats/d2tbl/text_dictionary_test.go +++ b/d2common/d2fileformats/d2tbl/text_dictionary_test.go @@ -8,7 +8,7 @@ func exampleData() *TextDictionary { result := &TextDictionary{ "abc": "def", "someStr": "Some long string", - "teststring": "TeSt", + "teststring": "TeStxwsas123 long strin122*8:wq", } return result @@ -27,7 +27,32 @@ func TestTBL_Marshal(t *testing.T) { newValue, ok := newTbl[key] if !ok { - t.Fatal("string wasn't encoded to table") + t.Fatalf("string %s wasn't encoded to table", key) + } + + if newValue != value { + t.Fatal("unexpected value set") + } + } +} + +func TestTBL_MarshalNoNameString(t *testing.T) { + tbl := &TextDictionary{ + "#0": "OKEY", + } + + data := tbl.Marshal() + + newTbl, err := LoadTextDictionary(data) + if err != nil { + t.Error(err) + } + + for key, value := range *tbl { + newValue, ok := newTbl[key] + + if !ok { + t.Fatalf("string %s wasn't encoded to table", key) } if newValue != value {