예제 #1
0
        internal override void Update()
        {
            if (_steps == ESteps.None || _steps == ESteps.Done)
            {
                return;
            }

            // 1. 准备工作
            if (_steps == ESteps.Prepare)
            {
                if (_bundleInfo.LoadMode == BundleInfo.ELoadMode.None)
                {
                    _steps = ESteps.Done;
                    Status = EOperationStatus.Failed;
                    Error  = $"Bundle info is invalid : {_bundleInfo.BundleName}";
                }
                else if (_bundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromRemote)
                {
                    _steps = ESteps.DownloadFromWeb;
                }
                else if (_bundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming)
                {
                    _steps = ESteps.DownloadFromApk;
                }
                else if (_bundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromCache)
                {
                    _steps = ESteps.CheckAndCopyFile;
                }
                else
                {
                    throw new System.NotImplementedException(_bundleInfo.LoadMode.ToString());
                }
            }

            // 2. 从服务器下载
            if (_steps == ESteps.DownloadFromWeb)
            {
                int failedTryAgain = int.MaxValue;
                _downloader = DownloadSystem.BeginDownload(_bundleInfo, failedTryAgain);
                _steps      = ESteps.CheckDownloadFromWeb;
            }

            // 3. 检测服务器下载结果
            if (_steps == ESteps.CheckDownloadFromWeb)
            {
                Progress = _downloader.DownloadProgress;
                if (_downloader.IsDone() == false)
                {
                    return;
                }

                if (_downloader.HasError())
                {
                    _steps = ESteps.Done;
                    Status = EOperationStatus.Failed;
                    Error  = _downloader.GetLastError();
                }
                else
                {
                    _steps = ESteps.CheckAndCopyFile;
                }
            }

            // 4. 从APK拷贝文件
            if (_steps == ESteps.DownloadFromApk)
            {
                string downloadURL = PathHelper.ConvertToWWWPath(_bundleInfo.GetStreamingLoadPath());
                _fileRequester = new UnityWebFileRequester();
                _fileRequester.SendRequest(downloadURL, GetCachePath());
                _steps = ESteps.CheckDownloadFromApk;
            }

            // 5. 检测APK拷贝文件结果
            if (_steps == ESteps.CheckDownloadFromApk)
            {
                Progress = _fileRequester.Progress();
                if (_fileRequester.IsDone() == false)
                {
                    return;
                }

                if (_fileRequester.HasError())
                {
                    _steps = ESteps.Done;
                    Status = EOperationStatus.Failed;
                    Error  = _fileRequester.GetError();
                }
                else
                {
                    _steps = ESteps.CheckAndCopyFile;
                }
                _fileRequester.Dispose();
            }

            // 6. 检测并拷贝原生文件
            if (_steps == ESteps.CheckAndCopyFile)
            {
                // 如果不需要保存文件
                if (string.IsNullOrEmpty(CopyPath))
                {
                    _steps = ESteps.Done;
                    Status = EOperationStatus.Succeed;
                    return;
                }

                // 如果原生文件已经存在,则验证其完整性
                if (File.Exists(CopyPath))
                {
                    bool result = DownloadSystem.CheckContentIntegrity(CopyPath, _bundleInfo.SizeBytes, _bundleInfo.CRC);
                    if (result)
                    {
                        _steps = ESteps.Done;
                        Status = EOperationStatus.Succeed;
                        return;
                    }
                    else
                    {
                        File.Delete(CopyPath);
                    }
                }

                try
                {
                    FileUtility.CreateFileDirectory(CopyPath);
                    File.Copy(GetCachePath(), CopyPath, true);
                    _steps = ESteps.Done;
                    Status = EOperationStatus.Succeed;
                }
                catch (System.Exception e)
                {
                    _steps = ESteps.Done;
                    Status = EOperationStatus.Failed;
                    Error  = e.ToString();
                }
            }
        }
