update wechat file system

pull/379/head
何冠峰 2024-09-30 18:00:02 +08:00
parent 8e7a43275f
commit c0df366676
4 changed files with 91 additions and 297 deletions

View File

@ -12,9 +12,6 @@ namespace YooAsset
private static GameObject _driver = null; private static GameObject _driver = null;
private static readonly List<ResourcePackage> _packages = new List<ResourcePackage>(); private static readonly List<ResourcePackage> _packages = new List<ResourcePackage>();
public const string DefaultPackageVersion_Key = "DefaultPackageVersion_Key";
public const string DefaultPcakageVersion = "100000";
/// <summary> /// <summary>
/// 是否已经初始化 /// 是否已经初始化
/// </summary> /// </summary>

View File

@ -1,87 +1,48 @@
#if UNITY_WEBGL && WEIXINMINIGAME #if UNITY_WEBGL && WEIXINMINIGAME
using System.Collections.Generic; using System.Collections.Generic;
using WeChatWASM;
using YooAsset;
using UnityEngine;
using System.Linq; using System.Linq;
using UnityEngine;
using YooAsset;
using WeChatWASM;
internal class WXFSClearAllBundleFilesOperation : FSClearAllBundleFilesOperation internal class WXFSClearAllBundleFilesOperation : FSClearAllBundleFilesOperation
{ {
private enum ESteps private enum ESteps
{ {
None, None,
GetAllCacheFiles, ClearAllCacheFiles,
ClearAllWXCacheBundleFiles,
Done, Done,
} }
private List<string> _wxBundleFilePaths; private readonly WechatFileSystem _fileSystem;
private int _fileTotalCount = 0;
private WechatFileSystem _fileSystem;
private ESteps _steps = ESteps.None; private ESteps _steps = ESteps.None;
internal WXFSClearAllBundleFilesOperation(WechatFileSystem fileSystem) internal WXFSClearAllBundleFilesOperation(WechatFileSystem fileSystem)
{ {
_fileSystem = fileSystem; _fileSystem = fileSystem;
var allCacheFilePathDic = _fileSystem.GetWXAllCacheFilePath();
_wxBundleFilePaths = allCacheFilePathDic.Values.ToList();
} }
internal override void InternalOnStart() internal override void InternalOnStart()
{ {
_steps = ESteps.GetAllCacheFiles; _steps = ESteps.ClearAllCacheFiles;
} }
internal override void InternalOnUpdate() internal override void InternalOnUpdate()
{ {
if (_steps == ESteps.None || _steps == ESteps.Done) if (_steps == ESteps.None || _steps == ESteps.Done)
return; return;
if (_steps == ESteps.GetAllCacheFiles) if (_steps == ESteps.ClearAllCacheFiles)
{ {
if (_wxBundleFilePaths == null) WX.CleanAllFileCache((bool isOk) =>
{ {
_steps = ESteps.Done; if (isOk)
Status = EOperationStatus.Failed; YooLogger.Log("微信缓存清理成功!");
return;
}
else
{
_steps = ESteps.ClearAllWXCacheBundleFiles;
_fileTotalCount = _wxBundleFilePaths.Count;
}
}
if (_steps == ESteps.ClearAllWXCacheBundleFiles)
{
for (int i = _wxBundleFilePaths.Count - 1; i >= 0; i--)
{
string bundlePath = _wxBundleFilePaths[i];
if (_fileSystem.CheckWXFileIsExist(bundlePath))
{
WX.RemoveFile(bundlePath, (bool isOk) =>
{
Debug.Log($"{_wxBundleFilePaths.Count}---删除缓存文件路径成功====={bundlePath}==");
_wxBundleFilePaths.Remove(bundlePath);
});
}
else else
{ YooLogger.Log("微信缓存清理失败!");
_wxBundleFilePaths.Remove(bundlePath); });
//Debug.LogWarning($"Not Exit Cache file:{bundlePath}");
}
if (OperationSystem.IsBusy) _steps = ESteps.Done;
break; Status = EOperationStatus.Succeed;
}
if (_fileTotalCount == 0)
Progress = 1.0f;
else
Progress = 1.0f - (_wxBundleFilePaths.Count / _fileTotalCount);
if (_wxBundleFilePaths.Count == 0)
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
} }
} }
} }

