diff --git a/README.md b/README.md index 271a02c..73dde5e 100644 --- a/README.md +++ b/README.md @@ -736,7 +736,7 @@ binary格式占空间最小,lua其次,json最大。 ![pipeline](docs/images/examples/d_70.jpg) -## [LubanAssistant](https://github.com/focus-creative-games/Tools/LubanAssistant) Excel插件。神器! +## [LubanAssistant](https://github.com/focus-creative-games/luban_examples/tree/main/Tools/LubanAssistant) Excel插件。神器! 对于多人协作编辑的配置表,如何以xlsx格式保存配置,很容易出现数据冲突覆盖的问题,在大型项目中尤为严重。另外合并多分支数据时,xlsx无法像文本文件那样自动解决冲突,给项目的版本维护带来很大麻烦。 一个合理的解决思路是,以json、xml之类格式保存配置数据,在excel中编辑。LubanAssistant较好地解决了这个问题,使用者既享受luban强大的数据处理能力、同时拥有json良好的可阅读性及多版本可维护性,还能兼顾excel的便捷的编辑能力。 diff --git a/src/Luban.Job.Cfg/Source/Validators/SizeValidator.cs b/src/Luban.Job.Cfg/Source/Validators/SizeValidator.cs new file mode 100644 index 0000000..3e59dab --- /dev/null +++ b/src/Luban.Job.Cfg/Source/Validators/SizeValidator.cs @@ -0,0 +1,90 @@ +using Luban.Job.Cfg.Datas; +using Luban.Job.Common.Defs; +using Luban.Job.Common.Types; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Luban.Job.Cfg.Validators +{ + internal class SizeValidator : IValidator + { + public const string NAME = "size"; + private readonly TType _field; + private readonly int _size; + + public SizeValidator(TType field, string rule) + { + this._field = field; + this._size = int.Parse(rule); + } + + public void Compile(DefFieldBase def) + { + + } + + private string Source => ValidatorContext.CurrentVisitor.CurrentValidateRecord.Source; + + public void Validate(ValidatorContext ctx, TType type, DType data) + { + var assembly = ctx.Assembly; + void LogError(int size) + { + assembly.Agent.Error("记录 {0}:{1} (来自文件:{2}) size:{3},但要求为 {4} ", ValidatorContext.CurrentRecordPath, data, Source, size, _size); + } + + if (type.IsNullable && data == null) + { + return; + } + + switch (data) + { + case DArray b: + { + if (b.Datas.Count != _size) + { + LogError(b.Datas.Count); + return; + } + break; + } + case DList b: + { + if (b.Datas.Count != _size) + { + LogError(b.Datas.Count); + return; + } + break; + } + case DSet b: + { + if (b.Datas.Count != _size) + { + LogError(b.Datas.Count); + return; + } + break; + } + case DMap b: + { + if (b.Datas.Count != _size) + { + LogError(b.Datas.Count); + return; + } + break; + } + default: + { + assembly.Agent.Error("记录 {0}:{1} (来自文件:{2}) 不支持 size", ValidatorContext.CurrentRecordPath, data, Source); + return; + } + } + } + } +} diff --git a/src/Luban.Job.Cfg/Source/Validators/ValidatorFactory.cs b/src/Luban.Job.Cfg/Source/Validators/ValidatorFactory.cs index 1aeb015..9ee1498 100644 --- a/src/Luban.Job.Cfg/Source/Validators/ValidatorFactory.cs +++ b/src/Luban.Job.Cfg/Source/Validators/ValidatorFactory.cs @@ -11,7 +11,13 @@ namespace Luban.Job.Cfg.Validators { private static readonly NLog.Logger s_logger = NLog.LogManager.GetCurrentClassLogger(); - private readonly static List s_validatorNames = new List() { RefValidator.NAME, PathValidator.NAME, RangeValidator.NAME }; + private readonly static List s_validatorNames = new List() + { + RefValidator.NAME, + PathValidator.NAME, + RangeValidator.NAME, + SizeValidator.NAME + }; public static List ValidatorNames => s_validatorNames; @@ -32,8 +38,12 @@ namespace Luban.Job.Cfg.Validators { return new RangeValidator(field, rule); } + case SizeValidator.NAME: + { + return new SizeValidator(field, rule); + } default: - throw new NotSupportedException("unknown validator type:" + type); + throw new NotSupportedException("unknown validator type:" + type); } } }