com.alicizax.unity.tuyoogam.../Runtime/FileSystem/DefaultBuildinFileSystem/DefaultBuildinFileSystem.cs

459 lines
16 KiB
C#
Raw Normal View History

2025-01-09 11:31:04 +08:00
using System;
using System.IO;
using System.Collections.Generic;
using UnityEngine;
namespace YooAsset
{
/// <summary>
/// 内置文件系统
/// </summary>
internal class DefaultBuildinFileSystem : IFileSystem
{
public class FileWrapper
{
public string FileName { private set; get; }
public FileWrapper(string fileName)
{
FileName = fileName;
}
}
protected readonly Dictionary<string, FileWrapper> _wrappers = new Dictionary<string, FileWrapper>(10000);
2025-02-28 16:11:01 +08:00
protected readonly Dictionary<string, string> _buildinFilePathMapping = new Dictionary<string, string>(10000);
2025-01-09 11:31:04 +08:00
protected IFileSystem _unpackFileSystem;
protected string _packageRoot;
/// <summary>
/// 包裹名称
/// </summary>
public string PackageName { private set; get; }
/// <summary>
/// 文件根目录
/// </summary>
public string FileRoot
{
get
{
return _packageRoot;
}
}
/// <summary>
/// 文件数量
/// </summary>
public int FileCount
{
get
{
return _wrappers.Count;
}
}
#region
2025-09-10 16:04:39 +08:00
/// <summary>
/// 自定义参数:覆盖安装缓存清理模式
/// </summary>
public EOverwriteInstallClearMode InstallClearMode { private set; get; } = EOverwriteInstallClearMode.ClearAllManifestFiles;
2025-01-09 11:31:04 +08:00
/// <summary>
/// 自定义参数:初始化的时候缓存文件校验级别
/// </summary>
public EFileVerifyLevel FileVerifyLevel { private set; get; } = EFileVerifyLevel.Middle;
2025-04-01 21:12:28 +08:00
/// <summary>
2025-09-10 16:04:39 +08:00
/// 自定义参数:初始化的时候缓存文件校验最大并发数
2025-04-01 21:12:28 +08:00
/// </summary>
2025-09-10 16:04:39 +08:00
public int FileVerifyMaxConcurrency { private set; get; } = int.MaxValue;
2025-04-01 21:12:28 +08:00
2025-01-09 11:31:04 +08:00
/// <summary>
/// 自定义参数:数据文件追加文件格式
/// </summary>
public bool AppendFileExtension { private set; get; } = false;
/// <summary>
/// 自定义参数禁用Catalog目录查询文件
/// </summary>
public bool DisableCatalogFile { private set; get; } = false;
2025-02-28 16:11:01 +08:00
/// <summary>
/// 自定义参数:拷贝内置清单
/// </summary>
public bool CopyBuildinPackageManifest { private set; get; } = false;
/// <summary>
/// 自定义参数:拷贝内置清单的目标目录
/// 注意:该参数为空的时候,会获取默认的沙盒目录!
/// </summary>
public string CopyBuildinPackageManifestDestRoot { private set; get; }
2025-01-09 11:31:04 +08:00
/// <summary>
/// 自定义参数:解密方法类
/// </summary>
public IDecryptionServices DecryptionServices { private set; get; }
2025-09-02 19:21:49 +08:00
/// <summary>
/// 自定义参数:资源清单服务类
/// </summary>
public IManifestRestoreServices ManifestServices { private set; get; }
/// <summary>
/// 自定义参数:拷贝内置文件服务类
/// </summary>
public ICopyLocalFileServices CopyLocalFileServices { private set; get; }
2025-01-09 11:31:04 +08:00
#endregion
public DefaultBuildinFileSystem()
{
}
public virtual FSInitializeFileSystemOperation InitializeFileSystemAsync()
{
var operation = new DBFSInitializeOperation(this);
return operation;
}
public virtual FSLoadPackageManifestOperation LoadPackageManifestAsync(string packageVersion, int timeout)
{
var operation = new DBFSLoadPackageManifestOperation(this, packageVersion);
return operation;
}
public virtual FSRequestPackageVersionOperation RequestPackageVersionAsync(bool appendTimeTicks, int timeout)
{
var operation = new DBFSRequestPackageVersionOperation(this);
return operation;
}
2025-05-13 10:40:30 +08:00
public virtual FSClearCacheFilesOperation ClearCacheFilesAsync(PackageManifest manifest, ClearCacheFilesOptions options)
2025-01-09 11:31:04 +08:00
{
2025-05-13 10:40:30 +08:00
return _unpackFileSystem.ClearCacheFilesAsync(manifest, options);
2025-01-09 11:31:04 +08:00
}
2025-05-13 10:40:30 +08:00
public virtual FSDownloadFileOperation DownloadFileAsync(PackageBundle bundle, DownloadFileOptions options)
2025-01-09 11:31:04 +08:00
{
2025-09-02 19:21:49 +08:00
// 注意:业务层的解压器会依赖该方法
2025-05-13 10:40:30 +08:00
options.ImportFilePath = GetBuildinFileLoadPath(bundle);
return _unpackFileSystem.DownloadFileAsync(bundle, options);
2025-01-09 11:31:04 +08:00
}
public virtual FSLoadBundleOperation LoadBundleFile(PackageBundle bundle)
{
2025-02-28 16:11:01 +08:00
if (IsUnpackBundleFile(bundle))
2025-01-09 11:31:04 +08:00
{
return _unpackFileSystem.LoadBundleFile(bundle);
}
if (bundle.BundleType == (int)EBuildBundleType.AssetBundle)
{
var operation = new DBFSLoadAssetBundleOperation(this, bundle);
return operation;
}
else if (bundle.BundleType == (int)EBuildBundleType.RawBundle)
{
var operation = new DBFSLoadRawBundleOperation(this, bundle);
return operation;
}
else
{
string error = $"{nameof(DefaultBuildinFileSystem)} not support load bundle type : {bundle.BundleType}";
var operation = new FSLoadBundleCompleteOperation(error);
return operation;
}
}
public virtual void SetParameter(string name, object value)
{
2025-09-10 16:04:39 +08:00
if (name == FileSystemParametersDefine.INSTALL_CLEAR_MODE)
{
InstallClearMode = (EOverwriteInstallClearMode)value;
}
else if (name == FileSystemParametersDefine.FILE_VERIFY_LEVEL)
2025-01-09 11:31:04 +08:00
{
FileVerifyLevel = (EFileVerifyLevel)value;
}
2025-09-10 16:04:39 +08:00
else if (name == FileSystemParametersDefine.FILE_VERIFY_MAX_CONCURRENCY)
2025-04-01 21:12:28 +08:00
{
2025-09-10 16:04:39 +08:00
int convertValue = Convert.ToInt32(value);
FileVerifyMaxConcurrency = Mathf.Clamp(convertValue, 1, int.MaxValue);
2025-04-01 21:12:28 +08:00
}
2025-01-09 11:31:04 +08:00
else if (name == FileSystemParametersDefine.APPEND_FILE_EXTENSION)
{
2025-04-01 21:12:28 +08:00
AppendFileExtension = Convert.ToBoolean(value);
2025-01-09 11:31:04 +08:00
}
else if (name == FileSystemParametersDefine.DISABLE_CATALOG_FILE)
{
2025-04-01 21:12:28 +08:00
DisableCatalogFile = Convert.ToBoolean(value);
2025-01-09 11:31:04 +08:00
}
2025-02-28 16:11:01 +08:00
else if (name == FileSystemParametersDefine.COPY_BUILDIN_PACKAGE_MANIFEST)
{
2025-04-01 21:12:28 +08:00
CopyBuildinPackageManifest = Convert.ToBoolean(value);
2025-02-28 16:11:01 +08:00
}
else if (name == FileSystemParametersDefine.COPY_BUILDIN_PACKAGE_MANIFEST_DEST_ROOT)
{
CopyBuildinPackageManifestDestRoot = (string)value;
}
2025-01-09 11:31:04 +08:00
else if (name == FileSystemParametersDefine.DECRYPTION_SERVICES)
{
DecryptionServices = (IDecryptionServices)value;
}
2025-09-02 19:21:49 +08:00
else if (name == FileSystemParametersDefine.MANIFEST_SERVICES)
{
ManifestServices = (IManifestRestoreServices)value;
}
else if (name == FileSystemParametersDefine.COPY_LOCAL_FILE_SERVICES)
{
CopyLocalFileServices = (ICopyLocalFileServices)value;
}
2025-01-09 11:31:04 +08:00
else
{
YooLogger.Warning($"Invalid parameter : {name}");
}
}
public virtual void OnCreate(string packageName, string packageRoot)
{
PackageName = packageName;
if (string.IsNullOrEmpty(packageRoot))
_packageRoot = GetDefaultBuildinPackageRoot(packageName);
else
_packageRoot = packageRoot;
// 创建解压文件系统
var remoteServices = new DefaultUnpackRemoteServices(_packageRoot);
_unpackFileSystem = new DefaultUnpackFileSystem();
_unpackFileSystem.SetParameter(FileSystemParametersDefine.REMOTE_SERVICES, remoteServices);
2025-04-01 21:12:28 +08:00
_unpackFileSystem.SetParameter(FileSystemParametersDefine.INSTALL_CLEAR_MODE, InstallClearMode);
2025-09-10 16:04:39 +08:00
_unpackFileSystem.SetParameter(FileSystemParametersDefine.FILE_VERIFY_LEVEL, FileVerifyLevel);
_unpackFileSystem.SetParameter(FileSystemParametersDefine.FILE_VERIFY_MAX_CONCURRENCY, FileVerifyMaxConcurrency);
2025-01-09 11:31:04 +08:00
_unpackFileSystem.SetParameter(FileSystemParametersDefine.APPEND_FILE_EXTENSION, AppendFileExtension);
_unpackFileSystem.SetParameter(FileSystemParametersDefine.DECRYPTION_SERVICES, DecryptionServices);
2025-09-02 19:21:49 +08:00
_unpackFileSystem.SetParameter(FileSystemParametersDefine.COPY_LOCAL_FILE_SERVICES, CopyLocalFileServices);
2025-01-09 11:31:04 +08:00
_unpackFileSystem.OnCreate(packageName, null);
}
2025-02-28 16:11:01 +08:00
public virtual void OnDestroy()
2025-01-09 11:31:04 +08:00
{
}
public virtual bool Belong(PackageBundle bundle)
{
if (DisableCatalogFile)
return true;
return _wrappers.ContainsKey(bundle.BundleGUID);
}
public virtual bool Exists(PackageBundle bundle)
{
if (DisableCatalogFile)
return true;
return _wrappers.ContainsKey(bundle.BundleGUID);
}
public virtual bool NeedDownload(PackageBundle bundle)
{
return false;
}
public virtual bool NeedUnpack(PackageBundle bundle)
{
2025-02-28 16:11:01 +08:00
if (IsUnpackBundleFile(bundle))
{
return _unpackFileSystem.Exists(bundle) == false;
}
else
{
2025-01-09 11:31:04 +08:00
return false;
2025-02-28 16:11:01 +08:00
}
2025-01-09 11:31:04 +08:00
}
public virtual bool NeedImport(PackageBundle bundle)
{
return false;
}
public virtual string GetBundleFilePath(PackageBundle bundle)
{
2025-02-28 16:11:01 +08:00
if (IsUnpackBundleFile(bundle))
2025-01-09 11:31:04 +08:00
return _unpackFileSystem.GetBundleFilePath(bundle);
return GetBuildinFileLoadPath(bundle);
}
public virtual byte[] ReadBundleFileData(PackageBundle bundle)
{
2025-02-28 16:11:01 +08:00
if (IsUnpackBundleFile(bundle))
2025-01-09 11:31:04 +08:00
return _unpackFileSystem.ReadBundleFileData(bundle);
if (Exists(bundle) == false)
return null;
2025-04-01 21:12:28 +08:00
#if UNITY_ANDROID
//TODO : 安卓平台内置文件属于APK压缩包内的文件。
YooLogger.Error($"Android platform not support read buildin bundle file data !");
return null;
#else
2025-01-09 11:31:04 +08:00
if (bundle.Encrypted)
{
if (DecryptionServices == null)
{
YooLogger.Error($"The {nameof(IDecryptionServices)} is null !");
return null;
}
string filePath = GetBuildinFileLoadPath(bundle);
var fileInfo = new DecryptFileInfo()
{
BundleName = bundle.BundleName,
FileLoadCRC = bundle.UnityCRC,
FileLoadPath = filePath,
};
return DecryptionServices.ReadFileData(fileInfo);
}
else
{
string filePath = GetBuildinFileLoadPath(bundle);
return FileUtility.ReadAllBytes(filePath);
}
2025-04-01 21:12:28 +08:00
#endif
2025-01-09 11:31:04 +08:00
}
public virtual string ReadBundleFileText(PackageBundle bundle)
{
2025-02-28 16:11:01 +08:00
if (IsUnpackBundleFile(bundle))
2025-01-09 11:31:04 +08:00
return _unpackFileSystem.ReadBundleFileText(bundle);
if (Exists(bundle) == false)
return null;
2025-04-01 21:12:28 +08:00
#if UNITY_ANDROID
//TODO : 安卓平台内置文件属于APK压缩包内的文件。
YooLogger.Error($"Android platform not support read buildin bundle file text !");
return null;
#else
2025-01-09 11:31:04 +08:00
if (bundle.Encrypted)
{
if (DecryptionServices == null)
{
YooLogger.Error($"The {nameof(IDecryptionServices)} is null !");
return null;
}
string filePath = GetBuildinFileLoadPath(bundle);
var fileInfo = new DecryptFileInfo()
{
BundleName = bundle.BundleName,
FileLoadCRC = bundle.UnityCRC,
FileLoadPath = filePath,
};
return DecryptionServices.ReadFileText(fileInfo);
}
else
{
string filePath = GetBuildinFileLoadPath(bundle);
return FileUtility.ReadAllText(filePath);
}
2025-04-01 21:12:28 +08:00
#endif
2025-01-09 11:31:04 +08:00
}
2025-09-10 16:04:39 +08:00
/// <summary>
/// 是否属于解压资源包文件
/// </summary>
protected virtual bool IsUnpackBundleFile(PackageBundle bundle)
{
if (Belong(bundle) == false)
return false;
#if UNITY_ANDROID || UNITY_OPENHARMONY
if (bundle.Encrypted)
return true;
if (bundle.BundleType == (int)EBuildBundleType.RawBundle)
return true;
return false;
#else
return false;
#endif
}
2025-01-09 11:31:04 +08:00
#region
protected string GetDefaultBuildinPackageRoot(string packageName)
{
2025-02-28 16:11:01 +08:00
string rootDirectory = YooAssetSettingsData.GetYooDefaultBuildinRoot();
2025-01-09 11:31:04 +08:00
return PathUtility.Combine(rootDirectory, packageName);
}
public string GetBuildinFileLoadPath(PackageBundle bundle)
{
2025-02-28 16:11:01 +08:00
if (_buildinFilePathMapping.TryGetValue(bundle.BundleGUID, out string filePath) == false)
2025-01-09 11:31:04 +08:00
{
filePath = PathUtility.Combine(_packageRoot, bundle.FileName);
2025-02-28 16:11:01 +08:00
_buildinFilePathMapping.Add(bundle.BundleGUID, filePath);
2025-01-09 11:31:04 +08:00
}
return filePath;
}
public string GetBuildinPackageVersionFilePath()
{
string fileName = YooAssetSettingsData.GetPackageVersionFileName(PackageName);
return PathUtility.Combine(_packageRoot, fileName);
}
public string GetBuildinPackageHashFilePath(string packageVersion)
{
string fileName = YooAssetSettingsData.GetPackageHashFileName(PackageName, packageVersion);
return PathUtility.Combine(_packageRoot, fileName);
}
public string GetBuildinPackageManifestFilePath(string packageVersion)
{
string fileName = YooAssetSettingsData.GetManifestBinaryFileName(PackageName, packageVersion);
return PathUtility.Combine(_packageRoot, fileName);
}
2025-04-01 21:12:28 +08:00
public string GetCatalogBinaryFileLoadPath()
2025-01-09 11:31:04 +08:00
{
2025-04-01 21:12:28 +08:00
return PathUtility.Combine(_packageRoot, DefaultBuildinFileSystemDefine.BuildinCatalogBinaryFileName);
2025-01-09 11:31:04 +08:00
}
/// <summary>
/// 记录文件信息
/// </summary>
public bool RecordCatalogFile(string bundleGUID, FileWrapper wrapper)
{
if (_wrappers.ContainsKey(bundleGUID))
{
YooLogger.Error($"{nameof(DefaultBuildinFileSystem)} has element : {bundleGUID}");
return false;
}
_wrappers.Add(bundleGUID, wrapper);
return true;
}
/// <summary>
/// 初始化解压文件系统
/// </summary>
public FSInitializeFileSystemOperation InitializeUpackFileSystem()
{
return _unpackFileSystem.InitializeFileSystemAsync();
}
/// <summary>
/// 加载加密的资源文件
/// </summary>
public DecryptResult LoadEncryptedAssetBundle(PackageBundle bundle)
{
string filePath = GetBuildinFileLoadPath(bundle);
var fileInfo = new DecryptFileInfo()
{
BundleName = bundle.BundleName,
FileLoadCRC = bundle.UnityCRC,
FileLoadPath = filePath,
};
return DecryptionServices.LoadAssetBundle(fileInfo);
}
/// <summary>
/// 加载加密的资源文件
/// </summary>
public DecryptResult LoadEncryptedAssetBundleAsync(PackageBundle bundle)
{
string filePath = GetBuildinFileLoadPath(bundle);
var fileInfo = new DecryptFileInfo()
{
BundleName = bundle.BundleName,
FileLoadCRC = bundle.UnityCRC,
FileLoadPath = filePath,
};
return DecryptionServices.LoadAssetBundleAsync(fileInfo);
}
#endregion
}
}