Start TEngine3.0

Start TEngine3.0
This commit is contained in:
ALEXTANG
2023-03-31 17:27:49 +08:00
parent 179765c43c
commit 36353294d6
1032 changed files with 21868 additions and 102407 deletions

View File

@@ -0,0 +1,108 @@
{{
is_value_type = x.is_value_type
is_abstract_type = x.is_abstract_type
name = x.name
full_name = x.full_name
parent_def_type = x.parent_def_type
parent = x.parent
fields = x.fields
hierarchy_fields = x.hierarchy_fields
}}
using Bright.Serialization;
namespace {{x.namespace_with_top_module}}
{
{{~if x.comment != '' ~}}
/// <summary>
/// {{x.escape_comment}}
/// </summary>
{{~end~}}
public {{if is_value_type}}struct{{else}}{{x.cs_class_modifier}} class{{end}} {{name}} : {{if parent_def_type}} {{parent}} {{else}} Bright.Serialization.BeanBase {{end}}
{
{{~if !is_value_type~}}
public {{name}}()
{
}
{{~end~}}
public {{name}}(Bright.Common.NotNullInitialization _) {{if parent_def_type}} : base(_) {{end}}
{
{{~ for field in fields ~}}
{{~if cs_need_init field.ctype~}}
{{cs_init_field_ctor_value field.convention_name field.ctype}}
{{~else if is_value_type~}}
{{field.convention_name}} = default;
{{~end~}}
{{~end~}}
}
public static void Serialize{{name}}(ByteBuf _buf, {{name}} x)
{
{{~if is_abstract_type~}}
_buf.WriteInt(x.GetTypeId());
x.Serialize(_buf);
{{~else~}}
x.Serialize(_buf);
{{~end~}}
}
public static {{name}} Deserialize{{name}}(ByteBuf _buf)
{
{{~if is_abstract_type~}}
{{full_name}} x;
switch (_buf.ReadInt())
{
{{~ for child in x.hierarchy_not_abstract_children~}}
case {{child.full_name}}.__ID__: x = new {{child.full_name}}(); break;
{{~end~}}
default: throw new SerializationException();
}
x.Deserialize(_buf);
{{~else~}}
var x = new {{full_name}}();
x.Deserialize(_buf);
{{~end~}}
return x;
}
{{~ for field in fields ~}}
{{~if field.comment != '' ~}}
/// <summary>
/// {{field.escape_comment}}
/// </summary>
{{~end~}}
public {{cs_define_type field.ctype}} {{field.convention_name}};
{{~end~}}
{{~if !is_abstract_type~}}
public const int __ID__ = {{x.id}};
public override int GetTypeId() => __ID__;
public override void Serialize(ByteBuf _buf)
{
{{~ for field in hierarchy_fields ~}}
{{cs_serialize '_buf' field.convention_name field.ctype}}
{{~end~}}
}
public override void Deserialize(ByteBuf _buf)
{
{{~ for field in hierarchy_fields ~}}
{{cs_deserialize '_buf' field.convention_name field.ctype}}
{{~end~}}
}
public override string ToString()
{
return "{{full_name}}{ "
{{~ for field in hierarchy_fields ~}}
+ "{{field.convention_name}}:" + {{cs_to_string field.convention_name field.ctype}} + ","
{{~end~}}
+ "}";
}
{{~end~}}
}
}

View File

