示例#1
0
        void Start()
        {
            mDownloader = new AssetDownloader();
            mPreStep = Step.None;
            mStep = Step.HashmapHash;

            // 读取本地的配置列表
            string localFiles = UrlUtil.Combine(Settings.UNITY_RAW_FOLDER, "files");
            if (File.Exists(localFiles))
            {
                string[] array = File.ReadAllLines(localFiles);
                mLocalCfgMap = new Dictionary<string, AssetCfg>(array.Length);
                for (int i = 0; i < array.Length; i++)
                {
                    string line = array[i];
                    if (string.IsNullOrEmpty(line))
                        continue;
                    string[] cells = line.Split('|');
                    if (cells == null || cells.Length == 0)
                        continue;
                    AssetCfg cfg = new AssetCfg(cells);
                    mLocalCfgMap.Add(cfg.key, cfg);
                }
            }
            else
            {
                mLocalCfgMap = new Dictionary<string, AssetCfg>();
            }
        }
示例#2
0
 private void OnSucceed(string url, byte[] raw, object userData)
 {
     switch (mPreStep)
     {
         case Step.HashmapHash:
             {
                 // 和本地的HashmapHash进行对比
                 mServerHash = new UTF8Encoding(false).GetString(raw);
                 string local_hashmaphash = GetLocalHashmapHash();
                 if (string.Equals(local_hashmaphash, mServerHash))
                 {
                     SendLocalMapToAssetManager();
                     Finish();
                 }
                 else
                     mStep = Step.Hashmap;
             }
             break;
         case Step.Hashmap:
             {
                 mServerFiles = new UTF8Encoding(false).GetString(raw);
                 string[] array = mServerFiles.Replace("\r\n", "\n").Split('\n');
                 mUpdateQueue = new Queue<string>(array.Length);// 筛选出待更新的项
                 mTotalUpdateSize = 0u;
                 for (int i = 0; i < array.Length; i++)
                 {
                     string line = array[i];
                     if (string.IsNullOrEmpty(line))
                         continue;
                     string[] cells = line.Split('|');
                     if (cells == null || cells.Length == 0)
                         continue;
                     string key = cells[0];
                     AssetCfg item = new AssetCfg(cells, GetLocalItemHash(key));
                     AssetManager.AddCfg(item);
                     if (item.needUpdate)
                     {
                         mUpdateQueue.Enqueue(key);
                         mTotalUpdateSize += item.size;
                     }
                 }
                 mUpdateQueue.TrimExcess();
                 // 选择Patch或更新
                 if (mUpdateQueue.Count == 0)
                 {
                     SendLocalMapToAssetManager();
                     mStep = Step.Patch;
                     onProgress(null, 1f);
                 }
                 else
                 {
                     mStep = Step.Update;
                     mUpdatedSize = 0u;
                     onProgress(null, 0f);
                 }
             }
             break;
         case Step.Update:
             {
                 // 写入到Raw中
                 string key = userData.ToString();
                 string rawPath = GetRawPath(key, true);
                 File.WriteAllBytes(rawPath, raw);
                 // 检查队列
                 if (mUpdateQueue.Count > 0)
                     mStep = Step.Update;
                 else
                     mStep = Step.Patch;
                 // 更新进度
                 AssetCfg item = AssetManager.GetCfg(key);
                 mUpdatedSize += item.size;
                 float percent = (mUpdatedSize * 1.0f / mTotalUpdateSize);
                 onProgress(key, percent);
             }
             break;
     }
 }