/// <summary> /// 初始化 /// </summary> IEnumerator Preprocess() { //创建资源根目录 if (!Directory.Exists(Common.PATH)) { Directory.CreateDirectory(Common.PATH); } //判断主资源文件是否存在,不存在则拷贝备份资源至资源根目录 bool do_initial_copy = false; string resources_manifest_file = Common.GetFileFullName(Common.RESOURCES_MANIFEST_FILE_NAME); if (!File.Exists(resources_manifest_file)) { do_initial_copy = true; } else { // 拷贝安装包初始化目录中的ResourcesManifest,并判断是否重新拷贝初始化目录下的所有文件 string full_name = Common.GetFileFullName(Common.RESOURCES_MANIFEST_FILE_NAME); string initial_full_name = Common.GetInitialFileFullName(Common.RESOURCES_MANIFEST_FILE_NAME); string cache_full_name = Common.GetCacheFileFullName(Common.RESOURCES_MANIFEST_FILE_NAME); yield return(Common.StartCopyFile(initial_full_name, cache_full_name)); //判断安装包初始目录是否完整 ResourcesManifest initial = Common.LoadResourcesManifestByPath(cache_full_name); if (initial == null) { Error(emErrorCode.PreprocessError , "Initial path don't contains " + Common.RESOURCES_MANIFEST_FILE_NAME + "!"); yield break; } ResourcesManifest current = Common.LoadResourcesManifestByPath(full_name); if (current == null) { do_initial_copy = true; } else if (current.Data.Version < initial.Data.Version) { do_initial_copy = true; } //删除缓存中的文件 if (File.Exists(cache_full_name)) { File.Delete(cache_full_name); } } if (do_initial_copy) { yield return(CopyAllInitialFiles()); } PreprocessFinished(); }
/// <summary> /// 写入下载缓存信息,用于断点续传 /// </summary> void SaveDownloadCacheData() { if (CurrentState < emState.UpdateAssetBundle) { return; } if (!Directory.Exists(Common.CACHE_PATH)) { return; } //载入新的Manifest string new_manifest_name = Common.GetCacheFileFullName(Common.MAIN_MANIFEST_FILE_NAME); AssetBundleManifest new_manifest = Common.LoadMainManifestByPath(new_manifest_name); if (new_manifest == null) { return; } //先尝试读取旧的缓存信息,再保存现在已经下载的数据 //PS:由于只有版本完整更新完才会移动Cache目录,且玩家可能多次尝试下载更新,所以必须保留旧的缓存信息 DownloadCache cache = new DownloadCache(); cache.Load(Common.DOWNLOADCACHE_FILE_PATH); if (ab_download_ != null && ab_download_.CompleteDownloads != null && ab_download_.CompleteDownloads.Count > 0) { for (int i = 0; i < ab_download_.CompleteDownloads.Count; ++i) { string assetbundle_name = ab_download_.CompleteDownloads[i]; Hash128 hash_code = new_manifest.GetAssetBundleHash(assetbundle_name); if (hash_code.isValid && !cache.Data.AssetBundles.ContainsKey(assetbundle_name)) { DownloadCacheData.AssetBundle elem = new DownloadCacheData.AssetBundle() { AssetBundleName = assetbundle_name, Hash = hash_code.ToString(), }; Debug.Log(cache.Data.AssetBundles.Count + " - Cache Add:" + assetbundle_name); cache.Data.AssetBundles.Add(assetbundle_name, elem); } } } if (cache.HasData()) { cache.Save(Common.DOWNLOADCACHE_FILE_PATH); } }
/// <summary> /// 拷贝文件并覆盖旧数据文件 /// </summary> IEnumerator StartCopyCacheFile() { if (ErrorCode != emErrorCode.None) { yield break; } //从缓存中拷贝主配置文件覆盖旧文件 for (int i = 0; i < Common.MAIN_CONFIG_NAME_ARRAY.Length; ++i) { string str = Common.GetCacheFileFullName(Common.MAIN_CONFIG_NAME_ARRAY[i]); string dest = Common.GetFileFullName(Common.MAIN_CONFIG_NAME_ARRAY[i]); UpdateCompleteValue(i, Common.MAIN_CONFIG_NAME_ARRAY.Length); yield return(Common.StartCopyFile(str, dest)); } }
/// <summary> /// 更新AssetBundle /// </summary> IEnumerator StartUpdateAssetBundle() { if (ErrorCode != emErrorCode.None) { yield break; } UpdateCompleteValue(0f, 0f); //载入新的ResourcesManifest ResourcesManifest old_resource_manifest = AssetBundleManager.Instance.ResourcesManifest; string file = Common.GetCacheFileFullName(Common.RESOURCES_MANIFEST_FILE_NAME); ResourcesManifest new_resources_manifest = Common.LoadResourcesManifestByPath(file); if (new_resources_manifest == null) { Error(emErrorCode.LoadNewResourcesManiFestFailed, "Can't load new verion ResourcesManifest!"); yield break; } //载入MainManifest AssetBundleManifest manifest = AssetBundleManager.Instance.MainManifest; file = Common.GetCacheFileFullName(Common.MAIN_MANIFEST_FILE_NAME); AssetBundleManifest new_manifest = Common.LoadMainManifestByPath(file); if (new_manifest == null) { Error(emErrorCode.LoadNewMainManifestFailed, "Can't find new version MainManifest!"); yield break; } //获取需下载的资源列表与删除的资源的列表 List <string> download_files = new List <string>(); List <string> delete_files = new List <string>(); CompareAssetBundleDifference(ref download_files, ref delete_files, manifest, new_manifest, old_resource_manifest, new_resources_manifest); //删除已废弃的文件 if (delete_files.Count > 0) { for (int i = 0; i < delete_files.Count; ++i) { string full_name = Common.GetFileFullName(delete_files[i]); if (File.Exists(full_name)) { File.Delete(full_name); yield return(0); } } } //更新所有需下载的资源 ab_download_ = new AssetBundleDownloader(current_url_); ab_download_.Start(Common.PATH, download_files, new_resources_manifest); while (!ab_download_.IsDone) { UpdateCompleteValue(ab_download_.CompletedSize, ab_download_.TotalSize); yield return(0); } if (ab_download_.IsFailed) { Error(emErrorCode.DownloadAssetBundleFailed); yield break; } }