com.alicizax.unity.tuyoogam.../RuntimeExtension/ExtensionFileSystem/WechatFileSystem/Operation/WXFSClearUnusedBundleFilesAsync.cs

111 lines
3.8 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;
2025-05-13 10:40:30 +08:00
using System.Linq;
2025-01-09 11:31:04 +08:00
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;
2025-04-01 21:12:28 +08:00
private List<string> _unusedCacheFiles = new List<string>(1000);
2025-01-09 11:31:04 +08:00
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-05-13 10:40:30 +08:00
// 说明__GAME_FILE_CACHE/yoo/ 目录下包含所有的资源文件和清单文件
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)
{
2025-05-13 10:40:30 +08:00
// 如果是目录文件
string fileExtension = Path.GetExtension(fileStat.path);
if (string.IsNullOrEmpty(fileExtension))
continue;
// 如果是资源清单
//TODO 默认的清单文件格式
if (fileExtension == ".bytes" || fileExtension == ".hash")
continue;
// 注意:适配不同的文件命名方式!
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileStat.path);
string bundleGUID = fileNameWithoutExtension.Split('_').Last();
2025-02-28 16:11:01 +08:00
if (_manifest.TryGetPackageBundleByBundleGUID(bundleGUID, out PackageBundle value) == false)
{
2025-05-13 10:40:30 +08:00
string filePath = _fileSystem.FileRoot + fileStat.path;
if (_unusedCacheFiles.Contains(filePath) == false)
_unusedCacheFiles.Add(filePath);
2025-02-28 16:11:01 +08:00
}
}
_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