예제 #2
0
        /// <summary>
        /// 轮询更新
        /// </summary>
        public override void Update()
        {
            if (_steps == ESteps.Done)
            {
                return;
            }

            if (_steps == ESteps.None)
            {
                if (MainBundleInfo.IsInvalid)
                {
                    _steps    = ESteps.Done;
                    Status    = EStatus.Failed;
                    LastError = $"The bundle info is invalid : {MainBundleInfo.BundleName}";
                    YooLogger.Error(LastError);
                    return;
                }

                if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromRemote)
                {
                    _steps        = ESteps.Download;
                    _fileLoadPath = MainBundleInfo.GetCacheLoadPath();
                }
                else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming)
                {
                    _steps        = ESteps.LoadFile;
                    _fileLoadPath = MainBundleInfo.GetStreamingLoadPath();
                }
                else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromCache)
                {
                    _steps        = ESteps.LoadFile;
                    _fileLoadPath = MainBundleInfo.GetCacheLoadPath();
                }
                else
                {
                    throw new System.NotImplementedException(MainBundleInfo.LoadMode.ToString());
                }
            }

            // 1. 从服务器下载
            if (_steps == ESteps.Download)
            {
                int failedTryAgain = int.MaxValue;
                _downloader = DownloadSystem.BeginDownload(MainBundleInfo, failedTryAgain);
                _steps      = ESteps.CheckDownload;
            }

            // 2. 检测服务器下载结果
            if (_steps == ESteps.CheckDownload)
            {
                if (_downloader.IsDone() == false)
                {
                    return;
                }

                if (_downloader.HasError())
                {
                    _steps    = ESteps.Done;
                    Status    = EStatus.Failed;
                    LastError = _downloader.GetLastError();
                }
                else
                {
                    _steps = ESteps.LoadFile;
                }
            }

            // 3. 加载AssetBundle
            if (_steps == ESteps.LoadFile)
            {
#if UNITY_EDITOR
                // 注意:Unity2017.4编辑器模式下,如果AssetBundle文件不存在会导致编辑器崩溃,这里做了预判。
                if (System.IO.File.Exists(_fileLoadPath) == false)
                {
                    _steps    = ESteps.Done;
                    Status    = EStatus.Failed;
                    LastError = $"Not found assetBundle file : {_fileLoadPath}";
                    YooLogger.Error(LastError);
                    return;
                }
#endif

                // Load assetBundle file
                if (MainBundleInfo.IsEncrypted)
                {
                    if (AssetSystem.DecryptionServices == null)
                    {
                        throw new Exception($"{nameof(AssetBundleFileLoader)} need {nameof(IDecryptionServices)} : {MainBundleInfo.BundleName}");
                    }

                    ulong offset = AssetSystem.DecryptionServices.GetFileOffset();
                    if (_isWaitForAsyncComplete)
                    {
                        CacheBundle = AssetBundle.LoadFromFile(_fileLoadPath, 0, offset);
                    }
                    else
                    {
                        _cacheRequest = AssetBundle.LoadFromFileAsync(_fileLoadPath, 0, offset);
                    }
                }
                else
                {
                    if (_isWaitForAsyncComplete)
                    {
                        CacheBundle = AssetBundle.LoadFromFile(_fileLoadPath);
                    }
                    else
                    {
                        _cacheRequest = AssetBundle.LoadFromFileAsync(_fileLoadPath);
                    }
                }
                _steps = ESteps.CheckFile;
            }

            // 4. 检测AssetBundle加载结果
            if (_steps == ESteps.CheckFile)
            {
                if (_cacheRequest != null)
                {
                    if (_isWaitForAsyncComplete)
                    {
                        // 强制挂起主线程(注意:该操作会很耗时)
                        YooLogger.Warning("Suspend the main thread to load unity bundle.");
                        CacheBundle = _cacheRequest.assetBundle;
                    }
                    else
                    {
                        if (_cacheRequest.isDone == false)
                        {
                            return;
                        }
                        CacheBundle = _cacheRequest.assetBundle;
                    }
                }

                // Check error
                if (CacheBundle == null)
                {
                    _steps    = ESteps.Done;
                    Status    = EStatus.Failed;
                    LastError = $"Failed to load assetBundle : {MainBundleInfo.BundleName}";
                    YooLogger.Error(LastError);
                }
                else
                {
                    _steps = ESteps.Done;
                    Status = EStatus.Succeed;
                }
            }
        }