Compare commits

..

5 Commits

Author SHA1 Message Date
何冠峰 31a0017351
Merge pull request #455 from suxf/dev
fix: 拷贝内置资源路径不正确
2025-02-07 16:27:15 +08:00
何冠峰 6e8cb0366d update extension sample
抖音小游戏文件重命名
2025-02-07 16:21:25 +08:00
何冠峰 a02b3fbf6e update extension sample
优化了小游戏文件系统的缓存机制。
2025-02-07 15:30:53 +08:00
何冠峰 37a663e0e1 update file system 2025-02-07 15:26:49 +08:00
阿枫 9f844912cf
fix: 拷贝内置资源路径不正确 2025-01-21 14:10:35 +08:00
37 changed files with 559 additions and 263 deletions

View File

@ -373,7 +373,7 @@ namespace YooAsset
{
if (_wrappers.ContainsKey(bundleGUID))
{
YooLogger.Error($"{nameof(DefaultCacheFileSystem)} has record file element : {bundleGUID}");
YooLogger.Error($"{nameof(DefaultCacheFileSystem)} has element : {bundleGUID}");
return false;
}

View File

@ -1,20 +0,0 @@
#if UNITY_WEBGL && BYTEMINIGAME
using YooAsset;
internal partial class BGFSInitializeOperation : FSInitializeFileSystemOperation
{
private readonly ByteGameFileSystem _fileSystem;
public BGFSInitializeOperation(ByteGameFileSystem fileSystem)
{
_fileSystem = fileSystem;
}
internal override void InternalOnStart()
{
Status = EOperationStatus.Succeed;
}
internal override void InternalOnUpdate()
{
}
}
#endif

View File

@ -1,88 +0,0 @@
#if UNITY_WEBGL && BYTEMINIGAME
using YooAsset;
internal class BGFSLoadPackageManifestOperation : FSLoadPackageManifestOperation
{
private enum ESteps
{
None,
RequestRemotePackageHash,
LoadRemotePackageManifest,
Done,
}
private readonly ByteGameFileSystem _fileSystem;
private readonly string _packageVersion;
private readonly int _timeout;
private RequestByteGamePackageHashOperation _requestRemotePackageHashOp;
private LoadByteGamePackageManifestOperation _loadRemotePackageManifestOp;
private ESteps _steps = ESteps.None;
public BGFSLoadPackageManifestOperation(ByteGameFileSystem fileSystem, string packageVersion, int timeout)
{
_fileSystem = fileSystem;
_packageVersion = packageVersion;
_timeout = timeout;
}
internal override void InternalOnStart()
{
_steps = ESteps.RequestRemotePackageHash;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.RequestRemotePackageHash)
{
if (_requestRemotePackageHashOp == null)
{
_requestRemotePackageHashOp = new RequestByteGamePackageHashOperation(_fileSystem, _packageVersion, _timeout);
OperationSystem.StartOperation(_fileSystem.PackageName, _requestRemotePackageHashOp);
}
if (_requestRemotePackageHashOp.IsDone == false)
return;
if (_requestRemotePackageHashOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.LoadRemotePackageManifest;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _requestRemotePackageHashOp.Error;
}
}
if (_steps == ESteps.LoadRemotePackageManifest)
{
if (_loadRemotePackageManifestOp == null)
{
string packageHash = _requestRemotePackageHashOp.PackageHash;
_loadRemotePackageManifestOp = new LoadByteGamePackageManifestOperation(_fileSystem, _packageVersion, packageHash, _timeout);
OperationSystem.StartOperation(_fileSystem.PackageName, _loadRemotePackageManifestOp);
}
Progress = _loadRemotePackageManifestOp.Progress;
if (_loadRemotePackageManifestOp.IsDone == false)
return;
if (_loadRemotePackageManifestOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.Done;
Manifest = _loadRemotePackageManifestOp.Manifest;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _loadRemotePackageManifestOp.Error;
}
}
}
}
#endif

View File