@@ -0,0 +1,81 @@
{{
name = x.name
full_name = x.full_name
parent = x.parent
fields = x.fields
}}
using Bright.Serialization;
namespace {{x.namespace_with_top_module}}
{
{{~if x.comment != '' ~}}
/// <summary>
/// {{x.escape_comment}}
/// </summary>
{{~end~}}
public sealed class {{name}} : Bright.Net.Codecs.Protocol
{
{{~ for field in fields ~}}
{{~if field.comment != '' ~}}
/// <summary>
/// {{field.escape_comment}}
/// </summary>
{{~end~}}
public {{cs_define_type field.ctype}} {{field.convention_name}};
{{~end~}}
public {{name}}()
{
}
public {{name}}(Bright.Common.NotNullInitialization _)
{
{{~ for field in fields ~}}
{{~if cs_need_init field.ctype~}}
{{cs_init_field_ctor_value field.convention_name field.ctype}}
{{~end~}}
{{~end~}}
}
public const int __ID__ = {{x.id}};
public override int GetTypeId()
{
return __ID__;
}
public override void Serialize(ByteBuf _buf)
{
{{~ for field in fields ~}}
{{cs_serialize '_buf' field.convention_name field.ctype}}
{{~end~}}
}
public override void Deserialize(ByteBuf _buf)
{
{{~ for field in fields ~}}
{{cs_deserialize '_buf' field.convention_name field.ctype}}
{{~end~}}
}
public override void Reset()
{
throw new System.NotImplementedException();
}
public override object Clone()
{
throw new System.NotImplementedException();
}
public override string ToString()
{
return "{{full_name}}{ "
{{~ for field in fields ~}}
+ "{{field.convention_name}}:" + {{cs_to_string field.convention_name field.ctype}} + ","
{{~end~}}
+ "}";
}
}
}

View File

@@ -0,0 +1,47 @@
{{
name = x.name
full_name = x.full_name
parent = x.parent
fields = x.fields
targ_type = x.targ_type
tres_type = x.tres_type
}}
using Bright.Serialization;
namespace {{x.namespace_with_top_module}}
{
{{~if x.comment != '' ~}}
/// <summary>
/// {{x.escape_comment}}
/// </summary>
{{~end~}}
public sealed class {{name}} : Bright.Net.Codecs.Rpc<{{cs_define_type targ_type}}, {{cs_define_type tres_type}}>
{
public {{name}}()
{
}
public const int __ID__ = {{x.id}};
public override int GetTypeId()
{
return __ID__;
}
public override void Reset()
{
throw new System.NotImplementedException();
}
public override object Clone()
{
throw new System.NotImplementedException();
}
public override string ToString()
{
return $"{{full_name}}{%{ {{arg:{Arg},res:{Res} }} }%}";
}
}
}

View File

@@ -0,0 +1,19 @@
using Bright.Serialization;
namespace {{namespace}}
{
public static class {{name}}
{
public static System.Collections.Generic.Dictionary<int, Bright.Net.Codecs.ProtocolCreator> Factories { get; } = new System.Collections.Generic.Dictionary<int, Bright.Net.Codecs.ProtocolCreator>
{
{{~ for proto in protos ~}}
[{{proto.full_name}}.__ID__] = () => new {{proto.full_name}}(),
{{~end~}}
{{~ for rpc in rpcs ~}}
[{{rpc.full_name}}.__ID__] = () => new {{rpc.full_name}}(),
{{~end~}}
};
}
}

View File

@@ -0,0 +1,74 @@
{{-
go_full_name = x.go_full_name
parent_def_type = x.parent_def_type
is_abstract_type = x.is_abstract_type
hierarchy_fields = x.hierarchy_fields
hierarchy_not_abstract_children = x.hierarchy_not_abstract_children
-}}
package {{x.top_module}}
import (
"bright/serialization"
)
{{x.go_bin_import}}
type {{go_full_name}} struct {
{{~for field in hierarchy_fields ~}}
{{field.convention_name}} {{go_define_type field.ctype}}
{{~end~}}
}
const TypeId_{{go_full_name}} = {{x.id}}
func (*{{go_full_name}}) GetTypeId() int32 {
return {{x.id}}
}
func (_v *{{go_full_name}})Serialize(_buf *serialization.ByteBuf) {
{{~for field in hierarchy_fields ~}}
{{go_serialize_field field.ctype ("_v." + field.convention_name) '_buf'}}
{{~end~}}
}
func (_v *{{go_full_name}})Deserialize(_buf *serialization.ByteBuf) (err error) {
{{~for field in hierarchy_fields ~}}
{{go_deserialize_field field.ctype ("_v." + field.convention_name) '_buf' 'err'}}
{{~end~}}
return
}
{{~if is_abstract_type~}}
func Serialize{{go_full_name}}(_v interface{}, _buf *serialization.ByteBuf) {
_b := _v.(serialization.ISerializable)
_buf.WriteInt(_b.GetTypeId())
_b.Serialize(_buf)
}
func Deserialize{{go_full_name}}(_buf *serialization.ByteBuf) (_v serialization.ISerializable, err error) {
var id int32
if id, err = _buf.ReadInt() ; err != nil {
return
}
switch id {
{{~for child in hierarchy_not_abstract_children~}}
case {{child.id}}: _v = &{{child.go_full_name}}{}; if err = _v.Deserialize(_buf); err != nil { return nil, err } else { return }
{{~end~}}
default: return nil, errors.New("unknown type id")
}
}
{{~else~}}
func Serialize{{go_full_name}}(_v serialization.ISerializable, _buf *serialization.ByteBuf) {
_v.Serialize(_buf)
}
func Deserialize{{go_full_name}}(_buf *serialization.ByteBuf) (*{{go_full_name}}, error) {
v := &{{go_full_name}}{}
if err := v.Deserialize(_buf); err == nil {
return v, nil
} else {
return nil, err
}
}
{{~end~}}

