mirror of https://github.com/tuyoogame/YooAsset
parent
068a712a30
commit
e52bdea255
|
@ -42,7 +42,9 @@ internal class WXFSDownloadFileOperation : DefaultDownloadFileOperation
|
||||||
Progress = DownloadProgress;
|
Progress = DownloadProgress;
|
||||||
if (_webRequest.isDone == false)
|
if (_webRequest.isDone == false)
|
||||||
{
|
{
|
||||||
CheckRequestTimeout();
|
//TODO 由于微信小游戏插件的问题,暂时不能判定超时!
|
||||||
|
// Issue : https://github.com/wechat-miniprogram/minigame-unity-webgl-transform/issues/108#
|
||||||
|
//CheckRequestTimeout();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,21 +1,18 @@
|
||||||
#if UNITY_WEBGL && WEIXINMINIGAME
|
#if UNITY_WEBGL && WEIXINMINIGAME
|
||||||
using UnityEngine;
|
|
||||||
using UnityEngine.Networking;
|
|
||||||
using YooAsset;
|
using YooAsset;
|
||||||
using WeChatWASM;
|
|
||||||
|
|
||||||
internal class WXFSLoadBundleOperation : FSLoadBundleOperation
|
internal class WXFSLoadBundleOperation : FSLoadBundleOperation
|
||||||
{
|
{
|
||||||
private enum ESteps
|
private enum ESteps
|
||||||
{
|
{
|
||||||
None,
|
None,
|
||||||
LoadBundleFile,
|
DownloadAssetBundle,
|
||||||
Done,
|
Done,
|
||||||
}
|
}
|
||||||
|
|
||||||
private readonly WechatFileSystem _fileSystem;
|
private readonly WechatFileSystem _fileSystem;
|
||||||
private readonly PackageBundle _bundle;
|
private readonly PackageBundle _bundle;
|
||||||
private UnityWebRequest _webRequest;
|
private DownloadAssetBundleOperation _downloadAssetBundleOp;
|
||||||
private ESteps _steps = ESteps.None;
|
private ESteps _steps = ESteps.None;
|
||||||
|
|
||||||
internal WXFSLoadBundleOperation(WechatFileSystem fileSystem, PackageBundle bundle)
|
internal WXFSLoadBundleOperation(WechatFileSystem fileSystem, PackageBundle bundle)
|
||||||
|
@ -25,75 +22,60 @@ internal class WXFSLoadBundleOperation : FSLoadBundleOperation
|
||||||
}
|
}
|
||||||
internal override void InternalOnStart()
|
internal override void InternalOnStart()
|
||||||
{
|
{
|
||||||
_steps = ESteps.LoadBundleFile;
|
_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.LoadBundleFile)
|
if (_steps == ESteps.DownloadAssetBundle)
|
||||||
{
|
{
|
||||||
if (_webRequest == null)
|
if (_downloadAssetBundleOp == null)
|
||||||
{
|
{
|
||||||
string mainURL = _fileSystem.RemoteServices.GetRemoteMainURL(_bundle.FileName);
|
DownloadParam downloadParam = new DownloadParam(int.MaxValue, 60);
|
||||||
_webRequest = _bundle.Encrypted ? UnityWebRequest.Get(mainURL) : WXAssetBundle.GetAssetBundle(mainURL);
|
downloadParam.MainURL = _fileSystem.RemoteServices.GetRemoteMainURL(_bundle.FileName); ;
|
||||||
_webRequest.SendWebRequest();
|
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)
|
if (_bundle.Encrypted)
|
||||||
{
|
{
|
||||||
var downloadHanlder = (DownloadHandlerBuffer)_webRequest.downloadHandler;
|
_downloadAssetBundleOp = new DownloadWebEncryptAssetBundleOperation(_fileSystem.DecryptionServices, _bundle, downloadParam);
|
||||||
assetBundle = _fileSystem.LoadEncryptedAssetBundle(_bundle, downloadHanlder.data);
|
OperationSystem.StartOperation(_fileSystem.PackageName, _downloadAssetBundleOp);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var downloadHanlder = (DownloadHandlerWXAssetBundle)_webRequest.downloadHandler;
|
_downloadAssetBundleOp = new DownloadWechatAssetBundleOperation(_bundle, downloadParam);
|
||||||
assetBundle = downloadHanlder.assetBundle;
|
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)
|
if (assetBundle == null)
|
||||||
{
|
{
|
||||||
_steps = ESteps.Done;
|
_steps = ESteps.Done;
|
||||||
Error = $"{nameof(DownloadHandlerWXAssetBundle)} loaded asset bundle is null !";
|
|
||||||
Status = EOperationStatus.Failed;
|
Status = EOperationStatus.Failed;
|
||||||
|
Error = $"{nameof(DownloadAssetBundleOperation)} loaded asset bundle is null !";
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_steps = ESteps.Done;
|
_steps = ESteps.Done;
|
||||||
Result = new WXAssetBundleResult(_fileSystem, _bundle, assetBundle);
|
Result = new AssetBundleResult(_fileSystem, _bundle, assetBundle, null);
|
||||||
Status = EOperationStatus.Succeed;
|
Status = EOperationStatus.Succeed;
|
||||||
|
|
||||||
//TODO 解决微信小游戏插件问题
|
|
||||||
// Issue : https://github.com/wechat-miniprogram/minigame-unity-webgl-transform/issues/108#
|
|
||||||
DownloadProgress = 1f;
|
|
||||||
DownloadedBytes = _bundle.FileSize;
|
|
||||||
Progress = 1f;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_steps = ESteps.Done;
|
_steps = ESteps.Done;
|
||||||
Status = EOperationStatus.Failed;
|
Status = EOperationStatus.Failed;
|
||||||
|
Error = _downloadAssetBundleOp.Error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -109,31 +91,11 @@ internal class WXFSLoadBundleOperation : FSLoadBundleOperation
|
||||||
}
|
}
|
||||||
public override void AbortDownloadOperation()
|
public override void AbortDownloadOperation()
|
||||||
{
|
{
|
||||||
}
|
if (_steps == ESteps.DownloadAssetBundle)
|
||||||
|
|
||||||
private bool CheckRequestResult()
|
|
||||||
{
|
|
||||||
#if UNITY_2020_3_OR_NEWER
|
|
||||||
if (_webRequest.result != UnityWebRequest.Result.Success)
|
|
||||||
{
|
{
|
||||||
Error = _webRequest.error;
|
if (_downloadAssetBundleOp != null)
|
||||||
return false;
|
_downloadAssetBundleOp.SetAbort();
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
if (_webRequest.isNetworkError || _webRequest.isHttpError)
|
|
||||||
{
|
|
||||||
Error = _webRequest.error;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
|
@ -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
|
|
@ -0,0 +1,11 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: e556be3f274429e4ca6c542fff90e6ab
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
Loading…
Reference in New Issue