private WebFileRequest CreateDownloader(PatchBundle patchBundle) { // 注意:资源版本号只用于确定下载路径 string mainURL = _patcherMgr.GetPatchDownloadURL(patchBundle.Version, patchBundle.Hash); string fallbackURL = _patcherMgr.GetPatchDownloadFallbackURL(patchBundle.Version, patchBundle.Hash); string savePath = PatchHelper.MakeSandboxCacheFilePath(patchBundle.Hash); FileUtility.CreateFileDirectory(savePath); // 创建下载器 MotionLog.Log($"Beginning to download web file : {patchBundle.BundleName} URL : {mainURL}"); WebFileRequest download = WebFileSystem.GetWebFileRequest(mainURL, fallbackURL, savePath, _failedTryAgain); download.UserData = patchBundle; return(download); }
public override void Update() { base.Update(); // 如果资源文件加载完毕 if (CheckFileLoadDone()) { return; } if (States == ELoaderStates.None) { // 检测加载地址是否为空 if (string.IsNullOrEmpty(BundleInfo.LocalPath)) { States = ELoaderStates.Fail; return; } if (string.IsNullOrEmpty(BundleInfo.RemoteURL)) { States = ELoaderStates.CheckDepends; } else { States = ELoaderStates.Download; } } // 1. 从服务器下载 if (States == ELoaderStates.Download) { int failedTryAgain = 3; _downloader = WebFileSystem.GetWebFileRequest(BundleInfo.RemoteURL, BundleInfo.RemoteFallbackURL, BundleInfo.LocalPath, failedTryAgain); States = ELoaderStates.CheckDownload; } // 2. 检测服务器下载结果 if (States == ELoaderStates.CheckDownload) { if (_downloader.IsDone() == false) { return; } if (_downloader.HasError()) { _downloader.ReportError(); States = ELoaderStates.Fail; } else { // 校验文件完整性 if (AssetSystem.BundleServices.CheckContentIntegrity(BundleInfo.BundleName) == false) { MotionLog.Error($"Check download content integrity is failed : {BundleInfo.BundleName}"); States = ELoaderStates.Fail; } else { States = ELoaderStates.CheckDepends; } } // 释放网络资源下载器 if (_downloader != null) { _downloader.Dispose(); _downloader = null; } } // 3. 检测所有依赖完成状态 if (States == ELoaderStates.CheckDepends) { foreach (var dpLoader in _depends) { if (_isWaitForAsyncComplete) { dpLoader.WaitForAsyncComplete(); } if (dpLoader.CheckFileLoadDone() == false) { return; } } States = ELoaderStates.LoadFile; } // 4. 加载AssetBundle if (States == ELoaderStates.LoadFile) { #if UNITY_EDITOR // 注意:Unity2017.4编辑器模式下,如果AssetBundle文件不存在会导致编辑器崩溃,这里做了预判。 if (System.IO.File.Exists(BundleInfo.LocalPath) == false) { MotionLog.Warning($"Not found assetBundle file : {BundleInfo.LocalPath}"); States = ELoaderStates.Fail; return; } #endif // Load assetBundle file if (BundleInfo.IsEncrypted) { if (AssetSystem.DecryptServices == null) { throw new Exception($"{nameof(AssetBundleLoader)} need IDecryptServices : {BundleInfo.BundleName}"); } EDecryptMethod decryptType = AssetSystem.DecryptServices.DecryptType; if (decryptType == EDecryptMethod.GetDecryptOffset) { ulong offset = AssetSystem.DecryptServices.GetDecryptOffset(BundleInfo); if (_isWaitForAsyncComplete) { CacheBundle = AssetBundle.LoadFromFile(BundleInfo.LocalPath, 0, offset); } else { _cacheRequest = AssetBundle.LoadFromFileAsync(BundleInfo.LocalPath, 0, offset); } } else if (decryptType == EDecryptMethod.GetDecryptBinary) { byte[] binary = AssetSystem.DecryptServices.GetDecryptBinary(BundleInfo); if (_isWaitForAsyncComplete) { CacheBundle = AssetBundle.LoadFromMemory(binary); } else { _cacheRequest = AssetBundle.LoadFromMemoryAsync(binary); } } else { throw new NotImplementedException($"{decryptType}"); } } else { if (_isWaitForAsyncComplete) { CacheBundle = AssetBundle.LoadFromFile(BundleInfo.LocalPath); } else { _cacheRequest = AssetBundle.LoadFromFileAsync(BundleInfo.LocalPath); } } States = ELoaderStates.CheckFile; } // 5. 检测AssetBundle加载结果 if (States == ELoaderStates.CheckFile) { if (_cacheRequest != null) { if (_isWaitForAsyncComplete) { // 强制挂起主线程(注意:该操作会很耗时) CacheBundle = _cacheRequest.assetBundle; } else { if (_cacheRequest.isDone == false) { return; } CacheBundle = _cacheRequest.assetBundle; } } // Check error if (CacheBundle == null) { MotionLog.Warning($"Failed to load assetBundle file : {BundleInfo.BundleName}"); States = ELoaderStates.Fail; } else { States = ELoaderStates.Success; } } }