com.alicizax.unity.tuyoogam.../Runtime/FileSystem/DefaultBuildinFileSystem/Operation/internal/LoadBuildinCatalogFileOperation.cs

99 lines
3.1 KiB
C#
Raw Normal View History

2025-04-01 21:12:28 +08:00
using System;
2025-09-10 16:04:39 +08:00
using System.IO;
2025-01-09 11:31:04 +08:00
namespace YooAsset
{
internal sealed class LoadBuildinCatalogFileOperation : AsyncOperationBase
{
private enum ESteps
{
None,
2025-09-10 16:04:39 +08:00
TryLoadFileData,
RequestFileData,
2025-01-09 11:31:04 +08:00
LoadCatalog,
Done,
}
private readonly DefaultBuildinFileSystem _fileSystem;
2025-04-01 21:12:28 +08:00
private UnityWebDataRequestOperation _webDataRequestOp;
2025-09-10 16:04:39 +08:00
private byte[] _fileData;
2025-01-09 11:31:04 +08:00
private ESteps _steps = ESteps.None;
2025-09-10 16:04:39 +08:00
/// <summary>
/// 内置资源目录
/// </summary>
public DefaultBuildinFileCatalog Catalog;
2025-01-09 11:31:04 +08:00
internal LoadBuildinCatalogFileOperation(DefaultBuildinFileSystem fileSystem)
{
_fileSystem = fileSystem;
}
2025-02-28 16:11:01 +08:00
internal override void InternalStart()
2025-01-09 11:31:04 +08:00
{
2025-09-10 16:04:39 +08:00
_steps = ESteps.TryLoadFileData;
2025-01-09 11:31:04 +08:00
}
2025-02-28 16:11:01 +08:00
internal override void InternalUpdate()
2025-01-09 11:31:04 +08:00
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
2025-09-10 16:04:39 +08:00
if (_steps == ESteps.TryLoadFileData)
{
string filePath = _fileSystem.GetCatalogBinaryFileLoadPath();
if (File.Exists(filePath))
{
_fileData = File.ReadAllBytes(filePath);
_steps = ESteps.LoadCatalog;
}
else
{
_steps = ESteps.RequestFileData;
}
}
if (_steps == ESteps.RequestFileData)
2025-01-09 11:31:04 +08:00
{
2025-04-01 21:12:28 +08:00
if (_webDataRequestOp == null)
2025-01-09 11:31:04 +08:00
{
2025-04-01 21:12:28 +08:00
string filePath = _fileSystem.GetCatalogBinaryFileLoadPath();
string url = DownloadSystemHelper.ConvertToWWWPath(filePath);
2025-09-02 19:21:49 +08:00
_webDataRequestOp = new UnityWebDataRequestOperation(url, 60);
2025-04-01 21:12:28 +08:00
_webDataRequestOp.StartOperation();
AddChildOperation(_webDataRequestOp);
2025-01-09 11:31:04 +08:00
}
2025-04-01 21:12:28 +08:00
_webDataRequestOp.UpdateOperation();
if (_webDataRequestOp.IsDone == false)
return;
if (_webDataRequestOp.Status == EOperationStatus.Succeed)
{
2025-09-10 16:04:39 +08:00
_fileData = _webDataRequestOp.Result;
2025-04-01 21:12:28 +08:00
_steps = ESteps.LoadCatalog;
}
else
2025-01-09 11:31:04 +08:00
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
2025-04-01 21:12:28 +08:00
Error = _webDataRequestOp.Error;
2025-01-09 11:31:04 +08:00
}
2025-04-01 21:12:28 +08:00
}
2025-01-09 11:31:04 +08:00
2025-04-01 21:12:28 +08:00
if (_steps == ESteps.LoadCatalog)
{
try
2025-01-09 11:31:04 +08:00
{
2025-09-10 16:04:39 +08:00
Catalog = CatalogTools.DeserializeFromBinary(_fileData);
2025-04-01 21:12:28 +08:00
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
catch (Exception e)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"Failed to load catalog file : {e.Message}";
}
2025-01-09 11:31:04 +08:00
}
}
}
}