【特性】【cfg】 新增puerts + unity框架下加载bin格式数据的Typescript 代码生成

main
walon 2021-05-01 14:49:22 +08:00
parent c538f2045d
commit 428652b433
16 changed files with 638 additions and 79 deletions

View File

@ -37,7 +37,7 @@
</bean> </bean>
<bean name="DemoE2"> <bean name="DemoE2">
<var name="y1" type="int"/> <var name="y1" type="int?"/>
<var name="y2" type="bool"/> <var name="y2" type="bool"/>
</bean> </bean>

View File

@ -239,7 +239,7 @@ namespace Luban.Job.Cfg.Defs
if (!_name2CfgTable.TryAdd(p.Name, p)) if (!_name2CfgTable.TryAdd(p.Name, p))
{ {
var exist = _name2CfgTable[p.Name]; var exist = _name2CfgTable[p.Name];
throw new Exception($"定义文件:{CurImportFile} table:{p.Namespace}.{p.Name} 与 {exist.Namespace}.{exist.Name} 重复"); throw new Exception($"定义文件:{CurImportFile} table:{p.Namespace}.{p.Name} 与 {exist.Namespace}.{exist.Name} 名称不能重复");
} }
_cfgTables.Add(p); _cfgTables.Add(p);
} }

View File

@ -94,7 +94,7 @@ namespace Luban.Job.Cfg.Defs
get get
{ {
var table = Assembly.GetCfgTable(Ref.FirstTable); var table = Assembly.GetCfgTable(Ref.FirstTable);
return $"{TsRefVarName} : {table.ValueTType.Apply(TsDefineTypeName.Ins)};"; return $"{TsRefVarName} : {table.ValueTType.Apply(TypescriptDefineTypeName.Ins)};";
} }
} }

View File

