update runtime code

移除了ILocationServices接口类。
增加了bool CheckLocationValid(string location)方法。
pull/51/head
hevinci 2022-10-21 18:36:03 +08:00
parent d7760cd34d
commit bb64ff7278
16 changed files with 70 additions and 148 deletions

View File

@ -142,6 +142,7 @@ namespace YooAsset
{ {
if (assetInfo.IsInvalid) if (assetInfo.IsInvalid)
{ {
YooLogger.Error($"Failed to load scene. {assetInfo.Error}");
CompletedProvider completedProvider = new CompletedProvider(assetInfo); CompletedProvider completedProvider = new CompletedProvider(assetInfo);
completedProvider.SetCompleted(assetInfo.Error); completedProvider.SetCompleted(assetInfo.Error);
return completedProvider.CreateHandle<SceneOperationHandle>(); return completedProvider.CreateHandle<SceneOperationHandle>();
@ -190,6 +191,7 @@ namespace YooAsset
{ {
if (assetInfo.IsInvalid) if (assetInfo.IsInvalid)
{ {
YooLogger.Error($"Failed to load asset. {assetInfo.Error}");
CompletedProvider completedProvider = new CompletedProvider(assetInfo); CompletedProvider completedProvider = new CompletedProvider(assetInfo);
completedProvider.SetCompleted(assetInfo.Error); completedProvider.SetCompleted(assetInfo.Error);
return completedProvider.CreateHandle<AssetOperationHandle>(); return completedProvider.CreateHandle<AssetOperationHandle>();
@ -216,6 +218,7 @@ namespace YooAsset
{ {
if (assetInfo.IsInvalid) if (assetInfo.IsInvalid)
{ {
YooLogger.Error($"Failed to load sub assets. {assetInfo.Error}");
CompletedProvider completedProvider = new CompletedProvider(assetInfo); CompletedProvider completedProvider = new CompletedProvider(assetInfo);
completedProvider.SetCompleted(assetInfo.Error); completedProvider.SetCompleted(assetInfo.Error);
return completedProvider.CreateHandle<SubAssetsOperationHandle>(); return completedProvider.CreateHandle<SubAssetsOperationHandle>();

View File

@ -13,7 +13,6 @@ namespace YooAsset
private EOperationStatus _initializeStatus = EOperationStatus.None; private EOperationStatus _initializeStatus = EOperationStatus.None;
private EPlayMode _playMode; private EPlayMode _playMode;
private IBundleServices _bundleServices; private IBundleServices _bundleServices;
private ILocationServices _locationServices;
private AssetSystemImpl _assetSystemImpl; private AssetSystemImpl _assetSystemImpl;
private EditorSimulateModeImpl _editorSimulateModeImpl; private EditorSimulateModeImpl _editorSimulateModeImpl;
private OfflinePlayModeImpl _offlinePlayModeImpl; private OfflinePlayModeImpl _offlinePlayModeImpl;
@ -33,7 +32,7 @@ namespace YooAsset
} }
internal AssetsPackage() private AssetsPackage()
{ {
} }
internal AssetsPackage(string packageName) internal AssetsPackage(string packageName)
@ -62,7 +61,6 @@ namespace YooAsset
_initializeStatus = EOperationStatus.None; _initializeStatus = EOperationStatus.None;
_bundleServices = null; _bundleServices = null;
_locationServices = null;
_editorSimulateModeImpl = null; _editorSimulateModeImpl = null;
_offlinePlayModeImpl = null; _offlinePlayModeImpl = null;
_hostPlayModeImpl = null; _hostPlayModeImpl = null;
@ -72,8 +70,6 @@ namespace YooAsset
_assetSystemImpl.DestroyAll(); _assetSystemImpl.DestroyAll();
_assetSystemImpl = null; _assetSystemImpl = null;
} }
YooLogger.Log("YooAssets destroy all !");
} }
} }
@ -87,7 +83,6 @@ namespace YooAsset
// 初始化资源系统 // 初始化资源系统
InitializationOperation initializeOperation; InitializationOperation initializeOperation;
_locationServices = parameters.LocationServices;
_assetSystemImpl = new AssetSystemImpl(); _assetSystemImpl = new AssetSystemImpl();
if (_playMode == EPlayMode.EditorSimulateMode) if (_playMode == EPlayMode.EditorSimulateMode)
{ {
@ -143,9 +138,6 @@ namespace YooAsset
throw new Exception($"Editor simulate mode only support unity editor."); throw new Exception($"Editor simulate mode only support unity editor.");
#endif #endif
if (parameters.LocationServices == null)
throw new Exception($"{nameof(ILocationServices)} is null.");
if (parameters is EditorSimulateModeParameters) if (parameters is EditorSimulateModeParameters)
{ {
var editorSimulateModeParameters = parameters as EditorSimulateModeParameters; var editorSimulateModeParameters = parameters as EditorSimulateModeParameters;
@ -335,7 +327,10 @@ namespace YooAsset
DebugCheckInitialize(); DebugCheckInitialize();
AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null); AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null);
if (assetInfo.IsInvalid) if (assetInfo.IsInvalid)
{
YooLogger.Warning(assetInfo.Error);
return false; return false;
}
BundleInfo bundleInfo = _bundleServices.GetBundleInfo(assetInfo); BundleInfo bundleInfo = _bundleServices.GetBundleInfo(assetInfo);
if (bundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromRemote) if (bundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromRemote)
@ -397,14 +392,14 @@ namespace YooAsset
} }
/// <summary> /// <summary>
/// 获取资源路径 /// 检查资源定位地址是否有效
/// </summary> /// </summary>
/// <param name="location">资源的定位地址</param> /// <param name="location">资源的定位地址</param>
/// <returns>如果location地址无效则返回空字符串</returns> public bool CheckLocationValid(string location)
public string GetAssetPath(string location)
{ {
DebugCheckInitialize(); DebugCheckInitialize();
return _locationServices.ConvertLocationToAssetPath(this, location); string assetPath = _bundleServices.TryMappingToAssetPath(location);
return string.IsNullOrEmpty(assetPath) == false;
} }
#endregion #endregion
@ -429,8 +424,6 @@ namespace YooAsset
public RawFileOperation GetRawFileAsync(AssetInfo assetInfo, string copyPath = null) public RawFileOperation GetRawFileAsync(AssetInfo assetInfo, string copyPath = null)
{ {
DebugCheckInitialize(); DebugCheckInitialize();
if (assetInfo.IsInvalid)
YooLogger.Warning(assetInfo.Error);
return GetRawFileInternal(assetInfo, copyPath); return GetRawFileInternal(assetInfo, copyPath);
} }
@ -439,6 +432,7 @@ namespace YooAsset
{ {
if (assetInfo.IsInvalid) if (assetInfo.IsInvalid)
{ {
YooLogger.Error($"Failed to get raw file. {assetInfo.Error}");
RawFileOperation operation = new CompletedRawFileOperation(assetInfo.Error, copyPath); RawFileOperation operation = new CompletedRawFileOperation(assetInfo.Error, copyPath);
OperationSystem.StartOperation(operation); OperationSystem.StartOperation(operation);
return operation; return operation;
@ -508,8 +502,6 @@ namespace YooAsset
public SceneOperationHandle LoadSceneAsync(AssetInfo assetInfo, LoadSceneMode sceneMode = LoadSceneMode.Single, bool activateOnLoad = true, int priority = 100) public SceneOperationHandle LoadSceneAsync(AssetInfo assetInfo, LoadSceneMode sceneMode = LoadSceneMode.Single, bool activateOnLoad = true, int priority = 100)
{ {
DebugCheckInitialize(); DebugCheckInitialize();
if (assetInfo.IsInvalid)
YooLogger.Warning(assetInfo.Error);
var handle = _assetSystemImpl.LoadSceneAsync(assetInfo, sceneMode, activateOnLoad, priority); var handle = _assetSystemImpl.LoadSceneAsync(assetInfo, sceneMode, activateOnLoad, priority);
return handle; return handle;
} }
@ -523,8 +515,6 @@ namespace YooAsset
public AssetOperationHandle LoadAssetSync(AssetInfo assetInfo) public AssetOperationHandle LoadAssetSync(AssetInfo assetInfo)
{ {
DebugCheckInitialize(); DebugCheckInitialize();
if (assetInfo.IsInvalid)
YooLogger.Warning(assetInfo.Error);
return LoadAssetInternal(assetInfo, true); return LoadAssetInternal(assetInfo, true);
} }
@ -560,8 +550,6 @@ namespace YooAsset
public AssetOperationHandle LoadAssetAsync(AssetInfo assetInfo) public AssetOperationHandle LoadAssetAsync(AssetInfo assetInfo)
{ {
DebugCheckInitialize(); DebugCheckInitialize();
if (assetInfo.IsInvalid)
YooLogger.Warning(assetInfo.Error);
return LoadAssetInternal(assetInfo, false); return LoadAssetInternal(assetInfo, false);
} }
@ -593,14 +581,17 @@ namespace YooAsset
private AssetOperationHandle LoadAssetInternal(AssetInfo assetInfo, bool waitForAsyncComplete) private AssetOperationHandle LoadAssetInternal(AssetInfo assetInfo, bool waitForAsyncComplete)
{ {
#if UNITY_EDITOR #if UNITY_EDITOR
BundleInfo bundleInfo = _bundleServices.GetBundleInfo(assetInfo); if (assetInfo.IsInvalid == false)
if (bundleInfo.Bundle.IsRawFile)
{ {
string error = $"Cannot load raw file using LoadAsset method !"; BundleInfo bundleInfo = _bundleServices.GetBundleInfo(assetInfo);
YooLogger.Error(error); if (bundleInfo.Bundle.IsRawFile)
CompletedProvider completedProvider = new CompletedProvider(assetInfo); {
completedProvider.SetCompleted(error); string error = $"Cannot load raw file using LoadAsset method !";
return completedProvider.CreateHandle<AssetOperationHandle>(); YooLogger.Error(error);
CompletedProvider completedProvider = new CompletedProvider(assetInfo);
completedProvider.SetCompleted(error);
return completedProvider.CreateHandle<AssetOperationHandle>();
}
} }
#endif #endif
@ -619,8 +610,6 @@ namespace YooAsset
public SubAssetsOperationHandle LoadSubAssetsSync(AssetInfo assetInfo) public SubAssetsOperationHandle LoadSubAssetsSync(AssetInfo assetInfo)
{ {
DebugCheckInitialize(); DebugCheckInitialize();
if (assetInfo.IsInvalid)
YooLogger.Warning(assetInfo.Error);
return LoadSubAssetsInternal(assetInfo, true); return LoadSubAssetsInternal(assetInfo, true);
} }
@ -656,8 +645,6 @@ namespace YooAsset
public SubAssetsOperationHandle LoadSubAssetsAsync(AssetInfo assetInfo) public SubAssetsOperationHandle LoadSubAssetsAsync(AssetInfo assetInfo)
{ {
DebugCheckInitialize(); DebugCheckInitialize();
if (assetInfo.IsInvalid)
YooLogger.Warning(assetInfo.Error);
return LoadSubAssetsInternal(assetInfo, false); return LoadSubAssetsInternal(assetInfo, false);
} }
@ -689,14 +676,17 @@ namespace YooAsset
private SubAssetsOperationHandle LoadSubAssetsInternal(AssetInfo assetInfo, bool waitForAsyncComplete) private SubAssetsOperationHandle LoadSubAssetsInternal(AssetInfo assetInfo, bool waitForAsyncComplete)
{ {
#if UNITY_EDITOR #if UNITY_EDITOR
BundleInfo bundleInfo = _bundleServices.GetBundleInfo(assetInfo); if (assetInfo.IsInvalid == false)
if (bundleInfo.Bundle.IsRawFile)
{ {
string error = $"Cannot load raw file using LoadSubAssets method !"; BundleInfo bundleInfo = _bundleServices.GetBundleInfo(assetInfo);
YooLogger.Error(error); if (bundleInfo.Bundle.IsRawFile)
CompletedProvider completedProvider = new CompletedProvider(assetInfo); {
completedProvider.SetCompleted(error); string error = $"Cannot load raw file using LoadSubAssets method !";
return completedProvider.CreateHandle<SubAssetsOperationHandle>(); YooLogger.Error(error);
CompletedProvider completedProvider = new CompletedProvider(assetInfo);
completedProvider.SetCompleted(error);
return completedProvider.CreateHandle<SubAssetsOperationHandle>();
}
} }
#endif #endif
@ -940,14 +930,6 @@ namespace YooAsset
#endregion #endregion
#region 内部方法 #region 内部方法
/// <summary>
/// 资源定位地址转换为资源完整路径
/// </summary>
internal string MappingToAssetPath(string location)
{
return _bundleServices.MappingToAssetPath(location);
}
/// <summary> /// <summary>
/// 是否包含资源文件 /// 是否包含资源文件
/// </summary> /// </summary>
@ -1014,7 +996,7 @@ namespace YooAsset
private AssetInfo ConvertLocationToAssetInfo(string location, System.Type assetType) private AssetInfo ConvertLocationToAssetInfo(string location, System.Type assetType)
{ {
DebugCheckLocation(location); DebugCheckLocation(location);
string assetPath = _locationServices.ConvertLocationToAssetPath(this, location); string assetPath = _bundleServices.MappingToAssetPath(location);
PatchAsset patchAsset = _bundleServices.TryGetPatchAsset(assetPath); PatchAsset patchAsset = _bundleServices.TryGetPatchAsset(assetPath);
if (patchAsset != null) if (patchAsset != null)
{ {
@ -1028,7 +1010,6 @@ namespace YooAsset
error = $"The location is null or empty !"; error = $"The location is null or empty !";
else else
error = $"The location is invalid : {location}"; error = $"The location is invalid : {location}";
YooLogger.Error(error);
AssetInfo assetInfo = new AssetInfo(error); AssetInfo assetInfo = new AssetInfo(error);
return assetInfo; return assetInfo;
} }

View File

@ -33,11 +33,6 @@ namespace YooAsset
/// </summary> /// </summary>
public bool LocationToLower = false; public bool LocationToLower = false;
/// <summary>
/// 资源定位服务接口
/// </summary>
public ILocationServices LocationServices = null;
/// <summary> /// <summary>
/// 文件解密服务接口 /// 文件解密服务接口
/// </summary> /// </summary>

View File

@ -147,6 +147,23 @@ namespace YooAsset
} }
} }
/// <summary>
/// 尝试映射为资源路径
/// </summary>
public string TryMappingToAssetPath(string location)
{
if (string.IsNullOrEmpty(location))
return string.Empty;
if (_locationToLower)
location = location.ToLower();
if (AssetPathMapping.TryGetValue(location, out string assetPath))
return assetPath;
else
return string.Empty;
}
/// <summary> /// <summary>
/// 获取主资源包 /// 获取主资源包
/// 注意:传入的资源路径一定合法有效! /// 注意:传入的资源路径一定合法有效!

View File

@ -66,6 +66,10 @@ namespace YooAsset
{ {
return _simulatePatchManifest.MappingToAssetPath(location); return _simulatePatchManifest.MappingToAssetPath(location);
} }
string IBundleServices.TryMappingToAssetPath(string location)
{
return _simulatePatchManifest.TryMappingToAssetPath(location);
}
string IBundleServices.GetPackageName() string IBundleServices.GetPackageName()
{ {
return _simulatePatchManifest.PackageName; return _simulatePatchManifest.PackageName;

View File

@ -392,6 +392,10 @@ namespace YooAsset
{ {
return LocalPatchManifest.MappingToAssetPath(location); return LocalPatchManifest.MappingToAssetPath(location);
} }
string IBundleServices.TryMappingToAssetPath(string location)
{
return LocalPatchManifest.TryMappingToAssetPath(location);
}
string IBundleServices.GetPackageName() string IBundleServices.GetPackageName()
{ {
return LocalPatchManifest.PackageName; return LocalPatchManifest.PackageName;

View File

@ -117,6 +117,10 @@ namespace YooAsset
{ {
return _appPatchManifest.MappingToAssetPath(location); return _appPatchManifest.MappingToAssetPath(location);
} }
string IBundleServices.TryMappingToAssetPath(string location)
{
return _appPatchManifest.TryMappingToAssetPath(location);
}
string IBundleServices.GetPackageName() string IBundleServices.GetPackageName()
{ {
return _appPatchManifest.PackageName; return _appPatchManifest.PackageName;

View File

@ -28,6 +28,11 @@ namespace YooAsset
/// </summary> /// </summary>
string MappingToAssetPath(string location); string MappingToAssetPath(string location);
/// <summary>
/// 尝试映射为资源路径
/// </summary>
string TryMappingToAssetPath(string location);
/// <summary> /// <summary>
/// 获取所属的包裹名 /// 获取所属的包裹名
/// </summary> /// </summary>

View File

@ -1,11 +0,0 @@

namespace YooAsset
{
public interface ILocationServices
{
/// <summary>
/// 定位地址转换为资源路径
/// </summary>
string ConvertLocationToAssetPath(AssetsPackage package, string location);
}
}

View File

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

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 6e400ee1e8b3556479bfa493ff7fe778
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,11 +0,0 @@

namespace YooAsset
{
public class AddressLocationServices : ILocationServices
{
string ILocationServices.ConvertLocationToAssetPath(AssetsPackage package, string location)
{
return package.MappingToAssetPath(location);
}
}
}

View File

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

View File

@ -1,27 +0,0 @@

namespace YooAsset
{
public class DefaultLocationServices : ILocationServices
{
private readonly string _resourceRoot;
public DefaultLocationServices(string resourceRoot)
{
if (string.IsNullOrEmpty(resourceRoot) == false)
_resourceRoot = PathHelper.GetRegularPath(resourceRoot);
}
string ILocationServices.ConvertLocationToAssetPath(AssetsPackage package, string location)
{
if (string.IsNullOrEmpty(_resourceRoot))
{
return package.MappingToAssetPath(location);
}
else
{
string tempLocation = $"{_resourceRoot}/{location}";
return package.MappingToAssetPath(tempLocation);
}
}
}
}

View File

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

View File

@ -70,14 +70,13 @@ namespace YooAsset
} }
/// <summary> /// <summary>
/// 获取资源路径 /// 检查资源定位地址是否有效
/// </summary> /// </summary>
/// <param name="location">资源的定位地址</param> /// <param name="location">资源的定位地址</param>
/// <returns>如果location地址无效则返回空字符串</returns> public static bool CheckLocationValid(string location)
public static string GetAssetPath(string location)
{ {
DebugCheckDefaultPackageValid(); DebugCheckDefaultPackageValid();
return _defaultPackage.GetAssetPath(location); return _defaultPackage.CheckLocationValid(location);
} }
#endregion #endregion