luban-gen-code

This commit is contained in:
zhaocy 2022-05-31 13:42:54 +08:00
parent 8fe33b1347
commit 834e9b4221
14 changed files with 1271 additions and 0 deletions

6
.gitignore vendored
View File

@ -9,3 +9,9 @@
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
*.log
.vscode
./bin/log
~$*.xlsx
cmd/luban/

74
bin/json/tbitem.json Normal file
View File

@ -0,0 +1,74 @@
[
{
"id": 40000,
"name": "发型1",
"desc": "初始发型",
"price": 0,
"batch_useable": false
},
{
"id": 40001,
"name": "外套1",
"desc": "初始外套",
"price": 100,
"batch_useable": false
},
{
"id": 40002,
"name": "上衣1",
"desc": "初始上衣",
"price": 0,
"batch_useable": false
},
{
"id": 40003,
"name": "裙子1",
"desc": "初始下装",
"price": 100,
"batch_useable": false
},
{
"id": 40004,
"name": "袜子1",
"desc": "初始袜子",
"price": 100,
"batch_useable": false
},
{
"id": 40005,
"name": "鞋子1",
"desc": "初始鞋子",
"price": 100,
"batch_useable": false
},
{
"id": 40006,
"name": "发饰1",
"desc": "初始发饰",
"price": 100,
"batch_useable": false
},
{
"id": 40007,
"name": "中秋节发饰1",
"desc": "中秋节发饰",
"price": 200,
"expire_time": 1633881599,
"batch_useable": false
},
{
"id": 40008,
"name": "中秋节鞋子1",
"desc": "中秋节鞋子",
"price": 300,
"expire_time": 1633967999,
"batch_useable": false
},
{
"id": 40009,
"name": "礼包1",
"desc": "中秋节礼包",
"price": 100,
"batch_useable": true
}
]

74
bin/json/tbrewards.json Normal file
View File

@ -0,0 +1,74 @@
[
{
"id": 10000,
"name": "发型",
"desc": "初始发型",
"price": 100,
"batch_useable": false
},
{
"id": 10001,
"name": "外套",
"desc": "初始外套",
"price": 100,
"batch_useable": false
},
{
"id": 10002,
"name": "上衣",
"desc": "初始上衣",
"price": 100,
"batch_useable": false
},
{
"id": 10003,
"name": "裙子",
"desc": "初始下装",
"price": 100,
"batch_useable": false
},
{
"id": 10004,
"name": "袜子",
"desc": "初始袜子",
"price": 100,
"batch_useable": false
},
{
"id": 10005,
"name": "鞋子",
"desc": "初始鞋子",
"price": 100,
"batch_useable": false
},
{
"id": 10006,
"name": "发饰",
"desc": "初始发饰",
"price": 100,
"batch_useable": false
},
{
"id": 10007,
"name": "中秋节发饰",
"desc": "中秋节发饰",
"price": 200,
"expire_time": 1633881599,
"batch_useable": false
},
{
"id": 10008,
"name": "中秋节鞋子",
"desc": "中秋节鞋子",
"price": 300,
"expire_time": 1633967999,
"batch_useable": false
},
{
"id": 10009,
"name": "礼包",
"desc": "中秋节礼包",
"price": 100,
"batch_useable": true
}
]

View File

