refactor : 重构代码

This commit is contained in:
何冠峰
2026-01-16 10:28:01 +08:00
parent 2b6e7856e7
commit b0e85017d2
100 changed files with 1042 additions and 1582 deletions
@@ -1,7 +1,7 @@

namespace YooAsset
{
internal class CatalogDefine
internal class CatalogFileDefine
{
/// <summary>
/// 文件极限大小(100MB)
@@ -5,14 +5,14 @@ using UnityEngine;
namespace YooAsset
{
internal static class CatalogTools
internal static class CatalogFileHelper
{
#if UNITY_EDITOR
/// <summary>
/// 生成包裹的内置资源目录文件
/// 说明:根据指定目录下的文件生成清单文件。
/// </summary>
public static bool CreateCatalogFile(IManifestRestoreServices services, string packageName, string packageDirectory)
public static bool CreateFile(IManifestRestoreServices services, string packageName, string packageDirectory)
{
// 获取资源清单版本
string packageVersion;
@@ -40,7 +40,7 @@ namespace YooAsset
}
var binaryData = FileUtility.ReadAllBytes(manifestFilePath);
packageManifest = ManifestTools.DeserializeFromBinary(binaryData, services);
packageManifest = PackageManifestTools.DeserializeFromBinary(binaryData, services);
}
// 获取文件名映射关系
@@ -54,7 +54,7 @@ namespace YooAsset
// 创建内置清单实例
var buildinFileCatalog = new DefaultBuildinFileCatalog();
buildinFileCatalog.FileVersion = CatalogDefine.FileVersion;
buildinFileCatalog.FileVersion = CatalogFileDefine.FileVersion;
buildinFileCatalog.PackageName = packageName;
buildinFileCatalog.PackageVersion = packageVersion;
@@ -122,11 +122,11 @@ namespace YooAsset
/// <summary>
/// 生成空的包裹内置资源目录文件
/// </summary>
public static bool CreateEmptyCatalogFile(string packageName, string packageVersion, string outputPath)
public static bool CreateEmptyFile(string packageName, string packageVersion, string outputPath)
{
// 创建内置清单实例
var buildinFileCatalog = new DefaultBuildinFileCatalog();
buildinFileCatalog.FileVersion = CatalogDefine.FileVersion;
buildinFileCatalog.FileVersion = CatalogFileDefine.FileVersion;
buildinFileCatalog.PackageName = packageName;
buildinFileCatalog.PackageVersion = packageVersion;
@@ -173,13 +173,13 @@ namespace YooAsset
using (FileStream fs = new FileStream(savePath, FileMode.Create))
{
// 创建缓存器
BufferWriter buffer = new BufferWriter(CatalogDefine.FileMaxSize);
BufferWriter buffer = new BufferWriter(CatalogFileDefine.FileMaxSize);
// 写入文件标记
buffer.WriteUInt32(CatalogDefine.FileSign);
buffer.WriteUInt32(CatalogFileDefine.FileSign);
// 写入文件版本
buffer.WriteUTF8(CatalogDefine.FileVersion);
buffer.WriteUTF8(CatalogFileDefine.FileVersion);
// 写入文件头信息
buffer.WriteUTF8(catalog.PackageName);
@@ -213,13 +213,13 @@ namespace YooAsset
// 读取文件标记
uint fileSign = buffer.ReadUInt32();
if (fileSign != CatalogDefine.FileSign)
if (fileSign != CatalogFileDefine.FileSign)
throw new Exception("Invalid catalog file !");
// 读取文件版本
string fileVersion = buffer.ReadUTF8();
if (fileVersion != CatalogDefine.FileVersion)
throw new Exception($"The catalog file version are not compatible : {fileVersion} != {CatalogDefine.FileVersion}");
if (fileVersion != CatalogFileDefine.FileVersion)
throw new Exception($"The catalog file version are not compatible : {fileVersion} != {CatalogFileDefine.FileVersion}");
DefaultBuildinFileCatalog catalog = new DefaultBuildinFileCatalog();
{
@@ -107,12 +107,12 @@ namespace YooAsset
/// <summary>
/// 自定义参数:解密服务接口的实例类
/// </summary>
public IDecryptionServices DecryptionServices { private set; get; }
public IBundleDecryptionServices BundleDecryptionServices { private set; get; }
/// <summary>
/// 自定义参数:资源清单服务类
/// </summary>
public IManifestRestoreServices ManifestServices { private set; get; }
public IManifestRestoreServices ManifestRestoreServices { private set; get; }
/// <summary>
/// 自定义参数:拷贝内置文件接口的实例类
@@ -139,29 +139,30 @@ namespace YooAsset
var operation = new DBFSLoadPackageManifestOperation(this, options.PackageVersion);
return operation;
}
public virtual FSClearCacheFilesOperation ClearCacheFilesAsync(PackageManifest manifest, ClearCacheFilesOptions options)
public virtual FSClearCacheFilesOperation ClearCacheFilesAsync(ClearCacheFilesOptions options)
{
return _unpackFileSystem.ClearCacheFilesAsync(manifest, options);
return _unpackFileSystem.ClearCacheFilesAsync(options);
}
public virtual FSDownloadFileOperation DownloadFileAsync(PackageBundle bundle, DownloadFileOptions options)
public virtual FSDownloadFileOperation DownloadFileAsync(DownloadFileOptions options)
{
// 注意:业务层的解压器会依赖该方法
options.ImportFilePath = GetBuildinFileLoadPath(bundle);
return _unpackFileSystem.DownloadFileAsync(bundle, options);
options.ImportFilePath = GetBuildinFileLoadPath(options.Bundle);
return _unpackFileSystem.DownloadFileAsync(options);
}
public virtual FSLoadBundleOperation LoadBundleFile(PackageBundle bundle)
public virtual FSLoadBundleOperation LoadBundleAsync(LoadBundleOptions options)
{
PackageBundle bundle = options.Bundle;
if (IsUnpackBundleFile(bundle))
{
return _unpackFileSystem.LoadBundleFile(bundle);
return _unpackFileSystem.LoadBundleAsync(options);
}
if (bundle.BundleType == (int)EBuildBundleType.AssetBundle)
if (bundle.BundleType == (int)EBundleType.AssetBundle)
{
var operation = new DBFSLoadAssetBundleOperation(this, bundle);
return operation;
}
else if (bundle.BundleType == (int)EBuildBundleType.RawBundle)
else if (bundle.BundleType == (int)EBundleType.RawBundle)
{
var operation = new DBFSLoadRawBundleOperation(this, bundle);
return operation;
@@ -224,13 +225,13 @@ namespace YooAsset
{
UnpackFileSystemRoot = (string)value;
}
else if (name == FileSystemParametersDefine.DECRYPTION_SERVICES)
else if (name == FileSystemParametersDefine.BUNDLE_DECRYPTION_SERVICES)
{
DecryptionServices = (IDecryptionServices)value;
BundleDecryptionServices = (IBundleDecryptionServices)value;
}
else if (name == FileSystemParametersDefine.MANIFEST_SERVICES)
else if (name == FileSystemParametersDefine.MANIFEST_RESTORE_SERVICES)
{
ManifestServices = (IManifestRestoreServices)value;
ManifestRestoreServices = (IManifestRestoreServices)value;
}
else if (name == FileSystemParametersDefine.COPY_LOCAL_FILE_SERVICES)
{
@@ -264,7 +265,7 @@ namespace YooAsset
_unpackFileSystem.SetParameter(FileSystemParametersDefine.FILE_VERIFY_LEVEL, FileVerifyLevel);
_unpackFileSystem.SetParameter(FileSystemParametersDefine.FILE_VERIFY_MAX_CONCURRENCY, FileVerifyMaxConcurrency);
_unpackFileSystem.SetParameter(FileSystemParametersDefine.APPEND_FILE_EXTENSION, AppendFileExtension);
_unpackFileSystem.SetParameter(FileSystemParametersDefine.DECRYPTION_SERVICES, DecryptionServices);
_unpackFileSystem.SetParameter(FileSystemParametersDefine.BUNDLE_DECRYPTION_SERVICES, BundleDecryptionServices);
_unpackFileSystem.SetParameter(FileSystemParametersDefine.COPY_LOCAL_FILE_SERVICES, CopyLocalFileServices);
_unpackFileSystem.OnCreate(packageName, UnpackFileSystemRoot);
}
@@ -409,7 +410,7 @@ namespace YooAsset
if (bundle.Encrypted)
return true;
if (bundle.BundleType == (int)EBuildBundleType.RawBundle)
if (bundle.BundleType == (int)EBundleType.RawBundle)
return true;
return false;
@@ -479,31 +480,31 @@ namespace YooAsset
/// <summary>
/// 加载加密的资源文件
/// </summary>
public DecryptResult LoadEncryptedAssetBundle(PackageBundle bundle)
public DecryptSyncResult LoadEncryptedBundleSync(PackageBundle bundle)
{
string filePath = GetBuildinFileLoadPath(bundle);
var fileInfo = new DecryptFileInfo()
var bundleInfo = new DecryptBundleInfo()
{
BundleName = bundle.BundleName,
FileLoadCRC = bundle.UnityCRC,
FileLoadPath = filePath,
};
return DecryptionServices.LoadAssetBundle(fileInfo);
return BundleDecryptionServices.LoadAssetBundleSync(bundleInfo);
}
/// <summary>
/// 加载加密的资源文件
/// </summary>
public DecryptResult LoadEncryptedAssetBundleAsync(PackageBundle bundle)
public DecryptAsyncResult LoadEncryptedBundleAsync(PackageBundle bundle)
{
string filePath = GetBuildinFileLoadPath(bundle);
var fileInfo = new DecryptFileInfo()
var bundleInfo = new DecryptBundleInfo()
{
BundleName = bundle.BundleName,
FileLoadCRC = bundle.UnityCRC,
FileLoadPath = filePath,
};
return DecryptionServices.LoadAssetBundleAsync(fileInfo);
return BundleDecryptionServices.LoadAssetBundleAsync(bundleInfo);
}
#endregion
}
@@ -44,11 +44,11 @@ namespace YooAsset
{
if (_bundle.Encrypted)
{
if (_fileSystem.DecryptionServices == null)
if (_fileSystem.BundleDecryptionServices == null)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"The {nameof(IDecryptionServices)} is null !";
Error = $"The {nameof(IBundleDecryptionServices)} is null !";
YooLogger.Error(Error);
return;
}
@@ -58,7 +58,7 @@ namespace YooAsset
{
if (_bundle.Encrypted)
{
var decryptResult = _fileSystem.LoadEncryptedAssetBundle(_bundle);
var decryptResult = _fileSystem.LoadEncryptedBundleSync(_bundle);
_assetBundle = decryptResult.Result;
_managedStream = decryptResult.ManagedStream;
}
@@ -72,7 +72,7 @@ namespace YooAsset
{
if (_bundle.Encrypted)
{
var decryptResult = _fileSystem.LoadEncryptedAssetBundleAsync(_bundle);
var decryptResult = _fileSystem.LoadEncryptedBundleAsync(_bundle);
_createRequest = decryptResult.CreateRequest;
_managedStream = decryptResult.ManagedStream;
}
@@ -82,7 +82,7 @@ namespace YooAsset
{
try
{
Catalog = CatalogTools.DeserializeFromBinary(_fileData);
Catalog = CatalogFileHelper.DeserializeFromBinary(_fileData);
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
@@ -86,7 +86,7 @@ namespace YooAsset
if (_steps == ESteps.VerifyFileData)
{
if (ManifestTools.VerifyManifestData(_fileData, _packageHash))
if (PackageManifestTools.VerifyManifestData(_fileData, _packageHash))
{
_steps = ESteps.LoadManifest;
}
@@ -102,7 +102,7 @@ namespace YooAsset
{
if (_deserializer == null)
{
_deserializer = new DeserializeManifestOperation(_fileSystem.ManifestServices, _fileData);
_deserializer = new DeserializeManifestOperation(_fileSystem.ManifestRestoreServices, _fileData);
_deserializer.StartOperation();
AddChildOperation(_deserializer);
}