update file system

This commit is contained in:
何冠峰
2024-07-07 00:52:17 +08:00
parent 481711fd75
commit bafd15571a
68 changed files with 483 additions and 889 deletions
@@ -10,6 +10,36 @@ namespace YooAsset
/// </summary>
internal class DefaultBuildinFileSystem : IFileSystem
{
private class UnpackRemoteServices : IRemoteServices
{
private readonly string _buildinPackageRoot;
protected readonly Dictionary<string, string> _mapping = new Dictionary<string, string>(10000);
public UnpackRemoteServices(string buildinPackRoot)
{
_buildinPackageRoot = buildinPackRoot;
}
string IRemoteServices.GetRemoteMainURL(string fileName)
{
return GetFileLoadURL(fileName);
}
string IRemoteServices.GetRemoteFallbackURL(string fileName)
{
return GetFileLoadURL(fileName);
}
private string GetFileLoadURL(string fileName)
{
if (_mapping.TryGetValue(fileName, out string url) == false)
{
string filePath = PathUtility.Combine(_buildinPackageRoot, fileName);
url = DownloadSystemHelper.ConvertToWWWPath(filePath);
_mapping.Add(fileName, url);
}
return url;
}
}
public class FileWrapper
{
public string FileName { private set; get; }
@@ -23,11 +53,9 @@ namespace YooAsset
protected readonly Dictionary<string, FileWrapper> _wrappers = new Dictionary<string, FileWrapper>(10000);
protected readonly Dictionary<string, Stream> _loadedStream = new Dictionary<string, Stream>(10000);
protected readonly Dictionary<string, string> _buildinFilePaths = new Dictionary<string, string>(10000);
protected IFileSystem _unpackFileSystem;
protected string _packageRoot;
// 解压文件系统
public IFileSystem UnpackFileSystem { private set; get; }
/// <summary>
/// 包裹名称
/// </summary>
@@ -99,37 +127,38 @@ namespace YooAsset
return operation;
#endif
}
public virtual FSLoadPackageManifestOperation LoadPackageManifestAsync(params object[] args)
public virtual FSLoadPackageManifestOperation LoadPackageManifestAsync(string packageVersion, int timeout)
{
var operation = new DBFSLoadPackageManifestOperation(this);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
public virtual FSRequestPackageVersionOperation RequestPackageVersionAsync(params object[] args)
public virtual FSRequestPackageVersionOperation RequestPackageVersionAsync(bool appendTimeTicks, int timeout)
{
var operation = new DBFSRequestPackageVersionOperation(this);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
public virtual FSClearAllBundleFilesOperation ClearAllBundleFilesAsync(params object[] args)
public virtual FSClearAllBundleFilesOperation ClearAllBundleFilesAsync()
{
return UnpackFileSystem.ClearAllBundleFilesAsync();
return _unpackFileSystem.ClearAllBundleFilesAsync();
}
public virtual FSClearUnusedBundleFilesOperation ClearUnusedBundleFilesAsync(params object[] args)
public virtual FSClearUnusedBundleFilesOperation ClearUnusedBundleFilesAsync(PackageManifest manifest)
{
PackageManifest manifest = args[0] as PackageManifest;
return UnpackFileSystem.ClearUnusedBundleFilesAsync(manifest);
return _unpackFileSystem.ClearUnusedBundleFilesAsync(manifest);
}
public virtual FSDownloadFileOperation DownloadFileAsync(params object[] args)
public virtual FSDownloadFileOperation DownloadFileAsync(PackageBundle bundle, DownloadParam param)
{
PackageBundle bundle = args[0] as PackageBundle;
int failedTryAgain = (int)args[2];
int timeout = (int)args[3];
string buidlinFilePath = GetBuildinFileLoadPath(bundle);
return UnpackFileSystem.DownloadFileAsync(bundle, buidlinFilePath, failedTryAgain, timeout);
param.ImportFilePath = GetBuildinFileLoadPath(bundle);
return _unpackFileSystem.DownloadFileAsync(bundle, param);
}
public virtual FSLoadBundleOperation LoadBundleFile(PackageBundle bundle)
{
if (NeedUnpack(bundle))
{
return _unpackFileSystem.LoadBundleFile(bundle);
}
if (RawFileBuildPipeline)
{
var operation = new DBFSLoadRawBundleOperation(this, bundle);
@@ -149,9 +178,9 @@ namespace YooAsset
if (assetBundle == null)
return;
if (UnpackFileSystem.Exists(bundle))
if (_unpackFileSystem.Exists(bundle))
{
UnpackFileSystem.UnloadBundleFile(bundle, assetBundle);
_unpackFileSystem.UnloadBundleFile(bundle, assetBundle);
}
else
{
@@ -196,11 +225,13 @@ namespace YooAsset
_packageRoot = PathUtility.Combine(rootDirectory, packageName);
// 创建解压文件系统
UnpackFileSystem = new DefaultUnpackFileSystem();
UnpackFileSystem.SetParameter("FILE_VERIFY_LEVEL", FileVerifyLevel);
UnpackFileSystem.SetParameter("APPEND_FILE_EXTENSION", AppendFileExtension);
UnpackFileSystem.SetParameter("RAW_FILE_BUILD_PIPELINE", RawFileBuildPipeline);
UnpackFileSystem.OnCreate(packageName, null);
var remoteServices = new UnpackRemoteServices(_packageRoot);
_unpackFileSystem = new DefaultUnpackFileSystem();
_unpackFileSystem.SetParameter("REMOTE_SERVICES", remoteServices);
_unpackFileSystem.SetParameter("FILE_VERIFY_LEVEL", FileVerifyLevel);
_unpackFileSystem.SetParameter("APPEND_FILE_EXTENSION", AppendFileExtension);
_unpackFileSystem.SetParameter("RAW_FILE_BUILD_PIPELINE", RawFileBuildPipeline);
_unpackFileSystem.OnCreate(packageName, null);
}
public virtual void OnUpdate()
{
@@ -237,12 +268,7 @@ namespace YooAsset
#region 内部方法
protected string GetDefaultRoot()
{
string path = PathUtility.Combine(UnityEngine.Application.streamingAssetsPath, YooAssetSettingsData.Setting.DefaultYooFolderName);
#if UNITY_OPENHARMONY
return $"file://{path}";
#else
return path;
#endif
return PathUtility.Combine(Application.streamingAssetsPath, YooAssetSettingsData.Setting.DefaultYooFolderName);
}
public string GetBuildinFileLoadPath(PackageBundle bundle)
{
@@ -288,6 +314,14 @@ namespace YooAsset
_wrappers.Add(bundleGUID, wrapper);
return true;
}
/// <summary>
/// 初始化解压文件系统
/// </summary>
public FSInitializeFileSystemOperation InitializeUpackFileSystem()
{
return _unpackFileSystem.InitializeFileSystemAsync();
}
#endregion
}
}
@@ -10,8 +10,8 @@ namespace YooAsset
public int callbackOrder { get { return 0; } }
/// <summary>
/// 在构建应用程序前自动生成内置资源清单。
/// 原理:搜索StreamingAssets目录下的所有资源文件,然后将这些文件信息写入内置资源清单,并存储在Resources目录下。
/// 在构建应用程序前自动生成内置资源目录文件。
/// 原理:搜索StreamingAssets目录下的所有资源文件,然后将这些文件信息写入文件,并存储在Resources目录下。
/// </summary>
public void OnPreprocessBuild(UnityEditor.Build.Reporting.BuildReport report)
{
@@ -24,7 +24,7 @@ namespace YooAsset
DirectoryInfo rootDirectory = new DirectoryInfo(rootPath);
if (rootDirectory.Exists == false)
{
Debug.LogWarning($"Not found buildin root folder : {rootPath}");
Debug.LogWarning($"Can not found buildin root folder : {rootPath}");
return;
}
@@ -44,7 +44,7 @@ namespace YooAsset
string versionFilePath = $"{pacakgeDirectory}/{versionFileName}";
if (File.Exists(versionFilePath) == false)
{
Debug.LogWarning($"Not found package version file : {versionFilePath}");
Debug.LogWarning($"Can not found package version file : {versionFilePath}");
return;
}
@@ -58,7 +58,7 @@ namespace YooAsset
string manifestFilePath = $"{pacakgeDirectory}/{manifestFileName}";
if (File.Exists(manifestFilePath) == false)
{
Debug.LogWarning($"Not found package manifest file : {manifestFilePath}");
Debug.LogWarning($"Can not found package manifest file : {manifestFilePath}");
return;
}
@@ -107,7 +107,7 @@ namespace YooAsset
UnityEditor.AssetDatabase.CreateAsset(buildinFileCatalog, saveFilePath);
UnityEditor.AssetDatabase.SaveAssets();
UnityEditor.AssetDatabase.Refresh();
Debug.Log($"一共记录{buildinFileCatalog.Wrappers.Count}个内置资源文件,内置资源目录文件保存成功 : {saveFilePath}");
Debug.Log($"Succeed to save buildin file catalog : {saveFilePath}");
}
}
}
@@ -34,7 +34,7 @@ namespace YooAsset
if (_steps == ESteps.InitUnpackFileSystem)
{
if (_initUnpackFIleSystemOp == null)
_initUnpackFIleSystemOp = _fileSystem.UnpackFileSystem.InitializeFileSystemAsync();
_initUnpackFIleSystemOp = _fileSystem.InitializeUpackFileSystem();
Progress = _initUnpackFIleSystemOp.Progress;
if (_initUnpackFIleSystemOp.IsDone == false)
@@ -113,7 +113,7 @@ namespace YooAsset
if (_steps == ESteps.InitUnpackFileSystem)
{
if (_initUnpackFIleSystemOp == null)
_initUnpackFIleSystemOp = _fileSystem.UnpackFileSystem.InitializeFileSystemAsync();
_initUnpackFIleSystemOp = _fileSystem.InitializeUpackFileSystem();
Progress = _initUnpackFIleSystemOp.Progress;
if (_initUnpackFIleSystemOp.IsDone == false)
@@ -156,7 +156,7 @@ namespace YooAsset
if (_steps == ESteps.RecordFiles)
{
PackageManifest manifest = _loadPackageManifestOp.Result;
PackageManifest manifest = _loadPackageManifestOp.Manifest;
string pacakgeDirectory = _fileSystem.FileRoot;
DirectoryInfo rootDirectory = new DirectoryInfo(pacakgeDirectory);
FileInfo[] fileInfos = rootDirectory.GetFiles();
@@ -174,7 +174,7 @@ namespace YooAsset
}
else
{
YooLogger.Warning($"Failed mapping file : {fileName}");
YooLogger.Warning($"Failed to mapping buildin bundle file : {fileName}");
}
}
@@ -3,13 +3,14 @@ using UnityEngine;
namespace YooAsset
{
/// <summary>
/// 加载AssetBundle文件
/// </summary>
internal class DBFSLoadAssetBundleOperation : FSLoadBundleOperation
{
private enum ESteps
{
None,
UnpackAssetBundleFile,
LoadUnpackAssetBundle,
LoadBuidlinAssetBundle,
CheckLoadBuildinResult,
Done,
@@ -17,8 +18,6 @@ namespace YooAsset
private readonly DefaultBuildinFileSystem _fileSystem;
private readonly PackageBundle _bundle;
private FSLoadBundleOperation _loadUnpackBundleOp;
private FSDownloadFileOperation _unpackBundleOp;
private AssetBundleCreateRequest _createRequest;
private bool _isWaitForAsyncComplete = false;
private ESteps _steps = ESteps.None;
@@ -33,67 +32,13 @@ namespace YooAsset
{
DownloadProgress = 1f;
DownloadedBytes = _bundle.FileSize;
if (_fileSystem.NeedUnpack(_bundle))
{
_steps = ESteps.UnpackAssetBundleFile;
}
else
{
_steps = ESteps.LoadBuidlinAssetBundle;
}
_steps = ESteps.LoadBuidlinAssetBundle;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.UnpackAssetBundleFile)
{
if (_unpackBundleOp == null)
{
int failedTryAgain = 0;
int timeout = int.MaxValue;
_unpackBundleOp = _fileSystem.DownloadFileAsync(_bundle, null, failedTryAgain, timeout);
}
if (_unpackBundleOp.IsDone == false)
return;
if (_unpackBundleOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.LoadUnpackAssetBundle;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _unpackBundleOp.Error;
}
}
if (_steps == ESteps.LoadUnpackAssetBundle)
{
if (_loadUnpackBundleOp == null)
_loadUnpackBundleOp = _fileSystem.UnpackFileSystem.LoadBundleFile(_bundle);
if (_loadUnpackBundleOp.IsDone == false)
return;
if (_loadUnpackBundleOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.Done;
Result = _loadUnpackBundleOp.Result;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _loadUnpackBundleOp.Error;
}
}
if (_steps == ESteps.LoadBuidlinAssetBundle)
{
string filePath = _fileSystem.GetBuildinFileLoadPath(_bundle);
@@ -146,18 +91,6 @@ namespace YooAsset
while (true)
{
if (_unpackBundleOp != null)
{
if (_unpackBundleOp.IsDone == false)
_unpackBundleOp.WaitForAsyncComplete();
}
if (_loadUnpackBundleOp != null)
{
if (_loadUnpackBundleOp.IsDone == false)
_loadUnpackBundleOp.WaitForAsyncComplete();
}
// 驱动流程
InternalOnUpdate();
@@ -168,21 +101,17 @@ namespace YooAsset
}
public override void AbortDownloadOperation()
{
if (_steps == ESteps.UnpackAssetBundleFile)
{
if (_unpackBundleOp != null)
_unpackBundleOp.SetAbort();
}
}
}
/// <summary>
/// 加载原生文件
/// </summary>
internal class DBFSLoadRawBundleOperation : FSLoadBundleOperation
{
private enum ESteps
{
None,
UnpackRawBundleFile,
LoadUnpackRawBundle,
LoadBuildinRawBundle,
CheckLoadBuildinResult,
Done,
@@ -190,8 +119,6 @@ namespace YooAsset
private readonly DefaultBuildinFileSystem _fileSystem;
private readonly PackageBundle _bundle;
private FSLoadBundleOperation _loadUnpackBundleOp;
private FSDownloadFileOperation _unpackBundleOp;
private ESteps _steps = ESteps.None;
@@ -204,67 +131,13 @@ namespace YooAsset
{
DownloadProgress = 1f;
DownloadedBytes = _bundle.FileSize;
if (_fileSystem.NeedUnpack(_bundle))
{
_steps = ESteps.UnpackRawBundleFile;
}
else
{
_steps = ESteps.LoadBuildinRawBundle;
}
_steps = ESteps.LoadBuildinRawBundle;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.UnpackRawBundleFile)
{
if (_unpackBundleOp == null)
{
int failedTryAgain = 0;
int timeout = int.MaxValue;
_unpackBundleOp = _fileSystem.DownloadFileAsync(_bundle, null, failedTryAgain, timeout);
}
if (_unpackBundleOp.IsDone == false)
return;
if (_unpackBundleOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.LoadUnpackRawBundle;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _unpackBundleOp.Error;
}
}
if (_steps == ESteps.LoadUnpackRawBundle)
{
if (_loadUnpackBundleOp == null)
_loadUnpackBundleOp = _fileSystem.UnpackFileSystem.LoadBundleFile(_bundle);
if (_loadUnpackBundleOp.IsDone == false)
return;
if (_loadUnpackBundleOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.Done;
Result = _loadUnpackBundleOp.Result;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _loadUnpackBundleOp.Error;
}
}
if (_steps == ESteps.LoadBuildinRawBundle)
{
string filePath = _fileSystem.GetBuildinFileLoadPath(_bundle);
@@ -302,18 +175,6 @@ namespace YooAsset
{
while (true)
{
if (_unpackBundleOp != null)
{
if (_unpackBundleOp.IsDone == false)
_unpackBundleOp.WaitForAsyncComplete();
}
if (_loadUnpackBundleOp != null)
{
if (_loadUnpackBundleOp.IsDone == false)
_loadUnpackBundleOp.WaitForAsyncComplete();
}
// 驱动流程
InternalOnUpdate();
@@ -324,11 +185,6 @@ namespace YooAsset
}
public override void AbortDownloadOperation()
{
if (_steps == ESteps.UnpackRawBundleFile)
{
if (_unpackBundleOp != null)
_unpackBundleOp.SetAbort();
}
}
}
}
@@ -95,7 +95,7 @@ namespace YooAsset
if (_loadBuildinPackageManifestOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.Done;
Result = _loadBuildinPackageManifestOp.Manifest;
Manifest = _loadBuildinPackageManifestOp.Manifest;
Status = EOperationStatus.Succeed;
}
else
@@ -6,7 +6,7 @@ namespace YooAsset
private enum ESteps
{
None,
GetPackageVersion,
RequestPackageVersion,
Done,
}
@@ -21,14 +21,14 @@ namespace YooAsset
}
internal override void InternalOnStart()
{
_steps = ESteps.GetPackageVersion;
_steps = ESteps.RequestPackageVersion;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.GetPackageVersion)
if (_steps == ESteps.RequestPackageVersion)
{
if (_requestBuildinPackageVersionOp == null)
{
@@ -44,7 +44,7 @@ namespace YooAsset
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"The catalog file package name {catalog.PackageName} cannot match the file system package name {_fileSystem.PackageName}";
Error = $"Catalog file package name {catalog.PackageName} cannot match the file system package name {_fileSystem.PackageName}";
return;
}
@@ -54,7 +54,7 @@ namespace YooAsset
_fileSystem.RecordFile(wrapper.BundleGUID, fileWrapper);
}
YooLogger.Log($"Package '{_fileSystem.PackageName}' catalog files count : {catalog.Wrappers.Count}");
YooLogger.Log($"Package '{_fileSystem.PackageName}' buildin catalog files count : {catalog.Wrappers.Count}");
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
@@ -20,7 +20,7 @@ namespace YooAsset
private ESteps _steps = ESteps.None;
/// <summary>
/// 加载的清单实例
/// 包裹清单
/// </summary>
public PackageManifest Manifest { private set; get; }
@@ -76,7 +76,7 @@ namespace YooAsset
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = "Failed to verify buildin package manifest file!";
Error = "Failed to verify buildin package manifest file !";
}
}