Compare commits

..

5 Commits

Author SHA1 Message Date
Carson - 宝鱼 720e2eaf49
[fix] 解决7c0579b4bbd7d2c746a8a264d54557ec1e209fd4提交造成的编译错误 (#55)
Co-authored-by: 宝鱼 <kteong1012@outlook.com>
2023-07-06 08:28:08 +08:00
SaNeOr 7c0579b4bb
[fix] 修复 导出python数据, DBool类型 格式问题 (#53) 2023-06-25 18:02:55 +08:00
宝鱼 b791b4276d [new] 新增正则校验器 2023-06-23 12:28:47 +08:00
walon de075ca7d9 [change] DataUtil由GBK编码改为UTF8 2023-06-23 12:21:24 +08:00
SaNeOr 803145e267
[fix] 修复 导出python数据, DMap类型 key的格式问题 (#49)
python的dict类型key不像json只能为string,还可能为其他类型。
2023-06-15 16:46:27 +08:00
5 changed files with 142 additions and 11 deletions

View File

@ -7,7 +7,7 @@ namespace Luban.Job.Cfg.DataVisitors
{
abstract class ToLiteralVisitorBase : IDataFuncVisitor<string>
{
public string Accept(DBool type)
public virtual string Accept(DBool type)
{
return type.Value ? "true" : "false";
}

View File

@ -10,6 +10,11 @@ namespace Luban.Job.Cfg.DataVisitors
{
public static ToPythonLiteralVisitor Ins { get; } = new();
public override string Accept(DBool type)
{
return type.Value ? "True" : "False";
}
public override string Accept(DText type)
{
return $"{{\"{DText.KEY_NAME}\":\"{type.Key}\",\"{DText.TEXT_NAME}\":\"{DataUtil.EscapeString(type.TextOfCurrentAssembly)}\"}}";
@ -94,7 +99,7 @@ namespace Luban.Job.Cfg.DataVisitors
x.Append(',');
}
++index;
x.Append('"').Append(e.Key.ToString()).Append('"');
x.Append(e.Key.Apply(this));
x.Append(':');
x.Append(e.Value.Apply(this));
}

View File

@ -384,6 +384,7 @@ namespace Luban.Job.Cfg.Defs
case "path":
case "range":
case "sep":
case "regex":
{
throw new Exception($"table:'{table.Name}' file:{file.OriginFile} title:'{name}' attr:'{attrName}' 属于type的属性必须用#分割,尝试'{cf.Type}#{attrs[i]}'");
}

View File

@ -30,7 +30,7 @@ namespace Luban.Job.Cfg.Utils
var values = SplitVectorString(x);
if (values.Length != 2)
{
throw new Exception($"'{x}' 不是合法vector2类型数据");
throw new Exception($"'{x}' 不是合法vector2类型数据");
}
return new DVector2(new System.Numerics.Vector2(float.Parse(values[0]), float.Parse(values[1])));
@ -41,7 +41,7 @@ namespace Luban.Job.Cfg.Utils
var values = SplitVectorString(x);
if (values.Length != 3)
{
throw new Exception($"'{x}' 不是合法vector3类型数据");
throw new Exception($"'{x}' 不是合法vector3类型数据");
}
return new DVector3(new System.Numerics.Vector3(float.Parse(values[0]), float.Parse(values[1]), float.Parse(values[2])));
@ -52,7 +52,7 @@ namespace Luban.Job.Cfg.Utils
var values = SplitVectorString(x);
if (values.Length != 4)
{
throw new Exception($"'{x}' 不是合法vector4类型数据");
throw new Exception($"'{x}' 不是合法vector4类型数据");
}
return new DVector4(new System.Numerics.Vector4(float.Parse(values[0]), float.Parse(values[1]), float.Parse(values[2]), float.Parse(values[3])));
}
@ -112,6 +112,10 @@ namespace Luban.Job.Cfg.Utils
{
return s.Replace("\\", "\\\\").Replace("\"", "\\\"");
}
public static string UnEscapeString(string s)
{
return s.Replace("\\\"", "\"").Replace("\\\"", "\"");
}
public static string EscapeLuaStringWithQuote(string s)
{
@ -164,11 +168,11 @@ namespace Luban.Job.Cfg.Utils
{
if (key == null || text == null)
{
throw new Exception("text的key或text属性不能为null");
throw new Exception("text的key或text属性不能为null");
}
if (key == "" && text != "")
{
throw new Exception($"text key为空, 但text:'{text}'不为空");
throw new Exception($"text key为空, 但text:'{text}'不为空");
}
}
@ -244,16 +248,16 @@ namespace Luban.Job.Cfg.Utils
{
if (string.IsNullOrEmpty(subType))
{
throw new Exception($"module:'{bean.Namespace}' 多态数据type不能为空");
throw new Exception($"module:'{bean.Namespace}' 多态数据type不能为空");
}
DefBean defType = bean.GetHierarchyChildren().Cast<DefBean>().Where(c => c.Alias == subType || c.Name == subType || c.FullName == subType).FirstOrDefault();
if (defType == null)
{
throw new Exception($"module:'{bean.Namespace}' type:'{subType}' 不是合法类型");
throw new Exception($"module:'{bean.Namespace}' type:'{subType}' 不是合法类型");
}
if (defType.IsAbstractType)
{
throw new Exception($"module:'{bean.Namespace}' type:'{subType}' 是抽象类. 不能创建实例");
throw new Exception($"module:'{bean.Namespace}' type:'{subType}' 是抽象类. 不能创建实例");
}
return defType;
}
@ -271,7 +275,7 @@ namespace Luban.Job.Cfg.Utils
case "0":
case "n":
case "no": return false;
default: throw new InvalidExcelDataException($"{s} 不是 bool 类型的值 (true|1|y|yes 或 false|0|n|no)");
default: throw new InvalidExcelDataException($"{s} 不是 bool 类型的值 (true|1|y|yes 或 false|0|n|no)");
}
}

View File

@ -0,0 +1,121 @@
using Luban.Job.Cfg.Datas;
using Luban.Job.Cfg.Utils;
using Luban.Job.Common.Defs;
using Luban.Job.Common.Types;
using System;
using System.Text.RegularExpressions;
namespace Luban.Job.Cfg.Validators
{
[Validator("regex")]
class RegexValidator : IValidator
{
public TType Type { get; }
private Regex _regex;
public string Source => ValidatorContext.CurrentVisitor.CurrentValidateRecord.Source;
public RegexValidator(TType type, string strRegex)
{
Type = type;
_regex = new Regex(DataUtil.UnEscapeString(strRegex), RegexOptions.Compiled);
}
public void Compile(DefFieldBase def)
{
switch (Type)
{
case TBean:
throw new Exception($"regex检查器不支持检查Bean类型{Type.TypeName}");
}
}
public void Validate(ValidatorContext ctx, TType type, DType data)
{
Validate(ctx, type.IsNullable, data);
}
private void Validate(ValidatorContext ctx, bool nullable, DType data)
{
var assembly = ctx.Assembly;
void Check(string str)
{
var match = _regex.Match(str);
if (!match.Success)
{
assembly.Agent.Error($"记录 {ValidatorContext.CurrentRecordPath}:{data} (来自文件:{Source}) 不符合正则表达式:'{_regex}'");
}
}
if (nullable && data == null)
{
return;
}
switch (data)
{
case DList dList:
{
foreach (var d in dList.Datas)
{
Validate(ctx, nullable, d);
}
break;
}
case DArray dArray:
{
foreach (var d in dArray.Datas)
{
Validate(ctx, nullable, d);
}
break;
}
case DSet dSet:
{
foreach (var d in dSet.Datas)
{
Validate(ctx, nullable, d);
}
break;
}
case DMap dMap:
{
foreach (var d in dMap.Datas.Values)
{
Validate(ctx, nullable, d);
}
break;
}
case DVector2 dVector2:
{
Check(dVector2.Value.X.ToString());
Check(dVector2.Value.Y.ToString());
break;
}
case DVector3 dVector3:
{
Check(dVector3.Value.X.ToString());
Check(dVector3.Value.Y.ToString());
Check(dVector3.Value.Z.ToString());
break;
}
case DVector4 dVector4:
{
Check(dVector4.Value.X.ToString());
Check(dVector4.Value.Y.ToString());
Check(dVector4.Value.Z.ToString());
Check(dVector4.Value.W.ToString());
break;
}
case DText dText:
{
Check(dText.RawValue);
break;
}
default:
Check(data.ToString());
break;
}
}
}
}