update extension sample

优化了小游戏文件系统的缓存机制。
pull/464/head
何冠峰 2025-02-07 15:30:53 +08:00
parent 37a663e0e1
commit a02b3fbf6e
14 changed files with 374 additions and 76 deletions

View File

@ -2,14 +2,17 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using YooAsset; using YooAsset;
using StarkSDKSpace; using TTSDK;
using System.Linq;
using WeChatWASM;
using System;
public static class ByteGameFileSystemCreater public static class ByteGameFileSystemCreater
{ {
public static FileSystemParameters CreateByteGameFileSystemParameters(IRemoteServices remoteServices) public static FileSystemParameters CreateByteGameFileSystemParameters(IRemoteServices remoteServices, string packageRoot)
{ {
string fileSystemClass = $"{nameof(ByteGameFileSystem)},YooAsset.RuntimeExtension"; string fileSystemClass = $"{nameof(ByteGameFileSystem)},YooAsset.RuntimeExtension";
var fileSystemParams = new FileSystemParameters(fileSystemClass, null); var fileSystemParams = new FileSystemParameters(fileSystemClass, packageRoot);
fileSystemParams.AddParameter("REMOTE_SERVICES", remoteServices); fileSystemParams.AddParameter("REMOTE_SERVICES", remoteServices);
return fileSystemParams; return fileSystemParams;
} }
@ -51,8 +54,10 @@ internal class ByteGameFileSystem : 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> _cacheFilePaths = new Dictionary<string, string>(10000);
private StarkFileSystemManager _fileSystemManager; private TTFileSystemManager _fileSystemMgr;
private string _ttCacheRoot = string.Empty;
/// <summary> /// <summary>
/// 包裹名称 /// 包裹名称
@ -66,7 +71,7 @@ internal class ByteGameFileSystem : IFileSystem
{ {
get get
{ {
return string.Empty; return _ttCacheRoot;
} }
} }
@ -77,7 +82,7 @@ internal class ByteGameFileSystem : IFileSystem
{ {
get get
{ {
return 0; return _recorders.Count;
} }
} }
@ -155,6 +160,12 @@ internal class ByteGameFileSystem : IFileSystem
public virtual void OnCreate(string packageName, string rootDirectory) public virtual void OnCreate(string packageName, string rootDirectory)
{ {
PackageName = packageName; PackageName = packageName;
_ttCacheRoot = rootDirectory;
if (string.IsNullOrEmpty(_ttCacheRoot))
{
throw new System.Exception("请配置抖音小游戏的缓存根目录!");
}
// 注意CDN服务未启用的情况下使用抖音WEB服务器 // 注意CDN服务未启用的情况下使用抖音WEB服务器
if (RemoteServices == null) if (RemoteServices == null)
@ -163,7 +174,7 @@ internal class ByteGameFileSystem : IFileSystem
RemoteServices = new WebRemoteServices(webRoot); RemoteServices = new WebRemoteServices(webRoot);
} }
_fileSystemManager = StarkSDK.API.GetStarkFileSystemManager(); _fileSystemMgr = TT.GetFileSystemManager();
} }
public virtual void OnUpdate() public virtual void OnUpdate()
{ {
@ -176,7 +187,7 @@ internal class ByteGameFileSystem : IFileSystem
public virtual bool Exists(PackageBundle bundle) public virtual bool Exists(PackageBundle bundle)
{ {
string filePath = GetCacheFileLoadPath(bundle); string filePath = GetCacheFileLoadPath(bundle);
return _fileSystemManager.AccessSync(filePath); return _recorders.Contains(filePath);
} }
public virtual bool NeedDownload(PackageBundle bundle) public virtual bool NeedDownload(PackageBundle bundle)
{ {
@ -196,27 +207,80 @@ internal class ByteGameFileSystem : IFileSystem
public virtual string GetBundleFilePath(PackageBundle bundle) public virtual string GetBundleFilePath(PackageBundle bundle)
{ {
throw new System.NotImplementedException(); return GetCacheFileLoadPath(bundle);
} }
public virtual byte[] ReadBundleFileData(PackageBundle bundle) public virtual byte[] ReadBundleFileData(PackageBundle bundle)
{ {
throw new System.NotImplementedException(); string filePath = GetCacheFileLoadPath(bundle);
if (CheckCacheFileExist(filePath))
return _fileSystemMgr.ReadFileSync(filePath);
else
return Array.Empty<byte>();
} }
public virtual string ReadBundleFileText(PackageBundle bundle) public virtual string ReadBundleFileText(PackageBundle bundle)
{ {
throw new System.NotImplementedException(); string filePath = GetCacheFileLoadPath(bundle);
if (CheckCacheFileExist(filePath))
return _fileSystemMgr.ReadFileSync(filePath, "utf8");
else
return string.Empty;
} }
#region 内部方法 #region 内部方法
public TTFileSystemManager GetFileSystemMgr()
{
return _fileSystemMgr;
}
public bool CheckCacheFileExist(string filePath)
{
return _fileSystemMgr.AccessSync(filePath);
}
private string GetCacheFileLoadPath(PackageBundle bundle) private string GetCacheFileLoadPath(PackageBundle bundle)
{ {
if (_cacheFilePaths.TryGetValue(bundle.BundleGUID, out string filePath) == false) if (_cacheFilePaths.TryGetValue(bundle.BundleGUID, out string filePath) == false)
{ {
filePath = _fileSystemManager.GetLocalCachedPathForUrl(bundle.FileName); filePath = _fileSystemMgr.GetLocalCachedPathForUrl(bundle.FileName);
_cacheFilePaths.Add(bundle.BundleGUID, filePath); _cacheFilePaths.Add(bundle.BundleGUID, filePath);
} }
return filePath; return filePath;
} }
#endregion #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 #endif

View File

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

View File

@ -2,7 +2,7 @@
using UnityEngine; using UnityEngine;
using UnityEngine.Networking; using UnityEngine.Networking;
using YooAsset; using YooAsset;
using StarkSDKSpace; using TTSDK;
internal class BGFSLoadBundleOperation : FSLoadBundleOperation internal class BGFSLoadBundleOperation : FSLoadBundleOperation
{ {

View File

@ -0,0 +1,57 @@
#if UNITY_WEBGL && BYTEMINIGAME
using YooAsset;
using TTSDK;
internal class RecordTiktokCacheFilesOperation : AsyncOperationBase
{
private enum ESteps
{
None,
RecordCacheFiles,
WaitResponse,
Done,
}
private readonly ByteGameFileSystem _fileSystem;
private ESteps _steps = ESteps.None;
public RecordTiktokCacheFilesOperation(ByteGameFileSystem 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 getSavedFileListParam = new GetSavedFileListParam();
getSavedFileListParam.success = (TTGetSavedFileListResponse response) =>
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
foreach (var fileInfo in response.fileList)
{
//TODO 需要确认存储文件为Bundle文件
_fileSystem.RecordBundleFile(fileInfo.filePath);
}
};
getSavedFileListParam.fail = (TTGetSavedFileListResponse response) =>
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = response.errMsg;
};
fileSystemMgr.GetSavedFileList(getSavedFileListParam);
}
}
}
#endif

View File

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

View File

@ -11,13 +11,13 @@ internal class WXFSClearAllBundleFilesOperation : FSClearCacheFilesOperation
{ {
None, None,
ClearAllCacheFiles, ClearAllCacheFiles,
WaitResult,
Done, Done,
} }
private readonly WechatFileSystem _fileSystem; private readonly WechatFileSystem _fileSystem;
private ESteps _steps = ESteps.None; private ESteps _steps = ESteps.None;
internal WXFSClearAllBundleFilesOperation(WechatFileSystem fileSystem) internal WXFSClearAllBundleFilesOperation(WechatFileSystem fileSystem)
{ {
_fileSystem = fileSystem; _fileSystem = fileSystem;
@ -33,16 +33,25 @@ internal class WXFSClearAllBundleFilesOperation : FSClearCacheFilesOperation
if (_steps == ESteps.ClearAllCacheFiles) if (_steps == ESteps.ClearAllCacheFiles)
{ {
_steps = ESteps.WaitResult;
WX.CleanAllFileCache((bool isOk) => WX.CleanAllFileCache((bool isOk) =>
{ {
if (isOk) if (isOk)
{
YooLogger.Log("微信缓存清理成功!"); YooLogger.Log("微信缓存清理成功!");
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
_fileSystem.ClearAllRecords();
}
else else
{
YooLogger.Log("微信缓存清理失败!"); YooLogger.Log("微信缓存清理失败!");
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = "微信缓存清理失败!";
}
}); });
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
} }
} }
} }

View File

@ -5,14 +5,11 @@ using UnityEngine;
using YooAsset; using YooAsset;
using WeChatWASM; using WeChatWASM;
internal class WXFSClearUnusedBundleFilesAsync : FSClearCacheFilesOperation internal class WXFSClearUnusedBundleFilesAsync : FSClearCacheFilesOperation
{ {
private enum ESteps private enum ESteps
{ {
None, None,
GetAllCacheFiles,
WaitResult,
GetUnusedCacheFiles, GetUnusedCacheFiles,
ClearUnusedCacheFiles, ClearUnusedCacheFiles,
Done, Done,
@ -22,7 +19,6 @@ internal class WXFSClearUnusedBundleFilesAsync : FSClearCacheFilesOperation
private readonly PackageManifest _manifest; private readonly PackageManifest _manifest;
private List<string> _unusedCacheFiles; private List<string> _unusedCacheFiles;
private int _unusedFileTotalCount = 0; private int _unusedFileTotalCount = 0;
private GetSavedFileListSuccessCallbackResult _result;
private ESteps _steps = ESteps.None; private ESteps _steps = ESteps.None;
internal WXFSClearUnusedBundleFilesAsync(WechatFileSystem fileSystem, PackageManifest manifest) internal WXFSClearUnusedBundleFilesAsync(WechatFileSystem fileSystem, PackageManifest manifest)
@ -32,38 +28,13 @@ internal class WXFSClearUnusedBundleFilesAsync : FSClearCacheFilesOperation
} }
internal override void InternalOnStart() internal override void InternalOnStart()
{ {
_steps = ESteps.GetAllCacheFiles; _steps = ESteps.GetUnusedCacheFiles;
} }
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)
{
_steps = ESteps.WaitResult;
var fileSystemMgr = WX.GetFileSystemManager();
var option = new GetSavedFileListOption();
fileSystemMgr.GetSavedFileList(option);
option.fail += (FileError error) =>
{
_steps = ESteps.Done;
Error = error.errMsg;
Status = EOperationStatus.Failed;
};
option.success += (GetSavedFileListSuccessCallbackResult result) =>
{
_result = result;
_steps = ESteps.GetUnusedCacheFiles;
};
}
if (_steps == ESteps.WaitResult)
{
return;
}
if (_steps == ESteps.GetUnusedCacheFiles) if (_steps == ESteps.GetUnusedCacheFiles)
{ {
_unusedCacheFiles = GetUnusedCacheFiles(); _unusedCacheFiles = GetUnusedCacheFiles();
@ -76,9 +47,10 @@ internal class WXFSClearUnusedBundleFilesAsync : FSClearCacheFilesOperation
{ {
for (int i = _unusedCacheFiles.Count - 1; i >= 0; i--) for (int i = _unusedCacheFiles.Count - 1; i >= 0; i--)
{ {
string cacheFilePath = _unusedCacheFiles[i]; string clearFilePath = _unusedCacheFiles[i];
WX.RemoveFile(cacheFilePath, null);
_unusedCacheFiles.RemoveAt(i); _unusedCacheFiles.RemoveAt(i);
_fileSystem.ClearRecord(clearFilePath);
WX.RemoveFile(clearFilePath, null);
if (OperationSystem.IsBusy) if (OperationSystem.IsBusy)
break; break;
@ -99,22 +71,23 @@ internal class WXFSClearUnusedBundleFilesAsync : FSClearCacheFilesOperation
private List<string> GetUnusedCacheFiles() private List<string> GetUnusedCacheFiles()
{ {
List<string> result = new List<string>(_result.fileList.Length); var allRecords = _fileSystem.GetAllRecords();
foreach (var fileInfo in _result.fileList) List<string> result = new List<string>(allRecords.Count);
foreach (var filePath in allRecords)
{ {
// 如果存储文件名是按照Bundle文件哈希值存储 // 如果存储文件名是按照Bundle文件哈希值存储
string bundleGUID = Path.GetFileNameWithoutExtension(fileInfo.filePath); string bundleGUID = Path.GetFileNameWithoutExtension(filePath);
if (_manifest.TryGetPackageBundleByBundleGUID(bundleGUID, out PackageBundle value) == false) if (_manifest.TryGetPackageBundleByBundleGUID(bundleGUID, out PackageBundle value) == false)
{ {
result.Add(fileInfo.filePath); result.Add(filePath);
} }
// 如果存储文件名是按照Bundle文件名称存储 // 如果存储文件名是按照Bundle文件名称存储
/* /*
string bundleName = Path.GetFileNameWithoutExtension(fileInfo.filePath); string bundleName = Path.GetFileNameWithoutExtension(filePath);
if (_manifest.TryGetPackageBundleByBundleName(bundleName, out PackageBundle value) == false) if (_manifest.TryGetPackageBundleByBundleName(bundleName, out PackageBundle value) == false)
{ {
result.Add(fileInfo.filePath); result.Add(filePath);
} }
*/ */
} }

View File

@ -51,6 +51,7 @@ internal class WXFSDownloadFileOperation : DefaultDownloadFileOperation
{ {
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EOperationStatus.Succeed; Status = EOperationStatus.Succeed;
_fileSystem.TryRecordBundle(Bundle); //记录下载文件
} }
else else
{ {

View File

@ -3,7 +3,16 @@ using YooAsset;
internal partial class WXFSInitializeOperation : FSInitializeFileSystemOperation internal partial class WXFSInitializeOperation : FSInitializeFileSystemOperation
{ {
private enum ESteps
{
None,
RecordCacheFiles,
Done,
}
private readonly WechatFileSystem _fileSystem; private readonly WechatFileSystem _fileSystem;
private RecordWechatCacheFilesOperation _recordWechatCacheFilesOp;
private ESteps _steps = ESteps.None;
public WXFSInitializeOperation(WechatFileSystem fileSystem) public WXFSInitializeOperation(WechatFileSystem fileSystem)
{ {
@ -11,10 +20,36 @@ internal partial class WXFSInitializeOperation : FSInitializeFileSystemOperation
} }
internal override void InternalOnStart() internal override void InternalOnStart()
{ {
Status = EOperationStatus.Succeed; _steps = ESteps.RecordCacheFiles;
} }
internal override void InternalOnUpdate() 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 #endif

View File

@ -61,6 +61,7 @@ internal class WXFSLoadBundleOperation : FSLoadBundleOperation
_steps = ESteps.Done; _steps = ESteps.Done;
Result = new WXAssetBundleResult(_fileSystem, _bundle, assetBundle); Result = new WXAssetBundleResult(_fileSystem, _bundle, assetBundle);
Status = EOperationStatus.Succeed; Status = EOperationStatus.Succeed;
_fileSystem.TryRecordBundle(_bundle); //记录下载文件
} }
} }
else else

View File

@ -0,0 +1,58 @@
#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 getSavedFileListOption = new GetSavedFileListOption();
getSavedFileListOption.success = (GetSavedFileListSuccessCallbackResult response) =>
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
foreach (var fileInfo in response.fileList)
{
//TODO 需要确认存储文件为Bundle文件
_fileSystem.RecordBundleFile(fileInfo.filePath);
}
};
getSavedFileListOption.fail = (FileError fileError) =>
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = fileError.errMsg;
};
fileSystemMgr.GetSavedFileList(getSavedFileListOption);
}
}
}
#endif

View File

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

View File

@ -1,16 +1,17 @@
#if UNITY_WEBGL && WEIXINMINIGAME #if UNITY_WEBGL && WEIXINMINIGAME
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using UnityEngine; using UnityEngine;
using YooAsset; using YooAsset;
using WeChatWASM; using WeChatWASM;
public static class WechatFileSystemCreater public static class WechatFileSystemCreater
{ {
public static FileSystemParameters CreateWechatFileSystemParameters(IRemoteServices remoteServices) public static FileSystemParameters CreateWechatFileSystemParameters(IRemoteServices remoteServices, string packageRoot)
{ {
string fileSystemClass = $"{nameof(WechatFileSystem)},YooAsset.RuntimeExtension"; string fileSystemClass = $"{nameof(WechatFileSystem)},YooAsset.RuntimeExtension";
var fileSystemParams = new FileSystemParameters(fileSystemClass, null); var fileSystemParams = new FileSystemParameters(fileSystemClass, packageRoot);
fileSystemParams.AddParameter("REMOTE_SERVICES", remoteServices); fileSystemParams.AddParameter("REMOTE_SERVICES", remoteServices);
return fileSystemParams; return fileSystemParams;
} }
@ -52,12 +53,10 @@ internal class WechatFileSystem : IFileSystem
} }
} }
/// <summary> private readonly HashSet<string> _recorders = new HashSet<string>();
/// Key:资源包GUID Value:缓存路径
/// </summary>
private readonly Dictionary<string, string> _cacheFilePaths = new Dictionary<string, string>(10000); private readonly Dictionary<string, string> _cacheFilePaths = new Dictionary<string, string>(10000);
private WXFileSystemManager _wxFileSystemMgr; private WXFileSystemManager _fileSystemMgr;
private string _fileCacheRoot = string.Empty; private string _wxCacheRoot = string.Empty;
/// <summary> /// <summary>
/// 包裹名称 /// 包裹名称
@ -71,7 +70,7 @@ internal class WechatFileSystem : IFileSystem
{ {
get get
{ {
return _fileCacheRoot; return _wxCacheRoot;
} }
} }
@ -82,7 +81,7 @@ internal class WechatFileSystem : IFileSystem
{ {
get get
{ {
return 0; return _recorders.Count;
} }
} }
@ -176,6 +175,12 @@ internal class WechatFileSystem : IFileSystem
public virtual void OnCreate(string packageName, string rootDirectory) public virtual void OnCreate(string packageName, string rootDirectory)
{ {
PackageName = packageName; PackageName = packageName;
_wxCacheRoot = rootDirectory;
if (string.IsNullOrEmpty(_wxCacheRoot))
{
throw new System.Exception("请配置微信小游戏缓存根目录!");
}
// 注意CDN服务未启用的情况下使用微信WEB服务器 // 注意CDN服务未启用的情况下使用微信WEB服务器
if (RemoteServices == null) if (RemoteServices == null)
@ -184,10 +189,7 @@ internal class WechatFileSystem : IFileSystem
RemoteServices = new WebRemoteServices(webRoot); RemoteServices = new WebRemoteServices(webRoot);
} }
_wxFileSystemMgr = WX.GetFileSystemManager(); _fileSystemMgr = WX.GetFileSystemManager();
_fileCacheRoot = $"{WX.env.USER_DATA_PATH}/__GAME_FILE_CACHE";
//_fileCacheRoot = $"{WX.env.USER_DATA_PATH}/__GAME_FILE_CACHE/子目录"; //注意:如果有子目录,请修改此处!
//_fileCacheRoot = PathUtility.Combine(WX.PluginCachePath, $"StreamingAssets/WebGL");
} }
public virtual void OnUpdate() public virtual void OnUpdate()
{ {
@ -200,7 +202,7 @@ internal class WechatFileSystem : IFileSystem
public virtual bool Exists(PackageBundle bundle) public virtual bool Exists(PackageBundle bundle)
{ {
string filePath = GetCacheFileLoadPath(bundle); string filePath = GetCacheFileLoadPath(bundle);
return CheckCacheFileExist(filePath); return _recorders.Contains(filePath);
} }
public virtual bool NeedDownload(PackageBundle bundle) public virtual bool NeedDownload(PackageBundle bundle)
{ {
@ -226,7 +228,7 @@ internal class WechatFileSystem : IFileSystem
{ {
string filePath = GetCacheFileLoadPath(bundle); string filePath = GetCacheFileLoadPath(bundle);
if (CheckCacheFileExist(filePath)) if (CheckCacheFileExist(filePath))
return _wxFileSystemMgr.ReadFileSync(filePath); return _fileSystemMgr.ReadFileSync(filePath);
else else
return Array.Empty<byte>(); return Array.Empty<byte>();
} }
@ -234,26 +236,67 @@ internal class WechatFileSystem : IFileSystem
{ {
string filePath = GetCacheFileLoadPath(bundle); string filePath = GetCacheFileLoadPath(bundle);
if (CheckCacheFileExist(filePath)) if (CheckCacheFileExist(filePath))
return _wxFileSystemMgr.ReadFileSync(filePath, "utf8"); return _fileSystemMgr.ReadFileSync(filePath, "utf8");
else else
return string.Empty; return string.Empty;
} }
#region 内部方法 #region 内部方法
public WXFileSystemManager GetFileSystemMgr()
{
return _fileSystemMgr;
}
public bool CheckCacheFileExist(string filePath) public bool CheckCacheFileExist(string filePath)
{ {
string result = _wxFileSystemMgr.AccessSync(filePath); string result = _fileSystemMgr.AccessSync(filePath);
return result.Equals("access:ok"); return result.Equals("access:ok");
} }
public string GetCacheFileLoadPath(PackageBundle bundle) public string GetCacheFileLoadPath(PackageBundle bundle)
{ {
if (_cacheFilePaths.TryGetValue(bundle.BundleGUID, out string filePath) == false) if (_cacheFilePaths.TryGetValue(bundle.BundleGUID, out string filePath) == false)
{ {
filePath = PathUtility.Combine(_fileCacheRoot, bundle.FileName); filePath = PathUtility.Combine(_wxCacheRoot, bundle.FileName);
_cacheFilePaths.Add(bundle.BundleGUID, filePath); _cacheFilePaths.Add(bundle.BundleGUID, filePath);
} }
return filePath; return filePath;
} }
#endregion #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 #endif

View File

@ -4,7 +4,7 @@
"references": [ "references": [
"GUID:e34a5702dd353724aa315fb8011f08c3", "GUID:e34a5702dd353724aa315fb8011f08c3",
"GUID:5efd170ecd8084500bed5692932fe14e", "GUID:5efd170ecd8084500bed5692932fe14e",
"GUID:0f8df04a5a494444eb8fa0504f6fb965" "GUID:bb21d6197862c4c3e863390dec9859a7"
], ],
"includePlatforms": [], "includePlatforms": [],
"excludePlatforms": [], "excludePlatforms": [],