View File

@ -1,35 +1,29 @@
#if UNITY_WEBGL && WEIXINMINIGAME #if UNITY_WEBGL && WEIXINMINIGAME
using System.Collections.Generic; using System.Collections.Generic;
using WeChatWASM; using System.IO;
using YooAsset;
using UnityEngine; using UnityEngine;
using System.Linq; using YooAsset;
using WeChatWASM;
internal class WXFSClearUnusedBundleFilesAsync : FSClearUnusedBundleFilesOperation internal class WXFSClearUnusedBundleFilesAsync : FSClearUnusedBundleFilesOperation
{ {
private enum ESteps private enum ESteps
{ {
None, None,
LoadCachePackageInfo, GetAllCacheFiles,
VerifyFileData, WaitResult,
LoadManifest,
GetUnusedCacheFiles, GetUnusedCacheFiles,
ClearUnusedCacheFiles, ClearUnusedCacheFiles,
Done, Done,
} }
private WechatFileSystem _fileSystem; private readonly WechatFileSystem _fileSystem;
private readonly PackageManifest _manifest; private readonly PackageManifest _manifest;
private PackageManifest _cacheManifest; private List<string> _unusedCacheFiles;
private List<string> _unusedBundleGUIDs;
private ESteps _steps = ESteps.None;
private DeserializeManifestOperation _deserializer;
private byte[] _fileData;
private string _packageHash;
private int _unusedFileTotalCount = 0; private int _unusedFileTotalCount = 0;
private string _lastPackageVersion; private GetSavedFileListSuccessCallbackResult _result;
private string _cacheManifestHashPath; private ESteps _steps = ESteps.None;
private string _cacheManifestPath;
internal WXFSClearUnusedBundleFilesAsync(WechatFileSystem fileSystem, PackageManifest manifest) internal WXFSClearUnusedBundleFilesAsync(WechatFileSystem fileSystem, PackageManifest manifest)
{ {
@ -38,98 +32,54 @@ internal class WXFSClearUnusedBundleFilesAsync : FSClearUnusedBundleFilesOperati
} }
internal override void InternalOnStart() internal override void InternalOnStart()
{ {
_steps = ESteps.LoadCachePackageInfo; _steps = ESteps.GetAllCacheFiles;
} }
internal override void InternalOnUpdate() internal override void InternalOnUpdate()
{ {
if (_steps == ESteps.None || _steps == ESteps.Done) if (_steps == ESteps.None || _steps == ESteps.Done)
return; return;
if (_steps == ESteps.LoadCachePackageInfo) if (_steps == ESteps.GetAllCacheFiles)
{ {
LoadManifestInfo(); _steps = ESteps.WaitResult;
if(_fileData != null && _fileData.Length > 0 && !string.IsNullOrEmpty(_packageHash))
{ var fileSystemMgr = WX.GetFileSystemManager();
_steps = ESteps.VerifyFileData; var option = new GetSavedFileListOption();
} fileSystemMgr.GetSavedFileList(option);
else option.fail += (FileError error) =>
{ {
_steps = ESteps.Done; _steps = ESteps.Done;
Error = error.errMsg;
Status = EOperationStatus.Failed; Status = EOperationStatus.Failed;
Error = "Failed to load cache package manifest file!"; };
} option.success += (GetSavedFileListSuccessCallbackResult result) =>
}
if(_steps == ESteps.VerifyFileData)
{
string fileHash = HashUtility.BytesMD5(_fileData);
if (fileHash == _packageHash)
{
_steps = ESteps.LoadManifest;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = "Failed to verify cache package manifest file!";
}
}
if (_steps == ESteps.LoadManifest)
{
if (_deserializer == null)
{
_deserializer = new DeserializeManifestOperation(_fileData);
OperationSystem.StartOperation(_fileSystem.PackageName, _deserializer);
}
Progress = _deserializer.Progress;
if (_deserializer.IsDone == false)
return;
if (_deserializer.Status == EOperationStatus.Succeed)
{ {
_result = result;
_steps = ESteps.GetUnusedCacheFiles; _steps = ESteps.GetUnusedCacheFiles;
_cacheManifest = _deserializer.Manifest; };
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _deserializer.Error;
}
} }
if(_steps == ESteps.GetUnusedCacheFiles) if (_steps == ESteps.WaitResult)
{ {
_unusedBundleGUIDs = GetUnusedBundleGUIDs(); return;
_unusedFileTotalCount = _unusedBundleGUIDs.Count; }
if (_steps == ESteps.GetUnusedCacheFiles)
{
_unusedCacheFiles = GetUnusedCacheFiles();
_unusedFileTotalCount = _unusedCacheFiles.Count;
_steps = ESteps.ClearUnusedCacheFiles; _steps = ESteps.ClearUnusedCacheFiles;
YooLogger.Log($"Found unused cache files count : {_unusedFileTotalCount}"); YooLogger.Log($"Found unused cache files count : {_unusedFileTotalCount}");
} }
if (_steps == ESteps.ClearUnusedCacheFiles) if (_steps == ESteps.ClearUnusedCacheFiles)
{ {
for (int i = _unusedBundleGUIDs.Count - 1; i >= 0; i--) for (int i = _unusedCacheFiles.Count - 1; i >= 0; i--)
{ {
string bundleGUID = _unusedBundleGUIDs[i]; string cacheFilePath = _unusedCacheFiles[i];
PackageBundle bundle = null; WX.RemoveFile(cacheFilePath, null);
if(_cacheManifest.TryGetPackageBundleByBundleGUID(bundleGUID,out bundle)) _unusedCacheFiles.RemoveAt(i);
{
if (bundle != null)
{
var cachePath = GetCachePathByFileName(bundle.FileName);
WX.RemoveFile(cachePath, (bool isOk) =>
{
Debug.Log($"{_unusedBundleGUIDs.Count}---删除缓存文件路径成功====={cachePath}==");
//_unusedBundleGUIDs.Remove(cachePath);
});
//_fileSystem.DeleteCacheFile(bundleGUID);
_unusedBundleGUIDs.RemoveAt(i);
}
}
if (OperationSystem.IsBusy) if (OperationSystem.IsBusy)
break; break;
} }
@ -137,74 +87,38 @@ internal class WXFSClearUnusedBundleFilesAsync : FSClearUnusedBundleFilesOperati
if (_unusedFileTotalCount == 0) if (_unusedFileTotalCount == 0)
Progress = 1.0f; Progress = 1.0f;
else else
Progress = 1.0f - (_unusedBundleGUIDs.Count / _unusedFileTotalCount); Progress = 1.0f - (_unusedCacheFiles.Count / _unusedFileTotalCount);
if (_unusedBundleGUIDs.Count == 0) if (_unusedCacheFiles.Count == 0)
{ {
CheckPackageVerion();
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EOperationStatus.Succeed; Status = EOperationStatus.Succeed;
} }
} }
} }
private List<string> GetUnusedBundleGUIDs() private List<string> GetUnusedCacheFiles()
{ {
var allBundleGUIDs = _cacheManifest.BundleDic3.Keys.ToList(); List<string> result = new List<string>(_result.fileList.Length);
List<string> result = new List<string>(allBundleGUIDs.Count); foreach (var fileInfo in _result.fileList)
foreach (var bundleGUID in allBundleGUIDs)
{ {
if (_manifest.IsIncludeBundleFile(bundleGUID) == false) // 如果存储文件名是按照Bundle文件哈希值存储
string bundleGUID = Path.GetFileNameWithoutExtension(fileInfo.filePath);
if (_manifest.TryGetPackageBundleByBundleGUID(bundleGUID, out PackageBundle value) == false)
{ {
result.Add(bundleGUID); result.Add(fileInfo.filePath);
} }
// 如果存储文件名是按照Bundle文件名称存储
/*
string bundleName = Path.GetFileNameWithoutExtension(fileInfo.filePath);
if (_manifest.TryGetPackageBundleByBundleName(bundleName, out PackageBundle value) == false)
{
result.Add(fileInfo.filePath);
}
*/
} }
return result; return result;
} }
private void LoadManifestInfo()
{
var packageName = _fileSystem.PackageName;
var packageVersion = _manifest.PackageVersion;
if (WX.StorageHasKeySync(YooAssets.DefaultPackageVersion_Key))
{
_lastPackageVersion = WX.StorageGetStringSync(YooAssets.DefaultPackageVersion_Key, YooAssets.DefaultPcakageVersion);
Debug.Log($"==========Get Storage PackageVerion Succ==={_lastPackageVersion}");
if (!string.IsNullOrEmpty(_lastPackageVersion) && (_lastPackageVersion != packageVersion))
{
_cacheManifestHashPath = GetCachePathByFileName(YooAssetSettingsData.GetPackageHashFileName(packageName, _lastPackageVersion));
_cacheManifestPath = GetCachePathByFileName(YooAssetSettingsData.GetManifestBinaryFileName(packageName, _lastPackageVersion));
if(string.IsNullOrEmpty(_cacheManifestHashPath) || string.IsNullOrEmpty(_cacheManifestPath)) { return; }
_packageHash = _fileSystem.ReadFileText(_cacheManifestHashPath);
_fileData = _fileSystem.ReadFileData(_cacheManifestPath);
}
}
else
{
WX.StorageSetStringSync(YooAssets.DefaultPackageVersion_Key, packageVersion);
Debug.Log($"first Set Storage PackageVerion Succ==={packageVersion}");
}
}
private void CheckPackageVerion()
{
var packageName = _fileSystem.PackageName;
var packageVersion = _manifest.PackageVersion;
if (_lastPackageVersion != packageVersion)
{
WX.StorageSetStringSync(YooAssets.DefaultPackageVersion_Key, packageVersion);
//删除旧的资源清单文件和哈希文件
WX.RemoveFile(_cacheManifestHashPath, (bool isOk) => { Debug.Log("====Delect manifestHashPath Succ"); });
WX.RemoveFile(_cacheManifestPath, (bool isOk) => { Debug.Log("====Delect manifestPath Succ"); });
Debug.Log($"==========Set Storage PackageVerion Succ==={packageVersion}");
}
}
private string GetUnuseCachePathByBundleName(string fileName)
{
var filePath = $"StreamingAssets/WebGL/{fileName}";
return WX.GetCachePath(filePath);
}
} }
#endif #endif

