diff --git a/F6/MetaTable.cs b/F6/MetaTable.cs index 665683d..8921d8e 100644 --- a/F6/MetaTable.cs +++ b/F6/MetaTable.cs @@ -6,6 +6,7 @@ using System.Text; using System.Threading.Tasks; using System.Collections; using System.Collections.Generic; +using Newtonsoft; namespace F6 { @@ -54,19 +55,19 @@ namespace F6 public class BaseTable { - private BaseMeta _meta; - private BitArray _flags; - public bool HasValue(string fieldName) + public BaseMeta _meta; + public BitArray _flags = new BitArray(100); + virtual public bool HasValue(string fieldName) { return this.HasValue(this.GetFieldIdx(fieldName)); } - public bool HasValue(int fieldIdx) + virtual public bool HasValue(int fieldIdx) { return this._flags.Get(fieldIdx); } - public int GetFieldIdx(string fieldName) + virtual public int GetFieldIdx(string fieldName) { int idx = 0; this._meta.nameIdxHash.TryGetValue(fieldName, out idx); @@ -75,7 +76,7 @@ namespace F6 } - public class RawMetaTable : IMetaTable + public class RawMetaTable : IMetaTable where T : BaseTable, new() { protected string fileName; protected string primKey; @@ -114,7 +115,66 @@ namespace F6 try { string fileContent = File.ReadAllText(this.fileName); - int i = 0; + var dataSet = Newtonsoft.Json.JsonConvert.DeserializeObject>>(fileContent); + foreach(var row in dataSet) + { + T t = new T(); + Type tType = typeof(T); + Type tBaseType = tType.BaseType; + int fieldIdx = 0; + foreach(var f in tBaseType.GetFields()) + { + string fieldName = f.Name; + var tAttrs = f.GetCustomAttributes(typeof(MetaTableFieldAttribute), false); + if (tAttrs.Length > 0) + { + var tAttr = (MetaTableFieldAttribute)tAttrs.First(); + if (tAttr != null) + { + fieldName = tAttr.fieldName; + } + } + var typeName = f.FieldType.ToString(); + string val; + if (row.TryGetValue(fieldName, out val)) { + switch (typeName) + { + case "System.Int32": + { + f.SetValue(t, int.Parse(val)); + t._flags.Set(fieldIdx, true); + } + break; + case "System.Int64": + { + f.SetValue(t, long.Parse(val)); + t._flags.Set(fieldIdx, true); + } + break; + case "System.Single": + { + f.SetValue(t, float.Parse(val)); + t._flags.Set(fieldIdx, true); + } + break; + case "System.Double": + { + f.SetValue(t, double.Parse(val)); + t._flags.Set(fieldIdx, true); + } + break; + case "System.String": + { + f.SetValue(t, val); + t._flags.Set(fieldIdx, true); + } + break; + } + }//endif + ++fieldIdx; + } + this.rawList.Add(t); + } }catch(Exception e) { int i = 0; @@ -127,7 +187,7 @@ namespace F6 } } - public class IdMetaTable: RawMetaTable + public class IdMetaTable: RawMetaTable where T : BaseTable, new() { protected Dictionary idHash = new Dictionary (); @@ -150,7 +210,7 @@ namespace F6 } - public class NameMetaTable: RawMetaTable + public class NameMetaTable: RawMetaTable where T : BaseTable, new() { protected Dictionary nameHash = new Dictionary(); diff --git a/Mtb/Equip.cs b/Mtb/Equip.cs index a57a691..99ffa51 100644 --- a/Mtb/Equip.cs +++ b/Mtb/Equip.cs @@ -8,7 +8,11 @@ namespace Mtb { class Equip : F6.BaseTable { - [F6.MetaTableField(fieldName ="id")] - public int id { get; } + [F6.MetaTableField(fieldName = "id")] + public readonly int id; + public readonly float f1; + public readonly double f2; + public readonly string f3; + public readonly long f4; } }