refactor: rename LoadRawFile API to LoadBundleFile with BundleFileHandle

This commit is contained in:
何冠峰
2026-05-13 17:35:16 +08:00
parent 6b23927f71
commit 322c4a9847
17 changed files with 111 additions and 111 deletions

View File

@@ -94,27 +94,27 @@ namespace YooAsset
}
#endregion
#region RawFileHandle -- GetRawFileData / GetRawFileText
public sealed partial class RawFileHandle
#region BundleFileHandle -- GetRawFileData / GetRawFileText
public sealed partial class BundleFileHandle
{
/// <summary>
/// v2.3: rawFileHandle.GetRawFileData()
/// </summary>
[Obsolete("Read file manually via GetRawFilePath().")]
[Obsolete("Read file manually via GetBundleFilePath().")]
public byte[] GetRawFileData()
{
if (CheckValidWithWarning() == false) return null;
return System.IO.File.ReadAllBytes(GetRawFilePath());
return System.IO.File.ReadAllBytes(GetBundleFilePath());
}
/// <summary>
/// v2.3: rawFileHandle.GetRawFileText()
/// </summary>
[Obsolete("Read file manually via GetRawFilePath().")]
[Obsolete("Read file manually via GetBundleFilePath().")]
public string GetRawFileText()
{
if (CheckValidWithWarning() == false) return null;
return System.IO.File.ReadAllText(GetRawFilePath());
return System.IO.File.ReadAllText(GetBundleFilePath());
}
}
#endregion

View File