@ -0,0 +1,662 @@
package serialization
import (
"errors"
"math"
)
var EmptyBytes []byte
var UnmarshalErr = errors.New("read not enough")
func init() {
EmptyBytes = make([]byte, 0)
}
type ByteBuf struct {
readerIndex int
writerIndex int
capacity int
bytes []byte
}
func NewByteBuf(capacity int) *ByteBuf {
v := &ByteBuf{}
v.bytes = make([]byte, capacity, capacity)
v.readerIndex = 0
v.writerIndex = 0
v.capacity = capacity
return v
}
func WrapByteBuf(bytes []byte) *ByteBuf {
v := &ByteBuf{}
v.bytes = bytes
v.readerIndex = 0
v.writerIndex = len(bytes)
v.capacity = len(bytes)
return v
}
func (buf *ByteBuf) Replace(bytes []byte) {
buf.bytes = bytes
buf.readerIndex = 0
buf.writerIndex = len(bytes)
buf.capacity = len(bytes)
}
func (buf *ByteBuf) Clear() {
buf.readerIndex = 0
buf.writerIndex = 0
}
func (buf *ByteBuf) Size() int {
return buf.writerIndex - buf.readerIndex
}
func (buf *ByteBuf) GetBytes() []byte {
return buf.bytes
}
func (buf *ByteBuf) CopyRemainData() []byte {
size := len(buf.bytes)
if size > 0 {
bs := make([]byte, size, size)
copy(bs, buf.bytes[buf.readerIndex:buf.writerIndex])
return bs
} else {
return EmptyBytes
}
}
func (buf *ByteBuf) CalcNewCap(curSize int, needSize int) int {
curSize *= 2
if curSize < 16 {
curSize = 16
}
for ; curSize < needSize; curSize *= 2 {
}
return curSize
}
func (buf *ByteBuf) EnsureWrite(remain int) {
if buf.writerIndex+remain > buf.capacity {
size := buf.Size()
if size+remain <= buf.capacity {
copy(buf.bytes, buf.bytes[buf.readerIndex:buf.writerIndex])
} else {
buf.capacity = buf.CalcNewCap(buf.capacity, size+remain)
newBytes := make([]byte, buf.capacity, buf.capacity)
copy(newBytes, buf.bytes[buf.readerIndex:buf.writerIndex])
buf.bytes = newBytes
}
buf.writerIndex = size
buf.readerIndex = 0
}
}
func (buf *ByteBuf) ReadBool() (bool, error) {
if buf.readerIndex < buf.writerIndex {
x := buf.bytes[buf.readerIndex] != 0
buf.readerIndex++
return x, nil
} else {
return false, UnmarshalErr
}
}
func (buf *ByteBuf) WriteBool(x bool) {
buf.EnsureWrite(1)
if x {
buf.bytes[buf.writerIndex] = 1
} else {
buf.bytes[buf.writerIndex] = 0
}
buf.writerIndex++
}
func (buf *ByteBuf) ReadByte() (byte, error) {
if buf.readerIndex < buf.writerIndex {
x := buf.bytes[buf.readerIndex]
buf.readerIndex++
return x, nil
} else {
return 0, UnmarshalErr
}
}
func (buf *ByteBuf) WriteByte(x byte) {
buf.EnsureWrite(1)
buf.bytes[buf.writerIndex] = x
buf.writerIndex++
}
func (buf *ByteBuf) ReadShort() (int16, error) {
if buf.readerIndex >= buf.writerIndex {
return 0, UnmarshalErr
}
h := uint32(buf.bytes[buf.readerIndex])
if h < 0x80 {
buf.readerIndex++
return int16(h), nil
} else if h < 0xc0 {
if buf.readerIndex+2 > buf.writerIndex {
return 0, UnmarshalErr
}
x := ((h & 0x3f) << 8) | uint32(buf.bytes[buf.readerIndex+1])
buf.readerIndex += 2
return int16(x), nil
} else if h == 0xff {
if buf.readerIndex+3 > buf.writerIndex {
return 0, UnmarshalErr
}
x := (uint32(buf.bytes[buf.readerIndex+1]) << 8) | uint32(buf.bytes[buf.readerIndex+2])
buf.readerIndex += 3
return int16(x), nil
} else {
return 0, UnmarshalErr
}
}
func (buf *ByteBuf) WriteShort(x int16) {
if x >= 0 {
if x < 0x80 {
buf.EnsureWrite(1)
buf.bytes[buf.writerIndex] = byte(x)
buf.writerIndex++
return
} else if x < 0x4000 {
buf.EnsureWrite(2)
buf.bytes[buf.writerIndex+1] = byte(x)
buf.bytes[buf.writerIndex] = byte((x >> 8) | 0x80)
buf.writerIndex += 2
return
}
}
buf.EnsureWrite(3)
buf.bytes[buf.writerIndex] = 0xff
buf.bytes[buf.writerIndex+2] = byte(x)
buf.bytes[buf.writerIndex+1] = byte(x >> 8)
buf.writerIndex += 3
}
func (buf *ByteBuf) ReadFshort() (int16, error) {
if buf.readerIndex+2 > buf.writerIndex {
return 0, UnmarshalErr
}
x := int(buf.bytes[buf.readerIndex]) | (int(buf.bytes[buf.readerIndex+1]) << 8)
buf.readerIndex += 2
return int16(x), nil
}
func (buf *ByteBuf) WriteFshort(x int16) {
buf.EnsureWrite(2)
buf.bytes[buf.writerIndex] = byte(x)
buf.bytes[buf.writerIndex+1] = byte(x >> 8)
buf.writerIndex += 2
}
func (buf *ByteBuf) ReadInt() (int32, error) {
x, err := buf.ReadUint()
return int32(x), err
}
func (buf *ByteBuf) WriteInt(x int32) {
buf.WriteUint(uint32(x))
}
func (buf *ByteBuf) ReadUint() (uint32, error) {
if buf.readerIndex >= buf.writerIndex {
return 0, UnmarshalErr
}
h := uint32(buf.bytes[buf.readerIndex])
if h < 0x80 {
buf.readerIndex++
return h, nil
} else if h < 0xc0 {
if buf.readerIndex+2 > buf.writerIndex {
return 0, UnmarshalErr
}
x := ((h & 0x3f) << 8) | uint32(buf.bytes[buf.readerIndex+1])
buf.readerIndex += 2
return x, nil
} else if h < 0xe0 {
if buf.readerIndex+3 > buf.writerIndex {
return 0, UnmarshalErr
}
x := ((h & 0x1f) << 16) | (uint32(buf.bytes[buf.readerIndex+1]) << 8) | uint32(buf.bytes[buf.readerIndex+2])
buf.readerIndex += 3
return x, nil
} else if h < 0xf0 {
if buf.readerIndex+4 > buf.writerIndex {
return 0, UnmarshalErr
}
x := ((h & 0x0f) << 24) | (uint32(buf.bytes[buf.readerIndex+1]) << 16) | (uint32(buf.bytes[buf.readerIndex+2]) << 8) | uint32(buf.bytes[buf.readerIndex+3])
buf.readerIndex += 4
return x, nil
} else {
if buf.readerIndex+5 > buf.writerIndex {
return 0, UnmarshalErr
}
x := (uint32(buf.bytes[buf.readerIndex+1]) << 24) | (uint32(buf.bytes[buf.readerIndex+2]) << 16) | (uint32(buf.bytes[buf.readerIndex+3]) << 8) | uint32(buf.bytes[buf.readerIndex+4])
buf.readerIndex += 5
return x, nil
}
}
func (buf *ByteBuf) WriteUint(x uint32) {
if x < 0x80 {
buf.EnsureWrite(1)
buf.bytes[buf.writerIndex] = byte(x)
buf.writerIndex++
} else if x < 0x4000 {
buf.EnsureWrite(2)
buf.bytes[buf.writerIndex+1] = byte(x)
buf.bytes[buf.writerIndex] = byte((x >> 8) | 0x80)
buf.writerIndex += 2
} else if x < 0x200000 {
buf.EnsureWrite(3)
buf.bytes[buf.writerIndex+2] = byte(x)
buf.bytes[buf.writerIndex+1] = byte(x >> 8)
buf.bytes[buf.writerIndex] = byte((x >> 16) | 0xc0)
buf.writerIndex += 3
} else if x < 0x10000000 {
buf.EnsureWrite(4)
buf.bytes[buf.writerIndex+3] = byte(x)
buf.bytes[buf.writerIndex+2] = byte(x >> 8)
buf.bytes[buf.writerIndex+1] = byte(x >> 16)
buf.bytes[buf.writerIndex] = byte((x >> 24) | 0xe0)
buf.writerIndex += 4
} else {
buf.EnsureWrite(5)
buf.bytes[buf.writerIndex] = 0xf0
buf.bytes[buf.writerIndex+4] = byte(x)
buf.bytes[buf.writerIndex+3] = byte(x >> 8)
buf.bytes[buf.writerIndex+2] = byte(x >> 16)
buf.bytes[buf.writerIndex+1] = byte(x >> 24)
buf.writerIndex += 5
}
}
func (buf *ByteBuf) ReadFint() (int32, error) {
if buf.readerIndex+4 > buf.writerIndex {
return 0, UnmarshalErr
}
x := int32(uint(buf.bytes[buf.readerIndex]) | (uint(buf.bytes[buf.readerIndex+1]) << 8) |
(uint(buf.bytes[buf.readerIndex+2]) << 16) | (uint(buf.bytes[buf.readerIndex+3]) << 24))
buf.readerIndex += 4
return x, nil
}
func (buf *ByteBuf) WriteFint(x int32) {
buf.EnsureWrite(4)
buf.bytes[buf.writerIndex] = byte(x)
buf.bytes[buf.writerIndex+1] = byte(x >> 8)
buf.bytes[buf.writerIndex+2] = byte(x >> 16)
buf.bytes[buf.writerIndex+3] = byte(x >> 24)
buf.writerIndex += 4
}
func (buf *ByteBuf) ReadLong() (int64, error) {
x, err := buf.ReadUlong()
return int64(x), err
}
func (buf *ByteBuf) WriteLong(x int64) {
buf.WriteUlong(uint64(x))
}
func (buf *ByteBuf) ReadUlong() (uint64, error) {
if buf.readerIndex >= buf.writerIndex {
return 0, UnmarshalErr
}
h := uint64(buf.bytes[buf.readerIndex])
if h < 0x80 {
buf.readerIndex++
return h, nil
} else if h < 0xc0 {
if buf.readerIndex+2 > buf.writerIndex {
return 0, UnmarshalErr
}
x := ((h & 0x3f) << 8) | uint64(buf.bytes[buf.readerIndex+1])
buf.readerIndex += 2
return x, nil
} else if h < 0xe0 {
if buf.readerIndex+3 > buf.writerIndex {
return 0, UnmarshalErr
}
x := ((h & 0x1f) << 16) | (uint64(buf.bytes[buf.readerIndex+1]) << 8) | uint64(buf.bytes[buf.readerIndex+2])
buf.readerIndex += 3
return x, nil
} else if h < 0xf0 {
if buf.readerIndex+4 > buf.writerIndex {
return 0, UnmarshalErr
}
x := ((h & 0x1f) << 24) | (uint64(buf.bytes[buf.readerIndex+1]) << 16) | (uint64(buf.bytes[buf.readerIndex+2]) << 8) |
uint64(buf.bytes[buf.readerIndex+3])
buf.readerIndex += 4
return x, nil
} else if h < 0xf8 {
if buf.readerIndex+5 > buf.writerIndex {
return 0, UnmarshalErr
}
x := ((h & 0x7) << 32) | (uint64(buf.bytes[buf.readerIndex+1]) << 24) | (uint64(buf.bytes[buf.readerIndex+2]) << 16) |
(uint64(buf.bytes[buf.readerIndex+3]) << 8) | uint64(buf.bytes[buf.readerIndex+4])
buf.readerIndex += 5
return x, nil
} else if h < 0xfc {
if buf.readerIndex+6 > buf.writerIndex {
return 0, UnmarshalErr
}
x := ((h & 0x3) << 40) | (uint64(buf.bytes[buf.readerIndex+1]) << 32) | (uint64(buf.bytes[buf.readerIndex+2]) << 24) |
(uint64(buf.bytes[buf.readerIndex+3]) << 16) | (uint64(buf.bytes[buf.readerIndex+4]) << 8) |
(uint64(buf.bytes[buf.readerIndex+5]))
buf.readerIndex += 6
return x, nil
} else if h < 0xfe {
if buf.readerIndex+7 > buf.writerIndex {
return 0, UnmarshalErr
}
x := ((h & 0x1) << 48) | (uint64(buf.bytes[buf.readerIndex+1]) << 40) | (uint64(buf.bytes[buf.readerIndex+2]) << 32) |
(uint64(buf.bytes[buf.readerIndex+3]) << 24) | (uint64(buf.bytes[buf.readerIndex+4]) << 16) |
(uint64(buf.bytes[buf.readerIndex+5]) << 8) | (uint64(buf.bytes[buf.readerIndex+6]))
buf.readerIndex += 7
return x, nil
} else if h < 0xff {
if buf.readerIndex+8 > buf.writerIndex {
return 0, UnmarshalErr
}
x := (uint64(buf.bytes[buf.readerIndex+1]) << 48) | (uint64(buf.bytes[buf.readerIndex+2]) << 40) |
(uint64(buf.bytes[buf.readerIndex+3]) << 32) | (uint64(buf.bytes[buf.readerIndex+4]) << 24) |
(uint64(buf.bytes[buf.readerIndex+5]) << 16) | (uint64(buf.bytes[buf.readerIndex+6]) << 8) |
(uint64(buf.bytes[buf.readerIndex+7]))
buf.readerIndex += 8
return x, nil
} else {
if buf.readerIndex+9 > buf.writerIndex {
return 0, UnmarshalErr
}
x := (uint64(buf.bytes[buf.readerIndex+1]) << 56) | (uint64(buf.bytes[buf.readerIndex+2]) << 48) |
(uint64(buf.bytes[buf.readerIndex+3]) << 40) | (uint64(buf.bytes[buf.readerIndex+4]) << 32) |
(uint64(buf.bytes[buf.readerIndex+5]) << 24) | (uint64(buf.bytes[buf.readerIndex+6]) << 16) |
(uint64(buf.bytes[buf.readerIndex+7]) << 8) | (uint64(buf.bytes[buf.readerIndex+8]))
buf.readerIndex += 9
return x, nil
}
}
func (buf *ByteBuf) WriteUlong(x uint64) {
if x < 0x80 {
buf.EnsureWrite(1)
buf.bytes[buf.writerIndex] = byte(x)
buf.writerIndex++
} else if x < 0x4000 {
buf.EnsureWrite(2)
buf.bytes[buf.writerIndex+1] = byte(x)
buf.bytes[buf.writerIndex] = byte((x >> 8) | 0x80)
buf.writerIndex += 2
} else if x < 0x200000 {
buf.EnsureWrite(3)
buf.bytes[buf.writerIndex+2] = byte(x)
buf.bytes[buf.writerIndex+1] = byte(x >> 8)
buf.bytes[buf.writerIndex] = byte((x >> 16) | 0xc0)
buf.writerIndex += 3
} else if x < 0x10000000 {
buf.EnsureWrite(4)
buf.bytes[buf.writerIndex+3] = byte(x)
buf.bytes[buf.writerIndex+2] = byte(x >> 8)
buf.bytes[buf.writerIndex+1] = byte(x >> 16)
buf.bytes[buf.writerIndex] = byte((x >> 24) | 0xe0)
buf.writerIndex += 4
} else if x < 0x800000000 {
buf.EnsureWrite(5)
buf.bytes[buf.writerIndex+4] = byte(x)
buf.bytes[buf.writerIndex+3] = byte(x >> 8)
buf.bytes[buf.writerIndex+2] = byte(x >> 16)
buf.bytes[buf.writerIndex+1] = byte(x >> 24)
buf.bytes[buf.writerIndex] = byte((x >> 32) | 0xf0)
buf.writerIndex += 5
} else if x < 0x40000000000 {
buf.EnsureWrite(6)
buf.bytes[buf.writerIndex+5] = byte(x)
buf.bytes[buf.writerIndex+4] = byte(x >> 8)
buf.bytes[buf.writerIndex+3] = byte(x >> 16)
buf.bytes[buf.writerIndex+2] = byte(x >> 24)
buf.bytes[buf.writerIndex+1] = byte(x >> 32)
buf.bytes[buf.writerIndex] = byte((x >> 40) | 0xf8)
buf.writerIndex += 6
} else if x < 0x200000000000 {
buf.EnsureWrite(7)
buf.bytes[buf.writerIndex+6] = byte(x)
buf.bytes[buf.writerIndex+5] = byte(x >> 8)
buf.bytes[buf.writerIndex+4] = byte(x >> 16)
buf.bytes[buf.writerIndex+3] = byte(x >> 24)
buf.bytes[buf.writerIndex+2] = byte(x >> 32)
buf.bytes[buf.writerIndex+1] = byte(x >> 40)
buf.bytes[buf.writerIndex] = byte((x >> 48) | 0xfc)
buf.writerIndex += 7
} else if x < 0x100000000000000 {
buf.EnsureWrite(8)
buf.bytes[buf.writerIndex+7] = byte(x)
buf.bytes[buf.writerIndex+6] = byte(x >> 8)
buf.bytes[buf.writerIndex+5] = byte(x >> 16)
buf.bytes[buf.writerIndex+4] = byte(x >> 24)
buf.bytes[buf.writerIndex+3] = byte(x >> 32)
buf.bytes[buf.writerIndex+2] = byte(x >> 40)
buf.bytes[buf.writerIndex+1] = byte(x >> 48)
buf.bytes[buf.writerIndex] = byte((x >> 56) | 0xfe)
buf.writerIndex += 8
} else {
buf.EnsureWrite(9)
buf.bytes[buf.writerIndex+8] = byte(x)
buf.bytes[buf.writerIndex+7] = byte(x >> 8)
buf.bytes[buf.writerIndex+6] = byte(x >> 16)
buf.bytes[buf.writerIndex+5] = byte(x >> 24)
buf.bytes[buf.writerIndex+4] = byte(x >> 32)
buf.bytes[buf.writerIndex+3] = byte(x >> 40)
buf.bytes[buf.writerIndex+2] = byte(x >> 48)
buf.bytes[buf.writerIndex+1] = byte(x >> 56)
buf.bytes[buf.writerIndex] = 0xff
buf.writerIndex += 9
}
}
func (buf *ByteBuf) ReadFlong() (int64, error) {
if buf.readerIndex+8 > buf.writerIndex {
return 0, UnmarshalErr
}
x := (uint64(buf.bytes[buf.readerIndex+7]) << 56) | (uint64(buf.bytes[buf.readerIndex+6]) << 48) |
(uint64(buf.bytes[buf.readerIndex+5]) << 40) | (uint64(buf.bytes[buf.readerIndex+4]) << 32) |
(uint64(buf.bytes[buf.readerIndex+3]) << 24) | (uint64(buf.bytes[buf.readerIndex+2]) << 16) |
(uint64(buf.bytes[buf.readerIndex+1]) << 8) | (uint64(buf.bytes[buf.readerIndex]))
buf.readerIndex += 8
return int64(x), nil
}
func (buf *ByteBuf) WriteFlong(x int64) {
buf.EnsureWrite(8)
buf.bytes[buf.writerIndex] = byte(x)
buf.bytes[buf.writerIndex+1] = byte(x >> 8)
buf.bytes[buf.writerIndex+2] = byte(x >> 16)
buf.bytes[buf.writerIndex+3] = byte(x >> 24)
buf.bytes[buf.writerIndex+4] = byte(x >> 32)
buf.bytes[buf.writerIndex+5] = byte(x >> 40)
buf.bytes[buf.writerIndex+6] = byte(x >> 48)
buf.bytes[buf.writerIndex+7] = byte(x >> 56)
buf.writerIndex += 8
}
func (buf *ByteBuf) ReadFloat() (float32, error) {
if x, err := buf.ReadFint(); err == nil {
return math.Float32frombits(uint32(x)), nil
} else {
return 0, err
}
}
func (buf *ByteBuf) WriteFloat(x float32) {
buf.WriteFint(int32(math.Float32bits(x)))
}
func (buf *ByteBuf) ReadDouble() (float64, error) {
if x, err := buf.ReadFlong(); err == nil {
return math.Float64frombits(uint64(x)), nil
} else {
return 0, err
}
}
func (buf *ByteBuf) WriteDouble(x float64) {
buf.WriteFlong(int64(math.Float64bits(x)))
}
func (buf *ByteBuf) ReadSize() (int, error) {
x, err := buf.ReadUint()
return int(x), err
}
func (buf *ByteBuf) WriteSize(x int) {
buf.WriteUint(uint32(x))
}
// marshal int
// n -> (n << 1) ^ (n >> 31)
// Read
// (x >>> 1) ^ ((x << 31) >> 31)
// (x >>> 1) ^ -(n&1)
func (buf *ByteBuf) ReadSint() (int32, error) {
if x, err := buf.ReadUint(); err == nil {
return int32((x >> 1) ^ ((x & 1) << 31)), nil
} else {
return 0, err
}
}
func (buf *ByteBuf) WriteSint(x int32) {
buf.WriteUint((uint32(x) << 1) ^ (uint32(x) >> 31))
}
// marshal long
// n -> (n << 1) ^ (n >> 63)
// Read
// (x >>> 1) ^((x << 63) >> 63)
// (x >>> 1) ^ -(n&1L)
func (buf *ByteBuf) ReadSlong() (int64, error) {
if x, err := buf.ReadUlong(); err == nil {
return int64((x >> 1) ^ ((x & 1) << 63)), nil
} else {
return 0, err
}
}
func (buf *ByteBuf) WriteSlong(x int64) {
buf.WriteUlong((uint64(x) << 1) ^ (uint64(x) >> 31))
}
func (buf *ByteBuf) WriteString(x string) {
bs := []byte(x)
buf.WriteSize(len(bs))
buf.WriteBytesWithoutSize(bs)
}
func (buf *ByteBuf) ReadString() (string, error) {
if size, err := buf.ReadSize(); err == nil {
if buf.readerIndex+size > buf.writerIndex {
return "", UnmarshalErr
}
s := string(buf.bytes[buf.readerIndex : buf.readerIndex+size])
buf.readerIndex += size
return s, nil
} else {
return "", err
}
}
func (buf *ByteBuf) ReadBytes() ([]byte, error) {
if size, err := buf.ReadSize(); err == nil {
if size == 0 {
return EmptyBytes, nil
} else if buf.readerIndex+size > buf.writerIndex {
return nil, UnmarshalErr
} else {
bs := make([]byte, size)
copy(bs, buf.bytes[buf.readerIndex:buf.readerIndex+size])
buf.readerIndex += size
return bs, nil
}
} else {
return nil, err
}
}
func (buf *ByteBuf) WriteBytes(x []byte) {
size := len(x)
buf.WriteSize(size)
if size > 0 {
buf.EnsureWrite(size)
copy(buf.bytes[buf.writerIndex:], x)
buf.writerIndex += size
}
}
func (buf *ByteBuf) WriteBytesWithSize(x []byte) {
buf.WriteBytes(x)
}
func (buf *ByteBuf) WriteBytesWithoutSize(x []byte) {
size := len(x)
buf.EnsureWrite(size)
copy(buf.bytes[buf.writerIndex:], x)
buf.writerIndex += size
}
func (buf *ByteBuf) ReadVector2() (Vector2, error) {
if x, err := buf.ReadFloat(); err == nil {
if y, err2 := buf.ReadFloat(); err2 == nil {
return Vector2{X: x, Y: y}, nil
}
}
return Vector2{}, UnmarshalErr
}
func (buf *ByteBuf) WriteVector2(x Vector2) {
buf.WriteFloat(x.X)
buf.WriteFloat(x.Y)
}
func (buf *ByteBuf) ReadVector3() (Vector3, error) {
if x, err := buf.ReadFloat(); err == nil {
if y, err2 := buf.ReadFloat(); err2 == nil {
if z, err3 := buf.ReadFloat(); err3 == nil {
return Vector3{X: x, Y: y, Z: z}, nil
}
}
}
return Vector3{}, UnmarshalErr
}
func (buf *ByteBuf) WriteVector3(x Vector3) {
buf.WriteFloat(x.X)
buf.WriteFloat(x.Y)
buf.WriteFloat(x.Z)
}
func (buf *ByteBuf) ReadVector4() (Vector4, error) {
if x, err := buf.ReadFloat(); err == nil {
if y, err2 := buf.ReadFloat(); err2 == nil {
if z, err3 := buf.ReadFloat(); err3 == nil {
if w, err4 := buf.ReadFloat(); err4 == nil {
return Vector4{X: x, Y: y, Z: z, W: w}, nil
}
}
}
}
return Vector4{}, UnmarshalErr
}
func (buf *ByteBuf) WriteVector4(x Vector4) {
buf.WriteFloat(x.X)
buf.WriteFloat(x.Y)
buf.WriteFloat(x.Z)
buf.WriteFloat(x.W)
}