View File

@@ -0,0 +1,40 @@
{{-
go_full_name = x.go_full_name
parent_def_type = x.parent_def_type
is_abstract_type = x.is_abstract_type
fields = x.fields
hierarchy_not_abstract_children = x.hierarchy_not_abstract_children
-}}
package {{x.top_module}}
import (
"bright/serialization"
)
{{x.go_bin_import}}
type {{go_full_name}} struct {
{{~for field in fields ~}}
{{field.convention_name}} {{go_define_type field.ctype}}
{{~end~}}
}
const TypeId_{{go_full_name}} = {{x.id}}
func (*{{go_full_name}}) GetTypeId() int32 {
return {{x.id}}
}
func (_v *{{go_full_name}})Serialize(_buf *serialization.ByteBuf) {
{{~for field in fields ~}}
{{go_serialize_field field.ctype ("_v." + field.convention_name) '_buf'}}
{{~end~}}
}
func (_v *{{go_full_name}})Deserialize(_buf *serialization.ByteBuf) (err error) {
{{~for field in fields ~}}
{{go_deserialize_field field.ctype ("_v." + field.convention_name) '_buf' 'err'}}
{{~end~}}
return
}

View File

@@ -0,0 +1,51 @@
{{-
go_full_name = x.go_full_name
parent_def_type = x.parent_def_type
is_abstract_type = x.is_abstract_type
hierarchy_fields = x.hierarchy_fields
hierarchy_not_abstract_children = x.hierarchy_not_abstract_children
arg_type = x.targ_type
res_type = x.tres_type
-}}
package {{x.top_module}}
import (
"bright/serialization"
"errors"
)
{{x.go_bin_import}}
type {{go_full_name}} struct {
SeqId int64
Arg {{go_define_type arg_type}}
Res {{go_define_type res_type}}
}
const TypeId_{{go_full_name}} = {{x.id}}
func (*{{go_full_name}}) GetTypeId() int32 {
return {{x.id}}
}
func (_v *{{go_full_name}})Serialize(_buf *serialization.ByteBuf) {
_buf.WriteLong(_v.SeqId)
if _v.SeqId & 0x1 == 0 {
{{go_serialize_field arg_type "_v.Arg" '_buf'}}
} else {
{{go_serialize_field res_type "_v.Res" '_buf'}}
}
}
func (_v *{{go_full_name}})Deserialize(_buf *serialization.ByteBuf) (err error) {
if _v.SeqId, err = _buf.ReadLong() ; err != nil {
return
}
if _v.SeqId & 0x1 == 0 {
{{go_deserialize_field arg_type "_v.Arg" '_buf' 'err'}}
} else {
{{go_deserialize_field res_type "_v.Res" '_buf' 'err'}}
}
return
}

View File

