update runtime code

pull/62/head
hevinci 2022-12-25 00:25:41 +08:00
parent 16943d4590
commit dd824254a9
10 changed files with 342 additions and 170 deletions

View File

@ -13,6 +13,7 @@ namespace YooAsset
private readonly static Dictionary<string, SceneOperationHandle> _sceneHandles = new Dictionary<string, SceneOperationHandle>(100);
private static long _sceneCreateCount = 0;
private string _packageName;
private bool _simulationOnEditor;
private int _loadingMaxNumber;
public IDecryptionServices DecryptionServices { private set; get; }
@ -23,8 +24,9 @@ namespace YooAsset
/// 初始化
/// 注意在使用AssetSystem之前需要初始化
/// </summary>
public void Initialize(bool simulationOnEditor, int loadingMaxNumber, IDecryptionServices decryptionServices, IBundleServices bundleServices)
public void Initialize(string packageName, bool simulationOnEditor, int loadingMaxNumber, IDecryptionServices decryptionServices, IBundleServices bundleServices)
{
_packageName = packageName;
_simulationOnEditor = simulationOnEditor;
_loadingMaxNumber = loadingMaxNumber;
DecryptionServices = decryptionServices;
@ -184,7 +186,7 @@ namespace YooAsset
}
var handle = provider.CreateHandle<SceneOperationHandle>();
handle.PackageName = BundleServices.GetPackageName();
handle.PackageName = _packageName;
_sceneHandles.Add(providerGUID, handle);
return handle;
}
@ -300,7 +302,7 @@ namespace YooAsset
// 释放资源包下的所有场景
if (BundleServices.IsServicesValid())
{
string packageName = BundleServices.GetPackageName();
string packageName = _packageName;
List<string> removeList = new List<string>();
foreach (var valuePair in _sceneHandles)
{

View File

@ -87,7 +87,7 @@ namespace YooAsset
var editorSimulateModeImpl = new EditorSimulateModeImpl();
_bundleServices = editorSimulateModeImpl;
_playModeServices = editorSimulateModeImpl;
_assetSystemImpl.Initialize(true, parameters.AssetLoadingMaxNumber, parameters.DecryptionServices, _bundleServices);
_assetSystemImpl.Initialize(PackageName, true, parameters.AssetLoadingMaxNumber, parameters.DecryptionServices, _bundleServices);
var initializeParameters = parameters as EditorSimulateModeParameters;
initializeOperation = editorSimulateModeImpl.InitializeAsync(PackageName, initializeParameters.LocationToLower, initializeParameters.SimulatePatchManifestPath);
@ -97,7 +97,7 @@ namespace YooAsset
var offlinePlayModeImpl = new OfflinePlayModeImpl();
_bundleServices = offlinePlayModeImpl;
_playModeServices = offlinePlayModeImpl;
_assetSystemImpl.Initialize(false, parameters.AssetLoadingMaxNumber, parameters.DecryptionServices, _bundleServices);
_assetSystemImpl.Initialize(PackageName, false, parameters.AssetLoadingMaxNumber, parameters.DecryptionServices, _bundleServices);
var initializeParameters = parameters as OfflinePlayModeParameters;
initializeOperation = offlinePlayModeImpl.InitializeAsync(PackageName, initializeParameters.LocationToLower);
@ -107,7 +107,7 @@ namespace YooAsset
var hostPlayModeImpl = new HostPlayModeImpl();
_bundleServices = hostPlayModeImpl;
_playModeServices = hostPlayModeImpl;
_assetSystemImpl.Initialize(false, parameters.AssetLoadingMaxNumber, parameters.DecryptionServices, _bundleServices);
_assetSystemImpl.Initialize(PackageName, false, parameters.AssetLoadingMaxNumber, parameters.DecryptionServices, _bundleServices);
var initializeParameters = parameters as HostPlayModeParameters;
initializeOperation = hostPlayModeImpl.InitializeAsync(
@ -319,7 +319,7 @@ namespace YooAsset
{
DebugCheckInitialize();
string[] tags = new string[] { tag };
return _bundleServices.GetAssetInfos(tags);
return _playModeServices.ActiveManifest.GetAssetsInfoByTags(tags);
}
/// <summary>
@ -329,7 +329,7 @@ namespace YooAsset
public AssetInfo[] GetAssetInfos(string[] tags)
{
DebugCheckInitialize();
return _bundleServices.GetAssetInfos(tags);
return _playModeServices.ActiveManifest.GetAssetsInfoByTags(tags);
}
/// <summary>
@ -350,7 +350,7 @@ namespace YooAsset
public bool CheckLocationValid(string location)
{
DebugCheckInitialize();
string assetPath = _bundleServices.TryMappingToAssetPath(location);
string assetPath = _playModeServices.ActiveManifest.TryMappingToAssetPath(location);
return string.IsNullOrEmpty(assetPath) == false;
}
#endregion
@ -725,7 +725,15 @@ namespace YooAsset
// NOTE : 编辑器模拟模式下始终返回TRUE
if (_playMode == EPlayMode.EditorSimulateMode)
return true;
return _bundleServices.IsIncludeBundleFile(fileName);
return _playModeServices.ActiveManifest.IsIncludeBundleFile(fileName);
}
/// <summary>
/// 资源定位地址转换为资源信息类
/// </summary>
private AssetInfo ConvertLocationToAssetInfo(string location, System.Type assetType)
{
return _playModeServices.ActiveManifest.ConvertLocationToAssetInfo(location, assetType);
}
#endregion
@ -739,24 +747,6 @@ namespace YooAsset
throw new Exception($"Package initialize failed ! {_initializeError}");
}
[Conditional("DEBUG")]
private void DebugCheckLocation(string location)
{
if (string.IsNullOrEmpty(location) == false)
{
// 检查路径末尾是否有空格
int index = location.LastIndexOf(" ");
if (index != -1)
{
if (location.Length == index + 1)
YooLogger.Warning($"Found blank character in location : \"{location}\"");
}
if (location.IndexOfAny(System.IO.Path.GetInvalidPathChars()) >= 0)
YooLogger.Warning($"Found illegal character in location : \"{location}\"");
}
}
[Conditional("DEBUG")]
private void DebugCheckUpdateManifest()
{
@ -777,33 +767,5 @@ namespace YooAsset
return data;
}
#endregion
#region 私有方法
/// <summary>
/// 资源定位地址转换为资源信息类,失败时内部会发出错误日志。
/// </summary>
/// <returns>如果转换失败会返回一个无效的资源信息类</returns>
private AssetInfo ConvertLocationToAssetInfo(string location, System.Type assetType)
{
DebugCheckLocation(location);
string assetPath = _bundleServices.MappingToAssetPath(location);
PatchAsset patchAsset = _bundleServices.TryGetPatchAsset(assetPath);
if (patchAsset != null)
{
AssetInfo assetInfo = new AssetInfo(patchAsset, assetType);
return assetInfo;
}
else
{
string error;
if (string.IsNullOrEmpty(location))
error = $"The location is null or empty !";
else
error = $"The location is invalid : {location}";
AssetInfo assetInfo = new AssetInfo(error);
return assetInfo;
}
}
#endregion
}
}