View File

@ -0,0 +1,7 @@
package serialization
type ISerializable interface {
GetTypeId() int32
Serialize(buf *ByteBuf)
Deserialize(buf *ByteBuf) error
}

View File

@ -0,0 +1,10 @@
package serialization
type Vector2 struct {
X float32
Y float32
}
func NewVector2(x float32, y float32) Vector2 {
return Vector2{X: x, Y: y}
}

View File

@ -0,0 +1,11 @@
package serialization
type Vector3 struct {
X float32
Y float32
Z float32
}
func NewVector3(x float32, y float32, z float32) Vector3 {
return Vector3{X: x, Y: y, Z: z}
}

View File

@ -0,0 +1,12 @@
package serialization
type Vector4 struct {
X float32
Y float32
Z float32
W float32
}
func NewVector4(x float32, y float32, z float32, w float32) Vector4 {
return Vector4{X: x, Y: y, Z: z, W: w}
}

View File

@ -0,0 +1,205 @@
package serialization
import (
"bytes"
"testing"
)
func TestMarshal(t *testing.T) {
buf := NewByteBuf(10)
for i := 0; i < 2; i++ {
x := i != 0
buf.WriteBool(x)
if v, err := buf.ReadBool(); err != nil || v != x {
t.Fatalf("expect %v, get %v", x, v)
}
if buf.Size() != 0 {
t.Fatalf("expect empty. but size:%v, x:%v", buf.Size(), x)
}
}
for i := 0; i < 256; i = i*3/2 + 1 {
x := byte(i)
buf.WriteByte(x)
if v, err := buf.ReadByte(); err != nil || v != x {
t.Fatalf("expect %v, get %v", x, v)
}
if buf.Size() != 0 {
t.Fatalf("expect empty. but size:%v, x:%v", buf.Size(), x)
}
}
for i := 0; i < 0x10000; i = i*3/2 + 1 {
x := int16(i)
buf.WriteShort(x)
if v, err := buf.ReadShort(); err != nil || v != x {
t.Fatalf("expect %v, get %v", x, v)
}
if buf.Size() != 0 {
t.Fatalf("expect empty. but size:%v, x:%v", buf.Size(), x)
}
}
for i := 0; i < 0x10000; i = i*3/2 + 1 {
x := int16(i)
buf.WriteFshort(x)
if v, err := buf.ReadFshort(); err != nil || v != x {
t.Fatalf("expect %v, get %v", x, v)
}
if buf.Size() != 0 {
t.Fatalf("expect empty. but size:%v, x:%v", buf.Size(), x)
}
}
for i := 0; i < 0x1000000000; i = i*3/2 + 1 {
x := int32(i)
buf.WriteInt(x)
if v, err := buf.ReadInt(); err != nil || v != x {
t.Fatalf("expect %v, get %v", x, v)
}
if buf.Size() != 0 {
t.Fatalf("expect empty. but size:%v, x:%v", buf.Size(), x)
}
}
for i := 0; i < 0x1000000000; i = i*3/2 + 1 {
x := int32(i)
buf.WriteFint(x)
if v, err := buf.ReadFint(); err != nil || v != x {
t.Fatalf("expect %v, get %v", x, v)
}
if buf.Size() != 0 {
t.Fatalf("expect empty. but size:%v, x:%v", buf.Size(), x)
}
}
for i := 0; i < 0x100000000; i = i*3/2 + 1 {
x := int(i)
buf.WriteSize(x)
if v, err := buf.ReadSize(); err != nil || v != x {
t.Fatalf("expect %v, get %v", x, v)
}
if buf.Size() != 0 {
t.Fatalf("expect empty. but size:%v, x:%v", buf.Size(), x)
}
}
for i := 0; i < 0x100000000; i = i*3/2 + 1 {
x := int32(i)
buf.WriteSint(x)
if v, err := buf.ReadSint(); err != nil || v != x {
t.Fatalf("expect %v, get %v", x, v)
}
if buf.Size() != 0 {
t.Fatalf("expect empty. but size:%v, x:%v", buf.Size(), x)
}
}
for i := 0; i < 0x1000000000000000; i = i*3/2 + 1 {
x := int64(i)
buf.WriteLong(x)
if v, err := buf.ReadLong(); err != nil || v != x {
t.Fatalf("expect %v, get %v", x, v)
}
if buf.Size() != 0 {
t.Fatalf("expect empty. but size:%v, x:%v", buf.Size(), x)
}
x = -x
buf.WriteLong(x)
if v, err := buf.ReadLong(); err != nil || v != x {
t.Fatalf("expect %v, get %v", x, v)
}
if buf.Size() != 0 {
t.Fatalf("expect empty. but size:%v, x:%v", buf.Size(), x)
}
}
for i := 0; i < 0x100000000; i = i*3/2 + 1 {
x := float32(i)
buf.WriteFloat(x)
if v, err := buf.ReadFloat(); err != nil || v != x {
t.Fatalf("expect %v, get %v", x, v)
}
if buf.Size() != 0 {
t.Fatalf("expect empty. but size:%v, x:%v", buf.Size(), x)
}
}
for i := 0; i < 0x100000000; i = i*3/2 + 1 {
x := float64(i)
buf.WriteDouble(x)
if v, err := buf.ReadDouble(); err != nil || v != x {
t.Fatalf("expect %v, get %v", x, v)
}
if buf.Size() != 0 {
t.Fatalf("expect empty. but size:%v, x:%v", buf.Size(), x)
}
}
{
x := "walon"
buf.WriteString(x)
if v, err := buf.ReadString(); err != nil || v != x {
t.Fatalf("expect %v, get %v", x, v)
}
if buf.Size() != 0 {
t.Fatalf("expect empty. but size:%v, x:%v", buf.Size(), x)
}
}
{
x := Vector2{X: 1, Y: 2}
buf.WriteVector2(x)
if v, err := buf.ReadVector2(); err != nil || v != x {
t.Fatalf("expect %v, get %v", x, v)
}
if buf.Size() != 0 {
t.Fatalf("expect empty. but size:%v, x:%v", buf.Size(), x)
}
}
{
x := Vector3{X: 1, Y: 2, Z: 3}
buf.WriteVector3(x)
if v, err := buf.ReadVector3(); err != nil || v != x {
t.Fatalf("expect %v, get %v", x, v)
}
if buf.Size() != 0 {
t.Fatalf("expect empty. but size:%v, x:%v", buf.Size(), x)
}
}
{
x := Vector4{X: 1, Y: 2, Z: 3, W: 4}
buf.WriteVector4(x)
if v, err := buf.ReadVector4(); err != nil || v != x {
t.Fatalf("expect %v, get %v", x, v)
}
if buf.Size() != 0 {
t.Fatalf("expect empty. but size:%v, x:%v", buf.Size(), x)
}
}
{
x := []byte{1, 2, 3}
buf.WriteBytes(x)
if v, err := buf.ReadBytes(); err != nil || !bytes.Equal(x, v) {
t.Fatalf("expect %v, get %v", x, v)
}
if buf.Size() != 0 {
t.Fatalf("expect empty. but size:%v, x:%v", buf.Size(), x)
}
}
{
x := []byte{1, 2, 3, 4}
buf.WriteBytesWithoutSize(x)
if v, err := buf.ReadFint(); err != nil || v != 0x04030201 {
t.Fatalf("expect %v, get %v", x, v)
}
if buf.Size() != 0 {
t.Fatalf("expect empty. but size:%v, x:%v", buf.Size(), x)
}
}
}

