private void WriteBundlesDatabase() { Console.WriteLine("Writing asset bundles database..."); _bundlesDatabaseBuilder.BuildAssetBundle(Path.Combine(_bundlesPath, "index"), _params.Target); string infoPath = Path.Combine(_params.BundlesLocation, _bundlesDatabaseBuilder.Database.Id + ".txt"); using (StreamWriter writer = File.CreateText(infoPath)) { writer.WriteLine("[" + PlayerSettings.companyName + " - " + PlayerSettings.productName + "]"); writer.WriteLine("Id: " + _bundlesDatabaseBuilder.Database.Id); writer.WriteLine("Version: " + CachingUtils.GetVersionFromId(_bundlesDatabaseBuilder.Database.Id)); writer.WriteLine("Hash: " + BuildUtils.ToHexString(_bundlesDatabaseBuilder.Database.Hash)); writer.WriteLine("Size: " + _bundlesDatabaseBuilder.Database.Size); writer.WriteLine("Created: " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss (K)")); writer.WriteLine("App Version: " + PlayerSettings.bundleVersion); writer.WriteLine("Platform: " + _params.Target.ToString()); writer.WriteLine("Subtarget: " + (_params.Target == BuildTarget.Android ? _params.AndroidSubtarget.ToString() : "Generic")); writer.WriteLine(); foreach (var bundleInfo in _bundles) { writer.WriteLine("[" + bundleInfo.Name + "]"); writer.WriteLine("Filename: " + bundleInfo.Data.Filename); writer.WriteLine("Size: " + bundleInfo.Data.Size); writer.WriteLine("Hash: " + BuildUtils.ToHexString(bundleInfo.Data.Hash)); writer.WriteLine("Cache Name: " + bundleInfo.Data.CacheName); writer.WriteLine("Version: " + CachingUtils.GetVersionFromHash(bundleInfo.Data.Hash)); if (bundleInfo.IsScene) { writer.WriteLine("Scenes:"); foreach (var scene in bundleInfo.AssetsPaths) { writer.WriteLine("\t" + scene + "\t(" + BuildUtils.ToHexString(GetHashForBuildAsset(scene)) + ")"); } } writer.WriteLine("Bundle Assets:"); foreach (var assetPath in bundleInfo.AssetsPaths) { writer.WriteLine("\t" + assetPath + "\t" + GetAssetId(assetPath) + "\t(" + BuildUtils.ToHexString(GetHashForBuildAsset(assetPath)) + ")"); } writer.WriteLine("Dependencies Assets:"); var assetPaths = BuildUtils.GetAssetPathsForObjects(bundleInfo.Dependencies); foreach (var assetPath in assetPaths) { writer.WriteLine("\t" + assetPath + "\t(" + BuildUtils.ToHexString(GetHashForBuildAsset(assetPath)) + ")"); } writer.WriteLine(); } } }
private IEnumerable <AssetBundlesDatabase.BundleData> EnumerateUncachedBundles(AssetBundlesDatabase database) { foreach (var bundleData in database.Bundles) { int version = CachingUtils.GetVersionFromHash(bundleData.Hash); if (!Caching.IsVersionCached(bundleData.Filename, version) && !IsBundleInShippedDatabase(bundleData.Name, version)) { yield return(bundleData); } } }
void Update() { // Recently switching to playmode cause some bugs with progress dialog UI, so configure a display for initial show const float initialProgressDisplayTime = 3.0f; try { if (_enumerator.MoveNext()) { var bundleData = _enumerator.Current; if (!UnityEditorInternal.InternalEditorUtility.inBatchMode) { int current = _database.Bundles.IndexOf(bundleData) + 1; if (Time.realtimeSinceStartup - _startTime > initialProgressDisplayTime) { bool cancelled = EditorUtility.DisplayCancelableProgressBar("Caching asset bundles", bundleData.Name, (float)(current) / (float)_database.Bundles.Count); if (cancelled) { throw new OperationCanceledException(); } } } Console.WriteLine("Caching bundle " + bundleData.Name + "..."); using (var www = WWW.LoadFromCacheOrDownload("file://" + _bundlesDirectory + "/" + bundleData.Filename, CachingUtils.GetVersionFromHash(bundleData.Hash))) { www.assetBundle.Unload(true); // Will block to load } } else { PlayerPrefs.SetString("AssetBundlesDatabaseCacher.Exception", ""); Destroy(gameObject); EditorApplication.isPlaying = false; } } catch (Exception e) { Debug.LogError(e.Message); PlayerPrefs.SetString("AssetBundlesDatabaseCacher.Exception", e.GetType().AssemblyQualifiedName); Destroy(gameObject); EditorApplication.isPlaying = false; } }
public IEnumerator DoLoadOneBundle(string bundleId) { AssetBundlesDatabase database = LoadAssetBundlesDatabaseFromId(_lastRunDatabaseId); AssetBundlesDatabase.BundleData bundleData = null; foreach (var bData in database.Bundles) { if (bData.Name == bundleId) { bundleData = bData; break; } } if (bundleData != null) { BundleInfo bundleInfo = new BundleInfo(bundleData); int version = CachingUtils.GetVersionFromHash(bundleData.Hash); string databaseId = _latestDatabaseId; string url = UpdateUrl + "/" + databaseId + "/"; using (var www = WWW.LoadFromCacheOrDownload(url + bundleData.Filename, version)) { yield return(www); bundleInfo.Bundle = www.assetBundle; } foreach (var sceneName in bundleData.SceneNames) { _scenesMap.Add(sceneName, bundleInfo); } foreach (var assetId in bundleData.AssetsIds) { _assetIdMap[assetId] = bundleInfo; } _bundlesMap.Add(bundleData.Name, bundleInfo); } }
private bool IsBundleInShippedDatabase(string name, int version) { if (!_buildInfo.BundlesIncluded && !_buildInfo.BundlesCacheIncluded) { return(false); } if (_shippedDatabase[name] != null) { int shippedVersion = CachingUtils.GetVersionFromHash(_shippedDatabase[name].Hash); if (version == shippedVersion) { return(true); } } return(false); }
private void AddToDatabase(AssetBundlesDatabase.BundleData bundle, byte[] hash, string bundlePath) { bundle.Filename = Path.GetFileName(bundlePath); using (FileStream stream = File.OpenRead(bundlePath)) { bundle.Size = (int)stream.Length; bundle.Hash = hash; var str = bundle.Filename + "@" + CachingUtils.GetVersionFromHash(bundle.Hash); bundle.CacheName = BuildUtils.ToHexString(BuildUtils.GetASCIIStringHash(str)); } _database.Bundles.Add(bundle); // Update hash, id and size _database.Hash = BuildUtils.Sha1.ComputeHash(_database.Hash.Concat(bundle.Hash).ToArray()); _database.Id = BuildUtils.ToHexString(_database.Hash.Take(4)); _database.Size += bundle.Size; //EditorUtility.SetDirty(_database); }
private IEnumerator DoLoadBundles(string databaseId) { Console_WriteLine("Loading database " + databaseId + "..."); if (LoadingStart != null) { LoadingStart(); } var database = LoadAssetBundlesDatabaseFromId(databaseId); if (LoadStart != null) { LoadStart(database); } //Console.WriteLine("==== Current language: " + LanguageTextManager.CurSystemLang); float lastYieldTime = Time.realtimeSinceStartup; const float maximumYieldTime = 1.0f / 5.0f; int i = 0; foreach (var bundleData in database.Bundles) { BundleInfo bundleInfo = new BundleInfo(bundleData); Console_WriteLine("Loading bundle " + bundleData.Name + "..."); string url = UpdateUrl + "/" + databaseId + "/"; int version = CachingUtils.GetVersionFromHash(bundleData.Hash); if (!Caching.IsVersionCached(bundleData.Filename, version) && IsBundleInShippedDatabase(bundleData.Name, version)) { if (_buildInfo.BundlesCacheIncluded) { // Uncompressed cache copy (faster) Console_WriteLine("Caching " + bundleData.Name); CopyToCache(_shippedDatabase[bundleData.Name].CacheName); } else { Console_WriteLine("Caching decompressing " + bundleData.Name); url = StreamingAssetsUrl + "/Bundles/"; } } if (LoadProgress != null) { LoadProgress(database.Bundles.IndexOf(bundleData)); } using (var www = WWW.LoadFromCacheOrDownload(url + bundleData.Filename, version)) { yield return(www); lastYieldTime = Time.realtimeSinceStartup; bundleInfo.Bundle = www.assetBundle; } yield return(null); foreach (var sceneName in bundleData.SceneNames) { _scenesMap.Add(sceneName, bundleInfo); } foreach (var assetId in bundleData.AssetsIds) { _assetIdMap[assetId] = bundleInfo; } _bundlesMap.Add(bundleData.Name, bundleInfo); // Allow some processing if hanging for too much long if (Time.realtimeSinceStartup - lastYieldTime > maximumYieldTime) { yield return(null); lastYieldTime = Time.realtimeSinceStartup; } i++; if (i % 30 == 0) { yield return(new WaitForSeconds(0.5f)); } } Console_WriteLine("Database loaded"); if (LoadingFinish != null) { LoadingFinish(); } }
private IEnumerator DoDownloadBundles(string databaseId) { ResetUpdateError(); Console_WriteLine("Downloading database " + databaseId + "..."); if (DownloadingStart != null) { DownloadingStart(); } if (DownloadingBundleStart != null) { DownloadingBundleStart(); } if (DownloadingDatabase != null) { DownloadingDatabase(databaseId); } AssetBundlesDatabase database = _shippedDatabase; if (databaseId != _shippedDatabase.Id) { if (!Caching.IsVersionCached("index", CachingUtils.GetVersionFromId(databaseId))) { if (Application.internetReachability == NetworkReachability.NotReachable) { ThrowUpdateError(UpdateErrorType.NoInternetAvailable); yield break; } } // Download requested database int version = CachingUtils.GetVersionFromId(databaseId); // This do-while: workaround of unity bug of caching startup delay do { using (WWW www = WWW.LoadFromCacheOrDownload(UpdateUrl + "/" + databaseId + "/index", version)) { yield return(StartCoroutine(WwwDone(www))); if (WwwHasBundleError(www)) { } else { database = www.assetBundle.mainAsset as AssetBundlesDatabase; www.assetBundle.Unload(false); } } } while (!Caching.IsVersionCached("index", version)); } else { database = _shippedDatabase; } int totalDownloadSize = 0; var bundlesToDownload = new List <AssetBundlesDatabase.BundleData>(); // Check which bundles must be downloaded/cached foreach (var bundleData in EnumerateUncachedBundles(database)) { totalDownloadSize += bundleData.Size; bundlesToDownload.Add(bundleData); } if (bundlesToDownload.Count == 0) { yield break; } if (DownloadStart != null) { DownloadStart(database, bundlesToDownload, totalDownloadSize); } int totalDownloadedBytes = 0; if (bundlesToDownload.Count > 0) { Console_WriteLine("Will download " + bundlesToDownload.Count + " bundles, with a total of " + totalDownloadSize + " bytes"); string url = UpdateUrl + "/" + databaseId + "/"; //foreach (var bundleData in bundlesToDownload) for (int i = 0; i < bundlesToDownload.Count;) { var bundleData = bundlesToDownload.ElementAt(i); Console_WriteLine("Downloading bundle " + bundleData.Name + "..."); using (var www = WWW.LoadFromCacheOrDownload(url + bundleData.Filename, CachingUtils.GetVersionFromHash(bundleData.Hash))) { int previousDownloadedBytes = totalDownloadedBytes; yield return(StartCoroutine(WwwDone(www, () => { totalDownloadedBytes = previousDownloadedBytes + (int)(www.progress * (float)bundleData.Size); if (DownloadProgress != null) { DownloadProgress(bundlesToDownload.IndexOf(bundleData), totalDownloadedBytes); } }))); if (WwwHasBundleError(www)) { } else { www.assetBundle.Unload(false); totalDownloadedBytes = previousDownloadedBytes + bundleData.Size; } } i++; } } Console_WriteLine("Database downloaded"); if (DownloadingBundleEnd != null) { DownloadingBundleEnd("success", "url"); } if (DownloadingFinish != null) { DownloadingFinish(); } }