View File

@ -0,0 +1,263 @@
using System;
using System.Collections;
using System.Collections.Generic;
namespace YooAsset
{
public abstract class PreDownloadPackageOperation : AsyncOperationBase
{
/// <summary>
/// 创建补丁下载器,用于下载更新指定资源版本所有的资源包文件
/// </summary>
/// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
/// <param name="failedTryAgain">下载失败的重试次数</param>
/// <param name="timeout">超时时间</param>
public virtual PatchDownloaderOperation CreatePatchDownloader(int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
{
return PatchDownloaderOperation.CreateEmptyDownloader(downloadingMaxNumber, failedTryAgain, timeout);
}
/// <summary>
/// 创建补丁下载器,用于下载更新资源标签指定的资源包文件
/// </summary>
/// <param name="tag">资源标签</param>
/// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
/// <param name="failedTryAgain">下载失败的重试次数</param>
/// <param name="timeout">超时时间</param>
public virtual PatchDownloaderOperation CreatePatchDownloader(string tag, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
{
return PatchDownloaderOperation.CreateEmptyDownloader(downloadingMaxNumber, failedTryAgain, timeout);
}
/// <summary>
/// 创建补丁下载器,用于下载更新资源标签指定的资源包文件
/// </summary>
/// <param name="tags">资源标签列表</param>
/// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
/// <param name="failedTryAgain">下载失败的重试次数</param>
/// <param name="timeout">超时时间</param>
public virtual PatchDownloaderOperation CreatePatchDownloader(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
{
return PatchDownloaderOperation.CreateEmptyDownloader(downloadingMaxNumber, failedTryAgain, timeout);
}
/// <summary>
/// 创建补丁下载器,用于下载更新指定的资源列表依赖的资源包文件
/// </summary>
/// <param name="locations">资源定位列表</param>
/// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
/// <param name="failedTryAgain">下载失败的重试次数</param>
/// <param name="timeout">超时时间</param>
public virtual PatchDownloaderOperation CreateBundleDownloader(string[] locations, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
{
return PatchDownloaderOperation.CreateEmptyDownloader(downloadingMaxNumber, failedTryAgain, timeout);
}
}
public class EditorPlayModePreDownloadPackageOperation : PreDownloadPackageOperation
{
internal override void Start()
{
Status = EOperationStatus.Succeed;
}
internal override void Update()
{
}
}
public class OfflinePlayModePreDownloadPackageOperation : PreDownloadPackageOperation
{
internal override void Start()
{
Status = EOperationStatus.Succeed;
}
internal override void Update()
{
}
}
public class HostPlayModePreDownloadPackageOperation : PreDownloadPackageOperation
{
private enum ESteps
{
None,
CheckActiveManifest,
TryLoadCacheManifest,
DownloadManifest,
LoadCacheManifest,
CheckDeserializeManifest,
Done,
}
private readonly HostPlayModeImpl _impl;
private readonly string _packageName;
private readonly string _packageVersion;
private readonly int _timeout;
private LoadCacheManifestOperation _tryLoadCacheManifestOp;
private LoadCacheManifestOperation _loadCacheManifestOp;
private DownloadManifestOperation _downloadManifestOp;
private PatchManifest _manifest;
private ESteps _steps = ESteps.None;
internal HostPlayModePreDownloadPackageOperation(HostPlayModeImpl impl, string packageName, string packageVersion, int timeout)
{
_impl = impl;
_packageName = packageName;
_packageVersion = packageVersion;
_timeout = timeout;
}
internal override void Start()
{
_steps = ESteps.CheckActiveManifest;
}
internal override void Update()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.CheckActiveManifest)
{
// 检测当前激活的清单对象
if (_impl.ActiveManifest != null)
{
if (_impl.ActiveManifest.PackageVersion == _packageVersion)
{
_manifest = _impl.ActiveManifest;
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
return;
}
}
_steps = ESteps.TryLoadCacheManifest;
}
if (_steps == ESteps.TryLoadCacheManifest)
{
if (_tryLoadCacheManifestOp == null)
{
_tryLoadCacheManifestOp = new LoadCacheManifestOperation(_packageName, _packageVersion);
OperationSystem.StartOperation(_tryLoadCacheManifestOp);
}
if (_tryLoadCacheManifestOp.IsDone == false)
return;
if (_tryLoadCacheManifestOp.Status == EOperationStatus.Succeed)
{
_manifest = _tryLoadCacheManifestOp.Manifest;
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.DownloadManifest;
}
}
if (_steps == ESteps.DownloadManifest)
{
if (_downloadManifestOp == null)
{
_downloadManifestOp = new DownloadManifestOperation(_impl, _packageName, _packageVersion, _timeout);
OperationSystem.StartOperation(_downloadManifestOp);
}
if (_downloadManifestOp.IsDone == false)
return;
if (_downloadManifestOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.LoadCacheManifest;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _downloadManifestOp.Error;
}
}
if (_steps == ESteps.LoadCacheManifest)
{
if (_loadCacheManifestOp == null)
{
_loadCacheManifestOp = new LoadCacheManifestOperation(_packageName, _packageVersion);
OperationSystem.StartOperation(_loadCacheManifestOp);
}
if (_loadCacheManifestOp.IsDone == false)
return;
if (_loadCacheManifestOp.Status == EOperationStatus.Succeed)
{
_manifest = _loadCacheManifestOp.Manifest;
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _loadCacheManifestOp.Error;
}
}
}
public override PatchDownloaderOperation CreatePatchDownloader(int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
{
if (Status != EOperationStatus.Succeed)
{
YooLogger.Warning($"{nameof(PreDownloadPackageOperation)} status is not succeed !");
return PatchDownloaderOperation.CreateEmptyDownloader(downloadingMaxNumber, failedTryAgain, timeout);
}
List<BundleInfo> downloadList = _impl.GetDownloadListByAll(_manifest);
var operation = new PatchDownloaderOperation(downloadList, downloadingMaxNumber, failedTryAgain, timeout);
return operation;
}
public override PatchDownloaderOperation CreatePatchDownloader(string tag, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
{
if (Status != EOperationStatus.Succeed)
{
YooLogger.Warning($"{nameof(PreDownloadPackageOperation)} status is not succeed !");
return PatchDownloaderOperation.CreateEmptyDownloader(downloadingMaxNumber, failedTryAgain, timeout);
}
List<BundleInfo> downloadList = _impl.GetDownloadListByTags(_manifest, new string[] { tag });
var operation = new PatchDownloaderOperation(downloadList, downloadingMaxNumber, failedTryAgain, timeout);
return operation;
}
public override PatchDownloaderOperation CreatePatchDownloader(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
{
if (Status != EOperationStatus.Succeed)
{
YooLogger.Warning($"{nameof(PreDownloadPackageOperation)} status is not succeed !");
return PatchDownloaderOperation.CreateEmptyDownloader(downloadingMaxNumber, failedTryAgain, timeout);
}
List<BundleInfo> downloadList = _impl.GetDownloadListByTags(_manifest, tags);
var operation = new PatchDownloaderOperation(downloadList, downloadingMaxNumber, failedTryAgain, timeout);
return operation;
}
public override PatchDownloaderOperation CreateBundleDownloader(string[] locations, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
{
if (Status != EOperationStatus.Succeed)
{
YooLogger.Warning($"{nameof(PreDownloadPackageOperation)} status is not succeed !");
return PatchDownloaderOperation.CreateEmptyDownloader(downloadingMaxNumber, failedTryAgain, timeout);
}
List<AssetInfo> assetInfos = new List<AssetInfo>(locations.Length);
foreach (var location in locations)
{
var assetInfo = _manifest.ConvertLocationToAssetInfo(location, null);
assetInfos.Add(assetInfo);
}
List<BundleInfo> downloadList = _impl.GetDownloadListByPaths(_manifest, assetInfos.ToArray());
var operation = new PatchDownloaderOperation(downloadList, downloadingMaxNumber, failedTryAgain, timeout);
return operation;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 51f7a5e97aff1a646972c4ea11612947
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,7 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
namespace YooAsset
{

View File

@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
@ -265,6 +266,31 @@ namespace YooAsset
return result.ToArray();
}
/// <summary>
/// 资源定位地址转换为资源信息类,失败时内部会发出错误日志。
/// </summary>
/// <returns>如果转换失败会返回一个无效的资源信息类</returns>
public AssetInfo ConvertLocationToAssetInfo(string location, System.Type assetType)
{
DebugCheckLocation(location);
string assetPath = MappingToAssetPath(location);
if (TryGetPatchAsset(assetPath, out PatchAsset patchAsset))
{
AssetInfo assetInfo = new AssetInfo(patchAsset, assetType);
return assetInfo;
}
else
{
string error;
if (string.IsNullOrEmpty(location))
error = $"The location is null or empty !";
else
error = $"The location is invalid : {location}";
AssetInfo assetInfo = new AssetInfo(error);
return assetInfo;
}
}
/// <summary>
/// 生成Bundle文件的正式名称
@ -291,5 +317,25 @@ namespace YooAsset
throw new NotImplementedException($"Invalid name style : {nameStyle}");
}
}
#region 调试方法
[Conditional("DEBUG")]
private void DebugCheckLocation(string location)
{
if (string.IsNullOrEmpty(location) == false)
{
// 检查路径末尾是否有空格
int index = location.LastIndexOf(" ");
if (index != -1)
{
if (location.Length == index + 1)
YooLogger.Warning($"Found blank character in location : \"{location}\"");
}
if (location.IndexOfAny(System.IO.Path.GetInvalidPathChars()) >= 0)
YooLogger.Warning($"Found illegal character in location : \"{location}\"");
}
}
#endregion
}
}

View File

@ -58,7 +58,7 @@ namespace YooAsset
OperationSystem.StartOperation(operation);
return operation;
}
PatchDownloaderOperation IPlayModeServices.CreatePatchDownloaderByAll(int downloadingMaxNumber, int failedTryAgain, int timeout)
{
return PatchDownloaderOperation.CreateEmptyDownloader(downloadingMaxNumber, failedTryAgain, timeout);
@ -97,33 +97,6 @@ namespace YooAsset
{
throw new NotImplementedException();
}
AssetInfo[] IBundleServices.GetAssetInfos(string[] tags)
{
return _activeManifest.GetAssetsInfoByTags(tags);
}
PatchAsset IBundleServices.TryGetPatchAsset(string assetPath)
{
if (_activeManifest.TryGetPatchAsset(assetPath, out PatchAsset patchAsset))
return patchAsset;
else
return null;
}
string IBundleServices.MappingToAssetPath(string location)
{
return _activeManifest.MappingToAssetPath(location);
}
string IBundleServices.TryMappingToAssetPath(string location)
{
return _activeManifest.TryMappingToAssetPath(location);
}
string IBundleServices.GetPackageName()
{
return _packageName;
}
bool IBundleServices.IsIncludeBundleFile(string fileName)
{
return _activeManifest.IsIncludeBundleFile(fileName);
}
bool IBundleServices.IsServicesValid()
{
return _activeManifest != null;

View File

@ -185,7 +185,7 @@ namespace YooAsset
var operation = new PatchDownloaderOperation(downloadList, downloadingMaxNumber, failedTryAgain, timeout);
return operation;
}
private List<BundleInfo> GetDownloadListByPaths(PatchManifest patchManifest, AssetInfo[] assetInfos)
public List<BundleInfo> GetDownloadListByPaths(PatchManifest patchManifest, AssetInfo[] assetInfos)
{
// 获取资源对象的资源包和所有依赖资源包
List<PatchBundle> checkList = new List<PatchBundle>();
@ -328,33 +328,6 @@ namespace YooAsset
}
return result.ToArray();
}
AssetInfo[] IBundleServices.GetAssetInfos(string[] tags)
{
return _activeManifest.GetAssetsInfoByTags(tags);
}
PatchAsset IBundleServices.TryGetPatchAsset(string assetPath)
{
if (_activeManifest.TryGetPatchAsset(assetPath, out PatchAsset patchAsset))
return patchAsset;
else
return null;
}
string IBundleServices.MappingToAssetPath(string location)
{
return _activeManifest.MappingToAssetPath(location);
}
string IBundleServices.TryMappingToAssetPath(string location)
{
return _activeManifest.TryMappingToAssetPath(location);
}
string IBundleServices.GetPackageName()
{
return _packageName;
}
bool IBundleServices.IsIncludeBundleFile(string fileName)
{
return _activeManifest.IsIncludeBundleFile(fileName);
}
bool IBundleServices.IsServicesValid()
{
return _activeManifest != null;

View File

@ -125,33 +125,6 @@ namespace YooAsset
}
return result.ToArray();
}
AssetInfo[] IBundleServices.GetAssetInfos(string[] tags)
{
return _activeManifest.GetAssetsInfoByTags(tags);
}
PatchAsset IBundleServices.TryGetPatchAsset(string assetPath)
{
if (_activeManifest.TryGetPatchAsset(assetPath, out PatchAsset patchAsset))
return patchAsset;
else
return null;
}
string IBundleServices.MappingToAssetPath(string location)
{
return _activeManifest.MappingToAssetPath(location);
}
string IBundleServices.TryMappingToAssetPath(string location)
{
return _activeManifest.TryMappingToAssetPath(location);
}
string IBundleServices.GetPackageName()
{
return _packageName;
}
bool IBundleServices.IsIncludeBundleFile(string fileName)
{
return _activeManifest.IsIncludeBundleFile(fileName);
}
bool IBundleServices.IsServicesValid()
{
return _activeManifest != null;

View File

@ -13,36 +13,6 @@ namespace YooAsset
/// </summary>
BundleInfo[] GetAllDependBundleInfos(AssetInfo assetPath);
/// <summary>
/// 获取资源信息列表
/// </summary>
AssetInfo[] GetAssetInfos(string[] tags);
/// <summary>
/// 尝试获取补丁资源
/// </summary>
PatchAsset TryGetPatchAsset(string assetPath);
/// <summary>
/// 映射为资源路径
/// </summary>
string MappingToAssetPath(string location);
/// <summary>
/// 尝试映射为资源路径
/// </summary>
string TryMappingToAssetPath(string location);
/// <summary>
/// 获取所属的包裹名
/// </summary>
string GetPackageName();
/// <summary>
/// 是否包含资源文件
/// </summary>
bool IsIncludeBundleFile(string fileName);
/// <summary>
/// 服务接口是否有效
/// </summary>