mirror of https://github.com/tuyoogame/YooAsset
增加查询预下载资源版本方法
parent
4c619778c3
commit
abaff0ede5
|
@ -18,6 +18,11 @@ namespace YooAsset
|
|||
/// </summary>
|
||||
UpdatePackageVersionOperation UpdatePackageVersionAsync(bool appendTimeTicks, int timeout);
|
||||
|
||||
/// <summary>
|
||||
/// 向网络端请求预下载资源版本
|
||||
/// </summary>
|
||||
UpdatePrePackageVersionOperation UpdatePrePackageVersionAsync(bool appendTimeTicks, int timeout);
|
||||
|
||||
/// <summary>
|
||||
/// 向网络端请求并更新清单
|
||||
/// </summary>
|
||||
|
|
|
@ -0,0 +1,106 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
namespace YooAsset
|
||||
{
|
||||
public class QueryRemotePrePackageVersionOperation : AsyncOperationBase
|
||||
{
|
||||
private enum ESteps
|
||||
{
|
||||
None,
|
||||
DownloadPrePackageVersion,
|
||||
Done,
|
||||
}
|
||||
|
||||
private readonly IRemoteServices _remoteServices;
|
||||
private readonly string _packageName;
|
||||
private readonly bool _appendTimeTicks;
|
||||
private readonly int _timeout;
|
||||
private UnityWebDataRequester _downloader;
|
||||
private ESteps _steps = ESteps.None;
|
||||
private int _requestCount = 0;
|
||||
|
||||
/// <summary>
|
||||
/// 包裹版本
|
||||
/// </summary>
|
||||
public string PrePackageVersion { private set; get; }
|
||||
|
||||
|
||||
public QueryRemotePrePackageVersionOperation(IRemoteServices remoteServices, string packageName, bool appendTimeTicks, int timeout)
|
||||
{
|
||||
_remoteServices = remoteServices;
|
||||
_packageName = packageName;
|
||||
_appendTimeTicks = appendTimeTicks;
|
||||
_timeout = timeout;
|
||||
}
|
||||
internal override void InternalOnStart()
|
||||
{
|
||||
_requestCount = RequestHelper.GetRequestFailedCount(_packageName, nameof(QueryRemotePrePackageVersionOperation));
|
||||
_steps = ESteps.DownloadPrePackageVersion;
|
||||
}
|
||||
internal override void InternalOnUpdate()
|
||||
{
|
||||
if (_steps == ESteps.None || _steps == ESteps.Done)
|
||||
return;
|
||||
|
||||
if (_steps == ESteps.DownloadPrePackageVersion)
|
||||
{
|
||||
if (_downloader == null)
|
||||
{
|
||||
string fileName = YooAssetSettingsData.GetPrePackageVersionFileName(_packageName);
|
||||
string webURL = GetPackageVersionRequestURL(fileName);
|
||||
YooLogger.Log($"Beginning to request pre package version : {webURL}");
|
||||
_downloader = new UnityWebDataRequester();
|
||||
_downloader.SendRequest(webURL, _timeout);
|
||||
}
|
||||
|
||||
Progress = _downloader.Progress();
|
||||
_downloader.CheckTimeout();
|
||||
if (_downloader.IsDone() == false)
|
||||
return;
|
||||
|
||||
if (_downloader.HasError())
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Failed;
|
||||
Error = _downloader.GetError();
|
||||
RequestHelper.RecordRequestFailed(_packageName, nameof(QueryRemotePrePackageVersionOperation));
|
||||
}
|
||||
else
|
||||
{
|
||||
PrePackageVersion = _downloader.GetText();
|
||||
if (string.IsNullOrEmpty(PrePackageVersion))
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Failed;
|
||||
Error = $"Remote package version is empty : {_downloader.URL}";
|
||||
}
|
||||
else
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Succeed;
|
||||
}
|
||||
}
|
||||
|
||||
_downloader.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
private string GetPackageVersionRequestURL(string fileName)
|
||||
{
|
||||
string url;
|
||||
|
||||
// 轮流返回请求地址
|
||||
if (_requestCount % 2 == 0)
|
||||
url = _remoteServices.GetRemoteMainURL(fileName);
|
||||
else
|
||||
url = _remoteServices.GetRemoteFallbackURL(fileName);
|
||||
|
||||
// 在URL末尾添加时间戳
|
||||
if (_appendTimeTicks)
|
||||
return $"{url}?{System.DateTime.UtcNow.Ticks}";
|
||||
else
|
||||
return url;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 35dd12fbef2e8d54ebf676a7e8c35d23
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,161 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
public abstract class UpdatePrePackageVersionOperation : AsyncOperationBase
|
||||
{
|
||||
/// <summary>
|
||||
/// 预下载包裹版本
|
||||
/// </summary>
|
||||
public string PrePackageVersion { protected set; get; }
|
||||
}
|
||||
/// <summary>
|
||||
/// 编辑器下模拟模式的请求远端包裹的预下载版本
|
||||
/// </summary>
|
||||
internal sealed class EditorPlayModeUpdatePrePackageVersionOperation : UpdatePrePackageVersionOperation
|
||||
{
|
||||
internal override void InternalOnStart()
|
||||
{
|
||||
Status = EOperationStatus.Succeed;
|
||||
}
|
||||
internal override void InternalOnUpdate()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 离线模式的请求远端包裹的预下载版本
|
||||
/// </summary>
|
||||
internal sealed class OfflinePlayModeUpdatePrePackageVersionOperation : UpdatePrePackageVersionOperation
|
||||
{
|
||||
internal override void InternalOnStart()
|
||||
{
|
||||
Status = EOperationStatus.Succeed;
|
||||
}
|
||||
internal override void InternalOnUpdate()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 联机模式的请求远端包裹的预下载版本
|
||||
/// </summary>
|
||||
internal sealed class HostPlayModeUpdatePrePackageVersionOperation : UpdatePrePackageVersionOperation
|
||||
{
|
||||
private enum ESteps
|
||||
{
|
||||
None,
|
||||
QueryRemotePrePackageVersion,
|
||||
Done,
|
||||
}
|
||||
|
||||
private readonly HostPlayModeImpl _impl;
|
||||
private readonly bool _appendTimeTicks;
|
||||
private readonly int _timeout;
|
||||
private QueryRemotePrePackageVersionOperation _queryRemotePrePackageVersionOp;
|
||||
private ESteps _steps = ESteps.None;
|
||||
|
||||
internal HostPlayModeUpdatePrePackageVersionOperation(HostPlayModeImpl impl, bool appendTimeTicks, int timeout)
|
||||
{
|
||||
_impl = impl;
|
||||
_appendTimeTicks = appendTimeTicks;
|
||||
_timeout = timeout;
|
||||
}
|
||||
internal override void InternalOnStart()
|
||||
{
|
||||
_steps = ESteps.QueryRemotePrePackageVersion;
|
||||
}
|
||||
internal override void InternalOnUpdate()
|
||||
{
|
||||
if (_steps == ESteps.None || _steps == ESteps.Done)
|
||||
return;
|
||||
|
||||
if (_steps == ESteps.QueryRemotePrePackageVersion)
|
||||
{
|
||||
if (_queryRemotePrePackageVersionOp == null)
|
||||
{
|
||||
_queryRemotePrePackageVersionOp = new QueryRemotePrePackageVersionOperation(_impl.RemoteServices, _impl.PackageName, _appendTimeTicks, _timeout);
|
||||
OperationSystem.StartOperation(_impl.PackageName, _queryRemotePrePackageVersionOp);
|
||||
}
|
||||
|
||||
if (_queryRemotePrePackageVersionOp.IsDone == false)
|
||||
return;
|
||||
|
||||
if (_queryRemotePrePackageVersionOp.Status == EOperationStatus.Succeed)
|
||||
{
|
||||
PrePackageVersion = _queryRemotePrePackageVersionOp.PrePackageVersion;
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Succeed;
|
||||
}
|
||||
else
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Failed;
|
||||
Error = _queryRemotePrePackageVersionOp.Error;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// WebGL模式的请求远端包裹的预下载版本
|
||||
/// </summary>
|
||||
internal sealed class WebPlayModeUpdatePrePackageVersionOperation : UpdatePrePackageVersionOperation
|
||||
{
|
||||
private enum ESteps
|
||||
{
|
||||
None,
|
||||
QueryRemotePackageVersion,
|
||||
Done,
|
||||
}
|
||||
|
||||
private readonly WebPlayModeImpl _impl;
|
||||
private readonly bool _appendTimeTicks;
|
||||
private readonly int _timeout;
|
||||
private QueryRemotePrePackageVersionOperation _queryRemotePrePackageVersionOp;
|
||||
private ESteps _steps = ESteps.None;
|
||||
|
||||
internal WebPlayModeUpdatePrePackageVersionOperation(WebPlayModeImpl impl, bool appendTimeTicks, int timeout)
|
||||
{
|
||||
_impl = impl;
|
||||
_appendTimeTicks = appendTimeTicks;
|
||||
_timeout = timeout;
|
||||
}
|
||||
internal override void InternalOnStart()
|
||||
{
|
||||
_steps = ESteps.QueryRemotePackageVersion;
|
||||
}
|
||||
internal override void InternalOnUpdate()
|
||||
{
|
||||
if (_steps == ESteps.None || _steps == ESteps.Done)
|
||||
return;
|
||||
|
||||
if (_steps == ESteps.QueryRemotePackageVersion)
|
||||
{
|
||||
if (_queryRemotePrePackageVersionOp == null)
|
||||
{
|
||||
_queryRemotePrePackageVersionOp = new QueryRemotePrePackageVersionOperation(_impl.RemoteServices, _impl.PackageName, _appendTimeTicks, _timeout);
|
||||
OperationSystem.StartOperation(_impl.PackageName, _queryRemotePrePackageVersionOp);
|
||||
}
|
||||
|
||||
if (_queryRemotePrePackageVersionOp.IsDone == false)
|
||||
return;
|
||||
|
||||
if (_queryRemotePrePackageVersionOp.Status == EOperationStatus.Succeed)
|
||||
{
|
||||
PrePackageVersion = _queryRemotePrePackageVersionOp.PrePackageVersion;
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Succeed;
|
||||
}
|
||||
else
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Failed;
|
||||
Error = _queryRemotePrePackageVersionOp.Error;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: d00ac4f9770af3a41a25dbdd53d0beaa
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -54,6 +54,12 @@ namespace YooAsset
|
|||
OperationSystem.StartOperation(PackageName, operation);
|
||||
return operation;
|
||||
}
|
||||
UpdatePrePackageVersionOperation IPlayMode.UpdatePrePackageVersionAsync(bool appendTimeTicks, int timeout)
|
||||
{
|
||||
var operation = new EditorPlayModeUpdatePrePackageVersionOperation();
|
||||
OperationSystem.StartOperation(PackageName, operation);
|
||||
return operation;
|
||||
}
|
||||
UpdatePackageManifestOperation IPlayMode.UpdatePackageManifestAsync(string packageVersion, bool autoSaveVersion, int timeout)
|
||||
{
|
||||
var operation = new EditorPlayModeUpdatePackageManifestOperation();
|
||||
|
|
|
@ -112,6 +112,12 @@ namespace YooAsset
|
|||
OperationSystem.StartOperation(PackageName, operation);
|
||||
return operation;
|
||||
}
|
||||
UpdatePrePackageVersionOperation IPlayMode.UpdatePrePackageVersionAsync(bool appendTimeTicks, int timeout)
|
||||
{
|
||||
var operation = new HostPlayModeUpdatePrePackageVersionOperation(this, appendTimeTicks, timeout);
|
||||
OperationSystem.StartOperation(PackageName, operation);
|
||||
return operation;
|
||||
}
|
||||
UpdatePackageManifestOperation IPlayMode.UpdatePackageManifestAsync(string packageVersion, bool autoSaveVersion, int timeout)
|
||||
{
|
||||
var operation = new HostPlayModeUpdatePackageManifestOperation(this, packageVersion, autoSaveVersion, timeout);
|
||||
|
|
|
@ -69,6 +69,12 @@ namespace YooAsset
|
|||
OperationSystem.StartOperation(PackageName, operation);
|
||||
return operation;
|
||||
}
|
||||
UpdatePrePackageVersionOperation IPlayMode.UpdatePrePackageVersionAsync(bool appendTimeTicks, int timeout)
|
||||
{
|
||||
var operation = new OfflinePlayModeUpdatePrePackageVersionOperation();
|
||||
OperationSystem.StartOperation(PackageName, operation);
|
||||
return operation;
|
||||
}
|
||||
UpdatePackageManifestOperation IPlayMode.UpdatePackageManifestAsync(string packageVersion, bool autoSaveVersion, int timeout)
|
||||
{
|
||||
var operation = new OfflinePlayModeUpdatePackageManifestOperation();
|
||||
|
|
|
@ -109,6 +109,12 @@ namespace YooAsset
|
|||
OperationSystem.StartOperation(PackageName, operation);
|
||||
return operation;
|
||||
}
|
||||
UpdatePrePackageVersionOperation IPlayMode.UpdatePrePackageVersionAsync(bool appendTimeTicks, int timeout)
|
||||
{
|
||||
var operation = new WebPlayModeUpdatePrePackageVersionOperation(this, appendTimeTicks, timeout);
|
||||
OperationSystem.StartOperation(PackageName, operation);
|
||||
return operation;
|
||||
}
|
||||
UpdatePackageManifestOperation IPlayMode.UpdatePackageManifestAsync(string packageVersion, bool autoSaveVersion, int timeout)
|
||||
{
|
||||
var operation = new WebPlayModeUpdatePackageManifestOperation(this, packageVersion, timeout);
|
||||
|
|
|
@ -284,6 +284,16 @@ namespace YooAsset
|
|||
return _playModeImpl.UpdatePackageVersionAsync(appendTimeTicks, timeout);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 向网络端请求预下载资源版本
|
||||
/// </summary>
|
||||
/// <param name="appendTimeTicks">在URL末尾添加时间戳</param>
|
||||
/// <param name="timeout">超时时间(默认值:3秒)</param>
|
||||
public UpdatePrePackageVersionOperation UpdatePrePackageVersionAsync(bool appendTimeTicks = true, int timeout = 3)
|
||||
{
|
||||
DebugCheckInitialize(false);
|
||||
return _playModeImpl.UpdatePrePackageVersionAsync(appendTimeTicks, timeout);
|
||||
}
|
||||
/// <summary>
|
||||
/// 向网络端请求并更新清单
|
||||
/// </summary>
|
||||
|
|
|
@ -71,5 +71,13 @@ namespace YooAsset
|
|||
{
|
||||
return $"{Setting.ManifestFileName}_{packageName}.version";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取包裹的预下载版本文件完整名称
|
||||
/// </summary>
|
||||
public static string GetPrePackageVersionFileName(string packageName)
|
||||
{
|
||||
return $"{Setting.ManifestFileName}_{packageName}.preversion";
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue