update file system

重构运行时核心代码
This commit is contained in:
何冠峰
2024-12-24 18:23:19 +08:00
parent 6d6fd3af2c
commit 6254d00bb5
67 changed files with 1811 additions and 204 deletions

View File

@@ -21,7 +21,6 @@ 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;
@@ -64,11 +63,6 @@ namespace YooAsset
/// </summary>
public bool AppendFileExtension { private set; get; } = false;
/// <summary>
/// 自定义参数:原生文件构建管线
/// </summary>
public bool RawFileBuildPipeline { private set; get; } = false;
/// <summary>
/// 自定义参数:解密方法类
/// </summary>
@@ -113,7 +107,13 @@ namespace YooAsset
return _unpackFileSystem.LoadBundleFile(bundle);
}
if (RawFileBuildPipeline)
if (bundle.BundleType == (int)EBuildBundleType.AssetBundle)
{
var operation = new DBFSLoadAssetBundleOperation(this, bundle);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
else if (bundle.BundleType == (int)EBuildBundleType.RawBundle)
{
var operation = new DBFSLoadRawBundleOperation(this, bundle);
OperationSystem.StartOperation(PackageName, operation);
@@ -121,37 +121,12 @@ namespace YooAsset
}
else
{
var operation = new DBFSLoadAssetBundleOperation(this, bundle);
string error = $"{nameof(DefaultBuildinFileSystem)} not support load bundle type : {bundle.BundleType}";
var operation = new FSLoadBundleCompleteOperation(error);
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))
{
if (managedStream != null)
{
managedStream.Close();
managedStream.Dispose();
}
_loadedStream.Remove(bundle.BundleGUID);
}
}
}
public virtual void SetParameter(string name, object value)
{
@@ -163,10 +138,6 @@ namespace YooAsset
{
AppendFileExtension = (bool)value;
}
else if (name == FileSystemParametersDefine.RAW_FILE_BUILD_PIPELINE)
{
RawFileBuildPipeline = (bool)value;
}
else if (name == FileSystemParametersDefine.DECRYPTION_SERVICES)
{
DecryptionServices = (IDecryptionServices)value;
@@ -191,7 +162,6 @@ namespace YooAsset
_unpackFileSystem.SetParameter(FileSystemParametersDefine.REMOTE_SERVICES, remoteServices);
_unpackFileSystem.SetParameter(FileSystemParametersDefine.FILE_VERIFY_LEVEL, FileVerifyLevel);
_unpackFileSystem.SetParameter(FileSystemParametersDefine.APPEND_FILE_EXTENSION, AppendFileExtension);
_unpackFileSystem.SetParameter(FileSystemParametersDefine.RAW_FILE_BUILD_PIPELINE, RawFileBuildPipeline);
_unpackFileSystem.SetParameter(FileSystemParametersDefine.DECRYPTION_SERVICES, DecryptionServices);
_unpackFileSystem.OnCreate(packageName, null);
}
@@ -218,7 +188,7 @@ namespace YooAsset
return false;
#if UNITY_ANDROID
return RawFileBuildPipeline || bundle.Encrypted;
return bundle.BundleType == (int)EBuildBundleType.RawBundle || bundle.Encrypted;
#else
return false;
#endif
@@ -228,10 +198,17 @@ namespace YooAsset
return false;
}
public virtual byte[] ReadFileData(PackageBundle bundle)
public virtual string GetBundleFilePath(PackageBundle bundle)
{
if (NeedUnpack(bundle))
return _unpackFileSystem.ReadFileData(bundle);
return _unpackFileSystem.GetBundleFilePath(bundle);
return GetBuildinFileLoadPath(bundle);
}
public virtual byte[] ReadBundleFileData(PackageBundle bundle)
{
if (NeedUnpack(bundle))
return _unpackFileSystem.ReadBundleFileData(bundle);
if (Exists(bundle) == false)
return null;
@@ -259,10 +236,10 @@ namespace YooAsset
return FileUtility.ReadAllBytes(filePath);
}
}
public virtual string ReadFileText(PackageBundle bundle)
public virtual string ReadBundleFileText(PackageBundle bundle)
{
if (NeedUnpack(bundle))
return _unpackFileSystem.ReadFileText(bundle);
return _unpackFileSystem.ReadBundleFileText(bundle);
if (Exists(bundle) == false)
return null;
@@ -350,9 +327,9 @@ namespace YooAsset
}
/// <summary>
/// 加载加密资源文件
/// 加载加密资源文件
/// </summary>
public AssetBundle LoadEncryptedAssetBundle(PackageBundle bundle)
public DecryptResult LoadEncryptedAssetBundle(PackageBundle bundle)
{
string filePath = GetBuildinFileLoadPath(bundle);
var fileInfo = new DecryptFileInfo()
@@ -361,16 +338,13 @@ namespace YooAsset
FileLoadCRC = bundle.UnityCRC,
FileLoadPath = filePath,
};
var assetBundle = DecryptionServices.LoadAssetBundle(fileInfo, out var managedStream);
_loadedStream.Add(bundle.BundleGUID, managedStream);
return assetBundle;
return DecryptionServices.LoadAssetBundle(fileInfo);
}
/// <summary>
/// 加载加密资源文件
/// 加载加密资源文件
/// </summary>
public AssetBundleCreateRequest LoadEncryptedAssetBundleAsync(PackageBundle bundle)
public DecryptResult LoadEncryptedAssetBundleAsync(PackageBundle bundle)
{
string filePath = GetBuildinFileLoadPath(bundle);
var fileInfo = new DecryptFileInfo()
@@ -379,10 +353,7 @@ namespace YooAsset
FileLoadCRC = bundle.UnityCRC,
FileLoadPath = filePath,
};
var createRequest = DecryptionServices.LoadAssetBundleAsync(fileInfo, out var managedStream);
_loadedStream.Add(bundle.BundleGUID, managedStream);
return createRequest;
return DecryptionServices.LoadAssetBundleAsync(fileInfo);
}
#endregion
}

