public IEnumerator LoadStream(Manifest.BundleInfo bundleInfo, Action <Stream> callback) { MemoryStream stream = null; if (Contains(bundleInfo)) { var uri = _streamingAssetsPathRoot + bundleInfo.name; var uwr = UnityWebRequest.Get(uri); yield return(uwr.SendWebRequest()); if (uwr.error == null && uwr.responseCode == 200) { try { var bytes = uwr.downloadHandler.data; stream = new MemoryStream(bytes); } catch (Exception exception) { Debug.LogWarning($"StreamingAssetsLoader load failed: {exception}"); } } else { Debug.LogWarning($"load failed {uwr.error}: {uwr.responseCode}"); } } callback(stream); }
//NOTE: 调用此接口时已经确认 StreamingAssets 以及本地包文件均无效 private DownloadWorker.JobInfo _DownloadBundleFile(Manifest.BundleInfo bundleInfo, Action callback, bool emergency) { var bytesPerSecond = emergency ? _bytesPerSecond : _bytesPerSecondIdle; var oldJob = _FindDownloadJob(bundleInfo.name); if (oldJob != null) { if (oldJob.bytesPerSecond < bytesPerSecond) { oldJob.bytesPerSecond = bytesPerSecond; } if (callback != null) { oldJob.callback += callback; } return(oldJob); } // 无法打开现有文件, 下载新文件 var bundlePath = Path.Combine(_localPathRoot, bundleInfo.name); var newJob = new DownloadWorker.JobInfo(bundleInfo.name, bundleInfo.checksum, bundleInfo.comment, bundleInfo.priority, bundleInfo.size, bundlePath) { emergency = emergency, bytesPerSecond = bytesPerSecond, callback = callback }; AddDownloadTask(newJob); Schedule(); return(newJob); }
public IEnumerator LoadStream(Manifest.BundleInfo bundleInfo, Action <Stream> callback) { MemoryStream stream = null; if (Contains(bundleInfo)) { var streamingAssetsFilePath = _streamingAssetsPathRoot + bundleInfo.name; var uwr = UnityWebRequest.Get(streamingAssetsFilePath); yield return(uwr.SendWebRequest()); if (uwr.error == null && uwr.responseCode == 200) { try { var bytes = uwr.downloadHandler.data; stream = new MemoryStream(bytes); } catch (Exception exception) { Debug.LogWarningFormat("LoadStream exception: {0}\n{1}", streamingAssetsFilePath, exception); } } else { Debug.LogWarningFormat("LoadStream request failed {0}: {1} {2}", streamingAssetsFilePath, uwr.responseCode, uwr.error); } } callback(stream); }
public DownloadWorker.JobInfo EnsureBundle(Manifest.BundleInfo bundleInfo) { if (!IsBundleAvailable(bundleInfo)) { return(_DownloadBundleFile(bundleInfo, null, false)); } return(null); }
public static DownloadTask Create( Manifest.BundleInfo bundleInfo, IList <string> urls, string filePathRoot, int retry, int timeout, Action <DownloadTask> callback) { return(Create(bundleInfo.name, bundleInfo.checksum, bundleInfo.size, bundleInfo.priority, urls, filePathRoot, retry, timeout, callback)); }
// 尝试获取包对象 (不会自动创建并加载) private UBundle TryGetBundle(Manifest.BundleInfo bundleInfo) { if (_closed) { return(null); } UBundle bundle; return(bundleInfo != null && _bundles.TryGetValue(bundleInfo.name, out bundle) ? bundle : null); }
public DownloadWorker.JobInfo EnsureBundle(Manifest.BundleInfo bundleInfo) { var list = _manifestObject?.bundles; if (list != null && list.Contains(bundleInfo)) { if (!IsBundleAvailable(bundleInfo)) { return(_DownloadBundleFile(bundleInfo, null, false)); } } return(null); }
//TODO: hints private UBundle GetBundle(Manifest.BundleInfo bundleInfo, EAssetHints hints) { if (_closed) { return(null); } UBundle bundle = null; if (bundleInfo != null) { var bundleName = bundleInfo.name; if (!_bundles.TryGetValue(bundleName, out bundle)) { switch (bundleInfo.type) { case Manifest.BundleType.AssetBundle: bundle = new UAssetBundleBundle(this, bundleInfo, hints); break; case Manifest.BundleType.ZipArchive: bundle = new UZipArchiveBundle(this, bundleInfo); break; case Manifest.BundleType.FileList: bundle = new UFileListBundle(this, bundleInfo); break; } if (bundle != null) { _bundles.Add(bundleName, bundle); _AddDependencies(bundle, hints, bundle.bundleInfo.dependencies); // 第一次访问 UBundle 时进入此处逻辑, 但这之前可能已经使用 EnsureBundles 等方式发起文件下载 // 优先检查是否已经存在下载中的任务, 如果已经存在下载任务, 任务完成时将自动调用 UBundle.Load(stream) var bundleJob = _FindDownloadJob(bundle.name); if (bundleJob == null) { if (!LoadBundleFile(bundle)) { DownloadBundleFile(bundle.bundleInfo, null); } } } } } return(bundle); }
// 下载包文件 (优先考虑从 StreamingAssets 载入, 无法载入时从网络下载) //NOTE: 调用此接口时已经确认本地包文件无效 (本地临时存储) private void DownloadBundleFile(Manifest.BundleInfo bundleInfo, Action callback) { var oldJob = _FindDownloadJob(bundleInfo.name); if (oldJob != null) { if (callback != null) { oldJob.callback += callback; } return; } if (_streamingAssets.Contains(bundleInfo)) { JobScheduler.DispatchCoroutine( _streamingAssets.LoadStream(bundleInfo, stream => { if (stream != null) { var bundle = TryGetBundle(bundleInfo); if (bundle != null) { bundle.Load(Utils.Helpers.GetDecryptStream(stream, bundle.bundleInfo, _password, _manifestObject.chunkSize)); } else { stream.Close(); } callback?.Invoke(); } else { Debug.LogWarningFormat("read from streamingassets failed: {0}", bundleInfo.name); _DownloadBundleFile(bundleInfo, callback, true); } }) ); } else { _DownloadBundleFile(bundleInfo, callback, true); } }
// 检查是否存在有效的本地包 public bool IsBundleAvailable(Manifest.BundleInfo bundleInfo) { // 仅验证 StreamingAssets 清单内存在此资源包 (因为没办法直接安全有效地访问 StreamingAssets 内文件) if (_streamingAssets.Contains(bundleInfo)) { return(true); } var fullPath = Path.Combine(_localPathRoot, bundleInfo.name); if (Utils.Helpers.IsBundleFileValid(fullPath, bundleInfo)) { return(true); } return(false); }
public bool Contains(Manifest.BundleInfo bundleInfo) { for (int i = 0, count = _manifest.bundles.Count; i < count; i++) { var embeddedBundleInfo = _manifest.bundles[i]; if (embeddedBundleInfo.name == bundleInfo.name) { if (embeddedBundleInfo.size == bundleInfo.size && embeddedBundleInfo.checksum == bundleInfo.checksum) { return(true); } return(false); } } return(false); }
public DownloadWorker.JobInfo EnsureBundle(Manifest.BundleInfo bundleInfo) { return(null); }
// 下载指定的资源包 (返回 null 表示不需要下载) public static DownloadWorker.JobInfo EnsureBundle(Manifest.BundleInfo bundleInfo) { return(GetAssetProvider().EnsureBundle(bundleInfo)); }
public UBundle(Manifest.BundleInfo bundleInfo) { _info = bundleInfo; }
public UFileListBundle(BundleAssetProvider provider, Manifest.BundleInfo bundleInfo) : base(bundleInfo) { _provider = provider; }
public UAssetBundleBundle(BundleAssetProvider provider, Manifest.BundleInfo bundleInfo, EAssetHints hints) : base(bundleInfo) { _hints = hints; _provider = provider; }
public UZipArchiveBundle(BundleAssetProvider provider, Manifest.BundleInfo bundleInfo) : base(bundleInfo) { _provider = provider; }
// 检查是否存在有效的本地包 public bool IsBundleFileValid(Manifest.BundleInfo bundleInfo) { var fullPath = Path.Combine(_localPathRoot, bundleInfo.name); return(Utils.Helpers.IsBundleFileValid(fullPath, bundleInfo)); }