【调整】调整cfg typescript有多种输出类型,解决每种类型引入 ByteBuf类及Bright类的方式不一样的问题。

main
walon 2021-07-13 15:15:55 +08:00
parent 6a43f7af6f
commit 00de219e16
6 changed files with 167 additions and 215 deletions

View File

@ -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 ~}}

View File

@ -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<string>
var fileContent = new List<string>();
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<string>
{
@$"
import {{Bright}} from '{byteBufRequirePath}'
//case "code_typescript_bin":
//{
// var render = new TypeScriptBinCodeRender();
// var brightRequirePath = args.TypescriptBrightRequirePath;
// tasks.Add(Task.Run(() =>
// {
// var fileContent = new List<string>();
// 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":
{

View File

@ -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)

View File

@ -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); }} }}";
}

View File

@ -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 {

View File

@ -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)
{