From 2955cbac5282de565ed9c8ad41f5e5aea821c06b Mon Sep 17 00:00:00 2001 From: walon Date: Thu, 26 Aug 2021 19:58:16 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E7=89=B9=E6=80=A7=E3=80=91=E4=B8=BAen?= =?UTF-8?q?um,enum.item,bean,bean.field,bean.field.type,table=20=E7=AD=89?= =?UTF-8?q?=E5=A4=A7=E5=A4=9A=E6=95=B0=E5=AE=9A=E4=B9=89=E7=9A=84=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1=E5=8A=A0=E4=B8=8Aattrs=E5=B1=9E=E6=80=A7=E3=80=82?= =?UTF-8?q?=E5=90=8C=E6=97=B6=E6=B7=BB=E5=8A=A0=20has=5Fattr=E5=92=8Cget?= =?UTF-8?q?=5Fattr=E6=A8=A1=E6=9D=BF=E5=87=BD=E6=95=B0=EF=BC=8C=E9=80=9A?= =?UTF-8?q?=E8=BF=87=E5=AF=B9=E5=AF=B9=E8=B1=A1=E6=A0=87=E7=AD=BE=E8=AF=86?= =?UTF-8?q?=E5=88=AB=E5=81=9A=E4=B8=80=E4=BA=9B=E6=A0=87=E5=87=86=E4=BB=A5?= =?UTF-8?q?=E5=A4=96=E7=9A=84=E8=87=AA=E5=AE=9A=E4=B9=89=E7=94=9F=E6=88=90?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Luban.Job.Cfg/Source/Defs/CfgDefLoader.cs | 32 ++++++++---- src/Luban.Job.Cfg/Source/Defs/DefTable.cs | 2 + src/Luban.Job.Cfg/Source/RawDefs/Table.cs | 2 + .../Source/Defs/CommonDefLoader.cs | 14 ++++-- .../Source/Defs/DefAssemblyBase.cs | 41 ++++++++------- .../Source/Defs/DefBeanBase.cs | 2 + src/Luban.Job.Common/Source/Defs/DefEnum.cs | 23 +++++++-- .../Source/Defs/DefFieldBase.cs | 14 ++++++ .../Source/Defs/DefTypeBase.cs | 13 +++++ .../Source/Defs/TTypeTemplateCommonExtends.cs | 10 ++++ src/Luban.Job.Common/Source/RawDefs/Bean.cs | 2 + src/Luban.Job.Common/Source/RawDefs/Field.cs | 2 + src/Luban.Job.Common/Source/RawDefs/PEnum.cs | 4 ++ src/Luban.Job.Common/Source/Types/TType.cs | 13 +++++ src/Luban.Job.Common/Source/Utils/DefUtil.cs | 50 +++++++++++++++++++ 15 files changed, 187 insertions(+), 37 deletions(-) create mode 100644 src/Luban.Job.Common/Source/Utils/DefUtil.cs diff --git a/src/Luban.Job.Cfg/Source/Defs/CfgDefLoader.cs b/src/Luban.Job.Cfg/Source/Defs/CfgDefLoader.cs index 56befb6..863a26a 100644 --- a/src/Luban.Job.Cfg/Source/Defs/CfgDefLoader.cs +++ b/src/Luban.Job.Cfg/Source/Defs/CfgDefLoader.cs @@ -260,11 +260,12 @@ namespace Luban.Job.Cfg.Defs string input = XmlUtil.GetRequiredAttribute(e, "input"); string branchInput = XmlUtil.GetOptionalAttribute(e, "branch_input"); string mode = XmlUtil.GetOptionalAttribute(e, "mode"); - AddTable(CurImportFile, name, module, valueType, index, mode, group, comment, defineFromFile, input, branchInput); + string attrs = XmlUtil.GetOptionalAttribute(e, "attrs"); + AddTable(CurImportFile, name, module, valueType, index, mode, group, comment, defineFromFile, input, branchInput, attrs); } private void AddTable(string defineFile, string name, string module, string valueType, string index, string mode, string group, - string comment, bool defineFromExcel, string input, string branchInput) + string comment, bool defineFromExcel, string input, string branchInput, string attrs) { var p = new Table() { @@ -276,6 +277,7 @@ namespace Luban.Job.Cfg.Defs Groups = CreateGroups(group), Comment = comment, Mode = ConvertMode(name, mode, index), + Attrs = attrs, }; if (p.Groups.Count == 0) @@ -454,6 +456,7 @@ namespace Luban.Job.Cfg.Defs new CfgField() { Name = "define_from_excel", Type = "bool" }, new CfgField() { Name = "input", Type = "string" }, new CfgField() { Name = "branch_input", Type = "string" }, + new CfgField() { Name = "attrs", Type = "string" }, } }) { @@ -488,7 +491,8 @@ namespace Luban.Job.Cfg.Defs bool isDefineFromExcel = (data.GetField("define_from_excel") as DBool).Value; string inputFile = (data.GetField("input") as DString).Value.Trim(); string branchInput = (data.GetField("branch_input") as DString).Value.Trim(); - AddTable(file.OriginFile, name, module, valueType, index, mode, group, comment, isDefineFromExcel, inputFile, branchInput); + string attrs = (data.GetField("attrs") as DString).Value.Trim(); + AddTable(file.OriginFile, name, module, valueType, index, mode, group, comment, isDefineFromExcel, inputFile, branchInput, attrs); }; } } @@ -518,6 +522,7 @@ namespace Luban.Job.Cfg.Defs new CfgField() { Name = "alias", Type = "string" }, new CfgField() { Name = "value", Type = "int" }, new CfgField() { Name = "comment", Type = "string" }, + new CfgField() { Name = "attrs", Type = "string" }, } }) { @@ -561,7 +566,8 @@ namespace Luban.Job.Cfg.Defs string alias = (data.GetField("alias") as DString).Value.Trim(); string value = (data.GetField("value") as DInt).Value.ToString(); string comment = (data.GetField("comment") as DString).Value.Trim(); - curEnum.Items.Add(new EnumItem() { Name = item, Alias = alias, Value = value, Comment = comment }); + string attrs = (data.GetField("attrs") as DString).Value.Trim(); + curEnum.Items.Add(new EnumItem() { Name = item, Alias = alias, Value = value, Comment = comment, Attrs = attrs }); }; } } @@ -598,6 +604,7 @@ namespace Luban.Job.Cfg.Defs new CfgField() { Name = "reference", Type = "string" }, new CfgField() { Name = "path", Type = "string" }, new CfgField() { Name = "comment", Type = "string" }, + new CfgField() { Name = "attrs", Type = "string" }, } }) { @@ -625,6 +632,7 @@ namespace Luban.Job.Cfg.Defs new CfgField() { Name = "full_name", Type = "string" }, new CfgField() { Name = "sep", Type = "string" }, new CfgField() { Name = "comment", Type = "string" }, + new CfgField() { Name = "attrs", Type = "string" }, new CfgField() { Name = "fields", Type = "list,__FieldInfo__", IsMultiRow = true }, } }) @@ -659,6 +667,7 @@ namespace Luban.Job.Cfg.Defs string sep = (data.GetField("sep") as DString).Value.Trim(); string comment = (data.GetField("comment") as DString).Value.Trim(); + string attrs = (data.GetField("attrs") as DString).Value.Trim(); DList fields = data.GetField("fields") as DList; var curBean = new CfgBean() { @@ -666,6 +675,7 @@ namespace Luban.Job.Cfg.Defs Namespace = module, Sep = sep, Comment = comment, + Attrs = attrs, Parent = "", Fields = fields.Datas.Select(d => (DBean)d).Select(b => this.CreateField( (b.GetField("name") as DString).Value.Trim(), @@ -682,7 +692,8 @@ namespace Luban.Job.Cfg.Defs "", "", "", - "" + "", + (b.GetField("attrs") as DString).Value.Trim() )).ToList(), }; this._beans.Add(curBean); @@ -698,7 +709,7 @@ namespace Luban.Job.Cfg.Defs private static readonly List _fieldOptionalAttrs = new List { "index", "sep", "validator", "key_validator", "value_validator", - "ref", "path", "range", "multi_rows", "group", "res", "convert", "comment" }; + "ref", "path", "range", "multi_rows", "group", "res", "convert", "comment", "attrs" }; private static readonly List _fieldRequireAttrs = new List { "name", "type" }; @@ -720,12 +731,13 @@ namespace Luban.Job.Cfg.Defs XmlUtil.GetOptionalAttribute(e, "range"), XmlUtil.GetOptionalAttribute(e, "key_validator"), XmlUtil.GetOptionalAttribute(e, "value_validator"), - XmlUtil.GetOptionalAttribute(e, "validator") + XmlUtil.GetOptionalAttribute(e, "validator"), + XmlUtil.GetOptionalAttribute(e, "attrs") ); } private Field CreateField(string name, string type, string index, string sep, bool isMultiRow, string group, string resource, string converter, - string comment, string refs, string path, string range, string keyValidator, string valueValidator, string validator) + string comment, string refs, string path, string range, string keyValidator, string valueValidator, string validator, string attrs) { var f = new CfgField() { @@ -737,6 +749,7 @@ namespace Luban.Job.Cfg.Defs Resource = resource, Converter = converter, Comment = comment, + Attrs = attrs, }; // 字段与table的默认组不一样。 @@ -763,7 +776,7 @@ namespace Luban.Job.Cfg.Defs return f; } - private static readonly List _beanOptinsAttrs = new List { "value_type", "alias", "sep", "comment" }; + private static readonly List _beanOptinsAttrs = new List { "value_type", "alias", "sep", "comment", "attrs" }; private static readonly List _beanRequireAttrs = new List { "name" }; protected override void AddBean(XElement e, string parent) @@ -781,6 +794,7 @@ namespace Luban.Job.Cfg.Defs Alias = XmlUtil.GetOptionalAttribute(e, "alias"), Sep = XmlUtil.GetOptionalAttribute(e, "sep"), Comment = XmlUtil.GetOptionalAttribute(e, "comment"), + Attrs = XmlUtil.GetOptionalAttribute(e, "attrs"), }; var childBeans = new List(); diff --git a/src/Luban.Job.Cfg/Source/Defs/DefTable.cs b/src/Luban.Job.Cfg/Source/Defs/DefTable.cs index 84d0fa6..016f1af 100644 --- a/src/Luban.Job.Cfg/Source/Defs/DefTable.cs +++ b/src/Luban.Job.Cfg/Source/Defs/DefTable.cs @@ -1,5 +1,6 @@ using Luban.Job.Cfg.RawDefs; using Luban.Job.Common.Types; +using Luban.Job.Common.Utils; using System; using System.Collections.Generic; using System.Linq; @@ -21,6 +22,7 @@ namespace Luban.Job.Cfg.Defs Groups = b.Groups; _branchInputFiles = b.BranchInputFiles; Comment = b.Comment; + Attrs = DefUtil.ParseAttrs(b.Attrs); } diff --git a/src/Luban.Job.Cfg/Source/RawDefs/Table.cs b/src/Luban.Job.Cfg/Source/RawDefs/Table.cs index 31821e8..f6eb8e0 100644 --- a/src/Luban.Job.Cfg/Source/RawDefs/Table.cs +++ b/src/Luban.Job.Cfg/Source/RawDefs/Table.cs @@ -35,6 +35,8 @@ namespace Luban.Job.Cfg.RawDefs public string Comment { get; set; } + public string Attrs { get; set; } + public List Groups { get; set; } = new List(); public List InputFiles { get; set; } = new List(); diff --git a/src/Luban.Job.Common/Source/Defs/CommonDefLoader.cs b/src/Luban.Job.Common/Source/Defs/CommonDefLoader.cs index 85bd9df..56bd8a8 100644 --- a/src/Luban.Job.Common/Source/Defs/CommonDefLoader.cs +++ b/src/Luban.Job.Common/Source/Defs/CommonDefLoader.cs @@ -195,7 +195,7 @@ namespace Luban.Job.Common.Defs } private static readonly List _fieldRequireAttrs = new List { "name", "type", }; - private static readonly List _fieldOptionalAttrs = new List { "id", "comment" }; + private static readonly List _fieldOptionalAttrs = new List { "id", "comment", "attrs" }; protected virtual Field CreateField(XElement e) { @@ -206,6 +206,7 @@ namespace Luban.Job.Common.Defs Name = XmlUtil.GetRequiredAttribute(e, "name"), Type = CreateType(e, "type"), Comment = XmlUtil.GetOptionalAttribute(e, "comment"), + Attrs = XmlUtil.GetOptionalAttribute(e, "attrs"), }; return f; } @@ -215,10 +216,10 @@ namespace Luban.Job.Common.Defs AddBean(e, ""); } - private static readonly List _beanOptinsAttrs1 = new List { "compatible", "value_type", "comment" }; + private static readonly List _beanOptinsAttrs1 = new List { "compatible", "value_type", "comment", "attrs" }; private static readonly List _beanRequireAttrs1 = new List { "id", "name" }; - private static readonly List _beanOptinsAttrs2 = new List { "id", "compatible", "value_type", "comment" }; + private static readonly List _beanOptinsAttrs2 = new List { "id", "compatible", "value_type", "comment", "attrs" }; private static readonly List _beanRequireAttrs2 = new List { "name" }; protected virtual void AddBean(XElement e, string parent) @@ -240,6 +241,7 @@ namespace Luban.Job.Common.Defs IsSerializeCompatible = XmlUtil.GetOptionBoolAttribute(e, "compatible", IsBeanDefaultCompatible), IsValueType = XmlUtil.GetOptionBoolAttribute(e, "value_type"), Comment = XmlUtil.GetOptionalAttribute(e, "comment"), + Attrs = XmlUtil.GetOptionalAttribute(e, "attrs"), }; var childBeans = new List(); @@ -334,11 +336,11 @@ namespace Luban.Job.Common.Defs _consts.Add(c); } - private static readonly List _enumOptionalAttrs = new List { "flags", "comment" }; + private static readonly List _enumOptionalAttrs = new List { "flags", "comment", "attrs" }; private static readonly List _enumRequiredAttrs = new List { "name" }; - private static readonly List _enumItemOptionalAttrs = new List { "value", "alias", "comment" }; + private static readonly List _enumItemOptionalAttrs = new List { "value", "alias", "comment", "attrs" }; private static readonly List _enumItemRequiredAttrs = new List { "name" }; protected void AddEnum(XElement e) @@ -350,6 +352,7 @@ namespace Luban.Job.Common.Defs Namespace = CurNamespace, Comment = XmlUtil.GetOptionalAttribute(e, "comment"), IsFlags = XmlUtil.GetOptionBoolAttribute(e, "flags"), + Attrs = XmlUtil.GetOptionalAttribute(e, "attrs"), }; foreach (XElement item in e.Elements()) @@ -361,6 +364,7 @@ namespace Luban.Job.Common.Defs Alias = XmlUtil.GetOptionalAttribute(item, "alias"), Value = XmlUtil.GetOptionalAttribute(item, "value"), Comment = XmlUtil.GetOptionalAttribute(item, "comment"), + Attrs = XmlUtil.GetOptionalAttribute(item, "attrs"), }); } s_logger.Trace("add enum:{@enum}", en); diff --git a/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs b/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs index c81e40d..408dbb7 100644 --- a/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs +++ b/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs @@ -1,6 +1,7 @@ using Luban.Common.Utils; using Luban.Job.Common.Types; +using Luban.Job.Common.Utils; using Luban.Server.Common; using System; using System.Collections.Generic; @@ -128,9 +129,11 @@ namespace Luban.Job.Common.Defs } } - protected TType CreateNotContainerType(string module, string type) + protected TType CreateNotContainerType(string module, string rawType) { bool nullable; + var (type, attrs) = DefUtil.ParseType(rawType); + if (type.EndsWith('?')) { if (!SupportNullable) @@ -146,33 +149,33 @@ namespace Luban.Job.Common.Defs } switch (type) { - case "bool": return nullable ? TBool.NullableIns : TBool.Ins; + case "bool": return attrs == null ? (nullable ? TBool.NullableIns : TBool.Ins) : new TBool(nullable) { Attrs = attrs }; case "uint8": - case "byte": return nullable ? TByte.NullableIns : TByte.Ins; + case "byte": return attrs == null ? (nullable ? TByte.NullableIns : TByte.Ins) : new TByte(nullable) { Attrs = attrs }; case "int16": - case "short": return nullable ? TShort.NullableIns : TShort.Ins; + case "short": return attrs == null ? (nullable ? TShort.NullableIns : TShort.Ins) : new TShort(nullable) { Attrs = attrs }; case "fint16": - case "fshort": return nullable ? TFshort.NullableIns : TFshort.Ins; + case "fshort": return attrs == null ? (nullable ? TFshort.NullableIns : TFshort.Ins) : new TFshort(nullable) { Attrs = attrs }; case "int32": - case "int": return nullable ? TInt.NullableIns : TInt.Ins; + case "int": return attrs == null ? (nullable ? TInt.NullableIns : TInt.Ins) : new TInt(nullable) { Attrs = attrs }; case "fint32": - case "fint": return nullable ? TFint.NullableIns : TFint.Ins; + case "fint": return attrs == null ? (nullable ? TFint.NullableIns : TFint.Ins) : new TFint(nullable) { Attrs = attrs }; case "int64": - case "long": return nullable ? TLong.NullableIns : TLong.Ins; - case "bigint": return nullable ? TLong.NullableBigIns : TLong.BigIns; + case "long": return attrs == null ? (nullable ? TLong.NullableIns : TLong.Ins) : new TLong(nullable, false) { Attrs = attrs }; + case "bigint": return attrs == null ? (nullable ? TLong.NullableBigIns : TLong.BigIns) : new TLong(nullable, true) { Attrs = attrs }; case "fint64": - case "flong": return nullable ? TFlong.NullableIns : TFlong.Ins; + case "flong": return attrs == null ? (nullable ? TFlong.NullableIns : TFlong.Ins) : new TFlong(nullable) { Attrs = attrs }; case "float32": - case "float": return nullable ? TFloat.NullableIns : TFloat.Ins; + case "float": return attrs == null ? (nullable ? TFloat.NullableIns : TFloat.Ins) : new TFloat(nullable) { Attrs = attrs }; case "float64": - case "double": return nullable ? TDouble.NullableIns : TDouble.Ins; - case "bytes": return TBytes.Ins; - case "string": return nullable ? TString.NullableIns : TString.Ins; - case "text": return nullable ? TText.NullableIns : TText.Ins; - case "vector2": return nullable ? TVector2.NullableIns : TVector2.Ins; - case "vector3": return nullable ? TVector3.NullableIns : TVector3.Ins; - case "vector4": return nullable ? TVector4.NullableIns : TVector4.Ins; - case "datetime": return SupportDatetimeType ? (nullable ? TDateTime.NullableIns : TDateTime.Ins) : throw new NotSupportedException($"只有配置支持datetime数据类型"); + case "double": return attrs == null ? (nullable ? TDouble.NullableIns : TDouble.Ins) : new TDouble(nullable) { Attrs = attrs }; + case "bytes": return attrs == null ? (nullable ? new TBytes(true) : TBytes.Ins) : new TBytes(false) { Attrs = attrs }; + case "string": return attrs == null ? (nullable ? TString.NullableIns : TString.Ins) : new TString(nullable) { Attrs = attrs }; + case "text": return attrs == null ? (nullable ? TText.NullableIns : TText.Ins) : new TText(nullable) { Attrs = attrs }; + case "vector2": return attrs == null ? (nullable ? TVector2.NullableIns : TVector2.Ins) : new TVector2(nullable) { Attrs = attrs }; + case "vector3": return attrs == null ? (nullable ? TVector3.NullableIns : TVector3.Ins) : new TVector3(nullable) { Attrs = attrs }; + case "vector4": return attrs == null ? (nullable ? TVector4.NullableIns : TVector4.Ins) : new TVector4(nullable) { Attrs = attrs }; + case "datetime": return SupportDatetimeType ? (attrs == null ? (nullable ? TDateTime.NullableIns : TDateTime.Ins) : new TDateTime(nullable) { Attrs = attrs }) : throw new NotSupportedException($"只有配置支持datetime数据类型"); default: { var dtype = GetDefTType(module, type, nullable); diff --git a/src/Luban.Job.Common/Source/Defs/DefBeanBase.cs b/src/Luban.Job.Common/Source/Defs/DefBeanBase.cs index 153bf84..ef723ee 100644 --- a/src/Luban.Job.Common/Source/Defs/DefBeanBase.cs +++ b/src/Luban.Job.Common/Source/Defs/DefBeanBase.cs @@ -1,4 +1,5 @@ using Luban.Job.Common.RawDefs; +using Luban.Job.Common.Utils; using System; using System.Collections.Generic; using System.Linq; @@ -50,6 +51,7 @@ namespace Luban.Job.Common.Defs Id = b.TypeId; IsValueType = b.IsValueType; Comment = b.Comment; + Attrs = DefUtil.ParseAttrs(b.Attrs); foreach (var field in b.Fields) { Fields.Add(CreateField(field, 0)); diff --git a/src/Luban.Job.Common/Source/Defs/DefEnum.cs b/src/Luban.Job.Common/Source/Defs/DefEnum.cs index 4039243..af0672e 100644 --- a/src/Luban.Job.Common/Source/Defs/DefEnum.cs +++ b/src/Luban.Job.Common/Source/Defs/DefEnum.cs @@ -1,4 +1,5 @@ using Luban.Job.Common.RawDefs; +using Luban.Job.Common.Utils; using System; using System.Collections.Generic; using System.Linq; @@ -21,15 +22,27 @@ namespace Luban.Job.Common.Defs public int IntValue { get; set; } public string Comment { get; init; } + + public Dictionary Attrs { get; init; } + + public bool HasAttr(string attrName) + { + return Attrs != null && Attrs.ContainsKey(attrName); + } + + public string GetAttr(string attrName) + { + return Attrs != null && Attrs.TryGetValue(attrName, out var value) ? value : null; + } } - public bool IsFlags { get; set; } + public bool IsFlags { get; } - public bool IsUniqueItemId { get; set; } + public bool IsUniqueItemId { get; } - public List Items { get; set; } = new List(); + public List Items { get; } = new List(); - private readonly Dictionary _nameOrAlias2Value = new Dictionary(); + private readonly Dictionary _nameOrAlias2Value = new(); public bool TryValueByNameOrAlias(string name, out int value) { @@ -71,6 +84,7 @@ namespace Luban.Job.Common.Defs IsFlags = e.IsFlags; IsUniqueItemId = e.IsUniqueItemId; Comment = e.Comment; + Attrs = DefUtil.ParseAttrs(e.Attrs); foreach (var item in e.Items) { @@ -81,6 +95,7 @@ namespace Luban.Job.Common.Defs Alias = item.Alias, Value = item.Value, Comment = string.IsNullOrWhiteSpace(item.Comment) ? item.Alias : item.Comment, + Attrs = DefUtil.ParseAttrs(item.Attrs), }); } } diff --git a/src/Luban.Job.Common/Source/Defs/DefFieldBase.cs b/src/Luban.Job.Common/Source/Defs/DefFieldBase.cs index 17e60a6..6796955 100644 --- a/src/Luban.Job.Common/Source/Defs/DefFieldBase.cs +++ b/src/Luban.Job.Common/Source/Defs/DefFieldBase.cs @@ -1,6 +1,7 @@ using Luban.Common.Utils; using Luban.Job.Common.RawDefs; using Luban.Job.Common.Types; +using Luban.Job.Common.Utils; using System; using System.Collections.Generic; @@ -71,6 +72,18 @@ namespace Luban.Job.Common.Defs public string Comment { get; } + public Dictionary Attrs { get; } + + public bool HasAttr(string attrName) + { + return Attrs != null && Attrs.ContainsKey(attrName); + } + + public string GetAttr(string attrName) + { + return Attrs != null && Attrs.TryGetValue(attrName, out var value) ? value : null; + } + public DefFieldBase(DefTypeBase host, Field f, int idOffset) { HostType = host; @@ -78,6 +91,7 @@ namespace Luban.Job.Common.Defs Name = f.Name; Type = f.Type; Comment = f.Comment; + Attrs = DefUtil.ParseAttrs(f.Attrs); } public virtual void Compile() diff --git a/src/Luban.Job.Common/Source/Defs/DefTypeBase.cs b/src/Luban.Job.Common/Source/Defs/DefTypeBase.cs index b21da8f..a426c77 100644 --- a/src/Luban.Job.Common/Source/Defs/DefTypeBase.cs +++ b/src/Luban.Job.Common/Source/Defs/DefTypeBase.cs @@ -1,5 +1,6 @@ using Luban.Common.Utils; using Luban.Server.Common; +using System.Collections.Generic; namespace Luban.Job.Common.Defs { @@ -45,6 +46,18 @@ namespace Luban.Job.Common.Defs public string Comment { get; protected set; } + public Dictionary Attrs { get; protected set; } + + public bool HasAttr(string attrName) + { + return Attrs != null && Attrs.ContainsKey(attrName); + } + + public string GetAttr(string attrName) + { + return Attrs != null && Attrs.TryGetValue(attrName, out var value) ? value : null; + } + public virtual void PreCompile() { } public abstract void Compile(); diff --git a/src/Luban.Job.Common/Source/Defs/TTypeTemplateCommonExtends.cs b/src/Luban.Job.Common/Source/Defs/TTypeTemplateCommonExtends.cs index 45f604c..69d9dee 100644 --- a/src/Luban.Job.Common/Source/Defs/TTypeTemplateCommonExtends.cs +++ b/src/Luban.Job.Common/Source/Defs/TTypeTemplateCommonExtends.cs @@ -162,5 +162,15 @@ namespace Luban.Job.Common.Defs { return type.Apply(LuaConstValueVisitor.Ins, value); } + + public static bool HasAttr(dynamic obj, string attrName) + { + return obj.HasAttr(attrName); + } + + public static string GetAttr(dynamic obj, string attrName) + { + return obj.GetAttr(attrName); + } } } diff --git a/src/Luban.Job.Common/Source/RawDefs/Bean.cs b/src/Luban.Job.Common/Source/RawDefs/Bean.cs index b5f554d..6d63595 100644 --- a/src/Luban.Job.Common/Source/RawDefs/Bean.cs +++ b/src/Luban.Job.Common/Source/RawDefs/Bean.cs @@ -20,6 +20,8 @@ namespace Luban.Job.Common.RawDefs public string Comment { get; set; } + public string Attrs { get; set; } + public List Fields { get; set; } = new List(); } } diff --git a/src/Luban.Job.Common/Source/RawDefs/Field.cs b/src/Luban.Job.Common/Source/RawDefs/Field.cs index 7f53b1c..2789b5c 100644 --- a/src/Luban.Job.Common/Source/RawDefs/Field.cs +++ b/src/Luban.Job.Common/Source/RawDefs/Field.cs @@ -10,5 +10,7 @@ namespace Luban.Job.Common.RawDefs public string Type { get; set; } public string Comment { get; set; } + + public string Attrs { get; set; } } } diff --git a/src/Luban.Job.Common/Source/RawDefs/PEnum.cs b/src/Luban.Job.Common/Source/RawDefs/PEnum.cs index 13cd420..045c4c1 100644 --- a/src/Luban.Job.Common/Source/RawDefs/PEnum.cs +++ b/src/Luban.Job.Common/Source/RawDefs/PEnum.cs @@ -11,6 +11,8 @@ namespace Luban.Job.Common.RawDefs public string Value { get; set; } public string Comment { get; set; } + + public string Attrs { get; set; } } public class PEnum @@ -26,6 +28,8 @@ namespace Luban.Job.Common.RawDefs public string Comment { get; set; } + public string Attrs { get; set; } + public List Items { get; set; } = new List(); } } diff --git a/src/Luban.Job.Common/Source/Types/TType.cs b/src/Luban.Job.Common/Source/Types/TType.cs index 98874d7..50d194e 100644 --- a/src/Luban.Job.Common/Source/Types/TType.cs +++ b/src/Luban.Job.Common/Source/Types/TType.cs @@ -1,4 +1,5 @@ using Luban.Job.Common.TypeVisitors; +using System.Collections.Generic; namespace Luban.Job.Common.Types { @@ -11,6 +12,18 @@ namespace Luban.Job.Common.Types IsNullable = isNullable; } + public Dictionary Attrs { get; set; } + + public bool HasAttr(string attrName) + { + return Attrs != null && Attrs.ContainsKey(attrName); + } + + public string GetAttr(string attrName) + { + return Attrs != null && Attrs.TryGetValue(attrName, out var value) ? value : null; + } + public abstract bool TryParseFrom(string s); public virtual bool IsCollection => false; diff --git a/src/Luban.Job.Common/Source/Utils/DefUtil.cs b/src/Luban.Job.Common/Source/Utils/DefUtil.cs new file mode 100644 index 0000000..5d2b671 --- /dev/null +++ b/src/Luban.Job.Common/Source/Utils/DefUtil.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Luban.Job.Common.Utils +{ + public class DefUtil + { + private readonly static char[] s_attrSep = new char[] { '|', '#', '&' }; + + private readonly static char[] s_attrKeyValueSep = new char[] { '=', ':' }; + + public static Dictionary ParseAttrs(string attrs) + { + if (string.IsNullOrWhiteSpace(attrs)) + { + return null; + } + var am = new Dictionary(); + foreach (var pair in attrs.Split(s_attrSep)) + { + int sepIndex = pair.IndexOfAny(s_attrKeyValueSep); + if (sepIndex >= 0) + { + am.Add(pair[..sepIndex].Trim(), pair[(sepIndex + 1)..].Trim()); + } + else + { + am.Add(pair.Trim(), pair.Trim()); + } + } + return am; + } + + public static (string, Dictionary) ParseType(string s) + { + int sepIndex = s.IndexOfAny(s_attrSep); + if (sepIndex < 0) + { + return (s, null); + } + else + { + return (s[..sepIndex], ParseAttrs(s[(sepIndex + 1)..])); + } + } + } +}