From 00de219e1687d905931cf9a535f94fc2d272a17c Mon Sep 17 00:00:00 2001 From: walon Date: Tue, 13 Jul 2021 15:15:55 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E8=B0=83=E6=95=B4=E3=80=91=E8=B0=83?= =?UTF-8?q?=E6=95=B4cfg=20typescript=E6=9C=89=E5=A4=9A=E7=A7=8D=E8=BE=93?= =?UTF-8?q?=E5=87=BA=E7=B1=BB=E5=9E=8B=EF=BC=8C=E8=A7=A3=E5=86=B3=E6=AF=8F?= =?UTF-8?q?=E7=A7=8D=E7=B1=BB=E5=9E=8B=E5=BC=95=E5=85=A5=20ByteBuf?= =?UTF-8?q?=E7=B1=BB=E5=8F=8ABright=E7=B1=BB=E7=9A=84=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=E4=B8=8D=E4=B8=80=E6=A0=B7=E7=9A=84=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Generate/TypeScriptBinCodeRender.cs | 10 +- src/Luban.Job.Cfg/Source/JobController.cs | 285 ++++++------------ ...ypescriptJsonUnderingConstructorVisitor.cs | 6 +- ...scriptBinUnderingDeserializeVisitorBase.cs | 4 +- .../Utils/TypescriptBrightTypeTemplates.cs | 71 ++++- src/Luban.Job.Proto/Source/JobController.cs | 6 +- 6 files changed, 167 insertions(+), 215 deletions(-) diff --git a/src/Luban.Job.Cfg/Source/Generate/TypeScriptBinCodeRender.cs b/src/Luban.Job.Cfg/Source/Generate/TypeScriptBinCodeRender.cs index dbce92e..c1221e0 100644 --- a/src/Luban.Job.Cfg/Source/Generate/TypeScriptBinCodeRender.cs +++ b/src/Luban.Job.Cfg/Source/Generate/TypeScriptBinCodeRender.cs @@ -36,7 +36,7 @@ namespace Luban.Job.Cfg.Generate export {{if x.is_abstract_type}} abstract {{end}} class {{name}} {{if parent_def_type}} extends {{x.parent}}{{end}} { {{~if x.is_abstract_type~}} - static constructorFrom(_buf_: Bright.Serialization.ByteBuf): {{name}} { + static constructorFrom(_buf_: ByteBuf): {{name}} { switch (_buf_.ReadInt()) { {{~ for child in x.hierarchy_not_abstract_children~}} case {{child.id}}: return new {{child.full_name}}(_buf_) @@ -46,7 +46,7 @@ export {{if x.is_abstract_type}} abstract {{end}} class {{name}} {{if parent_def } {{~end~}} - constructor(_buf_: Bright.Serialization.ByteBuf) { + constructor(_buf_: ByteBuf) { {{~if parent_def_type~}} super(_buf_) {{~end~}} @@ -101,7 +101,7 @@ export class {{name}} { 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) { + constructor(_buf_: ByteBuf) { this._dataMap = new Map<{{ts_define_type key_type}}, {{ts_define_type value_type}}>() this._dataList = [] @@ -128,7 +128,7 @@ export class {{name}} { private _data: {{ts_define_type value_type}} - constructor(_buf_: Bright.Serialization.ByteBuf) { + constructor(_buf_: ByteBuf) { if (_buf_.ReadInt() != 1) throw new Error('table mode=one, but size != 1') {{ts_bin_constructor 'this._data' '_buf_' value_type}} } @@ -164,7 +164,7 @@ export class {{name}} { }} -type ByteBufLoader = (file: string) => Bright.Serialization.ByteBuf +type ByteBufLoader = (file: string) => ByteBuf export class {{name}} { {{~ for table in tables ~}} diff --git a/src/Luban.Job.Cfg/Source/JobController.cs b/src/Luban.Job.Cfg/Source/JobController.cs index 35b6015..9588669 100644 --- a/src/Luban.Job.Cfg/Source/JobController.cs +++ b/src/Luban.Job.Cfg/Source/JobController.cs @@ -76,8 +76,14 @@ namespace Luban.Job.Cfg [Option("branch_input_data_dir", Required = false, HelpText = "branch input data root dir")] public string BranchInputDataDir { get; set; } - [Option("typescript_bytebuf_require_path", Required = false, HelpText = "bytebuf require path in typescript")] - public string TypescriptByteBufRequirePath { get; set; } + [Option("typescript_bright_require_path", Required = false, HelpText = "bright require path in typescript")] + public string TypescriptBrightRequirePath { get; set; } + + [Option("use_puerts_bytebuf", Required = false, HelpText = "use puerts bytebuf class. default is false")] + public bool UsePuertsByteBuf { get; set; } + + [Option("embed_bright_types", Required = false, HelpText = "use puerts bytebuf class. default is false")] + public bool EmbedBrightTypes { get; set; } } private ICodeRender CreateCodeRender(string genType) @@ -173,6 +179,17 @@ namespace Luban.Job.Cfg return false; } + if (!options.UsePuertsByteBuf && string.IsNullOrWhiteSpace(options.TypescriptBrightRequirePath)) + { + errMsg = $"while use_puerts_bytebuf is false, should provide option --typescript_bright_require_path"; + return false; + } + if (!options.EmbedBrightTypes && string.IsNullOrWhiteSpace(options.TypescriptBrightRequirePath)) + { + errMsg = $"while embed_bright_types is false, should provide option --typescript_bright_require_path"; + return false; + } + return true; } } @@ -423,112 +440,49 @@ namespace {ass.TopModule} break; } + case "code_typescript_bin": case "code_typescript_json": { - var render = new TypeScriptJsonCodeRender(); + bool isGenBinary = genType.EndsWith("bin"); + CodeRenderBase render = isGenBinary ? new TypeScriptBinCodeRender() : new TypeScriptJsonCodeRender(); + var brightRequirePath = args.TypescriptBrightRequirePath; tasks.Add(Task.Run(() => { - var fileContent = new List + var fileContent = new List(); + if (isGenBinary) { - @$" -export namespace {ass.TopModule} {{ -", - - @" -export class Vector2 { - x: number - y: number - constructor(x: number, y: number) { - this.x = x - this.y = y - } - - static from(_json_: any): Vector2 { - let x = _json_['x'] - let y = _json_['y'] - if (x == null || y == null) { - throw new Error() - } - 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(_json_: any): Vector3 { - let x = _json_['x'] - let y = _json_['y'] - let z = _json_['z'] - if (x == null || y == null || z == null) { - throw new Error() - } - 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(_json_: any): Vector4 { - let x = _json_['x'] - let y = _json_['y'] - let z = _json_['z'] - let w = _json_['w'] - if (x == null || y == null || z == null || w == null) { - throw new Error() - } - return new Vector4(x, y, z, w) - } - } - -" - }; - - foreach (var type in exportTypes) - { - if (type is DefEnum e) + if (args.UsePuertsByteBuf) { - fileContent.Add(render.Render(e)); + fileContent.Add(TypescriptBrightTypeTemplates.PuertsByteBufImports); + } + else + { + fileContent.Add(string.Format(TypescriptBrightTypeTemplates.BrightByteBufImportsFormat, brightRequirePath)); } } - foreach (var type in exportTypes) + if (args.EmbedBrightTypes) { - if (type is DefConst c) + fileContent.Add(isGenBinary ? TypescriptBrightTypeTemplates.VectorTypesByteBuf : TypescriptBrightTypeTemplates.VectorTypesJson); + if (isGenBinary) { - fileContent.Add(render.Render(c)); + fileContent.Add(TypescriptBrightTypeTemplates.SerializeTypes); } } + else + { + if (isGenBinary) + { + fileContent.Add(string.Format(TypescriptBrightTypeTemplates.SerializeImportsFormat, brightRequirePath)); + } + fileContent.Add(string.Format(TypescriptBrightTypeTemplates.VectorImportsFormat, brightRequirePath)); + } + + fileContent.Add(@$"export namespace {ass.TopModule} {{"); 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.RenderAny(type)); } fileContent.Add(render.RenderService("Tables", ass.TopModule, exportTables)); @@ -543,119 +497,50 @@ export class Vector2 { break; } - case "code_typescript_bin": - { - var render = new TypeScriptBinCodeRender(); - var byteBufRequirePath = args.TypescriptByteBufRequirePath ?? "csharp"; - tasks.Add(Task.Run(() => - { - var fileContent = new List - { - @$" -import {{Bright}} from '{byteBufRequirePath}' + //case "code_typescript_bin": + //{ + // var render = new TypeScriptBinCodeRender(); + // var brightRequirePath = args.TypescriptBrightRequirePath; + // tasks.Add(Task.Run(() => + // { + // var fileContent = new List(); + // if (args.UsePuertsByteBuf) + // { + // fileContent.Add(TypescriptBrightTypeTemplates.PuertsByteBufImports); + // } + // else + // { + // fileContent.Add(string.Format(TypescriptBrightTypeTemplates.BrightByteBufImportsFormat, brightRequirePath)); + // } + // if (args.EmbedBrightTypes) + // { + // fileContent.Add(TypescriptBrightTypeTemplates.VectorTypes); + // fileContent.Add(TypescriptBrightTypeTemplates.SerializeTypes); + // } + // else + // { + // fileContent.Add(string.Format(TypescriptBrightTypeTemplates.SerializeImportsFormat, brightRequirePath)); + // fileContent.Add(string.Format(TypescriptBrightTypeTemplates.VectorImportsFormat, brightRequirePath)); + // } -export namespace {ass.TopModule} {{ -", + // fileContent.Add(@$"export namespace {ass.TopModule} {{"); - @" -export class Vector2 { - x: number - y: number - constructor(x: number, y: number) { - this.x = x - this.y = y - } + // foreach (var type in exportTypes) + // { + // fileContent.Add(render.RenderAny(type)); + // } - static from(_buf_: Bright.Serialization.ByteBuf): Vector2 { - let x = _buf_.ReadFloat() - let y = _buf_.ReadFloat() - return new Vector2(x, y) - } - } + // fileContent.Add(render.RenderService("Tables", ass.TopModule, exportTables)); + // fileContent.Add("}"); // end of topmodule - 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) - } - } - -" - }; - - 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; - } + // 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_python27_json": { diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/TypescriptJsonUnderingConstructorVisitor.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/TypescriptJsonUnderingConstructorVisitor.cs index 3b8b5f7..22b0f96 100644 --- a/src/Luban.Job.Cfg/Source/TypeVisitors/TypescriptJsonUnderingConstructorVisitor.cs +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/TypescriptJsonUnderingConstructorVisitor.cs @@ -133,17 +133,17 @@ namespace Luban.Job.Cfg.TypeVisitors public string Accept(TVector2 type, string jsonVarName, string fieldName) { - return $"{fieldName} = Vector2.from({jsonVarName})"; + return $"{fieldName} = Vector2.deserializeFromJson({jsonVarName})"; } public string Accept(TVector3 type, string jsonVarName, string fieldName) { - return $"{fieldName} = Vector3.from({jsonVarName})"; + return $"{fieldName} = Vector3.deserializeFromJson({jsonVarName})"; } public string Accept(TVector4 type, string jsonVarName, string fieldName) { - return $"{fieldName} = Vector4.from({jsonVarName})"; + return $"{fieldName} = Vector4.deserializeFromJson({jsonVarName})"; } public string Accept(TDateTime type, string jsonVarName, string fieldName) diff --git a/src/Luban.Job.Common/Source/TypeVisitors/TypescriptBinUnderingDeserializeVisitorBase.cs b/src/Luban.Job.Common/Source/TypeVisitors/TypescriptBinUnderingDeserializeVisitorBase.cs index db4d90e..b3b6619 100644 --- a/src/Luban.Job.Common/Source/TypeVisitors/TypescriptBinUnderingDeserializeVisitorBase.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/TypescriptBinUnderingDeserializeVisitorBase.cs @@ -133,12 +133,12 @@ namespace Luban.Job.Common.TypeVisitors 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);}}}}"; + 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); }} }}"; + 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); }} }}"; } diff --git a/src/Luban.Job.Common/Source/Utils/TypescriptBrightTypeTemplates.cs b/src/Luban.Job.Common/Source/Utils/TypescriptBrightTypeTemplates.cs index 6613800..93270fc 100644 --- a/src/Luban.Job.Common/Source/Utils/TypescriptBrightTypeTemplates.cs +++ b/src/Luban.Job.Common/Source/Utils/TypescriptBrightTypeTemplates.cs @@ -44,7 +44,76 @@ export abstract class Protocol implements ISerializable { } "; - public const string VectorTypes = @" + public const string VectorTypesJson = @" + +export class Vector2 { + static deserializeFromJson(json: any): Vector2 { + let x = json['x'] + let y = json['y'] + if (x == null || y == null) { + throw new Error() + } + return new Vector2(x, y) + } + + x: number + y: number + constructor(x: number = 0, y: number = 0) { + this.x = x + this.y = y + } +} + +export class Vector3 { + static deserializeFromJson(json: any): Vector3 { + let x = json['x'] + let y = json['y'] + let z = json['z'] + if (x == null || y == null || z == null) { + throw new Error() + } + return new Vector3(x, y, z) + } + + x: number + y: number + z: number + + constructor(x: number = 0, y: number = 0, z: number = 0) { + this.x = x + this.y = y + this.z = z + } +} + +export class Vector4 { + static deserializeFromJson(json: any): Vector4 { + let x = json['x'] + let y = json['y'] + let z = json['z'] + let w = json['w'] + if (x == null || y == null || z == null || w == null) { + throw new Error() + } + return new Vector4(x, y, z, w) + } + + x: number + y: number + z: number + w: number + + constructor(x: number = 0, y: number = 0, z: number = 0, w: number = 0) { + this.x = x + this.y = y + this.z = z + this.w = w + } +} + +"; + + public const string VectorTypesByteBuf = @" export class Vector2 implements ISerializable { diff --git a/src/Luban.Job.Proto/Source/JobController.cs b/src/Luban.Job.Proto/Source/JobController.cs index 261889c..d196a2d 100644 --- a/src/Luban.Job.Proto/Source/JobController.cs +++ b/src/Luban.Job.Proto/Source/JobController.cs @@ -186,7 +186,7 @@ namespace Luban.Job.Proto } if (args.EmbedBrightTypes) { - fileContent.Add(TypescriptBrightTypeTemplates.VectorTypes); + fileContent.Add(TypescriptBrightTypeTemplates.VectorTypesByteBuf); fileContent.Add(TypescriptBrightTypeTemplates.SerializeTypes); fileContent.Add(TypescriptBrightTypeTemplates.ProtoTypes); } @@ -197,9 +197,7 @@ namespace Luban.Job.Proto fileContent.Add(string.Format(TypescriptBrightTypeTemplates.VectorImportsFormat, brightRequirePath)); } - fileContent.Add(@$" -export namespace {ass.TopModule} {{ -"); + fileContent.Add(@$"export namespace {ass.TopModule} {{"); foreach (var type in exportTypes) {