@@ -2,13 +2,13 @@
namespace YooAsset
{
/// <summary>
/// 原生文件句柄,用于访问未经 Unity 处理的原始文件
/// 资源包文件句柄,用于持有已加载的资源包引用
/// </summary>
public sealed partial class RawFileHandle : HandleBase
public sealed partial class BundleFileHandle : HandleBase
{
private System.Action<RawFileHandle> _callback;
private System.Action<BundleFileHandle> _callback;
internal RawFileHandle(ProviderBase provider) : base(provider)
internal BundleFileHandle(ProviderBase provider) : base(provider)
{
}
internal override void InvokeCallback()
@@ -19,12 +19,12 @@ namespace YooAsset
/// <summary>
/// 当加载完成时触发
/// </summary>
public event System.Action<RawFileHandle> Completed
public event System.Action<BundleFileHandle> Completed
{
add
{
if (CheckValidWithWarning() == false)
throw new YooHandleInvalidException($"{nameof(RawFileHandle)} is invalid. It may have been released or the provider was destroyed.");
throw new YooHandleInvalidException($"{nameof(BundleFileHandle)} is invalid. It may have been released or the provider was destroyed.");
if (Provider.IsDone)
value.Invoke(this);
else
@@ -33,7 +33,7 @@ namespace YooAsset
remove
{
if (CheckValidWithWarning() == false)
throw new YooHandleInvalidException($"{nameof(RawFileHandle)} is invalid. It may have been released or the provider was destroyed.");
throw new YooHandleInvalidException($"{nameof(BundleFileHandle)} is invalid. It may have been released or the provider was destroyed.");
_callback -= value;
}
}
@@ -49,10 +49,10 @@ namespace YooAsset
}
/// <summary>
/// 获取原生文件的路径
/// 获取资源包文件的路径
/// </summary>
/// <returns>原生文件的磁盘路径</returns>
public string GetRawFilePath()
/// <returns>资源包文件的磁盘路径</returns>
public string GetBundleFilePath()
{
if (CheckValidWithWarning() == false)
return string.Empty;

View File

@@ -14,7 +14,7 @@ namespace YooAsset
{ typeof(SceneHandle), op => new SceneHandle(op) },
{ typeof(SubAssetsHandle), op => new SubAssetsHandle(op) },
{ typeof(AllAssetsHandle), op => new AllAssetsHandle(op) },
{ typeof(RawFileHandle), op => new RawFileHandle(op) }
{ typeof(BundleFileHandle), op => new BundleFileHandle(op) }
};
/// <summary>

View File

@@ -0,0 +1,17 @@
namespace YooAsset
{
/// <summary>
/// 资源包文件提供者,负责加载资源包文件。
/// </summary>
internal sealed class BundleFileProvider : ProviderBase
{
public BundleFileProvider(ResourceManager manager, string providerKey, AssetInfo assetInfo) : base(manager, providerKey, assetInfo)
{
}
protected override void InternalProcessBundleHandle()
{
SetSuccess();
}
}
}

View File

@@ -1,17 +0,0 @@
namespace YooAsset
{
/// <summary>
/// 原生文件提供者,负责加载原始文件资源。
/// </summary>
internal sealed class RawFileProvider : ProviderBase
{
public RawFileProvider(ResourceManager manager, string providerKey, AssetInfo assetInfo) : base(manager, providerKey, assetInfo)
{
}
protected override void InternalProcessBundleHandle()
{
SetSuccess();
}
}
}

View File

@@ -305,12 +305,12 @@ namespace YooAsset
}
/// <summary>
/// 加载原生文件
/// 加载资源包文件
/// </summary>
/// <param name="assetInfo">资源信息</param>
/// <param name="priority">加载优先级</param>
/// <returns>原生文件句柄</returns>
public RawFileHandle LoadRawFileAsync(AssetInfo assetInfo, uint priority)
/// <returns>资源包文件句柄</returns>
public BundleFileHandle LoadBundleFileAsync(AssetInfo assetInfo, uint priority)
{
if (IsLoadingLocked)
{
@@ -318,29 +318,29 @@ namespace YooAsset
YooLogger.LogError(error);
ErrorProvider errorProvider = new ErrorProvider(this, assetInfo);
errorProvider.SetCompletedWithError(error);
return errorProvider.CreateHandle<RawFileHandle>();
return errorProvider.CreateHandle<BundleFileHandle>();
}
if (assetInfo.IsValid == false)
{
YooLogger.LogError($"Failed to load raw file. Error: {assetInfo.Error}");
YooLogger.LogError($"Failed to load bundle file. Error: {assetInfo.Error}");
ErrorProvider errorProvider = new ErrorProvider(this, assetInfo);
errorProvider.SetCompletedWithError(assetInfo.Error);
return errorProvider.CreateHandle<RawFileHandle>();
return errorProvider.CreateHandle<BundleFileHandle>();
}
string providerKey = nameof(LoadRawFileAsync) + assetInfo.AssetKey;
string providerKey = nameof(LoadBundleFileAsync) + assetInfo.AssetKey;
ProviderBase provider = GetAssetProvider(providerKey);
if (provider == null)
{
provider = new RawFileProvider(this, providerKey, assetInfo);
provider = new BundleFileProvider(this, providerKey, assetInfo);
provider.InitProviderDebugInfo();
_providerDict.Add(providerKey, provider);
AsyncOperationSystem.StartOperation(PackageName, provider);
}
provider.Priority = priority;
return provider.CreateHandle<RawFileHandle>();
return provider.CreateHandle<BundleFileHandle>();
}
/// <summary>

View File

@@ -32,9 +32,9 @@ namespace YooAsset
LoadScene,
/// <summary>
/// 加载原生文件
/// 加载资源包文件
/// </summary>
LoadRawFile,
LoadBundleFile,
}
/// <summary>

View File

