Compare commits

..

14 Commits

Author SHA1 Message Date
何冠峰 151e978e5b update space shoot 2025-02-12 16:29:26 +08:00
何冠峰 9e806861ec update extension sample 2025-02-12 16:28:31 +08:00
何冠峰 a9a9368b9b update test sample 2025-02-12 16:27:51 +08:00
何冠峰 7936ba31ea update file system
WebGL网页平台支持资源加密。
2025-02-12 16:27:30 +08:00
何冠峰 afc456de9a Update UnloadAllAssetsOperation.cs 2025-02-12 14:44:02 +08:00
何冠峰 a98db0ba83 update extension sample 2025-02-12 14:43:51 +08:00
何冠峰 fb8720edd3 update extension sample
抖音小游戏支持文件加密。
2025-02-12 11:59:09 +08:00
何冠峰 2aa2a0ac3a update space shooter 2025-02-12 11:46:38 +08:00
何冠峰 0531b6ef3a update extension sample 2025-02-12 11:45:12 +08:00
何冠峰 24142de11f update extension sample
支持微信小游戏文件加密
2025-02-12 11:23:27 +08:00
何冠峰 7fad6eb70b update services
增加WebGL端解密接口
2025-02-12 11:22:09 +08:00
何冠峰 f60227abdf
Merge pull request #469 from suxf/dev
优化微信小游戏文件系统逻辑
2025-02-12 10:10:32 +08:00
枫似锦 18d6a74c53 Merge branch 'dev' of https://github.com/suxf/YooAsset into dev 2025-02-12 00:30:22 +08:00
枫似锦 03d06ba657 优化微信小游戏文件系统逻辑 2025-02-12 00:29:10 +08:00
21 changed files with 469 additions and 121 deletions

View File

@ -47,6 +47,11 @@ namespace YooAsset
/// 自定义参数:跨域下载服务接口 /// 自定义参数:跨域下载服务接口
/// </summary> /// </summary>
public IRemoteServices RemoteServices { private set; get; } = null; public IRemoteServices RemoteServices { private set; get; } = null;
/// <summary>
/// 自定义参数:解密方法类
/// </summary>
public IWebDecryptionServices DecryptionServices { private set; get; }
#endregion #endregion
@ -108,6 +113,10 @@ namespace YooAsset
{ {
RemoteServices = (IRemoteServices)value; RemoteServices = (IRemoteServices)value;
} }
else if (name == FileSystemParametersDefine.DECRYPTION_SERVICES)
{
DecryptionServices = (IWebDecryptionServices)value;
}
else else
{ {
YooLogger.Warning($"Invalid parameter : {name}"); YooLogger.Warning($"Invalid parameter : {name}");

View File

@ -1,6 +1,4 @@
 
using UnityEngine;
namespace YooAsset namespace YooAsset
{ {
internal class DWRFSLoadAssetBundleOperation : FSLoadBundleOperation internal class DWRFSLoadAssetBundleOperation : FSLoadBundleOperation
@ -8,13 +6,13 @@ namespace YooAsset
private enum ESteps private enum ESteps
{ {
None, None,
DownloadFile, DownloadAssetBundle,
Done, Done,
} }
private readonly DefaultWebRemoteFileSystem _fileSystem; private readonly DefaultWebRemoteFileSystem _fileSystem;
private readonly PackageBundle _bundle; private readonly PackageBundle _bundle;
private DownloadHandlerAssetBundleOperation _downloadhanlderAssetBundleOp; private DownloadAssetBundleOperation _downloadAssetBundleOp;
private ESteps _steps = ESteps.None; private ESteps _steps = ESteps.None;
@ -25,38 +23,47 @@ namespace YooAsset
} }
internal override void InternalOnStart() internal override void InternalOnStart()
{ {
_steps = ESteps.DownloadFile; _steps = ESteps.DownloadAssetBundle;
} }
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.DownloadFile) if (_steps == ESteps.DownloadAssetBundle)
{ {
if (_downloadhanlderAssetBundleOp == null) if (_downloadAssetBundleOp == null)
{ {
DownloadParam downloadParam = new DownloadParam(int.MaxValue, 60); DownloadParam downloadParam = new DownloadParam(int.MaxValue, 60);
downloadParam.MainURL = _fileSystem.RemoteServices.GetRemoteMainURL(_bundle.FileName); downloadParam.MainURL = _fileSystem.RemoteServices.GetRemoteMainURL(_bundle.FileName);
downloadParam.FallbackURL = _fileSystem.RemoteServices.GetRemoteFallbackURL(_bundle.FileName); downloadParam.FallbackURL = _fileSystem.RemoteServices.GetRemoteFallbackURL(_bundle.FileName);
_downloadhanlderAssetBundleOp = new DownloadHandlerAssetBundleOperation(_fileSystem.DisableUnityWebCache, _bundle, downloadParam);
OperationSystem.StartOperation(_fileSystem.PackageName, _downloadhanlderAssetBundleOp); if (_bundle.Encrypted)
{
_downloadAssetBundleOp = new DownloadWebEncryptAssetBundleOperation(_fileSystem.DecryptionServices, _bundle, downloadParam);
OperationSystem.StartOperation(_fileSystem.PackageName, _downloadAssetBundleOp);
}
else
{
_downloadAssetBundleOp = new DownloadWebNormalAssetBundleOperation(_fileSystem.DisableUnityWebCache, _bundle, downloadParam);
OperationSystem.StartOperation(_fileSystem.PackageName, _downloadAssetBundleOp);
}
} }
DownloadProgress = _downloadhanlderAssetBundleOp.DownloadProgress; DownloadProgress = _downloadAssetBundleOp.DownloadProgress;
DownloadedBytes = _downloadhanlderAssetBundleOp.DownloadedBytes; DownloadedBytes = _downloadAssetBundleOp.DownloadedBytes;
Progress = _downloadhanlderAssetBundleOp.Progress; Progress = _downloadAssetBundleOp.Progress;
if (_downloadhanlderAssetBundleOp.IsDone == false) if (_downloadAssetBundleOp.IsDone == false)
return; return;
if (_downloadhanlderAssetBundleOp.Status == EOperationStatus.Succeed) if (_downloadAssetBundleOp.Status == EOperationStatus.Succeed)
{ {
var assetBundle = _downloadhanlderAssetBundleOp.Result; var assetBundle = _downloadAssetBundleOp.Result;
if(assetBundle == null) if(assetBundle == null)
{ {
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EOperationStatus.Failed; Status = EOperationStatus.Failed;
Error = $"{nameof(DownloadHandlerAssetBundleOperation)} loaded asset bundle is null !"; Error = $"{nameof(DownloadAssetBundleOperation)} loaded asset bundle is null !";
} }
else else
{ {
@ -69,7 +76,7 @@ namespace YooAsset
{ {
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EOperationStatus.Failed; Status = EOperationStatus.Failed;
Error = _downloadhanlderAssetBundleOp.Error; Error = _downloadAssetBundleOp.Error;
} }
} }
} }
@ -85,10 +92,10 @@ namespace YooAsset
} }
public override void AbortDownloadOperation() public override void AbortDownloadOperation()
{ {
if (_steps == ESteps.DownloadFile) if (_steps == ESteps.DownloadAssetBundle)
{ {
if (_downloadhanlderAssetBundleOp != null) if (_downloadAssetBundleOp != null)
_downloadhanlderAssetBundleOp.SetAbort(); _downloadAssetBundleOp.SetAbort();
} }
} }
} }

