github.com/15mga/kiwi@v0.0.2-0.20240324021231-b95d5c3ac751/util/bytes_buffer.go (about)

     1  package util
     2  
     3  import (
     4  	"encoding/gob"
     5  	"encoding/hex"
     6  	"io"
     7  	"math"
     8  	"sync"
     9  	"time"
    10  
    11  	"github.com/mitchellh/mapstructure"
    12  	"google.golang.org/protobuf/proto"
    13  )
    14  
    15  const (
    16  	_MinBytesCap = 16
    17  	_MaxBytesCap = 1 << 20
    18  )
    19  
    20  type _BT = uint8
    21  
    22  const (
    23  	Bool _BT = iota
    24  	Bools
    25  	Bytes
    26  	Uint8
    27  	Int8
    28  	Int8s
    29  	Uint16
    30  	Uint16s
    31  	Int16
    32  	Int16s
    33  	Int
    34  	Ints
    35  	Uint32
    36  	Uint32s
    37  	Int32
    38  	Int32s
    39  	Uint64
    40  	Uint64s
    41  	Int64
    42  	Int64s
    43  	Float32
    44  	Float32s
    45  	Float64
    46  	Float64s
    47  	Vector2
    48  	Vector2s
    49  	Vector3
    50  	Vector3s
    51  	Str
    52  	Strs
    53  	Time
    54  	Times
    55  	MInt32
    56  	MInt64
    57  	MStr
    58  	MAny
    59  	Uint16MUint16
    60  	Int32MInt32
    61  	Int32MInt64
    62  	Int64MInt32
    63  	Int64MInt64
    64  	AnyMAny
    65  )
    66  
    67  var (
    68  	_BytesPoolMap map[int]*sync.Pool
    69  	_Empty        []byte
    70  )
    71  
    72  func init() {
    73  	_BytesPoolMap = make(map[int]*sync.Pool)
    74  	_Empty = make([]byte, _MaxBytesCap)
    75  	for i := _MinBytesCap; i < _MaxBytesCap; i = i << 1 {
    76  		l := i
    77  		_BytesPoolMap[i] = &sync.Pool{
    78  			New: func() any {
    79  				return make([]byte, l)
    80  			},
    81  		}
    82  	}
    83  }
    84  
    85  func SpawnBytes() []byte {
    86  	return SpawnBytesWithLen(_MinBytesCap)
    87  }
    88  
    89  func SpawnBytesWithLen(l int) []byte {
    90  	//return make([]byte, l)
    91  	var c int
    92  	if l < _MinBytesCap {
    93  		c = _MinBytesCap
    94  	} else {
    95  		c = NextPowerOfTwo(l)
    96  		if c > _MaxBytesCap {
    97  			return make([]byte, l)
    98  		}
    99  	}
   100  	return _BytesPoolMap[c].Get().([]byte)
   101  }
   102  
   103  func RecycleBytes(bytes []byte) {
   104  	//return
   105  	c := cap(bytes)
   106  	if c < _MinBytesCap || c > _MaxBytesCap {
   107  		return
   108  	}
   109  	ll := NextPowerOfTwo(c)
   110  	if c < ll {
   111  		c = ll >> 1
   112  	}
   113  	_BytesPoolMap[c].Put(bytes[:c])
   114  }
   115  
   116  func CopyBytes(src []byte) []byte {
   117  	l := len(src)
   118  	dst := SpawnBytesWithLen(l)
   119  	copy(dst, src)
   120  	return dst[:l]
   121  }
   122  
   123  type ByteBuffer struct {
   124  	canRecycle bool
   125  	pos        int
   126  	len        int
   127  	cap        int
   128  	bytes      []byte
   129  }
   130  
   131  func (b *ByteBuffer) Reset() {
   132  	b.pos = 0
   133  }
   134  
   135  func (b *ByteBuffer) InitCap(c int) {
   136  	b.pos = 0
   137  	b.len = 0
   138  	b.bytes = SpawnBytesWithLen(c)
   139  	b.cap = len(b.bytes)
   140  	b.canRecycle = true
   141  }
   142  
   143  func (b *ByteBuffer) InitBytes(bytes []byte) {
   144  	b.pos = 0
   145  	b.len = len(bytes)
   146  	b.cap = b.len
   147  	b.bytes = bytes
   148  	b.canRecycle = false
   149  }
   150  
   151  func (b *ByteBuffer) Pos() int {
   152  	return b.pos
   153  }
   154  
   155  func (b *ByteBuffer) SetPos(v int) {
   156  	if v < b.len {
   157  		b.pos = v
   158  	} else {
   159  		b.pos = b.len
   160  	}
   161  }
   162  
   163  func (b *ByteBuffer) Available() int {
   164  	return b.len - b.pos
   165  }
   166  
   167  func (b *ByteBuffer) All() []byte {
   168  	return b.bytes[:b.len]
   169  }
   170  
   171  func (b *ByteBuffer) CopyBytes() ([]byte, int) {
   172  	return CopyBytes(b.bytes[:b.len]), b.len
   173  }
   174  
   175  func (b *ByteBuffer) Length() int {
   176  	return b.len
   177  }
   178  
   179  func (b *ByteBuffer) Cap() int {
   180  	return b.cap
   181  }
   182  
   183  func (b *ByteBuffer) WBool(v bool) {
   184  	if v {
   185  		b.WUint8(1)
   186  	} else {
   187  		b.WUint8(0)
   188  	}
   189  }
   190  
   191  func (b *ByteBuffer) WBools(v []bool) {
   192  	b.WUint16(uint16(len(v)))
   193  	for _, d := range v {
   194  		b.WBool(d)
   195  	}
   196  }
   197  
   198  func (b *ByteBuffer) tryGrow(c int) {
   199  	c, ok := NextCap(b.pos+c, b.cap, 2048)
   200  	if !ok {
   201  		return
   202  	}
   203  	b.cap = c
   204  	bytes := SpawnBytesWithLen(c)
   205  	copy(bytes, b.bytes[:b.pos])
   206  	RecycleBytes(b.bytes)
   207  	b.bytes = bytes
   208  }
   209  
   210  func (b *ByteBuffer) Write(v []byte) (int, error) {
   211  	l := len(v)
   212  	if v == nil || l == 0 {
   213  		return 0, nil
   214  	}
   215  	return b.write(l, v)
   216  }
   217  
   218  func (b *ByteBuffer) write(l int, v []byte) (int, error) {
   219  	c := b.pos + l
   220  	b.tryGrow(c)
   221  	l = b.pos
   222  	b.pos = c
   223  	b.len = c
   224  	return copy(b.bytes[l:], v), nil
   225  }
   226  
   227  func (b *ByteBuffer) WBytes(v []byte) {
   228  	b.WUint32(uint32(len(v)))
   229  	_, _ = b.Write(v)
   230  }
   231  
   232  func (b *ByteBuffer) WUint8(v uint8) {
   233  	c := b.pos + 1
   234  	b.tryGrow(c)
   235  	l := b.pos
   236  	b.pos = c
   237  	b.len = c
   238  	b.bytes[l] = v
   239  }
   240  
   241  func (b *ByteBuffer) WUint8s(v []uint8) {
   242  	b.WUint16(uint16(len(v)))
   243  	for _, d := range v {
   244  		b.WUint8(d)
   245  	}
   246  }
   247  
   248  func (b *ByteBuffer) WInt8(v int8) {
   249  	b.WUint8(uint8(v))
   250  }
   251  
   252  func (b *ByteBuffer) WInt8s(v []int8) {
   253  	b.WUint16(uint16(len(v)))
   254  	for _, d := range v {
   255  		b.WInt8(d)
   256  	}
   257  }
   258  
   259  func (b *ByteBuffer) WUint16(v uint16) {
   260  	_, _ = b.write(2, []byte{byte(v >> 8), byte(v)})
   261  }
   262  
   263  func (b *ByteBuffer) WUint16s(v []uint16) {
   264  	b.WUint16(uint16(len(v)))
   265  	for _, d := range v {
   266  		b.WUint16(d)
   267  	}
   268  }
   269  
   270  func (b *ByteBuffer) WInt16(v int16) {
   271  	b.WUint16(uint16(v))
   272  }
   273  
   274  func (b *ByteBuffer) WInt16s(v []int16) {
   275  	b.WUint16(uint16(len(v)))
   276  	for _, d := range v {
   277  		b.WInt16(d)
   278  	}
   279  }
   280  
   281  func (b *ByteBuffer) WInt(v int) {
   282  	_, _ = b.write(4, []byte{byte(v >> 24), byte(v >> 16), byte(v >> 8), byte(v)})
   283  }
   284  
   285  func (b *ByteBuffer) WInts(v []int) {
   286  	b.WUint16(uint16(len(v)))
   287  	for _, d := range v {
   288  		b.WInt(d)
   289  	}
   290  }
   291  
   292  func (b *ByteBuffer) WUint32(v uint32) {
   293  	_, _ = b.write(4, []byte{byte(v >> 24), byte(v >> 16), byte(v >> 8), byte(v)})
   294  }
   295  
   296  func (b *ByteBuffer) WUint32s(v []uint32) {
   297  	b.WUint16(uint16(len(v)))
   298  	for _, d := range v {
   299  		b.WUint32(d)
   300  	}
   301  }
   302  
   303  func (b *ByteBuffer) WInt32(v int32) {
   304  	b.WUint32(uint32(v))
   305  }
   306  
   307  func (b *ByteBuffer) WInt32s(v []int32) {
   308  	b.WUint16(uint16(len(v)))
   309  	for _, d := range v {
   310  		b.WInt32(d)
   311  	}
   312  }
   313  
   314  func (b *ByteBuffer) WUint64(v uint64) {
   315  	_, _ = b.write(8, []byte{byte(v >> 56), byte(v >> 48), byte(v >> 40), byte(v >> 32), byte(v >> 24), byte(v >> 16), byte(v >> 8), byte(v)})
   316  }
   317  
   318  func (b *ByteBuffer) WUint64s(v []uint64) {
   319  	b.WUint16(uint16(len(v)))
   320  	for _, d := range v {
   321  		b.WUint64(d)
   322  	}
   323  }
   324  
   325  func (b *ByteBuffer) WInt64(v int64) {
   326  	b.WUint64(uint64(v))
   327  }
   328  
   329  func (b *ByteBuffer) WInt64s(v []int64) {
   330  	b.WUint16(uint16(len(v)))
   331  	for _, d := range v {
   332  		b.WInt64(d)
   333  	}
   334  }
   335  
   336  func (b *ByteBuffer) WFloat32(v float32) {
   337  	b.WUint32(math.Float32bits(v))
   338  }
   339  
   340  func (b *ByteBuffer) WFloat32s(v []float32) {
   341  	b.WUint16(uint16(len(v)))
   342  	for _, d := range v {
   343  		b.WFloat32(d)
   344  	}
   345  }
   346  
   347  func (b *ByteBuffer) WFloat64(v float64) {
   348  	b.WUint64(math.Float64bits(v))
   349  }
   350  
   351  func (b *ByteBuffer) WFloat64s(v []float64) {
   352  	b.WUint16(uint16(len(v)))
   353  	for _, d := range v {
   354  		b.WFloat64(d)
   355  	}
   356  }
   357  
   358  func (b *ByteBuffer) WVec2(v Vec2) {
   359  	b.WFloat32(v.X)
   360  	b.WFloat32(v.Y)
   361  }
   362  
   363  func (b *ByteBuffer) WVec2s(v []Vec2) {
   364  	b.WUint16(uint16(len(v)))
   365  	for _, d := range v {
   366  		b.WVec2(d)
   367  	}
   368  }
   369  
   370  func (b *ByteBuffer) WVec3(v Vec3) {
   371  	b.WFloat32(v.X)
   372  	b.WFloat32(v.Y)
   373  	b.WFloat32(v.Z)
   374  }
   375  
   376  func (b *ByteBuffer) WVec3s(v []Vec3) {
   377  	b.WUint16(uint16(len(v)))
   378  	for _, d := range v {
   379  		b.WVec3(d)
   380  	}
   381  }
   382  
   383  func (b *ByteBuffer) WShortString(v string) {
   384  	l := len(v)
   385  	b.WUint8(uint8(l))
   386  	if l == 0 {
   387  		return
   388  	}
   389  	c := b.pos + l
   390  	b.tryGrow(c)
   391  	l = b.pos
   392  	b.pos = c
   393  	b.len = c
   394  	copy(b.bytes[l:], v)
   395  	return
   396  }
   397  
   398  func (b *ByteBuffer) WString(v string) {
   399  	l := len(v)
   400  	b.WUint16(uint16(l))
   401  	if l == 0 {
   402  		return
   403  	}
   404  	c := b.pos + l
   405  	b.tryGrow(c)
   406  	l = b.pos
   407  	b.pos = c
   408  	b.len = c
   409  	copy(b.bytes[l:], v)
   410  	return
   411  }
   412  
   413  func (b *ByteBuffer) WStrings(v []string) {
   414  	b.WUint16(uint16(len(v)))
   415  	for _, d := range v {
   416  		b.WString(d)
   417  	}
   418  }
   419  
   420  func (b *ByteBuffer) WStringNoLen(v string) {
   421  	if len(v) == 0 {
   422  		return
   423  	}
   424  	_, _ = b.Write([]byte(v))
   425  }
   426  
   427  func (b *ByteBuffer) WJson(o any) *Err {
   428  	bytes, err := JsonMarshal(o)
   429  	if err != nil {
   430  		return err
   431  	}
   432  	b.WBytes(bytes)
   433  	return nil
   434  }
   435  
   436  func (b *ByteBuffer) errLen(l int) *Err {
   437  	return NewErr(EcOutOfRange, M{
   438  		"available": b.len - b.pos,
   439  		"need":      l,
   440  	})
   441  }
   442  
   443  func (b *ByteBuffer) RBool() (v bool, err *Err) {
   444  	if b.Available() >= 1 {
   445  		v = b.bytes[b.pos] == byte(1)
   446  		b.pos++
   447  	} else {
   448  		err = b.errLen(1)
   449  	}
   450  	return
   451  }
   452  
   453  func (b *ByteBuffer) RBools() (v []bool, err *Err) {
   454  	p := b.pos
   455  	var l uint16
   456  	l, err = b.RUint16()
   457  	if err != nil {
   458  		b.pos = p
   459  		return
   460  	}
   461  	v = make([]bool, l)
   462  	for i := uint16(0); i < l; i++ {
   463  		v[i], err = b.RBool()
   464  		if err != nil {
   465  			b.pos = p
   466  			return
   467  		}
   468  	}
   469  	return
   470  }
   471  
   472  func (b *ByteBuffer) RBytes() ([]byte, *Err) {
   473  	c, err := b.RUint32()
   474  	if err != nil {
   475  		return nil, err
   476  	}
   477  	l := int(c)
   478  	if b.Available() < l {
   479  		return nil, b.errLen(l)
   480  	}
   481  	v := CopyBytes(b.bytes[b.pos : b.pos+l])
   482  	b.pos += l
   483  	return v, nil
   484  }
   485  
   486  // RAvailable 读取剩余所有字节
   487  func (b *ByteBuffer) RAvailable() (v []byte) {
   488  	v = b.bytes[b.pos:b.len]
   489  	b.pos = b.len
   490  	return
   491  }
   492  
   493  func (b *ByteBuffer) Read(p []byte) (n int, err error) {
   494  	if b.pos == b.len {
   495  		if len(p) == 0 {
   496  			return 0, nil
   497  		}
   498  		return 0, io.EOF
   499  	}
   500  	n = copy(p, b.bytes[b.pos:b.len])
   501  	b.pos = b.len
   502  	return n, nil
   503  }
   504  
   505  func (b *ByteBuffer) RUint8() (v uint8, err *Err) {
   506  	if b.Available() >= 1 {
   507  		v = b.bytes[b.pos]
   508  		b.pos++
   509  	} else {
   510  		err = b.errLen(1)
   511  	}
   512  	return
   513  }
   514  
   515  func (b *ByteBuffer) RUint8s() (v []uint8, err *Err) {
   516  	p := b.pos
   517  	var l uint16
   518  	l, err = b.RUint16()
   519  	if err != nil {
   520  		b.pos = p
   521  		return
   522  	}
   523  	v = make([]uint8, l)
   524  	for i := uint16(0); i < l; i++ {
   525  		v[i], err = b.RUint8()
   526  		if err != nil {
   527  			b.pos = p
   528  			return
   529  		}
   530  	}
   531  	return
   532  }
   533  
   534  func (b *ByteBuffer) RInt8() (v int8, err *Err) {
   535  	v1, e := b.RUint8()
   536  	if e == nil {
   537  		v = int8(v1)
   538  	} else {
   539  		err = e
   540  	}
   541  	return
   542  }
   543  
   544  func (b *ByteBuffer) RInt8s() (v []int8, err *Err) {
   545  	p := b.pos
   546  	var l uint16
   547  	l, err = b.RUint16()
   548  	if err != nil {
   549  		b.pos = p
   550  		return
   551  	}
   552  	v = make([]int8, l)
   553  	for i := uint16(0); i < l; i++ {
   554  		v[i], err = b.RInt8()
   555  		if err != nil {
   556  			b.pos = p
   557  			return
   558  		}
   559  	}
   560  	return
   561  }
   562  
   563  func (b *ByteBuffer) RUint16() (v uint16, err *Err) {
   564  	if b.Available() >= 2 {
   565  		v = uint16(b.bytes[b.pos])<<8 | uint16(b.bytes[b.pos+1])
   566  		b.pos += 2
   567  	} else {
   568  		err = b.errLen(2)
   569  	}
   570  	return
   571  }
   572  
   573  func (b *ByteBuffer) RUint16s() (v []uint16, err *Err) {
   574  	p := b.pos
   575  	var l uint16
   576  	l, err = b.RUint16()
   577  	if err != nil {
   578  		b.pos = p
   579  		return
   580  	}
   581  	v = make([]uint16, l)
   582  	for i := uint16(0); i < l; i++ {
   583  		v[i], err = b.RUint16()
   584  		if err != nil {
   585  			b.pos = p
   586  			return
   587  		}
   588  	}
   589  	return
   590  }
   591  
   592  func (b *ByteBuffer) RInt16() (v int16, err *Err) {
   593  	v1, e := b.RUint16()
   594  	if e == nil {
   595  		v = int16(v1)
   596  	} else {
   597  		err = e
   598  	}
   599  	return
   600  }
   601  
   602  func (b *ByteBuffer) RInt16s() (v []int16, err *Err) {
   603  	p := b.pos
   604  	var l uint16
   605  	l, err = b.RUint16()
   606  	if err != nil {
   607  		b.pos = p
   608  		return
   609  	}
   610  	v = make([]int16, l)
   611  	for i := uint16(0); i < l; i++ {
   612  		v[i], err = b.RInt16()
   613  		if err != nil {
   614  			b.pos = p
   615  			return
   616  		}
   617  	}
   618  	return
   619  }
   620  
   621  func (b *ByteBuffer) RInt() (v int, err *Err) {
   622  	if b.Available() >= 4 {
   623  		v = int(b.bytes[b.pos])<<24 | int(b.bytes[b.pos+1])<<16 | int(b.bytes[b.pos+2])<<8 | int(b.bytes[b.pos+3])
   624  		b.pos += 4
   625  	} else {
   626  		err = b.errLen(4)
   627  	}
   628  	return
   629  }
   630  
   631  func (b *ByteBuffer) RInts() (v []int, err *Err) {
   632  	p := b.pos
   633  	var l uint16
   634  	l, err = b.RUint16()
   635  	if err != nil {
   636  		b.pos = p
   637  		return
   638  	}
   639  	v = make([]int, l)
   640  	for i := uint16(0); i < l; i++ {
   641  		v[i], err = b.RInt()
   642  		if err != nil {
   643  			b.pos = p
   644  			return
   645  		}
   646  	}
   647  	return
   648  }
   649  
   650  func (b *ByteBuffer) RUint32() (v uint32, err *Err) {
   651  	if b.Available() >= 4 {
   652  		v = uint32(b.bytes[b.pos])<<24 | uint32(b.bytes[b.pos+1])<<16 | uint32(b.bytes[b.pos+2])<<8 | uint32(b.bytes[b.pos+3])
   653  		b.pos += 4
   654  	} else {
   655  		err = b.errLen(4)
   656  	}
   657  	return
   658  }
   659  
   660  func (b *ByteBuffer) RUint32s() (v []uint32, err *Err) {
   661  	p := b.pos
   662  	var l uint16
   663  	l, err = b.RUint16()
   664  	if err != nil {
   665  		b.pos = p
   666  		return
   667  	}
   668  	v = make([]uint32, l)
   669  	for i := uint16(0); i < l; i++ {
   670  		v[i], err = b.RUint32()
   671  		if err != nil {
   672  			b.pos = p
   673  			return
   674  		}
   675  	}
   676  	return
   677  }
   678  
   679  func (b *ByteBuffer) RInt32() (v int32, err *Err) {
   680  	v1, e := b.RUint32()
   681  	if e == nil {
   682  		v = int32(v1)
   683  	} else {
   684  		err = e
   685  	}
   686  	return
   687  }
   688  
   689  func (b *ByteBuffer) RInt32s() (v []int32, err *Err) {
   690  	p := b.pos
   691  	var l uint16
   692  	l, err = b.RUint16()
   693  	if err != nil {
   694  		b.pos = p
   695  		return
   696  	}
   697  	v = make([]int32, l)
   698  	for i := uint16(0); i < l; i++ {
   699  		v[i], err = b.RInt32()
   700  		if err != nil {
   701  			b.pos = p
   702  			return
   703  		}
   704  	}
   705  	return
   706  }
   707  
   708  func (b *ByteBuffer) RUint64() (v uint64, err *Err) {
   709  	if b.Available() >= 8 {
   710  		v = uint64(b.bytes[b.pos])<<56 | uint64(b.bytes[b.pos+1])<<48 | uint64(b.bytes[b.pos+2])<<40 | uint64(b.bytes[b.pos+3])<<32 | uint64(b.bytes[b.pos+4])<<24 | uint64(b.bytes[b.pos+5])<<16 | uint64(b.bytes[b.pos+6])<<8 | uint64(b.bytes[b.pos+7])
   711  		b.pos += 8
   712  	} else {
   713  		err = b.errLen(8)
   714  	}
   715  	return
   716  }
   717  
   718  func (b *ByteBuffer) RUint64s() (v []uint64, err *Err) {
   719  	p := b.pos
   720  	var l uint16
   721  	l, err = b.RUint16()
   722  	if err != nil {
   723  		b.pos = p
   724  		return
   725  	}
   726  	v = make([]uint64, l)
   727  	for i := uint16(0); i < l; i++ {
   728  		v[i], err = b.RUint64()
   729  		if err != nil {
   730  			b.pos = p
   731  			return
   732  		}
   733  	}
   734  	return
   735  }
   736  
   737  func (b *ByteBuffer) RInt64() (v int64, err *Err) {
   738  	v1, e := b.RUint64()
   739  	if e == nil {
   740  		v = int64(v1)
   741  	} else {
   742  		err = e
   743  	}
   744  	return
   745  }
   746  
   747  func (b *ByteBuffer) RInt64s() (v []int64, err *Err) {
   748  	p := b.pos
   749  	var l uint16
   750  	l, err = b.RUint16()
   751  	if err != nil {
   752  		b.pos = p
   753  		return
   754  	}
   755  	v = make([]int64, l)
   756  	for i := uint16(0); i < l; i++ {
   757  		v[i], err = b.RInt64()
   758  		if err != nil {
   759  			b.pos = p
   760  			return
   761  		}
   762  	}
   763  	return
   764  }
   765  
   766  func (b *ByteBuffer) RFloat32() (v float32, err *Err) {
   767  	v1, e := b.RUint32()
   768  	if e == nil {
   769  		v = math.Float32frombits(v1)
   770  	} else {
   771  		err = e
   772  	}
   773  	return
   774  }
   775  
   776  func (b *ByteBuffer) RFloat32s() (v []float32, err *Err) {
   777  	var l uint16
   778  	l, err = b.RUint16()
   779  	if err != nil {
   780  		return
   781  	}
   782  	v = make([]float32, l)
   783  	for i := uint16(0); i < l; i++ {
   784  		v[i], err = b.RFloat32()
   785  		if err != nil {
   786  			return
   787  		}
   788  	}
   789  	return
   790  }
   791  
   792  func (b *ByteBuffer) RFloat64() (v float64, err *Err) {
   793  	v1, e := b.RUint64()
   794  	if e == nil {
   795  		v = math.Float64frombits(v1)
   796  	} else {
   797  		err = e
   798  	}
   799  	return
   800  }
   801  
   802  func (b *ByteBuffer) RFloat64s() (v []float64, err *Err) {
   803  	p := b.pos
   804  	var l uint16
   805  	l, err = b.RUint16()
   806  	if err != nil {
   807  		b.pos = p
   808  		return
   809  	}
   810  	v = make([]float64, l)
   811  	for i := uint16(0); i < l; i++ {
   812  		v[i], err = b.RFloat64()
   813  		if err != nil {
   814  			b.pos = p
   815  			return
   816  		}
   817  	}
   818  	return
   819  }
   820  
   821  func (b *ByteBuffer) RVec2() (v Vec2, err *Err) {
   822  	x, err := b.RFloat32()
   823  	if err != nil {
   824  		return Vec2{}, err
   825  	}
   826  	y, err := b.RFloat32()
   827  	if err != nil {
   828  		return Vec2{}, err
   829  	}
   830  	return Vec2{X: x, Y: y}, nil
   831  }
   832  
   833  func (b *ByteBuffer) RVec2s() (v []Vec2, err *Err) {
   834  	var l uint16
   835  	l, err = b.RUint16()
   836  	if err != nil {
   837  		return
   838  	}
   839  	v = make([]Vec2, l)
   840  	for i := uint16(0); i < l; i++ {
   841  		v[i], err = b.RVec2()
   842  		if err != nil {
   843  			return
   844  		}
   845  	}
   846  	return
   847  }
   848  
   849  func (b *ByteBuffer) RVec3() (v Vec3, err *Err) {
   850  	x, err := b.RFloat32()
   851  	if err != nil {
   852  		return Vec3{}, err
   853  	}
   854  	y, err := b.RFloat32()
   855  	if err != nil {
   856  		return Vec3{}, err
   857  	}
   858  	z, err := b.RFloat32()
   859  	if err != nil {
   860  		return Vec3{}, err
   861  	}
   862  	return Vec3{X: x, Y: y, Z: z}, nil
   863  }
   864  
   865  func (b *ByteBuffer) RVec3s() (v []Vec3, err *Err) {
   866  	var l uint16
   867  	l, err = b.RUint16()
   868  	if err != nil {
   869  		return
   870  	}
   871  	v = make([]Vec3, l)
   872  	for i := uint16(0); i < l; i++ {
   873  		v[i], err = b.RVec3()
   874  		if err != nil {
   875  			return
   876  		}
   877  	}
   878  	return
   879  }
   880  
   881  func (b *ByteBuffer) RShortString() (v string, err *Err) {
   882  	var length uint8
   883  	length, err = b.RUint8()
   884  	if err != nil {
   885  		return
   886  	}
   887  	if length == 0 {
   888  		return
   889  	}
   890  	l := int(length)
   891  	if b.Available() < l {
   892  		err = b.errLen(l)
   893  		return
   894  	}
   895  	v = string(b.bytes[b.pos : b.pos+l])
   896  	b.pos += l
   897  	return
   898  }
   899  
   900  func (b *ByteBuffer) RString() (v string, err *Err) {
   901  	var length uint16
   902  	length, err = b.RUint16()
   903  	if err != nil {
   904  		return
   905  	}
   906  	if length == 0 {
   907  		return
   908  	}
   909  	l := int(length)
   910  	if b.Available() < l {
   911  		err = b.errLen(l)
   912  		return
   913  	}
   914  	v = string(b.bytes[b.pos : b.pos+l])
   915  	b.pos += l
   916  	return
   917  }
   918  
   919  func (b *ByteBuffer) RStrings() (v []string, err *Err) {
   920  	var l uint16
   921  	l, err = b.RUint16()
   922  	if err != nil {
   923  		return
   924  	}
   925  	v = make([]string, l)
   926  	for i := uint16(0); i < l; i++ {
   927  		v[i], err = b.RString()
   928  		if err != nil {
   929  			return
   930  		}
   931  	}
   932  	return
   933  }
   934  
   935  func (b *ByteBuffer) RStringNoLen() (v string) {
   936  	bs := b.RAvailable()
   937  	v = string(bs)
   938  	return
   939  }
   940  
   941  func (b *ByteBuffer) RJson(v any) *Err {
   942  	bytes, err := b.RBytes()
   943  	if err != nil {
   944  		return err
   945  	}
   946  	return JsonUnmarshal(bytes, v)
   947  }
   948  
   949  func (b *ByteBuffer) ToHex() string {
   950  	return hex.EncodeToString(b.bytes[:b.len])
   951  }
   952  
   953  func (b *ByteBuffer) WMInt32(m map[string]int32) {
   954  	b.WUint16(uint16(len(m)))
   955  	for k, v := range m {
   956  		b.WString(k)
   957  		b.WInt32(v)
   958  	}
   959  }
   960  
   961  func (b *ByteBuffer) RMInt32() (m map[string]int32, err *Err) {
   962  	var (
   963  		l uint16
   964  		k string
   965  		v int32
   966  	)
   967  	l, err = b.RUint16()
   968  	if err != nil {
   969  		return
   970  	}
   971  	m = make(map[string]int32, l)
   972  	if l == 0 {
   973  		return
   974  	}
   975  	for i := uint16(0); i < l; i++ {
   976  		k, err = b.RString()
   977  		if err != nil {
   978  			return
   979  		}
   980  		v, err = b.RInt32()
   981  		if err != nil {
   982  			return
   983  		}
   984  		m[k] = v
   985  	}
   986  	return
   987  }
   988  
   989  func (b *ByteBuffer) WMInt64(m map[string]int64) {
   990  	b.WUint16(uint16(len(m)))
   991  	for k, v := range m {
   992  		b.WString(k)
   993  		b.WInt64(v)
   994  	}
   995  }
   996  
   997  func (b *ByteBuffer) RMInt64() (m map[string]int64, err *Err) {
   998  	var (
   999  		l uint16
  1000  		k string
  1001  		v int64
  1002  	)
  1003  	l, err = b.RUint16()
  1004  	if err != nil {
  1005  		return
  1006  	}
  1007  	m = make(map[string]int64, l)
  1008  	if l == 0 {
  1009  		return
  1010  	}
  1011  	for i := uint16(0); i < l; i++ {
  1012  		k, err = b.RString()
  1013  		if err != nil {
  1014  			return
  1015  		}
  1016  		v, err = b.RInt64()
  1017  		if err != nil {
  1018  			return
  1019  		}
  1020  		m[k] = v
  1021  	}
  1022  	return
  1023  }
  1024  
  1025  func (b *ByteBuffer) WUint16MUint16(m map[uint16]uint16) {
  1026  	b.WUint16(uint16(len(m)))
  1027  	for k, v := range m {
  1028  		b.WUint16(k)
  1029  		b.WUint16(v)
  1030  	}
  1031  }
  1032  
  1033  func (b *ByteBuffer) RUint16MUint16() (m map[uint16]uint16, err *Err) {
  1034  	var (
  1035  		l uint16
  1036  		k uint16
  1037  		v uint16
  1038  	)
  1039  	l, err = b.RUint16()
  1040  	if err != nil {
  1041  		return
  1042  	}
  1043  	m = make(map[uint16]uint16, l)
  1044  	if l == 0 {
  1045  		return
  1046  	}
  1047  	for i := uint16(0); i < l; i++ {
  1048  		k, err = b.RUint16()
  1049  		if err != nil {
  1050  			return
  1051  		}
  1052  		v, err = b.RUint16()
  1053  		if err != nil {
  1054  			return
  1055  		}
  1056  		m[k] = v
  1057  	}
  1058  	return
  1059  }
  1060  
  1061  func (b *ByteBuffer) WInt32MInt32(m map[int32]int32) {
  1062  	l := len(m)
  1063  	b.WUint8(uint8(l))
  1064  	if l == 0 {
  1065  		return
  1066  	}
  1067  	for k, v := range m {
  1068  		b.WInt32(k)
  1069  		b.WInt32(v)
  1070  	}
  1071  }
  1072  
  1073  func (b *ByteBuffer) RInt32MInt32() (m map[int32]int32, err *Err) {
  1074  	var (
  1075  		l uint8
  1076  		k int32
  1077  		v int32
  1078  	)
  1079  	l, err = b.RUint8()
  1080  	if err != nil {
  1081  		return
  1082  	}
  1083  	m = make(map[int32]int32, l)
  1084  	if l == 0 {
  1085  		return
  1086  	}
  1087  	for i := uint8(0); i < l; i++ {
  1088  		k, err = b.RInt32()
  1089  		if err != nil {
  1090  			return
  1091  		}
  1092  		v, err = b.RInt32()
  1093  		if err != nil {
  1094  			return
  1095  		}
  1096  		m[k] = v
  1097  	}
  1098  	return
  1099  }
  1100  
  1101  func (b *ByteBuffer) WInt32MInt64(m map[int32]int64) {
  1102  	l := len(m)
  1103  	b.WUint8(uint8(l))
  1104  	if l == 0 {
  1105  		return
  1106  	}
  1107  	for k, v := range m {
  1108  		b.WInt32(k)
  1109  		b.WInt64(v)
  1110  	}
  1111  }
  1112  
  1113  func (b *ByteBuffer) RInt32MInt64() (m map[int32]int64, err *Err) {
  1114  	var (
  1115  		l uint8
  1116  		k int32
  1117  		v int64
  1118  	)
  1119  	l, err = b.RUint8()
  1120  	if err != nil {
  1121  		return
  1122  	}
  1123  	m = make(map[int32]int64, l)
  1124  	if l == 0 {
  1125  		return
  1126  	}
  1127  	for i := uint8(0); i < l; i++ {
  1128  		k, err = b.RInt32()
  1129  		if err != nil {
  1130  			return
  1131  		}
  1132  		v, err = b.RInt64()
  1133  		if err != nil {
  1134  			return
  1135  		}
  1136  		m[k] = v
  1137  	}
  1138  	return
  1139  }
  1140  
  1141  func (b *ByteBuffer) WInt64MInt32(m map[int64]int32) {
  1142  	l := len(m)
  1143  	b.WUint8(uint8(l))
  1144  	if l == 0 {
  1145  		return
  1146  	}
  1147  	for k, v := range m {
  1148  		b.WInt64(k)
  1149  		b.WInt32(v)
  1150  	}
  1151  }
  1152  
  1153  func (b *ByteBuffer) RInt64MInt32() (m map[int64]int32, err *Err) {
  1154  	var (
  1155  		l uint8
  1156  		k int64
  1157  		v int32
  1158  	)
  1159  	l, err = b.RUint8()
  1160  	if err != nil {
  1161  		return
  1162  	}
  1163  	m = make(map[int64]int32, l)
  1164  	if l == 0 {
  1165  		return
  1166  	}
  1167  	for i := uint8(0); i < l; i++ {
  1168  		k, err = b.RInt64()
  1169  		if err != nil {
  1170  			return
  1171  		}
  1172  		v, err = b.RInt32()
  1173  		if err != nil {
  1174  			return
  1175  		}
  1176  		m[k] = v
  1177  	}
  1178  	return
  1179  }
  1180  
  1181  func (b *ByteBuffer) WInt64MInt64(m map[int64]int64) {
  1182  	l := len(m)
  1183  	b.WUint8(uint8(l))
  1184  	if l == 0 {
  1185  		return
  1186  	}
  1187  	for k, v := range m {
  1188  		b.WInt64(k)
  1189  		b.WInt64(v)
  1190  	}
  1191  }
  1192  
  1193  func (b *ByteBuffer) RInt64MInt64() (m map[int64]int64, err *Err) {
  1194  	var (
  1195  		l uint8
  1196  		k int64
  1197  		v int64
  1198  	)
  1199  	l, err = b.RUint8()
  1200  	if err != nil {
  1201  		return
  1202  	}
  1203  	m = make(map[int64]int64, l)
  1204  	if l == 0 {
  1205  		return
  1206  	}
  1207  	for i := uint8(0); i < l; i++ {
  1208  		k, err = b.RInt64()
  1209  		if err != nil {
  1210  			return
  1211  		}
  1212  		v, err = b.RInt64()
  1213  		if err != nil {
  1214  			return
  1215  		}
  1216  		m[k] = v
  1217  	}
  1218  	return
  1219  }
  1220  
  1221  func (b *ByteBuffer) WMStr(m MS) {
  1222  	l := len(m)
  1223  	b.WUint8(uint8(l))
  1224  	if l == 0 {
  1225  		return
  1226  	}
  1227  	for k, v := range m {
  1228  		b.WString(k)
  1229  		b.WString(v)
  1230  	}
  1231  }
  1232  
  1233  func (b *ByteBuffer) RMStr() (m MS, err *Err) {
  1234  	var (
  1235  		l uint8
  1236  		k string
  1237  		v string
  1238  	)
  1239  	l, err = b.RUint8()
  1240  	if err != nil {
  1241  		return
  1242  	}
  1243  	m = make(MS, l)
  1244  	if l == 0 {
  1245  		return
  1246  	}
  1247  	for i := uint8(0); i < l; i++ {
  1248  		k, err = b.RString()
  1249  		if err != nil {
  1250  			return
  1251  		}
  1252  		v, err = b.RString()
  1253  		if err != nil {
  1254  			return
  1255  		}
  1256  		m[k] = v
  1257  	}
  1258  	return
  1259  }
  1260  
  1261  func (b *ByteBuffer) WMAny(m M) *Err {
  1262  	l := len(m)
  1263  	b.WUint16(uint16(l))
  1264  	if l == 0 {
  1265  		return nil
  1266  	}
  1267  	for k, v := range m {
  1268  		b.WString(k)
  1269  		err := b.WAny(v)
  1270  		if err != nil {
  1271  			return err
  1272  		}
  1273  	}
  1274  	return nil
  1275  }
  1276  
  1277  func (b *ByteBuffer) RMAny(m M) (err *Err) {
  1278  	c, err := b.RUint16()
  1279  	if err != nil {
  1280  		return err
  1281  	}
  1282  	for i := uint16(0); i < c; i++ {
  1283  		k, err := b.RString()
  1284  		if err != nil {
  1285  			return err
  1286  		}
  1287  		v, err := b.RAny()
  1288  		if err != nil {
  1289  			return err
  1290  		}
  1291  		m[k] = v
  1292  	}
  1293  	return nil
  1294  }
  1295  
  1296  func (b *ByteBuffer) WAnyMAny(m map[any]any) *Err {
  1297  	l := len(m)
  1298  	b.WUint8(uint8(l))
  1299  	if l == 0 {
  1300  		return nil
  1301  	}
  1302  	for k, v := range m {
  1303  		err := b.WAny(k)
  1304  		if err != nil {
  1305  			return err
  1306  		}
  1307  		err = b.WAny(v)
  1308  		if err != nil {
  1309  			return err
  1310  		}
  1311  	}
  1312  	return nil
  1313  }
  1314  
  1315  func (b *ByteBuffer) RAnyMAny(m map[any]any) (err *Err) {
  1316  	var (
  1317  		l uint8
  1318  		k any
  1319  		v any
  1320  	)
  1321  	l, err = b.RUint8()
  1322  	if err != nil {
  1323  		return
  1324  	}
  1325  	if l == 0 {
  1326  		return
  1327  	}
  1328  	for i := uint8(0); i < l; i++ {
  1329  		k, err = b.RAny()
  1330  		if err != nil {
  1331  			return
  1332  		}
  1333  		v, err = b.RAny()
  1334  		if err != nil {
  1335  			return
  1336  		}
  1337  		m[k] = v
  1338  	}
  1339  	return
  1340  }
  1341  
  1342  func (b *ByteBuffer) WAny(v any) *Err {
  1343  	switch v := v.(type) {
  1344  	case bool:
  1345  		b.WUint8(Bool)
  1346  		b.WBool(v)
  1347  	case []bool:
  1348  		b.WUint8(Bools)
  1349  		b.WBools(v)
  1350  	case uint8:
  1351  		b.WUint8(Uint8)
  1352  		b.WUint8(v)
  1353  	case []byte:
  1354  		b.WUint8(Bytes)
  1355  		b.WBytes(v)
  1356  	case int8:
  1357  		b.WUint8(Int8)
  1358  		b.WInt8(v)
  1359  	case []int8:
  1360  		b.WUint8(Int8s)
  1361  		b.WInt8s(v)
  1362  	case uint16:
  1363  		b.WUint8(Uint16)
  1364  		b.WUint16(v)
  1365  	case []uint16:
  1366  		b.WUint8(Uint16s)
  1367  		b.WUint16s(v)
  1368  	case int16:
  1369  		b.WUint8(Int16)
  1370  		b.WInt16(v)
  1371  	case []int16:
  1372  		b.WUint8(Int16s)
  1373  		b.WInt16s(v)
  1374  	case uint32:
  1375  		b.WUint8(Uint32)
  1376  		b.WUint32(v)
  1377  	case []uint32:
  1378  		b.WUint8(Uint32s)
  1379  		b.WUint32s(v)
  1380  	case int:
  1381  		b.WUint8(Int)
  1382  		b.WInt(v)
  1383  	case []int:
  1384  		b.WUint8(Ints)
  1385  		b.WInts(v)
  1386  	case int32:
  1387  		b.WUint8(Int32)
  1388  		b.WInt32(v)
  1389  	case []int32:
  1390  		b.WUint8(Int32s)
  1391  		b.WInt32s(v)
  1392  	case uint64:
  1393  		b.WUint8(Uint64)
  1394  		b.WUint64(v)
  1395  	case []uint64:
  1396  		b.WUint8(Uint64s)
  1397  		b.WUint64s(v)
  1398  	case int64:
  1399  		b.WUint8(Int64)
  1400  		b.WInt64(v)
  1401  	case []int64:
  1402  		b.WUint8(Int64s)
  1403  		b.WInt64s(v)
  1404  	case float32:
  1405  		b.WUint8(Float32)
  1406  		b.WFloat32(v)
  1407  	case []float32:
  1408  		b.WUint8(Float32s)
  1409  		b.WFloat32s(v)
  1410  	case float64:
  1411  		b.WUint8(Float64)
  1412  		b.WFloat64(v)
  1413  	case []float64:
  1414  		b.WUint8(Float64s)
  1415  		b.WFloat64s(v)
  1416  	case Vec2:
  1417  		b.WUint8(Vector2)
  1418  		b.WVec2(v)
  1419  	case []Vec2:
  1420  		b.WUint8(Vector2s)
  1421  		b.WVec2s(v)
  1422  	case Vec3:
  1423  		b.WUint8(Vector3)
  1424  		b.WVec3(v)
  1425  	case []Vec3:
  1426  		b.WUint8(Vector3s)
  1427  		b.WVec3s(v)
  1428  	case string:
  1429  		b.WUint8(Str)
  1430  		b.WString(v)
  1431  	case []string:
  1432  		b.WUint8(Strs)
  1433  		b.WStrings(v)
  1434  	case time.Time:
  1435  		b.WUint8(Time)
  1436  		b.WInt64(v.UnixNano())
  1437  	case []time.Time:
  1438  		b.WUint8(Times)
  1439  		b.WUint32(uint32(len(v)))
  1440  		for _, t := range v {
  1441  			b.WInt64(t.UnixMicro())
  1442  		}
  1443  	case map[string]int32:
  1444  		b.WUint8(MInt32)
  1445  		b.WMInt32(v)
  1446  	case map[string]int64:
  1447  		b.WUint8(MInt64)
  1448  		b.WMInt64(v)
  1449  	case map[uint16]uint16:
  1450  		b.WUint8(Uint16MUint16)
  1451  		b.WUint16MUint16(v)
  1452  	case map[int32]int32:
  1453  		b.WUint8(Int32MInt32)
  1454  		b.WInt32MInt32(v)
  1455  	case map[int32]int64:
  1456  		b.WUint8(Int32MInt64)
  1457  		b.WInt32MInt64(v)
  1458  	case map[int64]int32:
  1459  		b.WUint8(Int64MInt32)
  1460  		b.WInt64MInt32(v)
  1461  	case map[int64]int64:
  1462  		b.WUint8(Int64MInt64)
  1463  		b.WInt64MInt64(v)
  1464  	case MS:
  1465  		b.WUint8(MStr)
  1466  		b.WMStr(v)
  1467  	case M:
  1468  		b.WUint8(MAny)
  1469  		return b.WMAny(v)
  1470  	case map[any]any:
  1471  		b.WUint8(AnyMAny)
  1472  		return b.WAnyMAny(v)
  1473  	default:
  1474  		m := make(M)
  1475  		e := mapstructure.Decode(v, &m)
  1476  		if e != nil {
  1477  			return NewErr(EcNotImplement, M{
  1478  				"error": e.Error(),
  1479  			})
  1480  		}
  1481  		b.WUint8(MAny)
  1482  		return b.WMAny(m)
  1483  	}
  1484  	return nil
  1485  }
  1486  func (b *ByteBuffer) RAny() (v any, err *Err) {
  1487  	t, err := b.RUint8()
  1488  	if err != nil {
  1489  		return nil, err
  1490  	}
  1491  	switch t {
  1492  	case Bool:
  1493  		return b.RBool()
  1494  	case Bools:
  1495  		return b.RBools()
  1496  	case Uint8:
  1497  		return b.RUint8()
  1498  	case Bytes:
  1499  		return b.RBytes()
  1500  	case Int8:
  1501  		return b.RInt8()
  1502  	case Int8s:
  1503  		return b.RInt8s()
  1504  	case Uint16:
  1505  		return b.RUint16()
  1506  	case Uint16s:
  1507  		return b.RUint16s()
  1508  	case Int16:
  1509  		return b.RInt16()
  1510  	case Int16s:
  1511  		return b.RInt16s()
  1512  	case Uint32:
  1513  		return b.RUint32()
  1514  	case Uint32s:
  1515  		return b.RUint32s()
  1516  	case Int:
  1517  		return b.RInt()
  1518  	case Ints:
  1519  		return b.RInts()
  1520  	case Int32:
  1521  		return b.RInt32()
  1522  	case Int32s:
  1523  		return b.RInt32s()
  1524  	case Uint64:
  1525  		return b.RUint64()
  1526  	case Uint64s:
  1527  		return b.RUint64s()
  1528  	case Int64:
  1529  		return b.RInt64()
  1530  	case Int64s:
  1531  		return b.RInt64s()
  1532  	case Float32:
  1533  		return b.RFloat32()
  1534  	case Float32s:
  1535  		return b.RFloat32s()
  1536  	case Float64:
  1537  		return b.RFloat64()
  1538  	case Float64s:
  1539  		return b.RFloat64s()
  1540  	case Vector2:
  1541  		return b.RVec2()
  1542  	case Vector2s:
  1543  		return b.RVec2s()
  1544  	case Vector3:
  1545  		return b.RVec3()
  1546  	case Vector3s:
  1547  		return b.RVec3s()
  1548  	case Str:
  1549  		return b.RString()
  1550  	case Strs:
  1551  		return b.RStrings()
  1552  	case Time:
  1553  		v, e := b.RInt64()
  1554  		if e != nil {
  1555  			return time.Time{}, e
  1556  		}
  1557  		return time.UnixMicro(v), nil
  1558  	case Times:
  1559  		c, e := b.RUint32()
  1560  		if e != nil {
  1561  			return nil, e
  1562  		}
  1563  		ts := make([]time.Time, c)
  1564  		for i := uint32(0); i < c; i++ {
  1565  			t, e := b.RInt64()
  1566  			if e != nil {
  1567  				return nil, e
  1568  			}
  1569  			ts[i] = time.UnixMicro(t)
  1570  		}
  1571  		return ts, nil
  1572  	case MInt32:
  1573  		return b.RMInt32()
  1574  	case MInt64:
  1575  		return b.RMInt64()
  1576  	case MStr:
  1577  		return b.RMStr()
  1578  	case MAny:
  1579  		m := make(M)
  1580  		err := b.RMAny(m)
  1581  		if err != nil {
  1582  			return nil, err
  1583  		}
  1584  		return m, nil
  1585  	case Uint16MUint16:
  1586  		return b.RUint16MUint16()
  1587  	case Int32MInt32:
  1588  		return b.RInt32MInt32()
  1589  	case Int32MInt64:
  1590  		return b.RInt32MInt64()
  1591  	case Int64MInt32:
  1592  		return b.RInt64MInt32()
  1593  	case Int64MInt64:
  1594  		return b.RInt64MInt64()
  1595  	case AnyMAny:
  1596  		m := make(map[any]any)
  1597  		err := b.RAnyMAny(m)
  1598  		if err != nil {
  1599  			return nil, err
  1600  		}
  1601  		return m, nil
  1602  	default:
  1603  		return nil, NewErr(EcNotImplement, M{
  1604  			"type": t,
  1605  		})
  1606  	}
  1607  }
  1608  
  1609  func (b *ByteBuffer) WAny2(m map[any]any) (err *Err) {
  1610  	b.WUint16(uint16(len(m)))
  1611  	for k, v := range m {
  1612  		err = b.WAny(k)
  1613  		if err != nil {
  1614  			return
  1615  		}
  1616  		err = b.WAny(v)
  1617  		if err != nil {
  1618  			return
  1619  		}
  1620  	}
  1621  	return
  1622  }
  1623  
  1624  func (b *ByteBuffer) WErr(err *Err) *Err {
  1625  	b.WUint16(err.Code())
  1626  	bytes, err := JsonMarshal(err.Params())
  1627  	if err != nil {
  1628  		return err
  1629  	}
  1630  	b.WBytes(bytes)
  1631  	return nil
  1632  }
  1633  
  1634  func (b *ByteBuffer) RErr() (e *Err, re *Err) {
  1635  	code, err := b.RUint16()
  1636  	if err != nil {
  1637  		return nil, err
  1638  	}
  1639  	bytes := b.RAvailable()
  1640  	params := M{}
  1641  	err = JsonUnmarshal(bytes, params)
  1642  	if err != nil {
  1643  		return nil, err
  1644  	}
  1645  	return NewNoStackErr(code, params), nil
  1646  }
  1647  
  1648  func (b *ByteBuffer) WriteTo(writer io.Writer) (n int, err error) {
  1649  	return writer.Write(b.bytes[:b.len])
  1650  }
  1651  
  1652  // Dispose 释放后不要再使用数据
  1653  func (b *ByteBuffer) Dispose() {
  1654  	if !b.canRecycle {
  1655  		return
  1656  	}
  1657  	b.canRecycle = false
  1658  	RecycleBytes(b.bytes)
  1659  }
  1660  
  1661  func init() {
  1662  	gob.Register(M{})
  1663  	gob.Register([]any{})
  1664  	gob.Register(Vec2{})
  1665  	gob.Register(Vec3{})
  1666  }
  1667  
  1668  func PbMarshal(pkt IMsg) ([]byte, error) {
  1669  	return proto.Marshal(pkt)
  1670  }
  1671  
  1672  func PbUnmarshal(data []byte, pkt IMsg) error {
  1673  	return proto.Unmarshal(data, pkt)
  1674  }