@@ -419,60 +419,60 @@ namespace YooAsset
}
#endregion
#region
#region
/// <summary>
/// 同步加载原生文件
/// 同步加载资源包文件
/// </summary>
/// <param name="assetInfo">资源信息</param>
/// <returns>返回原生文件操作句柄</returns>
public RawFileHandle LoadRawFileSync(AssetInfo assetInfo)
/// <returns>返回资源包文件操作句柄</returns>
public BundleFileHandle LoadBundleFileSync(AssetInfo assetInfo)
{
CheckInitialized();
return LoadRawFileInternal(assetInfo, true, 0);
return LoadBundleFileInternal(assetInfo, true, 0);
}
/// <summary>
/// 同步加载原生文件
/// 同步加载资源包文件
/// </summary>
/// <param name="location">资源的定位地址</param>
/// <returns>返回原生文件操作句柄</returns>
public RawFileHandle LoadRawFileSync(string location)
/// <returns>返回资源包文件操作句柄</returns>
public BundleFileHandle LoadBundleFileSync(string location)
{
CheckInitialized();
AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null);
return LoadRawFileInternal(assetInfo, true, 0);
return LoadBundleFileInternal(assetInfo, true, 0);
}
/// <summary>
/// 加载原生文件
/// 异步加载资源包文件
/// </summary>
/// <param name="assetInfo">资源信息</param>
/// <param name="priority">加载的优先级</param>
/// <returns>返回原生文件操作句柄</returns>
public RawFileHandle LoadRawFileAsync(AssetInfo assetInfo, uint priority = 0)
/// <returns>返回资源包文件操作句柄</returns>
public BundleFileHandle LoadBundleFileAsync(AssetInfo assetInfo, uint priority = 0)
{
CheckInitialized();
return LoadRawFileInternal(assetInfo, false, priority);
return LoadBundleFileInternal(assetInfo, false, priority);
}
/// <summary>
/// 加载原生文件
/// 异步加载资源包文件
/// </summary>
/// <param name="location">资源的定位地址</param>
/// <param name="priority">加载的优先级</param>
/// <returns>返回原生文件操作句柄</returns>
public RawFileHandle LoadRawFileAsync(string location, uint priority = 0)
/// <returns>返回资源包文件操作句柄</returns>
public BundleFileHandle LoadBundleFileAsync(string location, uint priority = 0)
{
CheckInitialized();
AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null);
return LoadRawFileInternal(assetInfo, false, priority);
return LoadBundleFileInternal(assetInfo, false, priority);
}
private RawFileHandle LoadRawFileInternal(AssetInfo assetInfo, bool waitForAsyncComplete, uint priority)
private BundleFileHandle LoadBundleFileInternal(AssetInfo assetInfo, bool waitForAsyncComplete, uint priority)
{
assetInfo.LoadMethod = ELoadMethod.LoadRawFile;
var handle = _resourceManager.LoadRawFileAsync(assetInfo, priority);
assetInfo.LoadMethod = ELoadMethod.LoadBundleFile;
var handle = _resourceManager.LoadBundleFileAsync(assetInfo, priority);
if (waitForAsyncComplete)
handle.WaitForAsyncComplete();
return handle;

View File

@@ -6,29 +6,29 @@ using NUnit.Framework;
using YooAsset;
/// <summary>
/// 测试原生文件句柄释放与重新加载
/// 测试 Bundle 文件句柄释放与重新加载
/// </summary>
/// <remarks>
/// 覆盖 API: LoadRawFileAsync / RawFileHandle.Release / UnloadUnusedAssetsAsync
/// 覆盖 API: LoadBundleFileAsync / BundleFileHandle.Release / UnloadUnusedAssetsAsync
/// 测试内容:
/// 1. 异步加载原生文件,验证加载成功
/// 2. 释放 RawFileHandle 引用,等待一帧
/// 1. 异步加载 Bundle 文件,验证加载成功
/// 2. 释放 BundleFileHandle 引用,等待一帧
/// 3. 调用 UnloadUnusedAssetsAsync 清理未使用资源
/// 4. 再次加载同一原生文件,验证加载成功且文件路径有效
/// 4. 再次加载同一 Bundle 文件,验证加载成功且文件路径有效
/// </remarks>
public class TestRawFileRelease
public class TestBundleFileRelease
{
public IEnumerator RuntimeTester()
{
ResourcePackage package = YooAssets.GetPackage(TestConsts.RawBundlePackageName);
Assert.IsNotNull(package);
var rawFileHandle = package.LoadRawFileAsync("raw_file_e");
yield return rawFileHandle;
Assert.AreEqual(EOperationStatus.Succeeded, rawFileHandle.Status);
var bundleFileHandle = package.LoadBundleFileAsync("raw_file_e");
yield return bundleFileHandle;
Assert.AreEqual(EOperationStatus.Succeeded, bundleFileHandle.Status);
// 释放
rawFileHandle.Release();
bundleFileHandle.Release();
yield return new WaitForEndOfFrame();
var unloadOp = package.UnloadUnusedAssetsAsync();
@@ -36,10 +36,10 @@ public class TestRawFileRelease
Assert.AreEqual(EOperationStatus.Succeeded, unloadOp.Status);
// 再次加载
var reloadHandle = package.LoadRawFileAsync("raw_file_e");
var reloadHandle = package.LoadBundleFileAsync("raw_file_e");
yield return reloadHandle;
Assert.AreEqual(EOperationStatus.Succeeded, reloadHandle.Status);
Assert.IsTrue(File.Exists(reloadHandle.GetRawFilePath()));
Assert.IsTrue(File.Exists(reloadHandle.GetBundleFilePath()));
reloadHandle.Release();
}
}

View File

@@ -7,17 +7,17 @@ using NUnit.Framework;
using YooAsset;
/// <summary>
/// 测试原生文件加载
/// 测试 Bundle 文件加载
/// </summary>
/// <remarks>
/// 覆盖 API: LoadRawFileAsync / LoadRawFileSync / LoadAssetAsync(RawFileObject) / LoadAssetSync(RawFileObject)
/// 覆盖 API: LoadBundleFileAsync / LoadBundleFileSync / LoadAssetAsync(RawFileObject) / LoadAssetSync(RawFileObject)
/// 测试内容:
/// 1. 异步加载原生文件获取文件路径验证文件存在且二进制数据非空raw_file_a
/// 2. 同步加载原生文件获取文件路径验证文件存在且二进制数据非空raw_file_b
/// 1. 异步加载 Bundle 文件获取文件路径验证文件存在且二进制数据非空raw_file_a
/// 2. 同步加载 Bundle 文件获取文件路径验证文件存在且二进制数据非空raw_file_b
/// 3. 异步通过 RawFileObject 加载,验证 GetBytes() 和 GetText() 均返回有效数据raw_file_c
/// 4. 同步通过 RawFileObject 加载,验证 GetBytes() 和 GetText() 均返回有效数据raw_file_d
/// </remarks>
public class TestLoadRawFile
public class TestLoadBundleFile
{
public IEnumerator RuntimeTester()
{
@@ -26,33 +26,33 @@ public class TestLoadRawFile
// 测试异步加载
{
var rawFileHandle = package.LoadRawFileAsync("raw_file_a");
yield return rawFileHandle;
Assert.AreEqual(EOperationStatus.Succeeded, rawFileHandle.Status);
var bundleFileHandle = package.LoadBundleFileAsync("raw_file_a");
yield return bundleFileHandle;
Assert.AreEqual(EOperationStatus.Succeeded, bundleFileHandle.Status);
var filePath = rawFileHandle.GetRawFilePath();
var filePath = bundleFileHandle.GetBundleFilePath();
Assert.IsNotNull(filePath);
Assert.IsTrue(File.Exists(filePath));
byte[] fileBytes = File.ReadAllBytes(filePath);
Assert.IsNotNull(fileBytes);
Assert.Greater(fileBytes.Length, 0);
rawFileHandle.Release();
bundleFileHandle.Release();
}
// 测试同步加载
{
var rawFileHandle = package.LoadRawFileSync("raw_file_b");
Assert.AreEqual(EOperationStatus.Succeeded, rawFileHandle.Status);
var bundleFileHandle = package.LoadBundleFileSync("raw_file_b");
Assert.AreEqual(EOperationStatus.Succeeded, bundleFileHandle.Status);
var filePath = rawFileHandle.GetRawFilePath();
var filePath = bundleFileHandle.GetBundleFilePath();
Assert.IsNotNull(filePath);
Assert.IsTrue(File.Exists(filePath));
byte[] fileBytes = File.ReadAllBytes(filePath);
Assert.IsNotNull(fileBytes);
Assert.Greater(fileBytes.Length, 0);
rawFileHandle.Release();
bundleFileHandle.Release();
}
// 测试异步加载:通过 RawFileObject 获取二进制数据和文本数据

View File

@@ -241,9 +241,9 @@ public class T1_TestEditorFileSystem : IPrebuildSetup, IPostBuildCleanup
}
[UnityTest]
public IEnumerator B10_TestLoadRawFile()
public IEnumerator B10_TestLoadBundleFile()
{
var tester = new TestLoadRawFile();
var tester = new TestLoadBundleFile();
yield return tester.RuntimeTester();
}
@@ -304,9 +304,9 @@ public class T1_TestEditorFileSystem : IPrebuildSetup, IPostBuildCleanup
}
[UnityTest]
public IEnumerator C07_TestRawFileRelease()
public IEnumerator C07_TestBundleFileRelease()
{
var tester = new TestRawFileRelease();
var tester = new TestBundleFileRelease();
yield return tester.RuntimeTester();
}

View File

@@ -241,9 +241,9 @@ public class T2_TestBuiltinFileSystem : IPrebuildSetup, IPostBuildCleanup
}
[UnityTest]
public IEnumerator B10_TestLoadRawFile()
public IEnumerator B10_TestLoadBundleFile()
{
var tester = new TestLoadRawFile();
var tester = new TestLoadBundleFile();
yield return tester.RuntimeTester();
}
@@ -333,9 +333,9 @@ public class T2_TestBuiltinFileSystem : IPrebuildSetup, IPostBuildCleanup
}
[UnityTest]
public IEnumerator D07_TestRawFileRelease()
public IEnumerator D07_TestBundleFileRelease()
{
var tester = new TestRawFileRelease();
var tester = new TestBundleFileRelease();
yield return tester.RuntimeTester();
}

