From c0d91dfa81dff8559d8564d8fc29f29c8aa1510d Mon Sep 17 00:00:00 2001 From: walon Date: Thu, 24 Feb 2022 11:10:10 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E8=B0=83=E6=95=B4=E3=80=91config=20co?= =?UTF-8?q?de=5Fcs=5Funity=5Feditor=5Fjson=E7=94=9F=E6=88=90=E7=9A=84enum?= =?UTF-8?q?=E7=B1=BB=E6=94=B9=E6=88=90=E6=99=AE=E9=80=9A=E6=9E=9A=E4=B8=BE?= =?UTF-8?q?=E7=B1=BB=E3=80=82=E9=A2=9D=E5=A4=96=E7=94=9F=E6=88=90=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E6=9E=9A=E4=B8=BE=E5=85=83=E6=95=B0=E6=8D=AE=E7=B1=BB?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../TypeVisitors/CsEditorDefineTypeName.cs | 7 +------ .../TypeVisitors/CsEditorInitValueVisitor.cs | 2 +- .../Source/TypeVisitors/CsEditorJsonLoad.cs | 2 +- .../Source/TypeVisitors/CsEditorJsonSave.cs | 8 ++++---- .../CsEditorUnderlyingDefineTypeName.cs | 5 ----- .../config/cs_unity_editor_json/enum.tpl | 18 +++++++++++++++++- 6 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorDefineTypeName.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorDefineTypeName.cs index 0904a01..2036128 100644 --- a/src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorDefineTypeName.cs +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorDefineTypeName.cs @@ -19,14 +19,9 @@ namespace Luban.Job.Cfg.TypeVisitors return "string"; } - public override string Accept(TEnum type) - { - return "string"; - } - public override string Accept(TText type) { - return "Bright.Config.EditorText"; + return CfgConstStrings.EditorTextTypeName; } } } diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorInitValueVisitor.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorInitValueVisitor.cs index e010e85..ed1c2c8 100644 --- a/src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorInitValueVisitor.cs +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorInitValueVisitor.cs @@ -15,7 +15,7 @@ namespace Luban.Job.Cfg.TypeVisitors public override string Accept(TEnum type) { - return $"\"{(type.DefineEnum.Items.Count > 0 ? type.DefineEnum.Items[0].Name : "")}\""; + return $"{(type.DefineEnum.Items.Count > 0 ? $"{type.Apply(CsEditorDefineTypeName.Ins)}."+type.DefineEnum.Items[0].Name : "default")}"; } public override string Accept(TText type) diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorJsonLoad.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorJsonLoad.cs index 84971bd..86cdf81 100644 --- a/src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorJsonLoad.cs +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorJsonLoad.cs @@ -61,7 +61,7 @@ namespace Luban.Job.Cfg.TypeVisitors public string Accept(TEnum type, string json, string x) { - return $"if(!{json}.IsString) {{ throw new SerializationException(); }} {x} = {json};"; + return $"if({json}.IsString) {{ {x} = ({type.Apply(CsEditorDefineTypeName.Ins)})System.Enum.Parse(typeof({type.Apply(CsEditorDefineTypeName.Ins)}), {json}); }} else if({json}.IsNumber) {{ {x} = ({type.Apply(CsEditorDefineTypeName.Ins)})(int){json}; }} else {{ throw new SerializationException(); }} "; } public string Accept(TString type, string json, string x) diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorJsonSave.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorJsonSave.cs index 62804de..a33ff1e 100644 --- a/src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorJsonSave.cs +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorJsonSave.cs @@ -61,7 +61,7 @@ namespace Luban.Job.Cfg.TypeVisitors public string Accept(TEnum type, string jsonName, string jsonFieldName, string value) { - return $"{jsonName}[\"{jsonFieldName}\"] = new JSONString({value});"; + return $"{jsonName}[\"{jsonFieldName}\"] = new JSONNumber((int){value});"; } public string Accept(TString type, string jsonName, string jsonFieldName, string value) @@ -81,17 +81,17 @@ namespace Luban.Job.Cfg.TypeVisitors 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; }}"; + 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; }}"; + 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; }}"; + 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) diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorUnderlyingDefineTypeName.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorUnderlyingDefineTypeName.cs index 5babbec..a7ac694 100644 --- a/src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorUnderlyingDefineTypeName.cs +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/CsEditorUnderlyingDefineTypeName.cs @@ -12,11 +12,6 @@ namespace Luban.Job.Cfg.TypeVisitors { 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; 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 index 625dc7b..8d4a8ed 100644 --- a/src/Luban.Server/Templates/config/cs_unity_editor_json/enum.tpl +++ b/src/Luban.Server/Templates/config/cs_unity_editor_json/enum.tpl @@ -7,12 +7,28 @@ namespace {{x.namespace_with_editor_top_module}} { + {{~if comment != '' ~}} /// /// {{comment | html.escape}} /// {{~end~}} - public static partial class {{name}} + {{~if x.is_flags~}} + [System.Flags] + {{~end~}} + public enum {{name}} + { + {{~ for item in items ~}} +{{~if item.comment != '' ~}} + /// + /// {{item.escape_comment}} + /// +{{~end~}} + {{item.name}} = {{item.value}}, + {{~end~}} + } + + public static partial class {{name}}_Metadata { {{~ for item in items ~}} public static readonly {{itemType}} {{item.name}} = new {{itemType}}("{{item.name}}", "{{item.alias}}", {{item.int_value}}, "{{item.comment}}");