update test sample

增加cache file system单元测试
This commit is contained in:
何冠峰
2025-07-17 21:26:13 +08:00
parent ac7ee16017
commit db159428c6
42 changed files with 890 additions and 269 deletions
@@ -11,8 +11,8 @@ using YooAsset;
public class T2_TestBuldinFileSystem : IPrebuildSetup, IPostBuildCleanup
{
private const string ASSET_BUNDLE_PACKAGE_ROOT_KEY = "T2_ASSET_BUNDLE_PACKAGE_ROOT_KEY";
private const string RAW_BUNDLE_PACKAGE_ROOT_KEY = "T2_RAW_BUNDLE_PACKAGE_ROOT_KEY";
public const string ASSET_BUNDLE_PACKAGE_ROOT_KEY = "T2_ASSET_BUNDLE_PACKAGE_ROOT_KEY";
public const string RAW_BUNDLE_PACKAGE_ROOT_KEY = "T2_RAW_BUNDLE_PACKAGE_ROOT_KEY";
void IPrebuildSetup.Setup()
{
@@ -24,7 +24,7 @@ public class T2_TestBuldinFileSystem : IPrebuildSetup, IPostBuildCleanup
buildParams.InvokeAssmeblyName = "YooAsset.Test.Editor";
buildParams.InvokeClassFullName = "TestPackageBuilder";
buildParams.InvokeMethodName = "BuildPackage";
var simulateResult = PakcageInvokeBuilder.InvokeBuilder(buildParams);
var simulateResult = PackageInvokeBuilder.InvokeBuilder(buildParams);
UnityEditor.EditorPrefs.SetString(ASSET_BUNDLE_PACKAGE_ROOT_KEY, simulateResult.PackageRootDirectory);
}
@@ -35,7 +35,7 @@ public class T2_TestBuldinFileSystem : IPrebuildSetup, IPostBuildCleanup
buildParams.InvokeAssmeblyName = "YooAsset.Test.Editor";
buildParams.InvokeClassFullName = "TestPackageBuilder";
buildParams.InvokeMethodName = "BuildPackage";
var simulateResult = PakcageInvokeBuilder.InvokeBuilder(buildParams);
var simulateResult = PackageInvokeBuilder.InvokeBuilder(buildParams);
UnityEditor.EditorPrefs.SetString(RAW_BUNDLE_PACKAGE_ROOT_KEY, simulateResult.PackageRootDirectory);
}
#endif
@@ -6,6 +6,9 @@ using UnityEngine;
using NUnit.Framework;
using YooAsset;
/// <summary>
/// 测试加载加密文件
/// </summary>
public class TestBundleEncryption
{
public IEnumerator RuntimeTester()
@@ -39,245 +42,27 @@ public class TestBundleEncryption
Assert.IsNotNull(audioSource.clip);
}
// 试听三秒钟
yield return new WaitForSeconds(3f);
yield return new WaitForSeconds(1f);
}
}
/// <summary>
/// 文件流加密方式
/// </summary>
public class TestFileStreamEncryption : IEncryptionServices
/* 资源代码流程
* 内置文件解压(加载器)
BuildinFileSystem::LoadBundleFile()
{
public EncryptResult Encrypt(EncryptFileInfo fileInfo)
{
// 说明:对TestRes3资源目录进行加密
if (fileInfo.BundleName.Contains("_testres3_"))
{
var fileData = File.ReadAllBytes(fileInfo.FileLoadPath);
for (int i = 0; i < fileData.Length; i++)
{
fileData[i] ^= BundleStream.KEY;
}
EncryptResult result = new EncryptResult();
result.Encrypted = true;
result.EncryptedData = fileData;
return result;
}
else
{
EncryptResult result = new EncryptResult();
result.Encrypted = false;
return result;
}
}
_unpackFileSystem.LoadBundleFile(bundle);
}
/// <summary>
/// 文件偏移加密方式
/// </summary>
public class TestFileOffsetEncryption : IEncryptionServices
UnpackFileSystem::LoadAssetBundleOperation()
{
public EncryptResult Encrypt(EncryptFileInfo fileInfo)
{
// 说明:对TestRes3资源目录进行加密
if (fileInfo.BundleName.Contains("_testres3_"))
{
int offset = 32;
byte[] fileData = File.ReadAllBytes(fileInfo.FileLoadPath);
var encryptedData = new byte[fileData.Length + offset];
Buffer.BlockCopy(fileData, 0, encryptedData, offset, fileData.Length);
EncryptResult result = new EncryptResult();
result.Encrypted = true;
result.EncryptedData = encryptedData;
return result;
}
else
{
EncryptResult result = new EncryptResult();
result.Encrypted = false;
return result;
}
}
DownloadFileOptions options = new DownloadFileOptions(int.MaxValue, 60);
_unpackFileSystem.DownloadFileAsync(_bundle, options);
}
/// <summary>
/// 资源文件解密流
/// </summary>
public class BundleStream : FileStream
UnpackFileSystem::DownloadFileAsync()
{
public const byte KEY = 64;
public BundleStream(string path, FileMode mode, FileAccess access, FileShare share) : base(path, mode, access, share)
{
}
public BundleStream(string path, FileMode mode) : base(path, mode)
{
}
public override int Read(byte[] array, int offset, int count)
{
var index = base.Read(array, offset, count);
for (int i = 0; i < array.Length; i++)
{
array[i] ^= KEY;
}
return index;
}
//RemoteServices返回内置文件路径
string mainURL = RemoteServices.GetRemoteMainURL(bundle.FileName);
string fallbackURL = RemoteServices.GetRemoteFallbackURL(bundle.FileName);
options.SetURL(mainURL, fallbackURL);
return new DownloadPackageBundleOperation(bundle, options);
}
/// <summary>
/// 资源文件流解密类
/// </summary>
public class TestFileStreamDecryption : IDecryptionServices
{
/// <summary>
/// 同步方式获取解密的资源包对象
/// 注意:加载流对象在资源包对象释放的时候会自动释放
/// </summary>
DecryptResult IDecryptionServices.LoadAssetBundle(DecryptFileInfo fileInfo)
{
BundleStream bundleStream = new BundleStream(fileInfo.FileLoadPath, FileMode.Open, FileAccess.Read, FileShare.Read);
DecryptResult decryptResult = new DecryptResult();
decryptResult.ManagedStream = bundleStream;
decryptResult.Result = AssetBundle.LoadFromStream(bundleStream, fileInfo.FileLoadCRC, GetManagedReadBufferSize());
return decryptResult;
}
/// <summary>
/// 异步方式获取解密的资源包对象
/// 注意:加载流对象在资源包对象释放的时候会自动释放
/// </summary>
DecryptResult IDecryptionServices.LoadAssetBundleAsync(DecryptFileInfo fileInfo)
{
BundleStream bundleStream = new BundleStream(fileInfo.FileLoadPath, FileMode.Open, FileAccess.Read, FileShare.Read);
DecryptResult decryptResult = new DecryptResult();
decryptResult.ManagedStream = bundleStream;
decryptResult.CreateRequest = AssetBundle.LoadFromStreamAsync(bundleStream, fileInfo.FileLoadCRC, GetManagedReadBufferSize());
return decryptResult;
}
/// <summary>
/// 后备方式获取解密的资源包对象
/// </summary>
DecryptResult IDecryptionServices.LoadAssetBundleFallback(DecryptFileInfo fileInfo)
{
return new DecryptResult();
}
/// <summary>
/// 获取解密的字节数据
/// </summary>
byte[] IDecryptionServices.ReadFileData(DecryptFileInfo fileInfo)
{
throw new System.NotImplementedException();
}
/// <summary>
/// 获取解密的文本数据
/// </summary>
string IDecryptionServices.ReadFileText(DecryptFileInfo fileInfo)
{
throw new System.NotImplementedException();
}
private static uint GetManagedReadBufferSize()
{
return 1024;
}
}
/// <summary>
/// 资源文件偏移解密类
/// </summary>
public class TestFileOffsetDecryption : IDecryptionServices
{
/// <summary>
/// 同步方式获取解密的资源包对象
/// 注意:加载流对象在资源包对象释放的时候会自动释放
/// </summary>
DecryptResult IDecryptionServices.LoadAssetBundle(DecryptFileInfo fileInfo)
{
DecryptResult decryptResult = new DecryptResult();
decryptResult.ManagedStream = null;
decryptResult.Result = AssetBundle.LoadFromFile(fileInfo.FileLoadPath, fileInfo.FileLoadCRC, GetFileOffset());
return decryptResult;
}
/// <summary>
/// 异步方式获取解密的资源包对象
/// 注意:加载流对象在资源包对象释放的时候会自动释放
/// </summary>
DecryptResult IDecryptionServices.LoadAssetBundleAsync(DecryptFileInfo fileInfo)
{
DecryptResult decryptResult = new DecryptResult();
decryptResult.ManagedStream = null;
decryptResult.CreateRequest = AssetBundle.LoadFromFileAsync(fileInfo.FileLoadPath, fileInfo.FileLoadCRC, GetFileOffset());
return decryptResult;
}
/// <summary>
/// 后备方式获取解密的资源包对象
/// </summary>
DecryptResult IDecryptionServices.LoadAssetBundleFallback(DecryptFileInfo fileInfo)
{
return new DecryptResult();
}
/// <summary>
/// 获取解密的字节数据
/// </summary>
byte[] IDecryptionServices.ReadFileData(DecryptFileInfo fileInfo)
{
throw new System.NotImplementedException();
}
/// <summary>
/// 获取解密的文本数据
/// </summary>
string IDecryptionServices.ReadFileText(DecryptFileInfo fileInfo)
{
throw new System.NotImplementedException();
}
private static ulong GetFileOffset()
{
return 32;
}
}
/// <summary>
/// WebGL平台解密类
/// 注意:WebGL平台支持内存解密
/// </summary>
public class TestWebFileStreamDecryption : IWebDecryptionServices
{
public WebDecryptResult LoadAssetBundle(WebDecryptFileInfo fileInfo)
{
/*
byte[] copyData = new byte[fileInfo.FileData.Length];
Buffer.BlockCopy(fileInfo.FileData, 0, copyData, 0, fileInfo.FileData.Length);
for (int i = 0; i < copyData.Length; i++)
{
copyData[i] ^= BundleStream.KEY;
}
WebDecryptResult decryptResult = new WebDecryptResult();
decryptResult.Result = AssetBundle.LoadFromMemory(copyData);
return decryptResult;
*/
for (int i = 0; i < fileInfo.FileData.Length; i++)
{
fileInfo.FileData[i] ^= BundleStream.KEY;
}
WebDecryptResult decryptResult = new WebDecryptResult();
decryptResult.Result = AssetBundle.LoadFromMemory(fileInfo.FileData);
return decryptResult;
}
}
*/
@@ -0,0 +1,47 @@
using System;
using System.IO;
using System.Text;
using System.Collections;
using UnityEngine;
using NUnit.Framework;
using YooAsset;
/// <summary>
/// 测试内置文件解压
/// </summary>
public class TestBundleUnpacker
{
public IEnumerator RuntimeTester()
{
ResourcePackage package = YooAssets.GetPackage(TestDefine.AssetBundlePackageName);
Assert.IsNotNull(package);
var resourceUnpacker = package.CreateResourceUnpacker("unpack", 10, 1);
Assert.AreEqual(resourceUnpacker.TotalDownloadCount, 2);
yield return resourceUnpacker;
Assert.AreEqual(EOperationStatus.Succeed, resourceUnpacker.Status);
// 等待一秒
yield return new WaitForSeconds(1f);
}
}
/* 资源代码流程
* 内置文件解压(解压器)
BundleInfo::CreateDownloader()
{
return _buildFileSystem.DownloadFileAsync(Bundle, options);
}
BuildinFileSystem::DownloadFileAsync()
{
options.ImportFilePath = xxxxxx;
_unpackFileSystem.DownloadFileAsync(bundle, options);
}
UnpackFileSystem::DownloadFileAsync()
{
string mainURL = ConvertToWWWPath(options.ImportFilePath);
options.SetURL(mainURL, mainURL);
return new DownloadPackageBundleOperation(bundle, options);
}
*/
@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 437432f4d3838124085c13253d0c0094
guid: 5e56d0784f1af504c821e67a5ba81862
MonoImporter:
externalObjects: {}
serializedVersion: 2
@@ -1,55 +0,0 @@
using System;
using System.IO;
using System.Text;
using System.Collections;
using UnityEngine;
using NUnit.Framework;
using YooAsset;
public class TestProcessManifest : IManifestServices
{
public byte[] ProcessManifest(byte[] fileData)
{
return XorProcess(fileData, "YOO");
}
public byte[] RestoreManifest(byte[] fileData)
{
return XorProcess(fileData, "YOO");
}
/// <summary>
/// 使用异或加密/解密字节数组
/// </summary>
/// <param name="data">输入数据</param>
/// <param name="key">加密密钥</param>
/// <returns>处理后的字节数组</returns>
public static byte[] XorProcess(byte[] data, byte[] key)
{
if (data == null)
throw new ArgumentNullException(nameof(data));
if (key == null || key.Length == 0)
throw new ArgumentException("Key cannot be null or empty");
byte[] result = new byte[data.Length];
for (int i = 0; i < data.Length; i++)
{
// 循环使用密钥中的字节
result[i] = (byte)(data[i] ^ key[i % key.Length]);
}
return result;
}
/// <summary>
/// 使用字符串密钥进行异或处理(自动转换编码)
/// </summary>
/// <param name="data">输入数据</param>
/// <param name="key">字符串密钥</param>
/// <returns>处理后的字节数组</returns>
public static byte[] XorProcess(byte[] data, string key)
{
byte[] keyBytes = System.Text.Encoding.UTF8.GetBytes(key);
return XorProcess(data, keyBytes);
}
}