Files
YooAsset/Assets/YooAsset/Runtime/PackageSystem/PlayMode/HostPlayModeImpl.cs

331 lines
10 KiB
C#
Raw Normal View History

2022-03-01 10:44:12 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
namespace YooAsset
{
2022-12-27 10:19:35 +08:00
internal class HostPlayModeImpl : IPlayModeServices, IBundleServices, IRemoteServices
2022-03-01 10:44:12 +08:00
{
private PackageManifest _activeManifest;
2022-03-01 10:44:12 +08:00
// 参数相关
2022-12-17 22:37:39 +08:00
private string _packageName;
2022-03-01 10:44:12 +08:00
private string _defaultHostServer;
private string _fallbackHostServer;
2022-12-17 22:37:39 +08:00
private IQueryServices _queryServices;
2022-03-01 10:44:12 +08:00
/// <summary>
/// 异步初始化
/// </summary>
public InitializationOperation InitializeAsync(string packageName, string defaultHostServer, string fallbackHostServer, IQueryServices queryServices)
2022-03-01 10:44:12 +08:00
{
2022-12-17 22:37:39 +08:00
_packageName = packageName;
2022-03-01 10:44:12 +08:00
_defaultHostServer = defaultHostServer;
_fallbackHostServer = fallbackHostServer;
2022-12-17 22:37:39 +08:00
_queryServices = queryServices;
2022-03-01 10:44:12 +08:00
2022-10-27 14:20:05 +08:00
var operation = new HostPlayModeInitializationOperation(this, packageName);
2022-08-06 11:23:43 +08:00
OperationSystem.StartOperation(operation);
2022-03-01 10:44:12 +08:00
return operation;
}
2022-12-17 22:37:39 +08:00
// 下载相关
private List<BundleInfo> ConvertToDownloadList(List<PackageBundle> downloadList)
2022-12-17 22:37:39 +08:00
{
List<BundleInfo> result = new List<BundleInfo>(downloadList.Count);
foreach (var packageBundle in downloadList)
2022-12-17 22:37:39 +08:00
{
var bundleInfo = ConvertToDownloadInfo(packageBundle);
2022-12-17 22:37:39 +08:00
result.Add(bundleInfo);
}
return result;
}
private BundleInfo ConvertToDownloadInfo(PackageBundle packageBundle)
2022-12-17 22:37:39 +08:00
{
string remoteMainURL = GetRemoteMainURL(packageBundle.FileName);
string remoteFallbackURL = GetRemoteFallbackURL(packageBundle.FileName);
BundleInfo bundleInfo = new BundleInfo(packageBundle, BundleInfo.ELoadMode.LoadFromRemote, remoteMainURL, remoteFallbackURL);
2022-12-17 22:37:39 +08:00
return bundleInfo;
}
2022-12-27 10:19:35 +08:00
#region IRemoteServices接口
public string GetRemoteMainURL(string fileName)
{
return $"{_defaultHostServer}/{fileName}";
}
public string GetRemoteFallbackURL(string fileName)
{
return $"{_fallbackHostServer}/{fileName}";
2022-12-17 22:37:39 +08:00
}
2022-12-27 10:19:35 +08:00
#endregion
2022-12-17 22:37:39 +08:00
#region IPlayModeServices接口
public PackageManifest ActiveManifest
2022-12-17 22:37:39 +08:00
{
set
{
2022-12-24 22:09:14 +08:00
_activeManifest = value;
2022-12-17 22:37:39 +08:00
}
get
{
2022-12-24 22:09:14 +08:00
return _activeManifest;
2022-12-17 22:37:39 +08:00
}
}
public void FlushManifestVersionFile()
{
if (_activeManifest != null)
{
PersistentTools.GetPersistent(_packageName).SaveSandboxPackageVersionFile(_activeManifest.PackageVersion);
}
}
private bool IsBuildinPackageBundle(PackageBundle packageBundle)
{
return _queryServices.QueryStreamingAssets(_packageName, packageBundle.FileName);
}
private bool IsCachedPackageBundle(PackageBundle packageBundle)
{
return CacheSystem.IsCached(packageBundle.PackageName, packageBundle.CacheGUID);
}
2022-12-17 22:37:39 +08:00
UpdatePackageVersionOperation IPlayModeServices.UpdatePackageVersionAsync(bool appendTimeTicks, int timeout)
2022-03-01 10:44:12 +08:00
{
2022-12-17 22:37:39 +08:00
var operation = new HostPlayModeUpdatePackageVersionOperation(this, _packageName, appendTimeTicks, timeout);
2022-08-06 11:23:43 +08:00
OperationSystem.StartOperation(operation);
return operation;
}
UpdatePackageManifestOperation IPlayModeServices.UpdatePackageManifestAsync(string packageVersion, bool autoSaveVersion, int timeout)
{
var operation = new HostPlayModeUpdatePackageManifestOperation(this, _packageName, packageVersion, autoSaveVersion, timeout);
2022-08-06 11:23:43 +08:00
OperationSystem.StartOperation(operation);
return operation;
}
PreDownloadContentOperation IPlayModeServices.PreDownloadContentAsync(string packageVersion, int timeout)
{
var operation = new HostPlayModePreDownloadContentOperation(this, _packageName, packageVersion, timeout);
2022-12-26 00:48:56 +08:00
OperationSystem.StartOperation(operation);
return operation;
}
2022-03-01 10:44:12 +08:00
ResourceDownloaderOperation IPlayModeServices.CreateResourceDownloaderByAll(int downloadingMaxNumber, int failedTryAgain, int timeout)
{
2022-12-24 22:09:14 +08:00
List<BundleInfo> downloadList = GetDownloadListByAll(_activeManifest);
var operation = new ResourceDownloaderOperation(downloadList, downloadingMaxNumber, failedTryAgain, timeout);
return operation;
}
public List<BundleInfo> GetDownloadListByAll(PackageManifest manifest)
{
List<PackageBundle> downloadList = new List<PackageBundle>(1000);
foreach (var packageBundle in manifest.BundleList)
{
// 忽略缓存文件
if (IsCachedPackageBundle(packageBundle))
continue;
// 忽略APP资源
if (IsBuildinPackageBundle(packageBundle))
2022-09-28 11:55:12 +08:00
continue;
downloadList.Add(packageBundle);
}
return ConvertToDownloadList(downloadList);
}
ResourceDownloaderOperation IPlayModeServices.CreateResourceDownloaderByTags(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout)
2022-03-01 10:44:12 +08:00
{
2022-12-24 22:09:14 +08:00
List<BundleInfo> downloadList = GetDownloadListByTags(_activeManifest, tags);
var operation = new ResourceDownloaderOperation(downloadList, downloadingMaxNumber, failedTryAgain, timeout);
2022-03-03 18:07:20 +08:00
return operation;
2022-03-01 10:44:12 +08:00
}
public List<BundleInfo> GetDownloadListByTags(PackageManifest manifest, string[] tags)
2022-03-01 10:44:12 +08:00
{
List<PackageBundle> downloadList = new List<PackageBundle>(1000);
foreach (var packageBundle in manifest.BundleList)
2022-03-01 10:44:12 +08:00
{
// 忽略缓存文件
if (IsCachedPackageBundle(packageBundle))
2022-03-01 10:44:12 +08:00
continue;
// 忽略APP资源
if (IsBuildinPackageBundle(packageBundle))
2022-09-28 11:55:12 +08:00
continue;
2022-03-01 10:44:12 +08:00
2022-09-28 11:55:12 +08:00
// 如果未带任何标记,则统一下载
if (packageBundle.HasAnyTags() == false)
2022-03-01 10:44:12 +08:00
{
downloadList.Add(packageBundle);
2022-03-01 10:44:12 +08:00
}
else
{
// 查询DLC资源
if (packageBundle.HasTag(tags))
2022-03-01 10:44:12 +08:00
{
downloadList.Add(packageBundle);
2022-03-01 10:44:12 +08:00
}
}
}
return ConvertToDownloadList(downloadList);
}
ResourceDownloaderOperation IPlayModeServices.CreateResourceDownloaderByPaths(AssetInfo[] assetInfos, int downloadingMaxNumber, int failedTryAgain, int timeout)
2022-03-01 10:44:12 +08:00
{
2022-12-24 22:09:14 +08:00
List<BundleInfo> downloadList = GetDownloadListByPaths(_activeManifest, assetInfos);
var operation = new ResourceDownloaderOperation(downloadList, downloadingMaxNumber, failedTryAgain, timeout);
2022-03-03 18:07:20 +08:00
return operation;
2022-03-01 10:44:12 +08:00
}
public List<BundleInfo> GetDownloadListByPaths(PackageManifest manifest, AssetInfo[] assetInfos)
2022-03-01 10:44:12 +08:00
{
// 获取资源对象的资源包和所有依赖资源包
List<PackageBundle> checkList = new List<PackageBundle>();
foreach (var assetInfo in assetInfos)
2022-03-01 10:44:12 +08:00
{
if (assetInfo.IsInvalid)
2022-03-01 10:44:12 +08:00
{
YooLogger.Warning(assetInfo.Error);
continue;
2022-03-01 10:44:12 +08:00
}
// 注意:如果清单里未找到资源包会抛出异常!
PackageBundle mainBundle = manifest.GetMainPackageBundle(assetInfo.AssetPath);
if (checkList.Contains(mainBundle) == false)
checkList.Add(mainBundle);
// 注意:如果清单里未找到资源包会抛出异常!
PackageBundle[] dependBundles = manifest.GetAllDependencies(assetInfo.AssetPath);
foreach (var dependBundle in dependBundles)
2022-03-01 10:44:12 +08:00
{
if (checkList.Contains(dependBundle) == false)
checkList.Add(dependBundle);
2022-03-01 10:44:12 +08:00
}
}
List<PackageBundle> downloadList = new List<PackageBundle>(1000);
foreach (var packageBundle in checkList)
2022-03-01 10:44:12 +08:00
{
// 忽略缓存文件
if (IsCachedPackageBundle(packageBundle))
2022-03-01 10:44:12 +08:00
continue;
// 忽略APP资源
if (IsBuildinPackageBundle(packageBundle))
2022-09-28 11:55:12 +08:00
continue;
2022-03-01 10:44:12 +08:00
downloadList.Add(packageBundle);
2022-03-01 10:44:12 +08:00
}
return ConvertToDownloadList(downloadList);
}
ResourceUnpackerOperation IPlayModeServices.CreateResourceUnpackerByAll(int upackingMaxNumber, int failedTryAgain, int timeout)
2022-03-07 20:13:39 +08:00
{
2022-12-24 22:09:14 +08:00
List<BundleInfo> unpcakList = GetUnpackListByAll(_activeManifest);
var operation = new ResourceUnpackerOperation(unpcakList, upackingMaxNumber, failedTryAgain, timeout);
2022-03-07 20:13:39 +08:00
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;
if (IsBuildinPackageBundle(packageBundle))
{
downloadList.Add(packageBundle);
}
}
2023-06-26 10:42:56 +08:00
return ManifestTools.ConvertToUnpackInfos(downloadList);
}
ResourceUnpackerOperation IPlayModeServices.CreateResourceUnpackerByTags(string[] tags, int upackingMaxNumber, int failedTryAgain, int timeout)
{
2022-12-24 22:09:14 +08:00
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;
2022-12-18 21:55:49 +08:00
// 查询DLC资源
if (IsBuildinPackageBundle(packageBundle))
2022-09-28 11:55:12 +08:00
{
if (packageBundle.HasTag(tags))
2022-12-18 21:55:49 +08:00
{
downloadList.Add(packageBundle);
2022-12-18 21:55:49 +08:00
}
2022-09-28 11:55:12 +08:00
}
}
2023-06-26 10:42:56 +08:00
return ManifestTools.ConvertToUnpackInfos(downloadList);
}
2022-12-17 22:37:39 +08:00
#endregion
2022-03-01 10:44:12 +08:00
#region IBundleServices接口
private BundleInfo CreateBundleInfo(PackageBundle packageBundle)
2022-03-01 10:44:12 +08:00
{
if (packageBundle == null)
throw new Exception("Should never get here !");
// 查询沙盒资源
if (IsCachedPackageBundle(packageBundle))
2022-03-01 10:44:12 +08:00
{
BundleInfo bundleInfo = new BundleInfo(packageBundle, BundleInfo.ELoadMode.LoadFromCache);
return bundleInfo;
}
// 查询APP资源
if (IsBuildinPackageBundle(packageBundle))
{
BundleInfo bundleInfo = new BundleInfo(packageBundle, BundleInfo.ELoadMode.LoadFromStreaming);
2022-09-28 11:55:12 +08:00
return bundleInfo;
2022-03-01 10:44:12 +08:00
}
// 从服务端下载
return ConvertToDownloadInfo(packageBundle);
2022-03-01 10:44:12 +08:00
}
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 !");
// 注意:如果清单里未找到资源包会抛出异常!
2022-12-24 22:09:14 +08:00
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()
{
2022-12-24 22:09:14 +08:00
return _activeManifest != null;
}
2022-03-01 10:44:12 +08:00
#endregion
}
}