From f617d5f10f4f6fc45398ac229349a86e82841e02 Mon Sep 17 00:00:00 2001 From: walon Date: Wed, 26 Jan 2022 22:19:40 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E7=89=B9=E6=80=A7=E3=80=91=20cfg=20ro?= =?UTF-8?q?ot.xml=20=E6=96=B0=E5=A2=9E=E9=85=8D=E7=BD=AE=E9=A1=B9=20option?= =?UTF-8?q?=20=E3=80=90=E7=89=B9=E6=80=A7=E3=80=91=E6=96=B0=E5=A2=9E=20cod?= =?UTF-8?q?e=5Fcs=5Funity=5Feditor=5Fjson=20=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Luban.Job.Cfg/Source/CfgConstStrings.cs | 13 ++ src/Luban.Job.Cfg/Source/GenArgs.cs | 2 +- ...torRender.cs => CppUE4EditorJsonRender.cs} | 6 +- ...EditorRender.cs => CsUnityEditorRender.cs} | 21 ++- .../TypeVisitors/CsEditorDefineTypeName.cs | 32 +++++ .../TypeVisitors/CsEditorInitValueVisitor.cs | 56 ++++++++ .../Source/TypeVisitors/CsEditorJsonLoad.cs | 127 ++++++++++++++++++ .../Source/TypeVisitors/CsEditorJsonSave.cs | 127 ++++++++++++++++++ .../TypeVisitors/CsEditorNeedInitVisitor.cs | 25 ++++ .../CsEditorUnderlyingDefineTypeName.cs | 30 +++++ ...serialize.cs => CsUnityJsonDeserialize.cs} | 0 .../Source/Utils/TTypeTemplateExtends.cs | 37 +++-- .../Source/Defs/CommonDefLoader.cs | 35 +++-- .../Source/Defs/DefAssemblyBase.cs | 16 +++ .../Source/Defs/DefTypeBase.cs | 4 + .../Source/Defs/LoadDefException.cs | 24 ++++ .../Source/Defs/TTypeTemplateCommonExtends.cs | 10 ++ .../Source/RawDefs/DefinesCommon.cs | 2 + .../Source/TypeVisitors/CsCtorValueVisitor.cs | 7 +- .../Source/TypeVisitors/CsDefineTypeName.cs | 21 ++- .../CsIsRawNullableTypeVisitor.cs | 49 +++++++ .../TypeVisitors/CsUnderingDefineTypeName.cs | 12 +- src/Luban.Job.Common/Source/Types/TEnum.cs | 2 + src/Luban.Server/Luban.Server.csproj | 7 +- .../Templates/config/cs_editor_json/bean.tpl | 90 ------------- .../Templates/config/cs_editor_json/table.tpl | 63 --------- .../config/cs_editor_json/tables.tpl | 16 --- .../Templates/config/cs_json/bean.tpl | 4 +- .../config/cs_unity_editor_json/bean.tpl | 107 +++++++++++++++ .../config/cs_unity_editor_json/enum.tpl | 45 +++++++ 30 files changed, 763 insertions(+), 227 deletions(-) create mode 100644 src/Luban.Job.Cfg/Source/CfgConstStrings.cs rename src/Luban.Job.Cfg/Source/Generate/{CppUE4EditorRender.cs => CppUE4EditorJsonRender.cs} (98%) rename src/Luban.Job.Cfg/Source/Generate/{CsEditorRender.cs => CsUnityEditorRender.cs} (56%) create mode 100644 src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorDefineTypeName.cs create mode 100644 src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorInitValueVisitor.cs create mode 100644 src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorJsonLoad.cs create mode 100644 src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorJsonSave.cs create mode 100644 src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorNeedInitVisitor.cs create mode 100644 src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorUnderlyingDefineTypeName.cs rename src/Luban.Job.Cfg/Source/TypeVisitors/{CsJsonUnityDeserialize.cs => CsUnityJsonDeserialize.cs} (100%) create mode 100644 src/Luban.Job.Common/Source/Defs/LoadDefException.cs create mode 100644 src/Luban.Job.Common/Source/TypeVisitors/CsIsRawNullableTypeVisitor.cs delete mode 100644 src/Luban.Server/Templates/config/cs_editor_json/bean.tpl delete mode 100644 src/Luban.Server/Templates/config/cs_editor_json/table.tpl delete mode 100644 src/Luban.Server/Templates/config/cs_editor_json/tables.tpl create mode 100644 src/Luban.Server/Templates/config/cs_unity_editor_json/bean.tpl create mode 100644 src/Luban.Server/Templates/config/cs_unity_editor_json/enum.tpl diff --git a/src/Luban.Job.Cfg/Source/CfgConstStrings.cs b/src/Luban.Job.Cfg/Source/CfgConstStrings.cs new file mode 100644 index 0000000..adabed4 --- /dev/null +++ b/src/Luban.Job.Cfg/Source/CfgConstStrings.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Luban.Job.Cfg +{ + public static class CfgConstStrings + { + public const string EditorTextTypeName = "Bright.Config.EditorText"; + } +} diff --git a/src/Luban.Job.Cfg/Source/GenArgs.cs b/src/Luban.Job.Cfg/Source/GenArgs.cs index ec25e65..2d6a5bb 100644 --- a/src/Luban.Job.Cfg/Source/GenArgs.cs +++ b/src/Luban.Job.Cfg/Source/GenArgs.cs @@ -8,7 +8,7 @@ namespace Luban.Job.Cfg [Option('s', "service", Required = true, HelpText = "service")] public string Service { get; set; } - [Option("gen_types", Required = true, HelpText = "code_cs_bin,code_cs_json,code_cs_unity_json,code_lua_bin,code_java_bin,code_java_json,code_go_bin,code_go_json,code_cpp_bin,code_python2_json,code_python3_json,code_typescript_bin,code_typescript_json,code_rust_json,code_protobuf,code_template,code_flatbuffers,data_bin,data_lua,data_json,data_json2,data_json_monolithic,data_resources,data_template,data_protobuf_bin,data_protobuf_json,data_flatbuffers_json,convert_json,convert_lua,convert_xlsx . can be multi")] + [Option("gen_types", Required = true, HelpText = "code_cs_bin,code_cs_json,code_cs_unity_json,code_cs_unity_editor_json,code_lua_bin,code_java_bin,code_java_json,code_go_bin,code_go_json,code_cpp_bin,code_cpp_ue_editor_json,code_python2_json,code_python3_json,code_typescript_bin,code_typescript_json,code_rust_json,code_protobuf,code_template,code_flatbuffers,data_bin,data_lua,data_json,data_json2,data_json_monolithic,data_resources,data_template,data_protobuf_bin,data_protobuf_json,data_flatbuffers_json,convert_json,convert_lua,convert_xlsx . can be multi")] public string GenType { get; set; } [Option("input_data_dir", Required = true, HelpText = "input data dir")] diff --git a/src/Luban.Job.Cfg/Source/Generate/CppUE4EditorRender.cs b/src/Luban.Job.Cfg/Source/Generate/CppUE4EditorJsonRender.cs similarity index 98% rename from src/Luban.Job.Cfg/Source/Generate/CppUE4EditorRender.cs rename to src/Luban.Job.Cfg/Source/Generate/CppUE4EditorJsonRender.cs index 399fcae..565e6bc 100644 --- a/src/Luban.Job.Cfg/Source/Generate/CppUE4EditorRender.cs +++ b/src/Luban.Job.Cfg/Source/Generate/CppUE4EditorJsonRender.cs @@ -12,12 +12,12 @@ using System.Threading.Tasks; namespace Luban.Job.Cfg.Generate { - [Render("code_cpp_ue_editor")] - class CppUE4EditorRender : CodeRenderBase + [Render("code_cpp_ue_editor_json")] + class CppUE4EditorJsonRender : CodeRenderBase { public override void Render(GenContext ctx) { - var render = new CppUE4EditorRender(); + var render = new CppUE4EditorJsonRender(); var renderTypes = ctx.Assembly.Types.Values.Where(c => c is DefEnum || c is DefBean).ToList(); diff --git a/src/Luban.Job.Cfg/Source/Generate/CsEditorRender.cs b/src/Luban.Job.Cfg/Source/Generate/CsUnityEditorRender.cs similarity index 56% rename from src/Luban.Job.Cfg/Source/Generate/CsEditorRender.cs rename to src/Luban.Job.Cfg/Source/Generate/CsUnityEditorRender.cs index 6bd14e1..d178f16 100644 --- a/src/Luban.Job.Cfg/Source/Generate/CsEditorRender.cs +++ b/src/Luban.Job.Cfg/Source/Generate/CsUnityEditorRender.cs @@ -1,22 +1,37 @@ using Luban.Common.Protos; using Luban.Job.Cfg.Defs; using Luban.Job.Common; +using Luban.Job.Common.Defs; using Luban.Job.Common.Generate; +using Luban.Job.Common.Tpl; using Luban.Job.Common.Utils; using System.Collections.Generic; using System.Threading.Tasks; namespace Luban.Job.Cfg.Generate { - [Render("code_cs_unity_editor")] - class CsEditorRender : TemplateCodeRenderBase + [Render("code_cs_unity_editor_json")] + class CsUnityEditorRender : TemplateCodeRenderBase { - override protected string RenderTemplateDir => "cs_editor_json"; + override protected string RenderTemplateDir => "cs_unity_editor_json"; + + + public override string Render(DefEnum e) + { + var template = StringTemplateManager.Ins.GetTemplate($"config/{RenderTemplateDir}/enum"); + var result = template.RenderCode(e); + return result; + } public override void Render(GenContext ctx) { + ctx.Assembly.CurrentLanguage = ELanguage.CS; foreach (var c in ctx.Assembly.Types.Values) { + if (!(c is DefBean) && !(c is DefEnum)) + { + continue; + } ctx.Tasks.Add(Task.Run(() => { var content = FileHeaderUtil.ConcatAutoGenerationHeader(RenderAny(c), ELanguage.CS); diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorDefineTypeName.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorDefineTypeName.cs new file mode 100644 index 0000000..0904a01 --- /dev/null +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorDefineTypeName.cs @@ -0,0 +1,32 @@ +using Luban.Job.Common.Types; +using Luban.Job.Common.TypeVisitors; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Luban.Job.Cfg.TypeVisitors +{ + public class CsEditorDefineTypeName : CsDefineTypeName + { + public static new CsEditorDefineTypeName Ins { get; } = new CsEditorDefineTypeName(); + + protected override ITypeFuncVisitor UnderlyingVisitor => CsEditorUnderlyingDefineTypeName.Ins; + + public override string Accept(TDateTime type) + { + return "string"; + } + + public override string Accept(TEnum type) + { + return "string"; + } + + public override string Accept(TText type) + { + return "Bright.Config.EditorText"; + } + } +} diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorInitValueVisitor.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorInitValueVisitor.cs new file mode 100644 index 0000000..e010e85 --- /dev/null +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorInitValueVisitor.cs @@ -0,0 +1,56 @@ +using Luban.Job.Common; +using Luban.Job.Common.Types; +using Luban.Job.Common.TypeVisitors; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Luban.Job.Cfg.TypeVisitors +{ + public class CsEditorInitValueVisitor : CsCtorValueVisitor + { + public static new CsEditorInitValueVisitor Ins { get; } = new CsEditorInitValueVisitor(); + + public override string Accept(TEnum type) + { + return $"\"{(type.DefineEnum.Items.Count > 0 ? type.DefineEnum.Items[0].Name : "")}\""; + } + + public override string Accept(TText type) + { + return $"new {CfgConstStrings.EditorTextTypeName}(\"\", \"\")"; + } + + public override string Accept(TDateTime type) + { + return "\"1970-01-01 00:00:00\""; + } + + public override string Accept(TBean type) + { + return type.IsNullable || type.Bean.IsAbstractType ? "default" : $"new {type.Apply(CsEditorUnderlyingDefineTypeName.Ins)}()"; + } + + public override string Accept(TArray type) + { + return $"System.Array.Empty<{type.ElementType.Apply(CsEditorDefineTypeName.Ins)}>()"; + } + + public override string Accept(TList type) + { + return $"new {ConstStrings.CsList}<{type.ElementType.Apply(CsEditorDefineTypeName.Ins)}>()"; + } + + public override string Accept(TSet type) + { + return $"new {ConstStrings.CsHashSet}<{type.ElementType.Apply(CsEditorDefineTypeName.Ins)}>()"; + } + + public override string Accept(TMap type) + { + return $"new {ConstStrings.CsHashMap}<{type.KeyType.Apply(CsEditorDefineTypeName.Ins)},{type.ValueType.Apply(CsEditorDefineTypeName.Ins)}>()"; + } + } +} diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorJsonLoad.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorJsonLoad.cs new file mode 100644 index 0000000..84971bd --- /dev/null +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorJsonLoad.cs @@ -0,0 +1,127 @@ +using Luban.Job.Cfg.Datas; +using Luban.Job.Common.Types; +using Luban.Job.Common.TypeVisitors; +using System; + +namespace Luban.Job.Cfg.TypeVisitors +{ + class CsEditorJsonLoad : ITypeFuncVisitor + { + public static CsEditorJsonLoad Ins { get; } = new(); + + public string Accept(TBool type, string json, string x) + { + return $"if(!{json}.IsBoolean) {{ throw new SerializationException(); }} {x} = {json};"; + } + + public string Accept(TByte type, string json, string x) + { + return $"if(!{json}.IsNumber) {{ throw new SerializationException(); }} {x} = {json};"; + } + + public string Accept(TShort type, string json, string x) + { + return $"if(!{json}.IsNumber) {{ throw new SerializationException(); }} {x} = {json};"; + } + + public string Accept(TFshort type, string json, string x) + { + return $"if(!{json}.IsNumber) {{ throw new SerializationException(); }} {x} = {json};"; + } + + public string Accept(TInt type, string json, string x) + { + return $"if(!{json}.IsNumber) {{ throw new SerializationException(); }} {x} = {json};"; + } + + public string Accept(TFint type, string json, string x) + { + return $"if(!{json}.IsNumber) {{ throw new SerializationException(); }} {x} = {json};"; + } + + public string Accept(TLong type, string json, string x) + { + return $"if(!{json}.IsNumber) {{ throw new SerializationException(); }} {x} = {json};"; + } + + public string Accept(TFlong type, string json, string x) + { + return $"if(!{json}.IsNumber) {{ throw new SerializationException(); }} {x} = {json};"; + } + + public string Accept(TFloat type, string json, string x) + { + return $"if(!{json}.IsNumber) {{ throw new SerializationException(); }} {x} = {json};"; + } + + public string Accept(TDouble type, string json, string x) + { + return $"if(!{json}.IsNumber) {{ throw new SerializationException(); }} {x} = {json};"; + } + + public string Accept(TEnum type, string json, string x) + { + return $"if(!{json}.IsString) {{ throw new SerializationException(); }} {x} = {json};"; + } + + public string Accept(TString type, string json, string x) + { + return $"if(!{json}.IsString) {{ throw new SerializationException(); }} {x} = {json};"; + } + + public string Accept(TBytes type, string json, string x) + { + throw new NotSupportedException(); + } + + public string Accept(TText type, string json, string x) + { + return $"{x} = {type.Apply(CsEditorUnderlyingDefineTypeName.Ins)}.LoadJson({json});"; + } + + public string Accept(TBean type, string json, string x) + { + return $"if(!{json}.IsObject) {{ throw new SerializationException(); }} {x} = {type.Apply(CsEditorUnderlyingDefineTypeName.Ins)}.LoadJson{type.Bean.Name}({json});"; + } + + public string Accept(TArray type, string json, string x) + { + return $"if(!{json}.IsArray) {{ throw new SerializationException(); }} int _n = {json}.Count; {x} = new {type.ElementType.Apply(CsEditorDefineTypeName.Ins)}[_n]; int _index=0; foreach(SimpleJSON.JSONNode __e in {json}.Children) {{ {type.ElementType.Apply(CsEditorDefineTypeName.Ins)} __v; {type.ElementType.Apply(this, "__e", "__v")} {x}[_index++] = __v; }} "; + } + + public string Accept(TList type, string json, string x) + { + return $"if(!{json}.IsArray) {{ throw new SerializationException(); }} {x} = new {type.Apply(CsEditorDefineTypeName.Ins)}(); foreach(JSONNode __e in {json}.Children) {{ {type.ElementType.Apply(CsEditorDefineTypeName.Ins)} __v; {type.ElementType.Apply(this, "__e", "__v")} {x}.Add(__v); }} "; + } + + public string Accept(TSet type, string json, string x) + { + return $"if(!{json}.IsArray) {{ throw new SerializationException(); }} {x} = new {type.Apply(CsEditorDefineTypeName.Ins)}(); foreach(JSONNode __e in {json}.Children) {{ {type.ElementType.Apply(CsEditorDefineTypeName.Ins)} __v; {type.ElementType.Apply(this, "__e", "__v")} {x}.Add(__v); }} "; + } + + public string Accept(TMap type, string json, string x) + { + return @$"if(!{json}.IsArray) {{ throw new SerializationException(); }} {x} = new {type.Apply(CsEditorDefineTypeName.Ins)}(); foreach(JSONNode __e in {json}.Children) {{ {type.KeyType.Apply(CsEditorDefineTypeName.Ins)} __k; {type.KeyType.Apply(this, "__e[0]", "__k")} {type.ValueType.Apply(CsEditorDefineTypeName.Ins)} __v; {type.ValueType.Apply(this, "__e[1]", "__v")} {x}.Add(__k, __v); }} "; + } + + public string Accept(TVector2 type, string json, string x) + { + return $"if(!{json}.IsObject) {{ throw new SerializationException(); }} float __x; {TFloat.Ins.Apply(this, $"{json}[\"x\"]", "__x") } float __y; {TFloat.Ins.Apply(this, $"{json}[\"y\"]", "__y") } {x} = new {type.Apply(CsEditorDefineTypeName.Ins)}(__x, __y);"; + } + + public string Accept(TVector3 type, string json, string x) + { + return $"if(!{json}.IsObject) {{ throw new SerializationException(); }} float __x; {TFloat.Ins.Apply(this, $"{json}[\"x\"]", "__x") } float __y; {TFloat.Ins.Apply(this, $"{json}[\"y\"]", "__y") } float __z; {TFloat.Ins.Apply(this, $"{json}[\"z\"]", "__z") } {x} = new {type.Apply(CsEditorDefineTypeName.Ins)}(__x, __y,__z);"; + } + + public string Accept(TVector4 type, string json, string x) + { + return $"if(!{json}.IsObject) {{ throw new SerializationException(); }} float __x; {TFloat.Ins.Apply(this, $"{json}[\"x\"]", "__x") } float __y; {TFloat.Ins.Apply(this, $"{json}[\"y\"]", "__y") } float __z; {TFloat.Ins.Apply(this, $"{json}[\"z\"]", "__z") } float __w; {TFloat.Ins.Apply(this, $"{json}[\"w\"]", "__w") } {x} = new {type.Apply(CsEditorDefineTypeName.Ins)}(__x, __y, __z, __w);"; + } + + public string Accept(TDateTime type, string json, string x) + { + return $"if(!{json}.IsString) {{ throw new SerializationException(); }} {x} = {json};"; + } + } +} diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorJsonSave.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorJsonSave.cs new file mode 100644 index 0000000..93c8759 --- /dev/null +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorJsonSave.cs @@ -0,0 +1,127 @@ +using Luban.Job.Cfg.Datas; +using Luban.Job.Common.Types; +using Luban.Job.Common.TypeVisitors; +using System; + +namespace Luban.Job.Cfg.TypeVisitors +{ + class CsEditorJsonSave : ITypeFuncVisitor + { + public static CsEditorJsonSave Ins { get; } = new(); + + public string Accept(TBool type, string jsonName, string jsonFieldName, string value) + { + return $"{jsonName}[\"{jsonFieldName}\"] = new JSONBool({value}{(type.IsNullable ? ".Value" : "")});"; + } + + public string Accept(TByte type, string jsonName, string jsonFieldName, string value) + { + return $"{jsonName}[\"{jsonFieldName}\"] = new JSONNumber({value}{(type.IsNullable ? ".Value" : "")});"; + } + + public string Accept(TShort type, string jsonName, string jsonFieldName, string value) + { + return $"{jsonName}[\"{jsonFieldName}\"] = new JSONNumber({value}{(type.IsNullable ? ".Value" : "")});"; + } + + public string Accept(TFshort type, string jsonName, string jsonFieldName, string value) + { + return $"{jsonName}[\"{jsonFieldName}\"] = new JSONNumber({value}{(type.IsNullable ? ".Value" : "")});"; + } + + public string Accept(TInt type, string jsonName, string jsonFieldName, string value) + { + return $"{jsonName}[\"{jsonFieldName}\"] = new JSONNumber({value}{(type.IsNullable ? ".Value" : "")});"; + } + + public string Accept(TFint type, string jsonName, string jsonFieldName, string value) + { + return $"{jsonName}[\"{jsonFieldName}\"] = new JSONNumber({value}{(type.IsNullable ? ".Value" : "")});"; + } + + public string Accept(TLong type, string jsonName, string jsonFieldName, string value) + { + return $"{jsonName}[\"{jsonFieldName}\"] = new JSONNumber({value}{(type.IsNullable ? ".Value" : "")});"; + } + + public string Accept(TFlong type, string jsonName, string jsonFieldName, string value) + { + return $"{jsonName}[\"{jsonFieldName}\"] = new JSONNumber({value}{(type.IsNullable ? ".Value" : "")});"; + } + + public string Accept(TFloat type, string jsonName, string jsonFieldName, string value) + { + return $"{jsonName}[\"{jsonFieldName}\"] = new JSONNumber({value}{(type.IsNullable ? ".Value" : "")});"; + } + + public string Accept(TDouble type, string jsonName, string jsonFieldName, string value) + { + return $"{jsonName}[\"{jsonFieldName}\"] = new JSONNumber({value}{(type.IsNullable ? ".Value" : "")});"; + } + + public string Accept(TEnum type, string jsonName, string jsonFieldName, string value) + { + return $"{jsonName}[\"{jsonFieldName}\"] = new JSONString({value});"; + } + + public string Accept(TString type, string jsonName, string jsonFieldName, string value) + { + return $"{jsonName}[\"{jsonFieldName}\"] = new JSONString({value});"; + } + + public string Accept(TText type, string jsonName, string jsonFieldName, string value) + { + return $"{jsonName}[\"{jsonFieldName}\"] = {CfgConstStrings.EditorTextTypeName}.SaveJson({value});"; + } + + public string Accept(TBytes type, string jsonName, string jsonFieldName, string value) + { + throw new NotSupportedException(); + } + + public string Accept(TVector2 type, string jsonName, string jsonFieldName, string value) + { + return $"{{ var __vjson = new JSONObject(); __vjson[\"x\"] = {value}{(type.IsNullable ? ".Value" : "")}.X; __vjson[\"y\"] = {value}{(type.IsNullable ? ".Value" : "")}.Y; {jsonName}[\"{jsonFieldName}\"] = __vjson; }}"; + } + + public string Accept(TVector3 type, string jsonName, string jsonFieldName, string value) + { + return $"{{ var __vjson = new JSONObject(); __vjson[\"x\"] = {value}{(type.IsNullable ? ".Value" : "")}.X; __vjson[\"y\"] = {value}{(type.IsNullable ? ".Value" : "")}.Y; __vjson[\"z\"] = {value}{(type.IsNullable ? ".Value" : "")}.Z; {jsonName}[\"{jsonFieldName}\"] = __vjson; }}"; + } + + public string Accept(TVector4 type, string jsonName, string jsonFieldName, string value) + { + return $"{{ var __vjson = new JSONObject(); __vjson[\"x\"] = {value}{(type.IsNullable ? ".Value" : "")}.X; __vjson[\"y\"] = {value}{(type.IsNullable ? ".Value" : "")}.Y; __vjson[\"z\"] = {value}{(type.IsNullable ? ".Value" : "")}.Z; __vjson[\"w\"] = {value}{(type.IsNullable ? ".Value" : "")}.W; {jsonName}[\"{jsonFieldName}\"] = __vjson; }}"; + } + + public string Accept(TDateTime type, string jsonName, string jsonFieldName, string value) + { + return $"{jsonName}[\"{jsonFieldName}\"] = new JSONString({value});"; + } + + public string Accept(TBean type, string jsonName, string jsonFieldName, string value) + { + return $"{{ var __bjson = new JSONObject(); {type.Apply(CsEditorUnderlyingDefineTypeName.Ins)}.SaveJson{type.Bean.Name}({value}, __bjson); {jsonName}[\"{jsonFieldName}\"] = __bjson; }}"; + } + + public string Accept(TArray type, string jsonName, string jsonFieldName, string value) + { + return $"{{ var __cjson = new JSONArray(); foreach(var _e in {value}) {{ {type.ElementType.Apply(this, "__cjson", "null", "_e")} }} {jsonName}[\"{jsonFieldName}\"] = __cjson; }}"; + } + + public string Accept(TList type, string jsonName, string jsonFieldName, string value) + { + return $"{{ var __cjson = new JSONArray(); foreach(var _e in {value}) {{ {type.ElementType.Apply(this, "__cjson", "null", "_e")} }} {jsonName}[\"{jsonFieldName}\"] = __cjson; }}"; + } + + public string Accept(TSet type, string jsonName, string jsonFieldName, string value) + { + return $"{{ var __cjson = new JSONArray(); foreach(var _e in {value}) {{ {type.ElementType.Apply(this, "__cjson", "null", "_e")} }} {jsonName}[\"{jsonFieldName}\"] = __cjson; }}"; + } + + public string Accept(TMap type, string jsonName, string jsonFieldName, string value) + { + return $"{{ var __cjson = new JSONArray(); foreach(var _e in {value}) {{ var __entry = new JSONArray(); {type.KeyType.Apply(this, "__entry", "null", "_e.Key")} {type.ValueType.Apply(this, "__entry", "null", "_e.Value")} }} {jsonName}[\"{jsonFieldName}\"] = __cjson; }}"; + } + } +} diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorNeedInitVisitor.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorNeedInitVisitor.cs new file mode 100644 index 0000000..a391108 --- /dev/null +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorNeedInitVisitor.cs @@ -0,0 +1,25 @@ +using Luban.Job.Common.Types; +using Luban.Job.Common.TypeVisitors; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Luban.Job.Cfg.TypeVisitors +{ + public class CsEditorNeedInitVisitor : CsNeedInitVisitor + { + public static new CsEditorNeedInitVisitor Ins { get; } = new CsEditorNeedInitVisitor(); + + public override bool Accept(TEnum type) + { + return true; + } + + public override bool Accept(TDateTime type) + { + return true; + } + } +} diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorUnderlyingDefineTypeName.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorUnderlyingDefineTypeName.cs new file mode 100644 index 0000000..5babbec --- /dev/null +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorUnderlyingDefineTypeName.cs @@ -0,0 +1,30 @@ +using Luban.Job.Common.Types; +using Luban.Job.Common.TypeVisitors; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Luban.Job.Cfg.TypeVisitors +{ + public class CsEditorUnderlyingDefineTypeName : CsUnderingDefineTypeName + { + public static new CsEditorUnderlyingDefineTypeName Ins { get; } = new CsEditorUnderlyingDefineTypeName(); + + public override string Accept(TEnum type) + { + return "string"; + } + + public override string Accept(TText type) + { + return CfgConstStrings.EditorTextTypeName; + } + + public override string Accept(TDateTime type) + { + return "string"; + } + } +} diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/CsJsonUnityDeserialize.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/CsUnityJsonDeserialize.cs similarity index 100% rename from src/Luban.Job.Cfg/Source/TypeVisitors/CsJsonUnityDeserialize.cs rename to src/Luban.Job.Cfg/Source/TypeVisitors/CsUnityJsonDeserialize.cs diff --git a/src/Luban.Job.Cfg/Source/Utils/TTypeTemplateExtends.cs b/src/Luban.Job.Cfg/Source/Utils/TTypeTemplateExtends.cs index 91636ef..71b7a53 100644 --- a/src/Luban.Job.Cfg/Source/Utils/TTypeTemplateExtends.cs +++ b/src/Luban.Job.Cfg/Source/Utils/TTypeTemplateExtends.cs @@ -228,13 +228,34 @@ namespace Luban.Job.Cfg.Utils return string.Join(", ", table.IndexList.Select(idx => $"{idx.IndexField.Name}")); } - //public static string DeserializeTextKeyField(DefField field, string lan, string bufName) - //{ - // switch (lan) - // { - // case "cpp": return $"{CppDefineTypeName.Ins.Accept(TString.Ins)} {field.CppStyleName}{L10N_FIELD_SUFFIX};"; - // default: throw new NotSupportedException($"not support lan:{lan}"); - // } - //} + public static string CsEditorDefineType(TType type) + { + return type.Apply(CsEditorDefineTypeName.Ins); + } + + public static string CsUnityEditorJsonLoad(string jsonName, string fieldName, TType type) + { + return $"{type.Apply(CsEditorJsonLoad.Ins, jsonName, fieldName)}"; + } + + public static string CsUnityEditorJsonSave(string jsonName, string jsonFieldName, string fieldName, TType type) + { + return $"{type.Apply(CsEditorJsonSave.Ins, jsonName, jsonFieldName, fieldName)}"; + } + + public static bool CsIsEditorRawNullable(TType type) + { + return type.Apply(CsIsRawNullableTypeVisitor.Ins); + } + + public static bool CsEditorNeedInit(TType type) + { + return type.Apply(CsEditorNeedInitVisitor.Ins); + } + + public static string CsEditorInitValue(TType type) + { + return type.Apply(CsEditorInitValueVisitor.Ins); + } } } diff --git a/src/Luban.Job.Common/Source/Defs/CommonDefLoader.cs b/src/Luban.Job.Common/Source/Defs/CommonDefLoader.cs index c7ba11f..dd28113 100644 --- a/src/Luban.Job.Common/Source/Defs/CommonDefLoader.cs +++ b/src/Luban.Job.Common/Source/Defs/CommonDefLoader.cs @@ -4,30 +4,11 @@ using Luban.Job.Common.Utils; using Luban.Server.Common; using System; using System.Collections.Generic; -using System.Runtime.Serialization; using System.Threading.Tasks; using System.Xml.Linq; namespace Luban.Job.Common.Defs { - public class LoadDefException : Exception - { - public LoadDefException() - { - } - - public LoadDefException(string message) : base(message) - { - } - - public LoadDefException(string message, Exception innerException) : base(message, innerException) - { - } - - protected LoadDefException(SerializationInfo info, StreamingContext context) : base(info, context) - { - } - } public abstract class CommonDefLoader { @@ -53,11 +34,14 @@ namespace Luban.Job.Common.Defs protected readonly HashSet _externalSelectors = new(); protected readonly Dictionary _externalTypes = new(); + protected readonly Dictionary _options = new(); + protected CommonDefLoader(IAgent agent) { Agent = agent; _rootDefineHandlers.Add("topmodule", SetTopModule); + _rootDefineHandlers.Add("option", AddOption); _rootDefineHandlers.Add("externalselector", AddExternalSelector); _moduleDefineHandlers.Add("module", AddModule); @@ -115,6 +99,7 @@ namespace Luban.Job.Common.Defs defines.Beans = _beans; defines.ExternalSelectors = _externalSelectors; defines.ExternalTypes = _externalTypes; + defines.Options = _options; } #region root handler @@ -124,6 +109,18 @@ namespace Luban.Job.Common.Defs this.TopModule = XmlUtil.GetOptionalAttribute(e, "name"); } + private static readonly List _optionRequireAttrs = new List { "name", "value", }; + + private void AddOption(XElement e) + { + ValidAttrKeys(_rootXml, e, null, _optionRequireAttrs); + string name = XmlUtil.GetRequiredAttribute(e, "name"); + if (!_options.TryAdd(name, XmlUtil.GetRequiredAttribute(e, "value"))) + { + throw new LoadDefException($"option name:'{name}' duplicate"); + } + } + private async Task AddImportAsync(string xmlFile) { var rootFileName = FileUtil.GetFileName(_rootXml); diff --git a/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs b/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs index f41846c..3fbfd61 100644 --- a/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs +++ b/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs @@ -63,6 +63,20 @@ namespace Luban.Job.Common.Defs public List CurrentExternalSelectors { get; private set; } + public Dictionary Options { get; private set; } + + public string EditorTopModule { get; private set; } + + public bool ContainsOption(string optionName) + { + return Options.ContainsKey(optionName); + } + + public string GetOption(string optionName) + { + return Options.TryGetValue(optionName, out var value) ? value : null; + } + private void SetCurrentExternalSelectors(string selectors) { if (string.IsNullOrEmpty(selectors)) @@ -91,6 +105,8 @@ namespace Luban.Job.Common.Defs this.TopModule = defines.TopModule; this.ExternalSelectors = defines.ExternalSelectors; this.ExternalTypes = defines.ExternalTypes; + this.Options = defines.Options; + this.EditorTopModule = defines.Options.TryGetValue("editor.topmodule", out var editorTopModule) ? editorTopModule : TypeUtil.MakeFullName("editor", defines.TopModule); this.Args = args; diff --git a/src/Luban.Job.Common/Source/Defs/DefTypeBase.cs b/src/Luban.Job.Common/Source/Defs/DefTypeBase.cs index d7dad10..735f23b 100644 --- a/src/Luban.Job.Common/Source/Defs/DefTypeBase.cs +++ b/src/Luban.Job.Common/Source/Defs/DefTypeBase.cs @@ -31,6 +31,10 @@ namespace Luban.Job.Common.Defs public string FullNameWithTopModule => TypeUtil.MakeFullName(AssemblyBase.TopModule, FullName); + public string NamespaceWithEditorTopModule => TypeUtil.MakeNamespace(AssemblyBase.EditorTopModule, Namespace); + + public string FullNameWithEditorTopModule => TypeUtil.MakeFullName(AssemblyBase.EditorTopModule, FullName); + public string CsFullName => TypeUtil.MakeFullName(Namespace, Name); public string JavaFullName => TypeUtil.MakeFullName(Namespace, Name); diff --git a/src/Luban.Job.Common/Source/Defs/LoadDefException.cs b/src/Luban.Job.Common/Source/Defs/LoadDefException.cs new file mode 100644 index 0000000..7877a16 --- /dev/null +++ b/src/Luban.Job.Common/Source/Defs/LoadDefException.cs @@ -0,0 +1,24 @@ +using System; +using System.Runtime.Serialization; + +namespace Luban.Job.Common.Defs +{ + public class LoadDefException : Exception + { + public LoadDefException() + { + } + + public LoadDefException(string message) : base(message) + { + } + + public LoadDefException(string message, Exception innerException) : base(message, innerException) + { + } + + protected LoadDefException(SerializationInfo info, StreamingContext context) : base(info, context) + { + } + } +} diff --git a/src/Luban.Job.Common/Source/Defs/TTypeTemplateCommonExtends.cs b/src/Luban.Job.Common/Source/Defs/TTypeTemplateCommonExtends.cs index cb7dd8b..159f25b 100644 --- a/src/Luban.Job.Common/Source/Defs/TTypeTemplateCommonExtends.cs +++ b/src/Luban.Job.Common/Source/Defs/TTypeTemplateCommonExtends.cs @@ -288,5 +288,15 @@ namespace Luban.Job.Common.Defs { return obj.GetTag(attrName); } + + public static bool HasOption(string optionName) + { + return DefAssemblyBase.LocalAssebmly.ContainsOption(optionName); + } + + public static string GetOption(string optionName) + { + return DefAssemblyBase.LocalAssebmly.GetOption(optionName); + } } } diff --git a/src/Luban.Job.Common/Source/RawDefs/DefinesCommon.cs b/src/Luban.Job.Common/Source/RawDefs/DefinesCommon.cs index 1997345..fd68651 100644 --- a/src/Luban.Job.Common/Source/RawDefs/DefinesCommon.cs +++ b/src/Luban.Job.Common/Source/RawDefs/DefinesCommon.cs @@ -10,6 +10,8 @@ namespace Luban.Job.Common.RawDefs { public string TopModule { get; set; } = ""; + public Dictionary Options { get; set; } = new(); + public HashSet ExternalSelectors { get; set; } = new(); public Dictionary ExternalTypes { get; set; } = new(); diff --git a/src/Luban.Job.Common/Source/TypeVisitors/CsCtorValueVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/CsCtorValueVisitor.cs index b843064..0c9fc80 100644 --- a/src/Luban.Job.Common/Source/TypeVisitors/CsCtorValueVisitor.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/CsCtorValueVisitor.cs @@ -11,7 +11,6 @@ namespace Luban.Job.Common.TypeVisitors return "default"; } - public override string Accept(TString type) { return "\"\""; @@ -39,17 +38,17 @@ namespace Luban.Job.Common.TypeVisitors public override string Accept(TList type) { - return $"new System.Collections.Generic.List<{type.ElementType.Apply(CsDefineTypeName.Ins)}>()"; + return $"new {ConstStrings.CsList}<{type.ElementType.Apply(CsDefineTypeName.Ins)}>()"; } public override string Accept(TSet type) { - return $"new System.Collections.Generic.HashSet<{type.ElementType.Apply(CsDefineTypeName.Ins)}>()"; + return $"new {ConstStrings.CsHashSet}<{type.ElementType.Apply(CsDefineTypeName.Ins)}>()"; } public override string Accept(TMap type) { - return $"new System.Collections.Generic.Dictionary<{type.KeyType.Apply(CsDefineTypeName.Ins)},{type.ValueType.Apply(CsDefineTypeName.Ins)}>()"; + return $"new {ConstStrings.CsHashMap}<{type.KeyType.Apply(CsDefineTypeName.Ins)},{type.ValueType.Apply(CsDefineTypeName.Ins)}>()"; } } } diff --git a/src/Luban.Job.Common/Source/TypeVisitors/CsDefineTypeName.cs b/src/Luban.Job.Common/Source/TypeVisitors/CsDefineTypeName.cs index fc0d0b8..aa3085a 100644 --- a/src/Luban.Job.Common/Source/TypeVisitors/CsDefineTypeName.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/CsDefineTypeName.cs @@ -6,24 +6,31 @@ namespace Luban.Job.Common.TypeVisitors { public static CsDefineTypeName Ins { get; } = new CsDefineTypeName(); + protected virtual ITypeFuncVisitor UnderlyingVisitor => CsUnderingDefineTypeName.Ins; + public override string DoAccept(TType type) { - return type.IsNullable ? (type.Apply(CsUnderingDefineTypeName.Ins) + "?") : type.Apply(CsUnderingDefineTypeName.Ins); + return type.IsNullable && !type.Apply(CsIsRawNullableTypeVisitor.Ins) ? (type.Apply(UnderlyingVisitor) + "?") : type.Apply(UnderlyingVisitor); } - public override string Accept(TString type) + public override string Accept(TArray type) { - return type.Apply(CsUnderingDefineTypeName.Ins); + return $"{type.ElementType.Apply(this)}[]"; } - public override string Accept(TText type) + public override string Accept(TList type) { - return type.Apply(CsUnderingDefineTypeName.Ins); + return $"{ConstStrings.CsList}<{type.ElementType.Apply(this)}>"; } - public override string Accept(TBean type) + public override string Accept(TSet type) { - return type.Apply(CsUnderingDefineTypeName.Ins); + return $"{ConstStrings.CsHashSet}<{type.ElementType.Apply(this)}>"; + } + + public override string Accept(TMap type) + { + return $"{ConstStrings.CsHashMap}<{type.KeyType.Apply(this)}, {type.ValueType.Apply(this)}>"; } } } diff --git a/src/Luban.Job.Common/Source/TypeVisitors/CsIsRawNullableTypeVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/CsIsRawNullableTypeVisitor.cs new file mode 100644 index 0000000..43c227d --- /dev/null +++ b/src/Luban.Job.Common/Source/TypeVisitors/CsIsRawNullableTypeVisitor.cs @@ -0,0 +1,49 @@ +using Luban.Job.Common.Types; + +namespace Luban.Job.Common.TypeVisitors +{ + public class CsIsRawNullableTypeVisitor : AllFalseVisitor + { + public static CsIsRawNullableTypeVisitor Ins { get; } = new CsIsRawNullableTypeVisitor(); + + public override bool Accept(TString type) + { + return true; + } + + public override bool Accept(TText type) + { + return true; + } + + public override bool Accept(TBytes type) + { + return true; + } + + public override bool Accept(TArray type) + { + return true; + } + + public override bool Accept(TList type) + { + return true; + } + + public override bool Accept(TSet type) + { + return true; + } + + public override bool Accept(TMap type) + { + return true; + } + + public override bool Accept(TBean type) + { + return true; + } + } +} diff --git a/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDefineTypeName.cs b/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDefineTypeName.cs index 87053b3..eb48f2b 100644 --- a/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDefineTypeName.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDefineTypeName.cs @@ -58,7 +58,7 @@ namespace Luban.Job.Common.TypeVisitors return "double"; } - public string Accept(TEnum type) + public virtual string Accept(TEnum type) { return ExternalTypeUtil.CsMapperToExternalType(type.DefineEnum); } @@ -73,7 +73,7 @@ namespace Luban.Job.Common.TypeVisitors return "byte[]"; } - public string Accept(TText type) + public virtual string Accept(TText type) { return "string"; } @@ -90,17 +90,17 @@ namespace Luban.Job.Common.TypeVisitors public string Accept(TList type) { - return $"System.Collections.Generic.List<{type.ElementType.Apply(this)}>"; + return $"{ConstStrings.CsList}<{type.ElementType.Apply(this)}>"; } public string Accept(TSet type) { - return $"System.Collections.Generic.HashSet<{type.ElementType.Apply(this)}>"; + return $"{ConstStrings.CsHashSet}<{type.ElementType.Apply(this)}>"; } public string Accept(TMap type) { - return $"System.Collections.Generic.Dictionary<{type.KeyType.Apply(this)}, {type.ValueType.Apply(this)}>"; + return $"{ConstStrings.CsHashMap}<{type.KeyType.Apply(this)}, {type.ValueType.Apply(this)}>"; } public string Accept(TVector2 type) @@ -130,7 +130,7 @@ namespace Luban.Job.Common.TypeVisitors return "System.Numerics.Vector4"; } - public string Accept(TDateTime type) + public virtual string Accept(TDateTime type) { return "int"; } diff --git a/src/Luban.Job.Common/Source/Types/TEnum.cs b/src/Luban.Job.Common/Source/Types/TEnum.cs index 2742047..14da468 100644 --- a/src/Luban.Job.Common/Source/Types/TEnum.cs +++ b/src/Luban.Job.Common/Source/Types/TEnum.cs @@ -15,6 +15,8 @@ namespace Luban.Job.Common.Types public override string TypeName => "enum"; + public bool IsEnum => true; + public DefEnum DefineEnum { get; } private TEnum(bool isNullable, Dictionary tags, DefEnum defEnum) : base(isNullable, tags) diff --git a/src/Luban.Server/Luban.Server.csproj b/src/Luban.Server/Luban.Server.csproj index 878f682..381fb8f 100644 --- a/src/Luban.Server/Luban.Server.csproj +++ b/src/Luban.Server/Luban.Server.csproj @@ -100,13 +100,10 @@ PreserveNewest - + PreserveNewest - - PreserveNewest - - + PreserveNewest diff --git a/src/Luban.Server/Templates/config/cs_editor_json/bean.tpl b/src/Luban.Server/Templates/config/cs_editor_json/bean.tpl deleted file mode 100644 index 23860a3..0000000 --- a/src/Luban.Server/Templates/config/cs_editor_json/bean.tpl +++ /dev/null @@ -1,90 +0,0 @@ -using Bright.Serialization; - -namespace {{namespace}} -{ - -public {{cs_class_modifier}} class {{name}} : {{if parent_def_type}} {{parent}} {{else}} ISerializable {{if is_abstract_type}}, ITypeId {{end}} {{end}} -{ - public {{name}}() {{if parent_def_type}} : base() {{end}} - { - - } - - public {{name}}(Bright.Common.NotNullInitialization _) {{if parent_def_type}} : base(_) {{end}} - { - {{~for field in fields ~}} - {{~if field.ctype.need_init~}} - {{field.proto_cs_init_field}} - {{~end~}} - {{~end~}} - } - - {{~if is_abstract_type~}} - public static void Serialize{{name}}(ByteBuf _buf, {{name}} x) - { - if (x == null) { _buf.WriteInt(0); return; } - _buf.WriteInt(x.GetTypeId()); - x.Serialize(_buf); - } - - public static {{name}} Deserialize{{name}}(ByteBuf _buf) - { - {{name}} x; - switch (_buf.ReadInt()) - { - case 0 : return null; - {{~for child in hierarchy_not_abstract_children~}} - case {{child.full_name}}.__ID__: x = new {{child.full_name}}(false); break; - {{~end~}} - default: throw new SerializationException(); - } - x.Deserialize(_buf); - return x; - } - {{~end~}} - {{~for field in fields ~}} - public {{field.ctype.cs_define_type}} {{field.convention_name}}; - {{~end~}} - - {{~if !parent_def_type && is_abstract_type~}} - public abstract int GetTypeId(); - {{~end~}} - {{~if parent_def_type && !is_abstract_type~}} - public const int __ID__ = {{id}}; - public override int GetTypeId() - { - return __ID__; - } - {{~end~}} - - public {{cs_method_modifer}} void Serialize(ByteBuf _buf) - { - {{~if parent_def_type~}} - base.Serialize(_buf); - {{~end~}} - {{~for field in fields ~}} - {{field.cs_serialize}} - {{~end~}} - } - - public {{cs_method_modifer}} void Deserialize(ByteBuf _buf) - { - {{~if parent_def_type~}} - base.Deserialize(_buf); - {{~end~}} - {{~for field in fields ~}} - {{field.cs_deserialize}} - {{~end~}} - } - - public override string ToString() - { - return "{{full_name}}{ " - {{~for field in hierarchy_fields ~}} - + "{{field.convention_name}}:" + {{field.proto_cs_to_string}} + "," - {{~end~}} - + "}"; - } - } - -} diff --git a/src/Luban.Server/Templates/config/cs_editor_json/table.tpl b/src/Luban.Server/Templates/config/cs_editor_json/table.tpl deleted file mode 100644 index 4b2b006..0000000 --- a/src/Luban.Server/Templates/config/cs_editor_json/table.tpl +++ /dev/null @@ -1,63 +0,0 @@ -using Bright.Serialization; - -namespace {{namespace}} -{ - -public sealed class {{name}} : Bright.Net.Protocol -{ - {{~for field in fields ~}} - public {{field.ctype.cs_define_type}} {{field.convention_name}}; - {{~end~}} - public {{name}}() - { - } - public {{name}}(Bright.Common.NotNullInitialization _) - { - {{~for field in fields ~}} - {{~if field.ctype.need_init~}} - {{field.proto_cs_init_field}} - {{~end~}} - {{~end~}} - } - public const int __ID__ = {{id}}; - - public override int GetTypeId() - { - return __ID__; - } - - public override void Serialize(ByteBuf _buf) - { - {{~for field in fields ~}} - {{field.cs_serialize}} - {{~end~}} - } - - public override void Deserialize(ByteBuf _buf) - { - {{~for field in fields ~}} - {{field.cs_deserialize}} - {{~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}}:" + {{field.proto_cs_to_string}} + "," - {{~end~}} - + "}"; - } -} - -} diff --git a/src/Luban.Server/Templates/config/cs_editor_json/tables.tpl b/src/Luban.Server/Templates/config/cs_editor_json/tables.tpl deleted file mode 100644 index b9ff4c2..0000000 --- a/src/Luban.Server/Templates/config/cs_editor_json/tables.tpl +++ /dev/null @@ -1,16 +0,0 @@ -using Bright.Serialization; - -namespace {{namespace}} -{ - -public static class {{name}} -{ - public static System.Collections.Generic.Dictionary Factories { get; } = new System.Collections.Generic.Dictionary - { - {{~for proto in protos ~}} - [{{proto.full_name}}.__ID__] = () => new {{proto.full_name}}(false), - {{~end~}} - }; -} - -} diff --git a/src/Luban.Server/Templates/config/cs_json/bean.tpl b/src/Luban.Server/Templates/config/cs_json/bean.tpl index 527755b..eebba58 100644 --- a/src/Luban.Server/Templates/config/cs_json/bean.tpl +++ b/src/Luban.Server/Templates/config/cs_json/bean.tpl @@ -10,7 +10,7 @@ using System.Text.Json; hierarchy_export_fields = x.hierarchy_export_fields }} -namespace {{x.namespace_with_top_module}} +namespace {{x.namespace_with_editor_top_module}} { {{~if x.comment != '' ~}} @@ -18,7 +18,7 @@ namespace {{x.namespace_with_top_module}} /// {{x.escape_comment}} /// {{~end~}} -public {{x.cs_class_modifier}} partial class {{name}} : {{if parent_def_type}} {{parent}} {{else}} Bright.Config.BeanBase {{end}} +public {{x.cs_class_modifier}} partial class {{name}} : {{if parent_def_type}} {{parent}} {{else}} Bright.Config.EditorBeanBase {{end}} { public {{name}}(JsonElement _json) {{if parent_def_type}} : base(_json) {{end}} { diff --git a/src/Luban.Server/Templates/config/cs_unity_editor_json/bean.tpl b/src/Luban.Server/Templates/config/cs_unity_editor_json/bean.tpl new file mode 100644 index 0000000..ae7be75 --- /dev/null +++ b/src/Luban.Server/Templates/config/cs_unity_editor_json/bean.tpl @@ -0,0 +1,107 @@ +using Bright.Serialization; +using System.Collections.Generic; +using SimpleJSON; + +{{ + name = x.name + parent_def_type = x.parent_def_type + parent = x.parent + fields = x.fields +}} + +namespace {{x.namespace_with_editor_top_module}} +{ + +{{~if x.comment != '' ~}} +/// +/// {{x.escape_comment}} +/// +{{~end~}} +public {{x.cs_class_modifier}} partial class {{name}} : {{if parent_def_type}} {{parent}} {{else}} Bright.Config.EditorBeanBase {{end}} +{ + public {{name}}() + { + {{~ for field in fields ~}} + {{~if (cs_editor_need_init field.ctype) && !field.ctype.is_nullable ~}} + {{field.convention_name}} = {{cs_editor_init_value field.ctype}}; + {{~end~}} + {{~end~}} + } + + public override void LoadJson(SimpleJSON.JSONObject _json) + { + {{~if parent_def_type ~}} + base.LoadJson(_json); + {{~end~}} + {{~ for field in fields ~}} + { + var _fieldJson = _json["{{field.name}}"]; + if (_fieldJson != null) + { + {{cs_unity_editor_json_load '_fieldJson' field.convention_name field.ctype}} + } + } + + {{~end~}} + } + + public override void SaveJson(SimpleJSON.JSONObject _json) + { + {{~if parent_def_type ~}} + base.SaveJson(_json); + {{~end~}} + {{~ for field in fields ~}} + {{~if field.ctype.is_nullable}} + if ({{field.convention_name}} != null) + { + {{cs_unity_editor_json_save '_json' field.name field.convention_name field.ctype}} + } + {{~else~}} + { + {{~if (cs_is_editor_raw_nullable field.ctype)}} + if ({{field.convention_name}} == null) { throw new System.ArgumentNullException(); } + {{~end~}} + {{cs_unity_editor_json_save '_json' field.name field.convention_name field.ctype}} + } + {{~end~}} + {{~end~}} + } + + public static {{name}} LoadJson{{name}}(SimpleJSON.JSONNode _json) + { + {{~if x.is_abstract_type~}} + string type = _json["__type__"]; + {{name}} obj; + switch (type) + { + {{~for child in x.hierarchy_not_abstract_children~}} + case "{{child.name}}": obj = new {{child.full_name}}(); break; + {{~end~}} + default: throw new SerializationException(); + } + {{~else~}} + {{name}} obj = new {{x.full_name}}(); + {{~end~}} + obj.LoadJson((SimpleJSON.JSONObject)_json); + return obj; + } + + public static void SaveJson{{name}}({{name}} _obj, SimpleJSON.JSONNode _json) + { + {{~if x.is_abstract_type~}} + _json["__type__"] = _obj.GetType().Name; + {{~end~}} + _obj.SaveJson((SimpleJSON.JSONObject)_json); + } + + {{~ for field in fields ~}} +{{~if field.comment != '' ~}} + /// + /// {{field.escape_comment}} + /// +{{~end~}} + public {{cs_editor_define_type field.ctype}} {{field.convention_name}} { get; set; } + + {{~end~}} +} +} diff --git a/src/Luban.Server/Templates/config/cs_unity_editor_json/enum.tpl b/src/Luban.Server/Templates/config/cs_unity_editor_json/enum.tpl new file mode 100644 index 0000000..625dc7b --- /dev/null +++ b/src/Luban.Server/Templates/config/cs_unity_editor_json/enum.tpl @@ -0,0 +1,45 @@ +{{~ + name = x.name + comment = x.comment + items = x.items + itemType = 'Bright.Config.EditorEnumItemInfo' +~}} + +namespace {{x.namespace_with_editor_top_module}} +{ +{{~if comment != '' ~}} + /// + /// {{comment | html.escape}} + /// +{{~end~}} + public static partial class {{name}} + { + {{~ for item in items ~}} + public static readonly {{itemType}} {{item.name}} = new {{itemType}}("{{item.name}}", "{{item.alias}}", {{item.int_value}}, "{{item.comment}}"); + {{~end~}} + + private static readonly System.Collections.Generic.List<{{itemType}}> __items = new System.Collections.Generic.List<{{itemType}}> + { + {{~ for item in items ~}} + {{item.name}}, + {{~end~}} + }; + + public static System.Collections.Generic.List<{{itemType}}> GetItems() => __items; + + public static {{itemType}} GetByName(string name) + { + return __items.Find(c => c.Name == name); + } + + public static {{itemType}} GetByNameOrAlias(string name) + { + return __items.Find(c => c.Name == name || c.Alias == name); + } + + public static {{itemType}} GetByValue(int value) + { + return __items.Find(c => c.Value == value); + } + } +}