update extension sample

优化了微信小游戏文件系统缓存机制。
pull/469/head
何冠峰 2025-02-11 18:18:29 +08:00
parent efa4180340
commit 0a85f3126f
8 changed files with 47 additions and 190 deletions

View File

@ -42,7 +42,6 @@ internal class WXFSClearAllBundleFilesOperation : FSClearCacheFilesOperation
YooLogger.Log("微信缓存清理成功!");
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
_fileSystem.ClearAllRecords();
}
else
{

View File

@ -4,6 +4,7 @@ using System.IO;
using UnityEngine;
using YooAsset;
using WeChatWASM;
using static UnityEngine.Networking.UnityWebRequest;
internal class WXFSClearUnusedBundleFilesAsync : FSClearCacheFilesOperation
{
@ -11,6 +12,7 @@ internal class WXFSClearUnusedBundleFilesAsync : FSClearCacheFilesOperation
{
None,
GetUnusedCacheFiles,
WaitingSearch,
ClearUnusedCacheFiles,
Done,
}
@ -37,20 +39,44 @@ internal class WXFSClearUnusedBundleFilesAsync : FSClearCacheFilesOperation
if (_steps == ESteps.GetUnusedCacheFiles)
{
_unusedCacheFiles = GetUnusedCacheFiles();
_unusedFileTotalCount = _unusedCacheFiles.Count;
_steps = ESteps.ClearUnusedCacheFiles;
YooLogger.Log($"Found unused cache files count : {_unusedFileTotalCount}");
_steps = ESteps.WaitingSearch;
var fileSystemMgr = _fileSystem.GetFileSystemMgr();
var statOption = new WXStatOption();
statOption.path = _fileSystem.FileRoot;
statOption.recursive = true;
statOption.success = (WXStatResponse response) =>
{
foreach (var fileStat in response.stats)
{
// 注意存储文件必须按照Bundle文件哈希值存储
string bundleGUID = Path.GetFileNameWithoutExtension(fileStat.path);
if (_manifest.TryGetPackageBundleByBundleGUID(bundleGUID, out PackageBundle value) == false)
{
_unusedCacheFiles.Add(fileStat.path);
}
}
_steps = ESteps.ClearUnusedCacheFiles;
_unusedFileTotalCount = _unusedCacheFiles.Count;
YooLogger.Log($"Found unused cache files count : {_unusedFileTotalCount}");
};
statOption.fail = (WXStatResponse response) =>
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = response.errMsg;
};
fileSystemMgr.Stat(statOption);
}
if (_steps == ESteps.ClearUnusedCacheFiles)
{
for (int i = _unusedCacheFiles.Count - 1; i >= 0; i--)
{
string clearFilePath = _unusedCacheFiles[i];
string filePath = _unusedCacheFiles[i];
_unusedCacheFiles.RemoveAt(i);
_fileSystem.ClearRecord(clearFilePath);
WX.RemoveFile(clearFilePath, null);
WX.RemoveFile(filePath, null);
if (OperationSystem.IsBusy)
break;
@ -68,30 +94,5 @@ internal class WXFSClearUnusedBundleFilesAsync : FSClearCacheFilesOperation
}
}
}
private List<string> GetUnusedCacheFiles()
{
var allRecords = _fileSystem.GetAllRecords();
List<string> result = new List<string>(allRecords.Count);
foreach (var filePath in allRecords)
{
// 如果存储文件名是按照Bundle文件哈希值存储
string bundleGUID = Path.GetFileNameWithoutExtension(filePath);
if (_manifest.TryGetPackageBundleByBundleGUID(bundleGUID, out PackageBundle value) == false)
{
result.Add(filePath);
}
// 如果存储文件名是按照Bundle文件名称存储
/*
string bundleName = Path.GetFileNameWithoutExtension(filePath);
if (_manifest.TryGetPackageBundleByBundleName(bundleName, out PackageBundle value) == false)
{
result.Add(filePath);
}
*/
}
return result;
}
}
#endif

View File

@ -51,7 +51,6 @@ internal class WXFSDownloadFileOperation : DefaultDownloadFileOperation
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
_fileSystem.TryRecordBundle(Bundle); //记录下载文件
//TODO 解决微信小游戏插件问题
// Issue : https://github.com/wechat-miniprogram/minigame-unity-webgl-transform/issues/108#

View File

@ -3,16 +3,7 @@ using YooAsset;
internal partial class WXFSInitializeOperation : FSInitializeFileSystemOperation
{
private enum ESteps
{
None,
RecordCacheFiles,
Done,
}
private readonly WechatFileSystem _fileSystem;
private RecordWechatCacheFilesOperation _recordWechatCacheFilesOp;
private ESteps _steps = ESteps.None;
public WXFSInitializeOperation(WechatFileSystem fileSystem)
{
@ -20,36 +11,10 @@ internal partial class WXFSInitializeOperation : FSInitializeFileSystemOperation
}
internal override void InternalOnStart()
{
_steps = ESteps.RecordCacheFiles;
Status = EOperationStatus.Succeed;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.RecordCacheFiles)
{
if (_recordWechatCacheFilesOp == null)
{
_recordWechatCacheFilesOp = new RecordWechatCacheFilesOperation(_fileSystem);
OperationSystem.StartOperation(_fileSystem.PackageName, _recordWechatCacheFilesOp);
}
if (_recordWechatCacheFilesOp.IsDone == false)
return;
if (_recordWechatCacheFilesOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _recordWechatCacheFilesOp.Error;
}
}
}
}
#endif