View File

@ -1,9 +1,9 @@
#if UNITY_WEBGL && WEIXINMINIGAME #if UNITY_WEBGL && WEIXINMINIGAME
using System;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using YooAsset; using YooAsset;
using WeChatWASM; using WeChatWASM;
using System;
public static class WechatFileSystemCreater public static class WechatFileSystemCreater
{ {
@ -55,9 +55,9 @@ internal class WechatFileSystem : IFileSystem
/// <summary> /// <summary>
/// Key:资源包GUID Value:缓存路径 /// Key:资源包GUID Value:缓存路径
/// </summary> /// </summary>
private readonly Dictionary<string, string> _wxFilePaths = new Dictionary<string, string>(10000); private readonly Dictionary<string, string> _cacheFilePaths = new Dictionary<string, string>(10000);
private WXFileSystemManager _wxFileSystemMgr; private WXFileSystemManager _wxFileSystemMgr;
private string _wxFileCacheRoot = string.Empty; private string _fileCacheRoot = string.Empty;
/// <summary> /// <summary>
/// 包裹名称 /// 包裹名称
@ -71,7 +71,7 @@ internal class WechatFileSystem : IFileSystem
{ {
get get
{ {
return _wxFileCacheRoot; return _fileCacheRoot;
} }
} }
@ -86,12 +86,12 @@ internal class WechatFileSystem : IFileSystem
} }
} }
#region 自定义参数 #region 自定义参数
/// <summary> /// <summary>
/// 自定义参数:远程服务接口 /// 自定义参数:远程服务接口
/// </summary> /// </summary>
public IRemoteServices RemoteServices { private set; get; } = null; public IRemoteServices RemoteServices { private set; get; } = null;
#endregion #endregion
public WechatFileSystem() public WechatFileSystem()
@ -121,7 +121,6 @@ internal class WechatFileSystem : IFileSystem
OperationSystem.StartOperation(PackageName, operation); OperationSystem.StartOperation(PackageName, operation);
return operation; return operation;
} }
public virtual FSClearUnusedBundleFilesOperation ClearUnusedBundleFilesAsync(PackageManifest manifest) public virtual FSClearUnusedBundleFilesOperation ClearUnusedBundleFilesAsync(PackageManifest manifest)
{ {
var operation = new WXFSClearUnusedBundleFilesAsync(this, manifest); var operation = new WXFSClearUnusedBundleFilesAsync(this, manifest);
@ -172,8 +171,8 @@ internal class WechatFileSystem : IFileSystem
} }
_wxFileSystemMgr = WX.GetFileSystemManager(); _wxFileSystemMgr = WX.GetFileSystemManager();
_wxFileCacheRoot = PathUtility.Combine(WX.PluginCachePath, $"StreamingAssets/WebGL");// WX.PluginCachePath; //注意:如果有子目录,请修改此处! _fileCacheRoot = WX.env.USER_DATA_PATH; //注意:如果有子目录,请修改此处!
Debug.Log($"==_wxFileCacheRoot=={_wxFileCacheRoot}"); //_fileCacheRoot = PathUtility.Combine(WX.PluginCachePath, $"StreamingAssets/WebGL");
} }
public virtual void OnUpdate() public virtual void OnUpdate()
{ {
@ -185,16 +184,14 @@ internal class WechatFileSystem : IFileSystem
} }
public virtual bool Exists(PackageBundle bundle) public virtual bool Exists(PackageBundle bundle)
{ {
string filePath = GetWXFileLoadPath(bundle); string filePath = GetCacheFileLoadPath(bundle);
//Debug.Log($"CacheFile:{WX.GetCachePath($"StreamingAssets/WebGL/v1.0.0/{bundle.FileName}")}"); return CheckCacheFileExist(filePath);
return CheckWXFileIsExist(filePath);
} }
public virtual bool NeedDownload(PackageBundle bundle) public virtual bool NeedDownload(PackageBundle bundle)
{ {
if (Belong(bundle) == false) if (Belong(bundle) == false)
return false; return false;
//Debug.Log($"WX NeedDownload: bundleName:{bundle.BundleName} + Exists:{Exists(bundle)}");
return Exists(bundle) == false; return Exists(bundle) == false;
} }
public virtual bool NeedUnpack(PackageBundle bundle) public virtual bool NeedUnpack(PackageBundle bundle)
@ -208,111 +205,36 @@ internal class WechatFileSystem : IFileSystem
public virtual byte[] ReadFileData(PackageBundle bundle) public virtual byte[] ReadFileData(PackageBundle bundle)
{ {
throw new System.NotImplementedException(); string filePath = GetCacheFileLoadPath(bundle);
} if (CheckCacheFileExist(filePath))
public virtual string ReadFileText(PackageBundle bundle)
{
throw new System.NotImplementedException();
}
public byte[] ReadFileData(string filePath)
{
if (CheckWXFileIsExist(filePath))
return _wxFileSystemMgr.ReadFileSync(filePath); return _wxFileSystemMgr.ReadFileSync(filePath);
else else
return Array.Empty<byte>(); return Array.Empty<byte>();
//throw new System.NotImplementedException();
} }
public virtual string ReadFileText(PackageBundle bundle)
public string ReadFileText(string filePath)
{ {
if(CheckWXFileIsExist(filePath)) string filePath = GetCacheFileLoadPath(bundle);
if (CheckCacheFileExist(filePath))
return _wxFileSystemMgr.ReadFileSync(filePath, "utf8"); return _wxFileSystemMgr.ReadFileSync(filePath, "utf8");
else else
return string.Empty; return string.Empty;
//throw new System.NotImplementedException();
} }
/// <summary> #region 内部方法
/// 获取所有缓存文件的路径 public bool CheckCacheFileExist(string filePath)
/// </summary>
/// <returns></returns>
public Dictionary<string,string> GetWXAllCacheFilePath()
{
return _wxFilePaths;
}
/// <summary>
/// 判断微信缓存文件是否存在
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public bool CheckWXFileIsExist(string filePath)
{ {
string result = _wxFileSystemMgr.AccessSync(filePath); string result = _wxFileSystemMgr.AccessSync(filePath);
return result.Equals("access:ok"); return result.Equals("access:ok");
} }
#region 调用微信小游戏接口删除缓存文件目录下所有文件 public string GetCacheFileLoadPath(PackageBundle bundle)
public void ClearAllCacheFile()
{ {
#if !UNITY_EDITOR && UNITY_WEBGL && WEIXINMINIGAME if (_cacheFilePaths.TryGetValue(bundle.BundleGUID, out string filePath) == false)
ShowModalOption showModalOp = new ShowModalOption();
showModalOp.title = "提示";
showModalOp.content = "是否确定要清理缓存并重启";
showModalOp.confirmText = "确定";
showModalOp.cancelText = "取消";
showModalOp.complete = (GeneralCallbackResult callResult) => { Debug.Log($"complete==={callResult.errMsg}"); };
showModalOp.fail = (GeneralCallbackResult callResult) => { Debug.Log($"fail==={callResult.errMsg}"); };
showModalOp.success = (ShowModalSuccessCallbackResult callResult) =>
{
if(callResult.confirm)
RestartMiniGame();
};
WX.ShowModal(showModalOp);
#endif
}
/// <summary>
/// 微信小游戏清除缓存并且重启小游戏
/// 参考小游戏=>出发吧麦芬
/// </summary>
private void RestartMiniGame()
{
WX.CleanAllFileCache((bool isOk) =>
{ {
RestartMiniProgramOption restartMini = new RestartMiniProgramOption(); filePath = PathUtility.Combine(_fileCacheRoot, bundle.FileName);
restartMini.complete = RestartMiniComplete; _cacheFilePaths.Add(bundle.BundleGUID, filePath);
restartMini.fail = RestartMiniFailComplete;
restartMini.success = RestartMiniSuccComplete;
WX.RestartMiniProgram(restartMini);
});
}
private void RestartMiniComplete(GeneralCallbackResult result)
{
Debug.Log($"RestartMiniComplete:{result.errMsg}");
}
private void RestartMiniFailComplete(GeneralCallbackResult result)
{
Debug.Log($"RestartMiniFailComplete:{result.errMsg}");
}
private void RestartMiniSuccComplete(GeneralCallbackResult result)
{
Debug.Log($"RestartMiniSuccComplete:{result.errMsg}");
}
#endregion
#region 内部方法
private string GetWXFileLoadPath(PackageBundle bundle)
{
if (_wxFilePaths.TryGetValue(bundle.BundleGUID, out string filePath) == false)
{
filePath = PathUtility.Combine(_wxFileCacheRoot, bundle.FileName);
_wxFilePaths.Add(bundle.BundleGUID, filePath);
} }
return filePath; return filePath;
} }
#endregion #endregion
} }
#endif #endif