IEnumerator OnCheckInitialFile() { //创建资源根目录 if (!Directory.Exists(PathResolver.PATH)) { Directory.CreateDirectory(PathResolver.PATH); } //判断主文件是否存在,不存在则拷贝本地备份 var initialCopy = false; var resManifestFile = PathResolver.GetFileFullName(PathResolver.RESOURCES_MANIFEST_FILE_NAME); if (!File.Exists(resManifestFile)) { initialCopy = true; } else { var fullName = PathResolver.GetFileFullName(PathResolver.RESOURCES_MANIFEST_FILE_NAME); var initialFullName = PathResolver.GetInitialFileFullName(PathResolver.RESOURCES_MANIFEST_FILE_NAME); var cacheFullName = PathResolver.GetCacheFileFullName(PathResolver.RESOURCES_MANIFEST_FILE_NAME); yield return(FileHelper.CopyStreamingAssetsToFile(initialFullName, cacheFullName)); //判断包内初始目录是否完整 var initialFile = PathResolver.LoadResourceManifestByPath(initialFullName); if (null == initialFile) { Error(ErrorCode.LoadResourcesMainFestFailed, "Initial path don't contains" + PathResolver.RESOURCES_MANIFEST_FILE_NAME + "!"); yield break; } //全局路径是否需要拷贝 var current = PathResolver.LoadResourceManifestByPath(fullName); if (null == current) { initialCopy = true; } else if (current.data.version < initialFile.data.version) { initialCopy = true; } //删除缓存文件 if (File.Exists(cacheFullName)) { File.Delete(cacheFullName); } } if (initialCopy) { yield return(CopyAllInitialFiles()); } }
/// <summary> /// 拷贝缓存数据 /// </summary> IEnumerator StartCopyCacheFile() { if (errorCode != ErrorCode.None) { yield break; } for (int i = 0; i < PathResolver.MAIN_CONFIG_NAME_ARRAY.Length; ++i) { var file = PathResolver.MAIN_CONFIG_NAME_ARRAY[i]; var res = PathResolver.GetCacheFileFullName(file); var dest = PathResolver.GetFileFullName(file); UpdateCompleteValue(i, PathResolver.MAIN_CONFIG_NAME_ARRAY.Length); yield return(FileHelper.CopyStreamingAssetsToFile(res, dest)); } }
/// <summary> /// 写入下载缓存信息用于断点续传 /// </summary> void SaveDownloadCacheData() { if (currentState < State.UpdateAssetBundle) { return; } if (!Directory.Exists(PathResolver.CACHE_PATH)) { return; } var manifestPath = PathResolver.GetCacheFileFullName(PathResolver.MAIN_MANIFEST_FILE_NAME); var manifestFile = PathResolver.LoadMainManifestByPath(manifestPath); if (null == manifestFile) { return; } //加载缓存信息文件 var cache = new DownloadCache(); cache.Load(PathResolver.DOWNLOADCACHE_FILE_PATH); //获取下载器完成任务列表 if (m_Downloader != null && m_Downloader.completeDownloads != null && m_Downloader.completeDownloads.Count > 0) { foreach (var name in m_Downloader.completeDownloads) { var hashCode = manifestFile.GetAssetBundleHash(name); if (hashCode.isValid && !cache.data.assetBundles.ContainsKey(name)) { var elem = new DownloadCacheData.AssetBundle() { assetbundleName = name, hash = hashCode.ToString() }; Debug.Log(cache.data.assetBundles.Count + " - Cache Add:" + name); cache.data.assetBundles.Add(name, elem); } } } if (!cache.IsEmpty()) { cache.Save(PathResolver.DOWNLOADCACHE_FILE_PATH); } }
/// <summary> /// 更新AB资源 /// </summary> /// <returns></returns> IEnumerator StartUpdateAssetBundle() { if (errorCode != ErrorCode.None) { yield break; } UpdateCompleteValue(0f, 0f); //载入ResourceManifest var oldResManifestFile = OnLoadResourceFile(); var path = PathResolver.GetCacheFileFullName(PathResolver.RESOURCES_MANIFEST_FILE_NAME); var newResManifestFile = PathResolver.LoadResourceManifestByPath(path); if (null == newResManifestFile) { Error(ErrorCode.loadNewResourcesManiFestFailed, "Can't Load new version ResourceManifest"); yield break; } //载入MainManifest var oldManifest = PathResolver.LoadMainManifest(); var newManifest = PathResolver.LoadCacheMainManifest(); if (null == newManifest) { Error(ErrorCode.LoadNewMainManifestFailed, "can't load new version MainManifest"); yield break; } var downFiles = new List <string>(); var deleteFiles = new List <string>(); CompareAsset(ref downFiles, ref deleteFiles, oldManifest, newManifest, oldResManifestFile, newResManifestFile); //删除废气文件 if (deleteFiles.Count > 0) { for (int i = 0; i < deleteFiles.Count; ++i) { var fullName = PathResolver.GetFileFullName(deleteFiles[i]); if (File.Exists(fullName)) { File.Delete(fullName); yield return(0); } } } //更新下载资源 m_Downloader = new AssetBundleDownloader(m_CurUrl); m_Downloader.Start(PathResolver.PATH, downFiles, newResManifestFile); while (!m_Downloader.isDone) { UpdateCompleteValue(m_Downloader.completedSize, m_Downloader.totalSize); yield return(0); } if (m_Downloader.isFailed) { Error(ErrorCode.DonwloadAssetBundleFailed); yield break; } }