IEnumerator CopyAllInitialFiles() { //拷贝配置文件 foreach (var file in PathResolver.MAIN_CONFIG_NAME_ARRAY) { yield return(PathResolver.CopyInitialFileFile(file)); } //拷贝AB文件 var resManifest = PathResolver.LoadResourceManifest(); if (null == resManifest) { Error(ErrorCode.PreprocessError, "Can't load ResourcesManifes File"); yield break; } var itor = resManifest.data.assetbundles.GetEnumerator(); while (itor.MoveNext()) { if (itor.Current.Value.isNative) { var assetName = itor.Current.Value.assetBundleName; var dest = PathResolver.GetFileFullName(assetName); var dir = Path.GetDirectoryName(dest); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } //copy yield return(PathResolver.CopyInitialFileFile(assetName)); } } itor.Dispose(); }
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)); } }
void CompareAsset( ref List <string> downFiles, ref List <string> deleteFiles, AssetBundleManifest oldManifest, AssetBundleManifest newManifest, ResourcesManifest oldResManifest, ResourcesManifest newResManifest) { if (downFiles != null) { downFiles.Clear(); } if (deleteFiles != null) { deleteFiles.Clear(); } if (oldManifest == null) { Error(ErrorCode.LoadMainManifestFailed, "Local Manifest no Find"); return; } if (newManifest == null) { Error(ErrorCode.LoadNewMainManifestFailed, "Load New MainManifest no Find"); return; } if (newResManifest == null) { Error(ErrorCode.loadNewResourcesManiFestFailed, "Load New ResourceManifest no Find"); return; } //标记位 var old_ver_bit = 1 << 0; //存在旧资源 var new_ver_bit = 1 << 1; //存在新资源 var old_ver_native_bit = 1 << 2; //存在旧的本地资源 var new_ver_native_bit = 1 << 3; //存在新的本地资源 var tempDic = new Dictionary <string, int>(); //标记旧资源 var allAssetBundle = oldManifest.GetAllAssetBundles(); foreach (var name in allAssetBundle) { SetDictionaryBit(ref tempDic, name, old_ver_bit); } //标记新资源 var allNewAssetBundle = newManifest.GetAllAssetBundles(); foreach (var name in allNewAssetBundle) { SetDictionaryBit(ref tempDic, name, new_ver_bit); } //标记旧本地资源 if (oldResManifest.data != null && oldResManifest.data.assetbundles != null) { var itor = oldResManifest.data.assetbundles.GetEnumerator(); while (itor.MoveNext()) { if (itor.Current.Value.isNative) { var name = itor.Current.Value.assetBundleName; SetDictionaryBit(ref tempDic, name, old_ver_native_bit); } } } //标记新的本地资源 if (newResManifest.data != null && newResManifest.data.assetbundles != null) { var itor = newResManifest.data.assetbundles.GetEnumerator(); while (itor.MoveNext()) { if (itor.Current.Value.isNative) { var name = itor.Current.Value.assetBundleName; SetDictionaryBit(ref tempDic, name, new_ver_native_bit); } } } //优先级:both>add>delete //both: 第0位和第1位标记的 //delete : 第0位标记 //add:第2位未标记,第3位标记的 int both = old_ver_bit | new_ver_bit;//2个版本都存在的资源 var addFiles = new List <string>(); var bothFiles = new List <string>(); using (var itor = tempDic.GetEnumerator()) { while (itor.MoveNext()) { var name = itor.Current.Key; var mask = itor.Current.Value; if ((mask & new_ver_native_bit) == new_ver_native_bit && (mask & old_ver_native_bit) == 0) { addFiles.Add(name); } else if ((mask & both) == both) { bothFiles.Add(name); } else if ((mask & old_ver_bit) == old_ver_bit) { deleteFiles.Add(name); } } itor.Dispose(); } //加载下载缓存数据 var download = new DownloadCache(); download.Load(PathResolver.DOWNLOADCACHE_FILE_PATH); if (download.IsEmpty()) { download = null; } //记录需要下载的文件 { //加入新增文件 downFiles.AddRange(addFiles); //判断同时存在文件的哈希 foreach (var name in bothFiles) { var fullName = PathResolver.GetFileFullName(name); if (File.Exists(fullName)) { //判断哈希是否相同 var oldHash = oldManifest.GetAssetBundleHash(name).ToString(); var newHash = newManifest.GetAssetBundleHash(name).ToString(); if (oldHash.CompareTo(newHash) == 0) { continue; } downFiles.Add(name); } } //过滤已下载的文件 if (null != download) { var itor = download.data.assetBundles.GetEnumerator(); while (itor.MoveNext()) { var elem = itor.Current.Value; var name = elem.assetbundleName; var fullName = PathResolver.GetFileFullName(name); if (File.Exists(fullName)) { var cacheHash = elem.hash; var newHash = newManifest.GetAssetBundleHash(name).ToString(); if (!string.IsNullOrEmpty(cacheHash) && cacheHash.CompareTo(newHash) == 0) { downFiles.Remove(name); } } } } } }
/// <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; } }