View File

@ -0,0 +1,45 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
package cfg
import "errors"
type MyBeanItems struct {
Id int32
Name string
Desc string
Price int32
ExpireTime *int32
BatchUseable bool
}
const TypeId_MyBeanItems = -1804629298
func (*MyBeanItems) GetTypeId() int32 {
return -1804629298
}
func (_v *MyBeanItems)Deserialize(_buf map[string]interface{}) (err error) {
{ var _ok_ bool; var _tempNum_ float64; if _tempNum_, _ok_ = _buf["id"].(float64); !_ok_ { err = errors.New("id error"); return }; _v.Id = int32(_tempNum_) }
{ var _ok_ bool; if _v.Name, _ok_ = _buf["name"].(string); !_ok_ { err = errors.New("name error"); return } }
{ var _ok_ bool; if _v.Desc, _ok_ = _buf["desc"].(string); !_ok_ { err = errors.New("desc error"); return } }
{ var _ok_ bool; var _tempNum_ float64; if _tempNum_, _ok_ = _buf["price"].(float64); !_ok_ { err = errors.New("price error"); return }; _v.Price = int32(_tempNum_) }
{ var _ok_ bool; var __json_expire_time__ interface{}; if __json_expire_time__, _ok_ = _buf["expire_time"]; !_ok_ || __json_expire_time__ == nil { _v.ExpireTime = nil } else { var __x__ int32; { var _ok_ bool; var _x_ float64; if _x_, _ok_ = __json_expire_time__.(float64); !_ok_ { err = errors.New("__x__ error"); return }; __x__ = int32(_x_) }; _v.ExpireTime = &__x__ }}
{ var _ok_ bool; if _v.BatchUseable, _ok_ = _buf["batch_useable"].(bool); !_ok_ { err = errors.New("batch_useable error"); return } }
return
}
func DeserializeMyBeanItems(_buf map[string]interface{}) (*MyBeanItems, error) {
v := &MyBeanItems{}
if err := v.Deserialize(_buf); err == nil {
return v, nil
} else {
return nil, err
}
}