@ -1,15 +1,14 @@
#if UNITY_WEBGL && BYTEMINIGAME
#if UNITY_WEBGL && DOUYINMINIGAME
using UnityEngine;
using UnityEngine.Networking;
using YooAsset;
using StarkSDKSpace;
internal class BGFSDownloadFileOperation : DefaultDownloadFileOperation
internal class TTFSDownloadFileOperation : DefaultDownloadFileOperation
{
private ByteGameFileSystem _fileSystem;
private TiktokFileSystem _fileSystem;
private ESteps _steps = ESteps.None;
internal BGFSDownloadFileOperation(ByteGameFileSystem fileSystem, PackageBundle bundle, DownloadParam param) : base(bundle, param)
internal TTFSDownloadFileOperation(TiktokFileSystem fileSystem, PackageBundle bundle, DownloadParam param) : base(bundle, param)
{
_fileSystem = fileSystem;
}

View File

@ -0,0 +1,55 @@
#if UNITY_WEBGL && DOUYINMINIGAME
using YooAsset;
internal partial class TTFSInitializeOperation : FSInitializeFileSystemOperation
{
private enum ESteps
{
None,
RecordCacheFiles,
Done,
}
private readonly TiktokFileSystem _fileSystem;
private RecordTiktokCacheFilesOperation _recordTiktokCacheFilesOp;
private ESteps _steps = ESteps.None;
public TTFSInitializeOperation(TiktokFileSystem 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)
{
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

View File

@ -1,10 +1,10 @@
#if UNITY_WEBGL && BYTEMINIGAME
#if UNITY_WEBGL && DOUYINMINIGAME
using UnityEngine;
using UnityEngine.Networking;
using YooAsset;
using StarkSDKSpace;
using TTSDK;
internal class BGFSLoadBundleOperation : FSLoadBundleOperation
internal class TTFSLoadBundleOperation : FSLoadBundleOperation
{
private enum ESteps
{
@ -13,12 +13,12 @@ internal class BGFSLoadBundleOperation : FSLoadBundleOperation
Done,
}
private readonly ByteGameFileSystem _fileSystem;
private readonly TiktokFileSystem _fileSystem;
private readonly PackageBundle _bundle;
private UnityWebRequest _webRequest;
private ESteps _steps = ESteps.None;
internal BGFSLoadBundleOperation(ByteGameFileSystem fileSystem, PackageBundle bundle)
internal TTFSLoadBundleOperation(TiktokFileSystem fileSystem, PackageBundle bundle)
{
_fileSystem = fileSystem;
_bundle = bundle;

View File

@ -0,0 +1,88 @@
#if UNITY_WEBGL && DOUYINMINIGAME
using YooAsset;
internal class TTFSLoadPackageManifestOperation : FSLoadPackageManifestOperation
{
private enum ESteps
{
None,
RequestPackageHash,
LoadPackageManifest,
Done,
}
private readonly TiktokFileSystem _fileSystem;
private readonly string _packageVersion;
private readonly int _timeout;
private RequestTiktokPackageHashOperation _requestPackageHashOp;
private LoadTiktokPackageManifestOperation _loadPackageManifestOp;
private ESteps _steps = ESteps.None;
public TTFSLoadPackageManifestOperation(TiktokFileSystem fileSystem, string packageVersion, int timeout)
{
_fileSystem = fileSystem;
_packageVersion = packageVersion;
_timeout = timeout;
}
internal override void InternalOnStart()
{
_steps = ESteps.RequestPackageHash;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.RequestPackageHash)
{
if (_requestPackageHashOp == null)
{
_requestPackageHashOp = new RequestTiktokPackageHashOperation(_fileSystem, _packageVersion, _timeout);
OperationSystem.StartOperation(_fileSystem.PackageName, _requestPackageHashOp);
}
if (_requestPackageHashOp.IsDone == false)
return;
if (_requestPackageHashOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.LoadPackageManifest;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _requestPackageHashOp.Error;
}
}
if (_steps == ESteps.LoadPackageManifest)
{
if (_loadPackageManifestOp == null)
{
string packageHash = _requestPackageHashOp.PackageHash;
_loadPackageManifestOp = new LoadTiktokPackageManifestOperation(_fileSystem, _packageVersion, packageHash, _timeout);
OperationSystem.StartOperation(_fileSystem.PackageName, _loadPackageManifestOp);
}
Progress = _loadPackageManifestOp.Progress;
if (_loadPackageManifestOp.IsDone == false)
return;
if (_loadPackageManifestOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.Done;
Manifest = _loadPackageManifestOp.Manifest;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _loadPackageManifestOp.Error;
}
}
}
}
#endif

View File

@ -1,7 +1,7 @@
#if UNITY_WEBGL && BYTEMINIGAME
#if UNITY_WEBGL && DOUYINMINIGAME
using YooAsset;
internal class BGFSRequestPackageVersionOperation : FSRequestPackageVersionOperation
internal class TTFSRequestPackageVersionOperation : FSRequestPackageVersionOperation
{
private enum ESteps
{
@ -10,13 +10,13 @@ internal class BGFSRequestPackageVersionOperation : FSRequestPackageVersionOpera
Done,
}
private readonly ByteGameFileSystem _fileSystem;
private readonly TiktokFileSystem _fileSystem;
private readonly int _timeout;
private RequestByteGamePackageVersionOperation _requestWebPackageVersionOp;
private RequestTiktokPackageVersionOperation _requestPackageVersionOp;
private ESteps _steps = ESteps.None;
internal BGFSRequestPackageVersionOperation(ByteGameFileSystem fileSystem, int timeout)
internal TTFSRequestPackageVersionOperation(TiktokFileSystem fileSystem, int timeout)
{
_fileSystem = fileSystem;
_timeout = timeout;
@ -32,27 +32,27 @@ internal class BGFSRequestPackageVersionOperation : FSRequestPackageVersionOpera
if (_steps == ESteps.RequestPackageVersion)
{
if (_requestWebPackageVersionOp == null)
if (_requestPackageVersionOp == null)
{
_requestWebPackageVersionOp = new RequestByteGamePackageVersionOperation(_fileSystem, _timeout);
OperationSystem.StartOperation(_fileSystem.PackageName, _requestWebPackageVersionOp);
_requestPackageVersionOp = new RequestTiktokPackageVersionOperation(_fileSystem, _timeout);
OperationSystem.StartOperation(_fileSystem.PackageName, _requestPackageVersionOp);
}
Progress = _requestWebPackageVersionOp.Progress;
if (_requestWebPackageVersionOp.IsDone == false)
Progress = _requestPackageVersionOp.Progress;
if (_requestPackageVersionOp.IsDone == false)
return;
if (_requestWebPackageVersionOp.Status == EOperationStatus.Succeed)
if (_requestPackageVersionOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.Done;
PackageVersion = _requestWebPackageVersionOp.PackageVersion;
PackageVersion = _requestPackageVersionOp.PackageVersion;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _requestWebPackageVersionOp.Error;
Error = _requestPackageVersionOp.Error;
}
}
}

View File

@ -1,7 +1,7 @@
#if UNITY_WEBGL && BYTEMINIGAME
#if UNITY_WEBGL && DOUYINMINIGAME
using YooAsset;
internal class LoadByteGamePackageManifestOperation : AsyncOperationBase
internal class LoadTiktokPackageManifestOperation : AsyncOperationBase
{
private enum ESteps
{
@ -12,7 +12,7 @@ internal class LoadByteGamePackageManifestOperation : AsyncOperationBase
Done,
}
private readonly ByteGameFileSystem _fileSystem;
private readonly TiktokFileSystem _fileSystem;
private readonly string _packageVersion;
private readonly string _packageHash;
private readonly int _timeout;
@ -27,7 +27,7 @@ internal class LoadByteGamePackageManifestOperation : AsyncOperationBase
public PackageManifest Manifest { private set; get; }
internal LoadByteGamePackageManifestOperation(ByteGameFileSystem fileSystem, string packageVersion, string packageHash, int timeout)
internal LoadTiktokPackageManifestOperation(TiktokFileSystem fileSystem, string packageVersion, string packageHash, int timeout)
{
_fileSystem = fileSystem;
_packageVersion = packageVersion;
@ -36,7 +36,7 @@ internal class LoadByteGamePackageManifestOperation : AsyncOperationBase
}
internal override void InternalOnStart()
{
_requestCount = WebRequestCounter.GetRequestFailedCount(_fileSystem.PackageName, nameof(LoadByteGamePackageManifestOperation));
_requestCount = WebRequestCounter.GetRequestFailedCount(_fileSystem.PackageName, nameof(LoadTiktokPackageManifestOperation));
_steps = ESteps.RequestFileData;
}
internal override void InternalOnUpdate()
@ -67,7 +67,7 @@ internal class LoadByteGamePackageManifestOperation : AsyncOperationBase
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _webDataRequestOp.Error;
WebRequestCounter.RecordRequestFailed(_fileSystem.PackageName, nameof(LoadByteGamePackageManifestOperation));
WebRequestCounter.RecordRequestFailed(_fileSystem.PackageName, nameof(LoadTiktokPackageManifestOperation));
}
}

View File

@ -0,0 +1,57 @@
#if UNITY_WEBGL && DOUYINMINIGAME
using YooAsset;
using TTSDK;
internal class RecordTiktokCacheFilesOperation : AsyncOperationBase
{
private enum ESteps
{
None,
RecordCacheFiles,
WaitResponse,
Done,
}
private readonly TiktokFileSystem _fileSystem;
private ESteps _steps = ESteps.None;
public RecordTiktokCacheFilesOperation(TiktokFileSystem 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

@ -1,7 +1,7 @@
#if UNITY_WEBGL && BYTEMINIGAME
#if UNITY_WEBGL && DOUYINMINIGAME
using YooAsset;
internal class RequestByteGamePackageHashOperation : AsyncOperationBase
internal class RequestTiktokPackageHashOperation : AsyncOperationBase
{
private enum ESteps
{
@ -10,7 +10,7 @@ internal class RequestByteGamePackageHashOperation : AsyncOperationBase
Done,
}
private readonly ByteGameFileSystem _fileSystem;
private readonly TiktokFileSystem _fileSystem;
private readonly string _packageVersion;
private readonly int _timeout;
private UnityWebTextRequestOperation _webTextRequestOp;
@ -23,7 +23,7 @@ internal class RequestByteGamePackageHashOperation : AsyncOperationBase
public string PackageHash { private set; get; }
public RequestByteGamePackageHashOperation(ByteGameFileSystem fileSystem, string packageVersion, int timeout)
public RequestTiktokPackageHashOperation(TiktokFileSystem fileSystem, string packageVersion, int timeout)
{
_fileSystem = fileSystem;
_packageVersion = packageVersion;
@ -31,7 +31,7 @@ internal class RequestByteGamePackageHashOperation : AsyncOperationBase
}
internal override void InternalOnStart()
{
_requestCount = WebRequestCounter.GetRequestFailedCount(_fileSystem.PackageName, nameof(RequestByteGamePackageHashOperation));
_requestCount = WebRequestCounter.GetRequestFailedCount(_fileSystem.PackageName, nameof(RequestTiktokPackageHashOperation));
_steps = ESteps.RequestPackageHash;
}
internal override void InternalOnUpdate()
@ -73,7 +73,7 @@ internal class RequestByteGamePackageHashOperation : AsyncOperationBase
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _webTextRequestOp.Error;
WebRequestCounter.RecordRequestFailed(_fileSystem.PackageName, nameof(RequestByteGamePackageHashOperation));
WebRequestCounter.RecordRequestFailed(_fileSystem.PackageName, nameof(RequestTiktokPackageHashOperation));
}
}
}

View File

@ -1,7 +1,7 @@
#if UNITY_WEBGL && BYTEMINIGAME
#if UNITY_WEBGL && DOUYINMINIGAME
using YooAsset;
internal class RequestByteGamePackageVersionOperation : AsyncOperationBase
internal class RequestTiktokPackageVersionOperation : AsyncOperationBase
{
private enum ESteps
{
@ -10,7 +10,7 @@ internal class RequestByteGamePackageVersionOperation : AsyncOperationBase
Done,
}
private readonly ByteGameFileSystem _fileSystem;
private readonly TiktokFileSystem _fileSystem;
private readonly int _timeout;
private UnityWebTextRequestOperation _webTextRequestOp;
private int _requestCount = 0;
@ -22,14 +22,14 @@ internal class RequestByteGamePackageVersionOperation : AsyncOperationBase
public string PackageVersion { private set; get; }
public RequestByteGamePackageVersionOperation(ByteGameFileSystem fileSystem, int timeout)
public RequestTiktokPackageVersionOperation(TiktokFileSystem fileSystem, int timeout)
{
_fileSystem = fileSystem;
_timeout = timeout;
}
internal override void InternalOnStart()
{
_requestCount = WebRequestCounter.GetRequestFailedCount(_fileSystem.PackageName, nameof(RequestByteGamePackageVersionOperation));
_requestCount = WebRequestCounter.GetRequestFailedCount(_fileSystem.PackageName, nameof(RequestTiktokPackageVersionOperation));
_steps = ESteps.RequestPackageVersion;
}
internal override void InternalOnUpdate()
@ -71,7 +71,7 @@ internal class RequestByteGamePackageVersionOperation : AsyncOperationBase
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _webTextRequestOp.Error;
WebRequestCounter.RecordRequestFailed(_fileSystem.PackageName, nameof(RequestByteGamePackageVersionOperation));
WebRequestCounter.RecordRequestFailed(_fileSystem.PackageName, nameof(RequestTiktokPackageVersionOperation));
}
}
}

View File

@ -1,15 +1,17 @@
#if UNITY_WEBGL && BYTEMINIGAME
#if UNITY_WEBGL && DOUYINMINIGAME
using System.Collections.Generic;
using UnityEngine;
using YooAsset;
using StarkSDKSpace;
using TTSDK;
using System.Linq;
using System;
public static class ByteGameFileSystemCreater
public static class TiktokFileSystemCreater
{
public static FileSystemParameters CreateByteGameFileSystemParameters(IRemoteServices remoteServices)
public static FileSystemParameters CreateByteGameFileSystemParameters(IRemoteServices remoteServices, string packageRoot)
{
string fileSystemClass = $"{nameof(ByteGameFileSystem)},YooAsset.RuntimeExtension";
var fileSystemParams = new FileSystemParameters(fileSystemClass, null);
string fileSystemClass = $"{nameof(TiktokFileSystem)},YooAsset.RuntimeExtension";
var fileSystemParams = new FileSystemParameters(fileSystemClass, packageRoot);
fileSystemParams.AddParameter("REMOTE_SERVICES", remoteServices);
return fileSystemParams;
}
@ -19,7 +21,7 @@ public static class ByteGameFileSystemCreater
/// 抖音小游戏文件系统
/// 参考https://developer.open-douyin.com/docs/resource/zh-CN/mini-game/develop/guide/know
/// </summary>
internal class ByteGameFileSystem : IFileSystem
internal class TiktokFileSystem : IFileSystem
{
private class WebRemoteServices : IRemoteServices
{
@ -51,8 +53,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 StarkFileSystemManager _fileSystemManager;
private TTFileSystemManager _fileSystemMgr;
private string _ttCacheRoot = string.Empty;
/// <summary>
/// 包裹名称
@ -66,7 +70,7 @@ internal class ByteGameFileSystem : IFileSystem
{
get
{
return string.Empty;
return _ttCacheRoot;
}
}
@ -77,7 +81,7 @@ internal class ByteGameFileSystem : IFileSystem
{
get
{
return 0;
return _recorders.Count;
}
}
@ -89,24 +93,24 @@ internal class ByteGameFileSystem : IFileSystem
#endregion
public ByteGameFileSystem()
public TiktokFileSystem()
{
}
public virtual FSInitializeFileSystemOperation InitializeFileSystemAsync()
{
var operation = new BGFSInitializeOperation(this);
var operation = new TTFSInitializeOperation(this);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
public virtual FSLoadPackageManifestOperation LoadPackageManifestAsync(string packageVersion, int timeout)
{
var operation = new BGFSLoadPackageManifestOperation(this, packageVersion, timeout);
var operation = new TTFSLoadPackageManifestOperation(this, packageVersion, timeout);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
public virtual FSRequestPackageVersionOperation RequestPackageVersionAsync(bool appendTimeTicks, int timeout)
{
var operation = new BGFSRequestPackageVersionOperation(this, timeout);
var operation = new TTFSRequestPackageVersionOperation(this, timeout);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
@ -120,7 +124,7 @@ internal class ByteGameFileSystem : IFileSystem
{
param.MainURL = RemoteServices.GetRemoteMainURL(bundle.FileName);
param.FallbackURL = RemoteServices.GetRemoteFallbackURL(bundle.FileName);
var operation = new BGFSDownloadFileOperation(this, bundle, param);
var operation = new TTFSDownloadFileOperation(this, bundle, param);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
@ -128,13 +132,13 @@ internal class ByteGameFileSystem : IFileSystem
{
if (bundle.BundleType == (int)EBuildBundleType.AssetBundle)
{
var operation = new BGFSLoadBundleOperation(this, bundle);
var operation = new TTFSLoadBundleOperation(this, bundle);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
else
{
string error = $"{nameof(ByteGameFileSystem)} not support load bundle type : {bundle.BundleType}";
string error = $"{nameof(TiktokFileSystem)} not support load bundle type : {bundle.BundleType}";
var operation = new FSLoadBundleCompleteOperation(error);
OperationSystem.StartOperation(PackageName, operation);
return operation;
@ -155,6 +159,12 @@ internal class ByteGameFileSystem : IFileSystem
public virtual void OnCreate(string packageName, string rootDirectory)
{
PackageName = packageName;
_ttCacheRoot = rootDirectory;
if (string.IsNullOrEmpty(_ttCacheRoot))
{
throw new System.Exception("请配置抖音小游戏的缓存根目录!");
}
// 注意CDN服务未启用的情况下使用抖音WEB服务器
if (RemoteServices == null)
@ -163,7 +173,7 @@ internal class ByteGameFileSystem : IFileSystem
RemoteServices = new WebRemoteServices(webRoot);
}
_fileSystemManager = StarkSDK.API.GetStarkFileSystemManager();
_fileSystemMgr = TT.GetFileSystemManager();
}
public virtual void OnUpdate()
{
@ -176,7 +186,7 @@ internal class ByteGameFileSystem : IFileSystem
public virtual bool Exists(PackageBundle bundle)
{
string filePath = GetCacheFileLoadPath(bundle);
return _fileSystemManager.AccessSync(filePath);
return _recorders.Contains(filePath);
}
public virtual bool NeedDownload(PackageBundle bundle)
{
@ -196,27 +206,80 @@ internal class ByteGameFileSystem : IFileSystem
public virtual string GetBundleFilePath(PackageBundle bundle)
{
throw new System.NotImplementedException();
return GetCacheFileLoadPath(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)
{
throw new System.NotImplementedException();
string filePath = GetCacheFileLoadPath(bundle);
if (CheckCacheFileExist(filePath))
return _fileSystemMgr.ReadFileSync(filePath, "utf8");
else
return string.Empty;
}
#region 内部方法
public TTFileSystemManager GetFileSystemMgr()
{
return _fileSystemMgr;
}
public bool CheckCacheFileExist(string filePath)
{
return _fileSystemMgr.AccessSync(filePath);
}
private string GetCacheFileLoadPath(PackageBundle bundle)
{
if (_cacheFilePaths.TryGetValue(bundle.BundleGUID, out string filePath) == false)
{
filePath = _fileSystemManager.GetLocalCachedPathForUrl(bundle.FileName);
filePath = _fileSystemMgr.GetLocalCachedPathForUrl(bundle.FileName);
_cacheFilePaths.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(TiktokFileSystem)} 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

View File

@ -11,13 +11,13 @@ internal class WXFSClearAllBundleFilesOperation : FSClearCacheFilesOperation
{
None,
ClearAllCacheFiles,
WaitResult,
Done,
}
private readonly WechatFileSystem _fileSystem;
private ESteps _steps = ESteps.None;
internal WXFSClearAllBundleFilesOperation(WechatFileSystem fileSystem)
{
_fileSystem = fileSystem;
@ -33,16 +33,25 @@ internal class WXFSClearAllBundleFilesOperation : FSClearCacheFilesOperation
if (_steps == ESteps.ClearAllCacheFiles)
{
_steps = ESteps.WaitResult;
WX.CleanAllFileCache((bool isOk) =>
{
if (isOk)
{
YooLogger.Log("微信缓存清理成功!");
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
_fileSystem.ClearAllRecords();
}
else
{
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 WeChatWASM;
internal class WXFSClearUnusedBundleFilesAsync : FSClearCacheFilesOperation
{
private enum ESteps
{
None,
GetAllCacheFiles,
WaitResult,
GetUnusedCacheFiles,
ClearUnusedCacheFiles,
Done,
@ -22,7 +19,6 @@ internal class WXFSClearUnusedBundleFilesAsync : FSClearCacheFilesOperation
private readonly PackageManifest _manifest;
private List<string> _unusedCacheFiles;
private int _unusedFileTotalCount = 0;
private GetSavedFileListSuccessCallbackResult _result;
private ESteps _steps = ESteps.None;
internal WXFSClearUnusedBundleFilesAsync(WechatFileSystem fileSystem, PackageManifest manifest)
@ -32,38 +28,13 @@ internal class WXFSClearUnusedBundleFilesAsync : FSClearCacheFilesOperation
}
internal override void InternalOnStart()
{
_steps = ESteps.GetAllCacheFiles;
_steps = ESteps.GetUnusedCacheFiles;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
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)
{
_unusedCacheFiles = GetUnusedCacheFiles();
@ -76,9 +47,10 @@ internal class WXFSClearUnusedBundleFilesAsync : FSClearCacheFilesOperation
{
for (int i = _unusedCacheFiles.Count - 1; i >= 0; i--)
{
string cacheFilePath = _unusedCacheFiles[i];
WX.RemoveFile(cacheFilePath, null);
string clearFilePath = _unusedCacheFiles[i];
_unusedCacheFiles.RemoveAt(i);
_fileSystem.ClearRecord(clearFilePath);
WX.RemoveFile(clearFilePath, null);
if (OperationSystem.IsBusy)
break;
@ -99,22 +71,23 @@ internal class WXFSClearUnusedBundleFilesAsync : FSClearCacheFilesOperation
private List<string> GetUnusedCacheFiles()
{
List<string> result = new List<string>(_result.fileList.Length);
foreach (var fileInfo in _result.fileList)
var allRecords = _fileSystem.GetAllRecords();
List<string> result = new List<string>(allRecords.Count);
foreach (var filePath in allRecords)
{
// 如果存储文件名是按照Bundle文件哈希值存储
string bundleGUID = Path.GetFileNameWithoutExtension(fileInfo.filePath);
string bundleGUID = Path.GetFileNameWithoutExtension(filePath);
if (_manifest.TryGetPackageBundleByBundleGUID(bundleGUID, out PackageBundle value) == false)
{
result.Add(fileInfo.filePath);
result.Add(filePath);
}
// 如果存储文件名是按照Bundle文件名称存储
/*
string bundleName = Path.GetFileNameWithoutExtension(fileInfo.filePath);
string bundleName = Path.GetFileNameWithoutExtension(filePath);
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;
Status = EOperationStatus.Succeed;
_fileSystem.TryRecordBundle(Bundle); //记录下载文件
}
else
{

View File

@ -3,7 +3,16 @@ 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)
{
@ -11,10 +20,36 @@ internal partial class WXFSInitializeOperation : FSInitializeFileSystemOperation
}
internal override void InternalOnStart()
{
Status = EOperationStatus.Succeed;
_steps = ESteps.RecordCacheFiles;
}
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,6 +61,7 @@ internal class WXFSLoadBundleOperation : FSLoadBundleOperation
_steps = ESteps.Done;
Result = new WXAssetBundleResult(_fileSystem, _bundle, assetBundle);
Status = EOperationStatus.Succeed;
_fileSystem.TryRecordBundle(_bundle); //记录下载文件
}
}
else

View File

@ -6,16 +6,16 @@ internal class WXFSLoadPackageManifestOperation : FSLoadPackageManifestOperation
private enum ESteps
{
None,
RequestRemotePackageHash,
LoadRemotePackageManifest,
RequestPackageHash,
LoadPackageManifest,
Done,
}
private readonly WechatFileSystem _fileSystem;
private readonly string _packageVersion;
private readonly int _timeout;
private RequestWechatPackageHashOperation _requestRemotePackageHashOp;
private LoadWechatPackageManifestOperation _loadRemotePackageManifestOp;
private RequestWechatPackageHashOperation _requestPackageHashOp;
private LoadWechatPackageManifestOperation _loadPackageManifestOp;
private ESteps _steps = ESteps.None;
@ -27,60 +27,60 @@ internal class WXFSLoadPackageManifestOperation : FSLoadPackageManifestOperation
}
internal override void InternalOnStart()
{
_steps = ESteps.RequestRemotePackageHash;
_steps = ESteps.RequestPackageHash;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.RequestRemotePackageHash)
if (_steps == ESteps.RequestPackageHash)
{
if (_requestRemotePackageHashOp == null)
if (_requestPackageHashOp == null)
{
_requestRemotePackageHashOp = new RequestWechatPackageHashOperation(_fileSystem, _packageVersion, _timeout);
OperationSystem.StartOperation(_fileSystem.PackageName, _requestRemotePackageHashOp);
_requestPackageHashOp = new RequestWechatPackageHashOperation(_fileSystem, _packageVersion, _timeout);
OperationSystem.StartOperation(_fileSystem.PackageName, _requestPackageHashOp);
}
if (_requestRemotePackageHashOp.IsDone == false)
if (_requestPackageHashOp.IsDone == false)
return;
if (_requestRemotePackageHashOp.Status == EOperationStatus.Succeed)
if (_requestPackageHashOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.LoadRemotePackageManifest;
_steps = ESteps.LoadPackageManifest;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _requestRemotePackageHashOp.Error;
Error = _requestPackageHashOp.Error;
}
}
if (_steps == ESteps.LoadRemotePackageManifest)
if (_steps == ESteps.LoadPackageManifest)
{
if (_loadRemotePackageManifestOp == null)
if (_loadPackageManifestOp == null)
{
string packageHash = _requestRemotePackageHashOp.PackageHash;
_loadRemotePackageManifestOp = new LoadWechatPackageManifestOperation(_fileSystem, _packageVersion, packageHash, _timeout);
OperationSystem.StartOperation(_fileSystem.PackageName, _loadRemotePackageManifestOp);
string packageHash = _requestPackageHashOp.PackageHash;
_loadPackageManifestOp = new LoadWechatPackageManifestOperation(_fileSystem, _packageVersion, packageHash, _timeout);
OperationSystem.StartOperation(_fileSystem.PackageName, _loadPackageManifestOp);
}
Progress = _loadRemotePackageManifestOp.Progress;
if (_loadRemotePackageManifestOp.IsDone == false)
Progress = _loadPackageManifestOp.Progress;
if (_loadPackageManifestOp.IsDone == false)
return;
if (_loadRemotePackageManifestOp.Status == EOperationStatus.Succeed)
if (_loadPackageManifestOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.Done;
Manifest = _loadRemotePackageManifestOp.Manifest;
Manifest = _loadPackageManifestOp.Manifest;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _loadRemotePackageManifestOp.Error;
Error = _loadPackageManifestOp.Error;
}
}
}

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

View File

@ -129,13 +129,13 @@ public class CopyBuildinManifestOperation : GameAsyncOperation
{
string fileRoot = GetBuildinYooRoot();
string fileName = YooAssetSettingsData.GetPackageHashFileName(_packageName, _packageVersion);
return PathUtility.Combine(fileRoot, fileName);
return PathUtility.Combine(fileRoot, _packageName, fileName);
}
private string GetBuildinManifestFilePath()
{
string fileRoot = GetBuildinYooRoot();
string fileName = YooAssetSettingsData.GetManifestBinaryFileName(_packageName, _packageVersion);
return PathUtility.Combine(fileRoot, fileName);
return PathUtility.Combine(fileRoot, _packageName, fileName);
}
private string GetCacheYooRoot()
@ -146,12 +146,12 @@ public class CopyBuildinManifestOperation : GameAsyncOperation
{
string fileRoot = GetCacheYooRoot();
string fileName = YooAssetSettingsData.GetPackageHashFileName(_packageName, _packageVersion);
return PathUtility.Combine(fileRoot, fileName);
return PathUtility.Combine(fileRoot, _packageName, fileName);
}
private string GetCacheManifestFilePath()
{
string fileRoot = GetCacheYooRoot();
string fileName = YooAssetSettingsData.GetManifestBinaryFileName(_packageName, _packageVersion);
return PathUtility.Combine(fileRoot, fileName);
return PathUtility.Combine(fileRoot, _packageName, fileName);
}
}

View File

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