示例#1
0
        // 加载数据
        public static TableSet <T> Load(Stream inputReader)
        {
            TableSet <T> inst = Instance;

            inst.m_IsLoading = true;
            if (inputReader != null)
            {
                StreamReader reader = new StreamReader(inputReader);
                string       line;
                while ((line = reader.ReadLine()) != null)
                {
                    if (string.IsNullOrEmpty(line))
                    {
                        continue;
                    }
                    JObject obj   = JsonConvert.DeserializeObject <JObject>(line);
                    T       table = new T();
                    table.Init(obj);
                    inst.m_Datas[table.Id] = table;
                }
                reader.Close();
                reader.Dispose();
            }
            inst.m_IsLoading = false;
            return(inst);
        }
示例#2
0
        public static TableSet <T> Load(string tablename, string text)
        {
            TableSet <T> inst = Instance;

            LoadTo(tablename, text, inst);
            return(inst);
        }
示例#3
0
        public static TableSet <T> LoadAsNew(string tablename, string text)
        {
            TableSet <T> inst = new TableSet <T>();

            LoadTo(tablename, text, inst);
            return(inst);
        }
示例#4
0
 public static void Release()
 {
     if (s_Instance != null)
     {
         s_Instance.m_Datas.Clear();
         s_Instance = null;
     }
 }
示例#5
0
 // notify load complete.
 public static void LoadComplete(TableSet <T> table, bool merge)
 {
     if (table != null && table.IsLoading)
     {
         table.IsLoading = false;
         MainThread.RunOnMainThread(table.FireCompleteEvent, merge);
     }
 }
示例#6
0
        public static void MergeTo(TableSet <T> tables, TableSet <T> mergeTo)
        {
            if (tables == null || tables.Count == 0 || mergeTo == null)
            {
                return;
            }
            mergeTo.IsLoading = true;
            foreach (var tab in tables.mMerged)
            {
                mergeTo.mMerged.Add(tab);
            }
            List <T> list = new List <T>(mergeTo.Count + tables.Count);
            int      a    = 0;
            int      b    = 0;

            while (a < mergeTo.Count || b < tables.Count)
            {
                var ta = a < mergeTo.Count ? mergeTo.m_Datas[a] : null;
                var tb = b < tables.Count ? tables.m_Datas[b] : null;
                if (ta == null && tb == null)
                {
                    continue;
                }
                if (ta != null && tb != null)
                {
                    if (tb.Identify < ta.Identify)
                    {
                        list.Add(tb);
                        b++;
                    }
                    else if (ta.Identify < tb.Identify)
                    {
                        list.Add(ta);
                        a++;
                    }
                    else
                    {
                        list.Add(tb);
                        b++;
                        a++;
                    }
                }
                else if (ta == null)
                {
                    list.Add(tb);
                    b++;
                }
                else
                {
                    list.Add(ta);
                    a++;
                }
            }
            mergeTo.m_Datas   = list.ToArray();
            mergeTo.IsLoading = false;
            MainThread.RunOnMainThread(mergeTo.FireCompleteEvent, true);
        }
示例#7
0
        public static TableSet <T> LoadFromResources(string resPath)
        {
            TableSet <T> inst  = Instance;
            TextAsset    asset = Resources.Load <TextAsset>(resPath);

            if (asset)
            {
                return(Load(asset.text));
            }
            else
            {
                return(inst);
            }
        }
示例#8
0
        public static TableSet <T> Merge(string tablename, string text)
        {
            TableSet <T> inst = LoadAsNew(tablename, text);

            if (s_Instance == null)
            {
                s_Instance = inst;
            }
            else
            {
                MergeTo(inst, s_Instance);
            }
            return(s_Instance);
        }
示例#9
0
        public static TableSet <T> Load(string text)
        {
            TableSet <T> inst = Instance;

            inst.m_IsLoading = true;
            if (!string.IsNullOrEmpty(text))
            {
                string[] txt = text.Split('\n');
                for (int i = 0; i < txt.Length; i++)
                {
                    string s = txt[i].Trim();
                    if (string.IsNullOrEmpty(s))
                    {
                        continue;
                    }
                    JObject obj   = JsonConvert.DeserializeObject <JObject>(s);
                    T       table = new T();
                    table.Init(obj);
                    inst.m_Datas[table.Id] = table;
                }
            }
            inst.m_IsLoading = false;
            return(inst);
        }
示例#10
0
 public static void LoadTo(string tablename, string text, TableSet <T> tab)
 {
     if (!string.IsNullOrEmpty(text) && tab != null)
     {
         tab.IsLoading = true;
         tab.TableName = tablename;
         tab.mMerged.Clear();
         tab.mMerged.Add(HashTable(tablename));
         JsonData arr = JsonMapper.ToObject(text);
         //JArray arr = JsonConvert.DeserializeObject<JArray>(text);
         if (tab.m_Datas == null || tab.m_Datas.Length != arr.Count)
         {
             tab.m_Datas = new T[arr.Count];
         }
         for (int i = 0; i < arr.Count; i++)
         {
             T table = new T();
             table.Init(arr[i]);
             tab.m_Datas[i] = table;
         }
         tab.IsLoading = false;
         MainThread.RunOnMainThread(tab.FireCompleteEvent, false);
     }
 }