@@ -0,0 +1,17 @@
package {{namespace}}
import "bright/net"
type ProtocolFactory = func () net.Protocol
var ProtocolStub map[int]ProtocolFactory
func init() {
ProtocolStub = make(map[int]ProtocolFactory)
{{~for p in protos~}}
ProtocolStub[{{p.id}}] = func () net.Protocol { return &{{p.go_full_name}}{} }
{{~end~}}
{{~for r in rpcs~}}
ProtocolStub[{{r.id}}] = func () net.Protocol { return &{{r.go_full_name}}{} }
{{~end~}}
}

View File

@@ -0,0 +1,245 @@
{{
enums = x.enums
beans = x.beans
protos = x.protos
}}
local setmetatable = setmetatable
local pairs = pairs
local ipairs = ipairs
local tinsert = table.insert
local function SimpleClass()
local class = {}
class.__index = class
class.New = function(...)
local ctor = class.ctor
local o = ctor and ctor(...) or {}
setmetatable(o, class)
return o
end
return class
end
local function get_map_size(m)
local n = 0
for _ in pairs(m) do
n = n + 1
end
return n
end
local enums =
{
{{~ for c in enums ~}}
---@class {{c.full_name}}
{{~ for item in c.items ~}}
---@field public {{item.name}} int
{{~end~}}
['{{c.full_name}}'] = { {{ for item in c.items }} {{item.name}}={{item.int_value}}, {{end}} };
{{~end~}}
}
local function InitTypes(methods)
local readBool = methods.readBool
local writeBool = methods.writeBool
local readByte = methods.readByte
local writeByte = methods.writeByte
local readShort = methods.readShort
local writeShort = methods.writeShort
local readFshort = methods.readFshort
local writeInt = methods.writeInt
local readInt = methods.readInt
local writeFint = methods.writeFint
local readFint = methods.readFint
local readLong = methods.readLong
local writeLong = methods.writeLong
local readFlong = methods.readFlong
local writeFlong = methods.writeFlong
local readFloat = methods.readFloat
local writeFloat = methods.writeFloat
local readDouble = methods.readDouble
local writeDouble = methods.writeDouble
local readSize = methods.readSize
local writeSize = methods.writeSize
local readString = methods.readString
local writeString = methods.writeString
local readBytes = methods.readBytes
local writeBytes = methods.writeBytes
local function readVector2(bs)
return { x = readFloat(bs), y = readFloat(bs) }
end
local function writeVector2(bs, v)
writeFloat(bs, v.x)
writeFloat(bs, v.y)
end
local function readVector3(bs)
return { x = readFloat(bs), y = readFloat(bs), z = readFloat(bs) }
end
local function writeVector3(bs, v)
writeFloat(bs, v.x)
writeFloat(bs, v.y)
writeFloat(bs, v.z)
end
local function readVector4(bs)
return { x = readFloat(bs), y = readFloat(bs), z = readFloat(bs), w = readFloat(bs) }
end
local function writeVector4(bs, v)
writeFloat(bs, v.x)
writeFloat(bs, v.y)
writeFloat(bs, v.z)
writeFloat(bs, v.w)
end
local function writeList(bs, list, keyFun)
writeSize(bs, #list)
for _, v in pairs(list) do
keyFun(bs, v)
end
end
local function readList(bs, keyFun)
local list = {}
local v
for i = 1, readSize(bs) do
tinsert(list, keyFun(bs))
end
return list
end
local writeArray = writeList
local readArray = readList
local function writeSet(bs, set, keyFun)
writeSize(bs, #set)
for _, v in ipairs(set) do
keyFun(bs, v)
end
end
local function readSet(bs, keyFun)
local set = {}
local v
for i = 1, readSize(bs) do
tinsert(set, keyFun(bs))
end
return set
end
local function writeMap(bs, map, keyFun, valueFun)
writeSize(bs, get_map_size(map))
for k, v in pairs(map) do
keyFun(bs, k)
valueFun(bs, v)
end
end
local function readMap(bs, keyFun, valueFun)
local map = {}
for i = 1, readSize(bs) do
local k = keyFun(bs)
local v = valueFun(bs)
map[k] = v
end
return map
end
local function readNullableBool(bs)
if readBool(bs) then
return readBool(bs)
end
end
local default_vector2 = {x=0,y=0}
local default_vector3 = {x=0,y=0,z=0}
local default_vector4 = {x=0,y=0,z=0,w=0}
local beans = {}
{{ for bean in beans }}
do
---@class {{bean.full_name}} {{if bean.parent_def_type}}:{{bean.parent}} {{end}}
{{~ for field in bean.fields~}}
---@field public {{field.name}} {{lua_comment_type field.ctype}}
{{~end}}
local class = SimpleClass()
class._id = {{bean.id}}
class._name = '{{bean.full_name}}'
local id2name = { {{for c in bean.hierarchy_not_abstract_children}} [{{c.id}}] = '{{c.full_name}}', {{end}} }
{{if bean.is_abstract_type}}
class._serialize = function(bs, self)
writeInt(bs, {{bean.id}})
beans[self._name]._serialize(bs, self)
end
class._deserialize = function(bs)
local id = readInt(bs)
return beans[id2name[id]]._deserialize(bs)
end
{{else}}
class._serialize = function(bs, self)
{{~ for field in bean.hierarchy_fields ~}}
{{lua_serialize_while_nil 'bs' ('self.' + field.name) field.ctype}}
{{~end~}}
end
class._deserialize = function(bs)
local o = {
{{~ for field in bean.hierarchy_fields ~}}
{{~if !(need_marshal_bool_prefix field.ctype)~}}
{{field.name}} = {{lua_undering_deserialize 'bs' field.ctype}},
{{~else~}}
{{field.name}} = {{if !field.ctype.is_bool}}readBool(bs) and {{lua_undering_deserialize 'bs' field.ctype}} or nil {{else}} readNullableBool(bs) {{end}},
{{~end~}}
{{~end~}}
}
setmetatable(o, class)
return o
end
{{end}}
beans[class._name] = class
end
{{end}}
local protos = { }
{{ for proto in protos }}
do
---@class {{proto.full_name}}
{{~ for field in proto.fields~}}
---@field public {{field.name}} {{lua_comment_type field.ctype}}
{{~end}}
local class = SimpleClass()
class._id = {{proto.id}}
class._name = '{{proto.full_name}}'
class._serialize = function(bs, self)
{{~ for field in proto.fields ~}}
{{lua_serialize_while_nil 'bs' ('self.' + field.name) field.ctype}}
{{~end~}}
end
class._deserialize = function(bs)
local o = {
{{~ for field in proto.fields ~}}
{{~if !(need_marshal_bool_prefix field.ctype)~}}
{{field.name}} = {{lua_undering_deserialize 'bs' field.ctype}},
{{~else~}}
{{field.name}} = {{if !field.ctype.is_bool}}readBool(bs) and {{lua_undering_deserialize 'bs' field.ctype}} or nil {{else}} readNullableBool(bs) {{end}},
{{~end~}}
{{~end~}}
}
setmetatable(o, class)
return o
end
protos[class._id] = class
protos[class._name] = class
end
{{end}}
return { enums = enums, beans = beans, protos = protos }
end
return { InitTypes = InitTypes}

View File

@@ -0,0 +1,88 @@
{{
is_value_type = x.is_value_type
is_abstract_type = x.is_abstract_type
name = x.name
full_name = x.full_name
parent_def_type = x.parent_def_type
parent = x.parent
fields = x.fields
hierarchy_fields = x.hierarchy_fields
}}
{{x.typescript_namespace_begin}}
{{~if x.comment != '' ~}}
/**
* {{x.escape_comment}}
*/
{{~end~}}
export {{if x.is_abstract_type}} abstract {{end}} class {{name}} extends {{if parent_def_type}}{{x.parent}}{{else}}BeanBase{{end}} {
{{~if x.is_abstract_type~}}
static serializeTo(_buf_ : ByteBuf, _bean_ : {{name}}) {
_buf_.WriteInt(_bean_.getTypeId())
_bean_.serialize(_buf_)
}
static deserializeFrom(_buf_ : ByteBuf) : {{name}} {
let _bean_ :{{name}}
switch (_buf_.ReadInt()) {
{{~ for child in x.hierarchy_not_abstract_children~}}
case {{child.id}}: _bean_ = new {{child.full_name}}(); break
{{~end~}}
default: throw new Error()
}
_bean_.deserialize(_buf_)
return _bean_
}
{{else}}
static readonly __ID__ = {{x.id}}
getTypeId() { return {{name}}.__ID__ }
{{~end~}}
{{~ for field in fields ~}}
{{~if field.comment != '' ~}}
/**
* {{field.escape_comment}}
*/
{{~end~}}
{{field.convention_name}}{{if field.is_nullable}}?{{end}} : {{ts_define_type field.ctype}}
{{~end~}}
constructor() {
super()
{{~ for field in fields ~}}
this.{{field.convention_name}} = {{ts_ctor_default_value field.ctype}}
{{~end~}}
}
serialize(_buf_ : ByteBuf) {
{{~if parent_def_type~}}
super.serialize(_buf_)
{{~end~}}
{{~ for field in fields ~}}
{{ts_bin_serialize ('this.' + field.convention_name) '_buf_' field.ctype}}
{{~end~}}
}
deserialize(_buf_ : ByteBuf) {
{{~if parent_def_type~}}
super.deserialize(_buf_)
{{~end~}}
{{~ for field in fields ~}}
{{ts_bin_deserialize ('this.' + field.convention_name) '_buf_' field.ctype}}
{{~end~}}
}
toString(): string {
return '{{full_name}}{ '
{{~ for field in hierarchy_fields ~}}
+ '{{field.convention_name}}:' + this.{{field.convention_name}} + ','
{{~end~}}
+ '}'
}
}
{{x.typescript_namespace_end}}

View File

@@ -0,0 +1,55 @@
{{
name = x.name
full_name = x.full_name
parent = x.parent
fields = x.fields
}}
{{x.typescript_namespace_begin}}
{{~if x.comment != '' ~}}
/**
* {{x.escape_comment}}
*/
{{~end~}}
export class {{name}} extends Protocol {
static readonly __ID__ = {{x.id}}
getTypeId() { return {{name}}.__ID__ }
{{~ for field in fields ~}}
{{~if field.comment != '' ~}}
/**
* {{field.escape_comment}}
*/
{{~end~}}
{{field.convention_name}}{{if field.is_nullable}}?{{end}} : {{ts_define_type field.ctype}}
{{~end~}}
constructor() {
super()
{{~ for field in fields ~}}
this.{{field.convention_name}} = {{ts_ctor_default_value field.ctype}}
{{~end~}}
}
serialize(_buf_ : ByteBuf) {
{{~ for field in fields ~}}
{{ts_bin_serialize ('this.' + field.convention_name) '_buf_' field.ctype}}
{{~end~}}
}
deserialize(_buf_ : ByteBuf) {
{{~ for field in fields ~}}
{{ts_bin_deserialize ('this.' + field.convention_name) '_buf_' field.ctype}}
{{~end~}}
}
toString(): string {
return '{{full_name}}{ '
{{~ for field in fields ~}}
+ '{{field.convention_name}}:' + this.{{field.convention_name}} + ','
{{~end~}}
+ '}'
}
}
{{x.typescript_namespace_end}}

View File

@@ -0,0 +1,10 @@
{{
name = x.name
full_name = x.full_name
parent = x.parent
fields = x.fields
targ_type = x.targ_type
tres_type = x.tres_type
}}
// TODO {{full_name}}

View File

@@ -0,0 +1,15 @@
type ProtocolFactory = () => Protocol
export class {{name}} {
static readonly Factories = new Map<number, ProtocolFactory>([
{{~ for proto in protos ~}}
[{{proto.full_name}}.__ID__, () => new {{proto.full_name}}()],
{{~end~}}
{{~ for rpc in rpcs ~}}
// TODO RPC .. [{{rpc.full_name}}.__ID__] = () => new {{rpc.full_name}}(),
{{~end~}}
])
}