static void InitField(StringBuilder initer, FieldWrap field) { int level = SEM_LEVEL + 1; if (field.IsRaw || field.IsEnum || field.IsClass) { initer.Space(SEM_LEVEL); initer.AppendLine($"{field.Name} = {ReadValue(field)};"); } else if (field.OriginalType == Setting.LIST) { initer.Space(SEM_LEVEL).AppendLine($"for (int n = {ARG_DATASTREAM}.GetArrayLength(); n-- > 0;)"); initer.Space(SEM_LEVEL).AppendLine("{"); { FieldWrap item = field.GetItemDefine(); initer.Space(level).AppendLine($"var v = {ReadValue(item)};"); initer.Space(level).AppendLine($"{field.Name}.Add(v);"); } initer.Space(SEM_LEVEL).AppendLine("}"); } else if (field.OriginalType == Setting.DICT) { initer.Space(SEM_LEVEL).AppendLine($"for (int n = {ARG_DATASTREAM}.GetMapLength(); n-- > 0;)"); initer.Space(SEM_LEVEL).AppendLine("{"); { FieldWrap key = field.GetKeyDefine(); FieldWrap value = field.GetValueDefine(); initer.Space(level).AppendLine($"var k = {ReadValue(key)};"); initer.Space(level).AppendLine($"{field.Name}[k] = {ReadValue(value)};"); } initer.Space(SEM_LEVEL).AppendLine("}"); } }
static string ReadValue(FieldWrap field) { if (field.IsRaw) { return($"{ARG_DATASTREAM}.Get{field.FullName.FirstCharUpper()}()"); } else if (field.IsEnum) { return($"({Util.CorrectFullType(field.FullName)}){ARG_DATASTREAM}.GetInt()"); } else if (field.IsClass) { ClassWrap info = ClassWrap.Get(field.FullName); string fullType = Util.CorrectFullType(field.FullName); if (info.IsDynamic()) { return($"({fullType}){ARG_DATASTREAM}.GetObject({ARG_DATASTREAM}.GetString())"); } else { return($"new {fullType}({ARG_DATASTREAM})"); } } else { Util.Error("不支持集合嵌套类型:" + field.FullName); } return(null); }
static string GetDefineName(FieldWrap field) { if (field.IsRaw) { return($"o.Get{Util.FirstCharUpper(field.FullName)}()"); } else if (field.IsEnum) { return($"<{CorrectNamespace(field.FullName)}>o.GetInt()"); } else if (field.IsClass) { var cls = ClassWrap.Classes[field.FullName]; string parseName = GetDynamicName(cls); return($"o.Get{parseName}()"); } else if (field.OriginalType == Setting.LIST) { return($"o.Get{Util.FirstCharUpper(field.OriginalType)}('{GetDefineName(field.Types[1])}')"); } else if (field.OriginalType == Setting.DICT) { return($"o.Get{Util.FirstCharUpper(field.OriginalType)}('{GetDefineName(field.Types[1])}', '{GetDefineName(field.Types[2])}')"); } else { return("unknow:" + field.FullName); } }
public FClass(FClass host, FieldWrap define, XmlElement value) : base(host, define) { _fullType = define.FullName; ClassWrap info = ClassWrap.Get(define.FullName); Load(info, value); }
static void ReadFunc(StringBuilder reader, FieldWrap field) { int level = SEM_LEVEL + 1; string type = Util.CorrectFullType(field.FullName); if (field.IsRaw) { reader.Space(level).AppendLine($"case \"{field.Name}\": {field.Name} = Read{type.FirstCharUpper()}(_2); break;"); } else if (field.IsEnum) { reader.Space(level).AppendLine($"case \"{field.Name}\": {field.Name} = ({type})ReadInt(_2); break;"); } else if (field.IsClass) { if (field.IsDynamic) { string ns = type.Substring(0, type.LastIndexOf('.')); reader.Space(level).AppendLine($"case \"{field.Name}\": {field.Name} = ReadDynamicObject<{type}>(_2, \"{ns}\"); break;"); } else { reader.Space(level).AppendLine($"case \"{field.Name}\": {field.Name} = ReadObject<{type}>(_2, \"{type}\"); break;"); } } else if (field.IsContainer) { var level0 = level + 1; reader.Space(level).AppendLine($"case \"{field.Name}\":"); if (field.OriginalType == Setting.LIST) { var item = field.GetItemDefine(); reader.Space(level0).AppendLine($"var {field.Name}s = GetChilds(_2);"); reader.Space(level0).AppendLine($"for (int i = 0; i < {field.Name}s.Count; i++)"); reader.Space(level0).AppendLine($"{{"); reader.Space(level0 + 1).AppendLine($"var _3 = {field.Name}s[i];"); reader.Space(level0 + 1).AppendLine($"{field.Name}.Add({ReadList(item)});"); reader.Space(level0).AppendLine($"}}"); reader.Space(level0).AppendLine($"break;"); } else if (field.OriginalType == Setting.DICT) { var key = field.GetKeyDefine(); var value = field.GetValueDefine(); reader.Space(level0).AppendLine($"var {field.Name}s = GetChilds(_2);"); reader.Space(level0).AppendLine($"for (int i = 0; i < {field.Name}s.Count; i++)"); reader.Space(level0).AppendLine($"{{"); reader.Space(level0 + 1).AppendLine($"var _3 = {field.Name}s[i];"); reader.Space(level0 + 1).AppendLine($"var key = {ReadDict(key, Setting.KEY)};"); reader.Space(level0 + 1).AppendLine($"var value = {ReadDict(value, Setting.VALUE)};"); reader.Space(level0 + 1).AppendLine($"{field.Name}.Add(key, value);"); reader.Space(level0).AppendLine($"}}"); reader.Space(level0).AppendLine($"break;"); } } }
public FInt(FClass host, FieldWrap define, XmlElement value) : base(host, define) { string v = value.InnerText; int r; if (!int.TryParse(v, out r)) { Util.Error("{0}非int类型或者数值溢出", v); Value = -1; } Value = r; }
public FLong(FClass host, FieldWrap define, XmlElement xml) : base(host, define) { string v = xml.InnerText; long r; if (!long.TryParse(v, out r)) { Util.Error("{0}非long类型或者数值溢出", v); Value = -1; } Value = r; }
public FFloat(FClass host, FieldWrap define, XmlElement xml) : base(host, define) { string v = xml.InnerText; float r; if (!float.TryParse(v, out r)) { Util.Error("{0}非float类型或者数值溢出", v); Value = -1; } Value = r; }
public static void AddIndex(FieldWrap index, Data data) { if (_indexs.ContainsKey(index)) { _indexs[index].Add(data); } else { _indexs.Add(index, new HashSet <Data>() { data }); } }
public override void GetList(FList data, FieldWrap define) { FieldWrap item = define.GetItemDefine(); while (!IsSectionEnd()) { var d = Data.Create(data.Host, item, this); if (data.IsRoot)//数据表List { Program.AddLastData(d); } data.Values.Add(d); } }
public FEnum(FClass host, FieldWrap define, ImportExcel excel) : base(host, define) { EnumWrap info = EnumWrap.Enums[define.OriginalType]; string name = excel.GetEnum(); EnumName = info.GetEnumName(name); EnumName = EnumName.IsEmpty() ? name : EnumName; Value = info.GetEnumValue(EnumName); if (!info.ContainItem(name)) { excel.Error($"未定义枚举(名称/别名){define.FullName}.{name} !"); } }
public Checker(FieldWrap define, string rules) { _define = define; _rules = rules; string[] nodes = rules.Split(Setting.CheckSplit, System.StringSplitOptions.RemoveEmptyEntries); _ruleTable = new RuleInfo[nodes.Length]; for (int i = 0; i < nodes.Length; i++) { _ruleTable[i] = new RuleInfo() { _rule = nodes[i] } } ; }
static void Field(FieldWrap field) { Comment(field.Desc, MEM_LEVEL); builder.Space(MEM_LEVEL); if (field.IsRaw || field.IsEnum || field.IsClass) { builder.AppendLine($"{FEILD_MODIFIERS} {Util.CorrectFullType(field.FullName)} {field.Name};"); } else if (field.OriginalType == Setting.LIST) { builder.AppendLine($"{FEILD_MODIFIERS} List<{Util.CorrectFullType(field.Types[1])}> {field.Name} = new List<{Util.CorrectFullType(field.Types[1])}>();"); } else if (field.OriginalType == Setting.DICT) { builder.AppendLine($"{FEILD_MODIFIERS} Dictionary<{field.Types[1]}, {Util.CorrectFullType(field.Types[2])}> {field.Name} = new Dictionary<{field.Types[1]}, {Util.CorrectFullType(field.Types[2])}>();"); } }
public FBool(FClass host, FieldWrap define, XmlElement xml) : base(host, define) { string v = xml.InnerText; if (v.Equals("true", StringComparison.CurrentCultureIgnoreCase)) { Value = true; } else if (v.Equals("false", StringComparison.CurrentCultureIgnoreCase)) { Value = false; } else { Util.Error("{0}非bool类型", v); Value = false; } }
static string GetType(FieldWrap field) { if (field.IsRaw || field.IsEnum || field.IsClass) { return(GetRawType(field.OriginalType)); } else if (field.OriginalType == Setting.LIST) { return($"{GetRawType(field.Types[1])}[]"); } else if (field.OriginalType == Setting.DICT) { return($"Map<{GetRawType(field.Types[1])}, {GetRawType(field.Types[2])}>"); } else { return("unknow"); } }
public override void GetDict(FDict data, FieldWrap define) { FieldWrap key = define.GetKeyDefine(); FieldWrap value = define.GetValueDefine(); while (!IsSectionEnd()) { var dk = Data.Create(data.Host, key, this); var dv = Data.Create(data.Host, value, this); if (!data.Values.ContainsKey(dk)) { data.Values.Add(dk, dv); } else { Error($"字段:{define.Name} Key:{dk} 重复"); } } }
static string ReadDict(FieldWrap field, string node) { var type = Util.CorrectFullType(field.FullName); if (field.IsRaw) { return($"Read{type.FirstCharUpper()}(GetOnlyChild(_3, \"{node}\"))"); } else if (field.IsEnum) { return($"({type})ReadInt(GetOnlyChild(_3, \"{node}\"))"); } else if (field.IsClass) { return($"ReadObject<{type}>(GetOnlyChild(_3, \"{node}\"), \"{type}\")"); } else { Util.LogError($"集合中类型仅支持基础类型,Enum,Class!"); return("<集合中类型仅支持基础类型,Enum,Class!>"); } }
public FDict(FClass host, FieldWrap define, XmlElement data) : base(host, define) { _key = define.GetKeyDefine(); _value = define.GetValueDefine(); XmlNodeList dict = data.ChildNodes; for (int i = 0; i < dict.Count; i++) { XmlNode pair = dict[i]; XmlElement key = pair[Setting.KEY]; XmlElement value = pair[Setting.VALUE]; var dk = Data.Create(host, _key, key); var dv = Data.Create(host, _value, value); if (!Values.ContainsKey(dk)) { Values.Add(dk, dv); } else { Util.Error("字段:{0} Key:{1} 重复", define.Name, dk); } } }
/// <summary> /// 数据结构定义 /// </summary> private static void GenDataStruct() { StringBuilder builder = new StringBuilder(); builder.AppendLine("local Stream = require(\"Common.DataStream\")"); builder.AppendLine("local GetOrCreate = Util.GetOrCreate"); builder.AppendLine(); builder.AppendLine("local meta"); var cit = ClassWrap.Classes.GetEnumerator(); while (cit.MoveNext()) { var cls = cit.Current.Value; var fullType = Util.CorrectFullType(cls.FullName); builder.AppendLine("meta= {}"); builder.AppendLine("meta.__index = meta"); builder.AppendFormat("meta.__cname = '{0}'\n", fullType); //--常量字段 for (int j = 0; j < cls.Consts.Count; j++) { ConstWrap constant = cls.Consts[j]; if (!Util.MatchGroups(constant.Group)) { continue; } string value = constant.Value; switch (constant.FullName) { case Setting.BOOL: value = value.ToLower(); break; case Setting.STRING: value = string.Format("'{0}'", value); break; default: if (EnumWrap.IsEnum(constant.FullName)) { value = EnumWrap.Enums[constant.FullName].GetValue(constant.Value); } break; } builder.AppendFormat("meta.{0} = {1}\n", constant.Name, value); } builder.AppendFormat("GetOrCreate('{0}.{1}')['{2}'] = meta\n", Setting.ModuleName, cls.Namespace, cls.Name); string funcName = fullType.Replace(".", ""); if (cls.IsDynamic()) { builder.AppendFormat("function Stream:Get{0}Maker()\n", funcName); builder.Space().AppendFormat("return self['Get' .. self:GetString():gsub('%.', '')](self)\n"); builder.AppendFormat("end\n"); } builder.AppendFormat("function Stream:Get{0}()\n", funcName); if (!cls.Inherit.IsEmpty()) { builder.Space().AppendFormat("local o = self:Get{0}()\n", Util.CorrectFullType(cls.Inherit).Replace(".", "")); } else { builder.Space().AppendFormat("local o = {{}}\n"); } builder.Space().AppendFormat("setmetatable(o, {0})\n", fullType); //--普通字段 for (int j = 0; j < cls.Fields.Count; j++) { FieldWrap field = cls.Fields[j]; if (!Util.MatchGroups(field.Group)) { continue; } if (field.IsRaw) { builder.Space().AppendFormat("o.{0} = self:Get{1}()\n", field.Name, field.OriginalType.FirstCharUpper()); } else if (field.IsEnum) { builder.Space().AppendFormat("o.{0} = self:GetInt()\n", field.Name); } else if (field.IsClass) { if (field.IsDynamic) { builder.Space().AppendFormat("o.{0} = self:Get{1}Maker()\n", field.Name, Util.CorrectFullType(field.OriginalType).Replace(".", "")); } else { builder.Space().AppendFormat("o.{0} = self:Get{1}()\n", field.Name, Util.CorrectFullType(field.OriginalType).Replace(".", "")); } } else if (field.IsContainer) { if (field.OriginalType == Setting.LIST) { var item = field.GetItemDefine(); string index = Util.CorrectFullType(item.OriginalType).Replace(".", "").FirstCharUpper(); if (item.IsClass && item.IsDynamic) { index += "Maker"; } builder.Space().AppendFormat("o.{0} = self:GetList('{1}')\n", field.Name, index); } else if (field.OriginalType == Setting.DICT) { var k = field.GetKeyDefine(); string key = k.OriginalType.FirstCharUpper(); if (k.IsEnum) { key = "Int"; } var v = field.GetValueDefine(); string value = Util.CorrectFullType(v.OriginalType).Replace(".", "").FirstCharUpper(); if (v.IsClass && v.IsDynamic) { value += "Maker"; } else if (v.IsEnum) { value = "Int"; } builder.Space().AppendFormat("o.{0} = self:GetDict('{1}', '{2}')\n", field.Name, key, value); } } } builder.Space().AppendFormat("return o\n"); builder.AppendFormat("end\n"); } var eit = EnumWrap.Enums.GetEnumerator(); while (eit.MoveNext()) { EnumWrap en = eit.Current.Value; builder.AppendFormat("GetOrCreate('{0}.{1}')['{2}'] = {{\n", Setting.ModuleName, en.Namespace, en.Name); foreach (var item in en.Values) { builder.Space().AppendFormat("{0} = {1},\n", item.Key, item.Value); } builder.AppendLine("}"); } builder.AppendLine("return Stream"); string path = Path.Combine(Setting.LuaDir, DATA_STRUCT + ".lua"); Util.SaveFile(path, builder.ToString()); }
public abstract void GetDict(FDict data, FieldWrap define);
public abstract void GetList(FList data, FieldWrap define);
public static Data Create(FClass host, FieldWrap define, ImportExcel excel) { string type = define.OriginalType; if (define.IsRaw) { switch (type) { case Setting.BOOL: return(new FBool(host, define, excel)); case Setting.INT: return(new FInt(host, define, excel)); case Setting.LONG: return(new FLong(host, define, excel)); case Setting.FLOAT: return(new FFloat(host, define, excel)); case Setting.STRING: return(new FString(host, define, excel)); } } else if (define.IsEnum) { return(new FEnum(host, define, excel)); } else if (define.IsClass) { if (!define.IsDynamic) { return(new FClass(host, define, excel)); } string dType = excel.GetString(); ClassWrap cls = ClassWrap.Get(define.FullName); dType = ClassWrap.CorrectType(cls, dType); ClassWrap dynamic = ClassWrap.Get(dType); if (dynamic == null) { excel.Error("多态类型" + dType + "未知"); } if (cls.FullName != dType && !cls.HasChild(dType)) { excel.Error(string.Format("数据类型{0}非{1}子类", dType, cls.FullName)); } var define0 = new FieldWrap(define.Host, define.Name, dType, new string[] { define.FullName }, define.Group); return(new FClass(host, define0, excel)); } else if (define.IsContainer) { if (define.OriginalType == "list") { return(new FList(host, define, excel)); } else if (define.OriginalType == "dict") { return(new FDict(host, define, excel)); } } Util.LogError("未知类型" + type); return(null); }
public FEnum(FClass host, FieldWrap define, XmlElement xml) : base(host, define) { Value = xml.InnerText;//Xml中枚举以int表示 }
public NotEmptyChecker(FieldWrap define, string rule) : base(define, rule) { }
static void GenClass() { List <ClassWrap> classes = ClassWrap.GetExports(); for (int i = 0; i < classes.Count; i++) { ClassWrap cls = classes[i]; //命名空间 builder.AppendLine(string.Join("\r\n", namespaces)); builder.AppendLine($"namespace {Setting.ModuleName}.{cls.Namespace}"); Start(0); { //类 Comment(cls.Desc, TYPE_LEVEL); builder.Space(TYPE_LEVEL).AppendLine(cls.Attribute); if (cls.Inherit.IsEmpty()) { builder.Space(TYPE_LEVEL).AppendLine($"public partial class {cls.Name} : {CLASS_XML_OBJECT}"); } else { builder.Space(TYPE_LEVEL).AppendLine($"public partial class {cls.Name} : {Util.CorrectFullType(cls.Inherit)}"); } Start(TYPE_LEVEL); { StringBuilder writer = new StringBuilder(); StringBuilder reader = new StringBuilder(); for (int j = 0; j < cls.Consts.Count; j++) { ConstWrap constant = cls.Consts[j]; if (!Util.MatchGroups(constant.Group)) { continue; } Const(constant);//常量成员 } for (int j = 0; j < cls.Fields.Count; j++) { FieldWrap field = cls.Fields[j]; if (!Util.MatchGroups(field.Group)) { continue; } Field(field); //字段成员 WriteFunc(writer, field); //写xml数据 ReadFunc(reader, field); //读xml数据 } //写函数 builder.Space(MEM_LEVEL); builder.AppendLine($"public override void Write(TextWriter _1)"); Start(MEM_LEVEL); if (!cls.Inherit.IsEmpty()) { builder.Space(SEM_LEVEL).AppendLine("base.Write(_1);"); } builder.Append(writer.ToString()); End(MEM_LEVEL); //读函数 builder.Space(MEM_LEVEL); builder.AppendLine($"public override void Read(XmlNode _1)"); Start(MEM_LEVEL); { if (!cls.Inherit.IsEmpty()) { builder.Space(SEM_LEVEL).AppendLine("base.Read(_1);"); } builder.Space(SEM_LEVEL).AppendLine($"foreach (System.Xml.XmlNode _2 in GetChilds (_1))"); builder.Space(SEM_LEVEL).AppendLine($"switch (_2.Name)"); Start(SEM_LEVEL); builder.Append(reader.ToString()); End(SEM_LEVEL); } End(MEM_LEVEL); } End(TYPE_LEVEL); } End(0); string path = Path.Combine(Setting.XmlCodeDir, $"{Setting.ModuleName}.{cls.FullName}.cs"); Util.SaveFile(path, builder.ToString()); builder.Clear(); } }
public RangeChecker(FieldWrap define, string rule) : base(define, rule) { }
public Data(FClass host, FieldWrap define) { _define = define; _host = host; }
public FClass(FClass host, FieldWrap define, ImportExcel excel) : base(host, define) { _fullType = define.FullName; excel.GetClass(this, ClassWrap.Get(define.FullName)); }
//_ruleTable.Length == 0表示不需要检查dict.value //dict检查需要在规则中添加value public UniqueChecker(FieldWrap define, string rule) : base(define, rule) { }
static void WriteFunc(StringBuilder writer, FieldWrap field) { writer.Space(SEM_LEVEL).AppendLine($"Write(_1, \"{field.Name}\", {field.Name});"); }