View File

@ -56,6 +56,11 @@ namespace YooAsset
/// 禁用Unity的网络缓存 /// 禁用Unity的网络缓存
/// </summary> /// </summary>
public bool DisableUnityWebCache { private set; get; } = false; public bool DisableUnityWebCache { private set; get; } = false;
/// <summary>
/// 自定义参数:解密方法类
/// </summary>
public IWebDecryptionServices DecryptionServices { private set; get; }
#endregion #endregion
@ -113,6 +118,10 @@ namespace YooAsset
{ {
DisableUnityWebCache = (bool)value; DisableUnityWebCache = (bool)value;
} }
else if (name == FileSystemParametersDefine.DECRYPTION_SERVICES)
{
DecryptionServices = (IWebDecryptionServices)value;
}
else else
{ {
YooLogger.Warning($"Invalid parameter : {name}"); YooLogger.Warning($"Invalid parameter : {name}");

View File

@ -6,13 +6,13 @@ namespace YooAsset
private enum ESteps private enum ESteps
{ {
None, None,
DownloadFile, DownloadAssetBundle,
Done, Done,
} }
private readonly DefaultWebServerFileSystem _fileSystem; private readonly DefaultWebServerFileSystem _fileSystem;
private readonly PackageBundle _bundle; private readonly PackageBundle _bundle;
private DownloadHandlerAssetBundleOperation _downloadhanlderAssetBundleOp; private DownloadAssetBundleOperation _downloadAssetBundleOp;
private ESteps _steps = ESteps.None; private ESteps _steps = ESteps.None;
@ -23,39 +23,48 @@ namespace YooAsset
} }
internal override void InternalOnStart() internal override void InternalOnStart()
{ {
_steps = ESteps.DownloadFile; _steps = ESteps.DownloadAssetBundle;
} }
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.DownloadFile) if (_steps == ESteps.DownloadAssetBundle)
{ {
if (_downloadhanlderAssetBundleOp == null) if (_downloadAssetBundleOp == null)
{ {
DownloadParam downloadParam = new DownloadParam(int.MaxValue, 60); DownloadParam downloadParam = new DownloadParam(int.MaxValue, 60);
string fileLoadPath = _fileSystem.GetWebFileLoadPath(_bundle); string fileLoadPath = _fileSystem.GetWebFileLoadPath(_bundle);
downloadParam.MainURL = DownloadSystemHelper.ConvertToWWWPath(fileLoadPath); downloadParam.MainURL = DownloadSystemHelper.ConvertToWWWPath(fileLoadPath);
downloadParam.FallbackURL = downloadParam.MainURL; downloadParam.FallbackURL = downloadParam.MainURL;
_downloadhanlderAssetBundleOp = new DownloadHandlerAssetBundleOperation(_fileSystem.DisableUnityWebCache, _bundle, downloadParam);
OperationSystem.StartOperation(_fileSystem.PackageName, _downloadhanlderAssetBundleOp); if (_bundle.Encrypted)
{
_downloadAssetBundleOp = new DownloadWebEncryptAssetBundleOperation(_fileSystem.DecryptionServices, _bundle, downloadParam);
OperationSystem.StartOperation(_fileSystem.PackageName, _downloadAssetBundleOp);
}
else
{
_downloadAssetBundleOp = new DownloadWebNormalAssetBundleOperation(_fileSystem.DisableUnityWebCache, _bundle, downloadParam);
OperationSystem.StartOperation(_fileSystem.PackageName, _downloadAssetBundleOp);
}
} }
DownloadProgress = _downloadhanlderAssetBundleOp.DownloadProgress; DownloadProgress = _downloadAssetBundleOp.DownloadProgress;
DownloadedBytes = _downloadhanlderAssetBundleOp.DownloadedBytes; DownloadedBytes = _downloadAssetBundleOp.DownloadedBytes;
Progress = _downloadhanlderAssetBundleOp.Progress; Progress = _downloadAssetBundleOp.Progress;
if (_downloadhanlderAssetBundleOp.IsDone == false) if (_downloadAssetBundleOp.IsDone == false)
return; return;
if (_downloadhanlderAssetBundleOp.Status == EOperationStatus.Succeed) if (_downloadAssetBundleOp.Status == EOperationStatus.Succeed)
{ {
var assetBundle = _downloadhanlderAssetBundleOp.Result; var assetBundle = _downloadAssetBundleOp.Result;
if (assetBundle == null) if (assetBundle == null)
{ {
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EOperationStatus.Failed; Status = EOperationStatus.Failed;
Error = $"{nameof(DownloadHandlerAssetBundleOperation)} loaded asset bundle is null !"; Error = $"{nameof(DownloadAssetBundleOperation)} loaded asset bundle is null !";
} }
else else
{ {
@ -68,7 +77,7 @@ namespace YooAsset
{ {
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EOperationStatus.Failed; Status = EOperationStatus.Failed;
Error = _downloadhanlderAssetBundleOp.Error; Error = _downloadAssetBundleOp.Error;
} }
} }
} }
@ -84,10 +93,10 @@ namespace YooAsset
} }
public override void AbortDownloadOperation() public override void AbortDownloadOperation()
{ {
if (_steps == ESteps.DownloadFile) if (_steps == ESteps.DownloadAssetBundle)
{ {
if (_downloadhanlderAssetBundleOp != null) if (_downloadAssetBundleOp != null)
_downloadhanlderAssetBundleOp.SetAbort(); _downloadAssetBundleOp.SetAbort();
} }
} }
} }