View File

@@ -101,8 +101,8 @@ namespace Cysharp.Threading.Tasks
case SubAssetsHandle sub_asset_handle:
sub_asset_handle.Completed += result.SubContinuation;
break;
case RawFileHandle raw_file_handle:
raw_file_handle.Completed += result.RawFileContinuation;
case BundleFileHandle bundle_file_handle:
bundle_file_handle.Completed += result.BundleFileContinuation;
break;
case AllAssetsHandle all_assets_handle:
all_assets_handle.Completed += result.AllAssetsContinuation;
@@ -120,8 +120,8 @@ namespace Cysharp.Threading.Tasks
case SubAssetsHandle sub_asset_handle:
sub_asset_handle.Completed += result.completedCallback;
break;
case RawFileHandle raw_file_handle:
raw_file_handle.Completed += result.completedCallback;
case BundleFileHandle bundle_file_handle:
bundle_file_handle.Completed += result.completedCallback;
break;
case AllAssetsHandle all_assets_handle:
all_assets_handle.Completed += result.completedCallback;
@@ -137,7 +137,7 @@ namespace Cysharp.Threading.Tasks
void AssetContinuation(AssetHandle _) => HandleCompleted(null);
void SceneContinuation(SceneHandle _) => HandleCompleted(null);
void SubContinuation(SubAssetsHandle _) => HandleCompleted(null);
void RawFileContinuation(RawFileHandle _) => HandleCompleted(null);
void BundleFileContinuation(BundleFileHandle _) => HandleCompleted(null);
void AllAssetsContinuation(AllAssetsHandle _) => HandleCompleted(null);
#endif
@@ -174,8 +174,8 @@ namespace Cysharp.Threading.Tasks
case SubAssetsHandle sub_asset_handle:
sub_asset_handle.Completed -= SubContinuation;
break;
case RawFileHandle raw_file_handle:
raw_file_handle.Completed -= RawFileContinuation;
case BundleFileHandle bundle_file_handle:
bundle_file_handle.Completed -= BundleFileContinuation;
break;
case AllAssetsHandle all_assets_handle:
all_assets_handle.Completed -= AllAssetsContinuation;
@@ -193,8 +193,8 @@ namespace Cysharp.Threading.Tasks
case SubAssetsHandle sub_asset_handle:
sub_asset_handle.Completed -= completedCallback;
break;
case RawFileHandle raw_file_handle:
raw_file_handle.Completed -= completedCallback;
case BundleFileHandle bundle_file_handle:
bundle_file_handle.Completed -= completedCallback;
break;
case AllAssetsHandle all_assets_handle:
all_assets_handle.Completed -= completedCallback;