Files
YooAsset/Assets/YooAsset/Samples~/Extension Sample/Runtime/ExtensionFileSystem/WechatFileSystem/WechatFileSystem.cs

304 lines
10 KiB
C#
Raw Normal View History

2024-07-10 16:50:19 +08:00
#if UNITY_WEBGL && WEIXINMINIGAME
2024-09-30 18:00:02 +08:00
using System;
2024-07-07 00:52:50 +08:00
using System.Collections.Generic;
using System.Linq;
2024-07-07 00:52:50 +08:00
using UnityEngine;
using YooAsset;
using WeChatWASM;
public static class WechatFileSystemCreater
{
public static FileSystemParameters CreateWechatFileSystemParameters(string packageRoot, IRemoteServices remoteServices)
2024-07-07 00:52:50 +08:00
{
string fileSystemClass = $"{nameof(WechatFileSystem)},YooAsset.RuntimeExtension";
var fileSystemParams = new FileSystemParameters(fileSystemClass, packageRoot);
fileSystemParams.AddParameter(FileSystemParametersDefine.REMOTE_SERVICES, remoteServices);
return fileSystemParams;
}
public static FileSystemParameters CreateWechatFileSystemParameters(string packageRoot, IRemoteServices remoteServices, IWebDecryptionServices decryptionServices)
{
string fileSystemClass = $"{nameof(WechatFileSystem)},YooAsset.RuntimeExtension";
var fileSystemParams = new FileSystemParameters(fileSystemClass, packageRoot);
fileSystemParams.AddParameter(FileSystemParametersDefine.REMOTE_SERVICES, remoteServices);
fileSystemParams.AddParameter(FileSystemParametersDefine.DECRYPTION_SERVICES, decryptionServices);
2024-07-07 00:52:50 +08:00
return fileSystemParams;
}
}
/// <summary>
/// 微信小游戏文件系统
/// 参考https://wechat-miniprogram.github.io/minigame-unity-webgl-transform/Design/UsingAssetBundle.html
/// </summary>
internal class WechatFileSystem : IFileSystem
{
2024-08-13 10:54:29 +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;
}
}
private readonly Dictionary<string, string> _cacheFilePathMapping = new Dictionary<string, string>(10000);
private WXFileSystemManager _fileSystemMgr;
private string _wxCacheRoot = string.Empty;
2024-07-07 00:52:50 +08:00
/// <summary>
/// 包裹名称
/// </summary>
public string PackageName { private set; get; }
private readonly string _packageRoot = YooAssetSettingsData.Setting.DefaultYooFolderName;
2024-07-07 00:52:50 +08:00
/// <summary>
/// 文件根目录
/// </summary>
public string FileRoot
{
get
{
return _wxCacheRoot;
2024-07-07 00:52:50 +08:00
}
}
/// <summary>
/// 文件数量
/// </summary>
public int FileCount
{
get
{
return 0;
2024-07-07 00:52:50 +08:00
}
}
2024-09-30 18:00:02 +08:00
#region
2024-07-07 00:52:50 +08:00
/// <summary>
/// 自定义参数:远程服务接口
/// </summary>
public IRemoteServices RemoteServices { private set; get; } = null;
/// <summary>
/// 自定义参数:解密方法类
/// </summary>
public IWebDecryptionServices DecryptionServices { private set; get; }
2024-09-30 18:00:02 +08:00
#endregion
2024-07-07 00:52:50 +08:00
public WechatFileSystem()
{
}
public virtual FSInitializeFileSystemOperation InitializeFileSystemAsync()
{
var operation = new WXFSInitializeOperation(this);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
public virtual FSLoadPackageManifestOperation LoadPackageManifestAsync(string packageVersion, int timeout)
{
var operation = new WXFSLoadPackageManifestOperation(this, packageVersion, timeout);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
public virtual FSRequestPackageVersionOperation RequestPackageVersionAsync(bool appendTimeTicks, int timeout)
{
var operation = new WXFSRequestPackageVersionOperation(this, appendTimeTicks, timeout);
2024-07-07 00:52:50 +08:00
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
public virtual FSClearCacheFilesOperation ClearCacheFilesAsync(PackageManifest manifest, string clearMode, object clearParam)
2024-07-07 00:52:50 +08:00
{
2024-12-19 14:42:33 +08:00
if (clearMode == EFileClearMode.ClearAllBundleFiles.ToString())
{
var operation = new WXFSClearAllBundleFilesOperation(this);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
else if (clearMode == EFileClearMode.ClearUnusedBundleFiles.ToString())
{
var operation = new WXFSClearUnusedBundleFilesAsync(this, manifest);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
else
{
string error = $"Invalid clear mode : {clearMode}";
var operation = new FSClearCacheFilesCompleteOperation(error);
2024-12-19 14:42:33 +08:00
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
2024-07-07 00:52:50 +08:00
}
public virtual FSDownloadFileOperation DownloadFileAsync(PackageBundle bundle, DownloadParam param)
{
param.MainURL = RemoteServices.GetRemoteMainURL(bundle.FileName);
param.FallbackURL = RemoteServices.GetRemoteFallbackURL(bundle.FileName);
var operation = new WXFSDownloadFileOperation(this, bundle, param);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
public virtual FSLoadBundleOperation LoadBundleFile(PackageBundle bundle)
{
2024-12-25 17:24:48 +08:00
if (bundle.BundleType == (int)EBuildBundleType.AssetBundle)
{
var operation = new WXFSLoadBundleOperation(this, bundle);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
else
{
string error = $"{nameof(WechatFileSystem)} not support load bundle type : {bundle.BundleType}";
var operation = new FSLoadBundleCompleteOperation(error);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
2024-07-07 00:52:50 +08:00
}
public virtual void SetParameter(string name, object value)
{
if (name == FileSystemParametersDefine.REMOTE_SERVICES)
2024-07-07 00:52:50 +08:00
{
RemoteServices = (IRemoteServices)value;
}
else if (name == FileSystemParametersDefine.DECRYPTION_SERVICES)
{
DecryptionServices = (IWebDecryptionServices)value;
}
2024-07-07 00:52:50 +08:00
else
{
YooLogger.Warning($"Invalid parameter : {name}");
}
}
public virtual void OnCreate(string packageName, string packageRoot)
2024-07-07 00:52:50 +08:00
{
PackageName = packageName;
_wxCacheRoot = packageRoot;
if (string.IsNullOrEmpty(_wxCacheRoot))
{
throw new System.Exception("请配置微信小游戏缓存根目录!");
}
2024-07-07 00:52:50 +08:00
if (_wxCacheRoot.StartsWith(WX.PluginCachePath) == false)
{
_wxCacheRoot = PathUtility.Combine(WX.PluginCachePath, _wxCacheRoot);
}
2024-08-13 10:54:29 +08:00
// 注意CDN服务未启用的情况下使用微信WEB服务器
if (RemoteServices == null)
{
string webRoot = PathUtility.Combine(Application.streamingAssetsPath, YooAssetSettingsData.Setting.DefaultYooFolderName, packageName);
RemoteServices = new WebRemoteServices(webRoot);
}
_fileSystemMgr = WX.GetFileSystemManager();
2024-07-07 00:52:50 +08:00
}
public virtual void OnUpdate()
{
}
public virtual bool Belong(PackageBundle bundle)
{
return true;
}
public virtual bool Exists(PackageBundle bundle)
{
2024-09-30 18:00:02 +08:00
string filePath = GetCacheFileLoadPath(bundle);
return CheckCacheFileExist(filePath);
2024-07-07 00:52:50 +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;
}
2024-12-25 17:24:48 +08:00
public virtual string GetBundleFilePath(PackageBundle bundle)
{
return GetCacheFileLoadPath(bundle);
}
public virtual byte[] ReadBundleFileData(PackageBundle bundle)
2024-07-08 17:36:39 +08:00
{
2024-09-30 18:00:02 +08:00
string filePath = GetCacheFileLoadPath(bundle);
if (CheckCacheFileExist(filePath))
return _fileSystemMgr.ReadFileSync(filePath);
else
return Array.Empty<byte>();
}
2024-12-25 17:24:48 +08:00
public virtual string ReadBundleFileText(PackageBundle bundle)
{
2024-09-30 18:00:02 +08:00
string filePath = GetCacheFileLoadPath(bundle);
if (CheckCacheFileExist(filePath))
return _fileSystemMgr.ReadFileSync(filePath, "utf8");
else
return string.Empty;
}
2024-09-30 18:00:02 +08:00
#region
public WXFileSystemManager GetFileSystemMgr()
{
return _fileSystemMgr;
}
2024-09-30 18:00:02 +08:00
public bool CheckCacheFileExist(string filePath)
{
string result = WX.GetCachePath(filePath);
if (string.IsNullOrEmpty(result))
return false;
else
return true;
}
2024-09-30 18:00:02 +08:00
public string GetCacheFileLoadPath(PackageBundle bundle)
2024-07-07 00:52:50 +08:00
{
if (_cacheFilePathMapping.TryGetValue(bundle.BundleGUID, out string filePath) == false)
2024-07-07 00:52:50 +08:00
{
filePath = PathUtility.Combine(_wxCacheRoot, bundle.FileName);
_cacheFilePathMapping.Add(bundle.BundleGUID, filePath);
2024-07-07 00:52:50 +08:00
}
return filePath;
}
/// <summary>
/// 加载加密资源文件
/// </summary>
2025-02-12 16:28:31 +08:00
public AssetBundle LoadEncryptedAssetBundle(PackageBundle bundle, byte[] fileData)
{
2025-02-12 16:28:31 +08:00
WebDecryptFileInfo fileInfo = new WebDecryptFileInfo();
fileInfo.BundleName = bundle.BundleName;
fileInfo.FileLoadCRC = bundle.UnityCRC;
fileInfo.FileData = fileData;
var decryptResult = DecryptionServices.LoadAssetBundle(fileInfo);
return decryptResult.Result;
}
2024-09-30 18:00:02 +08:00
#endregion
2024-07-07 00:52:50 +08:00
}
#endif