using System.Reflection; namespace etoy { /// /// Common表数据 /// class Table { /// /// 表名 /// public string Name; /// /// 表描述 /// public string Description; /// /// 原始表路径 /// public string Path; /// /// 表Tag /// public TableTag TableTag; /// /// 格子数据 /// public List Rows; /// /// 表结构 /// public FieldInfo[] FieldInfos; /// /// 配置表额外生成的枚举 /// public TableEnum tableEnum; // 获取PK字段信息 public FieldInfo GetPKFieldInfo() { if (FieldInfos == null) return null; foreach (var fieldInfo in FieldInfos) { if (fieldInfo.Column == 0 && (fieldInfo.FieldType == FieldTypeDefine.Int || fieldInfo.FieldType == FieldTypeDefine.Long)) { // 第0列为int或者long,默认为主键 return fieldInfo; } if (fieldInfo.TagInfos == null) continue; foreach (var tagInfo in fieldInfo.TagInfos) if (tagInfo.Key == FieldTag.PrimaryKey) return fieldInfo; } return null; } } /// /// 行 /// class Row { /// /// 格子 /// public List Cells; } /// /// 字段信息 /// class FieldInfo { public int Column; public string FieldName; public string FieldType; public bool IsRepeated; public string Description; public string Tags; public FieldTagInfo[] TagInfos; /// /// 是否是服务端字段 /// public bool IsServerField => OutputTag.HasFlag(TableTag.Server); /// /// 是否是客户端字段 /// public bool IsClientField => OutputTag.HasFlag(TableTag.Client); /// /// 是否是Editor字段 /// public bool IsEditorField => OutputTag.HasFlag(TableTag.Editor); public TableTag OutputTag { get { if (TryGetTagValue(FieldTag.Output, out string tagValue)) { if (tagValue == "e") return TableTag.Editor; var result = TableTag.Editor; if (tagValue.Contains('c', StringComparison.OrdinalIgnoreCase)) { result |= TableTag.Client; } if (tagValue.Contains('s', StringComparison.OrdinalIgnoreCase)) { result |= TableTag.Server; } if (result == TableTag.Editor) { throw new Exception($"error tag, FieldName({FieldName}) tags({Tags})"); } return result; } return TableTag.All; } } /// /// 默认值 /// public string DefaultValue { get { if (TryGetTagValue(FieldTag.Default, out var tagValue)) { return tagValue; } else { if (IsRepeated) { return "[]"; } else { string result = FieldType switch { FieldTypeDefine.String => string.Empty, FieldTypeDefine.Int => default(int).ToString(), FieldTypeDefine.Long => default(long).ToString(), FieldTypeDefine.Boolean => default(bool).ToString(), FieldTypeDefine.Float => default(float).ToString(), FieldTypeDefine.Double => default(double).ToString(), _ => string.Empty, }; return result; } } } } /// /// 是否是i18n字段 /// public bool Isi18nField { get { if (TryGetTagValue(FieldTag.i18n, out var tagValue)) return tagValue == "true"; return false; } } /// /// 是否是主键 /// public bool IsPrimaryKey { get { if (Column == 0 && (FieldType == FieldTypeDefine.Int || FieldType == FieldTypeDefine.Long)) { // 第0列为int或者long,默认为主键 return true; } return ContainsTag(FieldTag.PrimaryKey); } } bool ContainsTag(FieldTag tag) { if (TagInfos == null) return false; foreach (var tagInfo in TagInfos) if (tagInfo.Key == tag) return true; return false; } bool TryGetTagValue(FieldTag tag, out string value) { value = null; if (TagInfos == null) return false; foreach (var tagInfo in TagInfos) if (tagInfo.Key == tag) { value = tagInfo.Value; return true; } return false; } } /// /// 格子信息 /// class Cell { string _value; public int Column; public int Row; public FieldInfo FieldInfo; public string Value { get { if (string.IsNullOrEmpty(_value)) _value = FieldInfo.DefaultValue.ToCellValue(); return _value; } set => _value = value; } } /// /// 字段tag信息 /// class FieldTagInfo { public FieldTag Key; public string Value; } /// /// 字段tag, 对应Excel第4行, 忽略大小写 /// enum FieldTag { /// /// 主键 /// PrimaryKey, /// /// 默认值 /// Default, /// /// 输出去向 (output:c output:s) /// Output, /// /// 翻译 /// i18n, i18nPlot, i18nHashKey, i18nUnvalidJson, NoTranslate, i18nStrKey, } /// /// 表格Tag, 指定Table为客户端或服务端。 /// 1.SheetName以 "c_" 或者 "s_" 开头以示区分, 未填则all。 /// 2.忽略大小写 /// enum TableTag { Client = 0x1, Server = 0x10, Editor = 0x100, All = Client | Server | Editor, } /// /// 配置表转枚举 /// 有些表需要将id转成枚举方便使用 /// class TableEnum { /// /// 枚举名字 /// public string name; /// /// 枚举类型的配置表字段名 /// public string key; /// /// 枚举值的配置表字段名 /// public string value; /// /// 枚举注释的配置表字段名 /// public string summary; } }