From edc09b67420708fa542edb489b39ccb441731e08 Mon Sep 17 00:00:00 2001 From: walon Date: Thu, 2 Dec 2021 14:59:25 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E7=89=B9=E6=80=A7=E3=80=91=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E5=AF=B9=E5=A4=96=E9=83=A8class=E7=B1=BB=E7=9A=84?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E3=80=82=20=E5=8F=AF=E4=BB=A5=E5=9C=A8?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E4=B8=AD=E5=BC=95=E7=94=A8=E7=8E=B0=E6=88=90?= =?UTF-8?q?=E7=9A=84=E5=A4=96=E9=83=A8=E7=B1=BB=EF=BC=8C=E5=A6=82UnityEngi?= =?UTF-8?q?ne.Color?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/Luban.Job.Cfg/Source/Defs/CfgDefLoader.cs | 3 +- src/Luban.Job.Cfg/Source/Defs/DefBean.cs | 2 ++ src/Luban.Job.Cfg/Source/Defs/DefField.cs | 2 +- .../Source/Defs/CommonDefLoader.cs | 10 ++++-- .../Source/Defs/DefTypeBase.cs | 13 ++++++-- .../Source/RawDefs/ExternalType.cs | 2 ++ .../TypeVisitors/CsUnderingDefineTypeName.cs | 6 ++-- .../CsUnderingDeserializeVisitor.cs | 4 ++- .../Source/Utils/ExternalTypeUtil.cs | 32 +++++++++++++++++++ 10 files changed, 64 insertions(+), 11 deletions(-) create mode 100644 src/Luban.Job.Common/Source/Utils/ExternalTypeUtil.cs diff --git a/README.md b/README.md index 69164eb..f965f5d 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,7 @@ luban相较于常规的excel导表工具有以下核心优势: - 强大的数据校验能力。支持内建数据格式检查;支持ref表引用检查(策划不用担心填错id);支持path资源检查(策划不用担心填错资源路径);支持range检查 - 支持常量别名。策划不必再为诸如 升级丹 这样的道具手写具体道具id了 - 支持多种常见数据表模式。 singleton(单例表)、map(常规key-value表)、**list(支持无索引、多主键联合索引、多主键独立索引)** +- 支持**external type**,即外部类。 你可以在配置中引用现成的enum和class类,比如 UnityEngine.Color,UnityEngine.AudioType。 - 支持res资源标记。可以一键导出配置中引用的所有资源列表(icon,ui,assetbundle等等) - 统一了自定义编辑器的配置数据。与Unity和UE4的自定义编辑器良好配合,为编辑器生成合适的加载与保存json配置的的c#(Unity)或c++(UE4)代码。保存的json配置能够被luban识别和处理。 - 支持emmylua anntations。生成的lua包含符合emmylua 格式anntations信息。配合emmylua有良好的配置代码提示能力 diff --git a/src/Luban.Job.Cfg/Source/Defs/CfgDefLoader.cs b/src/Luban.Job.Cfg/Source/Defs/CfgDefLoader.cs index 49e592b..aaf9990 100644 --- a/src/Luban.Job.Cfg/Source/Defs/CfgDefLoader.cs +++ b/src/Luban.Job.Cfg/Source/Defs/CfgDefLoader.cs @@ -770,7 +770,7 @@ namespace Luban.Job.Cfg.Defs return f; } - private static readonly List _beanOptinsAttrs = new List { "value_type", "alias", "sep", "comment", "tags", "group" }; + private static readonly List _beanOptinsAttrs = new List { "value_type", "alias", "sep", "comment", "tags", "group", "externaltype" }; private static readonly List _beanRequireAttrs = new List { "name" }; override protected void AddBean(string defineFile, XElement e, string parent) @@ -789,6 +789,7 @@ namespace Luban.Job.Cfg.Defs Sep = XmlUtil.GetOptionalAttribute(e, "sep"), Comment = XmlUtil.GetOptionalAttribute(e, "comment"), Tags = XmlUtil.GetOptionalAttribute(e, "tags"), + ExternalType = XmlUtil.GetOptionalAttribute(e, "externaltype"), }; var childBeans = new List(); diff --git a/src/Luban.Job.Cfg/Source/Defs/DefBean.cs b/src/Luban.Job.Cfg/Source/Defs/DefBean.cs index 44d571b..abb92a1 100644 --- a/src/Luban.Job.Cfg/Source/Defs/DefBean.cs +++ b/src/Luban.Job.Cfg/Source/Defs/DefBean.cs @@ -170,6 +170,8 @@ namespace Luban.Job.Cfg.Defs public override void Compile() { + ResolveExternalType(); + var cs = new List(); if (Children != null) { diff --git a/src/Luban.Job.Cfg/Source/Defs/DefField.cs b/src/Luban.Job.Cfg/Source/Defs/DefField.cs index b0c0ec2..30b73e6 100644 --- a/src/Luban.Job.Cfg/Source/Defs/DefField.cs +++ b/src/Luban.Job.Cfg/Source/Defs/DefField.cs @@ -28,7 +28,7 @@ namespace Luban.Job.Cfg.Defs // 如果ref了多个表,不再生成 xxx_ref之类的字段,也不会resolve public bool GenRef => Ref != null && Ref.GenRef; - public bool HasRecursiveRef => (CType.IsBean) + public bool HasRecursiveRef => (CType is TBean tb && tb.Bean.CurrentExternalTypeMapper == null) || (CType is TArray ta && ta.ElementType.IsBean) || (CType is TList tl && tl.ElementType.IsBean) || (CType is TMap tm && tm.ValueType.IsBean); diff --git a/src/Luban.Job.Common/Source/Defs/CommonDefLoader.cs b/src/Luban.Job.Common/Source/Defs/CommonDefLoader.cs index ddb635c..847e1ed 100644 --- a/src/Luban.Job.Common/Source/Defs/CommonDefLoader.cs +++ b/src/Luban.Job.Common/Source/Defs/CommonDefLoader.cs @@ -217,10 +217,10 @@ namespace Luban.Job.Common.Defs AddBean(defineFile, e, ""); } - private static readonly List _beanOptinsAttrs1 = new List { "compatible", "value_type", "comment", "tags" }; + private static readonly List _beanOptinsAttrs1 = new List { "compatible", "value_type", "comment", "tags", "externaltype" }; private static readonly List _beanRequireAttrs1 = new List { "id", "name" }; - private static readonly List _beanOptinsAttrs2 = new List { "id", "compatible", "value_type", "comment", "tags" }; + private static readonly List _beanOptinsAttrs2 = new List { "id", "compatible", "value_type", "comment", "tags", "externaltype" }; private static readonly List _beanRequireAttrs2 = new List { "name" }; protected virtual void AddBean(string defineFile, XElement e, string parent) @@ -243,6 +243,7 @@ namespace Luban.Job.Common.Defs IsValueType = XmlUtil.GetOptionBoolAttribute(e, "value_type"), Comment = XmlUtil.GetOptionalAttribute(e, "comment"), Tags = XmlUtil.GetOptionalAttribute(e, "tags"), + ExternalType = XmlUtil.GetOptionalAttribute(e, "externaltype"), }; var childBeans = new List(); @@ -414,6 +415,11 @@ namespace Luban.Job.Common.Defs m.TypeName = attrEle.Value; break; } + case "create_external_object_function": + { + m.CreateExternalObjectFunction = attrEle.Value; + break; + } default: throw new LoadDefException($"定义文件:{defineFile} externaltype:{externalType} 非法 tag:{tagName}"); } } diff --git a/src/Luban.Job.Common/Source/Defs/DefTypeBase.cs b/src/Luban.Job.Common/Source/Defs/DefTypeBase.cs index 4e1053b..84bee7b 100644 --- a/src/Luban.Job.Common/Source/Defs/DefTypeBase.cs +++ b/src/Luban.Job.Common/Source/Defs/DefTypeBase.cs @@ -31,6 +31,8 @@ namespace Luban.Job.Common.Defs public string FullNameWithTopModule => TypeUtil.MakeFullName(AssemblyBase.TopModule, FullName); + public string CsFullName => TypeUtil.MakeFullName(Namespace, Name); + public string JavaFullName => TypeUtil.MakeFullName(Namespace, Name); public string GoFullName => TypeUtil.MakeGoFullName(Namespace, Name); @@ -83,9 +85,7 @@ namespace Luban.Job.Common.Defs } } - public virtual void PreCompile() { } - - public virtual void Compile() + protected void ResolveExternalType() { if (!string.IsNullOrEmpty(_externalTypeName)) { @@ -100,6 +100,13 @@ namespace Luban.Job.Common.Defs } } + public virtual void PreCompile() { } + + public virtual void Compile() + { + ResolveExternalType(); + } + public virtual void PostCompile() { } } } diff --git a/src/Luban.Job.Common/Source/RawDefs/ExternalType.cs b/src/Luban.Job.Common/Source/RawDefs/ExternalType.cs index 0d61cfa..01f9062 100644 --- a/src/Luban.Job.Common/Source/RawDefs/ExternalType.cs +++ b/src/Luban.Job.Common/Source/RawDefs/ExternalType.cs @@ -14,6 +14,8 @@ namespace Luban.Job.Common.RawDefs public ELanguage Lan { get; set; } public string TypeName { get; set; } + + public string CreateExternalObjectFunction { get; set; } } public class ExternalType diff --git a/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDefineTypeName.cs b/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDefineTypeName.cs index 515e5eb..87053b3 100644 --- a/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDefineTypeName.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDefineTypeName.cs @@ -1,5 +1,6 @@ using Luban.Job.Common.Defs; using Luban.Job.Common.Types; +using Luban.Job.Common.Utils; namespace Luban.Job.Common.TypeVisitors { @@ -59,8 +60,7 @@ namespace Luban.Job.Common.TypeVisitors public string Accept(TEnum type) { - var mapper = type.DefineEnum.CurrentExternalTypeMapper; - return mapper != null ? mapper.TypeName : type.DefineEnum.FullName; + return ExternalTypeUtil.CsMapperToExternalType(type.DefineEnum); } public string Accept(TString type) @@ -80,7 +80,7 @@ namespace Luban.Job.Common.TypeVisitors public string Accept(TBean type) { - return type.Bean.FullName; + return ExternalTypeUtil.CsMapperToExternalType(type.Bean); } public string Accept(TArray type) diff --git a/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDeserializeVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDeserializeVisitor.cs index cf6e6d4..3bd1de3 100644 --- a/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDeserializeVisitor.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDeserializeVisitor.cs @@ -1,5 +1,6 @@ using Luban.Job.Common.Defs; using Luban.Job.Common.Types; +using Luban.Job.Common.Utils; namespace Luban.Job.Common.TypeVisitors { @@ -79,7 +80,8 @@ namespace Luban.Job.Common.TypeVisitors public string Accept(TBean type, string bufName, string fieldName) { - return $"{fieldName} = {type.Bean.FullName}.Deserialize{type.Bean.Name}({bufName});"; + string src = $"{type.Bean.FullName}.Deserialize{type.Bean.Name}({bufName})"; + return $"{fieldName} = {ExternalTypeUtil.CsCloneToExternal(type.Bean, src)};"; } public string Accept(TArray type, string bufName, string fieldName) diff --git a/src/Luban.Job.Common/Source/Utils/ExternalTypeUtil.cs b/src/Luban.Job.Common/Source/Utils/ExternalTypeUtil.cs new file mode 100644 index 0000000..66adaaf --- /dev/null +++ b/src/Luban.Job.Common/Source/Utils/ExternalTypeUtil.cs @@ -0,0 +1,32 @@ +using Luban.Job.Common.Defs; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Luban.Job.Common.Utils +{ + public static class ExternalTypeUtil + { + public static string CsMapperToExternalType(DefTypeBase type) + { + var mapper = type.CurrentExternalTypeMapper; + return mapper != null ? mapper.TypeName : type.CsFullName; + } + + public static string CsCloneToExternal(DefTypeBase type, string src) + { + var mapper = type.CurrentExternalTypeMapper; + if (mapper == null) + { + return src; + } + if (string.IsNullOrWhiteSpace(mapper.CreateExternalObjectFunction)) + { + throw new Exception($"type:{type.FullName} externaltype:{type.ExternalType.Name} lan:{mapper.Lan} selector:{mapper.Selector} 未定义clone_to_external元素"); + } + return $"{mapper.CreateExternalObjectFunction}({src})"; + } + } +}