using System; using System.Collections; using System.Collections.Generic; namespace YooAsset { internal class WebPlayModeImpl : IPlayMode, IBundleQuery { public readonly string PackageName; public IFileSystem WebFileSystem { set; get; } public WebPlayModeImpl(string packageName) { PackageName = packageName; } /// /// 异步初始化 /// public InitializationOperation InitializeAsync(WebPlayModeParameters initParameters) { var operation = new WebPlayModeInitializationOperation(this, initParameters); OperationSystem.StartOperation(PackageName, operation); return operation; } #region IPlayMode接口 public PackageManifest ActiveManifest { set; get; } void IPlayMode.UpdatePlayMode() { if (WebFileSystem != null) WebFileSystem.OnUpdate(); } RequestPackageVersionOperation IPlayMode.RequestPackageVersionAsync(bool appendTimeTicks, int timeout) { var operation = new RequestPackageVersionImplOperation(WebFileSystem, appendTimeTicks, timeout); OperationSystem.StartOperation(PackageName, operation); return operation; } UpdatePackageManifestOperation IPlayMode.UpdatePackageManifestAsync(string packageVersion, int timeout) { var operation = new UpdatePackageManifestImplOperation(this, WebFileSystem, packageVersion, timeout);; OperationSystem.StartOperation(PackageName, operation); return operation; } PreDownloadContentOperation IPlayMode.PreDownloadContentAsync(string packageVersion, int timeout) { var operation = new WebPlayModePreDownloadContentOperation(this); OperationSystem.StartOperation(PackageName, operation); return operation; } ClearAllBundleFilesOperation IPlayMode.ClearAllBundleFilesAsync() { var operation = new ClearAllBundleFilesImplOperation(this, WebFileSystem, null, null); OperationSystem.StartOperation(PackageName, operation); return operation; } ClearUnusedBundleFilesOperation IPlayMode.ClearUnusedBundleFilesAsync() { var operation = new ClearUnusedBundleFilesImplOperation(this, WebFileSystem, null, null); OperationSystem.StartOperation(PackageName, operation); return operation; } ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByAll(int downloadingMaxNumber, int failedTryAgain, int timeout) { List downloadList = PlayModeHelper.GetDownloadListByAll(ActiveManifest, WebFileSystem); var operation = new ResourceDownloaderOperation(PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout); return operation; } ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByTags(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout) { List downloadList = PlayModeHelper.GetDownloadListByTags(ActiveManifest, tags, WebFileSystem); var operation = new ResourceDownloaderOperation(PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout); return operation; } ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByPaths(AssetInfo[] assetInfos, int downloadingMaxNumber, int failedTryAgain, int timeout) { List downloadList = PlayModeHelper.GetDownloadListByPaths(ActiveManifest, assetInfos, WebFileSystem); var operation = new ResourceDownloaderOperation(PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout); return operation; } ResourceUnpackerOperation IPlayMode.CreateResourceUnpackerByAll(int upackingMaxNumber, int failedTryAgain, int timeout) { List unpcakList = PlayModeHelper.GetUnpackListByAll(ActiveManifest, WebFileSystem); var operation = new ResourceUnpackerOperation(PackageName, unpcakList, upackingMaxNumber, failedTryAgain, timeout); return operation; } ResourceUnpackerOperation IPlayMode.CreateResourceUnpackerByTags(string[] tags, int upackingMaxNumber, int failedTryAgain, int timeout) { List unpcakList = PlayModeHelper.GetUnpackListByTags(ActiveManifest, tags, WebFileSystem); var operation = new ResourceUnpackerOperation(PackageName, unpcakList, upackingMaxNumber, failedTryAgain, timeout); return operation; } ResourceImporterOperation IPlayMode.CreateResourceImporterByFilePaths(string[] filePaths, int importerMaxNumber, int failedTryAgain, int timeout) { List importerList = PlayModeHelper.GetImporterListByFilePaths(ActiveManifest, filePaths, WebFileSystem); var operation = new ResourceImporterOperation(PackageName, importerList, importerMaxNumber, failedTryAgain, timeout); return operation; } #endregion #region IBundleQuery接口 private BundleInfo CreateBundleInfo(PackageBundle packageBundle, AssetInfo assetInfo) { if (packageBundle == null) throw new Exception("Should never get here !"); if (WebFileSystem.Belong(packageBundle)) { BundleInfo bundleInfo = new BundleInfo(WebFileSystem, packageBundle); return bundleInfo; } throw new Exception($"Can not found belong file system : {packageBundle.BundleName}"); } BundleInfo IBundleQuery.GetMainBundleInfo(AssetInfo assetInfo) { if (assetInfo.IsInvalid) throw new Exception("Should never get here !"); // 注意:如果清单里未找到资源包会抛出异常! var packageBundle = ActiveManifest.GetMainPackageBundle(assetInfo.AssetPath); return CreateBundleInfo(packageBundle, assetInfo); } BundleInfo[] IBundleQuery.GetDependBundleInfos(AssetInfo assetInfo) { if (assetInfo.IsInvalid) throw new Exception("Should never get here !"); // 注意:如果清单里未找到资源包会抛出异常! var depends = ActiveManifest.GetAllDependencies(assetInfo.AssetPath); List result = new List(depends.Length); foreach (var packageBundle in depends) { BundleInfo bundleInfo = CreateBundleInfo(packageBundle, assetInfo); result.Add(bundleInfo); } return result.ToArray(); } string IBundleQuery.GetMainBundleName(AssetInfo assetInfo) { if (assetInfo.IsInvalid) throw new Exception("Should never get here !"); // 注意:如果清单里未找到资源包会抛出异常! var packageBundle = ActiveManifest.GetMainPackageBundle(assetInfo.AssetPath); return packageBundle.BundleName; } string[] IBundleQuery.GetDependBundleNames(AssetInfo assetInfo) { if (assetInfo.IsInvalid) throw new Exception("Should never get here !"); // 注意:如果清单里未找到资源包会抛出异常! var depends = ActiveManifest.GetAllDependencies(assetInfo.AssetPath); List result = new List(depends.Length); foreach (var packageBundle in depends) { result.Add(packageBundle.BundleName); } return result.ToArray(); } bool IBundleQuery.ManifestValid() { return ActiveManifest != null; } #endregion } }