Add pk.Option API
This commit is contained in:
32
bot/mcbot.go
32
bot/mcbot.go
@ -79,22 +79,20 @@ func (c *Client) join(ctx context.Context, d *mcnet.Dialer, addr string) error {
|
|||||||
}
|
}
|
||||||
// Login Start
|
// Login Start
|
||||||
c.KeyPair, err = user.GetOrFetchKeyPair(c.Auth.AsTk)
|
c.KeyPair, err = user.GetOrFetchKeyPair(c.Auth.AsTk)
|
||||||
HasSignature := err == nil
|
KeyPair := pk.Option[keyPair]{
|
||||||
|
Has: err == nil,
|
||||||
|
Val: keyPair(c.KeyPair),
|
||||||
|
}
|
||||||
c.UUID, err = uuid.Parse(c.Auth.UUID)
|
c.UUID, err = uuid.Parse(c.Auth.UUID)
|
||||||
HasPlayerUUID := err == nil
|
PlayerUUID := pk.Option[pk.UUID]{
|
||||||
|
Has: err == nil,
|
||||||
|
Val: pk.UUID(c.UUID),
|
||||||
|
}
|
||||||
err = c.Conn.WritePacket(pk.Marshal(
|
err = c.Conn.WritePacket(pk.Marshal(
|
||||||
packetid.LoginStart,
|
packetid.LoginStart,
|
||||||
pk.String(c.Auth.Name),
|
pk.String(c.Auth.Name),
|
||||||
pk.Boolean(HasSignature),
|
KeyPair,
|
||||||
pk.Opt{
|
PlayerUUID,
|
||||||
Has: HasSignature,
|
|
||||||
Field: keyPair(c.KeyPair),
|
|
||||||
},
|
|
||||||
pk.Boolean(HasPlayerUUID),
|
|
||||||
pk.Opt{
|
|
||||||
Has: HasPlayerUUID,
|
|
||||||
Field: pk.UUID(c.UUID),
|
|
||||||
},
|
|
||||||
))
|
))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return LoginErr{"login start", err}
|
return LoginErr{"login start", err}
|
||||||
@ -148,9 +146,10 @@ func (c *Client) join(ctx context.Context, d *mcnet.Dialer, addr string) error {
|
|||||||
return LoginErr{"Login Plugin", err}
|
return LoginErr{"Login Plugin", err}
|
||||||
}
|
}
|
||||||
|
|
||||||
handler, ok := c.LoginPlugin[string(channel)]
|
var PluginMessageData pk.Option[pk.PluginMessageData]
|
||||||
if ok {
|
if handler, ok := c.LoginPlugin[string(channel)]; ok {
|
||||||
data, err = handler(data)
|
PluginMessageData.Has = true
|
||||||
|
PluginMessageData.Val, err = handler(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return LoginErr{"Login Plugin", err}
|
return LoginErr{"Login Plugin", err}
|
||||||
}
|
}
|
||||||
@ -158,8 +157,7 @@ func (c *Client) join(ctx context.Context, d *mcnet.Dialer, addr string) error {
|
|||||||
|
|
||||||
if err := c.Conn.WritePacket(pk.Marshal(
|
if err := c.Conn.WritePacket(pk.Marshal(
|
||||||
packetid.LoginPluginResponse,
|
packetid.LoginPluginResponse,
|
||||||
msgid, pk.Boolean(ok),
|
msgid, PluginMessageData,
|
||||||
pk.Opt{Has: ok, Field: data},
|
|
||||||
)); err != nil {
|
)); err != nil {
|
||||||
return LoginErr{"login Plugin", err}
|
return LoginErr{"login Plugin", err}
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,7 @@
|
|||||||
package chat
|
package chat
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -113,6 +114,7 @@ func (m Message) MarshalJSON() ([]byte, error) {
|
|||||||
|
|
||||||
// UnmarshalJSON decode json to Message
|
// UnmarshalJSON decode json to Message
|
||||||
func (m *Message) UnmarshalJSON(raw []byte) (err error) {
|
func (m *Message) UnmarshalJSON(raw []byte) (err error) {
|
||||||
|
raw = bytes.TrimSpace(raw)
|
||||||
if len(raw) == 0 {
|
if len(raw) == 0 {
|
||||||
return io.EOF
|
return io.EOF
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,12 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ Field = (*Option[Field])(nil)
|
||||||
|
_ Field = (*Ary[VarInt])(nil)
|
||||||
|
_ Field = Tuple(nil)
|
||||||
|
)
|
||||||
|
|
||||||
// Ary is used to send or receive the packet field like "Array of X"
|
// Ary is used to send or receive the packet field like "Array of X"
|
||||||
// which has a count must be known from the context.
|
// which has a count must be known from the context.
|
||||||
//
|
//
|
||||||
@ -19,16 +25,16 @@ 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] struct {
|
type Ary[LEN VarInt | VarLong | Byte | UnsignedByte | Short | UnsignedShort | Int | Long] 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]) WriteTo(w io.Writer) (n int64, err error) {
|
func (a Ary[LEN]) 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 := LEN(array.Len())
|
||||||
if nn, err := any(&Len).(FieldEncoder).WriteTo(w); err != nil {
|
if nn, err := any(&Len).(FieldEncoder).WriteTo(w); err != nil {
|
||||||
return n, err
|
return n, err
|
||||||
} else {
|
} else {
|
||||||
@ -45,8 +51,8 @@ func (a Ary[T]) WriteTo(w io.Writer) (n int64, err error) {
|
|||||||
return n, nil
|
return n, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a Ary[T]) ReadFrom(r io.Reader) (n int64, err error) {
|
func (a Ary[LEN]) ReadFrom(r io.Reader) (n int64, err error) {
|
||||||
var Len T
|
var Len LEN
|
||||||
if nn, err := any(&Len).(FieldDecoder).ReadFrom(r); err != nil {
|
if nn, err := any(&Len).(FieldDecoder).ReadFrom(r); err != nil {
|
||||||
return nn, err
|
return nn, err
|
||||||
} else {
|
} else {
|
||||||
@ -133,7 +139,33 @@ func (o Opt) ReadFrom(r io.Reader) (int64, error) {
|
|||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type Tuple []interface{} // FieldEncoder, FieldDecoder or both (Field)
|
type Option[T any] struct {
|
||||||
|
Has Boolean
|
||||||
|
Val T
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o Option[T]) WriteTo(w io.Writer) (n int64, err error) {
|
||||||
|
n1, err := o.Has.WriteTo(w)
|
||||||
|
if err != nil || !o.Has {
|
||||||
|
return n1, err
|
||||||
|
}
|
||||||
|
n2, err := any(&o.Val).(FieldEncoder).WriteTo(w)
|
||||||
|
return n1 + n2, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *Option[T]) ReadFrom(r io.Reader) (n int64, err error) {
|
||||||
|
n1, err := o.Has.ReadFrom(r)
|
||||||
|
if err != nil || !o.Has {
|
||||||
|
return n1, err
|
||||||
|
}
|
||||||
|
// This lose performance, but current Golang doesn't provide any better solution.
|
||||||
|
// I hope Go support we declare type constraint like `Option[*T Field]` in the future
|
||||||
|
// and then we can prevent using any dynamic dispatch.
|
||||||
|
n2, err := any(&o.Val).(FieldDecoder).ReadFrom(r)
|
||||||
|
return n1 + n2, err
|
||||||
|
}
|
||||||
|
|
||||||
|
type Tuple []any // FieldEncoder, FieldDecoder or both (Field)
|
||||||
|
|
||||||
// WriteTo write Tuple to io.Writer, panic when any of filed don't implement FieldEncoder
|
// WriteTo write Tuple to io.Writer, panic when any of filed don't implement FieldEncoder
|
||||||
func (t Tuple) WriteTo(w io.Writer) (n int64, err error) {
|
func (t Tuple) WriteTo(w io.Writer) (n int64, err error) {
|
||||||
|
@ -138,7 +138,6 @@ func ExampleOpt_ReadFrom() {
|
|||||||
// WILL NOT BE READ, WILL NOT BE COVERED
|
// WILL NOT BE READ, WILL NOT BE COVERED
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleOpt_ReadFrom_func() {
|
|
||||||
// As an example, we define this packet as this:
|
// As an example, we define this packet as this:
|
||||||
// +------+-----------------+----------------------------------+
|
// +------+-----------------+----------------------------------+
|
||||||
// | Name | Type | Notes |
|
// | Name | Type | Notes |
|
||||||
@ -148,6 +147,7 @@ func ExampleOpt_ReadFrom_func() {
|
|||||||
// | User | Optional String | The player's name. |
|
// | User | Optional String | The player's name. |
|
||||||
// +------+-----------------+----------------------------------+
|
// +------+-----------------+----------------------------------+
|
||||||
// So we need a function to decide if the User field is present.
|
// So we need a function to decide if the User field is present.
|
||||||
|
func ExampleOpt_ReadFrom_func() {
|
||||||
var flag pk.Byte
|
var flag pk.Byte
|
||||||
var data pk.String
|
var data pk.String
|
||||||
p := pk.Packet{Data: []byte{
|
p := pk.Packet{Data: []byte{
|
||||||
@ -188,3 +188,38 @@ func ExampleTuple_ReadFrom() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// As an example, we define this packet as this:
|
||||||
|
// +------+-----------------+-----------------------------------+
|
||||||
|
// | Name | Type | Notes |
|
||||||
|
// +------+-----------------+-----------------------------------+
|
||||||
|
// | Has | Boolean | True if the following is present. |
|
||||||
|
// +------+-----------------+-----------------------------------+
|
||||||
|
// | User | Optional String | The player's name. |
|
||||||
|
// +------+-----------------+-----------------------------------+
|
||||||
|
// So we need a function to decide if the User field is present.
|
||||||
|
func ExampleOption_ReadFrom_func() {
|
||||||
|
p1 := pk.Packet{Data: []byte{
|
||||||
|
0x01, // pk.Boolean(true)
|
||||||
|
4, 'T', 'n', 'z', 'e', // pk.String("Tnze")
|
||||||
|
}}
|
||||||
|
p2 := pk.Packet{Data: []byte{
|
||||||
|
0x00, // pk.Boolean(false)
|
||||||
|
// empty
|
||||||
|
}}
|
||||||
|
|
||||||
|
var User1, User2 pk.Option[pk.String]
|
||||||
|
if err := p1.Scan(&User1); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if err := p2.Scan(&User2); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(User1.Has, User1.Val)
|
||||||
|
fmt.Println(User2.Has, User2.Val)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true Tnze
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
@ -118,7 +118,6 @@ func (k *KeepAlive) tickPlayer(c KeepAliveClient) {
|
|||||||
elem, ok := k.listIndex[c]
|
elem, ok := k.listIndex[c]
|
||||||
if !ok {
|
if !ok {
|
||||||
panic(errors.New("keepalive: fail to tick player: client not found"))
|
panic(errors.New("keepalive: fail to tick player: client not found"))
|
||||||
return
|
|
||||||
}
|
}
|
||||||
if elem.Prev() == nil {
|
if elem.Prev() == nil {
|
||||||
if !k.waitTimer.Stop() {
|
if !k.waitTimer.Stop() {
|
||||||
@ -156,7 +155,6 @@ func keepAliveSetTimer(l *list.List, timer *time.Timer, interval time.Duration)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
timer.Reset(interval)
|
timer.Reset(interval)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type keepAliveItem struct {
|
type keepAliveItem struct {
|
||||||
|
@ -64,21 +64,13 @@ func (d *MojangLoginHandler) AcceptLogin(conn *net.Conn, protocol int32) (name s
|
|||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
hasPubKey pk.Boolean
|
pubKey pk.Option[auth.PublicKey]
|
||||||
pubKey auth.PublicKey
|
profileUUID pk.Option[pk.UUID] // ignored
|
||||||
hasUUID pk.Boolean
|
|
||||||
profileUUID pk.UUID // ignored
|
|
||||||
)
|
)
|
||||||
err = p.Scan(
|
err = p.Scan(
|
||||||
(*pk.String)(&name), // decode username as pk.String
|
(*pk.String)(&name), // decode username as pk.String
|
||||||
&hasPubKey, pk.Opt{
|
&pubKey,
|
||||||
Has: &hasPubKey,
|
&profileUUID,
|
||||||
Field: &pubKey,
|
|
||||||
},
|
|
||||||
&hasUUID, pk.Opt{
|
|
||||||
Has: &hasUUID,
|
|
||||||
Field: &profileUUID,
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@ -86,12 +78,12 @@ func (d *MojangLoginHandler) AcceptLogin(conn *net.Conn, protocol int32) (name s
|
|||||||
|
|
||||||
// auth
|
// auth
|
||||||
if d.OnlineMode {
|
if d.OnlineMode {
|
||||||
if hasPubKey {
|
if pubKey.Has {
|
||||||
if !pubKey.Verify() {
|
if !pubKey.Val.Verify() {
|
||||||
err = LoginFailErr{reason: chat.TranslateMsg("multiplayer.disconnect.invalid_public_key_signature")}
|
err = LoginFailErr{reason: chat.TranslateMsg("multiplayer.disconnect.invalid_public_key_signature")}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
profilePubKey = &pubKey
|
profilePubKey = &pubKey.Val
|
||||||
} else if d.EnforceSecureProfile {
|
} else if d.EnforceSecureProfile {
|
||||||
err = LoginFailErr{reason: chat.TranslateMsg("multiplayer.disconnect.missing_public_key")}
|
err = LoginFailErr{reason: chat.TranslateMsg("multiplayer.disconnect.missing_public_key")}
|
||||||
return
|
return
|
||||||
@ -99,7 +91,7 @@ func (d *MojangLoginHandler) AcceptLogin(conn *net.Conn, protocol int32) (name s
|
|||||||
|
|
||||||
var resp *auth.Resp
|
var resp *auth.Resp
|
||||||
// Auth, Encrypt
|
// Auth, Encrypt
|
||||||
resp, err = auth.Encrypt(conn, name, pubKey.PubKey)
|
resp, err = auth.Encrypt(conn, name, pubKey.Val.PubKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user