format repo with "gofumpt" tool

This commit is contained in:
Tnze
2022-11-26 20:37:57 +08:00
parent 7814e7b1ab
commit fad92fe364
61 changed files with 333 additions and 268 deletions

View File

@ -1,5 +1,4 @@
//From https://play.golang.org/p/LTbId4b6M2
// Package CFB8 is copied from https://play.golang.org/p/LTbId4b6M2
package CFB8
import "crypto/cipher"

View File

@ -213,8 +213,8 @@ func (c *Conn) WritePacket(p pk.Packet) error {
// SetCipher load the decode/encode stream to this Conn
func (c *Conn) SetCipher(ecoStream, decoStream cipher.Stream) {
//加密连接
c.Reader = cipher.StreamReader{ //Set receiver for AES
// 加密连接
c.Reader = cipher.StreamReader{ // Set receiver for AES
S: decoStream,
R: c.Socket,
}

View File

@ -37,6 +37,7 @@ func TestVarInt_WriteTo(t *testing.T) {
}
}
}
func TestVarInt_ReadFrom(t *testing.T) {
for i, v := range PackedVarInts {
var vi pk.VarInt
@ -51,7 +52,7 @@ func TestVarInt_ReadFrom(t *testing.T) {
func TestVarInt_ReadFrom_tooLongData(t *testing.T) {
var vi pk.VarInt
var data = []byte{0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01}
data := []byte{0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01}
if _, err := vi.ReadFrom(bytes.NewReader(data)); err != nil {
t.Logf("unpack \"% x\" error: %v", data, err)
} else {
@ -87,6 +88,7 @@ func TestVarLong_WriteTo(t *testing.T) {
}
}
}
func TestVarLong_ReadFrom(t *testing.T) {
for i, v := range PackedVarLongs {
var vi pk.VarLong

View File

@ -3,10 +3,11 @@ package packet
import (
"bytes"
"errors"
"github.com/google/uuid"
"io"
"math"
"github.com/google/uuid"
"github.com/Tnze/go-mc/nbt"
)
@ -23,62 +24,64 @@ type FieldEncoder io.WriterTo
type FieldDecoder io.ReaderFrom
type (
//Boolean of True is encoded as 0x01, false as 0x00.
// Boolean of True is encoded as 0x01, false as 0x00.
Boolean bool
//Byte is signed 8-bit integer, two's complement
// Byte is signed 8-bit integer, two's complement
Byte int8
//UnsignedByte is unsigned 8-bit integer
// UnsignedByte is unsigned 8-bit integer
UnsignedByte uint8
//Short is signed 16-bit integer, two's complement
// Short is signed 16-bit integer, two's complement
Short int16
//UnsignedShort is unsigned 16-bit integer
// UnsignedShort is unsigned 16-bit integer
UnsignedShort uint16
//Int is signed 32-bit integer, two's complement
// Int is signed 32-bit integer, two's complement
Int int32
//Long is signed 64-bit integer, two's complement
// Long is signed 64-bit integer, two's complement
Long int64
//A Float is a single-precision 32-bit IEEE 754 floating point number
// A Float is a single-precision 32-bit IEEE 754 floating point number
Float float32
//A Double is a double-precision 64-bit IEEE 754 floating point number
// A Double is a double-precision 64-bit IEEE 754 floating point number
Double float64
//String is sequence of Unicode scalar values
// String is sequence of Unicode scalar values
String string
//Chat is encoded as a String with max length of 32767.
// Chat is encoded as a String with max length of 32767.
// Deprecated: Use chat.Message
Chat = String
//Identifier is encoded as a String with max length of 32767.
// Identifier is encoded as a String with max length of 32767.
Identifier = String
//VarInt is variable-length data encoding a two's complement signed 32-bit integer
// VarInt is variable-length data encoding a two's complement signed 32-bit integer
VarInt int32
//VarLong is variable-length data encoding a two's complement signed 64-bit integer
// VarLong is variable-length data encoding a two's complement signed 64-bit integer
VarLong int64
//Position x as a 26-bit integer, followed by y as a 12-bit integer, followed by z as a 26-bit integer (all signed, two's complement)
// Position x as a 26-bit integer, followed by y as a 12-bit integer, followed by z as a 26-bit integer (all signed, two's complement)
Position struct {
X, Y, Z int
}
//Angle is rotation angle in steps of 1/256 of a full turn
// Angle is rotation angle in steps of 1/256 of a full turn
Angle Byte
//UUID encoded as an unsigned 128-bit integer
// UUID encoded as an unsigned 128-bit integer
UUID uuid.UUID
//ByteArray is []byte with prefix VarInt as length
// ByteArray is []byte with prefix VarInt as length
ByteArray []byte
//PluginMessageData is only used in LoginPlugin,and it will read all left bytes
// PluginMessageData is only used in LoginPlugin,and it will read all left bytes
PluginMessageData []byte
//BitSet represents Java's BitSet, a list of bits.
// BitSet represents Java's BitSet, a list of bits.
BitSet []int64
)
const MaxVarIntLen = 5
const MaxVarLongLen = 10
const (
MaxVarIntLen = 5
MaxVarLongLen = 10
)
func (b Boolean) WriteTo(w io.Writer) (int64, error) {
var v byte
@ -112,7 +115,7 @@ func (s String) WriteTo(w io.Writer) (int64, error) {
}
func (s *String) ReadFrom(r io.Reader) (n int64, err error) {
var l VarInt //String length
var l VarInt // String length
nn, err := l.ReadFrom(r)
if err != nil {
@ -245,7 +248,7 @@ func (l *Long) ReadFrom(r io.Reader) (n int64, err error) {
}
func (v VarInt) WriteTo(w io.Writer) (n int64, err error) {
var vi = make([]byte, 0, MaxVarIntLen)
vi := make([]byte, 0, MaxVarIntLen)
num := uint32(v)
for {
b := num & 0x7F
@ -282,7 +285,7 @@ func (v *VarInt) ReadFrom(r io.Reader) (n int64, err error) {
}
func (v VarLong) WriteTo(w io.Writer) (n int64, err error) {
var vi = make([]byte, 0, MaxVarLongLen)
vi := make([]byte, 0, MaxVarLongLen)
num := uint64(v)
for {
b := num & 0x7F
@ -340,7 +343,7 @@ func (p *Position) ReadFrom(r io.Reader) (n int64, err error) {
y := int(v & 0xFFF)
z := int(v << 26 >> 38)
//处理负数
// 处理负数
if x >= 1<<25 {
x -= 1 << 26
}

View File

@ -3,8 +3,9 @@ package packet_test
import (
"bytes"
"fmt"
pk "github.com/Tnze/go-mc/net/packet"
"testing"
pk "github.com/Tnze/go-mc/net/packet"
)
func ExampleAry_WriteTo() {
@ -34,12 +35,12 @@ func ExampleAry_ReadFrom() {
func TestAry_ReadFrom(t *testing.T) {
var ary []pk.String
var bin = []byte{
bin := []byte{
0, 0, 0, 2,
4, 'T', 'n', 'z', 'e',
0,
}
var data = pk.Ary[pk.Int]{Ary: &ary}
data := pk.Ary[pk.Int]{Ary: &ary}
if _, err := data.ReadFrom(bytes.NewReader(bin)); err != nil {
t.Fatal(err)
}

View File

@ -24,14 +24,14 @@ func DialRCON(addr string, password string) (client RCONClientConn, err error) {
return
}
//Login
// Login
err = c.WritePacket(c.ReqID, 3, password)
if err != nil {
err = fmt.Errorf("login fail: %v", err)
return
}
//Login resp
// Login resp
r, _, _, err := c.ReadPacket()
if err != nil {
err = fmt.Errorf("read login resp fail: %v", err)
@ -55,7 +55,7 @@ type RCONConn struct {
}
func (r *RCONConn) ReadPacket() (RequestID, Type int32, Payload string, err error) {
//read packet length
// read packet length
var Length int32
err = binary.Read(r, binary.LittleEndian, &Length)
if err != nil {
@ -63,7 +63,7 @@ func (r *RCONConn) ReadPacket() (RequestID, Type int32, Payload string, err erro
return
}
//check length
// check length
if Length < 4+4+0+2 {
err = errors.New("packet too short")
return
@ -73,7 +73,7 @@ func (r *RCONConn) ReadPacket() (RequestID, Type int32, Payload string, err erro
return
}
//read packet data
// read packet data
buf := make([]byte, Length)
err = binary.Read(r, binary.LittleEndian, &buf)
if err != nil {
@ -91,11 +91,11 @@ func (r *RCONConn) WritePacket(RequestID, Type int32, Payload string) error {
buf := new(bytes.Buffer)
for _, v := range []interface{}{
int32(4 + 4 + len(Payload) + 2), //Length
RequestID, //Request ID
Type, //Type
[]byte(Payload), //Payload
[]byte{0, 0}, //pad
int32(4 + 4 + len(Payload) + 2), // Length
RequestID, // Request ID
Type, // Type
[]byte(Payload), // Payload
[]byte{0, 0}, // pad
} {
err := binary.Write(buf, binary.LittleEndian, v)
if err != nil {
@ -136,7 +136,7 @@ func (r *RCONConn) AcceptLogin(password string) error {
r.ReqID = R
//Check packet type
// Check packet type
if T != 3 {
return fmt.Errorf("not a login packet: %d", T)
}
@ -165,7 +165,7 @@ func (r *RCONConn) AcceptCmd() (string, error) {
r.ReqID = R
//Check packet type
// Check packet type
if T != 2 {
return P, fmt.Errorf("not a command packet: %d", T)
}