Merge branch 'master' into anim-data-encoder

This commit is contained in:
gravestench 2021-03-03 11:13:11 -08:00 committed by GitHub
commit 9a2d92198e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 6 deletions

View File

@ -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))
}

View File

@ -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 {