View File

@ -61,7 +61,6 @@ internal class WXFSLoadBundleOperation : FSLoadBundleOperation
_steps = ESteps.Done;
Result = new WXAssetBundleResult(_fileSystem, _bundle, assetBundle);
Status = EOperationStatus.Succeed;
_fileSystem.TryRecordBundle(_bundle); //记录下载文件
//TODO 解决微信小游戏插件问题
// Issue : https://github.com/wechat-miniprogram/minigame-unity-webgl-transform/issues/108#

View File

@ -1,60 +0,0 @@
#if UNITY_WEBGL && WEIXINMINIGAME
using YooAsset;
using WeChatWASM;
using System.IO;
internal class RecordWechatCacheFilesOperation : AsyncOperationBase
{
private enum ESteps
{
None,
RecordCacheFiles,
WaitResponse,
Done,
}
private readonly WechatFileSystem _fileSystem;
private ESteps _steps = ESteps.None;
public RecordWechatCacheFilesOperation(WechatFileSystem fileSystem)
{
_fileSystem = fileSystem;
}
internal override void InternalOnStart()
{
_steps = ESteps.RecordCacheFiles;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.RecordCacheFiles)
{
_steps = ESteps.WaitResponse;
var fileSystemMgr = _fileSystem.GetFileSystemMgr();
var statOption = new WXStatOption();
statOption.path = _fileSystem.FileRoot;
statOption.recursive = true;
statOption.success = (WXStatResponse response) =>
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
foreach (var fileStat in response.stats)
{
//TODO 需要确认存储文件为Bundle文件
_fileSystem.RecordBundleFile(_fileSystem.FileRoot + fileStat.path);
}
};
statOption.fail = (WXStatResponse response) =>
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = response.errMsg;
};
fileSystemMgr.Stat(statOption);
}
}
}
#endif

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 640ec7bd883b8314db53b508278aea6e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -53,8 +53,7 @@ internal class WechatFileSystem : IFileSystem
}
}
private readonly HashSet<string> _recorders = new HashSet<string>();
private readonly Dictionary<string, string> _cacheFilePaths = new Dictionary<string, string>(10000);
private readonly Dictionary<string, string> _cacheFilePathMapping = new Dictionary<string, string>(10000);
private WXFileSystemManager _fileSystemMgr;
private string _wxCacheRoot = string.Empty;
@ -83,7 +82,7 @@ internal class WechatFileSystem : IFileSystem
{
get
{
return _recorders.Count;
return 0;
}
}
@ -174,10 +173,10 @@ internal class WechatFileSystem : IFileSystem
YooLogger.Warning($"Invalid parameter : {name}");
}
}
public virtual void OnCreate(string packageName, string rootDirectory)
public virtual void OnCreate(string packageName, string packageRoot)
{
PackageName = packageName;
_wxCacheRoot = rootDirectory;
_wxCacheRoot = packageRoot;
if (string.IsNullOrEmpty(_wxCacheRoot))
{
@ -204,7 +203,7 @@ internal class WechatFileSystem : IFileSystem
public virtual bool Exists(PackageBundle bundle)
{
string filePath = GetCacheFileLoadPath(bundle);
return _recorders.Contains(filePath);
return CheckCacheFileExist(filePath);
}
public virtual bool NeedDownload(PackageBundle bundle)
{
@ -250,55 +249,21 @@ internal class WechatFileSystem : IFileSystem
}
public bool CheckCacheFileExist(string filePath)
{
string result = _fileSystemMgr.AccessSync(filePath);
return result.Equals("access:ok");
string result = WX.GetCachePath(filePath);
if (string.IsNullOrEmpty(result))
return false;
else
return true;
}
public string GetCacheFileLoadPath(PackageBundle bundle)
{
if (_cacheFilePaths.TryGetValue(bundle.BundleGUID, out string filePath) == false)
if (_cacheFilePathMapping.TryGetValue(bundle.BundleGUID, out string filePath) == false)
{
filePath = PathUtility.Combine(_wxCacheRoot, "__GAME_FILE_CACHE", _packageRoot, bundle.FileName);
_cacheFilePaths.Add(bundle.BundleGUID, filePath);
filePath = PathUtility.Combine(_wxCacheRoot, bundle.FileName);
_cacheFilePathMapping.Add(bundle.BundleGUID, filePath);
}
return filePath;
}
#endregion
#region 本地记录
public List<string> GetAllRecords()
{
return _recorders.ToList();
}
public bool RecordBundleFile(string filePath)
{
if (_recorders.Contains(filePath))
{
YooLogger.Error($"{nameof(WechatFileSystem)} has element : {filePath}");
return false;
}
_recorders.Add(filePath);
return true;
}
public void TryRecordBundle(PackageBundle bundle)
{
string filePath = GetCacheFileLoadPath(bundle);
if (_recorders.Contains(filePath) == false)
{
_recorders.Add(filePath);
}
}
public void ClearAllRecords()
{
_recorders.Clear();
}
public void ClearRecord(string filePath)
{
if (_recorders.Contains(filePath))
{
_recorders.Remove(filePath);
}
}
#endregion
}
#endif