【特性】 cfg root.xml 新增配置项 option

【特性】新增 code_cs_unity_editor_json 支持
main
walon 2022-01-26 22:19:40 +08:00
parent 1acaac5b87
commit f617d5f10f
30 changed files with 763 additions and 227 deletions

View File

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

View File

@ -8,7 +8,7 @@ namespace Luban.Job.Cfg
[Option('s', "service", Required = true, HelpText = "service")] [Option('s', "service", Required = true, HelpText = "service")]
public string Service { get; set; } 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; } public string GenType { get; set; }
[Option("input_data_dir", Required = true, HelpText = "input data dir")] [Option("input_data_dir", Required = true, HelpText = "input data dir")]

View File

@ -12,12 +12,12 @@ using System.Threading.Tasks;
namespace Luban.Job.Cfg.Generate namespace Luban.Job.Cfg.Generate
{ {
[Render("code_cpp_ue_editor")] [Render("code_cpp_ue_editor_json")]
class CppUE4EditorRender : CodeRenderBase class CppUE4EditorJsonRender : CodeRenderBase
{ {
public override void Render(GenContext ctx) 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(); var renderTypes = ctx.Assembly.Types.Values.Where(c => c is DefEnum || c is DefBean).ToList();

View File

@ -1,22 +1,37 @@
using Luban.Common.Protos; using Luban.Common.Protos;
using Luban.Job.Cfg.Defs; using Luban.Job.Cfg.Defs;
using Luban.Job.Common; using Luban.Job.Common;
using Luban.Job.Common.Defs;
using Luban.Job.Common.Generate; using Luban.Job.Common.Generate;
using Luban.Job.Common.Tpl;
using Luban.Job.Common.Utils; using Luban.Job.Common.Utils;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Luban.Job.Cfg.Generate namespace Luban.Job.Cfg.Generate
{ {
[Render("code_cs_unity_editor")] [Render("code_cs_unity_editor_json")]
class CsEditorRender : TemplateCodeRenderBase 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) public override void Render(GenContext ctx)
{ {
ctx.Assembly.CurrentLanguage = ELanguage.CS;
foreach (var c in ctx.Assembly.Types.Values) foreach (var c in ctx.Assembly.Types.Values)
{ {
if (!(c is DefBean) && !(c is DefEnum))
{
continue;
}
ctx.Tasks.Add(Task.Run(() => ctx.Tasks.Add(Task.Run(() =>
{ {
var content = FileHeaderUtil.ConcatAutoGenerationHeader(RenderAny(c), ELanguage.CS); var content = FileHeaderUtil.ConcatAutoGenerationHeader(RenderAny(c), ELanguage.CS);

View File

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

View File

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

View File

@ -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<string, string, string>
{
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};";
}
}
}

View File

@ -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<string, string, string, string>
{
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; }}";
}
}
}

View File

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

View File

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

View File

@ -228,13 +228,34 @@ namespace Luban.Job.Cfg.Utils
return string.Join(", ", table.IndexList.Select(idx => $"{idx.IndexField.Name}")); return string.Join(", ", table.IndexList.Select(idx => $"{idx.IndexField.Name}"));
} }
//public static string DeserializeTextKeyField(DefField field, string lan, string bufName) public static string CsEditorDefineType(TType type)
//{ {
// switch (lan) return type.Apply(CsEditorDefineTypeName.Ins);
// { }
// case "cpp": return $"{CppDefineTypeName.Ins.Accept(TString.Ins)} {field.CppStyleName}{L10N_FIELD_SUFFIX};";
// default: throw new NotSupportedException($"not support lan:{lan}"); 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);
}
} }
} }

View File

@ -4,30 +4,11 @@ using Luban.Job.Common.Utils;
using Luban.Server.Common; using Luban.Server.Common;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Xml.Linq; using System.Xml.Linq;
namespace Luban.Job.Common.Defs 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 public abstract class CommonDefLoader
{ {
@ -53,11 +34,14 @@ namespace Luban.Job.Common.Defs
protected readonly HashSet<string> _externalSelectors = new(); protected readonly HashSet<string> _externalSelectors = new();
protected readonly Dictionary<string, ExternalType> _externalTypes = new(); protected readonly Dictionary<string, ExternalType> _externalTypes = new();
protected readonly Dictionary<string, string> _options = new();
protected CommonDefLoader(IAgent agent) protected CommonDefLoader(IAgent agent)
{ {
Agent = agent; Agent = agent;
_rootDefineHandlers.Add("topmodule", SetTopModule); _rootDefineHandlers.Add("topmodule", SetTopModule);
_rootDefineHandlers.Add("option", AddOption);
_rootDefineHandlers.Add("externalselector", AddExternalSelector); _rootDefineHandlers.Add("externalselector", AddExternalSelector);
_moduleDefineHandlers.Add("module", AddModule); _moduleDefineHandlers.Add("module", AddModule);
@ -115,6 +99,7 @@ namespace Luban.Job.Common.Defs
defines.Beans = _beans; defines.Beans = _beans;
defines.ExternalSelectors = _externalSelectors; defines.ExternalSelectors = _externalSelectors;
defines.ExternalTypes = _externalTypes; defines.ExternalTypes = _externalTypes;
defines.Options = _options;
} }
#region root handler #region root handler
@ -124,6 +109,18 @@ namespace Luban.Job.Common.Defs
this.TopModule = XmlUtil.GetOptionalAttribute(e, "name"); this.TopModule = XmlUtil.GetOptionalAttribute(e, "name");
} }
private static readonly List<string> _optionRequireAttrs = new List<string> { "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) private async Task AddImportAsync(string xmlFile)
{ {
var rootFileName = FileUtil.GetFileName(_rootXml); var rootFileName = FileUtil.GetFileName(_rootXml);

View File

@ -63,6 +63,20 @@ namespace Luban.Job.Common.Defs
public List<string> CurrentExternalSelectors { get; private set; } public List<string> CurrentExternalSelectors { get; private set; }
public Dictionary<string, string> 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) private void SetCurrentExternalSelectors(string selectors)
{ {
if (string.IsNullOrEmpty(selectors)) if (string.IsNullOrEmpty(selectors))
@ -91,6 +105,8 @@ namespace Luban.Job.Common.Defs
this.TopModule = defines.TopModule; this.TopModule = defines.TopModule;
this.ExternalSelectors = defines.ExternalSelectors; this.ExternalSelectors = defines.ExternalSelectors;
this.ExternalTypes = defines.ExternalTypes; 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; this.Args = args;

View File

@ -31,6 +31,10 @@ namespace Luban.Job.Common.Defs
public string FullNameWithTopModule => TypeUtil.MakeFullName(AssemblyBase.TopModule, FullName); 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 CsFullName => TypeUtil.MakeFullName(Namespace, Name);
public string JavaFullName => TypeUtil.MakeFullName(Namespace, Name); public string JavaFullName => TypeUtil.MakeFullName(Namespace, Name);

View File

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

View File

@ -288,5 +288,15 @@ namespace Luban.Job.Common.Defs
{ {
return obj.GetTag(attrName); 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);
}
} }
} }

View File

@ -10,6 +10,8 @@ namespace Luban.Job.Common.RawDefs
{ {
public string TopModule { get; set; } = ""; public string TopModule { get; set; } = "";
public Dictionary<string, string> Options { get; set; } = new();
public HashSet<string> ExternalSelectors { get; set; } = new(); public HashSet<string> ExternalSelectors { get; set; } = new();
public Dictionary<string, ExternalType> ExternalTypes { get; set; } = new(); public Dictionary<string, ExternalType> ExternalTypes { get; set; } = new();

View File

@ -11,7 +11,6 @@ namespace Luban.Job.Common.TypeVisitors
return "default"; return "default";
} }
public override string Accept(TString type) public override string Accept(TString type)
{ {
return "\"\""; return "\"\"";
@ -39,17 +38,17 @@ namespace Luban.Job.Common.TypeVisitors
public override string Accept(TList type) 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) 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) 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)}>()";
} }
} }
} }

