/// <summary> /// 下载包资源 /// </summary> IEnumerator StartDownloadPack() { if (ErrorCode != emErrorCode.None) { yield break; } UpdateCompleteValue(0f, 0f); if (packages_name_ == null) { Error(emErrorCode.InvalidPackageName); yield break; } //收集所有需要下载的AssetBundle List <string> ab_list = new List <string>(); for (int i = 0; i < packages_name_.Count; ++i) { string pack_name = packages_name_[i]; List <string> list = AssetBundleManager.Instance.FindAllAssetBundleFilesNameByPackage(pack_name); ab_list.AddRange(list); } if (ab_list == null) { Error(emErrorCode.NotFindAssetBundle); yield break; } //过滤已下载的资源 ab_list.RemoveAll((assetbundle_name) => { return(File.Exists(Common.GetFileFullName(assetbundle_name))); }); if (ab_list.Count == 0) { yield break; } //载入资源信息描述文件 ResourcesManifest resources_manifest = AssetBundleManager.Instance.ResourcesManifest; //开始下载 ab_download_ = new AssetBundleDownloader(current_url_); ab_download_.Start(Common.PATH, ab_list, resources_manifest); while (!ab_download_.IsDone) { UpdateCompleteValue(ab_download_.CompletedSize, ab_download_.TotalSize); yield return(null); } if (ab_download_.IsFailed) { Error(emErrorCode.DownloadAssetBundleFailed); yield break; } ab_download_ = null; yield return(null); }
/// <summary> /// 从安装目录拷贝文件到本地目录 /// </summary> IEnumerator StartCopyInitialFile(string local_name, System.Action <AssetBundleBatchCopy> callback) { if (copy_list_.Contains(local_name)) { yield break; } copy_list_.Add(local_name); StreamingAssetsCopy copy = new StreamingAssetsCopy(); yield return(copy.Copy(Common.GetInitialFileFullName(local_name), Common.GetFileFullName(local_name))); copy_list_.Remove(local_name); ++progress; if (callback != null) { callback(this); } if (copy.resultCode != emIOOperateCode.Succeed) { SetResult(true, copy.resultCode); } }
/// <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> /// 判断一个AssetBundle是否存在缓存 /// </summary> public bool IsExist(string assetbundlename) { if (!IsReady) { return(false); } if (string.IsNullOrEmpty(assetbundlename)) { return(false); } return(File.Exists(Common.GetFileFullName(assetbundlename))); }
/// <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> /// 拷贝文件并覆盖旧数据文件 /// </summary> IEnumerator UpdatingCopyCacheFile() { if (ErrorCode != emErrorCode.None) { yield break; } //从缓存中剪切主配置文件覆盖旧文件 for (int i = 0; i < Common.CONFIG_NAME_ARRAY.Length; ++i) { UpdateCompleteValue(i, Common.CONFIG_NAME_ARRAY.Length); try { var file = Common.CONFIG_NAME_ARRAY[i]; var src = Common.GetUpdaterCacheFileFullName(file); var dest = Common.GetFileFullName(file); if (File.Exists(dest)) { File.Delete(dest); } File.Move(src, dest); } catch (System.Exception ex) { if (Common.CONFIG_REQUIRE_CONDITION_ARRAY[i]) { var message = Common.CONFIG_NAME_ARRAY[i] + ", " + ex.Message; ErrorWriteFile(emIOOperateCode.Fail, message); break; } } } // 拷贝失败则需要把本地配置文件删除 // (由于部分配置文件拷贝失败,会导致本地的配置文件不匹配会引起版本信息错误, 统一全部删除则下次进入游戏会重新从安装包拷贝全部数据) if (IsFailed) { for (int i = 0; i < Common.CONFIG_NAME_ARRAY.Length; ++i) { var fileFullName = Common.GetFileFullName(Common.CONFIG_NAME_ARRAY[i]); if (File.Exists(fileFullName)) { File.Delete(fileFullName); } } } }
void CutCacheFileToNative(string file) { try { var src = Common.GetUpdaterCacheFileFullName(file); var dest = Common.GetFileFullName(file); if (!File.Exists(src)) { return; } if (File.Exists(dest)) { File.Delete(dest); } File.Move(src, dest); } catch (System.Exception ex) { ErrorWriteFile(emIOOperateCode.Fail, ex.Message); } }
/// <summary> /// 拷贝初始目录所有文件 /// </summary> IEnumerator CopyAllInitialFiles() { //拷贝所有配置文件 for (int i = 0; i < Common.MAIN_CONFIG_NAME_ARRAY.Length; ++i) { yield return(Common.StartCopyInitialFile(Common.MAIN_CONFIG_NAME_ARRAY[i])); } //拷贝AssetBundle文件 ResourcesManifest resources_manifest = Common.LoadResourcesManifest(); if (resources_manifest == null) { Debug.LogWarning("Can't load ResourcesManifest file!"); yield break; } var itr = resources_manifest.Data.AssetBundles.GetEnumerator(); while (itr.MoveNext()) { if (itr.Current.Value.IsNative) { string assetbundlename = itr.Current.Value.AssetBundleName; string dest = Common.GetFileFullName(assetbundlename); //保证路径存在 string directory = Path.GetDirectoryName(dest); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } //拷贝数据 yield return(Common.StartCopyInitialFile(assetbundlename)); } } itr.Dispose(); }
/// <summary> /// 比较本地数据,获得需要修复的资源文件列表 /// </summary> public static void CompareAndCalcRecoverFiles(ref List <string> recover_files , ResourcesManifest resourcesmanifest) { if (resourcesmanifest == null) { return; } var itr = resourcesmanifest.Data.AssetBundles.GetEnumerator(); while (itr.MoveNext()) { if (itr.Current.Value.IsNative) { string name = itr.Current.Value.AssetBundleName; string full_name = Common.GetFileFullName(name); if (!File.Exists(full_name)) { recover_files.Add(name); } } } }
/// <summary> /// 载入ResourcesPack /// </summary> public static ResourcesPackages LoadResourcesPackages() { string file = Common.GetFileFullName(Common.RESOURCES_PACKAGE_FILE_NAME); return(LoadResourcesPackagesByPath(file)); }
/// <summary> /// 载入ResourcesMnifest /// </summary> public static ResourcesManifest LoadResourcesManifest() { string file = Common.GetFileFullName(Common.RESOURCES_MANIFEST_FILE_NAME); return(LoadResourcesManifestByPath(file)); }
/// <summary> /// 载入Manifest /// </summary> public static AssetBundleManifest LoadMainManifest() { string file = Common.GetFileFullName(Common.MAIN_MANIFEST_FILE_NAME); return(LoadMainManifestByPath(file)); }
/// <summary> /// 比较AssetBundle差异,获得下载列表与删除列表 /// </summary> static void CompareAssetBundleDifference(ref List <string> download_files, ref List <string> delete_files, AssetBundleManifest old_manifest, AssetBundleManifest new_manifest, ResourcesManifest old_resourcesmanifest, ResourcesManifest new_resourcesmanifest) { if (download_files != null) { download_files.Clear(); } if (delete_files != null) { delete_files.Clear(); } if (old_manifest == null) { return; } if (new_manifest == null) { return; } if (new_resourcesmanifest == null) { return; } //采用位标记的方式判断资源 //位标记: 0: 存在旧资源中 1: 存在新资源中 2:本地资源标记 int old_version_bit = 0x1; // 存在旧资源中 int new_version_bit = 0x2; // 存在新资源中 int old_version_native_bit = 0x4; // 旧的本地资源 int new_version_native_bit = 0x8; // 新的本地资源 Dictionary <string, int> temp_dic = new Dictionary <string, int>(); //标记旧资源 string[] all_assetbundle = old_manifest.GetAllAssetBundles(); for (int i = 0; i < all_assetbundle.Length; ++i) { string name = all_assetbundle[i]; _SetDictionaryBit(ref temp_dic, name, old_version_bit); } //标记新资源 string[] new_all_assetbundle = new_manifest.GetAllAssetBundles(); for (int i = 0; i < new_all_assetbundle.Length; ++i) { string name = new_all_assetbundle[i]; _SetDictionaryBit(ref temp_dic, name, new_version_bit); } //标记旧的本地资源 if (old_resourcesmanifest.Data != null && old_resourcesmanifest.Data.AssetBundles != null) { var resource_manifest_itr = old_resourcesmanifest.Data.AssetBundles.GetEnumerator(); while (resource_manifest_itr.MoveNext()) { if (resource_manifest_itr.Current.Value.IsNative) { string name = resource_manifest_itr.Current.Value.AssetBundleName; _SetDictionaryBit(ref temp_dic, name, old_version_native_bit); } } } //标记新的本地资源 if (new_resourcesmanifest.Data != null && new_resourcesmanifest.Data.AssetBundles != null) { var resource_manifest_itr = new_resourcesmanifest.Data.AssetBundles.GetEnumerator(); while (resource_manifest_itr.MoveNext()) { if (resource_manifest_itr.Current.Value.IsNative) { string name = resource_manifest_itr.Current.Value.AssetBundleName; _SetDictionaryBit(ref temp_dic, name, new_version_native_bit); } } } //获得对应需操作的文件名, 优先级: both > add > delete //both: 第0位与第1位都被标记的 //delete: 仅第0位被标记的 //add: 第2位未标记,且第3位被标记的 int both_bit = old_version_bit | new_version_bit; // 二个版本资源都存在 List <string> add_files = new List <string>(); List <string> both_files = new List <string>(); var itr = temp_dic.GetEnumerator(); while (itr.MoveNext()) { string name = itr.Current.Key; int mask = itr.Current.Value; if ((mask & new_version_native_bit) == new_version_native_bit && (mask & old_version_native_bit) == 0) { add_files.Add(name); } else if ((mask & both_bit) == both_bit) { both_files.Add(name); } else if ((mask & old_version_bit) == old_version_bit) { delete_files.Add(name); } } itr.Dispose(); //载入下载缓存数据 DownloadCache download_cache = new DownloadCache(); download_cache.Load(Common.DOWNLOADCACHE_FILE_PATH); if (!download_cache.HasData()) { download_cache = null; } //记录需下载的文件 { //加入新增的文件 download_files.AddRange(add_files); //比较所有同时存在的文件,判断哪些需要更新 for (int i = 0; i < both_files.Count; ++i) { string name = both_files[i]; string full_name = Common.GetFileFullName(name); if (File.Exists(full_name)) { //判断哈希值是否相等 string old_hash = old_manifest.GetAssetBundleHash(name).ToString(); string new_hash = new_manifest.GetAssetBundleHash(name).ToString(); if (old_hash.CompareTo(new_hash) == 0) { continue; } download_files.Add(name); } } //过滤缓存中已下载的文件 if (download_cache != null) { var cache_itr = download_cache.Data.AssetBundles.GetEnumerator(); while (cache_itr.MoveNext()) { DownloadCacheData.AssetBundle elem = cache_itr.Current.Value; string name = elem.AssetBundleName; string full_name = Common.GetFileFullName(name); if (File.Exists(full_name)) { string cache_hash = elem.Hash; string new_hash = new_manifest.GetAssetBundleHash(name).ToString(); if (!string.IsNullOrEmpty(cache_hash) && cache_hash.CompareTo(new_hash) == 0) { download_files.Remove(name); } } } } } }
/// <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; } }
/// <summary> /// 更新AssetBundle /// </summary> IEnumerator UpdatingUpdateAssetBundle() { if (ErrorCode != emErrorCode.None) { yield break; } UpdateCompleteValue(0f, 0f); //载入新的ResourcesManifest ResourcesManifest old_resource_manifest = AssetBundleManager.Instance.ResManifest; string file = Common.UPDATER_CACHE_PATH + "/" + 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.UPDATER_CACHE_PATH + "/" + 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>(); ComparisonUtils.CompareAndCalcDifferenceFiles(ref download_files, ref delete_files , manifest, new_manifest , old_resource_manifest, new_resources_manifest , ComparisonUtils.emCompareMode.All); // 进度控制 float totalProgress = delete_files.Count + download_files.Count; float currentProgress = 0; //载入下载缓存数据, 过滤缓存中已下载的文件 DownloadCache download_cache = new DownloadCache(); download_cache.Load(Common.DOWNLOADCACHE_FILE_PATH); if (!download_cache.HasData()) { download_cache = null; } if (download_cache != null) { var cache_itr = download_cache.Data.AssetBundles.GetEnumerator(); while (cache_itr.MoveNext()) { DownloadCacheData.AssetBundle elem = cache_itr.Current.Value; string name = elem.AssetBundleName; string full_name = Common.GetFileFullName(name); if (File.Exists(full_name)) { string cache_hash = elem.Hash; string new_hash = new_manifest.GetAssetBundleHash(name).ToString(); if (!string.IsNullOrEmpty(cache_hash) && cache_hash.CompareTo(new_hash) == 0) { download_files.Remove(name); ++currentProgress; UpdateCompleteValue(currentProgress, totalProgress); yield return(null); } } } } //删除已废弃的文件 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); ++currentProgress; UpdateCompleteValue(currentProgress, totalProgress); yield return(null); } } } //更新所有需下载的资源 ab_download_ = new AssetBundleDownloader(current_url_); ab_download_.Start(Common.PATH, download_files, new_resources_manifest); while (!ab_download_.IsDone) { UpdateCompleteValue(currentProgress + ab_download_.CompleteDownloadList.Count, totalProgress); yield return(null); } if (ab_download_.IsFailed) { Error(ab_download_.ErrorCode); yield break; } }
/// <summary> /// 比较AssetBundle差异,获得增量更新列表与删除列表 /// </summary> public static void CompareAndCalcDifferenceFiles(ref List <string> download_files , ref List <string> delete_files , AssetBundleManifest old_manifest , AssetBundleManifest new_manifest , ResourcesManifest old_resourcesmanifest , ResourcesManifest new_resourcesmanifest , emCompareMode compareMode) { if (download_files == null) { return; } download_files.Clear(); if (delete_files == null) { return; } delete_files.Clear(); if (old_manifest == null) { return; } if (new_manifest == null) { return; } if (new_resourcesmanifest == null) { return; } //采用位标记的方式判断资源 //位标记: 0: 存在旧资源中 1: 存在新资源中 2:旧的本地资源 3:新的本地资源 int old_version_bit = 0x1; // 存在旧版本中 int new_version_bit = 0x2; // 存在新版本中 int old_version_native_bit = 0x4; // 旧的本地资源 int new_version_native_bit = 0x8; // 新的本地资源 Dictionary <string, int> temp_dic = new Dictionary <string, int>(); //标记旧资源 string[] all_assetbundle = old_manifest.GetAllAssetBundles(); for (int i = 0; i < all_assetbundle.Length; ++i) { string name = all_assetbundle[i]; _SetDictionaryBit(ref temp_dic, name, old_version_bit); } //标记新资源 string[] new_all_assetbundle = new_manifest.GetAllAssetBundles(); for (int i = 0; i < new_all_assetbundle.Length; ++i) { string name = new_all_assetbundle[i]; _SetDictionaryBit(ref temp_dic, name, new_version_bit); } //标记旧的本地资源 if (old_resourcesmanifest.Data != null && old_resourcesmanifest.Data.AssetBundles != null) { var resource_manifest_itr = old_resourcesmanifest.Data.AssetBundles.GetEnumerator(); while (resource_manifest_itr.MoveNext()) { if (resource_manifest_itr.Current.Value.IsNative) { string name = resource_manifest_itr.Current.Value.AssetBundleName; string full_name = Common.GetFileFullName(Common.RESOURCES_MANIFEST_FILE_NAME); if (File.Exists(full_name)) { _SetDictionaryBit(ref temp_dic, name, old_version_native_bit); } } } } //标记新的本地资源 if (new_resourcesmanifest.Data != null && new_resourcesmanifest.Data.AssetBundles != null) { var resource_manifest_itr = new_resourcesmanifest.Data.AssetBundles.GetEnumerator(); while (resource_manifest_itr.MoveNext()) { if (resource_manifest_itr.Current.Value.IsNative) { string name = resource_manifest_itr.Current.Value.AssetBundleName; _SetDictionaryBit(ref temp_dic, name, new_version_native_bit); } } } //获得对应需操作的文件名, 优先级: both > add > delete int both_bit = old_version_bit | new_version_bit; // 二个版本资源都存在 List <string> add_files = new List <string>(); List <string> both_files = new List <string>(); var itr = temp_dic.GetEnumerator(); while (itr.MoveNext()) { string name = itr.Current.Key; int mask = itr.Current.Value; //add: 第2位未标记,且第3位被标记的 if ((mask & new_version_native_bit) == new_version_native_bit && (mask & old_version_native_bit) == 0) { add_files.Add(name); } //both: 第0位与第1位都被标记的 else if ((mask & both_bit) == both_bit) { // 如果为emCompareMode.OnlyInitial比较方式,则需确定是否为本地资源 if (compareMode == emCompareMode.OnlyInitial) { if ((mask & new_version_native_bit) == new_version_native_bit) { both_files.Add(name); } } else { both_files.Add(name); } } //delete: 第0位被标记的 else if ((mask & old_version_bit) == old_version_bit) { // 且必须本地存在 string full_name = Common.GetFileFullName(name); if (File.Exists(full_name)) { delete_files.Add(name); } } } itr.Dispose(); //记录需下载的文件 { //加入新增的文件 download_files.AddRange(add_files); //比较所有同时存在的文件,判断哪些需要更新 for (int i = 0; i < both_files.Count; ++i) { string name = both_files[i]; string full_name = Common.GetFileFullName(name); if (File.Exists(full_name)) { //判断哈希值是否相等 string old_hash = old_manifest.GetAssetBundleHash(name).ToString(); string new_hash = new_manifest.GetAssetBundleHash(name).ToString(); if (old_hash.CompareTo(new_hash) == 0) { continue; } download_files.Add(name); } } } }