public static (AssetBundleTable oldTable, AssetBundleTable newTable) LoadTableData() { if (!Directory.Exists(cacheDir)) { Directory.CreateDirectory(cacheDir); } var oldTable = new AssetBundleTable(); if (File.Exists($"{cacheDir}/{GlobalSettings.TableFileName}")) { var json = File.ReadAllText($"{cacheDir}/{GlobalSettings.TableFileName}"); oldTable = JsonUtility.FromJson <AssetBundleTable>(json); } var records = new List <AssetBundleRecord>(); var newTable = new AssetBundleTable(); // AssetGraphからビルドを行うのでビルドマップを取得する。 var buildMap = UnityEngine.AssetGraph.AssetBundleBuildMap.GetBuildMap(); if (buildMap != null) { var platformName = GlobalSettings.GetPlatformName(); var assetBundleNames = buildMap.GetAllAssetBundleNames().ToList(); assetBundleNames.Add(platformName); // CRCとHashの取得方法 // https://qiita.com/satotin/items/9373ba3c5cc85172350c foreach (var assetBundleName in assetBundleNames) { var path = $"{exportAssetBundleLocation}/{assetBundleName}"; uint crc = 0; Hash128 hash; if (!BuildPipeline.GetCRCForAssetBundle(path, out crc)) { Debug.LogError("CRC Get Failed!!"); } if (!BuildPipeline.GetHashForAssetBundle(path, out hash)) { Debug.LogError("Hash Get Failed!!"); } Debug.Log($"AssetBundle path:{path} crc:{crc}, hash:{hash}"); var rec = new AssetBundleRecord(assetBundleName, crc, hash.ToString()); records.Add(rec); } } var version = oldTable.Version + 1; newTable.SetData(version, records.ToArray()); return(oldTable, newTable); }
private void MergeAssetBundleTable(AssetBundleTable table, Dictionary <string, AssetBundleTable.AssetBundleBundleData> append) { var origin = new Dictionary <string, AssetBundleTable.AssetBundleBundleData>(); foreach (var assetBundleBundleData in table.BundleInfos) { origin.Add( assetBundleBundleData.bundleFileName + AssetBundleTable.AssetBundleBundleData.BundleExtension, assetBundleBundleData); } foreach (var assetBundleBundleData in append) { origin[assetBundleBundleData.Key] = assetBundleBundleData.Value; } var values = origin.Values; Array.Resize(ref table.BundleInfos, values.Count); values.CopyTo(table.BundleInfos, 0); }
void OnTableAssetBundleLoaded(UnityEngine.Object asset) { this.Table = asset as AssetBundleTable; isTableLoading = false; }
void LoadTable(Uri uri, string assetBundleName, Hash128 hash, uint crc = 0) { Debug.Log(Caching.ready); //構造体生成 var cachedAssetBundle = new CachedAssetBundle(assetBundleName, hash); //全てのtableのキャッシュ削除 Caching.ClearAllCachedVersions(assetBundleName); //if (Caching.IsVersionCached(cachedAssetBundle)) //{ // Debug.Log("キャッシュから"); // //キャッシュ存在 // string dataPath = AssetBundlePath(cachedAssetBundle); // Debug.Log(dataPath); // var op = UnityEngine.AssetBundle.LoadFromFileAsync(dataPath); // op.completed += (obj) => // { // // ダウンロード成功 // Debug.Log("ダウンロード成功"); // AssetBundle bundle = op.assetBundle; // var prefab = bundle.LoadAllAssets(); // string text = prefab[0].ToString(); // table = JsonUtility.FromJson<AssetBundleTable>(text); // Debug.Log("バージョン" + table.Version); // int preVersion = PlayerPrefs.GetInt("DOWNLOAD", -1); // if (preVersion != table.Version) // { // Debug.Log("新しいバージョンがあります"); // } // LoadEnd(LOAD.TABLE); // }; //} //else //{ Debug.Log("サーバーから"); var request = UnityWebRequestAssetBundle.GetAssetBundle(uri, cachedAssetBundle, crc); var op = request.SendWebRequest(); op.completed += (obj) => { if (op.webRequest.isHttpError || op.webRequest.isNetworkError) { Debug.Log($"ダウンロードに失敗しました!! error:{op.webRequest.error}"); LoadFailed(); } else { // ダウンロード成功 Debug.Log("ダウンロード成功"); var bundle = DownloadHandlerAssetBundle.GetContent(request); var prefab = bundle.LoadAllAssets(); string text = prefab[0].ToString(); table = JsonUtility.FromJson <AssetBundleTable>(text); Debug.Log("バージョン" + table.Version); int preVersion = PlayerPrefs.GetInt("VERSION", -1); if (preVersion != table.Version) { Debug.Log("新しいバージョンがあります"); newDataObj.SetActive(true); } else { Debug.Log("同じバージョン"); downLoadObj.SetActive(true); LoadEnd(LOAD.TABLE); } } }; //} }
static void CreateTableByAssetEntryRecords( AssetBundleEncryptor encryptor, AssetBundleManifest manifest, string tableFilePath, BuildTarget buildTarget, IEnumerable <AssetEntryRecord> assetEntryRecords) { string planeAssetBundleDirPath = encryptor.PlaneAssetBundleDirPath; HashSet <string> checkedAssetBundleNames = new HashSet <string>( assetEntryRecords .Select(assetEntryRecord => assetEntryRecord.AssetBundleName) .Distinct()); Queue <string> checkingAssetBundleNameQueue = new Queue <string>(checkedAssetBundleNames); while (!checkingAssetBundleNameQueue.IsEmpty()) { string assetBundleName = checkingAssetBundleNameQueue.Dequeue(); foreach (string dependency in manifest.GetDirectDependencies(assetBundleName)) { if (checkedAssetBundleNames.Contains(dependency)) { continue; } checkedAssetBundleNames.Add(dependency); checkingAssetBundleNameQueue.Enqueue(dependency); } } IEnumerable <AssetBundleRecord> assetBundleRecords = checkedAssetBundleNames .Select(assetBundleName => { string crcFilePath = GetCrcFilePath(planeAssetBundleDirPath, assetBundleName); uint crc = 0; var isSucceeded = BuildPipeline.GetCRCForAssetBundle(crcFilePath, out crc); if (!isSucceeded) { Debug.LogError($"cannot get crc: AssetBundelName = {assetBundleName}"); } string encryptedAssetBundleFilePath = encryptor.Encrypt(assetBundleName, true); //string encryptedAssetBundleFilePath = encryptor.Encrypt(assetBundleName, false); System.IO.FileInfo fileInfo = new System.IO.FileInfo(encryptedAssetBundleFilePath); long fileSizeBytes = fileInfo.Length; return(new AssetBundleRecord( assetBundleName, manifest.GetDirectDependencies(assetBundleName), crc, fileSizeBytes)); }); DateTime current = DateTime.Now; long version = long.Parse(current.ToString("yyyyMMddHHmmss")); string platform = buildTarget.ToString(); var table = AssetBundleTable.Create(version, platform, assetBundleRecords, assetEntryRecords); string tableDirName = Path.GetDirectoryName(tableFilePath); if (!Directory.Exists(tableDirName)) { Directory.CreateDirectory(tableDirName); } //var json = JsonUtility.ToJson(table); //using (StreamWriter writer = new StreamWriter(outputPath + ".json")) //{ // writer.Write(json); //} AssetDatabase.CreateAsset(table, tableFilePath); }
private IEnumerator RequestVersions() { OnMessage("正在获取版本信息..."); if (Application.internetReachability == NetworkReachability.NotReachable) { var mb = MessageBox.Show("提示", "请检查网络连接状态", "重试", "退出"); yield return(mb); if (mb.isOk) { StartUpdate(); } else { Quit(); } yield break; } var versionFilePath = _tempDownloadPath + VersionInfoFileName; var singleFileDownloadRequest = new SingleFileDownloadRequest(); singleFileDownloadRequest.Reset(GetDownloadURL(VersionInfoFileName), versionFilePath); yield return(DownloadSingleFile(singleFileDownloadRequest)); if (!singleFileDownloadRequest.success) { yield break; } VersionInfo versionInfo; try { var versionFileStr = File.ReadAllText(versionFilePath); versionInfo = versionFileStr.FromXML <VersionInfo>(); } catch (Exception e) { CommonLog.Error(e.Message); MessageBox.Show("提示", "版本文件加载失败", "重试", "退出").onComplete += delegate(MessageBox.EventId id) { if (id == MessageBox.EventId.Ok) { StartUpdate(); } else { Quit(); } }; yield break; } var state = versionInfo.CheckUpdateState(AssetBundleManager.Instance.GetVersion()); _currentTargetVersion = versionInfo; var versionStr = versionInfo.DumpVersion(); CommonLog.Log(MAuthor.WY, $"check version {versionStr} result {state}"); if (state == VersionInfo.State.NeedUpdate) { //下载xmf var xmfFileName = AssetBundlePathResolver.BundleSaveDirName + FileMapGroupInfo.FileExtension; var xmfTargetPath = _tempDownloadPath + xmfFileName; singleFileDownloadRequest.Reset(GetDownloadURL(xmfFileName, versionStr), xmfTargetPath, new MD5Creater.MD5Struct { MD51 = versionInfo.AssetBundlesCacheXmfMd51, MD52 = versionInfo.AssetBundlesCacheXmfMd52 }); yield return(DownloadSingleFile(singleFileDownloadRequest)); if (!singleFileDownloadRequest.success) { yield break; } //下载AssetBundleXMLData.xml var xmlPath = _tempDownloadPath + AssetBundlePathResolver.DependFileName; singleFileDownloadRequest.Reset( GetDownloadURL(AssetBundlePathResolver.DependFileName, versionStr), xmlPath, new MD5Creater.MD5Struct { MD51 = versionInfo.AssetBundleXmlMd51, MD52 = versionInfo.AssetBundleXmlMd52 }); yield return(DownloadSingleFile(singleFileDownloadRequest)); if (!singleFileDownloadRequest.success) { yield break; } var tableStr = File.ReadAllText(xmlPath); _currentDownloadAssetBundleTable = tableStr.FromXML <AssetBundleTable>(); var newFileMap = new FileMapSystem.FileMapSystem(_tempDownloadPath); var bs = File.ReadAllBytes(xmfTargetPath); newFileMap.InitFileMapInfo(bs); PrepareDownloads(newFileMap, versionStr); _step = Step.Prepared; } else { switch (state) { case VersionInfo.State.MustDownloadAppAgain: { #if UNITY_ANDROID ShowAndroidUpdateDialog(versionInfo.AppUpdateUrl); yield break; #elif UNITY_IOS ShowIOSUpdateDialog(versionInfo.AppUpdateUrl); yield break; #else var mb = MessageBox.Show("提示", $"需要重新下载app"); yield return(mb); Quit(); #endif break; } case VersionInfo.State.NotNeedUpdate: { OnComplete(); break; } } } }
private IEnumerator Processing() { if (!Directory.Exists(_savePath)) { Directory.CreateDirectory(_savePath); } if (!Directory.Exists(_tempDownloadPath)) { Directory.CreateDirectory(_tempDownloadPath); } _currentDownloadingGroupDesc = null; _currentDownloadAssetBundleTable = null; _currentTargetVersion = null; _step = Step.Versions; if (_step == Step.Versions) { yield return(RequestVersions()); } if (_step == Step.Prepared) { OnMessage("正在检查版本信息..."); var totalSize = _downloader.GetToDownloadSize(); if (totalSize > 0) { var tips = $"发现内容更新,总计需要下载 {GetDisplaySize(totalSize)} 内容"; var mb = MessageBox.Show("提示", tips, "下载", "退出"); yield return(mb); if (mb.isOk) { _downloader.StartDownloadFile(); _step = Step.Download; } else { Quit(); yield break; } } else { _step = Step.Download; } } if (_step == Step.Download) { var progress = _downloader.GetProgress(); do { OnMessage( $"下载中...{GetDisplaySize(progress.CompletedSize)}/{GetDisplaySize(progress.TotalSize)}, 速度:{GetDisplaySpeed(progress.Speed)}"); OnProgress(progress.CompletedSize * 1f / progress.TotalSize); yield return(null); } while (!progress.IsCompleted); if (progress.IsError) { var tips = $"下载文件时发生错误(错误码:00{(int) progress.ErrorType}), 是否重新下载"; var mb = MessageBox.Show("提示", tips, "下载", "退出"); yield return(mb); if (mb.isOk) { StartUpdate(); } else { Quit(); } yield break; } _step = Step.Refresh; MergeUpdateFileMaps(); OnProgress(1); OnMessage("更新完成"); StartCoroutine(ReloadResources()); } }