Files
YooAsset/Assets/YooAsset/Runtime/PatchSystem/PlayMode/OfflinePlayModeImpl.cs
2022-12-17 22:37:39 +08:00

176 lines
5.8 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
namespace YooAsset
{
internal class OfflinePlayModeImpl : IPlayModeServices, IBundleServices
{
private PatchManifest _activePatchManifest;
private string _packageName;
private bool _locationToLower;
/// <summary>
/// 异步初始化
/// </summary>
public InitializationOperation InitializeAsync(string packageName, bool locationToLower)
{
_packageName = packageName;
_locationToLower = locationToLower;
var operation = new OfflinePlayModeInitializationOperation(this, packageName);
OperationSystem.StartOperation(operation);
return operation;
}
#region IPlayModeServices接口
public PatchManifest ActivePatchManifest
{
set
{
_activePatchManifest = value;
_activePatchManifest.InitAssetPathMapping(_locationToLower);
}
get
{
return _activePatchManifest;
}
}
public string GetPackageVersion()
{
if (_activePatchManifest == null)
return string.Empty;
return _activePatchManifest.PackageVersion;
}
public bool IsBuildinPatchBundle(PatchBundle patchBundle)
{
return true;
}
UpdatePackageVersionOperation IPlayModeServices.UpdatePackageVersionAsync(bool appendTimeTicks, int timeout)
{
var operation = new OfflinePlayModeUpdatePackageVersionOperation();
OperationSystem.StartOperation(operation);
return operation;
}
UpdatePackageManifestOperation IPlayModeServices.UpdatePackageManifestAsync(string packageVersion, bool autoSaveManifest, bool autoActiveManifest, int timeout)
{
var operation = new OfflinePlayModeUpdatePackageManifestOperation();
OperationSystem.StartOperation(operation);
return operation;
}
CheckPackageContentsOperation IPlayModeServices.CheckPackageContentsAsync()
{
var operation = new OfflinePlayModeCheckPackageContentsOperation();
OperationSystem.StartOperation(operation);
return operation;
}
PatchDownloaderOperation IPlayModeServices.CreatePatchDownloaderByAll(int downloadingMaxNumber, int failedTryAgain, int timeout)
{
List<BundleInfo> downloadList = new List<BundleInfo>();
var operation = new PatchDownloaderOperation(downloadList, downloadingMaxNumber, failedTryAgain, timeout);
return operation;
}
PatchDownloaderOperation IPlayModeServices.CreatePatchDownloaderByTags(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout)
{
List<BundleInfo> downloadList = new List<BundleInfo>();
var operation = new PatchDownloaderOperation(downloadList, downloadingMaxNumber, failedTryAgain, timeout);
return operation;
}
PatchDownloaderOperation IPlayModeServices.CreatePatchDownloaderByPaths(AssetInfo[] assetInfos, int downloadingMaxNumber, int failedTryAgain, int timeout)
{
List<BundleInfo> downloadList = new List<BundleInfo>();
var operation = new PatchDownloaderOperation(downloadList, downloadingMaxNumber, failedTryAgain, timeout);
return operation;
}
PatchUnpackerOperation IPlayModeServices.CreatePatchUnpackerByTags(string[] tags, int upackingMaxNumber, int failedTryAgain, int timeout)
{
List<BundleInfo> downloadList = new List<BundleInfo>();
var operation = new PatchUnpackerOperation(downloadList, upackingMaxNumber, failedTryAgain, int.MaxValue);
return operation;
}
PatchUnpackerOperation IPlayModeServices.CreatePatchUnpackerByAll(int upackingMaxNumber, int failedTryAgain, int timeout)
{
List<BundleInfo> downloadList = new List<BundleInfo>();
var operation = new PatchUnpackerOperation(downloadList, upackingMaxNumber, failedTryAgain, int.MaxValue);
return operation;
}
#endregion
#region IBundleServices接口
private BundleInfo CreateBundleInfo(PatchBundle patchBundle)
{
if (patchBundle == null)
throw new Exception("Should never get here !");
// 查询沙盒资源
if (CacheSystem.IsCached(patchBundle))
{
BundleInfo bundleInfo = new BundleInfo(patchBundle, BundleInfo.ELoadMode.LoadFromCache);
return bundleInfo;
}
// 查询APP资源
{
BundleInfo bundleInfo = new BundleInfo(patchBundle, BundleInfo.ELoadMode.LoadFromStreaming);
return bundleInfo;
}
}
BundleInfo IBundleServices.GetBundleInfo(AssetInfo assetInfo)
{
if (assetInfo.IsInvalid)
throw new Exception("Should never get here !");
// 注意:如果补丁清单里未找到资源包会抛出异常!
var patchBundle = _activePatchManifest.GetMainPatchBundle(assetInfo.AssetPath);
return CreateBundleInfo(patchBundle);
}
BundleInfo[] IBundleServices.GetAllDependBundleInfos(AssetInfo assetInfo)
{
if (assetInfo.IsInvalid)
throw new Exception("Should never get here !");
// 注意:如果补丁清单里未找到资源包会抛出异常!
var depends = _activePatchManifest.GetAllDependencies(assetInfo.AssetPath);
List<BundleInfo> result = new List<BundleInfo>(depends.Length);
foreach (var patchBundle in depends)
{
BundleInfo bundleInfo = CreateBundleInfo(patchBundle);
result.Add(bundleInfo);
}
return result.ToArray();
}
AssetInfo[] IBundleServices.GetAssetInfos(string[] tags)
{
return _activePatchManifest.GetAssetsInfoByTags(tags);
}
PatchAsset IBundleServices.TryGetPatchAsset(string assetPath)
{
if (_activePatchManifest.TryGetPatchAsset(assetPath, out PatchAsset patchAsset))
return patchAsset;
else
return null;
}
string IBundleServices.MappingToAssetPath(string location)
{
return _activePatchManifest.MappingToAssetPath(location);
}
string IBundleServices.TryMappingToAssetPath(string location)
{
return _activePatchManifest.TryMappingToAssetPath(location);
}
string IBundleServices.GetPackageName()
{
return _packageName;
}
bool IBundleServices.IsIncludeBundleFile(string fileName)
{
return _activePatchManifest.IsIncludeBundleFile(fileName);
}
bool IBundleServices.IsServicesValid()
{
return _activePatchManifest != null;
}
#endregion
}
}