refactor the runtime code

重构了运行时代码,支持全新的文件系统。
This commit is contained in:
何冠峰
2024-07-04 20:36:26 +08:00
parent 2987d356b6
commit ff02da5c54
313 changed files with 9889 additions and 7234 deletions
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using UnityEngine;
namespace YooAsset
{
/// <summary>
/// 内置资源清单目录
/// </summary>
internal class DefaultBuildinFileCatalog : ScriptableObject
{
[Serializable]
public class FileWrapper
{
public string BundleGUID;
public string FileName;
public FileWrapper(string bundleGUID, string fileName)
{
BundleGUID = bundleGUID;
FileName = fileName;
}
}
/// <summary>
/// 包裹名称
/// </summary>
public string PackageName;
/// <summary>
/// 包裹版本
/// </summary>
public string PackageVersion;
/// <summary>
/// 文件列表
/// </summary>
public List<FileWrapper> Wrappers = new List<FileWrapper>();
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b05c83971e3dca94f9fa460d396385e5
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,335 @@
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);
protected readonly Dictionary<string, Stream> _loadedStream = new Dictionary<string, Stream>(10000);
protected readonly Dictionary<string, string> _buildinFilePaths = new Dictionary<string, string>(10000);
protected string _packageRoot;
/// <summary>
/// 解压文件系统
/// </summary>
public IFileSystem UnpackFileSystem { private set; get; }
/// <summary>
/// 包裹名称
/// </summary>
public string PackageName { private set; get; }
/// <summary>
/// 文件访问权限
/// </summary>
public EFileAccess FileSystemAccess
{
get
{
return EFileAccess.Read;
}
}
/// <summary>
/// 文件根目录
/// </summary>
public string FileRoot
{
get
{
return _packageRoot;
}
}
/// <summary>
/// 文件数量
/// </summary>
public int FileCount
{
get
{
return _wrappers.Count;
}
}
#region 自定义参数
/// <summary>
/// 自定义参数:初始化的时候缓存文件校验级别
/// </summary>
public EFileVerifyLevel FileVerifyLevel { private set; get; } = EFileVerifyLevel.Middle;
/// <summary>
/// 自定义参数:数据文件追加文件格式
/// </summary>
public bool AppendFileExtension { private set; get; } = false;
/// <summary>
/// 自定义参数:原生文件构建管线
/// </summary>
public bool RawFileBuildPipeline { private set; get; } = false;
#endregion
public DefaultBuildinFileSystem()
{
}
public virtual FSInitializeFileSystemOperation InitializeFileSystemAsync()
{
#if UNITY_EDITOR
var operation = new DBFSInitializeInEditorPlayModeOperation(this);
OperationSystem.StartOperation(PackageName, operation);
return operation;
#else
var operation = new DBFSInitializeOperation(this);
OperationSystem.StartOperation(PackageName, operation);
return operation;
#endif
}
public virtual FSLoadPackageManifestOperation LoadPackageManifestAsync(params object[] args)
{
var operation = new DBFSLoadPackageManifestOperation(this);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
public virtual FSRequestPackageVersionOperation RequestPackageVersionAsync(params object[] args)
{
throw new System.NotImplementedException();
}
public virtual FSClearAllBundleFilesOperation ClearAllBundleFilesAsync(params object[] args)
{
var operation = new DBFSClearAllBundleFilesOperation(this);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
public virtual FSClearUnusedBundleFilesOperation ClearUnusedBundleFilesAsync(params object[] args)
{
PackageManifest manifest = args[0] as PackageManifest;
var operation = new DBFSClearUnusedBundleFilesOperation(this, manifest);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
public virtual FSDownloadFileOperation DownloadFileAsync(params object[] args)
{
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);
}
public virtual void SetParameter(string name, object value)
{
if (name == "FILE_VERIFY_LEVEL")
{
FileVerifyLevel = (EFileVerifyLevel)value;
}
else if (name == "APPEND_FILE_EXTENSION")
{
AppendFileExtension = (bool)value;
}
else if (name == "RAW_FILE_BUILD_PIPELINE")
{
RawFileBuildPipeline = (bool)value;
}
else
{
YooLogger.Warning($"Invalid parameter : {name}");
}
}
public virtual void OnCreate(string packageName, string rootDirectory)
{
PackageName = packageName;
if (string.IsNullOrEmpty(rootDirectory))
rootDirectory = GetDefaultRoot();
_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);
}
public virtual void OnUpdate()
{
}
public virtual bool Belong(PackageBundle bundle)
{
return _wrappers.ContainsKey(bundle.BundleGUID);
}
public virtual bool Belong(string bundleGUID)
{
return _wrappers.ContainsKey(bundleGUID);
}
public virtual bool Exists(PackageBundle bundle)
{
return _wrappers.ContainsKey(bundle.BundleGUID);
}
public virtual bool Exists(string bundleGUID)
{
return _wrappers.ContainsKey(bundleGUID);
}
public virtual bool CheckNeedDownload(PackageBundle bundle)
{
return false;
}
public virtual bool CheckNeedUnpack(PackageBundle bundle)
{
if (Belong(bundle) == false)
return false;
#if UNITY_ANDROID
return RawFileBuildPipeline || bundle.Encrypted;
#else
return false;
#endif
}
public virtual bool CheckNeedImport(PackageBundle bundle)
{
return false;
}
public virtual bool WriteFile(PackageBundle bundle, string copyPath)
{
return UnpackFileSystem.WriteFile(bundle, copyPath);
}
public virtual bool DeleteFile(PackageBundle bundle)
{
return UnpackFileSystem.DeleteFile(bundle);
}
public virtual bool DeleteFile(string bundleGUID)
{
return UnpackFileSystem.DeleteFile(bundleGUID);
}
public virtual EFileVerifyResult VerifyFile(PackageBundle bundle)
{
return UnpackFileSystem.VerifyFile(bundle);
}
public virtual byte[] ReadFileBytes(PackageBundle bundle)
{
return UnpackFileSystem.ReadFileBytes(bundle);
}
public virtual string ReadFileText(PackageBundle bundle)
{
return UnpackFileSystem.ReadFileText(bundle);
}
public virtual FSLoadBundleOperation LoadBundleFile(PackageBundle bundle)
{
if (RawFileBuildPipeline)
{
var operation = new DBFSLoadRawBundleOperation(this, bundle);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
else
{
var operation = new DBFSLoadAssetBundleOperation(this, bundle);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
}
public virtual void UnloadBundleFile(PackageBundle bundle, object result)
{
AssetBundle assetBundle = result as AssetBundle;
if (assetBundle == null)
return;
if (UnpackFileSystem.Exists(bundle))
{
UnpackFileSystem.UnloadBundleFile(bundle, assetBundle);
}
else
{
if (assetBundle != null)
assetBundle.Unload(true);
if (_loadedStream.TryGetValue(bundle.BundleGUID, out Stream managedStream))
{
managedStream.Close();
managedStream.Dispose();
_loadedStream.Remove(bundle.BundleGUID);
}
}
}
#region 内部方法
protected string GetDefaultRoot()
{
string path = PathUtility.Combine(UnityEngine.Application.streamingAssetsPath, YooAssetSettingsData.Setting.DefaultYooFolderName);
#if UNITY_OPENHARMONY
return $"file://{path}";
#else
return path;
#endif
}
public string GetBuildinFileLoadPath(PackageBundle bundle)
{
if (_buildinFilePaths.TryGetValue(bundle.BundleGUID, out string filePath) == false)
{
filePath = PathUtility.Combine(_packageRoot, bundle.FileName);
_buildinFilePaths.Add(bundle.BundleGUID, filePath);
}
return filePath;
}
public string GetBuildinCatalogFileLoadPath()
{
string fileName = Path.GetFileNameWithoutExtension(DefaultBuildinFileSystemDefine.BuildinCatalogFileName);
return PathUtility.Combine(YooAssetSettingsData.Setting.DefaultYooFolderName, PackageName, fileName);
}
public string GetBuildinPackageVersionFilePath()
{
string fileName = YooAssetSettingsData.GetPackageVersionFileName(PackageName);
return PathUtility.Combine(FileRoot, fileName);
}
public string GetBuildinPackageHashFilePath(string packageVersion)
{
string fileName = YooAssetSettingsData.GetPackageHashFileName(PackageName, packageVersion);
return PathUtility.Combine(FileRoot, fileName);
}
public string GetBuildinPackageManifestFilePath(string packageVersion)
{
string fileName = YooAssetSettingsData.GetManifestBinaryFileName(PackageName, packageVersion);
return PathUtility.Combine(FileRoot, fileName);
}
/// <summary>
/// 记录缓存信息
/// </summary>
public bool Record(string bundleGUID, FileWrapper wrapper)
{
if (_wrappers.ContainsKey(bundleGUID))
{
YooLogger.Error($"{nameof(DefaultBuildinFileSystem)} has element : {bundleGUID}");
return false;
}
_wrappers.Add(bundleGUID, wrapper);
return true;
}
#endregion
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 98465b987635afc479022ec358477491
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,114 @@
#if UNITY_EDITOR
using System.Collections.Generic;
using System.IO;
using UnityEngine;
namespace YooAsset
{
internal class DefaultBuildinFileSystemBuild : UnityEditor.Build.IPreprocessBuildWithReport
{
public int callbackOrder { get { return 0; } }
/// <summary>
/// 在构建应用程序前自动生成内置资源清单。
/// 原理:搜索StreamingAssets目录下的所有资源文件,然后将这些文件信息写入内置资源清单,并存储在Resources目录下。
/// </summary>
public void OnPreprocessBuild(UnityEditor.Build.Reporting.BuildReport report)
{
string savePath = $"Assets/Resources/{YooAssetSettingsData.Setting.DefaultYooFolderName}";
DirectoryInfo saveDirectory = new DirectoryInfo(savePath);
if (saveDirectory.Exists)
saveDirectory.Delete(true);
string rootPath = $"{Application.dataPath}/StreamingAssets/{YooAssetSettingsData.Setting.DefaultYooFolderName}";
DirectoryInfo rootDirectory = new DirectoryInfo(rootPath);
if (rootDirectory.Exists == false)
{
Debug.LogWarning($"Not found buildin root folder : {rootPath}");
return;
}
// 搜索所有Package目录
DirectoryInfo[] subDirectories = rootDirectory.GetDirectories();
foreach (var subDirectory in subDirectories)
{
CreateBuildinManifest(subDirectory.Name, subDirectory.FullName);
}
}
private void CreateBuildinManifest(string packageName, string pacakgeDirectory)
{
// 获取资源清单版本
string packageVersion;
{
string versionFileName = YooAssetSettingsData.GetPackageVersionFileName(packageName);
string versionFilePath = $"{pacakgeDirectory}/{versionFileName}";
if (File.Exists(versionFilePath) == false)
{
Debug.LogWarning($"Not found package version file : {versionFilePath}");
return;
}
packageVersion = FileUtility.ReadAllText(versionFilePath);
}
// 加载资源清单文件
PackageManifest packageManifest;
{
string manifestFileName = YooAssetSettingsData.GetManifestBinaryFileName(packageName, packageVersion);
string manifestFilePath = $"{pacakgeDirectory}/{manifestFileName}";
if (File.Exists(manifestFilePath) == false)
{
Debug.LogWarning($"Not found package manifest file : {manifestFilePath}");
return;
}
var binaryData = FileUtility.ReadAllBytes(manifestFilePath);
packageManifest = ManifestTools.DeserializeFromBinary(binaryData);
}
// 获取文件名映射关系
Dictionary<string, string> fileMapping = new Dictionary<string, string>();
{
foreach (var packageBundle in packageManifest.BundleList)
{
fileMapping.Add(packageBundle.FileName, packageBundle.BundleGUID);
}
}
// 创建内置清单实例
var buildinFileCatalog = ScriptableObject.CreateInstance<DefaultBuildinFileCatalog>();
buildinFileCatalog.PackageName = packageName;
buildinFileCatalog.PackageVersion = packageVersion;
// 记录所有内置资源文件
DirectoryInfo rootDirectory = new DirectoryInfo(pacakgeDirectory);
FileInfo[] fileInfos = rootDirectory.GetFiles();
foreach (var fileInfo in fileInfos)
{
if (fileInfo.Extension == ".meta" || fileInfo.Extension == ".version" ||
fileInfo.Extension == ".hash" || fileInfo.Extension == ".bytes")
continue;
string fileName = fileInfo.Name;
if (fileMapping.TryGetValue(fileName, out string bundleGUID))
{
var wrapper = new DefaultBuildinFileCatalog.FileWrapper(bundleGUID, fileName);
buildinFileCatalog.Wrappers.Add(wrapper);
}
else
{
Debug.LogWarning($"Failed mapping file : {fileName}");
}
}
string saveFilePath = $"Assets/Resources/{YooAssetSettingsData.Setting.DefaultYooFolderName}/{packageName}/{DefaultBuildinFileSystemDefine.BuildinCatalogFileName}";
FileUtility.CreateFileDirectory(saveFilePath);
UnityEditor.AssetDatabase.CreateAsset(buildinFileCatalog, saveFilePath);
UnityEditor.AssetDatabase.SaveAssets();
UnityEditor.AssetDatabase.Refresh();
Debug.Log($"一共记录{buildinFileCatalog.Wrappers.Count}个内置资源文件,内置资源目录文件保存成功 : {saveFilePath}");
}
}
}
#endif
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6b5abe115ebfe1344b674db78b2edf6c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,11 @@
namespace YooAsset
{
internal class DefaultBuildinFileSystemDefine
{
/// <summary>
/// 内置清单文件名称
/// </summary>
public const string BuildinCatalogFileName = "BuildinCatalog.asset";
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4aa038f71ffd2394abc5daee917fbc66
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 06f6a75331ed07a4a9e5e8f46dcf157e
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,45 @@
namespace YooAsset
{
internal sealed class DBFSClearAllBundleFilesOperation : FSClearAllBundleFilesOperation
{
private enum ESteps
{
None,
ClearUnpackFileSystem,
Done,
}
private readonly DefaultBuildinFileSystem _fileSystem;
private FSClearAllBundleFilesOperation _unpackClearAllBundleFilesOp;
private ESteps _steps = ESteps.None;
internal DBFSClearAllBundleFilesOperation(DefaultBuildinFileSystem fileSystem)
{
_fileSystem = fileSystem;
}
internal override void InternalOnStart()
{
_steps = ESteps.ClearUnpackFileSystem;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.ClearUnpackFileSystem)
{
if (_unpackClearAllBundleFilesOp == null)
_unpackClearAllBundleFilesOp = _fileSystem.UnpackFileSystem.ClearAllBundleFilesAsync();
Progress = _unpackClearAllBundleFilesOp.Progress;
if (_unpackClearAllBundleFilesOp.IsDone == false)
return;
_steps = ESteps.Done;
Status = _unpackClearAllBundleFilesOp.Status;
Error = _unpackClearAllBundleFilesOp.Error;
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 624ca0ff2f0c7da4bb9c1a1d58e732b1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,47 @@
namespace YooAsset
{
internal sealed class DBFSClearUnusedBundleFilesOperation : FSClearUnusedBundleFilesOperation
{
private enum ESteps
{
None,
ClearUnpackFileSystem,
Done,
}
private readonly DefaultBuildinFileSystem _fileSystem;
private readonly PackageManifest _manifest;
private FSClearUnusedBundleFilesOperation _unpackClearUnusedBundleFilesOp;
private ESteps _steps = ESteps.None;
internal DBFSClearUnusedBundleFilesOperation(DefaultBuildinFileSystem fileSystem, PackageManifest manifest)
{
_fileSystem = fileSystem;
_manifest = manifest;
}
internal override void InternalOnStart()
{
_steps = ESteps.ClearUnpackFileSystem;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.ClearUnpackFileSystem)
{
if (_unpackClearUnusedBundleFilesOp == null)
_unpackClearUnusedBundleFilesOp = _fileSystem.UnpackFileSystem.ClearUnusedBundleFilesAsync(_manifest);
Progress = _unpackClearUnusedBundleFilesOp.Progress;
if (_unpackClearUnusedBundleFilesOp.IsDone == false)
return;
_steps = ESteps.Done;
Status = _unpackClearUnusedBundleFilesOp.Status;
Error = _unpackClearUnusedBundleFilesOp.Error;
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1e0e1c080b28b3c45b401da4ac06f687
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,186 @@
using System;
using System.IO;
namespace YooAsset
{
internal class DBFSInitializeOperation : FSInitializeFileSystemOperation
{
private enum ESteps
{
None,
InitUnpackFileSystem,
LoadCatalogFile,
Done,
}
private readonly DefaultBuildinFileSystem _fileSystem;
private FSInitializeFileSystemOperation _initUnpackFIleSystemOp;
private LoadBuildinCatalogFileOperation _loadCatalogFileOp;
private ESteps _steps = ESteps.None;
internal DBFSInitializeOperation(DefaultBuildinFileSystem fileSystem)
{
_fileSystem = fileSystem;
}
internal override void InternalOnStart()
{
_steps = ESteps.InitUnpackFileSystem;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.InitUnpackFileSystem)
{
if (_initUnpackFIleSystemOp == null)
_initUnpackFIleSystemOp = _fileSystem.UnpackFileSystem.InitializeFileSystemAsync();
Progress = _initUnpackFIleSystemOp.Progress;
if (_initUnpackFIleSystemOp.IsDone == false)
return;
if (_initUnpackFIleSystemOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.LoadCatalogFile;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _initUnpackFIleSystemOp.Error;
}
}
if (_steps == ESteps.LoadCatalogFile)
{
if (_loadCatalogFileOp == null)
{
_loadCatalogFileOp = new LoadBuildinCatalogFileOperation(_fileSystem);
OperationSystem.StartOperation(_fileSystem.PackageName, _loadCatalogFileOp);
}
if (_loadCatalogFileOp.IsDone == false)
return;
if (_loadCatalogFileOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _loadCatalogFileOp.Error;
}
}
}
}
/// <summary>
/// 在编辑器下离线模式的兼容性初始化
/// </summary>
internal sealed class DBFSInitializeInEditorPlayModeOperation : FSInitializeFileSystemOperation
{
private enum ESteps
{
None,
InitUnpackFileSystem,
LoadPackageManifest,
RecordFiles,
Done,
}
private readonly DefaultBuildinFileSystem _fileSystem;
private FSInitializeFileSystemOperation _initUnpackFIleSystemOp;
private DBFSLoadPackageManifestOperation _loadPackageManifestOp;
private ESteps _steps = ESteps.None;
internal DBFSInitializeInEditorPlayModeOperation(DefaultBuildinFileSystem fileSystem)
{
_fileSystem = fileSystem;
}
internal override void InternalOnStart()
{
_steps = ESteps.InitUnpackFileSystem;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.InitUnpackFileSystem)
{
if (_initUnpackFIleSystemOp == null)
_initUnpackFIleSystemOp = _fileSystem.UnpackFileSystem.InitializeFileSystemAsync();
Progress = _initUnpackFIleSystemOp.Progress;
if (_initUnpackFIleSystemOp.IsDone == false)
return;
if (_initUnpackFIleSystemOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.LoadPackageManifest;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _initUnpackFIleSystemOp.Error;
}
}
if (_steps == ESteps.LoadPackageManifest)
{
if (_loadPackageManifestOp == null)
{
_loadPackageManifestOp = new DBFSLoadPackageManifestOperation(_fileSystem);
OperationSystem.StartOperation(_fileSystem.PackageName, _loadPackageManifestOp);
}
if (_loadPackageManifestOp.IsDone == false)
return;
if (_loadPackageManifestOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.RecordFiles;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _loadPackageManifestOp.Error;
}
}
if (_steps == ESteps.RecordFiles)
{
PackageManifest manifest = _loadPackageManifestOp.Result;
string pacakgeDirectory = _fileSystem.FileRoot;
DirectoryInfo rootDirectory = new DirectoryInfo(pacakgeDirectory);
FileInfo[] fileInfos = rootDirectory.GetFiles();
foreach (var fileInfo in fileInfos)
{
if (fileInfo.Extension == ".meta" || fileInfo.Extension == ".version" ||
fileInfo.Extension == ".hash" || fileInfo.Extension == ".bytes")
continue;
string fileName = fileInfo.Name;
if (manifest.TryGetPackageBundleByFileName(fileName, out PackageBundle value))
{
var fileWrapper = new DefaultBuildinFileSystem.FileWrapper(fileName);
_fileSystem.Record(value.BundleGUID, fileWrapper);
}
else
{
YooLogger.Warning($"Failed mapping file : {fileName}");
}
}
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a15804d2ba6172a4b91f6c17245495a6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,334 @@
using System.IO;
using UnityEngine;
namespace YooAsset
{
internal class DBFSLoadAssetBundleOperation : FSLoadBundleOperation
{
private enum ESteps
{
None,
UnpackAssetBundleFile,
LoadUnpackAssetBundle,
LoadBuidlinAssetBundle,
CheckLoadBuildinResult,
Done,
}
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;
internal DBFSLoadAssetBundleOperation(DefaultBuildinFileSystem fileSystem, PackageBundle bundle)
{
_fileSystem = fileSystem;
_bundle = bundle;
}
internal override void InternalOnStart()
{
DownloadProgress = 1f;
DownloadedBytes = _bundle.FileSize;
if (_fileSystem.CheckNeedUnpack(_bundle))
{
_steps = ESteps.UnpackAssetBundleFile;
}
else
{
_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);
if (_isWaitForAsyncComplete)
{
Result = AssetBundle.LoadFromFile(filePath);
}
else
{
_createRequest = AssetBundle.LoadFromFileAsync(filePath);
}
_steps = ESteps.CheckLoadBuildinResult;
}
if (_steps == ESteps.CheckLoadBuildinResult)
{
if (_createRequest != null)
{
if (_isWaitForAsyncComplete)
{
// 强制挂起主线程(注意:该操作会很耗时)
YooLogger.Warning("Suspend the main thread to load unity bundle.");
Result = _createRequest.assetBundle;
}
else
{
if (_createRequest.isDone == false)
return;
Result = _createRequest.assetBundle;
}
}
if (Result != null)
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"Failed to load buildin asset bundle file : {_bundle.BundleName}";
}
}
}
public override void WaitForAsyncComplete()
{
_isWaitForAsyncComplete = true;
while (true)
{
if (_unpackBundleOp != null)
{
if (_unpackBundleOp.IsDone == false)
_unpackBundleOp.WaitForAsyncComplete();
}
if (_loadUnpackBundleOp != null)
{
if (_loadUnpackBundleOp.IsDone == false)
_loadUnpackBundleOp.WaitForAsyncComplete();
}
// 驱动流程
InternalOnUpdate();
// 完成后退出
if (IsDone)
break;
}
}
public override void AbortDownloadOperation()
{
if (_steps == ESteps.UnpackAssetBundleFile)
{
if (_unpackBundleOp != null)
_unpackBundleOp.SetAbort();
}
}
}
internal class DBFSLoadRawBundleOperation : FSLoadBundleOperation
{
private enum ESteps
{
None,
UnpackRawBundleFile,
LoadUnpackRawBundle,
LoadBuildinRawBundle,
CheckLoadBuildinResult,
Done,
}
private readonly DefaultBuildinFileSystem _fileSystem;
private readonly PackageBundle _bundle;
private FSLoadBundleOperation _loadUnpackBundleOp;
private FSDownloadFileOperation _unpackBundleOp;
private ESteps _steps = ESteps.None;
internal DBFSLoadRawBundleOperation(DefaultBuildinFileSystem fileSystem, PackageBundle bundle)
{
_fileSystem = fileSystem;
_bundle = bundle;
}
internal override void InternalOnStart()
{
DownloadProgress = 1f;
DownloadedBytes = _bundle.FileSize;
if (_fileSystem.CheckNeedUnpack(_bundle))
{
_steps = ESteps.UnpackRawBundleFile;
}
else
{
_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);
Result = filePath;
_steps = ESteps.CheckLoadBuildinResult;
}
if (_steps == ESteps.CheckLoadBuildinResult)
{
if (Result != null)
{
string filePath = Result as string;
if (File.Exists(filePath))
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"Can not found buildin raw bundle file : {filePath}";
}
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"Failed to load buildin raw bundle file : {_bundle.BundleName}";
}
}
}
public override void WaitForAsyncComplete()
{
while (true)
{
if (_unpackBundleOp != null)
{
if (_unpackBundleOp.IsDone == false)
_unpackBundleOp.WaitForAsyncComplete();
}
if (_loadUnpackBundleOp != null)
{
if (_loadUnpackBundleOp.IsDone == false)
_loadUnpackBundleOp.WaitForAsyncComplete();
}
// 驱动流程
InternalOnUpdate();
// 完成后退出
if (IsDone)
break;
}
}
public override void AbortDownloadOperation()
{
if (_steps == ESteps.UnpackRawBundleFile)
{
if (_unpackBundleOp != null)
_unpackBundleOp.SetAbort();
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 99fc95c784d960c45ba9373f31fbc7fe
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,110 @@
namespace YooAsset
{
internal class DBFSLoadPackageManifestOperation : FSLoadPackageManifestOperation
{
private enum ESteps
{
None,
RequestBuildinPackageVersion,
RequestBuildinPackageHash,
LoadBuildinPackageManifest,
Done,
}
private readonly DefaultBuildinFileSystem _fileSystem;
private RequestBuildinPackageVersionOperation _requestBuildinPackageVersionOp;
private RequestBuildinPackageHashOperation _requestBuildinPackageHashOp;
private LoadBuildinPackageManifestOperation _loadBuildinPackageManifestOp;
private ESteps _steps = ESteps.None;
public DBFSLoadPackageManifestOperation(DefaultBuildinFileSystem fileSystem)
{
_fileSystem = fileSystem;
}
internal override void InternalOnStart()
{
_steps = ESteps.RequestBuildinPackageVersion;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.RequestBuildinPackageVersion)
{
if (_requestBuildinPackageVersionOp == null)
{
_requestBuildinPackageVersionOp = new RequestBuildinPackageVersionOperation(_fileSystem);
OperationSystem.StartOperation(_fileSystem.PackageName, _requestBuildinPackageVersionOp);
}
if (_requestBuildinPackageVersionOp.IsDone == false)
return;
if (_requestBuildinPackageVersionOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.RequestBuildinPackageHash;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _requestBuildinPackageVersionOp.Error;
}
}
if (_steps == ESteps.RequestBuildinPackageHash)
{
if (_requestBuildinPackageHashOp == null)
{
string packageVersion = _requestBuildinPackageVersionOp.PackageVersion;
_requestBuildinPackageHashOp = new RequestBuildinPackageHashOperation(_fileSystem, packageVersion);
OperationSystem.StartOperation(_fileSystem.PackageName, _requestBuildinPackageHashOp);
}
if (_requestBuildinPackageHashOp.IsDone == false)
return;
if (_requestBuildinPackageHashOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.LoadBuildinPackageManifest;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _requestBuildinPackageHashOp.Error;
}
}
if (_steps == ESteps.LoadBuildinPackageManifest)
{
if (_loadBuildinPackageManifestOp == null)
{
string packageVersion = _requestBuildinPackageVersionOp.PackageVersion;
string packageHash = _requestBuildinPackageHashOp.PackageHash;
_loadBuildinPackageManifestOp = new LoadBuildinPackageManifestOperation(_fileSystem, packageVersion, packageHash);
OperationSystem.StartOperation(_fileSystem.PackageName, _loadBuildinPackageManifestOp);
}
if (_loadBuildinPackageManifestOp.IsDone == false)
return;
if (_loadBuildinPackageManifestOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.Done;
Result = _loadBuildinPackageManifestOp.Manifest;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _loadBuildinPackageManifestOp.Error;
}
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b55b3624add2db6489954d999b13a9ec
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: be1c19353672bb34ca3e4ddcb462402f
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,63 @@
using UnityEngine;
namespace YooAsset
{
internal sealed class LoadBuildinCatalogFileOperation : AsyncOperationBase
{
private enum ESteps
{
None,
LoadCatalog,
Done,
}
private readonly DefaultBuildinFileSystem _fileSystem;
private ESteps _steps = ESteps.None;
internal LoadBuildinCatalogFileOperation(DefaultBuildinFileSystem fileSystem)
{
_fileSystem = fileSystem;
}
internal override void InternalOnStart()
{
_steps = ESteps.LoadCatalog;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.LoadCatalog)
{
string catalogFilePath = _fileSystem.GetBuildinCatalogFileLoadPath();
var catalog = Resources.Load<DefaultBuildinFileCatalog>(catalogFilePath);
if (catalog == null)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"Failed to load catalog file : {catalogFilePath}";
return;
}
if (catalog.PackageName != _fileSystem.PackageName)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"The catalog file package name {catalog.PackageName} cannot match the file system package name {_fileSystem.PackageName}";
return;
}
foreach (var wrapper in catalog.Wrappers)
{
var fileWrapper = new DefaultBuildinFileSystem.FileWrapper(wrapper.FileName);
_fileSystem.Record(wrapper.BundleGUID, fileWrapper);
}
YooLogger.Log($"Package '{_fileSystem.PackageName}' catalog files count : {catalog.Wrappers.Count}");
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a4113df346bbd1b4ead918b52ac46f55
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,110 @@
namespace YooAsset
{
internal class LoadBuildinPackageManifestOperation : AsyncOperationBase
{
private enum ESteps
{
None,
RequestFileData,
VerifyFileData,
LoadManifest,
Done,
}
private readonly DefaultBuildinFileSystem _fileSystem;
private readonly string _packageVersion;
private readonly string _packageHash;
private UnityWebDataRequestOperation _webDataRequestOp;
private DeserializeManifestOperation _deserializer;
private ESteps _steps = ESteps.None;
/// <summary>
/// 加载的清单实例
/// </summary>
public PackageManifest Manifest { private set; get; }
internal LoadBuildinPackageManifestOperation(DefaultBuildinFileSystem fileSystem, string packageVersion, string packageHash)
{
_fileSystem = fileSystem;
_packageVersion = packageVersion;
_packageHash = packageHash;
}
internal override void InternalOnStart()
{
_steps = ESteps.RequestFileData;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.RequestFileData)
{
if (_webDataRequestOp == null)
{
string filePath = _fileSystem.GetBuildinPackageManifestFilePath(_packageVersion);
string url = DownloadSystemHelper.ConvertToWWWPath(filePath);
_webDataRequestOp = new UnityWebDataRequestOperation(url);
OperationSystem.StartOperation(_fileSystem.PackageName, _webDataRequestOp);
}
if (_webDataRequestOp.IsDone == false)
return;
if (_webDataRequestOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.VerifyFileData;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _webDataRequestOp.Error;
}
}
if (_steps == ESteps.VerifyFileData)
{
string fileHash = HashUtility.BytesMD5(_webDataRequestOp.Result);
if (fileHash == _packageHash)
{
_steps = ESteps.LoadManifest;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = "Failed to verify buildin package manifest file!";
}
}
if (_steps == ESteps.LoadManifest)
{
if (_deserializer == null)
{
_deserializer = new DeserializeManifestOperation(_webDataRequestOp.Result);
OperationSystem.StartOperation(_fileSystem.PackageName, _deserializer);
}
Progress = _deserializer.Progress;
if (_deserializer.IsDone == false)
return;
if (_deserializer.Status == EOperationStatus.Succeed)
{
_steps = ESteps.Done;
Manifest = _deserializer.Manifest;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _deserializer.Error;
}
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8af9b72837c6a01428ee627011f4341b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,75 @@
namespace YooAsset
{
internal class RequestBuildinPackageHashOperation : AsyncOperationBase
{
private enum ESteps
{
None,
RequestPackageHash,
Done,
}
private readonly DefaultBuildinFileSystem _fileSystem;
private readonly string _packageVersion;
private UnityWebTextRequestOperation _webTextRequestOp;
private ESteps _steps = ESteps.None;
/// <summary>
/// 包裹哈希值
/// </summary>
public string PackageHash { private set; get; }
internal RequestBuildinPackageHashOperation(DefaultBuildinFileSystem fileSystem, string packageVersion)
{
_fileSystem = fileSystem;
_packageVersion = packageVersion;
}
internal override void InternalOnStart()
{
_steps = ESteps.RequestPackageHash;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.RequestPackageHash)
{
if (_webTextRequestOp == null)
{
string filePath = _fileSystem.GetBuildinPackageHashFilePath(_packageVersion);
string url = DownloadSystemHelper.ConvertToWWWPath(filePath);
_webTextRequestOp = new UnityWebTextRequestOperation(url);
OperationSystem.StartOperation(_fileSystem.PackageName, _webTextRequestOp);
}
if (_webTextRequestOp.IsDone == false)
return;
if (_webTextRequestOp.Status == EOperationStatus.Succeed)
{
PackageHash = _webTextRequestOp.Result;
if (string.IsNullOrEmpty(PackageHash))
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"Buildin package hash file content is empty !";
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _webTextRequestOp.Error;
}
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b2084625d8253164aa71ef934e0690fd
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,73 @@
namespace YooAsset
{
internal class RequestBuildinPackageVersionOperation : AsyncOperationBase
{
private enum ESteps
{
None,
RequestPackageVersion,
Done,
}
private readonly DefaultBuildinFileSystem _fileSystem;
private UnityWebTextRequestOperation _webTextRequestOp;
private ESteps _steps = ESteps.None;
/// <summary>
/// 包裹版本
/// </summary>
public string PackageVersion { private set; get; }
internal RequestBuildinPackageVersionOperation(DefaultBuildinFileSystem fileSystem)
{
_fileSystem = fileSystem;
}
internal override void InternalOnStart()
{
_steps = ESteps.RequestPackageVersion;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.RequestPackageVersion)
{
if (_webTextRequestOp == null)
{
string filePath = _fileSystem.GetBuildinPackageVersionFilePath();
string url = DownloadSystemHelper.ConvertToWWWPath(filePath);
_webTextRequestOp = new UnityWebTextRequestOperation(url);
OperationSystem.StartOperation(_fileSystem.PackageName, _webTextRequestOp);
}
if (_webTextRequestOp.IsDone == false)
return;
if (_webTextRequestOp.Status == EOperationStatus.Succeed)
{
PackageVersion = _webTextRequestOp.Result;
if (string.IsNullOrEmpty(PackageVersion))
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"Buildin package version file content is empty !";
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _webTextRequestOp.Error;
}
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6bddb076e377b66488ec28c9dbdd18cd
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: