Example #1
0
        /// <summary>
        /// Md5码校验
        /// </summary>
        /// <param name="downLoadAssets"></param>
        /// <param name="callBack"></param>
        void VerifyMD5(List <DownLoadAssetBundle> downLoadAssets, Action callBack)
        {
            //存储MD5码错误的补丁文件,这些要重新下载
            List <Patch> downLoadList = new List <Patch>();

            foreach (DownLoadAssetBundle downLoad in downLoadAssets)
            {
                string md5 = "";
                if (m_DownLoadMD5Dic.TryGetValue(downLoad.FileName, out md5))
                {
                    Debug.Log("下载的文件的MD5:" + MD5Manager.Instance.BuildFileMd5(downLoad.SaveFilePath));
                    Debug.Log("服务器上该文件的MD5:" + md5);
                    if (MD5Manager.Instance.BuildFileMd5(downLoad.SaveFilePath) != md5)
                    {
                        Debug.Log(string.Format("此文件{0}MD5校验失败,即将重新下载", downLoad.FileName));
                        Patch patch = FindPatchByGamePath(downLoad.FileName);
                        if (patch != null)
                        {
                            downLoadList.Add(patch);
                        }
                    }
                }
            }

            //全部文件都是正确的
            if (downLoadList.Count <= 0)
            {
                m_DownLoadMD5Dic.Clear();
                if (callBack != null)
                {
                    StartDownload = false;
                    callBack();
                }
                if (LoadOver != null)
                {
                    LoadOver();
                }
            }
            else
            {
                if (m_TryDownCount >= DOWNLOADCOUNT)
                {
                    string allName = "";
                    StartDownload = false;
                    foreach (Patch patch in downLoadList)
                    {
                        allName += patch.Name + ";";
                    }
                    Debug.LogError("资源重复下载4次MD5校验都失败,请检查资源" + allName);
                    ItemError?.Invoke(allName);
                }
                else
                {
                    m_TryDownCount++;
                    m_DownLoadMD5Dic.Clear();
                    foreach (Patch patch in downLoadList)
                    {
                        m_DownLoadMD5Dic.Add(patch.Name, patch.Md5);
                    }
                    //自动重新下载校验失败的文件
                    m_Mono.StartCoroutine(StartDownLoadAB(callBack, downLoadList));
                }
            }
        }
Example #2
0
        /// <summary>
        /// 开始下载AB包
        /// </summary>
        /// <param name="callBack"></param>
        /// <returns></returns>
        public IEnumerator StartDownLoadAB(Action callBack, List <Patch> allPatch = null)
        {
            m_AlreadyDownList.Clear();
            StartDownload = true;
            if (allPatch == null)
            {
                allPatch = m_DownLoadList;
            }
            if (!Directory.Exists(m_DownLoadPath))
            {
                Directory.CreateDirectory(m_DownLoadPath);
            }

            List <DownLoadAssetBundle> downLoadAssetBundles = new List <DownLoadAssetBundle>();

            foreach (Patch patch in allPatch)
            {
                downLoadAssetBundles.Add(new DownLoadAssetBundle(patch.Url, m_DownLoadPath));
            }

            foreach (DownLoadAssetBundle downLoad in downLoadAssetBundles)
            {
                m_CurDownload = downLoad;
                yield return(m_Mono.StartCoroutine(downLoad.Download()));

                Patch patch = FindPatchByGamePath(downLoad.FileName);
                if (patch != null)
                {
                    m_AlreadyDownList.Add(patch);
                }
                downLoad.Destory();
            }

            //MD5码校验,如果校验没通过,自动重新下载没通过的文件,重复下载计数,达到一定次数后,反馈某某文件下载失败

            //存储MD5码错误的补丁文件,这些要重新下载
            List <Patch> downLoadList = new List <Patch>();

            foreach (DownLoadAssetBundle downLoad in downLoadAssetBundles)
            {
                string md5 = "";
                if (m_DownLoadMD5Dic.TryGetValue(downLoad.FileName, out md5))
                {
                    Debug.Log("下载的文件的MD5:" + MD5Manager.Instance.BuildFileMd5(downLoad.SaveFilePath));
                    Debug.Log("服务器上该文件的MD5:" + md5);
                    if (MD5Manager.Instance.BuildFileMd5(downLoad.SaveFilePath) != md5)
                    {
                        Debug.Log(string.Format("此文件{0}MD5校验失败,即将重新下载", downLoad.FileName));
                        Patch patch = FindPatchByGamePath(downLoad.FileName);
                        if (patch != null)
                        {
                            downLoadList.Add(patch);
                        }
                    }
                }
            }

            //全部文件都是正确的
            if (downLoadList.Count <= 0)
            {
                m_DownLoadMD5Dic.Clear();
                if (callBack != null)
                {
                    yield return(wait);

                    StartDownload = false;
                    callBack();
                }
                if (LoadOver != null)
                {
                    LoadOver();
                }
            }
            else
            {
                if (m_TryDownCount >= DOWNLOADCOUNT)
                {
                    string allName = "";
                    StartDownload = false;
                    foreach (Patch patch in downLoadList)
                    {
                        allName += patch.Name + ";";
                    }
                    Debug.LogError("资源重复下载4次MD5校验都失败,请检查资源" + allName);
                    ItemError?.Invoke(allName);
                }
                else
                {
                    m_TryDownCount++;
                    m_DownLoadMD5Dic.Clear();
                    foreach (Patch patch in downLoadList)
                    {
                        m_DownLoadMD5Dic.Add(patch.Name, patch.Md5);
                    }
                    //自动重新下载校验失败的文件
                    m_Mono.StartCoroutine(StartDownLoadAB(callBack, downLoadList));
                }
            }
        }