View File

@ -0,0 +1,45 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
package cfg
import "errors"
type MyBeanRewards struct {
Id int32
Name string
Desc string
Price int32
ExpireTime *int32
BatchUseable bool
}
const TypeId_MyBeanRewards = -97538382
func (*MyBeanRewards) GetTypeId() int32 {
return -97538382
}
func (_v *MyBeanRewards)Deserialize(_buf map[string]interface{}) (err error) {
{ var _ok_ bool; var _tempNum_ float64; if _tempNum_, _ok_ = _buf["id"].(float64); !_ok_ { err = errors.New("id error"); return }; _v.Id = int32(_tempNum_) }
{ var _ok_ bool; if _v.Name, _ok_ = _buf["name"].(string); !_ok_ { err = errors.New("name error"); return } }
{ var _ok_ bool; if _v.Desc, _ok_ = _buf["desc"].(string); !_ok_ { err = errors.New("desc error"); return } }
{ var _ok_ bool; var _tempNum_ float64; if _tempNum_, _ok_ = _buf["price"].(float64); !_ok_ { err = errors.New("price error"); return }; _v.Price = int32(_tempNum_) }
{ var _ok_ bool; var __json_expire_time__ interface{}; if __json_expire_time__, _ok_ = _buf["expire_time"]; !_ok_ || __json_expire_time__ == nil { _v.ExpireTime = nil } else { var __x__ int32; { var _ok_ bool; var _x_ float64; if _x_, _ok_ = __json_expire_time__.(float64); !_ok_ { err = errors.New("__x__ error"); return }; __x__ = int32(_x_) }; _v.ExpireTime = &__x__ }}
{ var _ok_ bool; if _v.BatchUseable, _ok_ = _buf["batch_useable"].(bool); !_ok_ { err = errors.New("batch_useable error"); return } }
return
}
func DeserializeMyBeanRewards(_buf map[string]interface{}) (*MyBeanRewards, error) {
v := &MyBeanRewards{}
if err := v.Deserialize(_buf); err == nil {
return v, nil
} else {
return nil, err
}
}