View File

@ -110,10 +110,11 @@ namespace YooAsset
/// 创建默认的WebServer文件系统参数 /// 创建默认的WebServer文件系统参数
/// </summary> /// </summary>
/// <param name="disableUnityWebCache">禁用Unity的网络缓存</param> /// <param name="disableUnityWebCache">禁用Unity的网络缓存</param>
public static FileSystemParameters CreateDefaultWebServerFileSystemParameters(bool disableUnityWebCache = false) public static FileSystemParameters CreateDefaultWebServerFileSystemParameters(IWebDecryptionServices decryptionServices = null, bool disableUnityWebCache = false)
{ {
string fileSystemClass = typeof(DefaultWebServerFileSystem).FullName; string fileSystemClass = typeof(DefaultWebServerFileSystem).FullName;
var fileSystemParams = new FileSystemParameters(fileSystemClass, null); var fileSystemParams = new FileSystemParameters(fileSystemClass, null);
fileSystemParams.AddParameter(FileSystemParametersDefine.DECRYPTION_SERVICES, decryptionServices);
fileSystemParams.AddParameter(FileSystemParametersDefine.DISABLE_UNITY_WEB_CACHE, disableUnityWebCache); fileSystemParams.AddParameter(FileSystemParametersDefine.DISABLE_UNITY_WEB_CACHE, disableUnityWebCache);
return fileSystemParams; return fileSystemParams;
} }
@ -123,11 +124,12 @@ namespace YooAsset
/// </summary> /// </summary>
/// <param name="remoteServices">远端资源地址查询服务类</param> /// <param name="remoteServices">远端资源地址查询服务类</param>
/// <param name="disableUnityWebCache">禁用Unity的网络缓存</param> /// <param name="disableUnityWebCache">禁用Unity的网络缓存</param>
public static FileSystemParameters CreateDefaultWebRemoteFileSystemParameters(IRemoteServices remoteServices, bool disableUnityWebCache = false) public static FileSystemParameters CreateDefaultWebRemoteFileSystemParameters(IRemoteServices remoteServices, IWebDecryptionServices decryptionServices = null, bool disableUnityWebCache = false)
{ {
string fileSystemClass = typeof(DefaultWebRemoteFileSystem).FullName; string fileSystemClass = typeof(DefaultWebRemoteFileSystem).FullName;
var fileSystemParams = new FileSystemParameters(fileSystemClass, null); var fileSystemParams = new FileSystemParameters(fileSystemClass, null);
fileSystemParams.AddParameter(FileSystemParametersDefine.REMOTE_SERVICES, remoteServices); fileSystemParams.AddParameter(FileSystemParametersDefine.REMOTE_SERVICES, remoteServices);
fileSystemParams.AddParameter(FileSystemParametersDefine.DECRYPTION_SERVICES, decryptionServices);
fileSystemParams.AddParameter(FileSystemParametersDefine.DISABLE_UNITY_WEB_CACHE, disableUnityWebCache); fileSystemParams.AddParameter(FileSystemParametersDefine.DISABLE_UNITY_WEB_CACHE, disableUnityWebCache);
return fileSystemParams; return fileSystemParams;
} }