View File

@@ -19,6 +19,8 @@ namespace YooAsset
private readonly DefaultBuildinFileSystem _fileSystem;
private readonly PackageBundle _bundle;
private AssetBundleCreateRequest _createRequest;
private AssetBundle _assetBundle;
private Stream _managedStream;
private ESteps _steps = ESteps.None;
@@ -56,19 +58,23 @@ namespace YooAsset
{
if (_bundle.Encrypted)
{
Result = _fileSystem.LoadEncryptedAssetBundle(_bundle);
var decryptResult = _fileSystem.LoadEncryptedAssetBundle(_bundle);
_assetBundle = decryptResult.Result;
_managedStream = decryptResult.ManagedStream;
}
else
{
string filePath = _fileSystem.GetBuildinFileLoadPath(_bundle);
Result = AssetBundle.LoadFromFile(filePath);
_assetBundle = AssetBundle.LoadFromFile(filePath);
}
}
else
{
if (_bundle.Encrypted)
{
_createRequest = _fileSystem.LoadEncryptedAssetBundleAsync(_bundle);
var decryptResult = _fileSystem.LoadEncryptedAssetBundleAsync(_bundle);
_createRequest = decryptResult.CreateRequest;
_managedStream = decryptResult.ManagedStream;
}
else
{
@@ -88,19 +94,20 @@ namespace YooAsset
{
// 强制挂起主线程(注意:该操作会很耗时)
YooLogger.Warning("Suspend the main thread to load unity bundle.");
Result = _createRequest.assetBundle;
_assetBundle = _createRequest.assetBundle;
}
else
{
if (_createRequest.isDone == false)
return;
Result = _createRequest.assetBundle;
_assetBundle = _createRequest.assetBundle;
}
}
if (Result != null)
if (_assetBundle != null)
{
_steps = ESteps.Done;
Result = new AssetBundleResult(_fileSystem, _bundle, _assetBundle, _managedStream);
Status = EOperationStatus.Succeed;
return;
}
@@ -176,7 +183,7 @@ namespace YooAsset
if (File.Exists(filePath))
{
_steps = ESteps.Done;
Result = new RawBundle(_fileSystem, _bundle, filePath);
Result = new RawBundleResult(_fileSystem, _bundle);
Status = EOperationStatus.Succeed;
}
else