View File

@ -0,0 +1,36 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
package cfg
type JsonLoader func(string) ([]map[string]interface{}, error)
type Tables struct {
TbRewards *TbRewards
TbItem *TbItem
}
func NewTables(loader JsonLoader) (*Tables, error) {
var err error
var buf []map[string]interface{}
tables := &Tables{}
if buf, err = loader("tbrewards") ; err != nil {
return nil, err
}
if tables.TbRewards, err = NewTbRewards(buf) ; err != nil {
return nil, err
}
if buf, err = loader("tbitem") ; err != nil {
return nil, err
}
if tables.TbItem, err = NewTbItem(buf) ; err != nil {
return nil, err
}
return tables, nil
}

View File

@ -0,0 +1,42 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
package cfg
type TbItem struct {
_dataMap map[int32]*MyBeanItems
_dataList []*MyBeanItems
}
func NewTbItem(_buf []map[string]interface{}) (*TbItem, error) {
_dataList := make([]*MyBeanItems, 0, len(_buf))
dataMap := make(map[int32]*MyBeanItems)
for _, _ele_ := range _buf {
if _v, err2 := DeserializeMyBeanItems(_ele_); err2 != nil {
return nil, err2
} else {
_dataList = append(_dataList, _v)
dataMap[_v.Id] = _v
}
}
return &TbItem{_dataList:_dataList, _dataMap:dataMap}, nil
}
func (table *TbItem) GetDataMap() map[int32]*MyBeanItems {
return table._dataMap
}
func (table *TbItem) GetDataList() []*MyBeanItems {
return table._dataList
}
func (table *TbItem) Get(key int32) *MyBeanItems {
return table._dataMap[key]
}

View File

@ -0,0 +1,42 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
package cfg
type TbRewards struct {
_dataMap map[int32]*MyBeanRewards
_dataList []*MyBeanRewards
}
func NewTbRewards(_buf []map[string]interface{}) (*TbRewards, error) {
_dataList := make([]*MyBeanRewards, 0, len(_buf))
dataMap := make(map[int32]*MyBeanRewards)
for _, _ele_ := range _buf {
if _v, err2 := DeserializeMyBeanRewards(_ele_); err2 != nil {
return nil, err2
} else {
_dataList = append(_dataList, _v)
dataMap[_v.Id] = _v
}
}
return &TbRewards{_dataList:_dataList, _dataMap:dataMap}, nil
}
func (table *TbRewards) GetDataMap() map[int32]*MyBeanRewards {
return table._dataMap
}
func (table *TbRewards) GetDataList() []*MyBeanRewards {
return table._dataList
}
func (table *TbRewards) Get(key int32) *MyBeanRewards {
return table._dataMap[key]
}