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