123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- namespace YooAsset
- {
- internal class OfflinePlayModeImpl : IPlayModeServices, IBundleServices
- {
- private PackageManifest _activeManifest;
- /// <summary>
- /// 异步初始化
- /// </summary>
- public InitializationOperation InitializeAsync(string packageName)
- {
- var operation = new OfflinePlayModeInitializationOperation(this, packageName);
- OperationSystem.StartOperation(operation);
- return operation;
- }
- #region IPlayModeServices接口
- public PackageManifest ActiveManifest
- {
- set
- {
- _activeManifest = value;
- }
- get
- {
- return _activeManifest;
- }
- }
- public void FlushManifestVersionFile()
- {
- }
- private bool IsCachedPackageBundle(PackageBundle packageBundle)
- {
- return CacheSystem.IsCached(packageBundle.PackageName, packageBundle.CacheGUID);
- }
- UpdatePackageVersionOperation IPlayModeServices.UpdatePackageVersionAsync(bool appendTimeTicks, int timeout)
- {
- var operation = new OfflinePlayModeUpdatePackageVersionOperation();
- OperationSystem.StartOperation(operation);
- return operation;
- }
- UpdatePackageManifestOperation IPlayModeServices.UpdatePackageManifestAsync(string packageVersion, bool autoSaveVersion, int timeout)
- {
- var operation = new OfflinePlayModeUpdatePackageManifestOperation();
- OperationSystem.StartOperation(operation);
- return operation;
- }
- PreDownloadContentOperation IPlayModeServices.PreDownloadContentAsync(string packageVersion, int timeout)
- {
- var operation = new OfflinePlayModePreDownloadContentOperation();
- OperationSystem.StartOperation(operation);
- return operation;
- }
- ResourceDownloaderOperation IPlayModeServices.CreateResourceDownloaderByAll(int downloadingMaxNumber, int failedTryAgain, int timeout)
- {
- return ResourceDownloaderOperation.CreateEmptyDownloader(downloadingMaxNumber, failedTryAgain, timeout);
- }
- ResourceDownloaderOperation IPlayModeServices.CreateResourceDownloaderByTags(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout)
- {
- return ResourceDownloaderOperation.CreateEmptyDownloader(downloadingMaxNumber, failedTryAgain, timeout);
- }
- ResourceDownloaderOperation IPlayModeServices.CreateResourceDownloaderByPaths(AssetInfo[] assetInfos, int downloadingMaxNumber, int failedTryAgain, int timeout)
- {
- return ResourceDownloaderOperation.CreateEmptyDownloader(downloadingMaxNumber, failedTryAgain, timeout);
- }
- ResourceUnpackerOperation IPlayModeServices.CreateResourceUnpackerByAll(int upackingMaxNumber, int failedTryAgain, int timeout)
- {
- List<BundleInfo> unpcakList = GetUnpackListByAll(_activeManifest);
- var operation = new ResourceUnpackerOperation(unpcakList, upackingMaxNumber, failedTryAgain, timeout);
- return operation;
- }
- private List<BundleInfo> GetUnpackListByAll(PackageManifest manifest)
- {
- List<PackageBundle> downloadList = new List<PackageBundle>(1000);
- foreach (var packageBundle in manifest.BundleList)
- {
- // 忽略缓存文件
- if (IsCachedPackageBundle(packageBundle))
- continue;
- downloadList.Add(packageBundle);
- }
- return ManifestTools.ConvertToUnpackInfos(downloadList);
- }
- ResourceUnpackerOperation IPlayModeServices.CreateResourceUnpackerByTags(string[] tags, int upackingMaxNumber, int failedTryAgain, int timeout)
- {
- List<BundleInfo> unpcakList = GetUnpackListByTags(_activeManifest, tags);
- var operation = new ResourceUnpackerOperation(unpcakList, upackingMaxNumber, failedTryAgain, timeout);
- return operation;
- }
- private List<BundleInfo> GetUnpackListByTags(PackageManifest manifest, string[] tags)
- {
- List<PackageBundle> downloadList = new List<PackageBundle>(1000);
- foreach (var packageBundle in manifest.BundleList)
- {
- // 忽略缓存文件
- if (IsCachedPackageBundle(packageBundle))
- continue;
- // 查询DLC资源
- if (packageBundle.HasTag(tags))
- {
- downloadList.Add(packageBundle);
- }
- }
- return ManifestTools.ConvertToUnpackInfos(downloadList);
- }
- #endregion
- #region IBundleServices接口
- private BundleInfo CreateBundleInfo(PackageBundle packageBundle)
- {
- if (packageBundle == null)
- throw new Exception("Should never get here !");
- // 查询沙盒资源
- if (CacheSystem.IsCached(packageBundle.PackageName, packageBundle.CacheGUID))
- {
- BundleInfo bundleInfo = new BundleInfo(packageBundle, BundleInfo.ELoadMode.LoadFromCache);
- return bundleInfo;
- }
- // 查询APP资源
- {
- BundleInfo bundleInfo = new BundleInfo(packageBundle, BundleInfo.ELoadMode.LoadFromStreaming);
- return bundleInfo;
- }
- }
- BundleInfo IBundleServices.GetBundleInfo(AssetInfo assetInfo)
- {
- if (assetInfo.IsInvalid)
- throw new Exception("Should never get here !");
- // 注意:如果清单里未找到资源包会抛出异常!
- var packageBundle = _activeManifest.GetMainPackageBundle(assetInfo.AssetPath);
- return CreateBundleInfo(packageBundle);
- }
- BundleInfo[] IBundleServices.GetAllDependBundleInfos(AssetInfo assetInfo)
- {
- if (assetInfo.IsInvalid)
- throw new Exception("Should never get here !");
- // 注意:如果清单里未找到资源包会抛出异常!
- var depends = _activeManifest.GetAllDependencies(assetInfo.AssetPath);
- List<BundleInfo> result = new List<BundleInfo>(depends.Length);
- foreach (var packageBundle in depends)
- {
- BundleInfo bundleInfo = CreateBundleInfo(packageBundle);
- result.Add(bundleInfo);
- }
- return result.ToArray();
- }
- string IBundleServices.GetBundleName(int bundleID)
- {
- return _activeManifest.GetBundleName(bundleID);
- }
- bool IBundleServices.IsServicesValid()
- {
- return _activeManifest != null;
- }
- #endregion
- }
- }
|