change pk.Ary API
This commit is contained in:
@ -62,7 +62,7 @@ func (p *Player) handleLoginPacket(packet pk.Packet) error {
|
||||
(*pk.Boolean)(&p.Hardcore),
|
||||
(*pk.UnsignedByte)(&p.Gamemode),
|
||||
(*pk.Byte)(&p.PrevGamemode),
|
||||
pk.Ary[pk.VarInt, *pk.VarInt]{Ary: &WorldNames},
|
||||
pk.Array(&WorldNames),
|
||||
pk.NBT(&p.WorldInfo.DimensionCodec),
|
||||
pk.NBT(&p.WorldInfo.Dimension),
|
||||
(*pk.Identifier)(&p.WorldName),
|
||||
|
@ -102,9 +102,7 @@ func (m *Manager) onSetContentPacket(p pk.Packet) error {
|
||||
if err := p.Scan(
|
||||
&ContainerID,
|
||||
&StateID,
|
||||
pk.Ary[pk.VarInt, *pk.VarInt]{
|
||||
Ary: &SlotData,
|
||||
},
|
||||
pk.Array(&SlotData),
|
||||
&CarriedItem,
|
||||
); err != nil {
|
||||
return Error{err}
|
||||
|
@ -126,9 +126,7 @@ func ExamplePacket_Scan_joinGame() {
|
||||
&Hardcore,
|
||||
&Gamemode,
|
||||
&PreGamemode,
|
||||
pk.Ary[pk.VarInt, *pk.VarInt]{
|
||||
Ary: &WorldNames,
|
||||
},
|
||||
pk.Array(&WorldNames),
|
||||
pk.NBT(&DimensionCodec),
|
||||
pk.NBT(&Dimension),
|
||||
&WorldName,
|
||||
|
@ -18,21 +18,17 @@ import (
|
||||
// So it's allowed to directly set an integer type Len, but not a pointer.
|
||||
//
|
||||
// Note that Ary DO read or write the Len. You aren't need to do so by your self.
|
||||
type Ary[T VarInt | VarLong | Byte | UnsignedByte | Short | UnsignedShort | Int | Long, L interface {
|
||||
*T
|
||||
ReadFrom(r io.Reader) (n int64, err error)
|
||||
WriteTo(w io.Writer) (n int64, err error)
|
||||
}] struct {
|
||||
type Ary[T VarInt | VarLong | Byte | UnsignedByte | Short | UnsignedShort | Int | Long] struct {
|
||||
Ary interface{} // Slice or Pointer of Slice of FieldEncoder, FieldDecoder or both (Field)
|
||||
}
|
||||
|
||||
func (a Ary[T, L]) WriteTo(w io.Writer) (n int64, err error) {
|
||||
func (a Ary[T]) WriteTo(w io.Writer) (n int64, err error) {
|
||||
array := reflect.ValueOf(a.Ary)
|
||||
for array.Kind() == reflect.Ptr {
|
||||
array = array.Elem()
|
||||
}
|
||||
Len := T(array.Len())
|
||||
if nn, err := L(&Len).WriteTo(w); err != nil {
|
||||
if nn, err := any(&Len).(FieldEncoder).WriteTo(w); err != nil {
|
||||
return n, err
|
||||
} else {
|
||||
n += nn
|
||||
@ -48,9 +44,9 @@ func (a Ary[T, L]) WriteTo(w io.Writer) (n int64, err error) {
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (a Ary[T, L]) ReadFrom(r io.Reader) (n int64, err error) {
|
||||
func (a Ary[T]) ReadFrom(r io.Reader) (n int64, err error) {
|
||||
var Len T
|
||||
if nn, err := L(&Len).ReadFrom(r); err != nil {
|
||||
if nn, err := any(&Len).(FieldDecoder).ReadFrom(r); err != nil {
|
||||
return nn, err
|
||||
} else {
|
||||
n += nn
|
||||
@ -80,7 +76,7 @@ func (a Ary[T, L]) ReadFrom(r io.Reader) (n int64, err error) {
|
||||
}
|
||||
|
||||
func Array(ary interface{}) Field {
|
||||
return Ary[VarInt, *VarInt]{Ary: ary}
|
||||
return Ary[VarInt]{Ary: ary}
|
||||
}
|
||||
|
||||
type Opt struct {
|
||||
|
@ -13,7 +13,7 @@ func ExampleAry_WriteTo() {
|
||||
// The length is inferred from the length of Ary.
|
||||
pk.Marshal(
|
||||
0x00,
|
||||
pk.Ary[pk.VarInt, *pk.VarInt]{
|
||||
pk.Ary[pk.VarInt]{
|
||||
Ary: data,
|
||||
},
|
||||
)
|
||||
@ -24,7 +24,7 @@ func ExampleAry_ReadFrom() {
|
||||
|
||||
var p pk.Packet // = conn.ReadPacket()
|
||||
if err := p.Scan(
|
||||
pk.Ary[pk.VarInt, *pk.VarInt]{ // then decode Ary according to length
|
||||
pk.Ary[pk.VarInt]{ // then decode Ary according to length
|
||||
Ary: &data,
|
||||
},
|
||||
); err != nil {
|
||||
@ -39,7 +39,7 @@ func TestAry_ReadFrom(t *testing.T) {
|
||||
4, 'T', 'n', 'z', 'e',
|
||||
0,
|
||||
}
|
||||
var data = pk.Ary[pk.Int, *pk.Int]{Ary: &ary}
|
||||
var data = pk.Ary[pk.Int]{Ary: &ary}
|
||||
if _, err := data.ReadFrom(bytes.NewReader(bin)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -61,15 +61,15 @@ func TestAry_WriteTo(t *testing.T) {
|
||||
0x00, 0x00, 0x00, 0x03,
|
||||
}
|
||||
for _, item := range [...]pk.FieldEncoder{
|
||||
pk.Ary[pk.Int, *pk.Int]{Ary: []pk.Int{1, 2, 3}},
|
||||
pk.Ary[pk.Int, *pk.Int]{Ary: []pk.Int{1, 2, 3}},
|
||||
pk.Ary[pk.Long, *pk.Long]{Ary: []pk.Int{1, 2, 3}},
|
||||
pk.Ary[pk.VarInt, *pk.VarInt]{Ary: []pk.Int{1, 2, 3}},
|
||||
pk.Ary[pk.VarLong, *pk.VarLong]{Ary: []pk.Int{1, 2, 3}},
|
||||
pk.Ary[pk.Int, *pk.Int]{Ary: []pk.Int{1, 2, 3}},
|
||||
pk.Ary[pk.Long, *pk.Long]{Ary: []pk.Int{1, 2, 3}},
|
||||
pk.Ary[pk.VarInt, *pk.VarInt]{Ary: []pk.Int{1, 2, 3}},
|
||||
pk.Ary[pk.VarLong, *pk.VarLong]{Ary: []pk.Int{1, 2, 3}},
|
||||
pk.Ary[pk.Int]{Ary: []pk.Int{1, 2, 3}},
|
||||
pk.Ary[pk.Int]{Ary: []pk.Int{1, 2, 3}},
|
||||
pk.Ary[pk.Long]{Ary: []pk.Int{1, 2, 3}},
|
||||
pk.Ary[pk.VarInt]{Ary: []pk.Int{1, 2, 3}},
|
||||
pk.Ary[pk.VarLong]{Ary: []pk.Int{1, 2, 3}},
|
||||
pk.Ary[pk.Int]{Ary: []pk.Int{1, 2, 3}},
|
||||
pk.Ary[pk.Long]{Ary: []pk.Int{1, 2, 3}},
|
||||
pk.Ary[pk.VarInt]{Ary: []pk.Int{1, 2, 3}},
|
||||
pk.Ary[pk.VarLong]{Ary: []pk.Int{1, 2, 3}},
|
||||
} {
|
||||
_, err := item.WriteTo(&buf)
|
||||
if err != nil {
|
||||
@ -90,7 +90,7 @@ func TestAry_WriteTo_pointer(t *testing.T) {
|
||||
0x00, 0x00, 0x00, 0x02,
|
||||
0x00, 0x00, 0x00, 0x03,
|
||||
}
|
||||
data := pk.Ary[pk.Int, *pk.Int]{Ary: &[]pk.Int{1, 2, 3}}
|
||||
data := pk.Ary[pk.Int]{Ary: &[]pk.Int{1, 2, 3}}
|
||||
|
||||
_, err := data.WriteTo(&buf)
|
||||
if err != nil {
|
||||
@ -126,9 +126,7 @@ func ExampleOpt_ReadFrom() {
|
||||
}}
|
||||
if err := p2.Scan(
|
||||
&has,
|
||||
pk.Opt{
|
||||
Has: &has, Field: &data2,
|
||||
},
|
||||
pk.Opt{Has: &has, Field: &data2},
|
||||
); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -182,9 +180,7 @@ func ExampleTuple_ReadFrom() {
|
||||
pk.Opt{
|
||||
Has: &has,
|
||||
Field: pk.Tuple{
|
||||
pk.Ary[pk.Int, *pk.Int]{
|
||||
Ary: &ary,
|
||||
},
|
||||
pk.Ary[pk.Int]{Ary: &ary},
|
||||
},
|
||||
},
|
||||
); err != nil {
|
||||
|
Reference in New Issue
Block a user