public static ITask LoadLocalBundles(IEnumerable <string> whitelist, IEnumerable <string> blacklist = null) { if (whitelist.IsNullOrEmpty()) { return(Task.Resolved); } string basePath = BundleUtility.GetLocalBundlePath(""); log.Info("Loading local asset bundles from {0}....", basePath); var whitelistRegex = whitelist.EmptyIfNull() .Select(r => new Regex(r.Replace("*", "(.*?)"), RegexOptions.Compiled)).ToArray(); var blacklistRegex = blacklist.EmptyIfNull() .Select(r => new Regex(r.Replace("*", "(.*?)"), RegexOptions.Compiled)).ToArray(); var task = Initialize().Then(map => Task.All(map.BundleNames .Where(name => blacklistRegex.All(r => !r.IsMatch(name)) && whitelistRegex.Any(r => r.IsMatch(name))) .Select(name => LoadAssetBundleAsync(name).Then(loadedBundle => { var bundle = loadedBundle.AssetBundle; if (bundle == null) { log.Error("Failed to load an AssetBundle from {0}.", loadedBundle.Metadata.Name); return(Task.Resolved); } var assetName = bundle.GetAllAssetNames()[0]; ITask <Object> assetTask = bundle.mainAsset != null ? Task.FromResult(bundle.mainAsset) : LoadAssetAsync <Object>(loadedBundle.Metadata.Name, assetName); return(assetTask.Then(asset => { if (asset == null) { return; } var assetType = asset.GetType(); Delegate handler; if (TypeHandlers.TryGetValue(assetType, out handler)) { log.Info("Loaded {0} ({1}) from \"{2}\".", asset.name, assetType.Name, loadedBundle.Metadata.Name); handler.DynamicInvoke(asset); } else { log.Error( "Attempted to load an asset of type {0} from asset bundle {1}, but no handler was found. Unloading...", assetType, loadedBundle.Metadata.Name); UnloadAssetBundle(loadedBundle.Metadata.Name); } })); })) )); task.Then(() => log.Info("Done loading local asset bundles")); return(task); }
// Where we actually load the assetbundles from the local disk. static ITask <LoadedAssetBundle> LoadAssetBundleInternal(string assetBundleName, bool isManifest) { // Already loaded. var name = SeperatorRegex.Replace(assetBundleName, "/"); ITask <LoadedAssetBundle> bundle; if (AssetBundles.TryGetValue(name, out bundle)) { bundle.Then(b => b.ReferencedCount++); return(bundle); } ITask <string> pathTask; if (isManifest) { pathTask = Task.FromResult(BundleUtility.GetLocalBundlePath(name)); } else { pathTask = Manifest.Then(manfiest => { if (manfiest == null) { return(null); } foreach (var path in manfiest[name].Paths) { var fullPath = BundleUtility.GetLocalBundlePath(path); if (File.Exists(fullPath)) { return(fullPath); } } throw new FileNotFoundException("No valid path for asset bundle {0} could be found.".With(name)); }); } // For manifest assetbundle, always download it as we don't have hash for it. var task = pathTask.Then(path => { var operation = AssetBundle.LoadFromFileAsync(path); return(AsyncManager.AddOperation(operation).Then(request => { var assetBundle = request.assetBundle; if (assetBundle == null) { throw new Exception("{0} is not a valid asset bundle.".With(name)); } LoadedAssetBundle loadedBundle; if (isManifest) { loadedBundle = new LoadedAssetBundle( new BundleMetadata(assetBundleName, new Hash128(), Enumerable.Empty <BundleMetadata>(), Enumerable.Empty <string>()), assetBundle); } else { loadedBundle = new LoadedAssetBundle(Manifest.Result[name], assetBundle); } log.Info("Loaded bundle \"{0}\" from {1}.", name, path); return loadedBundle; })); }); AssetBundles.Add(name, task); return(task); }