@ -100,12 +100,12 @@ namespace Luban.Job.Cfg.Defs
public static string TsDeserialize(string fieldName, string jsonFieldName, TType type) public static string TsDeserialize(string fieldName, string jsonFieldName, TType type)
{ {
return type.Apply(TsDeserializeVisitor.Ins, $"{jsonFieldName}", fieldName); return type.Apply(TypescriptDeserializeVisitor.Ins, $"{jsonFieldName}", fieldName);
} }
public static string TsRecursiveResolve(DefField field, string tables) public static string TsRecursiveResolve(DefField field, string tables)
{ {
return field.CType.Apply(TsRecursiveResolveVisitor.Ins, "this." + field.CsStyleName, tables); return field.CType.Apply(TypescriptRecursiveResolveVisitor.Ins, "this." + field.CsStyleName, tables);
} }
public static string TsRefValidatorResolve(DefField field) public static string TsRefValidatorResolve(DefField field)
@ -124,6 +124,11 @@ namespace Luban.Job.Cfg.Defs
} }
} }
public static string TsDeserializeBin(string fieldName, string byteBufName, TType type)
{
return type.Apply(TypescriptDeserializeBinVisitor.Ins, byteBufName, fieldName);
}
public static string Py3Deserialize(string fieldName, string jsonFieldName, TType type) public static string Py3Deserialize(string fieldName, string jsonFieldName, TType type)
{ {
return type.Apply(PyDeserializeVisitor.Py3Ins, $"{jsonFieldName}", fieldName); return type.Apply(PyDeserializeVisitor.Py3Ins, $"{jsonFieldName}", fieldName);

View File

@ -0,0 +1,234 @@
using Luban.Job.Cfg.Defs;
using Luban.Job.Common.Defs;
using Scriban;
using System;
using System.Collections.Generic;
namespace Luban.Job.Cfg.Generate
{
class TypeScriptBinCodeRender : TypescriptCodeRenderBase
{
[ThreadStatic]
private static Template t_beanRender;
public override string Render(DefBean b)
{
var template = t_beanRender ??= Template.Parse(@"
{{
name = x.name
parent_def_type = x.parent_def_type
export_fields = x.export_fields
hierarchy_export_fields = x.hierarchy_export_fields
}}
namespace {{x.namespace}} {
export {{if x.is_abstract_type}} abstract {{end}} class {{name}} {{if parent_def_type}} extends {{x.parent}}{{end}} {
{{~if x.is_abstract_type~}}
static deserialize(_buf_ : Bright.Serialization.ByteBuf) : {{name}} {
switch (_buf_.ReadInt())
{
case 0 : return null;
{{~ for child in x.hierarchy_not_abstract_children~}}
case {{child.id}}: return new {{child.full_name}}(_buf_);
{{~end~}}
default: throw new Error();
}
}
{{~end~}}
constructor(_buf_ : Bright.Serialization.ByteBuf) {
{{~if parent_def_type~}}
super(_buf_);
{{~end~}}
{{~ for field in export_fields ~}}
{{ts_deserialize_bin ('this.' + field.ts_style_name) '_buf_' field.ctype}}
{{~end~}}
}
{{~ for field in export_fields ~}}
{{field.ts_style_name}}{{if field.is_nullable}}?{{end}} : {{ts_define_type field.ctype}};
{{~if field.gen_ref~}}
{{field.ts_ref_validator_define}}
{{~end~}}
{{~end~}}
resolve(_tables : Map<string, any>) : void {
{{~if parent_def_type~}}
super.resolve(_tables);
{{~end~}}
{{~ for field in export_fields ~}}
{{~if field.gen_ref~}}
{{ts_ref_validator_resolve field}}
{{~else if field.has_recursive_ref~}}
{{ts_recursive_resolve field '_tables'}}
{{~end~}}
{{~end~}}
}
}
}
");
var result = template.RenderCode(b);
return result;
}
[ThreadStatic]
private static Template t_tableRender;
public override string Render(DefTable p)
{
var template = t_tableRender ??= Template.Parse(@"
{{
name = x.name
key_type = x.key_ttype
key_type1 = x.key_ttype1
key_type2 = x.key_ttype2
value_type = x.value_ttype
}}
namespace {{x.namespace}} {
export class {{name}}{
{{~ if x.is_two_key_map_table ~}}
private _dataListMap : Map<{{ts_define_type key_type1}}, {{ts_define_type value_type}}[]>;
private _dataMapMap : Map<{{ts_define_type key_type1}}, Map<{{ts_define_type key_type2}}, {{ts_define_type value_type}}>>;
private _dataList : {{ts_define_type value_type}}[];
constructor(_buf_ : Bright.Serialization.ByteBuf) {
this._dataListMap = new Map<{{ts_define_type key_type1}}, {{ts_define_type value_type}}[]>();
this._dataMapMap = new Map<{{ts_define_type key_type1}}, Map<{{ts_define_type key_type2}}, {{ts_define_type value_type}}>>();
this._dataList = [];
for(let n = _buf_.ReadInt(); n > 0 ; n--) {
let _v : {{ts_define_type value_type}};
{{ts_deserialize_bin '_v' '_buf_' value_type}}
this._dataList.push(_v);
var _key = _v.{{x.index_field1.ts_style_name}};
let list : {{ts_define_type value_type}}[] = this._dataListMap.get(_key);
if (list == null) {
list = [];
this._dataListMap.set(_key, list);
}
list.push(_v);
let map : Map<{{ts_define_type key_type2}}, {{ts_define_type value_type}}> = this._dataMapMap.get(_key);
if (map == null) {
map = new Map<{{ts_define_type key_type2}}, {{ts_define_type value_type}}>();
this._dataMapMap.set(_key, map);
}
map.set(_v.{{x.index_field2.ts_style_name}}, _v);
}
}
getDataListMap() : Map<{{ts_define_type key_type1}}, {{ts_define_type value_type}}[]> { return this._dataListMap; }
getDataMapMap() : Map<{{ts_define_type key_type1}}, Map<{{ts_define_type key_type2}}, {{ts_define_type value_type}}>> { return this._dataMapMap; }
getDataList() : {{ts_define_type value_type}}[] { return this._dataList; }
get(key1 : {{ts_define_type key_type1}}, key2 : {{ts_define_type key_type2}}) : {{ts_define_type value_type}} { return this._dataMapMap.get(key1).get(key2); }
resolve(_tables : Map<string, any>) : void {
for(var v of this._dataList) {
v.resolve(_tables);
}
}
{{~else if x.is_map_table ~}}
private _dataMap : Map<{{ts_define_type key_type}}, {{ts_define_type value_type}}>;
private _dataList : {{ts_define_type value_type}}[];
constructor(_buf_ : Bright.Serialization.ByteBuf) {
this._dataMap = new Map<{{ts_define_type key_type}}, {{ts_define_type value_type}}>();
this._dataList = [];
for(let n = _buf_.ReadInt() ; n > 0 ; n--) {
let _v : {{ts_define_type value_type}};
{{ts_deserialize_bin '_v' '_buf_' value_type}}
this._dataList.push(_v);
this._dataMap.set(_v.{{x.index_field.ts_style_name}}, _v);
}
}
getDataMap() : Map<{{ts_define_type key_type}}, {{ts_define_type value_type}}> { return this._dataMap; }
getDataList() : {{ts_define_type value_type}}[] { return this._dataList; }
get(key : {{ts_define_type key_type}}) : {{ts_define_type value_type}} { return this._dataMap.get(key); }
resolve(_tables : Map<string, any>) : void {
for(var v of this._dataList) {
v.resolve(_tables);
}
}
{{~else~}}
private _data : {{ts_define_type value_type}};
constructor(_buf_ : Bright.Serialization.ByteBuf) {
if (_buf_.ReadInt() != 1) throw new Error('table mode=one, but size != 1');
{{ts_deserialize_bin 'this._data' '_buf_' value_type}}
}
getData() : {{ts_define_type value_type}} { return this._data; }
{{~ for field in value_type.bean.hierarchy_export_fields ~}}
get {{field.ts_style_name}}() : {{ts_define_type field.ctype}} { return this._data.{{field.ts_style_name}}; }
{{~end~}}
resolve(_tables : Map<string, any>) : void {
this._data.resolve(_tables);
}
{{end}}
}
}
");
var result = template.RenderCode(p);
return result;
}
[ThreadStatic]
private static Template t_serviceRender;
public override string RenderService(string name, string module, List<DefTable> tables)
{
var template = t_serviceRender ??= Template.Parse(@"
{{
name = x.name
namespace = x.namespace
tables = x.tables
}}
type ByteBufLoader = (file : string) => Bright.Serialization.ByteBuf
export class {{name}} {
{{~ for table in tables ~}}
private _{{table.name}} : {{table.full_name}};
get {{table.name}}() : {{table.full_name}} { return this._{{table.name}};}
{{~end~}}
constructor(loader : ByteBufLoader) {
let tables = new Map<string, any>();
{{~for table in tables ~}}
this._{{table.name}} = new {{table.full_name}}(loader('{{table.output_data_file}}'));
tables.set('{{table.full_name}}', this._{{table.name}});
{{~end~}}
{{~ for table in tables ~}}
this._{{table.name}}.resolve(tables);
{{~end~}}
}
}
");
var result = template.RenderCode(new
{
Name = name,
Namespace = module,
Tables = tables,
});
return result;
}
}
}

View File

@ -6,54 +6,8 @@ using System.Collections.Generic;
namespace Luban.Job.Cfg.Generate namespace Luban.Job.Cfg.Generate
{ {
class TypeScriptJsonCodeRender : CodeRenderBase class TypeScriptJsonCodeRender : TypescriptCodeRenderBase
{ {
[ThreadStatic]
private static Template t_tsConstRender;
public override string Render(DefConst c)
{
var ctx = new TemplateContext();
var env = new TTypeTemplateCommonExtends
{
["x"] = c
};
ctx.PushGlobal(env);
var template = t_tsConstRender ??= Template.Parse(@"
namespace {{x.namespace}} {
export class {{x.name}} {
{{~ for item in x.items ~}}
static {{item.name}} : {{ts_define_type item.ctype}} = {{ts_const_value item.ctype item.value}};
{{~end~}}
}
}
");
var result = template.Render(ctx);
return result;
}
[ThreadStatic]
private static Template t_tsEnumRender;
public override string Render(DefEnum e)
{
var template = t_tsEnumRender ??= Template.Parse(@"
namespace {{namespace}} {
export enum {{name}} {
{{- for item in items }}
{{item.name}} = {{item.value}},
{{-end}}
}
}
");
var result = template.Render(e);
return result;
}
[ThreadStatic] [ThreadStatic]
private static Template t_beanRender; private static Template t_beanRender;
public override string Render(DefBean b) public override string Render(DefBean b)
@ -96,7 +50,7 @@ export {{if x.is_abstract_type}} abstract {{end}} class {{name}} {{if parent_def
} }
{{~ for field in export_fields ~}} {{~ for field in export_fields ~}}
{{field.ts_style_name}} : {{ts_define_type field.ctype}}; {{field.ts_style_name}}{{if field.is_nullable}}?{{end}} : {{ts_define_type field.ctype}};
{{~if field.gen_ref~}} {{~if field.gen_ref~}}
{{field.ts_ref_validator_define}} {{field.ts_ref_validator_define}}
{{~end~}} {{~end~}}

View File

@ -0,0 +1,59 @@
using Luban.Job.Common.Defs;
using Scriban;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Luban.Job.Cfg.Generate
{
abstract class TypescriptCodeRenderBase : CodeRenderBase
{
[ThreadStatic]
private static Template t_tsConstRender;
public override string Render(DefConst c)
{
var ctx = new TemplateContext();
var env = new TTypeTemplateCommonExtends
{
["x"] = c
};
ctx.PushGlobal(env);
var template = t_tsConstRender ??= Template.Parse(@"
namespace {{x.namespace}} {
export class {{x.name}} {
{{~ for item in x.items ~}}
static {{item.name}} = {{ts_const_value item.ctype item.value}};
{{~end~}}
}
}
");
var result = template.Render(ctx);
return result;
}
[ThreadStatic]
private static Template t_tsEnumRender;
public override string Render(DefEnum e)
{
var template = t_tsEnumRender ??= Template.Parse(@"
namespace {{namespace}} {
export enum {{name}} {
{{- for item in items }}
{{item.name}} = {{item.value}},
{{-end}}
}
}
");
var result = template.Render(e);
return result;
}
}
}

View File

@ -435,7 +435,7 @@ export class Vector2 {
this.y = y; this.y = y;
} }
static fromJson(_json_: any): Vector2 { static from(_json_: any): Vector2 {
let x = _json_['x']; let x = _json_['x'];
let y = _json_['y']; let y = _json_['y'];
if (x == null || y == null) { if (x == null || y == null) {
@ -456,7 +456,7 @@ export class Vector2 {
this.z = z; this.z = z;
} }
static fromJson(_json_: any): Vector3 { static from(_json_: any): Vector3 {
let x = _json_['x']; let x = _json_['x'];
let y = _json_['y']; let y = _json_['y'];
let z = _json_['z']; let z = _json_['z'];
@ -479,7 +479,7 @@ export class Vector2 {
this.w = w; this.w = w;
} }
static fromJson(_json_: any): Vector4 { static from(_json_: any): Vector4 {
let x = _json_['x']; let x = _json_['x'];
let y = _json_['y']; let y = _json_['y'];
let z = _json_['z']; let z = _json_['z'];
@ -491,6 +491,119 @@ export class Vector2 {
} }
} }
"
};
foreach (var type in exportTypes)
{
if (type is DefEnum e)
{
fileContent.Add(render.Render(e));
}
}
foreach (var type in exportTypes)
{
if (type is DefConst c)
{
fileContent.Add(render.Render(c));
}
}
foreach (var type in exportTypes)
{
if (type is DefBean e)
{
fileContent.Add(render.Render(e));
}
}
foreach (var type in exportTables)
{
fileContent.Add(render.Render(type));
}
fileContent.Add(render.RenderService("Tables", ass.TopModule, exportTables));
fileContent.Add("}"); // end of topmodule
var content = FileHeaderUtil.ConcatAutoGenerationHeader(string.Join('\n', fileContent), ELanguage.TYPESCRIPT);
var file = "Types.ts";
var md5 = CacheFileUtil.GenMd5AndAddCache(file, content);
genCodeFilesInOutputCodeDir.Add(new FileInfo() { FilePath = file, MD5 = md5 });
}));
break;
}
case "code_typescript_bin":
{
var render = new TypeScriptBinCodeRender();
tasks.Add(Task.Run(() =>
{
var fileContent = new List<string>
{
@$"
import {{Bright}} from 'csharp'
export namespace {ass.TopModule} {{
",
@"
export class Vector2 {
x: number;
y: number;
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
static from(_buf_: Bright.Serialization.ByteBuf): Vector2 {
let x = _buf_.ReadFloat();
let y = _buf_.ReadFloat();
return new Vector2(x, y);
}
}
export class Vector3 {
x: number;
y: number;
z: number;
constructor(x: number, y: number, z: number) {
this.x = x;
this.y = y;
this.z = z;
}
static from(_buf_: Bright.Serialization.ByteBuf): Vector3 {
let x = _buf_.ReadFloat();
let y = _buf_.ReadFloat();
let z = _buf_.ReadFloat();
return new Vector3(x, y, z);
}
}
export class Vector4 {
x: number;
y: number;
z: number;
w: number;
constructor(x: number, y: number, z: number, w: number) {
this.x = x;
this.y = y;
this.z = z;
this.w = w;
}
static from(_buf_: Bright.Serialization.ByteBuf): Vector4 {
let x = _buf_.ReadFloat();
let y = _buf_.ReadFloat();
let z = _buf_.ReadFloat();
let w = _buf_.ReadFloat();
return new Vector4(x, y, z, w);
}
}
" "
}; };

View File

@ -0,0 +1,27 @@
using Luban.Job.Common.Types;
using Luban.Job.Common.TypeVisitors;
namespace Luban.Job.Cfg.TypeVisitors
{
class TypescriptDeserializeBinVisitor : DecoratorFuncVisitor<string, string, string>
{
public static TypescriptDeserializeBinVisitor Ins { get; } = new TypescriptDeserializeBinVisitor();
public override string DoAccept(TType type, string byteBufName, string fieldName)
{
if (type.IsNullable)
{
return $"if({byteBufName}.ReadBool()) {{ {type.Apply(TypescriptUnderingDeserializeBinVisitor.Ins, byteBufName, fieldName)} }} else {{ {fieldName} = null; }}";
}
else
{
return type.Apply(TypescriptUnderingDeserializeBinVisitor.Ins, byteBufName, fieldName);
}
}
public override string Accept(TBean type, string bufName, string fieldName)
{
return type.Apply(TypescriptUnderingDeserializeBinVisitor.Ins, bufName, fieldName);
}
}
}

View File

@ -3,25 +3,25 @@ using Luban.Job.Common.TypeVisitors;
namespace Luban.Job.Cfg.TypeVisitors namespace Luban.Job.Cfg.TypeVisitors
{ {
class TsDeserializeVisitor : DecoratorFuncVisitor<string, string, string> class TypescriptDeserializeVisitor : DecoratorFuncVisitor<string, string, string>
{ {
public static TsDeserializeVisitor Ins { get; } = new TsDeserializeVisitor(); public static TypescriptDeserializeVisitor Ins { get; } = new TypescriptDeserializeVisitor();
public override string DoAccept(TType type, string jsonFieldName, string fieldName) public override string DoAccept(TType type, string jsonFieldName, string fieldName)
{ {
if (type.IsNullable) if (type.IsNullable)
{ {
return $"if({jsonFieldName} != null) {{ {type.Apply(TsUnderingDeserializeVisitor.Ins, jsonFieldName, fieldName)} }} else {{ {fieldName} = null; }}"; return $"if({jsonFieldName} != null) {{ {type.Apply(TypescriptUnderingDeserializeVisitor.Ins, jsonFieldName, fieldName)} }} else {{ {fieldName} = null; }}";
} }
else else
{ {
return type.Apply(TsUnderingDeserializeVisitor.Ins, jsonFieldName, fieldName); return type.Apply(TypescriptUnderingDeserializeVisitor.Ins, jsonFieldName, fieldName);
} }
} }
public override string Accept(TBean type, string bufName, string fieldName) public override string Accept(TBean type, string bufName, string fieldName)
{ {
return type.Apply(TsUnderingDeserializeVisitor.Ins, bufName, fieldName); return type.Apply(TypescriptUnderingDeserializeVisitor.Ins, bufName, fieldName);
} }
} }
} }

View File

@ -4,9 +4,9 @@ using System;
namespace Luban.Job.Cfg.TypeVisitors namespace Luban.Job.Cfg.TypeVisitors
{ {
class TsRecursiveResolveVisitor : ITypeFuncVisitor<string, string, string> class TypescriptRecursiveResolveVisitor : ITypeFuncVisitor<string, string, string>
{ {
public static TsRecursiveResolveVisitor Ins { get; } = new TsRecursiveResolveVisitor(); public static TypescriptRecursiveResolveVisitor Ins { get; } = new TypescriptRecursiveResolveVisitor();
public string Accept(TBool type, string fieldName, string tablesName) public string Accept(TBool type, string fieldName, string tablesName)
{ {

View File

@ -0,0 +1,167 @@
using Luban.Job.Common.Types;
using Luban.Job.Common.TypeVisitors;
namespace Luban.Job.Cfg.TypeVisitors
{
class TypescriptUnderingDeserializeBinVisitor : ITypeFuncVisitor<string, string, string>
{
public static TypescriptUnderingDeserializeBinVisitor Ins { get; } = new TypescriptUnderingDeserializeBinVisitor();
public string Accept(TBool type, string bufName, string fieldName)
{
return $"{fieldName} = {bufName}.ReadBool();";
}
public string Accept(TByte type, string bufName, string fieldName)
{
return $"{fieldName} = {bufName}.ReadByte();";
}
public string Accept(TShort type, string bufName, string fieldName)
{
return $"{fieldName} = {bufName}.ReadShort();";
}
public string Accept(TFshort type, string bufName, string fieldName)
{
return $"{fieldName} = {bufName}.ReadFshort();";
}
public string Accept(TInt type, string bufName, string fieldName)
{
return $"{fieldName} = {bufName}.ReadInt();";
}
public string Accept(TFint type, string bufName, string fieldName)
{
return $"{fieldName} = {bufName}.ReadFint();";
}
public string Accept(TLong type, string bufName, string fieldName)
{
return $"{fieldName} = {bufName}.ReadLong();";
}
public string Accept(TFlong type, string bufName, string fieldName)
{
return $"{fieldName} = {bufName}.ReadFlong();";
}
public string Accept(TFloat type, string bufName, string fieldName)
{
return $"{fieldName} = {bufName}.ReadFloat();";
}
public string Accept(TDouble type, string bufName, string fieldName)
{
return $"{fieldName} = {bufName}.ReadDouble();";
}
public string Accept(TEnum type, string bufName, string fieldName)
{
return $"{fieldName} = {bufName}.ReadInt();";
}
public string Accept(TString type, string bufName, string fieldName)
{
return $"{fieldName} = {bufName}.ReadString();";
}
public string Accept(TBytes type, string bufName, string fieldName)
{
return $"{fieldName} = {bufName}.ReadArrayBuf();";
}
public string Accept(TText type, string bufName, string fieldName)
{
return $"{fieldName} = {bufName}.ReadString();";
}
public string Accept(TBean type, string bufVarName, string fieldName)
{
if (type.Bean.IsAbstractType)
{
return $"{fieldName} = {type.Bean.FullName}.deserialize({bufVarName});";
}
else
{
return $"{fieldName} = new {type.Bean.FullName}({bufVarName});";
}
}
private string GetNewArray(TArray arrayType, string size)
{
switch (arrayType.ElementType)
{
case TByte _: return $"new Uint8Array({size})";
case TShort _:
case TFshort _: return $"new Int16Array({size})";
case TInt _:
case TFint _: return $"new Int32Array({size})";
case TLong _:
case TFlong _: return $"new Int64Array({size})";
case TFloat _: return $"new Float32Array({size})";
case TDouble _: return $"new Float64Array({size})";
default: return "[]";
}
}
private bool IsRawArrayElementType(TType elementType)
{
switch (elementType)
{
case TByte _:
case TShort _:
case TFshort _:
case TInt _:
case TFint _:
case TLong _:
case TFlong _:
case TFloat _:
case TDouble _: return true;
default: return false;
}
}
public string Accept(TArray type, string bufVarName, string fieldName)
{
return $"{{ let n = Math.min({bufVarName}.ReadSize(), {bufVarName}.Size); {fieldName} = {GetNewArray(type, "n")}; for(let i = 0 ; i < n ; i++) {{ let _e :{type.ElementType.Apply(TypescriptDefineTypeName.Ins)};{type.ElementType.Apply(this, bufVarName, "_e")}; { (IsRawArrayElementType(type.ElementType) ? $"{fieldName}[i] = _e" : $"{fieldName}.push(_e)") } }} }}";
}
public string Accept(TList type, string bufVarName, string fieldName)
{
return $"{{ {fieldName} = []; for(let i = 0, n = {bufVarName}.ReadSize() ; i < n ; i++) {{ let _e :{type.ElementType.Apply(TypescriptDefineTypeName.Ins)};{type.ElementType.Apply(this, bufVarName, "_e")}; {fieldName}.push(_e) }} }}";
}
public string Accept(TSet type, string bufVarName, string fieldName)
{
return $"{{ {fieldName} = new {type.Apply(TypescriptDefineTypeName.Ins)}(); for(let i = 0, n = {bufVarName}.ReadSize() ; i < n ; i++) {{ let _e:{type.ElementType.Apply(TypescriptDefineTypeName.Ins)};{type.ElementType.Apply(this, bufVarName, "_e")} {fieldName}.add(_e);}}}}";
}
public string Accept(TMap type, string bufVarName, string fieldName)
{
return $"{{ {fieldName} = new {type.Apply(TypescriptDefineTypeName.Ins)}(); for(let i = 0, n = {bufVarName}.ReadSize() ; i < n ; i++) {{ let _k:{type.KeyType.Apply(TypescriptDefineTypeName.Ins)}; {type.KeyType.Apply(this, bufVarName, "_k")} let _v:{type.ValueType.Apply(TypescriptDefineTypeName.Ins)}; {type.ValueType.Apply(this, bufVarName, "_v")} {fieldName}.set(_k, _v); }} }}";
}
public string Accept(TVector2 type, string bufVarName, string fieldName)
{
return $"{fieldName} = Vector2.from({bufVarName})";
}
public string Accept(TVector3 type, string bufVarName, string fieldName)
{
return $"{fieldName} = Vector3.from({bufVarName})";
}
public string Accept(TVector4 type, string bufVarName, string fieldName)
{
return $"{fieldName} = Vector4.from({bufVarName})";
}
public string Accept(TDateTime type, string bufVarName, string fieldName)
{
return $"{fieldName} = {bufVarName}.ReadInt();";
}
}
}

View File

@ -3,9 +3,9 @@ using Luban.Job.Common.TypeVisitors;
namespace Luban.Job.Cfg.TypeVisitors namespace Luban.Job.Cfg.TypeVisitors
{ {
class TsUnderingDeserializeVisitor : ITypeFuncVisitor<string, string, string> class TypescriptUnderingDeserializeVisitor : ITypeFuncVisitor<string, string, string>
{ {
public static TsUnderingDeserializeVisitor Ins { get; } = new TsUnderingDeserializeVisitor(); public static TypescriptUnderingDeserializeVisitor Ins { get; } = new TypescriptUnderingDeserializeVisitor();
public string Accept(TBool type, string jsonVarName, string fieldName) public string Accept(TBool type, string jsonVarName, string fieldName)
{ {
@ -59,7 +59,7 @@ namespace Luban.Job.Cfg.TypeVisitors
public string Accept(TEnum type, string jsonVarName, string fieldName) public string Accept(TEnum type, string jsonVarName, string fieldName)
{ {
return $"{fieldName} = {jsonVarName} as number;"; return $"{fieldName} = {jsonVarName};";
} }
public string Accept(TString type, string jsonVarName, string fieldName) public string Accept(TString type, string jsonVarName, string fieldName)
@ -97,7 +97,7 @@ namespace Luban.Job.Cfg.TypeVisitors
} }
else else
{ {
return $"{{ {fieldName} = []; for(var _ele of {jsonVarName}) {{ let _e :{type.ElementType.Apply(TsDefineTypeName.Ins)};{type.ElementType.Apply(this, "_ele", "_e")} {fieldName}.push(_e);}}}}"; return $"{{ {fieldName} = []; for(var _ele of {jsonVarName}) {{ let _e :{type.ElementType.Apply(TypescriptDefineTypeName.Ins)};{type.ElementType.Apply(this, "_ele", "_e")} {fieldName}.push(_e);}}}}";
} }
} }
@ -109,7 +109,7 @@ namespace Luban.Job.Cfg.TypeVisitors
} }
else else
{ {
return $"{{ {fieldName} = []; for(var _ele of {jsonVarName}) {{ let _e : {type.ElementType.Apply(TsDefineTypeName.Ins)};{type.ElementType.Apply(this, "_ele", "_e")} {fieldName}.push(_e);}}}}"; return $"{{ {fieldName} = []; for(var _ele of {jsonVarName}) {{ let _e : {type.ElementType.Apply(TypescriptDefineTypeName.Ins)};{type.ElementType.Apply(this, "_ele", "_e")} {fieldName}.push(_e);}}}}";
} }
} }
@ -121,29 +121,29 @@ namespace Luban.Job.Cfg.TypeVisitors
} }
else else
{ {
return $"{{ {fieldName} = new {type.Apply(TsDefineTypeName.Ins)}(); for(var _ele of {jsonVarName}) {{ let _e:{type.ElementType.Apply(TsDefineTypeName.Ins)};{type.ElementType.Apply(this, "_ele", "_e")} {fieldName}.add(_e);}}}}"; return $"{{ {fieldName} = new {type.Apply(TypescriptDefineTypeName.Ins)}(); for(var _ele of {jsonVarName}) {{ let _e:{type.ElementType.Apply(TypescriptDefineTypeName.Ins)};{type.ElementType.Apply(this, "_ele", "_e")} {fieldName}.add(_e);}}}}";
} }
} }
public string Accept(TMap type, string jsonVarName, string fieldName) public string Accept(TMap type, string jsonVarName, string fieldName)
{ {
return $"{fieldName} = new {type.Apply(TsDefineTypeName.Ins)}(); for(var _entry_ of {jsonVarName}) {{ let _k:{type.KeyType.Apply(TsDefineTypeName.Ins)}; {type.KeyType.Apply(this, "_entry_[0]", "_k")} let _v:{type.ValueType.Apply(TsDefineTypeName.Ins)}; {type.ValueType.Apply(this, "_entry_[1]", "_v")} {fieldName}.set(_k, _v); }}"; return $"{fieldName} = new {type.Apply(TypescriptDefineTypeName.Ins)}(); for(var _entry_ of {jsonVarName}) {{ let _k:{type.KeyType.Apply(TypescriptDefineTypeName.Ins)}; {type.KeyType.Apply(this, "_entry_[0]", "_k")} let _v:{type.ValueType.Apply(TypescriptDefineTypeName.Ins)}; {type.ValueType.Apply(this, "_entry_[1]", "_v")} {fieldName}.set(_k, _v); }}";
} }
public string Accept(TVector2 type, string jsonVarName, string fieldName) public string Accept(TVector2 type, string jsonVarName, string fieldName)
{ {
return $"{fieldName} = Vector2.fromJson({jsonVarName});"; return $"{fieldName} = Vector2.from({jsonVarName});";
} }
public string Accept(TVector3 type, string jsonVarName, string fieldName) public string Accept(TVector3 type, string jsonVarName, string fieldName)
{ {
return $"{fieldName} = Vector3.fromJson({jsonVarName});"; return $"{fieldName} = Vector3.from({jsonVarName});";
} }
public string Accept(TVector4 type, string jsonVarName, string fieldName) public string Accept(TVector4 type, string jsonVarName, string fieldName)
{ {
return $"{fieldName} = Vector4.fromJson({jsonVarName});"; return $"{fieldName} = Vector4.from({jsonVarName});";
} }
public string Accept(TDateTime type, string jsonVarName, string fieldName) public string Accept(TDateTime type, string jsonVarName, string fieldName)

View File

@ -125,7 +125,7 @@ namespace Luban.Job.Common.Defs
public static string TsDefineType(TType type) public static string TsDefineType(TType type)
{ {
return type.Apply(TsDefineTypeName.Ins); return type.Apply(TypescriptDefineTypeName.Ins);
} }
public static string TsToString(string filedName, TType type) public static string TsToString(string filedName, TType type)

View File

@ -2,9 +2,9 @@ using Luban.Job.Common.Types;
namespace Luban.Job.Common.TypeVisitors namespace Luban.Job.Common.TypeVisitors
{ {
public class TsDefineTypeName : ITypeFuncVisitor<string> public class TypescriptDefineTypeName : ITypeFuncVisitor<string>
{ {
public static TsDefineTypeName Ins { get; } = new TsDefineTypeName(); public static TypescriptDefineTypeName Ins { get; } = new TypescriptDefineTypeName();
public string Accept(TBool type) public string Accept(TBool type)
{ {
@ -38,12 +38,12 @@ namespace Luban.Job.Common.TypeVisitors
public string Accept(TLong type) public string Accept(TLong type)
{ {
return "number"; return "bigint";
} }
public string Accept(TFlong type) public string Accept(TFlong type)
{ {
return "number"; return "bigint";
} }
public string Accept(TFloat type) public string Accept(TFloat type)
@ -106,7 +106,7 @@ namespace Luban.Job.Common.TypeVisitors
public string Accept(TList type) public string Accept(TList type)
{ {
return GetArrayType(type.ElementType); return $"{type.ElementType.Apply(this)}[]";
} }
public string Accept(TSet type) public string Accept(TSet type)