View File

@ -6,24 +6,31 @@ namespace Luban.Job.Common.TypeVisitors
{ {
public static CsDefineTypeName Ins { get; } = new CsDefineTypeName(); public static CsDefineTypeName Ins { get; } = new CsDefineTypeName();
protected virtual ITypeFuncVisitor<string> UnderlyingVisitor => CsUnderingDefineTypeName.Ins;
public override string DoAccept(TType type) 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)}>";
} }
} }
} }

View File

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

View File

@ -58,7 +58,7 @@ namespace Luban.Job.Common.TypeVisitors
return "double"; return "double";
} }
public string Accept(TEnum type) public virtual string Accept(TEnum type)
{ {
return ExternalTypeUtil.CsMapperToExternalType(type.DefineEnum); return ExternalTypeUtil.CsMapperToExternalType(type.DefineEnum);
} }
@ -73,7 +73,7 @@ namespace Luban.Job.Common.TypeVisitors
return "byte[]"; return "byte[]";
} }
public string Accept(TText type) public virtual string Accept(TText type)
{ {
return "string"; return "string";
} }
@ -90,17 +90,17 @@ namespace Luban.Job.Common.TypeVisitors
public string Accept(TList type) 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) 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) 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) public string Accept(TVector2 type)
@ -130,7 +130,7 @@ namespace Luban.Job.Common.TypeVisitors
return "System.Numerics.Vector4"; return "System.Numerics.Vector4";
} }
public string Accept(TDateTime type) public virtual string Accept(TDateTime type)
{ {
return "int"; return "int";
} }

