2025-02-28 16:11:01 +08:00
|
|
|
|
#if UNITY_WEBGL && DOUYINMINIGAME
|
2025-09-10 16:04:39 +08:00
|
|
|
|
using System;
|
2025-01-09 11:31:04 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using YooAsset;
|
2025-02-28 16:11:01 +08:00
|
|
|
|
using TTSDK;
|
2025-01-09 11:31:04 +08:00
|
|
|
|
|
2025-02-28 16:11:01 +08:00
|
|
|
|
public static class TiktokFileSystemCreater
|
2025-01-09 11:31:04 +08:00
|
|
|
|
{
|
2025-02-28 16:11:01 +08:00
|
|
|
|
public static FileSystemParameters CreateFileSystemParameters(string packageRoot, IRemoteServices remoteServices)
|
2025-01-09 11:31:04 +08:00
|
|
|
|
{
|
2025-09-02 19:21:49 +08:00
|
|
|
|
string fileSystemClass = $"{nameof(TiktokFileSystem)},YooAsset.MiniGame";
|
2025-02-28 16:11:01 +08:00
|
|
|
|
var fileSystemParams = new FileSystemParameters(fileSystemClass, packageRoot);
|
|
|
|
|
fileSystemParams.AddParameter(FileSystemParametersDefine.REMOTE_SERVICES, remoteServices);
|
|
|
|
|
return fileSystemParams;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static FileSystemParameters CreateFileSystemParameters(string packageRoot, IRemoteServices remoteServices, IWebDecryptionServices decryptionServices)
|
|
|
|
|
{
|
2025-09-02 19:21:49 +08:00
|
|
|
|
string fileSystemClass = $"{nameof(TiktokFileSystem)},YooAsset.MiniGame";
|
2025-02-28 16:11:01 +08:00
|
|
|
|
var fileSystemParams = new FileSystemParameters(fileSystemClass, packageRoot);
|
|
|
|
|
fileSystemParams.AddParameter(FileSystemParametersDefine.REMOTE_SERVICES, remoteServices);
|
|
|
|
|
fileSystemParams.AddParameter(FileSystemParametersDefine.DECRYPTION_SERVICES, decryptionServices);
|
2025-01-09 11:31:04 +08:00
|
|
|
|
return fileSystemParams;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 抖音小游戏文件系统
|
|
|
|
|
/// 参考:https://developer.open-douyin.com/docs/resource/zh-CN/mini-game/develop/guide/know
|
|
|
|
|
/// </summary>
|
2025-02-28 16:11:01 +08:00
|
|
|
|
internal class TiktokFileSystem : IFileSystem
|
2025-01-09 11:31:04 +08:00
|
|
|
|
{
|
|
|
|
|
private class WebRemoteServices : IRemoteServices
|
|
|
|
|
{
|
|
|
|
|
private readonly string _webPackageRoot;
|
|
|
|
|
protected readonly Dictionary<string, string> _mapping = new Dictionary<string, string>(10000);
|
|
|
|
|
|
|
|
|
|
public WebRemoteServices(string buildinPackRoot)
|
|
|
|
|
{
|
|
|
|
|
_webPackageRoot = buildinPackRoot;
|
|
|
|
|
}
|
|
|
|
|
string IRemoteServices.GetRemoteMainURL(string fileName)
|
|
|
|
|
{
|
|
|
|
|
return GetFileLoadURL(fileName);
|
|
|
|
|
}
|
|
|
|
|
string IRemoteServices.GetRemoteFallbackURL(string fileName)
|
|
|
|
|
{
|
|
|
|
|
return GetFileLoadURL(fileName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetFileLoadURL(string fileName)
|
|
|
|
|
{
|
|
|
|
|
if (_mapping.TryGetValue(fileName, out string url) == false)
|
|
|
|
|
{
|
|
|
|
|
string filePath = PathUtility.Combine(_webPackageRoot, fileName);
|
|
|
|
|
url = DownloadSystemHelper.ConvertToWWWPath(filePath);
|
|
|
|
|
_mapping.Add(fileName, url);
|
|
|
|
|
}
|
|
|
|
|
return url;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-28 16:11:01 +08:00
|
|
|
|
private readonly Dictionary<string, string> _cacheFilePathMapping = new Dictionary<string, string>(10000);
|
|
|
|
|
private TTFileSystemManager _fileSystemMgr;
|
|
|
|
|
private string _ttCacheRoot = string.Empty;
|
2025-01-09 11:31:04 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 包裹名称
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string PackageName { private set; get; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 文件根目录
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string FileRoot
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2025-02-28 16:11:01 +08:00
|
|
|
|
return _ttCacheRoot;
|
2025-01-09 11:31:04 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 文件数量
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int FileCount
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region 自定义参数
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 自定义参数:远程服务接口
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IRemoteServices RemoteServices { private set; get; } = null;
|
2025-02-28 16:11:01 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 自定义参数:解密方法类
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IWebDecryptionServices DecryptionServices { private set; get; }
|
2025-09-02 19:21:49 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 自定义参数:资源清单服务类
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IManifestRestoreServices ManifestServices { private set; get; }
|
2025-01-09 11:31:04 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
2025-02-28 16:11:01 +08:00
|
|
|
|
public TiktokFileSystem()
|
2025-01-09 11:31:04 +08:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
public virtual FSInitializeFileSystemOperation InitializeFileSystemAsync()
|
|
|
|
|
{
|
2025-02-28 16:11:01 +08:00
|
|
|
|
var operation = new TTFSInitializeOperation(this);
|
2025-01-09 11:31:04 +08:00
|
|
|
|
return operation;
|
|
|
|
|
}
|
|
|
|
|
public virtual FSLoadPackageManifestOperation LoadPackageManifestAsync(string packageVersion, int timeout)
|
|
|
|
|
{
|
2025-02-28 16:11:01 +08:00
|
|
|
|
var operation = new TTFSLoadPackageManifestOperation(this, packageVersion, timeout);
|
2025-01-09 11:31:04 +08:00
|
|
|
|
return operation;
|
|
|
|
|
}
|
|
|
|
|
public virtual FSRequestPackageVersionOperation RequestPackageVersionAsync(bool appendTimeTicks, int timeout)
|
|
|
|
|
{
|
2025-09-02 19:21:49 +08:00
|
|
|
|
var operation = new TTFSRequestPackageVersionOperation(this, appendTimeTicks, timeout);
|
2025-01-09 11:31:04 +08:00
|
|
|
|
return operation;
|
|
|
|
|
}
|
2025-05-13 10:40:30 +08:00
|
|
|
|
public virtual FSClearCacheFilesOperation ClearCacheFilesAsync(PackageManifest manifest, ClearCacheFilesOptions options)
|
2025-01-09 11:31:04 +08:00
|
|
|
|
{
|
2025-02-05 13:06:28 +08:00
|
|
|
|
var operation = new FSClearCacheFilesCompleteOperation();
|
2025-01-09 11:31:04 +08:00
|
|
|
|
return operation;
|
|
|
|
|
}
|
2025-05-13 10:40:30 +08:00
|
|
|
|
public virtual FSDownloadFileOperation DownloadFileAsync(PackageBundle bundle, DownloadFileOptions options)
|
2025-01-09 11:31:04 +08:00
|
|
|
|
{
|
2025-09-02 19:21:49 +08:00
|
|
|
|
string mainURL = RemoteServices.GetRemoteMainURL(bundle.FileName);
|
|
|
|
|
string fallbackURL = RemoteServices.GetRemoteFallbackURL(bundle.FileName);
|
|
|
|
|
options.SetURL(mainURL, fallbackURL);
|
2025-05-13 10:40:30 +08:00
|
|
|
|
var operation = new TTFSDownloadFileOperation(this, bundle, options);
|
2025-01-09 11:31:04 +08:00
|
|
|
|
return operation;
|
|
|
|
|
}
|
|
|
|
|
public virtual FSLoadBundleOperation LoadBundleFile(PackageBundle bundle)
|
|
|
|
|
{
|
|
|
|
|
if (bundle.BundleType == (int)EBuildBundleType.AssetBundle)
|
|
|
|
|
{
|
2025-02-28 16:11:01 +08:00
|
|
|
|
var operation = new TTFSLoadBundleOperation(this, bundle);
|
2025-01-09 11:31:04 +08:00
|
|
|
|
return operation;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-02-28 16:11:01 +08:00
|
|
|
|
string error = $"{nameof(TiktokFileSystem)} not support load bundle type : {bundle.BundleType}";
|
2025-01-09 11:31:04 +08:00
|
|
|
|
var operation = new FSLoadBundleCompleteOperation(error);
|
|
|
|
|
return operation;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void SetParameter(string name, object value)
|
|
|
|
|
{
|
2025-02-28 16:11:01 +08:00
|
|
|
|
if (name == FileSystemParametersDefine.REMOTE_SERVICES)
|
2025-01-09 11:31:04 +08:00
|
|
|
|
{
|
|
|
|
|
RemoteServices = (IRemoteServices)value;
|
|
|
|
|
}
|
2025-02-28 16:11:01 +08:00
|
|
|
|
else if (name == FileSystemParametersDefine.DECRYPTION_SERVICES)
|
|
|
|
|
{
|
|
|
|
|
DecryptionServices = (IWebDecryptionServices)value;
|
|
|
|
|
}
|
2025-09-02 19:21:49 +08:00
|
|
|
|
else if (name == FileSystemParametersDefine.MANIFEST_SERVICES)
|
|
|
|
|
{
|
|
|
|
|
ManifestServices = (IManifestRestoreServices)value;
|
|
|
|
|
}
|
2025-01-09 11:31:04 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
YooLogger.Warning($"Invalid parameter : {name}");
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-09-02 19:21:49 +08:00
|
|
|
|
public virtual void OnCreate(string packageName, string packageRoot)
|
2025-01-09 11:31:04 +08:00
|
|
|
|
{
|
|
|
|
|
PackageName = packageName;
|
2025-09-02 19:21:49 +08:00
|
|
|
|
_ttCacheRoot = packageRoot;
|
2025-02-28 16:11:01 +08:00
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(_ttCacheRoot))
|
|
|
|
|
{
|
2025-09-10 16:04:39 +08:00
|
|
|
|
throw new System.Exception("请配置小游戏的缓存根目录!");
|
2025-02-28 16:11:01 +08:00
|
|
|
|
}
|
2025-01-09 11:31:04 +08:00
|
|
|
|
|
2025-09-10 16:04:39 +08:00
|
|
|
|
// 注意:CDN服务未启用的情况下,使用WEB服务器
|
2025-01-09 11:31:04 +08:00
|
|
|
|
if (RemoteServices == null)
|
|
|
|
|
{
|
|
|
|
|
string webRoot = PathUtility.Combine(Application.streamingAssetsPath, YooAssetSettingsData.Setting.DefaultYooFolderName, packageName);
|
|
|
|
|
RemoteServices = new WebRemoteServices(webRoot);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-28 16:11:01 +08:00
|
|
|
|
_fileSystemMgr = TT.GetFileSystemManager();
|
2025-01-09 11:31:04 +08:00
|
|
|
|
}
|
2025-02-28 16:11:01 +08:00
|
|
|
|
public virtual void OnDestroy()
|
2025-01-09 11:31:04 +08:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual bool Belong(PackageBundle bundle)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
public virtual bool Exists(PackageBundle bundle)
|
|
|
|
|
{
|
2025-02-28 16:11:01 +08:00
|
|
|
|
return CheckCacheFileExist(bundle);
|
2025-01-09 11:31:04 +08:00
|
|
|
|
}
|
|
|
|
|
public virtual bool NeedDownload(PackageBundle bundle)
|
|
|
|
|
{
|
|
|
|
|
if (Belong(bundle) == false)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return Exists(bundle) == false;
|
|
|
|
|
}
|
|
|
|
|
public virtual bool NeedUnpack(PackageBundle bundle)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
public virtual bool NeedImport(PackageBundle bundle)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual string GetBundleFilePath(PackageBundle bundle)
|
|
|
|
|
{
|
2025-02-28 16:11:01 +08:00
|
|
|
|
return GetCacheFileLoadPath(bundle);
|
2025-01-09 11:31:04 +08:00
|
|
|
|
}
|
|
|
|
|
public virtual byte[] ReadBundleFileData(PackageBundle bundle)
|
|
|
|
|
{
|
2025-02-28 16:11:01 +08:00
|
|
|
|
if (CheckCacheFileExist(bundle))
|
|
|
|
|
{
|
|
|
|
|
string filePath = GetCacheFileLoadPath(bundle);
|
|
|
|
|
return _fileSystemMgr.ReadFileSync(filePath);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return Array.Empty<byte>();
|
|
|
|
|
}
|
2025-01-09 11:31:04 +08:00
|
|
|
|
}
|
|
|
|
|
public virtual string ReadBundleFileText(PackageBundle bundle)
|
|
|
|
|
{
|
2025-02-28 16:11:01 +08:00
|
|
|
|
if (CheckCacheFileExist(bundle))
|
|
|
|
|
{
|
|
|
|
|
string filePath = GetCacheFileLoadPath(bundle);
|
|
|
|
|
return _fileSystemMgr.ReadFileSync(filePath, "utf8");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
2025-01-09 11:31:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region 内部方法
|
2025-02-28 16:11:01 +08:00
|
|
|
|
public TTFileSystemManager GetFileSystemMgr()
|
|
|
|
|
{
|
|
|
|
|
return _fileSystemMgr;
|
|
|
|
|
}
|
|
|
|
|
public bool CheckCacheFileExist(PackageBundle bundle)
|
|
|
|
|
{
|
|
|
|
|
string url = RemoteServices.GetRemoteMainURL(bundle.FileName);
|
|
|
|
|
return _fileSystemMgr.IsUrlCached(url);
|
|
|
|
|
}
|
2025-01-09 11:31:04 +08:00
|
|
|
|
private string GetCacheFileLoadPath(PackageBundle bundle)
|
|
|
|
|
{
|
2025-02-28 16:11:01 +08:00
|
|
|
|
if (_cacheFilePathMapping.TryGetValue(bundle.BundleGUID, out string filePath) == false)
|
2025-01-09 11:31:04 +08:00
|
|
|
|
{
|
2025-02-28 16:11:01 +08:00
|
|
|
|
filePath = _fileSystemMgr.GetLocalCachedPathForUrl(bundle.FileName);
|
|
|
|
|
_cacheFilePathMapping.Add(bundle.BundleGUID, filePath);
|
2025-01-09 11:31:04 +08:00
|
|
|
|
}
|
|
|
|
|
return filePath;
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
#endif
|