com.alicizax.unity.tuyoogam.../Samples~/Extension Sample/Runtime/ExtensionFileSystem/WechatFileSystem/Operation/WXFSClearUnusedBundleFilesAsync.cs

98 lines
3.2 KiB
C#
Raw Normal View History

2025-01-09 11:31:04 +08:00
#if UNITY_WEBGL && WEIXINMINIGAME
using System.Collections.Generic;
using System.IO;
using YooAsset;
using WeChatWASM;
2025-02-05 13:06:28 +08:00
internal class WXFSClearUnusedBundleFilesAsync : FSClearCacheFilesOperation
2025-01-09 11:31:04 +08:00
{
private enum ESteps
{
None,
GetUnusedCacheFiles,
2025-02-28 16:11:01 +08:00
WaitingSearch,
2025-01-09 11:31:04 +08:00
ClearUnusedCacheFiles,
Done,
}
private readonly WechatFileSystem _fileSystem;
private readonly PackageManifest _manifest;
private List<string> _unusedCacheFiles;
private int _unusedFileTotalCount = 0;
private ESteps _steps = ESteps.None;
internal WXFSClearUnusedBundleFilesAsync(WechatFileSystem fileSystem, PackageManifest manifest)
{
_fileSystem = fileSystem;
_manifest = manifest;
}
2025-02-28 16:11:01 +08:00
internal override void InternalStart()
2025-01-09 11:31:04 +08:00
{
2025-02-28 16:11:01 +08:00
_steps = ESteps.GetUnusedCacheFiles;
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-02-28 16:11:01 +08:00
if (_steps == ESteps.GetUnusedCacheFiles)
2025-01-09 11:31:04 +08:00
{
2025-02-28 16:11:01 +08:00
_steps = ESteps.WaitingSearch;
2025-01-09 11:31:04 +08:00
2025-02-28 16:11:01 +08:00
var fileSystemMgr = _fileSystem.GetFileSystemMgr();
var statOption = new WXStatOption();
statOption.path = _fileSystem.FileRoot;
statOption.recursive = true;
statOption.success = (WXStatResponse response) =>
2025-01-09 11:31:04 +08:00
{
2025-02-28 16:11:01 +08:00
foreach (var fileStat in response.stats)
{
// 注意存储文件必须按照Bundle文件哈希值存储
string bundleGUID = Path.GetFileNameWithoutExtension(fileStat.path);
if (_manifest.TryGetPackageBundleByBundleGUID(bundleGUID, out PackageBundle value) == false)
{
string fullPath = WX.GetCachePath(fileStat.path);
if (_unusedCacheFiles.Contains(fullPath) == false)
_unusedCacheFiles.Add(fullPath);
}
}
_steps = ESteps.ClearUnusedCacheFiles;
_unusedFileTotalCount = _unusedCacheFiles.Count;
YooLogger.Log($"Found unused cache files count : {_unusedFileTotalCount}");
2025-01-09 11:31:04 +08:00
};
2025-02-28 16:11:01 +08:00
statOption.fail = (WXStatResponse response) =>
2025-01-09 11:31:04 +08:00
{
2025-02-28 16:11:01 +08:00
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = response.errMsg;
2025-01-09 11:31:04 +08:00
};
2025-02-28 16:11:01 +08:00
fileSystemMgr.Stat(statOption);
2025-01-09 11:31:04 +08:00
}
if (_steps == ESteps.ClearUnusedCacheFiles)
{
for (int i = _unusedCacheFiles.Count - 1; i >= 0; i--)
{
2025-02-28 16:11:01 +08:00
string filePath = _unusedCacheFiles[i];
2025-01-09 11:31:04 +08:00
_unusedCacheFiles.RemoveAt(i);
2025-02-28 16:11:01 +08:00
WX.RemoveFile(filePath, null);
2025-01-09 11:31:04 +08:00
if (OperationSystem.IsBusy)
break;
}
if (_unusedFileTotalCount == 0)
Progress = 1.0f;
else
Progress = 1.0f - (_unusedCacheFiles.Count / _unusedFileTotalCount);
if (_unusedCacheFiles.Count == 0)
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
}
}
}
#endif