YooAsset/Assets/YooAsset/Runtime/PatchSystem/PlayMode/HostPlayModeImpl.cs

252 lines
8.0 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
{
internal class HostPlayModeImpl : IBundleServices
{
// 补丁清单
internal PatchManifest AppPatchManifest;
internal PatchManifest LocalPatchManifest;
// 参数相关
internal bool ClearCacheWhenDirty { private set; get; }
internal bool IgnoreResourceVersion { private set; get; }
private string _defaultHostServer;
private string _fallbackHostServer;
/// <summary>
/// 异步初始化
/// </summary>
public InitializationOperation InitializeAsync(bool clearCacheWhenDirty, bool ignoreResourceVersion,
string defaultHostServer, string fallbackHostServer)
{
ClearCacheWhenDirty = clearCacheWhenDirty;
IgnoreResourceVersion = ignoreResourceVersion;
_defaultHostServer = defaultHostServer;
_fallbackHostServer = fallbackHostServer;
var operation = new HostPlayModeInitializationOperation(this);
2022-03-09 21:53:01 +08:00
OperationSystem.ProcessOperaiton(operation);
2022-03-01 10:44:12 +08:00
return operation;
}
/// <summary>
/// 异步更新补丁清单
/// </summary>
public UpdateManifestOperation UpdatePatchManifestAsync(int updateResourceVersion, int timeout)
{
var operation = new HostPlayModeUpdateManifestOperation(this, updateResourceVersion, timeout);
2022-03-09 21:53:01 +08:00
OperationSystem.ProcessOperaiton(operation);
2022-03-01 10:44:12 +08:00
return operation;
}
/// <summary>
/// 获取资源版本号
/// </summary>
public int GetResourceVersion()
{
if (LocalPatchManifest == null)
return 0;
return LocalPatchManifest.ResourceVersion;
}
/// <summary>
2022-03-03 18:07:20 +08:00
/// 创建下载器
2022-03-01 10:44:12 +08:00
/// </summary>
2022-03-03 18:07:20 +08:00
public DownloaderOperation CreateDownloaderByTags(string[] tags, int fileLoadingMaxNumber, int failedTryAgain)
2022-03-01 10:44:12 +08:00
{
2022-03-07 21:20:58 +08:00
List<BundleInfo> downloadList = GetDownloadListByTags(tags);
2022-03-03 18:07:20 +08:00
var operation = new DownloaderOperation(downloadList, fileLoadingMaxNumber, failedTryAgain);
return operation;
2022-03-01 10:44:12 +08:00
}
2022-03-07 21:20:58 +08:00
private List<BundleInfo> GetDownloadListByTags(string[] tags)
2022-03-01 10:44:12 +08:00
{
List<PatchBundle> downloadList = new List<PatchBundle>(1000);
foreach (var patchBundle in LocalPatchManifest.BundleList)
{
// 忽略缓存文件
if (DownloadSystem.ContainsVerifyFile(patchBundle.Hash))
continue;
// 忽略APP资源
// 注意如果是APP资源并且哈希值相同则不需要下载
if (AppPatchManifest.Bundles.TryGetValue(patchBundle.BundleName, out PatchBundle appPatchBundle))
{
if (appPatchBundle.IsBuildin && appPatchBundle.Hash == patchBundle.Hash)
continue;
}
// 如果是纯内置资源,则统一下载
// 注意:可能是新增的或者变化的内置资源
// 注意:可能是由热更资源转换的内置资源
if (patchBundle.IsPureBuildin())
{
downloadList.Add(patchBundle);
}
else
{
// 查询DLC资源
2022-03-03 18:07:20 +08:00
if (patchBundle.HasTag(tags))
2022-03-01 10:44:12 +08:00
{
downloadList.Add(patchBundle);
}
}
}
return ConvertToDownloadList(downloadList);
}
/// <summary>
2022-03-03 18:07:20 +08:00
/// 创建下载器
2022-03-01 10:44:12 +08:00
/// </summary>
2022-03-03 18:07:20 +08:00
public DownloaderOperation CreateDownloaderByPaths(List<string> assetPaths, int fileLoadingMaxNumber, int failedTryAgain)
2022-03-01 10:44:12 +08:00
{
2022-03-07 21:20:58 +08:00
List<BundleInfo> downloadList = GetDownloadListByPaths(assetPaths);
2022-03-03 18:07:20 +08:00
var operation = new DownloaderOperation(downloadList, fileLoadingMaxNumber, failedTryAgain);
return operation;
2022-03-01 10:44:12 +08:00
}
2022-03-07 21:20:58 +08:00
private List<BundleInfo> GetDownloadListByPaths(List<string> assetPaths)
2022-03-01 10:44:12 +08:00
{
// 获取资源对象的资源包和所有依赖资源包
List<PatchBundle> checkList = new List<PatchBundle>();
foreach (var assetPath in assetPaths)
{
2022-03-07 21:20:58 +08:00
string mainBundleName = LocalPatchManifest.GetBundleName(assetPath);
2022-03-01 10:44:12 +08:00
if (string.IsNullOrEmpty(mainBundleName) == false)
{
if (LocalPatchManifest.Bundles.TryGetValue(mainBundleName, out PatchBundle mainBundle))
{
if (checkList.Contains(mainBundle) == false)
checkList.Add(mainBundle);
}
}
string[] dependBundleNames = LocalPatchManifest.GetAllDependencies(assetPath);
foreach (var dependBundleName in dependBundleNames)
{
if (LocalPatchManifest.Bundles.TryGetValue(dependBundleName, out PatchBundle dependBundle))
{
if (checkList.Contains(dependBundle) == false)
checkList.Add(dependBundle);
}
}
}
List<PatchBundle> downloadList = new List<PatchBundle>(1000);
foreach (var patchBundle in checkList)
{
// 忽略缓存文件
if (DownloadSystem.ContainsVerifyFile(patchBundle.Hash))
continue;
// 忽略APP资源
// 注意如果是APP资源并且哈希值相同则不需要下载
if (AppPatchManifest.Bundles.TryGetValue(patchBundle.BundleName, out PatchBundle appPatchBundle))
{
if (appPatchBundle.IsBuildin && appPatchBundle.Hash == patchBundle.Hash)
continue;
}
downloadList.Add(patchBundle);
}
return ConvertToDownloadList(downloadList);
}
2022-03-07 20:13:39 +08:00
/// <summary>
/// 创建解压器
/// </summary>
public DownloaderOperation CreateUnpackerByTags(string[] tags, int fileUpackingMaxNumber, int failedTryAgain)
{
2022-03-25 15:02:21 +08:00
List<BundleInfo> unpcakList = PatchHelper.GetUnpackListByTags(AppPatchManifest, tags);
2022-03-07 20:13:39 +08:00
var operation = new DownloaderOperation(unpcakList, fileUpackingMaxNumber, failedTryAgain);
return operation;
}
2022-03-01 10:44:12 +08:00
// WEB相关
2022-03-25 15:02:21 +08:00
public string GetPatchDownloadMainURL(int resourceVersion, string fileName)
2022-03-01 10:44:12 +08:00
{
if (IgnoreResourceVersion)
return $"{_defaultHostServer}/{fileName}";
else
return $"{_defaultHostServer}/{resourceVersion}/{fileName}";
}
2022-03-25 15:02:21 +08:00
public string GetPatchDownloadFallbackURL(int resourceVersion, string fileName)
2022-03-01 10:44:12 +08:00
{
if (IgnoreResourceVersion)
return $"{_fallbackHostServer}/{fileName}";
else
return $"{_fallbackHostServer}/{resourceVersion}/{fileName}";
}
// 下载相关
2022-03-07 21:20:58 +08:00
private List<BundleInfo> ConvertToDownloadList(List<PatchBundle> downloadList)
2022-03-01 10:44:12 +08:00
{
2022-03-07 21:20:58 +08:00
List<BundleInfo> result = new List<BundleInfo>(downloadList.Count);
2022-03-01 10:44:12 +08:00
foreach (var patchBundle in downloadList)
{
var bundleInfo = ConvertToDownloadInfo(patchBundle);
result.Add(bundleInfo);
}
return result;
}
2022-03-25 15:02:21 +08:00
private BundleInfo ConvertToDownloadInfo(PatchBundle patchBundle)
2022-03-07 20:13:39 +08:00
{
2022-03-25 15:02:21 +08:00
// 注意:资源版本号只用于确定下载路径
2022-03-09 23:57:04 +08:00
string sandboxPath = SandboxHelper.MakeSandboxCacheFilePath(patchBundle.Hash);
2022-03-25 15:02:21 +08:00
string remoteMainURL = GetPatchDownloadMainURL(patchBundle.Version, patchBundle.Hash);
string remoteFallbackURL = GetPatchDownloadFallbackURL(patchBundle.Version, patchBundle.Hash);
BundleInfo bundleInfo = new BundleInfo(patchBundle, sandboxPath, remoteMainURL, remoteFallbackURL);
2022-03-07 20:13:39 +08:00
return bundleInfo;
}
2022-03-01 10:44:12 +08:00
#region IBundleServices接口
2022-03-07 21:20:58 +08:00
BundleInfo IBundleServices.GetBundleInfo(string bundleName)
2022-03-01 10:44:12 +08:00
{
if (string.IsNullOrEmpty(bundleName))
2022-03-07 21:20:58 +08:00
return new BundleInfo(string.Empty, string.Empty);
2022-03-01 10:44:12 +08:00
if (LocalPatchManifest.Bundles.TryGetValue(bundleName, out PatchBundle patchBundle))
{
// 查询APP资源
if (AppPatchManifest.Bundles.TryGetValue(bundleName, out PatchBundle appPatchBundle))
{
if (appPatchBundle.IsBuildin && appPatchBundle.Hash == patchBundle.Hash)
{
2022-03-09 23:57:04 +08:00
string appLoadPath = PathHelper.MakeStreamingLoadPath(appPatchBundle.Hash);
2022-03-07 21:20:58 +08:00
BundleInfo bundleInfo = new BundleInfo(appPatchBundle, appLoadPath);
2022-03-01 10:44:12 +08:00
return bundleInfo;
}
}
// 查询沙盒资源
if (DownloadSystem.ContainsVerifyFile(patchBundle.Hash))
{
2022-03-09 23:57:04 +08:00
string sandboxLoadPath = SandboxHelper.MakeSandboxCacheFilePath(patchBundle.Hash);
2022-03-07 21:20:58 +08:00
BundleInfo bundleInfo = new BundleInfo(patchBundle, sandboxLoadPath);
2022-03-01 10:44:12 +08:00
return bundleInfo;
}
// 从服务端下载
return ConvertToDownloadInfo(patchBundle);
}
else
{
2022-03-09 21:53:01 +08:00
YooLogger.Warning($"Not found bundle in patch manifest : {bundleName}");
2022-03-07 21:20:58 +08:00
BundleInfo bundleInfo = new BundleInfo(bundleName, string.Empty);
2022-03-01 10:44:12 +08:00
return bundleInfo;
}
}
2022-03-07 21:20:58 +08:00
string IBundleServices.GetBundleName(string assetPath)
2022-03-01 10:44:12 +08:00
{
2022-03-07 21:20:58 +08:00
return LocalPatchManifest.GetBundleName(assetPath);
2022-03-01 10:44:12 +08:00
}
string[] IBundleServices.GetAllDependencies(string assetPath)
{
return LocalPatchManifest.GetAllDependencies(assetPath);
}
#endregion
}
}