support TAG_List<TAG_Compound>

This commit is contained in:
Tnze
2021-05-27 00:22:26 +08:00
parent fa2ab9a5d5
commit d0c7a295a5
2 changed files with 24 additions and 3 deletions

View File

@ -105,8 +105,7 @@ func writeCompoundPayload(e *Encoder, d *decodeState) error {
func writeListOrArray(e *Encoder, d *decodeState, tagName string) error { func writeListOrArray(e *Encoder, d *decodeState, tagName string) error {
d.scanWhile(scanSkipSpace) d.scanWhile(scanSkipSpace)
if d.opcode == scanEndValue { // ']', empty TAG_List if d.opcode == scanEndValue { // ']', empty TAG_List
e.writeTag(TagList, tagName) e.writeListHeader(TagEnd, tagName, 0)
e.writeInt32(0)
return nil return nil
} }
@ -150,8 +149,8 @@ func writeListOrArray(e *Encoder, d *decodeState, tagName string) error {
if d.opcode != scanListValue { if d.opcode != scanListValue {
panic(phasePanicMsg) panic(phasePanicMsg)
} }
start = d.readIndex()
d.scanNext() d.scanNext()
start = d.readIndex()
d.scanWhile(scanContinue) d.scanWhile(scanContinue)
literal = d.data[start:d.readIndex()] literal = d.data[start:d.readIndex()]
} }
@ -159,6 +158,27 @@ func writeListOrArray(e *Encoder, d *decodeState, tagName string) error {
e.w.Write(buf.Bytes()) e.w.Write(buf.Bytes())
case scanBeginList: // TAG_List<TAG_List> case scanBeginList: // TAG_List<TAG_List>
case scanBeginCompound: // TAG_List<TAG_Compound> case scanBeginCompound: // TAG_List<TAG_Compound>
for {
d.scanWhile(scanSkipSpace)
if d.opcode != scanBeginCompound {
return errors.New("different TagType in List")
}
writeCompoundPayload(e2, d)
count++
// read ',' or ']'
if d.opcode == scanSkipSpace {
d.scanWhile(scanSkipSpace)
}
d.scanNext()
if d.opcode == scanEndValue {
break
}
if d.opcode != scanListValue {
panic(phasePanicMsg)
}
}
e.writeListHeader(TagCompound, tagName, count)
e.w.Write(buf.Bytes())
} }
return nil return nil
} }

View File

@ -32,6 +32,7 @@ func TestEncoder_WriteSNBT(t *testing.T) {
{`[1b,2b,3b]`, []byte{9, 0, 0, 1, 0, 0, 0, 3, 1, 2, 3}}, {`[1b,2b,3b]`, []byte{9, 0, 0, 1, 0, 0, 0, 3, 1, 2, 3}},
{`[a,"b",'c']`, []byte{9, 0, 0, 8, 0, 0, 0, 3, 0, 1, 'a', 0, 1, 'b', 0, 1, 'c'}}, {`[a,"b",'c']`, []byte{9, 0, 0, 8, 0, 0, 0, 3, 0, 1, 'a', 0, 1, 'b', 0, 1, 'c'}},
{`[{},{a:1b},{}]`, []byte{9, 0, 0, 10, 0, 0, 0, 3, 0, 1, 0, 1, 'a', 1, 0, 0}}, {`[{},{a:1b},{}]`, []byte{9, 0, 0, 10, 0, 0, 0, 3, 0, 1, 0, 1, 'a', 1, 0, 0}},
{`[ { } , { a : 1b } , { } ] `, []byte{9, 0, 0, 10, 0, 0, 0, 3, 0, 1, 0, 1, 'a', 1, 0, 0}},
{`[[],[]]`, []byte{9, 0, 0, 9, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0}}, {`[[],[]]`, []byte{9, 0, 0, 9, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0}},
} }
for i := range testCases { for i := range testCases {