From de1c0ebe5dc2e1e1d6f71ec19b8d21a775c0f97d Mon Sep 17 00:00:00 2001 From: "M. Sz" Date: Sun, 28 Feb 2021 12:24:19 +0100 Subject: [PATCH 1/5] hotfix: string table: fixed bug, when sometimes encoded end decoded table wasn't the same --- d2common/d2fileformats/d2tbl/text_dictionary.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/d2common/d2fileformats/d2tbl/text_dictionary.go b/d2common/d2fileformats/d2tbl/text_dictionary.go index b0b8e51b..35540073 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) @@ -227,7 +233,8 @@ 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] for _, i := range key { sw.PushBytes(byte(i)) } From 287fb2bf4dd2153e43b348346a7a7d59771659ae Mon Sep 17 00:00:00 2001 From: "M. Sz" Date: Sun, 28 Feb 2021 14:14:47 +0100 Subject: [PATCH 2/5] hotfix: lintfix --- d2common/d2fileformats/d2tbl/text_dictionary.go | 1 + d2common/d2fileformats/d2tbl/text_dictionary_test.go | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/d2common/d2fileformats/d2tbl/text_dictionary.go b/d2common/d2fileformats/d2tbl/text_dictionary.go index 35540073..863b92e8 100644 --- a/d2common/d2fileformats/d2tbl/text_dictionary.go +++ b/d2common/d2fileformats/d2tbl/text_dictionary.go @@ -235,6 +235,7 @@ func (td *TextDictionary) Marshal() []byte { // data stream: put all data in appropriate order for _, key := range keys { value := (*td)[key] + 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..20267803 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,7 @@ 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 { From 4104d9d9aebbe2bb5487810d50756551d7d24f76 Mon Sep 17 00:00:00 2001 From: "M. Sz" Date: Sun, 28 Feb 2021 16:29:05 +0100 Subject: [PATCH 3/5] text dictionary: added support for non-name (#??) strings --- d2common/d2fileformats/d2tbl/text_dictionary.go | 12 +++++++++++- d2common/d2fileformats/d2tbl/text_dictionary_test.go | 7 +++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/d2common/d2fileformats/d2tbl/text_dictionary.go b/d2common/d2fileformats/d2tbl/text_dictionary.go index 863b92e8..a31c67c6 100644 --- a/d2common/d2fileformats/d2tbl/text_dictionary.go +++ b/d2common/d2fileformats/d2tbl/text_dictionary.go @@ -224,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 @@ -236,6 +242,10 @@ func (td *TextDictionary) Marshal() []byte { 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 20267803..f92824c9 100644 --- a/d2common/d2fileformats/d2tbl/text_dictionary_test.go +++ b/d2common/d2fileformats/d2tbl/text_dictionary_test.go @@ -6,8 +6,11 @@ import ( func exampleData() *TextDictionary { result := &TextDictionary{ - "abc": "def", - "someStr": "Some long string", + "abc": "def", + "someStr": "Some long string", + // #2 is non-named (X: OK) + // so 2 is an index in map + "#2": "OK", "teststring": "TeStxwsas123 long strin122*8:wq", } From 3b41f9e89bee425b191def30602ce5c259a553e5 Mon Sep 17 00:00:00 2001 From: "M. Sz" Date: Sun, 28 Feb 2021 20:31:44 +0100 Subject: [PATCH 4/5] text dictionary: removed #2 string from test --- d2common/d2fileformats/d2tbl/text_dictionary_test.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/d2common/d2fileformats/d2tbl/text_dictionary_test.go b/d2common/d2fileformats/d2tbl/text_dictionary_test.go index f92824c9..142ff14c 100644 --- a/d2common/d2fileformats/d2tbl/text_dictionary_test.go +++ b/d2common/d2fileformats/d2tbl/text_dictionary_test.go @@ -1,16 +1,15 @@ package d2tbl import ( + "fmt" + "testing" ) func exampleData() *TextDictionary { result := &TextDictionary{ - "abc": "def", - "someStr": "Some long string", - // #2 is non-named (X: OK) - // so 2 is an index in map - "#2": "OK", + "abc": "def", + "someStr": "Some long string", "teststring": "TeStxwsas123 long strin122*8:wq", } @@ -26,6 +25,8 @@ func TestTBL_Marshal(t *testing.T) { t.Error(err) } + fmt.Println(newTbl) + for key, value := range *tbl { newValue, ok := newTbl[key] From b43da8f083690b32aab89b43ac522dfeec0ae7e4 Mon Sep 17 00:00:00 2001 From: "M. Sz" Date: Sun, 28 Feb 2021 20:35:35 +0100 Subject: [PATCH 5/5] text dictionary: added no-named string check as separated test function --- .../d2tbl/text_dictionary_test.go | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/d2common/d2fileformats/d2tbl/text_dictionary_test.go b/d2common/d2fileformats/d2tbl/text_dictionary_test.go index 142ff14c..aea1f7d2 100644 --- a/d2common/d2fileformats/d2tbl/text_dictionary_test.go +++ b/d2common/d2fileformats/d2tbl/text_dictionary_test.go @@ -1,8 +1,6 @@ package d2tbl import ( - "fmt" - "testing" ) @@ -25,7 +23,30 @@ func TestTBL_Marshal(t *testing.T) { t.Error(err) } - fmt.Println(newTbl) + for key, value := range *tbl { + newValue, ok := newTbl[key] + + if !ok { + 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]