Exemplo n.º 1
0
    public IEnumerator CheckAndUpdateABToPD(AssetBundleSettingHelper ABSettingHelper, AssetBundleHelper abh, Action callback = null)
    {
        StageInfo.ToStage(UpdaterStage.CheckCopySAToPD);
        {
            //先大致写下思路,android下,streamingAssets目录是在jar包里的,所以不能直接操作文件和路径,得www或者assetbundle.loadfromfile去读取,但是其他地方可以直接读,所以为了照顾android,SA目录下都改用www和assetbundle.loadfromfile
            //但是有些地方需要搞复制这种事,所以WWW后可以直接写入到新文件中。所以可以发现很多地方用了www


            //如果使用AB,检查一下自己的私有目录下是否有assetbundle的拷贝,如果没有则把所有的AB拷过去
            //bool bCopySAToPDFinished = false;
            if (!ABSettingHelper.UseAssetBundle || ABSettingHelper.UseSteamingAssetRes)
            {
                StageInfo.ToStage(UpdaterStage.NotNeedUpdater);
                if (callback != null)
                {
                    callback.Invoke();
                }
                yield break;
            }
            var abpiSrc = ABSettingHelper.GetCurABVersionXmlStreamingAssetPath();
            var abpiDst = ABSettingHelper.GetCurABVersionXmlPersistentDataPath();
            //首先检查目标文件是否存在
            if (!File.Exists(abpiDst.FullName))
            {
                //不存在,这个时候无脑删除文件夹,然后拷贝,防止文件夹里残留了奇怪的东西
                abh.StartCoroutine(CopyAllABToPD(ABSettingHelper, abh, () => StageInfo.ToStage(UpdaterStage.Copy_Over)));

                AssetBundleHelper.TestLog("!File.Exists(abpiDst.FullName):" + abpiDst.FullName);
            }
            else
            {
                //虽然存在,但是对比一下版本号,如果不如SA目录下的新,也是要覆盖过去的
                VersionXmlInfo verSrc = null;
                //AssetBundleHelper.TestLog("abpiSrc.URI:" + abpiSrc.URI);
                using (WWW www = new WWW(abpiSrc.URI))
                {
                    bool bFind = false;
                    while (true)
                    {
                        if (www.error != null)
                        {
                            Debug.LogError(www.error);
                            AssetBundleHelper.TestLog(www.error);
                            bFind = true;
                            break;
                        }
                        else if (www.isDone)
                        {
                            break;
                        }
                        yield return(null);
                    }
                    if (!bFind)
                    {
                        verSrc = GetVersion(www.bytes);
                    }
                }
                if (verSrc != null)
                {
                    //AssetBundleHelper.TestLog("abpiDst.URI:" + abpiDst.URI);
                    VersionXmlInfo verDst = null;

                    verDst = GetVersion(abpiDst.FullName);

                    //AssetBundleHelper.TestLog("verDst.IsUpTodate(verSrc)");
                    if (verDst.IsUpTodate(verSrc))
                    {
                        //目标版本比较新,所以什么都不用干
                        //AssetBundleHelper.TestLog("Is Up To Date");
                        StageInfo.ToStage(UpdaterStage.Copy_Over);
                    }
                    else
                    {
                        //无脑拷过去覆盖
                        //CopyAllABToPD(ABSettingHelper);

                        //AssetBundleHelper.TestLog("Copy all:");
                        abh.StartCoroutine(CopyAllABToPD(ABSettingHelper, abh, () => StageInfo.ToStage(UpdaterStage.Copy_Over)));
                    }
                }
                else
                {
                    StageInfo.ToStage(UpdaterStage.Copy_Over);
                }
            }
            //AssetBundleHelper.TestLog("Wait bCopySAToPDFinished");
            while (!StageInfo.IsStage(UpdaterStage.Copy_Over))
            {
                yield return(null);
            }
        }


        StageInfo.ToStage(UpdaterStage.DownloadRes);
        //然后检查网络上是否有更新的版本
        //尝试从网络上获得version的xml
        string VersionABPrefixUrl = Path.Combine(UrlPrefix, ABSettingHelper.GetPlatformPathName());
        string manifestUrl        = Path.Combine(VersionABPrefixUrl, ABSettingHelper.GetPlatformPathName());
        var    VersionUrl         = Path.Combine(VersionABPrefixUrl, VersionXmlInfo.FileName + AssetBundleSettingHelper.xmlExtName);

        //AssetBundleHelper.TestLog(@"Begin WWW VersionUrl:"+ VersionUrl);
        using (WWW www = new WWW(VersionUrl))
        {
            while (true)
            {
                yield return(null);

                //AssetBundleHelper.TestLog(@"Begin www Time:" + DateTime.Now.ToString());
                if (www.error != null)
                {
                    //Debug.Assert(false, "www.error != null " + www.error);
                    AssetBundleHelper.TestLog(@"www.error != null " + www.error);
                    StageInfo.ToStage(UpdaterStage.DownloadRes_Error);
                    if (callback != null)
                    {
                        callback.Invoke();
                    }

                    yield break;
                }
                else if (www.isDone)
                {
                    break;
                }
            }
//			AssetBundleHelper.TestLog(@"Begin etVersion(www.bytes):");
            var versionOnline = GetVersion(www.bytes);
/*			AssetBundleHelper.TestLog(@"Begin e11111111111:"+ versionOnline.GetVersionString());*/
            var abpiDst = ABSettingHelper.GetCurABVersionXmlPersistentDataPath();
//          AssetBundleHelper.TestLog(@"Begin 222222222222222222:"+ abpiDst.FullName);
//          AssetBundleHelper.TestLog(@"Begin 333333333333:" + Application.persistentDataPath);
            VersionXmlInfo verDst = null;
            try
            {
                verDst = GetVersion(abpiDst.FullName);
            }
            catch (Exception e)
            {
                AssetBundleHelper.TestLog(e.Message);
            }
            AssetBundleHelper.TestLog(@"Begin abpiDst.FullName:" + abpiDst.FullName);
            if (verDst != null && verDst.IsUpTodate(versionOnline))
            {
                AssetBundleHelper.TestLog(@"Up To Date Online:" + abpiDst.FullName);
                StageInfo.ToStage(UpdaterStage.ResIsUpToDate);
                //本地的是最新的,不需要更新
                if (callback != null)
                {
                    callback.Invoke();
                }
                yield break;
            }

            //Debug.Log("需要更新");
            //先下载manifest,然后下载所有需要更新的AB
            AssetBundleHelper.TestLog(@"Begin WWW manifestUrl:" + manifestUrl);
            using (WWW wwwMF = new WWW(manifestUrl))
            {
                while (true)
                {
                    yield return(null);

                    if (wwwMF.error != null)
                    {
                        AssetBundleHelper.TestLog(@"wwwMF.error != null " + wwwMF.error);
                        StageInfo.ToStage(UpdaterStage.DownloadRes_Error);
                        if (callback != null)
                        {
                            callback.Invoke();
                        }
                        yield break;;
                    }
                    else if (wwwMF.isDone)
                    {
                        break;
                    }
                }

                var           newManifest       = GetPathManifest(wwwMF.bytes);
                var           localManifestABPI = ABSettingHelper.GetCurPlatformManifestPath();
                var           localManifest     = GetPathManifest(localManifestABPI.FullName);
                List <string> updateList        = null;
                List <string> deleteList        = null;
                GetNeedUpdateList(localManifest, newManifest, out updateList, out deleteList);
                int needUpdateCount = 0;
                if (updateList != null)
                {
                    needUpdateCount += updateList.Count();
                }
                if (deleteList != null)
                {
                    needUpdateCount += deleteList.Count();
                }
                {
                    StageInfo.SetNUC(needUpdateCount);
                    DeleteFile(deleteList, ABSettingHelper);

                    if (updateList != null && updateList.Count > 0)
                    {
                        foreach (var f in updateList)
                        {
                            string dlUrl = Path.Combine(VersionABPrefixUrl, f);
                            using (WWW wwwUPDATE = new WWW(dlUrl))
                            {
                                while (true)
                                {
                                    yield return(null);

                                    if (wwwUPDATE.error != null)
                                    {
                                        AssetBundleHelper.TestLog(@"wwwUPDATE.error != null " + wwwUPDATE.error);
                                        StageInfo.ToStage(UpdaterStage.DownloadRes_Error);
                                        if (callback != null)
                                        {
                                            callback.Invoke();
                                        }
                                        yield break;;
                                    }
                                    else if (wwwUPDATE.isDone)
                                    {
                                        break;
                                    }
                                }
                                var abpi = ABSettingHelper.GetCurPlatformABPath(f);
                                UpdateFile(abpi, wwwUPDATE.bytes, ABSettingHelper);

                                if (wwwUPDATE.assetBundle != null)
                                {
                                    wwwUPDATE.assetBundle.Unload(false);
                                }
                            }
                            StageInfo.AddUC();
                            yield return(null);
                        }
                    }
                }
                //保存manifest
                UpdateFile(localManifestABPI, wwwMF.bytes, ABSettingHelper);
                if (wwwMF.assetBundle != null)
                {
                    wwwMF.assetBundle.Unload(false);
                }
                StageInfo.AddUC();
                yield return(null);
            }
            //最后需要保存version。xml
            UpdateFile(abpiDst, www.bytes, ABSettingHelper);
            if (www.assetBundle != null)
            {
                www.assetBundle.Unload(false);
            }

            StageInfo.AddUC();
            yield return(null);
        }

        StageInfo.ToStage(UpdaterStage.Download_Over);
        if (callback != null)
        {
            callback.Invoke();
        }
    }