View File

@ -15,6 +15,8 @@ namespace Luban.Job.Common.Types
public override string TypeName => "enum"; public override string TypeName => "enum";
public bool IsEnum => true;
public DefEnum DefineEnum { get; } public DefEnum DefineEnum { get; }
private TEnum(bool isNullable, Dictionary<string, string> tags, DefEnum defEnum) : base(isNullable, tags) private TEnum(bool isNullable, Dictionary<string, string> tags, DefEnum defEnum) : base(isNullable, tags)

View File

@ -100,13 +100,10 @@
<None Update="Templates\config\cs_bin\tables.tpl"> <None Update="Templates\config\cs_bin\tables.tpl">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>
<None Update="Templates\config\cs_editor_json\bean.tpl"> <None Update="Templates\config\cs_unity_editor_json\bean.tpl">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>
<None Update="Templates\config\cs_editor_json\table.tpl"> <None Update="Templates\config\cs_unity_editor_json\enum.tpl">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Templates\config\cs_editor_json\tables.tpl">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>
<None Update="Templates\config\cs_json\bean.tpl"> <None Update="Templates\config\cs_json\bean.tpl">

View File

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

View File

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

View File

@ -1,16 +0,0 @@
using Bright.Serialization;
namespace {{namespace}}
{
public static class {{name}}
{
public static System.Collections.Generic.Dictionary<int, Bright.Net.IProtocolFactory> Factories { get; } = new System.Collections.Generic.Dictionary<int, Bright.Net.IProtocolFactory>
{
{{~for proto in protos ~}}
[{{proto.full_name}}.__ID__] = () => new {{proto.full_name}}(false),
{{~end~}}
};
}
}

View File

@ -10,7 +10,7 @@ using System.Text.Json;
hierarchy_export_fields = x.hierarchy_export_fields hierarchy_export_fields = x.hierarchy_export_fields
}} }}
namespace {{x.namespace_with_top_module}} namespace {{x.namespace_with_editor_top_module}}
{ {
{{~if x.comment != '' ~}} {{~if x.comment != '' ~}}
@ -18,7 +18,7 @@ namespace {{x.namespace_with_top_module}}
/// {{x.escape_comment}} /// {{x.escape_comment}}
/// </summary> /// </summary>
{{~end~}} {{~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}} public {{name}}(JsonElement _json) {{if parent_def_type}} : base(_json) {{end}}
{ {

View File

@ -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 != '' ~}}
/// <summary>
/// {{x.escape_comment}}
/// </summary>
{{~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 != '' ~}}
/// <summary>
/// {{field.escape_comment}}
/// </summary>
{{~end~}}
public {{cs_editor_define_type field.ctype}} {{field.convention_name}} { get; set; }
{{~end~}}
}
}

View File

@ -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 != '' ~}}
/// <summary>
/// {{comment | html.escape}}
/// </summary>
{{~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);
}
}
}