微信小游戏资源加密
pull/497/head
何冠峰 2025-02-13 12:02:09 +08:00
parent 068a712a30
commit e52bdea255
4 changed files with 171 additions and 67 deletions

View File

@ -42,7 +42,9 @@ internal class WXFSDownloadFileOperation : DefaultDownloadFileOperation
Progress = DownloadProgress;
if (_webRequest.isDone == false)
{
CheckRequestTimeout();
//TODO 由于微信小游戏插件的问题,暂时不能判定超时!
// Issue : https://github.com/wechat-miniprogram/minigame-unity-webgl-transform/issues/108#
//CheckRequestTimeout();
return;
}

View File

@ -1,21 +1,18 @@
#if UNITY_WEBGL && WEIXINMINIGAME
using UnityEngine;
using UnityEngine.Networking;
using YooAsset;
using WeChatWASM;
internal class WXFSLoadBundleOperation : FSLoadBundleOperation
{
private enum ESteps
{
None,
LoadBundleFile,
DownloadAssetBundle,
Done,
}
private readonly WechatFileSystem _fileSystem;
private readonly PackageBundle _bundle;
private UnityWebRequest _webRequest;
private DownloadAssetBundleOperation _downloadAssetBundleOp;
private ESteps _steps = ESteps.None;
internal WXFSLoadBundleOperation(WechatFileSystem fileSystem, PackageBundle bundle)
@ -25,75 +22,60 @@ internal class WXFSLoadBundleOperation : FSLoadBundleOperation
}
internal override void InternalOnStart()
{
_steps = ESteps.LoadBundleFile;
_steps = ESteps.DownloadAssetBundle;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.LoadBundleFile)
if (_steps == ESteps.DownloadAssetBundle)
{
if (_webRequest == null)
if (_downloadAssetBundleOp == null)
{
string mainURL = _fileSystem.RemoteServices.GetRemoteMainURL(_bundle.FileName);
_webRequest = _bundle.Encrypted ? UnityWebRequest.Get(mainURL) : WXAssetBundle.GetAssetBundle(mainURL);
_webRequest.SendWebRequest();
}
DownloadParam downloadParam = new DownloadParam(int.MaxValue, 60);
downloadParam.MainURL = _fileSystem.RemoteServices.GetRemoteMainURL(_bundle.FileName); ;
downloadParam.FallbackURL = _fileSystem.RemoteServices.GetRemoteFallbackURL(_bundle.FileName);
DownloadProgress = _webRequest.downloadProgress;
DownloadedBytes = (long)_webRequest.downloadedBytes;
Progress = DownloadProgress;
if (_webRequest.isDone == false)
return;
if (CheckRequestResult())
{
if (_bundle.Encrypted && _fileSystem.DecryptionServices == null)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"The {nameof(IWebDecryptionServices)} is null !";
YooLogger.Error(Error);
return;
}
AssetBundle assetBundle;
if (_bundle.Encrypted)
{
var downloadHanlder = (DownloadHandlerBuffer)_webRequest.downloadHandler;
assetBundle = _fileSystem.LoadEncryptedAssetBundle(_bundle, downloadHanlder.data);
_downloadAssetBundleOp = new DownloadWebEncryptAssetBundleOperation(_fileSystem.DecryptionServices, _bundle, downloadParam);
OperationSystem.StartOperation(_fileSystem.PackageName, _downloadAssetBundleOp);
}
else
{
var downloadHanlder = (DownloadHandlerWXAssetBundle)_webRequest.downloadHandler;
assetBundle = downloadHanlder.assetBundle;
_downloadAssetBundleOp = new DownloadWechatAssetBundleOperation(_bundle, downloadParam);
OperationSystem.StartOperation(_fileSystem.PackageName, _downloadAssetBundleOp);
}
}
DownloadProgress = _downloadAssetBundleOp.DownloadProgress;
DownloadedBytes = (long)_downloadAssetBundleOp.DownloadedBytes;
Progress = _downloadAssetBundleOp.DownloadProgress; ;
if (_downloadAssetBundleOp.IsDone == false)
return;
if (_downloadAssetBundleOp.Status == EOperationStatus.Succeed)
{
var assetBundle = _downloadAssetBundleOp.Result;
if (assetBundle == null)
{
_steps = ESteps.Done;
Error = $"{nameof(DownloadHandlerWXAssetBundle)} loaded asset bundle is null !";
Status = EOperationStatus.Failed;
Error = $"{nameof(DownloadAssetBundleOperation)} loaded asset bundle is null !";
}
else
{
_steps = ESteps.Done;
Result = new WXAssetBundleResult(_fileSystem, _bundle, assetBundle);
Result = new AssetBundleResult(_fileSystem, _bundle, assetBundle, null);
Status = EOperationStatus.Succeed;
//TODO 解决微信小游戏插件问题
// Issue : https://github.com/wechat-miniprogram/minigame-unity-webgl-transform/issues/108#
DownloadProgress = 1f;
DownloadedBytes = _bundle.FileSize;
Progress = 1f;
}
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _downloadAssetBundleOp.Error;
}
}
}
@ -109,31 +91,11 @@ internal class WXFSLoadBundleOperation : FSLoadBundleOperation
}
public override void AbortDownloadOperation()
{
}
private bool CheckRequestResult()
{
#if UNITY_2020_3_OR_NEWER
if (_webRequest.result != UnityWebRequest.Result.Success)
if (_steps == ESteps.DownloadAssetBundle)
{
Error = _webRequest.error;
return false;
if (_downloadAssetBundleOp != null)
_downloadAssetBundleOp.SetAbort();
}
else
{
return true;
}
#else
if (_webRequest.isNetworkError || _webRequest.isHttpError)
{
Error = _webRequest.error;
return false;
}
else
{
return true;
}
#endif
}
}
#endif

View File

@ -0,0 +1,129 @@
#if UNITY_WEBGL && WEIXINMINIGAME
using UnityEngine;
using UnityEngine.Networking;
using WeChatWASM;
namespace YooAsset
{
internal class DownloadWechatAssetBundleOperation : DownloadAssetBundleOperation
{
private ESteps _steps = ESteps.None;
internal DownloadWechatAssetBundleOperation(PackageBundle bundle, DownloadParam param) : base(bundle, param)
{
}
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)
{
//TODO 由于微信小游戏插件的问题,暂时不能判定超时!
// Issue : https://github.com/wechat-miniprogram/minigame-unity-webgl-transform/issues/108#
//CheckRequestTimeout();
return;
}
// 检查网络错误
if (CheckRequestResult())
{
var downloadHanlder = (DownloadHandlerWXAssetBundle)_webRequest.downloadHandler;
AssetBundle assetBundle = downloadHanlder.assetBundle;
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;
//TODO 解决微信小游戏插件问题
// Issue : https://github.com/wechat-miniprogram/minigame-unity-webgl-transform/issues/108#
DownloadProgress = 1f;
DownloadedBytes = Bundle.FileSize;
Progress = 1f;
}
}
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()
{
_webRequest = WXAssetBundle.GetAssetBundle(_requestURL);
_webRequest.disposeDownloadHandlerOnDispose = true;
_webRequest.SendWebRequest();
}
private void DisposeWebRequest()
{
if (_webRequest != null)
{
//注意引擎底层会自动调用Abort方法
_webRequest.Dispose();
_webRequest = null;
}
}
}
}
#endif

View File

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