View File

@ -0,0 +1,14 @@
using UnityEngine;
using UnityEngine.Networking;
namespace YooAsset
{
internal abstract class DownloadAssetBundleOperation : DefaultDownloadFileOperation
{
internal DownloadAssetBundleOperation(PackageBundle bundle, DownloadParam param) : base(bundle, param)
{
}
public AssetBundle Result;
}
}

View File

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

View File

@ -0,0 +1,144 @@
using UnityEngine;
using UnityEngine.Networking;
namespace YooAsset
{
internal class DownloadWebEncryptAssetBundleOperation : DownloadAssetBundleOperation
{
private readonly IWebDecryptionServices _decryptionServices;
private DownloadHandlerBuffer _downloadhandler;
private ESteps _steps = ESteps.None;
internal DownloadWebEncryptAssetBundleOperation(IWebDecryptionServices decryptionServices, PackageBundle bundle, DownloadParam param) : base(bundle, param)
{
_decryptionServices = decryptionServices;
}
internal override void InternalOnStart()
{
_steps = ESteps.CreateRequest;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
// 创建下载器
if (_steps == ESteps.CreateRequest)
{
// 获取请求地址
_requestURL = GetRequestURL();
// 重置变量
ResetRequestFiled();
// 创建下载器
CreateWebRequest();
_steps = ESteps.CheckRequest;
}
// 检测下载结果
if (_steps == ESteps.CheckRequest)
{
DownloadProgress = _webRequest.downloadProgress;
DownloadedBytes = (long)_webRequest.downloadedBytes;
Progress = DownloadProgress;
if (_webRequest.isDone == false)
{
CheckRequestTimeout();
return;
}
// 检查网络错误
if (CheckRequestResult())
{
if (_decryptionServices == null)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"The {nameof(IWebDecryptionServices)} is null !";
YooLogger.Error(Error);
return;
}
AssetBundle assetBundle = LoadEncryptedAssetBundle(_downloadhandler.data);
if (assetBundle == null)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = "Download handler asset bundle object is null !";
}
else
{
_steps = ESteps.Done;
Result = assetBundle;
Status = EOperationStatus.Succeed;
}
}
else
{
_steps = ESteps.TryAgain;
}
// 注意:最终释放请求器
DisposeWebRequest();
}
// 重新尝试下载
if (_steps == ESteps.TryAgain)
{
if (FailedTryAgain <= 0)
{
Status = EOperationStatus.Failed;
_steps = ESteps.Done;
YooLogger.Error(Error);
return;
}
_tryAgainTimer += Time.unscaledDeltaTime;
if (_tryAgainTimer > 1f)
{
FailedTryAgain--;
_steps = ESteps.CreateRequest;
YooLogger.Warning(Error);
}
}
}
internal override void InternalOnAbort()
{
_steps = ESteps.Done;
DisposeWebRequest();
}
private void CreateWebRequest()
{
_downloadhandler = new DownloadHandlerBuffer();
_webRequest = DownloadSystemHelper.NewUnityWebRequestGet(_requestURL);
_webRequest.downloadHandler = _downloadhandler;
_webRequest.disposeDownloadHandlerOnDispose = true;
_webRequest.SendWebRequest();
}
private void DisposeWebRequest()
{
if (_webRequest != null)
{
//注意引擎底层会自动调用Abort方法
_webRequest.Dispose();
_webRequest = null;
}
}
/// <summary>
/// 加载加密资源文件
/// </summary>
private AssetBundle LoadEncryptedAssetBundle(byte[] fileData)
{
var fileInfo = new WebDecryptFileInfo();
fileInfo.BundleName = Bundle.BundleName;
fileInfo.FileLoadCRC = Bundle.UnityCRC;
fileInfo.FileData = fileData;
var decryptResult = _decryptionServices.LoadAssetBundle(fileInfo);
return decryptResult.Result;
}
}
}

View File

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

View File

@ -3,16 +3,13 @@ using UnityEngine.Networking;
namespace YooAsset namespace YooAsset
{ {
internal class DownloadHandlerAssetBundleOperation : DefaultDownloadFileOperation internal class DownloadWebNormalAssetBundleOperation : DownloadAssetBundleOperation
{ {
private readonly bool _disableUnityWebCache; private readonly bool _disableUnityWebCache;
private DownloadHandlerAssetBundle _downloadhandler; private DownloadHandlerAssetBundle _downloadhandler;
private ESteps _steps = ESteps.None; private ESteps _steps = ESteps.None;
public AssetBundle Result { private set; get; } internal DownloadWebNormalAssetBundleOperation(bool disableUnityWebCache, PackageBundle bundle, DownloadParam param) : base(bundle, param)
internal DownloadHandlerAssetBundleOperation(bool disableUnityWebCache, PackageBundle bundle, DownloadParam param) : base(bundle, param)
{ {
_disableUnityWebCache = disableUnityWebCache; _disableUnityWebCache = disableUnityWebCache;
} }
@ -55,12 +52,12 @@ namespace YooAsset
// 检查网络错误 // 检查网络错误
if (CheckRequestResult()) if (CheckRequestResult())
{ {
var assetBundle = _downloadhandler.assetBundle; AssetBundle assetBundle = _downloadhandler.assetBundle;
if (assetBundle == null) if (assetBundle == null)
{ {
_steps = ESteps.Done; _steps = ESteps.Done;
Error = "Download handler asset bundle object is null !";
Status = EOperationStatus.Failed; Status = EOperationStatus.Failed;
Error = "Download handler asset bundle object is null !";
} }
else else
{ {
@ -106,7 +103,7 @@ namespace YooAsset
private void CreateWebRequest() private void CreateWebRequest()
{ {
_downloadhandler = CreateDownloadHandler(); _downloadhandler = CreateWebDownloadHandler();
_webRequest = DownloadSystemHelper.NewUnityWebRequestGet(_requestURL); _webRequest = DownloadSystemHelper.NewUnityWebRequestGet(_requestURL);
_webRequest.downloadHandler = _downloadhandler; _webRequest.downloadHandler = _downloadhandler;
_webRequest.disposeDownloadHandlerOnDispose = true; _webRequest.disposeDownloadHandlerOnDispose = true;
@ -121,7 +118,7 @@ namespace YooAsset
_webRequest = null; _webRequest = null;
} }
} }
private DownloadHandlerAssetBundle CreateDownloadHandler() private DownloadHandlerAssetBundle CreateWebDownloadHandler()
{ {
if (_disableUnityWebCache) if (_disableUnityWebCache)
{ {

View File

@ -8,12 +8,12 @@ namespace YooAsset
/// <summary> /// <summary>
/// 释放所有资源句柄,防止卸载过程中触发完成回调! /// 释放所有资源句柄,防止卸载过程中触发完成回调!
/// </summary> /// </summary>
public bool ReleaseAllHandles = true; public bool ReleaseAllHandles = false;
/// <summary> /// <summary>
/// 卸载过程中锁定加载操作,防止新的任务请求! /// 卸载过程中锁定加载操作,防止新的任务请求!
/// </summary> /// </summary>
public bool LockLoadOperation = true; public bool LockLoadOperation = false;
} }
public sealed class UnloadAllAssetsOperation : AsyncOperationBase public sealed class UnloadAllAssetsOperation : AsyncOperationBase

View File

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

View File

@ -3,6 +3,7 @@ using UnityEngine;
using UnityEngine.Networking; using UnityEngine.Networking;
using YooAsset; using YooAsset;
using TTSDK; using TTSDK;
using WeChatWASM;
internal class TTFSLoadBundleOperation : FSLoadBundleOperation internal class TTFSLoadBundleOperation : FSLoadBundleOperation
{ {
@ -49,7 +50,22 @@ internal class TTFSLoadBundleOperation : FSLoadBundleOperation
if (CheckRequestResult()) if (CheckRequestResult())
{ {
var assetBundle = (_webRequest.downloadHandler as DownloadHandlerTTAssetBundle).assetBundle; if (_bundle.Encrypted && _fileSystem.DecryptionServices == null)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"The {nameof(IWebDecryptionServices)} is null !";
YooLogger.Error(Error);
return;
}
AssetBundle assetBundle;
var downloadHanlder = _webRequest.downloadHandler as DownloadHandlerTTAssetBundle;
if (_bundle.Encrypted)
assetBundle = _fileSystem.LoadEncryptedAssetBundle(_bundle, downloadHanlder.data);
else
assetBundle = downloadHanlder.assetBundle;
if (assetBundle == null) if (assetBundle == null)
{ {
_steps = ESteps.Done; _steps = ESteps.Done;

View File

@ -8,11 +8,20 @@ using System;
public static class TiktokFileSystemCreater public static class TiktokFileSystemCreater
{ {
public static FileSystemParameters CreateByteGameFileSystemParameters(IRemoteServices remoteServices, string packageRoot) public static FileSystemParameters CreateByteGameFileSystemParameters(string packageRoot, IRemoteServices remoteServices)
{ {
string fileSystemClass = $"{nameof(TiktokFileSystem)},YooAsset.RuntimeExtension"; string fileSystemClass = $"{nameof(TiktokFileSystem)},YooAsset.RuntimeExtension";
var fileSystemParams = new FileSystemParameters(fileSystemClass, packageRoot); var fileSystemParams = new FileSystemParameters(fileSystemClass, packageRoot);
fileSystemParams.AddParameter("REMOTE_SERVICES", remoteServices); fileSystemParams.AddParameter(FileSystemParametersDefine.REMOTE_SERVICES, remoteServices);
return fileSystemParams;
}
public static FileSystemParameters CreateByteGameFileSystemParameters(string packageRoot, IRemoteServices remoteServices, IWebDecryptionServices decryptionServices)
{
string fileSystemClass = $"{nameof(TiktokFileSystem)},YooAsset.RuntimeExtension";
var fileSystemParams = new FileSystemParameters(fileSystemClass, packageRoot);
fileSystemParams.AddParameter(FileSystemParametersDefine.REMOTE_SERVICES, remoteServices);
fileSystemParams.AddParameter(FileSystemParametersDefine.DECRYPTION_SERVICES, decryptionServices);
return fileSystemParams; return fileSystemParams;
} }
} }
@ -89,6 +98,11 @@ internal class TiktokFileSystem : IFileSystem
/// 自定义参数:远程服务接口 /// 自定义参数:远程服务接口
/// </summary> /// </summary>
public IRemoteServices RemoteServices { private set; get; } = null; public IRemoteServices RemoteServices { private set; get; } = null;
/// <summary>
/// 自定义参数:解密方法类
/// </summary>
public IWebDecryptionServices DecryptionServices { private set; get; }
#endregion #endregion
@ -146,10 +160,14 @@ internal class TiktokFileSystem : IFileSystem
public virtual void SetParameter(string name, object value) public virtual void SetParameter(string name, object value)
{ {
if (name == "REMOTE_SERVICES") if (name == FileSystemParametersDefine.REMOTE_SERVICES)
{ {
RemoteServices = (IRemoteServices)value; RemoteServices = (IRemoteServices)value;
} }
else if (name == FileSystemParametersDefine.DECRYPTION_SERVICES)
{
DecryptionServices = (IWebDecryptionServices)value;
}
else else
{ {
YooLogger.Warning($"Invalid parameter : {name}"); YooLogger.Warning($"Invalid parameter : {name}");
@ -250,6 +268,19 @@ internal class TiktokFileSystem : IFileSystem
} }
return filePath; return filePath;
} }
/// <summary>
/// 加载加密资源文件
/// </summary>
public AssetBundle LoadEncryptedAssetBundle(PackageBundle bundle, byte[] fileData)
{
WebDecryptFileInfo fileInfo = new WebDecryptFileInfo();
fileInfo.BundleName = bundle.BundleName;
fileInfo.FileLoadCRC = bundle.UnityCRC;
fileInfo.FileData = fileData;
var decryptResult = DecryptionServices.LoadAssetBundle(fileInfo);
return decryptResult.Result;
}
#endregion #endregion
} }
#endif #endif

View File

@ -53,7 +53,9 @@ internal class WXFSClearUnusedBundleFilesAsync : FSClearCacheFilesOperation
string bundleGUID = Path.GetFileNameWithoutExtension(fileStat.path); string bundleGUID = Path.GetFileNameWithoutExtension(fileStat.path);
if (_manifest.TryGetPackageBundleByBundleGUID(bundleGUID, out PackageBundle value) == false) if (_manifest.TryGetPackageBundleByBundleGUID(bundleGUID, out PackageBundle value) == false)
{ {
_unusedCacheFiles.Add(fileStat.path); string fullPath = WX.GetCachePath(fileStat.path);
if (_unusedCacheFiles.Contains(fullPath) == false)
_unusedCacheFiles.Add(fullPath);
} }
} }

View File

@ -49,7 +49,22 @@ internal class WXFSLoadBundleOperation : FSLoadBundleOperation
if (CheckRequestResult()) if (CheckRequestResult())
{ {
var assetBundle = (_webRequest.downloadHandler as DownloadHandlerWXAssetBundle).assetBundle; if (_bundle.Encrypted && _fileSystem.DecryptionServices == null)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"The {nameof(IWebDecryptionServices)} is null !";
YooLogger.Error(Error);
return;
}
AssetBundle assetBundle;
var downloadHanlder = _webRequest.downloadHandler as DownloadHandlerWXAssetBundle;
if (_bundle.Encrypted)
assetBundle = _fileSystem.LoadEncryptedAssetBundle(_bundle, downloadHanlder.data);
else
assetBundle = downloadHanlder.assetBundle;
if (assetBundle == null) if (assetBundle == null)
{ {
_steps = ESteps.Done; _steps = ESteps.Done;

View File

@ -8,11 +8,19 @@ using WeChatWASM;
public static class WechatFileSystemCreater public static class WechatFileSystemCreater
{ {
public static FileSystemParameters CreateWechatFileSystemParameters(IRemoteServices remoteServices, string packageRoot) public static FileSystemParameters CreateWechatFileSystemParameters(string packageRoot, IRemoteServices remoteServices)
{ {
string fileSystemClass = $"{nameof(WechatFileSystem)},YooAsset.RuntimeExtension"; string fileSystemClass = $"{nameof(WechatFileSystem)},YooAsset.RuntimeExtension";
var fileSystemParams = new FileSystemParameters(fileSystemClass, packageRoot); var fileSystemParams = new FileSystemParameters(fileSystemClass, packageRoot);
fileSystemParams.AddParameter("REMOTE_SERVICES", remoteServices); 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);
return fileSystemParams; return fileSystemParams;
} }
} }
@ -61,7 +69,7 @@ internal class WechatFileSystem : IFileSystem
/// 包裹名称 /// 包裹名称
/// </summary> /// </summary>
public string PackageName { private set; get; } public string PackageName { private set; get; }
private readonly string _packageRoot = YooAssetSettingsData.Setting.DefaultYooFolderName; private readonly string _packageRoot = YooAssetSettingsData.Setting.DefaultYooFolderName;
/// <summary> /// <summary>
@ -91,6 +99,11 @@ internal class WechatFileSystem : IFileSystem
/// 自定义参数:远程服务接口 /// 自定义参数:远程服务接口
/// </summary> /// </summary>
public IRemoteServices RemoteServices { private set; get; } = null; public IRemoteServices RemoteServices { private set; get; } = null;
/// <summary>
/// 自定义参数:解密方法类
/// </summary>
public IWebDecryptionServices DecryptionServices { private set; get; }
#endregion #endregion
@ -164,10 +177,14 @@ internal class WechatFileSystem : IFileSystem
public virtual void SetParameter(string name, object value) public virtual void SetParameter(string name, object value)
{ {
if (name == "REMOTE_SERVICES") if (name == FileSystemParametersDefine.REMOTE_SERVICES)
{ {
RemoteServices = (IRemoteServices)value; RemoteServices = (IRemoteServices)value;
} }
else if (name == FileSystemParametersDefine.DECRYPTION_SERVICES)
{
DecryptionServices = (IWebDecryptionServices)value;
}
else else
{ {
YooLogger.Warning($"Invalid parameter : {name}"); YooLogger.Warning($"Invalid parameter : {name}");
@ -183,6 +200,11 @@ internal class WechatFileSystem : IFileSystem
throw new System.Exception("请配置微信小游戏缓存根目录!"); throw new System.Exception("请配置微信小游戏缓存根目录!");
} }
if (_wxCacheRoot.StartsWith(WX.PluginCachePath) == false)
{
_wxCacheRoot = PathUtility.Combine(WX.PluginCachePath, _wxCacheRoot);
}
// 注意CDN服务未启用的情况下使用微信WEB服务器 // 注意CDN服务未启用的情况下使用微信WEB服务器
if (RemoteServices == null) if (RemoteServices == null)
{ {
@ -264,6 +286,19 @@ internal class WechatFileSystem : IFileSystem
} }
return filePath; return filePath;
} }
/// <summary>
/// 加载加密资源文件
/// </summary>
public AssetBundle LoadEncryptedAssetBundle(PackageBundle bundle, byte[] fileData)
{
WebDecryptFileInfo fileInfo = new WebDecryptFileInfo();
fileInfo.BundleName = bundle.BundleName;
fileInfo.FileLoadCRC = bundle.UnityCRC;
fileInfo.FileData = fileData;
var decryptResult = DecryptionServices.LoadAssetBundle(fileInfo);
return decryptResult.Result;
}
#endregion #endregion
} }
#endif #endif

View File

@ -74,10 +74,11 @@ internal class FsmInitializePackage : IStateNode
#if UNITY_WEBGL && WEIXINMINIGAME && !UNITY_EDITOR #if UNITY_WEBGL && WEIXINMINIGAME && !UNITY_EDITOR
string defaultHostServer = GetHostServerURL(); string defaultHostServer = GetHostServerURL();
string fallbackHostServer = GetHostServerURL(); string fallbackHostServer = GetHostServerURL();
string packageRoot = $"{WeChatWASM.WX.env.USER_DATA_PATH}/__GAME_FILE_CACHE"; //注意:如果有子目录,请修改此处!
IRemoteServices remoteServices = new RemoteServices(defaultHostServer, fallbackHostServer); IRemoteServices remoteServices = new RemoteServices(defaultHostServer, fallbackHostServer);
createParameters.WebServerFileSystemParameters = WechatFileSystemCreater.CreateWechatFileSystemParameters(remoteServices); createParameters.WebServerFileSystemParameters = WechatFileSystemCreater.CreateWechatFileSystemParameters(packageRoot, remoteServices);
#else #else
createParameters.WebServerFileSystemParameters = FileSystemParameters.CreateDefaultWebServerFileSystemParameters(); createParameters.WebServerFileSystemParameters = FileSystemParameters.CreateDefaultWebServerFileSystemParameters(new WebDecryption());
#endif #endif
initializationOperation = package.InitializeAsync(createParameters); initializationOperation = package.InitializeAsync(createParameters);
} }
@ -148,4 +149,24 @@ internal class FsmInitializePackage : IStateNode
return $"{_fallbackHostServer}/{fileName}"; return $"{_fallbackHostServer}/{fileName}";
} }
} }
private class WebDecryption : IWebDecryptionServices
{
public const byte KEY = 64;
public WebDecryptResult LoadAssetBundle(WebDecryptFileInfo fileInfo)
{
byte[] copyData = new byte[fileInfo.FileData.Length];
Buffer.BlockCopy(fileInfo.FileData, 0, copyData, 0, fileInfo.FileData.Length);
for (int i = 0; i < copyData.Length; i++)
{
copyData[i] ^= KEY;
}
WebDecryptResult decryptResult = new WebDecryptResult();
decryptResult.Result = AssetBundle.LoadFromMemory(copyData);
return decryptResult;
}
}
} }

View File

@ -4,6 +4,7 @@ using System.Text;
using UnityEngine; using UnityEngine;
using YooAsset; using YooAsset;
#region 文件流
/// <summary> /// <summary>
/// 资源文件解密流 /// 资源文件解密流
/// </summary> /// </summary>
@ -29,6 +30,35 @@ public class BundleStream : FileStream
} }
} }
/// <summary>
/// 文件流加密方式
/// </summary>
public class FileStreamEncryption : IEncryptionServices
{
public EncryptResult Encrypt(EncryptFileInfo fileInfo)
{
if (fileInfo.BundleName.Contains("_gameres_audio"))
{
var fileData = File.ReadAllBytes(fileInfo.FileLoadPath);
for (int i = 0; i < fileData.Length; i++)
{
fileData[i] ^= BundleStream.KEY;
}
EncryptResult result = new EncryptResult();
result.Encrypted = true;
result.EncryptedData = fileData;
return result;
}
else
{
EncryptResult result = new EncryptResult();
result.Encrypted = false;
return result;
}
}
}
/// <summary> /// <summary>
/// 资源文件流加载解密类 /// 资源文件流加载解密类
/// </summary> /// </summary>
@ -81,6 +111,37 @@ public class FileStreamDecryption : IDecryptionServices
return 1024; return 1024;
} }
} }
#endregion
#region 文件偏移
/// <summary>
/// 文件偏移加密方式
/// </summary>
public class FileOffsetEncryption : IEncryptionServices
{
public EncryptResult Encrypt(EncryptFileInfo fileInfo)
{
// 注意:只对音频资源包加密
if (fileInfo.BundleName.Contains("_gameres_audio"))
{
int offset = 32;
byte[] fileData = File.ReadAllBytes(fileInfo.FileLoadPath);
var encryptedData = new byte[fileData.Length + offset];
Buffer.BlockCopy(fileData, 0, encryptedData, offset, fileData.Length);
EncryptResult result = new EncryptResult();
result.Encrypted = true;
result.EncryptedData = encryptedData;
return result;
}
else
{
EncryptResult result = new EncryptResult();
result.Encrypted = false;
return result;
}
}
}
/// <summary> /// <summary>
/// 资源文件偏移加载解密类 /// 资源文件偏移加载解密类
@ -132,61 +193,4 @@ public class FileOffsetDecryption : IDecryptionServices
return 32; return 32;
} }
} }
#endregion
/// <summary>
/// 文件偏移加密方式
/// </summary>
public class FileOffsetEncryption : IEncryptionServices
{
public EncryptResult Encrypt(EncryptFileInfo fileInfo)
{
// 注意:只对音频资源包加密
if (fileInfo.BundleName.Contains("_gameres_audio"))
{
int offset = 32;
byte[] fileData = File.ReadAllBytes(fileInfo.FileLoadPath);
var encryptedData = new byte[fileData.Length + offset];
Buffer.BlockCopy(fileData, 0, encryptedData, offset, fileData.Length);
EncryptResult result = new EncryptResult();
result.Encrypted = true;
result.EncryptedData = encryptedData;
return result;
}
else
{
EncryptResult result = new EncryptResult();
result.Encrypted = false;
return result;
}
}
}
/// <summary>
/// 文件流加密方式
/// </summary>
public class FileStreamEncryption : IEncryptionServices
{
public EncryptResult Encrypt(EncryptFileInfo fileInfo)
{
if (fileInfo.BundleName.Contains("_gameres_audio"))
{
var fileData = File.ReadAllBytes(fileInfo.FileLoadPath);
for (int i = 0; i < fileData.Length; i++)
{
fileData[i] ^= BundleStream.KEY;
}
EncryptResult result = new EncryptResult();
result.Encrypted = true;
result.EncryptedData = fileData;
return result;
}
else
{
EncryptResult result = new EncryptResult();
result.Encrypted = false;
return result;
}
}
}