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,206 @@
using System;
using System.IO;
using System.Collections.Generic;
using UnityEngine;
namespace YooAsset
{
/// <summary>
/// 内置文件系统
/// </summary>
internal class DefaultEditorFileSystem : IFileSystem
{
protected string _packageRoot;
/// <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 0;
}
}
#region 自定义参数
/// <summary>
/// 自定义参数:模拟运行的资源清单文件路径
/// </summary>
public string SimulateManifestFilePath { private set; get; } = string.Empty;
#endregion
public DefaultEditorFileSystem()
{
}
public virtual FSInitializeFileSystemOperation InitializeFileSystemAsync()
{
var operation = new DEFSInitializeOperation(this);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
public virtual FSLoadPackageManifestOperation LoadPackageManifestAsync(params object[] args)
{
var operation = new DEFSLoadPackageManifestOperation(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 DEFSClearAllBundleFilesOperation(this);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
public virtual FSClearUnusedBundleFilesOperation ClearUnusedBundleFilesAsync(params object[] args)
{
var operation = new DEFSClearUnusedBundleFilesOperation(this);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
public virtual FSDownloadFileOperation DownloadFileAsync(params object[] args)
{
PackageBundle bundle = args[0] as PackageBundle;
var operation = new DEFSDownloadFileOperation(bundle);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
public virtual void SetParameter(string name, object value)
{
if (name == "SIMULATE_MANIFEST_FILE_PATH")
{
SimulateManifestFilePath = (string)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);
}
public virtual void OnUpdate()
{
}
public virtual bool Belong(PackageBundle bundle)
{
return true;
}
public virtual bool Belong(string bundleGUID)
{
return true;
}
public virtual bool Exists(PackageBundle bundle)
{
return true;
}
public virtual bool Exists(string bundleGUID)
{
return true;
}
public virtual bool CheckNeedDownload(PackageBundle bundle)
{
return false;
}
public virtual bool CheckNeedUnpack(PackageBundle bundle)
{
return false;
}
public virtual bool CheckNeedImport(PackageBundle bundle)
{
return false;
}
public virtual bool WriteFile(PackageBundle bundle, string copyPath)
{
return true;
}
public virtual bool DeleteFile(PackageBundle bundle)
{
return true;
}
public virtual bool DeleteFile(string bundleGUID)
{
return true;
}
public virtual EFileVerifyResult VerifyFile(PackageBundle bundle)
{
return EFileVerifyResult.Succeed;
}
public virtual byte[] ReadFileBytes(PackageBundle bundle)
{
throw new System.NotImplementedException();
}
public virtual string ReadFileText(PackageBundle bundle)
{
throw new System.NotImplementedException();
}
public virtual FSLoadBundleOperation LoadBundleFile(PackageBundle bundle)
{
var operation = new DEFSLoadBundleOperation(this, bundle);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
public virtual void UnloadBundleFile(PackageBundle bundle, object result)
{
}
#region 内部方法
protected string GetDefaultRoot()
{
return "Assets/";
}
/// <summary>
/// 记录缓存信息
/// </summary>
public bool Record(string bundleGUID, object value)
{
return true;
}
#endregion
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 53f40d241c7bcf340841a33459678568
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,7 @@
namespace YooAsset
{
internal class DefaultEditorFileSystemDefine
{
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d6d4ba58bf1a1bb4db469b2cdd741ad0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 3779423aa03ffb6499efda626280c72a
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,20 @@
namespace YooAsset
{
internal sealed class DEFSClearAllBundleFilesOperation : FSClearAllBundleFilesOperation
{
private readonly DefaultEditorFileSystem _fileSystem;
internal DEFSClearAllBundleFilesOperation(DefaultEditorFileSystem fileSystem)
{
_fileSystem = fileSystem;
}
internal override void InternalOnStart()
{
Status = EOperationStatus.Succeed;
}
internal override void InternalOnUpdate()
{
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a16dae8497d54984daa95a3682a7beb4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,20 @@
namespace YooAsset
{
internal sealed class DEFSClearUnusedBundleFilesOperation : FSClearUnusedBundleFilesOperation
{
private readonly DefaultEditorFileSystem _fileSystem;
internal DEFSClearUnusedBundleFilesOperation(DefaultEditorFileSystem fileSystem)
{
_fileSystem = fileSystem;
}
internal override void InternalOnStart()
{
Status = EOperationStatus.Succeed;
}
internal override void InternalOnUpdate()
{
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1f86d3bce4381694b8c619ceda2e7309
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,17 @@
namespace YooAsset
{
internal class DEFSDownloadFileOperation : FSDownloadFileOperation
{
internal DEFSDownloadFileOperation(PackageBundle bundle) : base(bundle)
{
}
internal override void InternalOnStart()
{
Status = EOperationStatus.Succeed;
}
internal override void InternalOnUpdate()
{
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 06a8abe71cf3fe64180ec076a79b4b66
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,20 @@
namespace YooAsset
{
internal class DEFSInitializeOperation : FSInitializeFileSystemOperation
{
private readonly DefaultEditorFileSystem _fileSytem;
internal DEFSInitializeOperation(DefaultEditorFileSystem fileSystem)
{
_fileSytem = fileSystem;
}
internal override void InternalOnStart()
{
Status = EOperationStatus.Succeed;
}
internal override void InternalOnUpdate()
{
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 99cfeba8095613442992a0e21a248b73
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,31 @@
namespace YooAsset
{
internal class DEFSLoadBundleOperation : FSLoadBundleOperation
{
private readonly DefaultEditorFileSystem _fileSystem;
private readonly PackageBundle _bundle;
internal DEFSLoadBundleOperation(DefaultEditorFileSystem fileSystem, PackageBundle bundle)
{
_fileSystem = fileSystem;
_bundle = bundle;
}
internal override void InternalOnStart()
{
DownloadProgress = 1f;
DownloadedBytes = _bundle.FileSize;
Status = EOperationStatus.Succeed;
}
internal override void InternalOnUpdate()
{
}
public override void WaitForAsyncComplete()
{
}
public override void AbortDownloadOperation()
{
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 47b871368218c7c49920927c10c6955e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,58 @@
namespace YooAsset
{
internal class DEFSLoadPackageManifestOperation : FSLoadPackageManifestOperation
{
private enum ESteps
{
None,
LoadEditorPackageManifest,
Done,
}
private readonly DefaultEditorFileSystem _fileSystem;
private LoadEditorPackageManifestOperation _loadEditorPackageManifestOp;
private ESteps _steps = ESteps.None;
internal DEFSLoadPackageManifestOperation(DefaultEditorFileSystem fileSystem)
{
_fileSystem = fileSystem;
}
internal override void InternalOnStart()
{
_steps = ESteps.LoadEditorPackageManifest;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.LoadEditorPackageManifest)
{
if (_loadEditorPackageManifestOp == null)
{
_loadEditorPackageManifestOp = new LoadEditorPackageManifestOperation(_fileSystem);
OperationSystem.StartOperation(_fileSystem.PackageName, _loadEditorPackageManifestOp);
}
Progress = _loadEditorPackageManifestOp.Progress;
if (_loadEditorPackageManifestOp.IsDone == false)
return;
if (_loadEditorPackageManifestOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.Done;
Result = _loadEditorPackageManifestOp.Manifest;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _loadEditorPackageManifestOp.Error;
}
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f3abaff43b56f62489688162296203cd
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: cf9de625784f5fe41a8d4fc53bf71ff0
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,82 @@
using System.IO;
namespace YooAsset
{
internal class LoadEditorPackageManifestOperation : AsyncOperationBase
{
private enum ESteps
{
None,
LoadFileData,
LoadManifest,
Done,
}
private readonly DefaultEditorFileSystem _fileSystem;
private DeserializeManifestOperation _deserializer;
private byte[] _fileData;
private ESteps _steps = ESteps.None;
/// <summary>
/// 加载的清单实例
/// </summary>
public PackageManifest Manifest { private set; get; }
internal LoadEditorPackageManifestOperation(DefaultEditorFileSystem fileSystem)
{
_fileSystem = fileSystem;
}
internal override void InternalOnStart()
{
_steps = ESteps.LoadFileData;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.LoadFileData)
{
string manifestFilePath = _fileSystem.SimulateManifestFilePath;
if (File.Exists(manifestFilePath))
{
_steps = ESteps.LoadManifest;
_fileData = FileUtility.ReadAllBytes(manifestFilePath);
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"Simulation manifest file not found : {manifestFilePath}";
}
}
if (_steps == ESteps.LoadManifest)
{
if (_deserializer == null)
{
_deserializer = new DeserializeManifestOperation(_fileData);
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: b6504949dca3fbf4fbb8c61c3ab9c51f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: