Compare commits

...

7 Commits

Author SHA1 Message Date
何冠峰
053b4a00d7 update test sample 2025-07-17 22:24:27 +08:00
何冠峰
db159428c6 update test sample
增加cache file system单元测试
2025-07-17 21:26:13 +08:00
何冠峰
ac7ee16017 update mini game 2025-07-17 21:23:34 +08:00
何冠峰
c2fb7c3cbb update package invoke
correct name
2025-07-17 21:09:35 +08:00
何冠峰
e70b0d37cd update extension sample 2025-07-17 21:00:46 +08:00
何冠峰
53db012fc8 update download system 2025-07-17 21:00:12 +08:00
何冠峰
b3622167da update download system
remove timeout
2025-07-17 20:59:15 +08:00
113 changed files with 1907 additions and 950 deletions

View File

@@ -94,4 +94,25 @@ namespace YooAsset
/// </summary>
public long FileSize;
}
/// <summary>
/// 导入文件的信息
/// </summary>
public struct ImportFileInfo
{
/// <summary>
/// 本地文件路径
/// </summary>
public string FilePath;
/// <summary>
/// 资源包名称
/// </summary>
public string BundleName;
/// <summary>
/// 资源包GUID
/// </summary>
public string BundleGUID;
}
}

View File

@@ -24,7 +24,7 @@ namespace YooAsset
/// </summary>
public AssetBundle Result { private set; get; }
internal UnityAssetBundleRequestOperation(PackageBundle packageBundle, bool disableUnityWebCache, string url, int timeout = 60) : base(url, timeout)
internal UnityAssetBundleRequestOperation(PackageBundle packageBundle, bool disableUnityWebCache, string url) : base(url)
{
_packageBundle = packageBundle;
_disableUnityWebCache = disableUnityWebCache;
@@ -40,7 +40,6 @@ namespace YooAsset
if (_steps == ESteps.CreateRequest)
{
ResetTimeout();
CreateWebRequest();
_steps = ESteps.Download;
}
@@ -51,10 +50,7 @@ namespace YooAsset
DownloadedBytes = (long)_webRequest.downloadedBytes;
Progress = _requestOperation.progress;
if (_requestOperation.isDone == false)
{
CheckRequestTimeout();
return;
}
if (CheckRequestResult())
{

View File

@@ -0,0 +1,77 @@
using UnityEngine.Networking;
using UnityEngine;
namespace YooAsset
{
internal class UnityWebCacheRequestOperation : UnityWebRequestOperation
{
protected enum ESteps
{
None,
CreateRequest,
Download,
Done,
}
private UnityWebRequestAsyncOperation _requestOperation;
private ESteps _steps = ESteps.None;
internal UnityWebCacheRequestOperation(string url) : base(url)
{
}
internal override void InternalStart()
{
_steps = ESteps.CreateRequest;
}
internal override void InternalUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.CreateRequest)
{
CreateWebRequest();
_steps = ESteps.Download;
}
if (_steps == ESteps.Download)
{
DownloadProgress = _webRequest.downloadProgress;
DownloadedBytes = (long)_webRequest.downloadedBytes;
Progress = _requestOperation.progress;
if (_requestOperation.isDone == false)
return;
if (CheckRequestResult())
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
}
// 注意:最终释放请求器
DisposeRequest();
}
}
/// <summary>
/// 设置请求头信息
/// </summary>
public void SetRequestHeader(string name, string value)
{
_webRequest.SetRequestHeader(name, value);
}
private void CreateWebRequest()
{
_webRequest = DownloadSystemHelper.NewUnityWebRequestGet(_requestURL);
_webRequest.disposeDownloadHandlerOnDispose = true;
_requestOperation = _webRequest.SendWebRequest();
}
}
}

View File

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

View File

@@ -14,17 +14,24 @@ namespace YooAsset
}
private UnityWebRequestAsyncOperation _requestOperation;
private bool _checkTimeout = true;
private ESteps _steps = ESteps.None;
/// <summary>
/// 响应的超时时间单位在经过Timeout的秒数后尝试中止。
/// 注意当Timeout设置为0时不会应用超时。
/// 注意设置的超时值可能应用于Android上的每个URL重定向这可能会导致响应时间增加。
/// </summary>
private readonly int _timeout;
/// <summary>
/// 请求结果
/// </summary>
public byte[] Result { private set; get; }
internal UnityWebDataRequestOperation(string url, int timeout = 60) : base(url, timeout)
internal UnityWebDataRequestOperation(string url, int timeout) : base(url)
{
_timeout = timeout;
}
internal override void InternalStart()
{
@@ -37,7 +44,6 @@ namespace YooAsset
if (_steps == ESteps.CreateRequest)
{
ResetTimeout();
CreateWebRequest();
_steps = ESteps.Download;
}
@@ -48,11 +54,7 @@ namespace YooAsset
DownloadedBytes = (long)_webRequest.downloadedBytes;
Progress = _requestOperation.progress;
if (_requestOperation.isDone == false)
{
if (_checkTimeout)
CheckRequestTimeout();
return;
}
if (CheckRequestResult())
{
@@ -81,18 +83,11 @@ namespace YooAsset
}
}
/// <summary>
/// 不检测超时
/// </summary>
public void DontCheckTimeout()
{
_checkTimeout = false;
}
private void CreateWebRequest()
{
_webRequest = DownloadSystemHelper.NewUnityWebRequestGet(_requestURL);
DownloadHandlerBuffer handler = new DownloadHandlerBuffer();
_webRequest = DownloadSystemHelper.NewUnityWebRequestGet(_requestURL);
_webRequest.timeout = _timeout;
_webRequest.downloadHandler = handler;
_webRequest.disposeDownloadHandlerOnDispose = true;
_requestOperation = _webRequest.SendWebRequest();

View File

@@ -17,10 +17,18 @@ namespace YooAsset
private readonly string _fileSavePath;
private ESteps _steps = ESteps.None;
/// <summary>
/// 响应的超时时间单位在经过Timeout的秒数后尝试中止。
/// 注意当Timeout设置为0时不会应用超时。
/// 注意设置的超时值可能应用于Android上的每个URL重定向这可能会导致响应时间增加。
/// </summary>
private readonly int _timeout;
internal UnityWebFileRequestOperation(string url, string fileSavePath, int timeout = 60) : base(url, timeout)
internal UnityWebFileRequestOperation(string url, string fileSavePath, int timeout) : base(url)
{
_fileSavePath = fileSavePath;
_timeout = timeout;
}
internal override void InternalStart()
{
@@ -33,7 +41,6 @@ namespace YooAsset
if (_steps == ESteps.CreateRequest)
{
ResetTimeout();
CreateWebRequest();
_steps = ESteps.Download;
}
@@ -44,10 +51,7 @@ namespace YooAsset
DownloadedBytes = (long)_webRequest.downloadedBytes;
Progress = _requestOperation.progress;
if (_requestOperation.isDone == false)
{
CheckRequestTimeout();
return;
}
if (CheckRequestResult())
{
@@ -67,9 +71,10 @@ namespace YooAsset
private void CreateWebRequest()
{
_webRequest = DownloadSystemHelper.NewUnityWebRequestGet(_requestURL);
DownloadHandlerFile handler = new DownloadHandlerFile(_fileSavePath);
handler.removeFileOnAbort = true;
_webRequest = DownloadSystemHelper.NewUnityWebRequestGet(_requestURL);
_webRequest.timeout = _timeout;
_webRequest.downloadHandler = handler;
_webRequest.disposeDownloadHandlerOnDispose = true;
_requestOperation = _webRequest.SendWebRequest();

View File

@@ -8,11 +8,6 @@ namespace YooAsset
{
protected UnityWebRequest _webRequest;
protected readonly string _requestURL;
// 超时相关
private readonly float _timeout;
private ulong _latestDownloadBytes;
private float _latestDownloadRealtime;
private bool _isAbort = false;
/// <summary>
@@ -38,10 +33,9 @@ namespace YooAsset
get { return _requestURL; }
}
internal UnityWebRequestOperation(string url, int timeout)
internal UnityWebRequestOperation(string url)
{
_requestURL = url;
_timeout = timeout;
}
internal override void InternalAbort()
{
@@ -71,42 +65,6 @@ namespace YooAsset
}
}
/// <summary>
/// 重置超时计时
/// </summary>
protected void ResetTimeout()
{
_latestDownloadBytes = 0;
_latestDownloadRealtime = Time.realtimeSinceStartup;
}
/// <summary>
/// 检测超时
/// </summary>
protected void CheckRequestTimeout()
{
if (_webRequest.isDone)
return;
// 注意:在连续时间段内无新增下载数据及判定为超时
if (_isAbort == false)
{
if (_latestDownloadBytes != _webRequest.downloadedBytes)
{
_latestDownloadBytes = _webRequest.downloadedBytes;
_latestDownloadRealtime = Time.realtimeSinceStartup;
}
float offset = Time.realtimeSinceStartup - _latestDownloadRealtime;
if (offset > _timeout)
{
YooLogger.Warning($"Web request timeout : {_requestURL}");
_webRequest.Abort();
_isAbort = true;
}
}
}
/// <summary>
/// 检测请求结果
/// </summary>

View File

@@ -16,14 +16,22 @@ namespace YooAsset
private UnityWebRequestAsyncOperation _requestOperation;
private ESteps _steps = ESteps.None;
/// <summary>
/// 响应的超时时间单位在经过Timeout的秒数后尝试中止。
/// 注意当Timeout设置为0时不会应用超时。
/// 注意设置的超时值可能应用于Android上的每个URL重定向这可能会导致响应时间增加。
/// </summary>
private readonly int _timeout;
/// <summary>
/// 请求结果
/// </summary>
public string Result { private set; get; }
internal UnityWebTextRequestOperation(string url, int timeout = 60) : base(url, timeout)
internal UnityWebTextRequestOperation(string url, int timeout) : base(url)
{
_timeout = timeout;
}
internal override void InternalStart()
{
@@ -36,7 +44,6 @@ namespace YooAsset
if (_steps == ESteps.CreateRequest)
{
ResetTimeout();
CreateWebRequest();
_steps = ESteps.Download;
}
@@ -47,10 +54,7 @@ namespace YooAsset
DownloadedBytes = (long)_webRequest.downloadedBytes;
Progress = _requestOperation.progress;
if (_requestOperation.isDone == false)
{
CheckRequestTimeout();
return;
}
if (CheckRequestResult())
{
@@ -81,8 +85,9 @@ namespace YooAsset
private void CreateWebRequest()
{
_webRequest = DownloadSystemHelper.NewUnityWebRequestGet(_requestURL);
DownloadHandlerBuffer handler = new DownloadHandlerBuffer();
_webRequest = DownloadSystemHelper.NewUnityWebRequestGet(_requestURL);
_webRequest.timeout = _timeout;
_webRequest.downloadHandler = handler;
_webRequest.disposeDownloadHandlerOnDispose = true;
_requestOperation = _webRequest.SendWebRequest();

View File

@@ -83,7 +83,7 @@ namespace YooAsset
string sourcePath = _fileSystem.GetBuildinPackageHashFilePath(_buildinPackageVersion);
string destPath = GetCopyPackageHashDestPath(_buildinPackageVersion);
string url = DownloadSystemHelper.ConvertToWWWPath(sourcePath);
_hashFileRequestOp = new UnityWebFileRequestOperation(url, destPath);
_hashFileRequestOp = new UnityWebFileRequestOperation(url, destPath, 60);
_hashFileRequestOp.StartOperation();
AddChildOperation(_hashFileRequestOp);
}
@@ -124,7 +124,7 @@ namespace YooAsset
string sourcePath = _fileSystem.GetBuildinPackageManifestFilePath(_buildinPackageVersion);
string destPath = GetCopyPackageManifestDestPath(_buildinPackageVersion);
string url = DownloadSystemHelper.ConvertToWWWPath(sourcePath);
_manifestFileRequestOp = new UnityWebFileRequestOperation(url, destPath);
_manifestFileRequestOp = new UnityWebFileRequestOperation(url, destPath, 60);
_manifestFileRequestOp.StartOperation();
AddChildOperation(_manifestFileRequestOp);
}

View File

@@ -35,7 +35,7 @@ namespace YooAsset
{
string filePath = _fileSystem.GetCatalogBinaryFileLoadPath();
string url = DownloadSystemHelper.ConvertToWWWPath(filePath);
_webDataRequestOp = new UnityWebDataRequestOperation(url);
_webDataRequestOp = new UnityWebDataRequestOperation(url, 60);
_webDataRequestOp.StartOperation();
AddChildOperation(_webDataRequestOp);
}

View File

@@ -46,7 +46,7 @@ namespace YooAsset
{
string filePath = _fileSystem.GetBuildinPackageManifestFilePath(_packageVersion);
string url = DownloadSystemHelper.ConvertToWWWPath(filePath);
_webDataRequestOp = new UnityWebDataRequestOperation(url);
_webDataRequestOp = new UnityWebDataRequestOperation(url, 60);
_webDataRequestOp.StartOperation();
AddChildOperation(_webDataRequestOp);
}

View File

@@ -41,7 +41,7 @@ namespace YooAsset
{
string filePath = _fileSystem.GetBuildinPackageHashFilePath(_packageVersion);
string url = DownloadSystemHelper.ConvertToWWWPath(filePath);
_webTextRequestOp = new UnityWebTextRequestOperation(url);
_webTextRequestOp = new UnityWebTextRequestOperation(url, 60);
_webTextRequestOp.StartOperation();
AddChildOperation(_webTextRequestOp);
}

View File

@@ -39,7 +39,7 @@ namespace YooAsset
{
string filePath = _fileSystem.GetBuildinPackageVersionFilePath();
string url = DownloadSystemHelper.ConvertToWWWPath(filePath);
_webTextRequestOp = new UnityWebTextRequestOperation(url);
_webTextRequestOp = new UnityWebTextRequestOperation(url, 60);
_webTextRequestOp.StartOperation();
AddChildOperation(_webTextRequestOp);
}

View File

@@ -57,7 +57,7 @@ namespace YooAsset
{
if (_downloadFileOp == null)
{
DownloadFileOptions options = new DownloadFileOptions(int.MaxValue, 60);
DownloadFileOptions options = new DownloadFileOptions(int.MaxValue);
_downloadFileOp = _fileSystem.DownloadFileAsync(_bundle, options);
_downloadFileOp.StartOperation();
AddChildOperation(_downloadFileOp);
@@ -304,7 +304,7 @@ namespace YooAsset
{
if (_downloadFileOp == null)
{
DownloadFileOptions options = new DownloadFileOptions(int.MaxValue, 60);
DownloadFileOptions options = new DownloadFileOptions(int.MaxValue);
_downloadFileOp = _fileSystem.DownloadFileAsync(_bundle, options);
_downloadFileOp.StartOperation();
AddChildOperation(_downloadFileOp);

View File

@@ -85,7 +85,7 @@ namespace YooAsset
/// <summary>
/// 创建下载任务
/// </summary>
public UnityDownloadFileOperation DownloadFileAsync(PackageBundle bundle, string url, int timeout)
public UnityDownloadFileOperation DownloadFileAsync(PackageBundle bundle, string url)
{
// 查询旧的下载器
if (_downloaders.TryGetValue(bundle.BundleGUID, out var oldDownloader))
@@ -99,7 +99,7 @@ namespace YooAsset
bool isRequestLocalFile = DownloadSystemHelper.IsRequestLocalFile(url);
if (isRequestLocalFile)
{
newDownloader = new UnityDownloadLocalFileOperation(_fileSystem, bundle, url, timeout);
newDownloader = new UnityDownloadLocalFileOperation(_fileSystem, bundle, url);
AddChildOperation(newDownloader);
_downloaders.Add(bundle.BundleGUID, newDownloader);
}
@@ -107,13 +107,13 @@ namespace YooAsset
{
if (bundle.FileSize >= _fileSystem.ResumeDownloadMinimumSize)
{
newDownloader = new UnityDownloadResumeFileOperation(_fileSystem, bundle, url, timeout);
newDownloader = new UnityDownloadResumeFileOperation(_fileSystem, bundle, url);
AddChildOperation(newDownloader);
_downloaders.Add(bundle.BundleGUID, newDownloader);
}
else
{
newDownloader = new UnityDownloadNormalFileOperation(_fileSystem, bundle, url, timeout);
newDownloader = new UnityDownloadNormalFileOperation(_fileSystem, bundle, url);
AddChildOperation(newDownloader);
_downloaders.Add(bundle.BundleGUID, newDownloader);
}

View File

@@ -67,7 +67,7 @@ namespace YooAsset
}
string url = GetRequestURL();
_unityDownloadFileOp = _fileSystem.DownloadCenter.DownloadFileAsync(Bundle, url, _options.Timeout);
_unityDownloadFileOp = _fileSystem.DownloadCenter.DownloadFileAsync(Bundle, url);
_steps = ESteps.CheckRequest;
}

View File

@@ -22,7 +22,7 @@ namespace YooAsset
/// </summary>
public int RefCount { private set; get; }
internal UnityDownloadFileOperation(DefaultCacheFileSystem fileSystem, PackageBundle bundle, string url, int timeout) : base(url, timeout)
internal UnityDownloadFileOperation(DefaultCacheFileSystem fileSystem, PackageBundle bundle, string url) : base(url)
{
_fileSystem = fileSystem;
_bundle = bundle;

View File

@@ -9,8 +9,8 @@ namespace YooAsset
private VerifyTempFileOperation _verifyOperation;
private ESteps _steps = ESteps.None;
internal UnityDownloadLocalFileOperation(DefaultCacheFileSystem fileSystem, PackageBundle bundle, string url, int timeout = 60)
: base(fileSystem, bundle, url, timeout)
internal UnityDownloadLocalFileOperation(DefaultCacheFileSystem fileSystem, PackageBundle bundle, string url)
: base(fileSystem, bundle, url)
{
}
internal override void InternalStart()
@@ -32,7 +32,6 @@ namespace YooAsset
if (File.Exists(_tempFilePath))
File.Delete(_tempFilePath);
ResetTimeout();
CreateWebRequest();
_steps = ESteps.Download;
}
@@ -44,10 +43,7 @@ namespace YooAsset
DownloadedBytes = (long)_webRequest.downloadedBytes;
Progress = DownloadProgress;
if (_webRequest.isDone == false)
{
CheckRequestTimeout();
return;
}
// 检查网络错误
if (CheckRequestResult())
@@ -161,9 +157,9 @@ namespace YooAsset
private void CreateWebRequest()
{
_webRequest = DownloadSystemHelper.NewUnityWebRequestGet(_requestURL);
DownloadHandlerFile handler = new DownloadHandlerFile(_tempFilePath);
handler.removeFileOnAbort = true;
_webRequest = DownloadSystemHelper.NewUnityWebRequestGet(_requestURL);
_webRequest.downloadHandler = handler;
_webRequest.disposeDownloadHandlerOnDispose = true;
_webRequest.SendWebRequest();

View File

@@ -9,8 +9,8 @@ namespace YooAsset
private VerifyTempFileOperation _verifyOperation;
private ESteps _steps = ESteps.None;
internal UnityDownloadNormalFileOperation(DefaultCacheFileSystem fileSystem, PackageBundle bundle, string url, int timeout = 60)
: base(fileSystem, bundle, url, timeout)
internal UnityDownloadNormalFileOperation(DefaultCacheFileSystem fileSystem, PackageBundle bundle, string url)
: base(fileSystem, bundle, url)
{
}
internal override void InternalStart()
@@ -29,7 +29,6 @@ namespace YooAsset
if (File.Exists(_tempFilePath))
File.Delete(_tempFilePath);
ResetTimeout();
CreateWebRequest();
_steps = ESteps.Download;
}
@@ -41,10 +40,7 @@ namespace YooAsset
DownloadedBytes = (long)_webRequest.downloadedBytes;
Progress = DownloadProgress;
if (_webRequest.isDone == false)
{
CheckRequestTimeout();
return;
}
// 检查网络错误
if (CheckRequestResult())
@@ -115,9 +111,9 @@ namespace YooAsset
private void CreateWebRequest()
{
_webRequest = DownloadSystemHelper.NewUnityWebRequestGet(_requestURL);
DownloadHandlerFile handler = new DownloadHandlerFile(_tempFilePath);
handler.removeFileOnAbort = true;
_webRequest = DownloadSystemHelper.NewUnityWebRequestGet(_requestURL);
_webRequest.downloadHandler = handler;
_webRequest.disposeDownloadHandlerOnDispose = true;
_webRequest.SendWebRequest();

View File

@@ -10,8 +10,8 @@ namespace YooAsset
private long _fileOriginLength = 0;
private ESteps _steps = ESteps.None;
internal UnityDownloadResumeFileOperation(DefaultCacheFileSystem fileSystem, PackageBundle bundle, string url, int timeout = 60)
: base(fileSystem, bundle, url, timeout)
internal UnityDownloadResumeFileOperation(DefaultCacheFileSystem fileSystem, PackageBundle bundle, string url)
: base(fileSystem, bundle, url)
{
}
internal override void InternalStart()
@@ -46,7 +46,6 @@ namespace YooAsset
}
}
ResetTimeout();
CreateWebRequest(fileBeginLength);
_steps = ESteps.Download;
}
@@ -58,10 +57,7 @@ namespace YooAsset
DownloadedBytes = _fileOriginLength + (long)_webRequest.downloadedBytes;
Progress = DownloadProgress;
if (_webRequest.isDone == false)
{
CheckRequestTimeout();
return;
}
// 检查网络错误
if (CheckRequestResult())
@@ -147,9 +143,9 @@ namespace YooAsset
}
private void CreateWebRequest(long fileBeginLength)
{
_webRequest = DownloadSystemHelper.NewUnityWebRequestGet(_requestURL);
var handler = new DownloadHandlerFile(_tempFilePath, true);
handler.removeFileOnAbort = false;
_webRequest = DownloadSystemHelper.NewUnityWebRequestGet(_requestURL);
_webRequest.downloadHandler = handler;
_webRequest.disposeDownloadHandlerOnDispose = true;
if (fileBeginLength > 0)

View File

@@ -6,13 +6,13 @@ namespace YooAsset
private enum ESteps
{
None,
DownloadAssetBundle,
LoadWebAssetBundle,
Done,
}
private readonly DefaultWebRemoteFileSystem _fileSystem;
private readonly PackageBundle _bundle;
private DownloadAssetBundleOperation _downloadAssetBundleOp;
private LoadWebAssetBundleOperation _loadWebAssetBundleOp;
private ESteps _steps = ESteps.None;
@@ -23,51 +23,51 @@ namespace YooAsset
}
internal override void InternalStart()
{
_steps = ESteps.DownloadAssetBundle;
_steps = ESteps.LoadWebAssetBundle;
}
internal override void InternalUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.DownloadAssetBundle)
if (_steps == ESteps.LoadWebAssetBundle)
{
if (_downloadAssetBundleOp == null)
if (_loadWebAssetBundleOp == null)
{
string mainURL = _fileSystem.RemoteServices.GetRemoteMainURL(_bundle.FileName);
string fallbackURL = _fileSystem.RemoteServices.GetRemoteFallbackURL(_bundle.FileName);
DownloadFileOptions options = new DownloadFileOptions(int.MaxValue, 60);
DownloadFileOptions options = new DownloadFileOptions(int.MaxValue);
options.SetURL(mainURL, fallbackURL);
if (_bundle.Encrypted)
{
_downloadAssetBundleOp = new DownloadEncryptAssetBundleOperation(_bundle, options, true, _fileSystem.DecryptionServices);
_downloadAssetBundleOp.StartOperation();
AddChildOperation(_downloadAssetBundleOp);
_loadWebAssetBundleOp = new LoadWebEncryptAssetBundleOperation(_bundle, options, _fileSystem.DecryptionServices);
_loadWebAssetBundleOp.StartOperation();
AddChildOperation(_loadWebAssetBundleOp);
}
else
{
_downloadAssetBundleOp = new DownloadNormalAssetBundleOperation(_bundle, options, _fileSystem.DisableUnityWebCache);
_downloadAssetBundleOp.StartOperation();
AddChildOperation(_downloadAssetBundleOp);
_loadWebAssetBundleOp = new LoadWebNormalAssetBundleOperation(_bundle, options, _fileSystem.DisableUnityWebCache);
_loadWebAssetBundleOp.StartOperation();
AddChildOperation(_loadWebAssetBundleOp);
}
}
_downloadAssetBundleOp.UpdateOperation();
DownloadProgress = _downloadAssetBundleOp.DownloadProgress;
DownloadedBytes = _downloadAssetBundleOp.DownloadedBytes;
Progress = _downloadAssetBundleOp.Progress;
if (_downloadAssetBundleOp.IsDone == false)
_loadWebAssetBundleOp.UpdateOperation();
DownloadProgress = _loadWebAssetBundleOp.DownloadProgress;
DownloadedBytes = _loadWebAssetBundleOp.DownloadedBytes;
Progress = _loadWebAssetBundleOp.Progress;
if (_loadWebAssetBundleOp.IsDone == false)
return;
if (_downloadAssetBundleOp.Status == EOperationStatus.Succeed)
if (_loadWebAssetBundleOp.Status == EOperationStatus.Succeed)
{
var assetBundle = _downloadAssetBundleOp.Result;
var assetBundle = _loadWebAssetBundleOp.Result;
if (assetBundle == null)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"{nameof(DownloadAssetBundleOperation)} loaded asset bundle is null !";
Error = $"{nameof(LoadWebAssetBundleOperation)} loaded asset bundle is null !";
}
else
{
@@ -80,7 +80,7 @@ namespace YooAsset
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _downloadAssetBundleOp.Error;
Error = _loadWebAssetBundleOp.Error;
}
}
}

View File

@@ -64,7 +64,7 @@ namespace YooAsset
if (_loadWebPackageManifestOp == null)
{
string packageHash = _requestWebPackageHashOp.PackageHash;
_loadWebPackageManifestOp = new LoadWebRemotePackageManifestOperation(_fileSystem, _packageVersion, packageHash);
_loadWebPackageManifestOp = new LoadWebRemotePackageManifestOperation(_fileSystem, _packageVersion, packageHash, _timeout);
_loadWebPackageManifestOp.StartOperation();
AddChildOperation(_loadWebPackageManifestOp);
}

View File

@@ -15,6 +15,7 @@ namespace YooAsset
private readonly DefaultWebRemoteFileSystem _fileSystem;
private readonly string _packageVersion;
private readonly string _packageHash;
private readonly int _timeout;
private UnityWebDataRequestOperation _webDataRequestOp;
private DeserializeManifestOperation _deserializer;
private int _requestCount = 0;
@@ -26,11 +27,12 @@ namespace YooAsset
public PackageManifest Manifest { private set; get; }
internal LoadWebRemotePackageManifestOperation(DefaultWebRemoteFileSystem fileSystem, string packageVersion, string packageHash)
internal LoadWebRemotePackageManifestOperation(DefaultWebRemoteFileSystem fileSystem, string packageVersion, string packageHash, int timeout)
{
_fileSystem = fileSystem;
_packageVersion = packageVersion;
_packageHash = packageHash;
_timeout = timeout;
}
internal override void InternalStart()
{
@@ -48,7 +50,7 @@ namespace YooAsset
{
string fileName = YooAssetSettingsData.GetManifestBinaryFileName(_fileSystem.PackageName, _packageVersion);
string url = GetWebRequestURL(fileName);
_webDataRequestOp = new UnityWebDataRequestOperation(url);
_webDataRequestOp = new UnityWebDataRequestOperation(url, _timeout);
_webDataRequestOp.StartOperation();
AddChildOperation(_webDataRequestOp);
}

View File

@@ -48,7 +48,7 @@ namespace YooAsset
*/
#endif
_loadCatalogFileOp = new LoadWebServerCatalogFileOperation(_fileSystem);
_loadCatalogFileOp = new LoadWebServerCatalogFileOperation(_fileSystem, 60);
_loadCatalogFileOp.StartOperation();
AddChildOperation(_loadCatalogFileOp);
}

View File

@@ -6,13 +6,13 @@ namespace YooAsset
private enum ESteps
{
None,
DownloadAssetBundle,
LoadWebAssetBundle,
Done,
}
private readonly DefaultWebServerFileSystem _fileSystem;
private readonly PackageBundle _bundle;
private DownloadAssetBundleOperation _downloadAssetBundleOp;
private LoadWebAssetBundleOperation _loadWebAssetBundleOp;
private ESteps _steps = ESteps.None;
@@ -23,51 +23,51 @@ namespace YooAsset
}
internal override void InternalStart()
{
_steps = ESteps.DownloadAssetBundle;
_steps = ESteps.LoadWebAssetBundle;
}
internal override void InternalUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.DownloadAssetBundle)
if (_steps == ESteps.LoadWebAssetBundle)
{
if (_downloadAssetBundleOp == null)
if (_loadWebAssetBundleOp == null)
{
string fileLoadPath = _fileSystem.GetWebFileLoadPath(_bundle);
string mainURL = DownloadSystemHelper.ConvertToWWWPath(fileLoadPath);
DownloadFileOptions options = new DownloadFileOptions(int.MaxValue, 60);
DownloadFileOptions options = new DownloadFileOptions(int.MaxValue);
options.SetURL(mainURL, mainURL);
if (_bundle.Encrypted)
{
_downloadAssetBundleOp = new DownloadEncryptAssetBundleOperation(_bundle, options, true, _fileSystem.DecryptionServices);
_downloadAssetBundleOp.StartOperation();
AddChildOperation(_downloadAssetBundleOp);
_loadWebAssetBundleOp = new LoadWebEncryptAssetBundleOperation(_bundle, options, _fileSystem.DecryptionServices);
_loadWebAssetBundleOp.StartOperation();
AddChildOperation(_loadWebAssetBundleOp);
}
else
{
_downloadAssetBundleOp = new DownloadNormalAssetBundleOperation(_bundle, options, _fileSystem.DisableUnityWebCache);
_downloadAssetBundleOp.StartOperation();
AddChildOperation(_downloadAssetBundleOp);
_loadWebAssetBundleOp = new LoadWebNormalAssetBundleOperation(_bundle, options, _fileSystem.DisableUnityWebCache);
_loadWebAssetBundleOp.StartOperation();
AddChildOperation(_loadWebAssetBundleOp);
}
}
_downloadAssetBundleOp.UpdateOperation();
DownloadProgress = _downloadAssetBundleOp.DownloadProgress;
DownloadedBytes = _downloadAssetBundleOp.DownloadedBytes;
Progress = _downloadAssetBundleOp.Progress;
if (_downloadAssetBundleOp.IsDone == false)
_loadWebAssetBundleOp.UpdateOperation();
DownloadProgress = _loadWebAssetBundleOp.DownloadProgress;
DownloadedBytes = _loadWebAssetBundleOp.DownloadedBytes;
Progress = _loadWebAssetBundleOp.Progress;
if (_loadWebAssetBundleOp.IsDone == false)
return;
if (_downloadAssetBundleOp.Status == EOperationStatus.Succeed)
if (_loadWebAssetBundleOp.Status == EOperationStatus.Succeed)
{
var assetBundle = _downloadAssetBundleOp.Result;
var assetBundle = _loadWebAssetBundleOp.Result;
if (assetBundle == null)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"{nameof(DownloadAssetBundleOperation)} loaded asset bundle is null !";
Error = $"{nameof(LoadWebAssetBundleOperation)} loaded asset bundle is null !";
}
else
{
@@ -80,7 +80,7 @@ namespace YooAsset
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _downloadAssetBundleOp.Error;
Error = _loadWebAssetBundleOp.Error;
}
}
}

View File

@@ -64,7 +64,7 @@ namespace YooAsset
if (_loadWebPackageManifestOp == null)
{
string packageHash = _requestWebPackageHashOp.PackageHash;
_loadWebPackageManifestOp = new LoadWebServerPackageManifestOperation(_fileSystem, _packageVersion, packageHash);
_loadWebPackageManifestOp = new LoadWebServerPackageManifestOperation(_fileSystem, _packageVersion, packageHash, _timeout);
_loadWebPackageManifestOp.StartOperation();
AddChildOperation(_loadWebPackageManifestOp);
}

View File

@@ -16,12 +16,14 @@ namespace YooAsset
}
private readonly DefaultWebServerFileSystem _fileSystem;
private readonly int _timeout;
private UnityWebDataRequestOperation _webDataRequestOp;
private ESteps _steps = ESteps.None;
internal LoadWebServerCatalogFileOperation(DefaultWebServerFileSystem fileSystem)
internal LoadWebServerCatalogFileOperation(DefaultWebServerFileSystem fileSystem, int timeout)
{
_fileSystem = fileSystem;
_timeout = timeout;
}
internal override void InternalStart()
{
@@ -38,7 +40,7 @@ namespace YooAsset
{
string filePath = _fileSystem.GetCatalogBinaryFileLoadPath();
string url = DownloadSystemHelper.ConvertToWWWPath(filePath);
_webDataRequestOp = new UnityWebDataRequestOperation(url);
_webDataRequestOp = new UnityWebDataRequestOperation(url, _timeout);
_webDataRequestOp.StartOperation();
AddChildOperation(_webDataRequestOp);
}

View File

@@ -15,6 +15,7 @@ namespace YooAsset
private readonly DefaultWebServerFileSystem _fileSystem;
private readonly string _packageVersion;
private readonly string _packageHash;
private readonly int _timeout;
private UnityWebDataRequestOperation _webDataRequestOp;
private DeserializeManifestOperation _deserializer;
private ESteps _steps = ESteps.None;
@@ -25,11 +26,12 @@ namespace YooAsset
public PackageManifest Manifest { private set; get; }
internal LoadWebServerPackageManifestOperation(DefaultWebServerFileSystem fileSystem, string packageVersion, string packageHash)
internal LoadWebServerPackageManifestOperation(DefaultWebServerFileSystem fileSystem, string packageVersion, string packageHash, int timeout)
{
_fileSystem = fileSystem;
_packageVersion = packageVersion;
_packageHash = packageHash;
_timeout = timeout;
}
internal override void InternalStart()
{
@@ -46,7 +48,7 @@ namespace YooAsset
{
string filePath = _fileSystem.GetWebPackageManifestFilePath(_packageVersion);
string url = DownloadSystemHelper.ConvertToWWWPath(filePath);
_webDataRequestOp = new UnityWebDataRequestOperation(url);
_webDataRequestOp = new UnityWebDataRequestOperation(url, _timeout);
_webDataRequestOp.StartOperation();
AddChildOperation(_webDataRequestOp);
}

View File

@@ -8,11 +8,6 @@ namespace YooAsset
/// </summary>
public readonly int FailedTryAgain;
/// <summary>
/// 超时时间
/// </summary>
public readonly int Timeout;
/// <summary>
/// 主资源地址
/// </summary>
@@ -28,10 +23,9 @@ namespace YooAsset
/// </summary>
public string ImportFilePath { set; get; }
public DownloadFileOptions(int failedTryAgain, int timeout)
public DownloadFileOptions(int failedTryAgain)
{
FailedTryAgain = failedTryAgain;
Timeout = timeout;
}
/// <summary>

View File

@@ -2,7 +2,7 @@
namespace YooAsset
{
internal abstract class DownloadAssetBundleOperation : AsyncOperationBase
internal abstract class LoadWebAssetBundleOperation : AsyncOperationBase
{
/// <summary>
/// AssetBundle对象

View File

@@ -2,7 +2,7 @@
namespace YooAsset
{
internal class DownloadEncryptAssetBundleOperation : DownloadAssetBundleOperation
internal class LoadWebEncryptAssetBundleOperation : LoadWebAssetBundleOperation
{
protected enum ESteps
{
@@ -16,7 +16,6 @@ namespace YooAsset
private UnityWebDataRequestOperation _unityWebDataRequestOp;
private readonly PackageBundle _bundle;
private readonly DownloadFileOptions _options;
private readonly bool _checkTimeout;
private readonly IWebDecryptionServices _decryptionServices;
protected int _requestCount = 0;
@@ -24,11 +23,10 @@ namespace YooAsset
protected int _failedTryAgain;
private ESteps _steps = ESteps.None;
internal DownloadEncryptAssetBundleOperation(PackageBundle bundle, DownloadFileOptions options, bool checkTimeout, IWebDecryptionServices decryptionServices)
internal LoadWebEncryptAssetBundleOperation(PackageBundle bundle, DownloadFileOptions options, IWebDecryptionServices decryptionServices)
{
_bundle = bundle;
_options = options;
_checkTimeout = checkTimeout;
_decryptionServices = decryptionServices;
}
internal override void InternalStart()
@@ -53,10 +51,8 @@ namespace YooAsset
}
string url = GetRequestURL();
_unityWebDataRequestOp = new UnityWebDataRequestOperation(url, _options.Timeout);
_unityWebDataRequestOp = new UnityWebDataRequestOperation(url, 0);
_unityWebDataRequestOp.StartOperation();
if (_checkTimeout == false)
_unityWebDataRequestOp.DontCheckTimeout();
_steps = ESteps.CheckRequest;
}

View File

@@ -2,7 +2,7 @@
namespace YooAsset
{
internal class DownloadNormalAssetBundleOperation : DownloadAssetBundleOperation
internal class LoadWebNormalAssetBundleOperation : LoadWebAssetBundleOperation
{
protected enum ESteps
{
@@ -13,18 +13,18 @@ namespace YooAsset
Done,
}
private UnityAssetBundleRequestOperation _unityAssetBundleRequestOp;
private readonly PackageBundle _bundle;
private readonly DownloadFileOptions _options;
private readonly bool _disableUnityWebCache;
private UnityAssetBundleRequestOperation _unityAssetBundleRequestOp;
protected int _requestCount = 0;
protected float _tryAgainTimer;
protected int _failedTryAgain;
private ESteps _steps = ESteps.None;
internal DownloadNormalAssetBundleOperation(PackageBundle bundle, DownloadFileOptions options, bool disableUnityWebCache)
internal LoadWebNormalAssetBundleOperation(PackageBundle bundle, DownloadFileOptions options, bool disableUnityWebCache)
{
_bundle = bundle;
_options = options;
@@ -43,7 +43,7 @@ namespace YooAsset
if (_steps == ESteps.CreateRequest)
{
string url = GetRequestURL();
_unityAssetBundleRequestOp = new UnityAssetBundleRequestOperation(_bundle, _disableUnityWebCache, url, _options.Timeout);
_unityAssetBundleRequestOp = new UnityAssetBundleRequestOperation(_bundle, _disableUnityWebCache, url);
_unityAssetBundleRequestOp.StartOperation();
_steps = ESteps.CheckRequest;
}

View File

@@ -3,7 +3,7 @@ using System.Reflection;
namespace YooAsset
{
public static class PakcageInvokeBuilder
public static class PackageInvokeBuilder
{
/// <summary>
/// 调用Editro类来执行构建资源包任务
@@ -32,7 +32,7 @@ namespace YooAsset
#else
namespace YooAsset
{
public static class PakcageInvokeBuilder
public static class PackageInvokeBuilder
{
public static PackageInvokeBuildResult InvokeBuilder(PackageInvokeBuildParam buildParam)
{

View File

@@ -36,9 +36,9 @@ namespace YooAsset
/// <summary>
/// 创建下载器
/// </summary>
public FSDownloadFileOperation CreateDownloader(int failedTryAgain, int timeout)
public FSDownloadFileOperation CreateDownloader(int failedTryAgain)
{
DownloadFileOptions options = new DownloadFileOptions(failedTryAgain, timeout);
DownloadFileOptions options = new DownloadFileOptions(failedTryAgain);
options.ImportFilePath = _importFilePath;
return _fileSystem.DownloadFileAsync(Bundle, options);
}

View File

@@ -34,15 +34,16 @@ namespace YooAsset
ClearCacheFilesOperation ClearCacheFilesAsync(ClearCacheFilesOptions options);
// 下载相关
ResourceDownloaderOperation CreateResourceDownloaderByAll(int downloadingMaxNumber, int failedTryAgain, int timeout);
ResourceDownloaderOperation CreateResourceDownloaderByTags(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout);
ResourceDownloaderOperation CreateResourceDownloaderByPaths(AssetInfo[] assetInfos, bool recursiveDownload, int downloadingMaxNumber, int failedTryAgain, int timeout);
ResourceDownloaderOperation CreateResourceDownloaderByAll(int downloadingMaxNumber, int failedTryAgain);
ResourceDownloaderOperation CreateResourceDownloaderByTags(string[] tags, int downloadingMaxNumber, int failedTryAgain);
ResourceDownloaderOperation CreateResourceDownloaderByPaths(AssetInfo[] assetInfos, bool recursiveDownload, int downloadingMaxNumber, int failedTryAgain);
// 解压相关
ResourceUnpackerOperation CreateResourceUnpackerByAll(int upackingMaxNumber, int failedTryAgain, int timeout);
ResourceUnpackerOperation CreateResourceUnpackerByTags(string[] tags, int upackingMaxNumber, int failedTryAgain, int timeout);
ResourceUnpackerOperation CreateResourceUnpackerByAll(int upackingMaxNumber, int failedTryAgain);
ResourceUnpackerOperation CreateResourceUnpackerByTags(string[] tags, int upackingMaxNumber, int failedTryAgain);
// 导入相关
ResourceImporterOperation CreateResourceImporterByFilePaths(string[] filePaths, int importerMaxNumber, int failedTryAgain, int timeout);
ResourceImporterOperation CreateResourceImporterByFilePaths(string[] filePaths, int importingMaxNumber, int failedTryAgain);
ResourceImporterOperation CreateResourceImporterByFileInfos(ImportFileInfo[] fileInfos, int importingMaxNumber, int failedTryAgain);
}
}

View File

@@ -40,7 +40,6 @@ namespace YooAsset
private readonly string _packageName;
private readonly int _downloadingMaxNumber;
private readonly int _failedTryAgain;
private readonly int _timeout;
private readonly List<BundleInfo> _bundleInfoList;
private readonly List<FSDownloadFileOperation> _downloaders = new List<FSDownloadFileOperation>(MAX_LOADER_COUNT);
private readonly List<FSDownloadFileOperation> _removeList = new List<FSDownloadFileOperation>(MAX_LOADER_COUNT);
@@ -102,13 +101,12 @@ namespace YooAsset
public DownloadFileBegin DownloadFileBeginCallback { set; get; }
internal DownloaderOperation(string packageName, List<BundleInfo> downloadList, int downloadingMaxNumber, int failedTryAgain, int timeout)
internal DownloaderOperation(string packageName, List<BundleInfo> downloadList, int downloadingMaxNumber, int failedTryAgain)
{
_packageName = packageName;
_bundleInfoList = downloadList;
_downloadingMaxNumber = UnityEngine.Mathf.Clamp(downloadingMaxNumber, 1, MAX_LOADER_COUNT); ;
_failedTryAgain = failedTryAgain;
_timeout = timeout;
// 设置包裹名称 (fix #210)
SetPackageName(packageName);
@@ -203,7 +201,7 @@ namespace YooAsset
{
int index = _bundleInfoList.Count - 1;
var bundleInfo = _bundleInfoList[index];
var downloader = bundleInfo.CreateDownloader(_failedTryAgain, _timeout);
var downloader = bundleInfo.CreateDownloader(_failedTryAgain);
downloader.StartOperation();
this.AddChildOperation(downloader);
@@ -374,52 +372,52 @@ namespace YooAsset
public sealed class ResourceDownloaderOperation : DownloaderOperation
{
internal ResourceDownloaderOperation(string packageName, List<BundleInfo> downloadList, int downloadingMaxNumber, int failedTryAgain, int timeout)
: base(packageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout)
internal ResourceDownloaderOperation(string packageName, List<BundleInfo> downloadList, int downloadingMaxNumber, int failedTryAgain)
: base(packageName, downloadList, downloadingMaxNumber, failedTryAgain)
{
}
/// <summary>
/// 创建空的下载器
/// </summary>
internal static ResourceDownloaderOperation CreateEmptyDownloader(string packageName, int downloadingMaxNumber, int failedTryAgain, int timeout)
internal static ResourceDownloaderOperation CreateEmptyDownloader(string packageName, int downloadingMaxNumber, int failedTryAgain)
{
List<BundleInfo> downloadList = new List<BundleInfo>();
var operation = new ResourceDownloaderOperation(packageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout);
var operation = new ResourceDownloaderOperation(packageName, downloadList, downloadingMaxNumber, failedTryAgain);
return operation;
}
}
public sealed class ResourceUnpackerOperation : DownloaderOperation
{
internal ResourceUnpackerOperation(string packageName, List<BundleInfo> downloadList, int downloadingMaxNumber, int failedTryAgain, int timeout)
: base(packageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout)
internal ResourceUnpackerOperation(string packageName, List<BundleInfo> downloadList, int downloadingMaxNumber, int failedTryAgain)
: base(packageName, downloadList, downloadingMaxNumber, failedTryAgain)
{
}
/// <summary>
/// 创建空的解压器
/// </summary>
internal static ResourceUnpackerOperation CreateEmptyUnpacker(string packageName, int upackingMaxNumber, int failedTryAgain, int timeout)
internal static ResourceUnpackerOperation CreateEmptyUnpacker(string packageName, int upackingMaxNumber, int failedTryAgain)
{
List<BundleInfo> downloadList = new List<BundleInfo>();
var operation = new ResourceUnpackerOperation(packageName, downloadList, upackingMaxNumber, failedTryAgain, int.MaxValue);
var operation = new ResourceUnpackerOperation(packageName, downloadList, upackingMaxNumber, failedTryAgain);
return operation;
}
}
public sealed class ResourceImporterOperation : DownloaderOperation
{
internal ResourceImporterOperation(string packageName, List<BundleInfo> downloadList, int downloadingMaxNumber, int failedTryAgain, int timeout)
: base(packageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout)
internal ResourceImporterOperation(string packageName, List<BundleInfo> downloadList, int downloadingMaxNumber, int failedTryAgain)
: base(packageName, downloadList, downloadingMaxNumber, failedTryAgain)
{
}
/// <summary>
/// 创建空的导入器
/// </summary>
internal static ResourceImporterOperation CreateEmptyImporter(string packageName, int upackingMaxNumber, int failedTryAgain, int timeout)
internal static ResourceImporterOperation CreateEmptyImporter(string packageName, int upackingMaxNumber, int failedTryAgain)
{
List<BundleInfo> downloadList = new List<BundleInfo>();
var operation = new ResourceImporterOperation(packageName, downloadList, upackingMaxNumber, failedTryAgain, int.MaxValue);
var operation = new ResourceImporterOperation(packageName, downloadList, upackingMaxNumber, failedTryAgain);
return operation;
}
}

View File

@@ -107,11 +107,11 @@ namespace YooAsset
if (Status != EOperationStatus.Succeed)
{
YooLogger.Warning($"{nameof(PreDownloadContentOperation)} status is not succeed !");
return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout);
return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.PackageName, downloadingMaxNumber, failedTryAgain);
}
List<BundleInfo> downloadList = _impl.GetDownloadListByAll(_manifest);
var operation = new ResourceDownloaderOperation(_impl.PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout);
var operation = new ResourceDownloaderOperation(_impl.PackageName, downloadList, downloadingMaxNumber, failedTryAgain);
return operation;
}
@@ -127,11 +127,11 @@ namespace YooAsset
if (Status != EOperationStatus.Succeed)
{
YooLogger.Warning($"{nameof(PreDownloadContentOperation)} status is not succeed !");
return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout);
return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.PackageName, downloadingMaxNumber, failedTryAgain);
}
List<BundleInfo> downloadList = _impl.GetDownloadListByTags(_manifest, new string[] { tag });
var operation = new ResourceDownloaderOperation(_impl.PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout);
var operation = new ResourceDownloaderOperation(_impl.PackageName, downloadList, downloadingMaxNumber, failedTryAgain);
return operation;
}
@@ -147,11 +147,11 @@ namespace YooAsset
if (Status != EOperationStatus.Succeed)
{
YooLogger.Warning($"{nameof(PreDownloadContentOperation)} status is not succeed !");
return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout);
return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.PackageName, downloadingMaxNumber, failedTryAgain);
}
List<BundleInfo> downloadList = _impl.GetDownloadListByTags(_manifest, tags);
var operation = new ResourceDownloaderOperation(_impl.PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout);
var operation = new ResourceDownloaderOperation(_impl.PackageName, downloadList, downloadingMaxNumber, failedTryAgain);
return operation;
}
@@ -167,7 +167,7 @@ namespace YooAsset
if (Status != EOperationStatus.Succeed)
{
YooLogger.Warning($"{nameof(PreDownloadContentOperation)} status is not succeed !");
return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout);
return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.PackageName, downloadingMaxNumber, failedTryAgain);
}
List<AssetInfo> assetInfos = new List<AssetInfo>();
@@ -175,7 +175,7 @@ namespace YooAsset
assetInfos.Add(assetInfo);
List<BundleInfo> downloadList = _impl.GetDownloadListByPaths(_manifest, assetInfos.ToArray(), recursiveDownload);
var operation = new ResourceDownloaderOperation(_impl.PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout);
var operation = new ResourceDownloaderOperation(_impl.PackageName, downloadList, downloadingMaxNumber, failedTryAgain);
return operation;
}
@@ -191,7 +191,7 @@ namespace YooAsset
if (Status != EOperationStatus.Succeed)
{
YooLogger.Warning($"{nameof(PreDownloadContentOperation)} status is not succeed !");
return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout);
return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.PackageName, downloadingMaxNumber, failedTryAgain);
}
List<AssetInfo> assetInfos = new List<AssetInfo>(locations.Length);
@@ -202,7 +202,7 @@ namespace YooAsset
}
List<BundleInfo> downloadList = _impl.GetDownloadListByPaths(_manifest, assetInfos.ToArray(), recursiveDownload);
var operation = new ResourceDownloaderOperation(_impl.PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout);
var operation = new ResourceDownloaderOperation(_impl.PackageName, downloadList, downloadingMaxNumber, failedTryAgain);
return operation;
}
}

View File

@@ -10,7 +10,7 @@ namespace YooAsset
buildParam.InvokeAssmeblyName = "YooAsset.Editor";
buildParam.InvokeClassFullName = "YooAsset.Editor.AssetBundleSimulateBuilder";
buildParam.InvokeMethodName = "SimulateBuild";
return PakcageInvokeBuilder.InvokeBuilder(buildParam);
return PackageInvokeBuilder.InvokeBuilder(buildParam);
}
}
}

View File

@@ -104,44 +104,50 @@ namespace YooAsset
}
// 下载相关
ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByAll(int downloadingMaxNumber, int failedTryAgain, int timeout)
ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByAll(int downloadingMaxNumber, int failedTryAgain)
{
List<BundleInfo> downloadList = GetDownloadListByAll(ActiveManifest);
var operation = new ResourceDownloaderOperation(PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout);
var operation = new ResourceDownloaderOperation(PackageName, downloadList, downloadingMaxNumber, failedTryAgain);
return operation;
}
ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByTags(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout)
ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByTags(string[] tags, int downloadingMaxNumber, int failedTryAgain)
{
List<BundleInfo> downloadList = GetDownloadListByTags(ActiveManifest, tags);
var operation = new ResourceDownloaderOperation(PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout);
var operation = new ResourceDownloaderOperation(PackageName, downloadList, downloadingMaxNumber, failedTryAgain);
return operation;
}
ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByPaths(AssetInfo[] assetInfos, bool recursiveDownload, int downloadingMaxNumber, int failedTryAgain, int timeout)
ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByPaths(AssetInfo[] assetInfos, bool recursiveDownload, int downloadingMaxNumber, int failedTryAgain)
{
List<BundleInfo> downloadList = GetDownloadListByPaths(ActiveManifest, assetInfos, recursiveDownload);
var operation = new ResourceDownloaderOperation(PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout);
var operation = new ResourceDownloaderOperation(PackageName, downloadList, downloadingMaxNumber, failedTryAgain);
return operation;
}
// 解压相关
ResourceUnpackerOperation IPlayMode.CreateResourceUnpackerByAll(int upackingMaxNumber, int failedTryAgain, int timeout)
ResourceUnpackerOperation IPlayMode.CreateResourceUnpackerByAll(int upackingMaxNumber, int failedTryAgain)
{
List<BundleInfo> unpcakList = GetUnpackListByAll(ActiveManifest);
var operation = new ResourceUnpackerOperation(PackageName, unpcakList, upackingMaxNumber, failedTryAgain, timeout);
var operation = new ResourceUnpackerOperation(PackageName, unpcakList, upackingMaxNumber, failedTryAgain);
return operation;
}
ResourceUnpackerOperation IPlayMode.CreateResourceUnpackerByTags(string[] tags, int upackingMaxNumber, int failedTryAgain, int timeout)
ResourceUnpackerOperation IPlayMode.CreateResourceUnpackerByTags(string[] tags, int upackingMaxNumber, int failedTryAgain)
{
List<BundleInfo> unpcakList = GetUnpackListByTags(ActiveManifest, tags);
var operation = new ResourceUnpackerOperation(PackageName, unpcakList, upackingMaxNumber, failedTryAgain, timeout);
var operation = new ResourceUnpackerOperation(PackageName, unpcakList, upackingMaxNumber, failedTryAgain);
return operation;
}
// 导入相关
ResourceImporterOperation IPlayMode.CreateResourceImporterByFilePaths(string[] filePaths, int importerMaxNumber, int failedTryAgain, int timeout)
ResourceImporterOperation IPlayMode.CreateResourceImporterByFilePaths(string[] filePaths, int importingMaxNumber, int failedTryAgain)
{
List<BundleInfo> importerList = GetImporterListByFilePaths(ActiveManifest, filePaths);
var operation = new ResourceImporterOperation(PackageName, importerList, importerMaxNumber, failedTryAgain, timeout);
var operation = new ResourceImporterOperation(PackageName, importerList, importingMaxNumber, failedTryAgain);
return operation;
}
ResourceImporterOperation IPlayMode.CreateResourceImporterByFileInfos(ImportFileInfo[] fileInfos, int importingMaxNumber, int failedTryAgain)
{
List<BundleInfo> importerList = GetImporterListByFileInfos(ActiveManifest, fileInfos);
var operation = new ResourceImporterOperation(PackageName, importerList, importingMaxNumber, failedTryAgain);
return operation;
}
#endregion
@@ -419,11 +425,56 @@ namespace YooAsset
if (manifest == null)
return new List<BundleInfo>();
List<BundleInfo> result = new List<BundleInfo>();
foreach (var filePath in filePaths)
ImportFileInfo[] fileInfos = new ImportFileInfo[filePaths.Length];
for (int i = 0; i < filePaths.Length; i++)
{
string fileName = System.IO.Path.GetFileName(filePath);
if (manifest.TryGetPackageBundleByFileName(fileName, out PackageBundle packageBundle))
ImportFileInfo fileInfo = new ImportFileInfo();
fileInfo.FilePath = filePaths[i];
fileInfos[i] = fileInfo;
}
return GetImporterListByFileInfos(manifest, fileInfos);
}
public List<BundleInfo> GetImporterListByFileInfos(PackageManifest manifest, ImportFileInfo[] fileInfos)
{
if (manifest == null)
return new List<BundleInfo>();
List<BundleInfo> result = new List<BundleInfo>();
foreach (var fileInfo in fileInfos)
{
string filePath = fileInfo.FilePath;
if (string.IsNullOrEmpty(filePath))
continue;
PackageBundle packageBundle = null;
if (string.IsNullOrEmpty(fileInfo.BundleName) == false)
{
if (manifest.TryGetPackageBundleByBundleName(fileInfo.BundleName, out packageBundle) == false)
{
YooLogger.Warning($"Not found package bundle, bundle name : {fileInfo.BundleName}");
continue;
}
}
else if (string.IsNullOrEmpty(fileInfo.BundleGUID) == false)
{
if (manifest.TryGetPackageBundleByBundleGUID(fileInfo.BundleGUID, out packageBundle) == false)
{
YooLogger.Warning($"Not found package bundle, bundle guid : {fileInfo.BundleGUID}");
continue;
}
}
else
{
string fileName = System.IO.Path.GetFileName(filePath);
if (manifest.TryGetPackageBundleByFileName(fileName, out packageBundle) == false)
{
YooLogger.Warning($"Not found package bundle, file name : {fileName}");
continue;
}
}
if (packageBundle != null)
{
var fileSystem = GetBelongFileSystem(packageBundle);
if (fileSystem == null)
@@ -435,10 +486,6 @@ namespace YooAsset
result.Add(bundleInfo);
}
}
else
{
YooLogger.Warning($"Not found package bundle, importer file path : {filePath}");
}
}
return result;
}

View File

@@ -969,7 +969,7 @@ namespace YooAsset
public ResourceDownloaderOperation CreateResourceDownloader(int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
{
DebugCheckInitialize();
return _playModeImpl.CreateResourceDownloaderByAll(downloadingMaxNumber, failedTryAgain, timeout);
return _playModeImpl.CreateResourceDownloaderByAll(downloadingMaxNumber, failedTryAgain);
}
/// <summary>
@@ -982,7 +982,7 @@ namespace YooAsset
public ResourceDownloaderOperation CreateResourceDownloader(string tag, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
{
DebugCheckInitialize();
return _playModeImpl.CreateResourceDownloaderByTags(new string[] { tag }, downloadingMaxNumber, failedTryAgain, timeout);
return _playModeImpl.CreateResourceDownloaderByTags(new string[] { tag }, downloadingMaxNumber, failedTryAgain);
}
/// <summary>
@@ -995,7 +995,7 @@ namespace YooAsset
public ResourceDownloaderOperation CreateResourceDownloader(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
{
DebugCheckInitialize();
return _playModeImpl.CreateResourceDownloaderByTags(tags, downloadingMaxNumber, failedTryAgain, timeout);
return _playModeImpl.CreateResourceDownloaderByTags(tags, downloadingMaxNumber, failedTryAgain);
}
/// <summary>
@@ -1011,11 +1011,11 @@ namespace YooAsset
DebugCheckInitialize();
var assetInfo = ConvertLocationToAssetInfo(location, null);
AssetInfo[] assetInfos = new AssetInfo[] { assetInfo };
return _playModeImpl.CreateResourceDownloaderByPaths(assetInfos, recursiveDownload, downloadingMaxNumber, failedTryAgain, timeout);
return _playModeImpl.CreateResourceDownloaderByPaths(assetInfos, recursiveDownload, downloadingMaxNumber, failedTryAgain);
}
public ResourceDownloaderOperation CreateBundleDownloader(string location, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
{
return CreateBundleDownloader(location, false, downloadingMaxNumber, failedTryAgain, timeout);
return CreateBundleDownloader(location, false, downloadingMaxNumber, failedTryAgain);
}
/// <summary>
@@ -1035,11 +1035,11 @@ namespace YooAsset
var assetInfo = ConvertLocationToAssetInfo(location, null);
assetInfos.Add(assetInfo);
}
return _playModeImpl.CreateResourceDownloaderByPaths(assetInfos.ToArray(), recursiveDownload, downloadingMaxNumber, failedTryAgain, timeout);
return _playModeImpl.CreateResourceDownloaderByPaths(assetInfos.ToArray(), recursiveDownload, downloadingMaxNumber, failedTryAgain);
}
public ResourceDownloaderOperation CreateBundleDownloader(string[] locations, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
{
return CreateBundleDownloader(locations, false, downloadingMaxNumber, failedTryAgain, timeout);
return CreateBundleDownloader(locations, false, downloadingMaxNumber, failedTryAgain);
}
/// <summary>
@@ -1054,11 +1054,11 @@ namespace YooAsset
{
DebugCheckInitialize();
AssetInfo[] assetInfos = new AssetInfo[] { assetInfo };
return _playModeImpl.CreateResourceDownloaderByPaths(assetInfos, recursiveDownload, downloadingMaxNumber, failedTryAgain, timeout);
return _playModeImpl.CreateResourceDownloaderByPaths(assetInfos, recursiveDownload, downloadingMaxNumber, failedTryAgain);
}
public ResourceDownloaderOperation CreateBundleDownloader(AssetInfo assetInfo, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
{
return CreateBundleDownloader(assetInfo, false, downloadingMaxNumber, failedTryAgain, timeout);
return CreateBundleDownloader(assetInfo, false, downloadingMaxNumber, failedTryAgain);
}
/// <summary>
@@ -1072,11 +1072,11 @@ namespace YooAsset
public ResourceDownloaderOperation CreateBundleDownloader(AssetInfo[] assetInfos, bool recursiveDownload, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
{
DebugCheckInitialize();
return _playModeImpl.CreateResourceDownloaderByPaths(assetInfos, recursiveDownload, downloadingMaxNumber, failedTryAgain, timeout);
return _playModeImpl.CreateResourceDownloaderByPaths(assetInfos, recursiveDownload, downloadingMaxNumber, failedTryAgain);
}
public ResourceDownloaderOperation CreateBundleDownloader(AssetInfo[] assetInfos, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
{
return CreateBundleDownloader(assetInfos, false, downloadingMaxNumber, failedTryAgain, timeout);
return CreateBundleDownloader(assetInfos, false, downloadingMaxNumber, failedTryAgain);
}
#endregion
@@ -1089,7 +1089,7 @@ namespace YooAsset
public ResourceUnpackerOperation CreateResourceUnpacker(int unpackingMaxNumber, int failedTryAgain)
{
DebugCheckInitialize();
return _playModeImpl.CreateResourceUnpackerByAll(unpackingMaxNumber, failedTryAgain, int.MaxValue);
return _playModeImpl.CreateResourceUnpackerByAll(unpackingMaxNumber, failedTryAgain);
}
/// <summary>
@@ -1101,7 +1101,7 @@ namespace YooAsset
public ResourceUnpackerOperation CreateResourceUnpacker(string tag, int unpackingMaxNumber, int failedTryAgain)
{
DebugCheckInitialize();
return _playModeImpl.CreateResourceUnpackerByTags(new string[] { tag }, unpackingMaxNumber, failedTryAgain, int.MaxValue);
return _playModeImpl.CreateResourceUnpackerByTags(new string[] { tag }, unpackingMaxNumber, failedTryAgain);
}
/// <summary>
@@ -1113,7 +1113,7 @@ namespace YooAsset
public ResourceUnpackerOperation CreateResourceUnpacker(string[] tags, int unpackingMaxNumber, int failedTryAgain)
{
DebugCheckInitialize();
return _playModeImpl.CreateResourceUnpackerByTags(tags, unpackingMaxNumber, failedTryAgain, int.MaxValue);
return _playModeImpl.CreateResourceUnpackerByTags(tags, unpackingMaxNumber, failedTryAgain);
}
#endregion
@@ -1128,7 +1128,20 @@ namespace YooAsset
public ResourceImporterOperation CreateResourceImporter(string[] filePaths, int importerMaxNumber, int failedTryAgain)
{
DebugCheckInitialize();
return _playModeImpl.CreateResourceImporterByFilePaths(filePaths, importerMaxNumber, failedTryAgain, int.MaxValue);
return _playModeImpl.CreateResourceImporterByFilePaths(filePaths, importerMaxNumber, failedTryAgain);
}
/// <summary>
/// 创建资源导入器
/// 注意资源信息里需要指定BundleName或BundleGUID
/// </summary>
/// <param name="fileInfos">资源信息列表</param>
/// <param name="importerMaxNumber">同时导入的最大文件数</param>
/// <param name="failedTryAgain">导入失败的重试次数</param>
public ResourceImporterOperation CreateResourceImporter(ImportFileInfo[] fileInfos, int importerMaxNumber, int failedTryAgain)
{
DebugCheckInitialize();
return _playModeImpl.CreateResourceImporterByFileInfos(fileInfos, importerMaxNumber, failedTryAgain);
}
#endregion

View File

@@ -58,7 +58,7 @@ public class CopyBuildinManifestOperation : GameAsyncOperation
string sourcePath = GetBuildinHashFilePath();
string destPath = GetCacheHashFilePath();
string url = DownloadSystemHelper.ConvertToWWWPath(sourcePath);
_hashFileRequestOp = new UnityWebFileRequestOperation(url, destPath);
_hashFileRequestOp = new UnityWebFileRequestOperation(url, destPath, 60);
OperationSystem.StartOperation(_packageName, _hashFileRequestOp);
}
@@ -97,7 +97,7 @@ public class CopyBuildinManifestOperation : GameAsyncOperation
string sourcePath = GetBuildinManifestFilePath();
string destPath = GetCacheManifestFilePath();
string url = DownloadSystemHelper.ConvertToWWWPath(sourcePath);
_manifestFileRequestOp = new UnityWebFileRequestOperation(url, destPath);
_manifestFileRequestOp = new UnityWebFileRequestOperation(url, destPath, 60);
OperationSystem.StartOperation(_packageName, _manifestFileRequestOp);
}

View File

@@ -44,7 +44,7 @@ public class GetBuildinPackageVersionOperation : GameAsyncOperation
{
string filePath = GetBuildinPackageVersionFilePath();
string url = DownloadSystemHelper.ConvertToWWWPath(filePath);
_versionFileRequestOp = new UnityWebTextRequestOperation(url);
_versionFileRequestOp = new UnityWebTextRequestOperation(url, 60);
OperationSystem.StartOperation(_packageName, _versionFileRequestOp);
}

View File

@@ -1,16 +1,30 @@
#if UNITY_WEBGL && DOUYINMINIGAME
using UnityEngine;
using UnityEngine.Networking;
using YooAsset;
internal class TTFSDownloadFileOperation : DefaultDownloadFileOperation
internal class TTFSDownloadFileOperation : FSDownloadFileOperation
{
private TiktokFileSystem _fileSystem;
protected enum ESteps
{
None,
CreateRequest,
CheckRequest,
TryAgain,
Done,
}
private readonly TiktokFileSystem _fileSystem;
private readonly DownloadFileOptions _options;
private UnityWebCacheRequestOperation _unityWebCacheRequestOp;
private int _requestCount = 0;
private float _tryAgainTimer;
private int _failedTryAgain;
private ESteps _steps = ESteps.None;
internal TTFSDownloadFileOperation(TiktokFileSystem fileSystem, PackageBundle bundle, DownloadFileOptions options) : base(bundle, options)
internal TTFSDownloadFileOperation(TiktokFileSystem fileSystem, PackageBundle bundle, DownloadFileOptions options) : base(bundle)
{
_fileSystem = fileSystem;
_options = options;
}
internal override void InternalStart()
{
@@ -21,81 +35,75 @@ internal class TTFSDownloadFileOperation : DefaultDownloadFileOperation
// 创建下载器
if (_steps == ESteps.CreateRequest)
{
// 获取请求地址
_requestURL = GetRequestURL();
// 重置变量
ResetRequestFiled();
// 创建下载器
CreateWebRequest();
string url = GetRequestURL();
_unityWebCacheRequestOp = new UnityWebCacheRequestOperation(url);
_unityWebCacheRequestOp.StartOperation();
_steps = ESteps.CheckRequest;
}
// 检测下载结果
if (_steps == ESteps.CheckRequest)
{
DownloadProgress = _webRequest.downloadProgress;
DownloadedBytes = (long)_webRequest.downloadedBytes;
Progress = DownloadProgress;
if (_webRequest.isDone == false)
{
CheckRequestTimeout();
Progress = _unityWebCacheRequestOp.Progress;
DownloadProgress = _unityWebCacheRequestOp.DownloadProgress;
DownloadedBytes = (long)_unityWebCacheRequestOp.DownloadedBytes;
if (_unityWebCacheRequestOp.IsDone == false)
return;
}
// 检查网络错误
if (CheckRequestResult())
if (_unityWebCacheRequestOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
//TODO 需要验证抖音插件请求器的下载进度
DownloadProgress = 1f;
DownloadedBytes = Bundle.FileSize;
Progress = 1f;
}
else
{
_steps = ESteps.TryAgain;
if (_failedTryAgain > 0)
{
_steps = ESteps.TryAgain;
YooLogger.Warning($"Failed download : {_unityWebCacheRequestOp.URL} Try again !");
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _unityWebCacheRequestOp.Error;
YooLogger.Error(Error);
}
}
// 注意:最终释放请求器
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--;
_tryAgainTimer = 0f;
_failedTryAgain--;
Progress = 0f;
DownloadProgress = 0f;
DownloadedBytes = 0;
_steps = ESteps.CreateRequest;
YooLogger.Warning(Error);
}
}
}
private void CreateWebRequest()
/// <summary>
/// 获取网络请求地址
/// </summary>
private string GetRequestURL()
{
//TODO : 抖音小游戏没有找到预下载方法
_webRequest = UnityWebRequest.Get(_requestURL);
_webRequest.disposeDownloadHandlerOnDispose = true;
_webRequest.SendWebRequest();
}
private void DisposeWebRequest()
{
if (_webRequest != null)
{
//注意引擎底层会自动调用Abort方法
_webRequest.Dispose();
_webRequest = null;
}
// 轮流返回请求地址
_requestCount++;
if (_requestCount % 2 == 0)
return _options.FallbackURL;
else
return _options.MainURL;
}
}
#endif

View File

@@ -1,6 +1,5 @@
#if UNITY_WEBGL && DOUYINMINIGAME
using UnityEngine;
using UnityEngine.Networking;
using YooAsset;
internal class TTFSLoadBundleOperation : FSLoadBundleOperation
@@ -14,7 +13,7 @@ internal class TTFSLoadBundleOperation : FSLoadBundleOperation
private readonly TiktokFileSystem _fileSystem;
private readonly PackageBundle _bundle;
private DownloadAssetBundleOperation _downloadAssetBundleOp;
private LoadWebAssetBundleOperation _loadWebAssetBundleOp;
private ESteps _steps = ESteps.None;
internal TTFSLoadBundleOperation(TiktokFileSystem fileSystem, PackageBundle bundle)
@@ -33,54 +32,46 @@ internal class TTFSLoadBundleOperation : FSLoadBundleOperation
if (_steps == ESteps.DownloadAssetBundle)
{
if (_downloadAssetBundleOp == null)
if (_loadWebAssetBundleOp == null)
{
DownloadFileOptions options = new DownloadFileOptions(int.MaxValue, 60);
options.MainURL = _fileSystem.RemoteServices.GetRemoteMainURL(_bundle.FileName); ;
options.FallbackURL = _fileSystem.RemoteServices.GetRemoteFallbackURL(_bundle.FileName);
string mainRUL = _fileSystem.RemoteServices.GetRemoteMainURL(_bundle.FileName);
string fallbackURL = _fileSystem.RemoteServices.GetRemoteFallbackURL(_bundle.FileName);
DownloadFileOptions options = new DownloadFileOptions(int.MaxValue);
options.SetURL(mainRUL, fallbackURL);
if (_bundle.Encrypted)
{
_downloadAssetBundleOp = new DownloadEncryptAssetBundleOperation(false, _fileSystem.DecryptionServices, _bundle, options);
_downloadAssetBundleOp.StartOperation();
AddChildOperation(_downloadAssetBundleOp);
_loadWebAssetBundleOp = new LoadWebEncryptAssetBundleOperation(_bundle, options, _fileSystem.DecryptionServices);
_loadWebAssetBundleOp.StartOperation();
AddChildOperation(_loadWebAssetBundleOp);
}
else
{
_downloadAssetBundleOp = new DownloadTiktokAssetBundleOperation(_bundle, options);
_downloadAssetBundleOp.StartOperation();
AddChildOperation(_downloadAssetBundleOp);
_loadWebAssetBundleOp = new LoadTiktokAssetBundleOperation(_bundle, options);
_loadWebAssetBundleOp.StartOperation();
AddChildOperation(_loadWebAssetBundleOp);
}
}
_downloadAssetBundleOp.UpdateOperation();
DownloadProgress = _downloadAssetBundleOp.DownloadProgress;
DownloadedBytes = (long)_downloadAssetBundleOp.DownloadedBytes;
_loadWebAssetBundleOp.UpdateOperation();
DownloadProgress = _loadWebAssetBundleOp.DownloadProgress;
DownloadedBytes = (long)_loadWebAssetBundleOp.DownloadedBytes;
Progress = DownloadProgress;
if (_downloadAssetBundleOp.IsDone == false)
if (_loadWebAssetBundleOp.IsDone == false)
return;
if (_downloadAssetBundleOp.Status == EOperationStatus.Succeed)
if (_loadWebAssetBundleOp.Status == EOperationStatus.Succeed)
{
var assetBundle = _downloadAssetBundleOp.Result;
if (assetBundle == null)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"{nameof(DownloadAssetBundleOperation)} loaded asset bundle is null !";
}
else
{
_steps = ESteps.Done;
Result = new TTAssetBundleResult(_fileSystem, _bundle, assetBundle);
Status = EOperationStatus.Succeed;
}
var assetBundle = _loadWebAssetBundleOp.Result;
_steps = ESteps.Done;
Result = new TTAssetBundleResult(_fileSystem, _bundle, assetBundle);
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _downloadAssetBundleOp.Error;
Error = _loadWebAssetBundleOp.Error;
}
}
}

View File

@@ -1,126 +0,0 @@
#if UNITY_WEBGL && DOUYINMINIGAME
using UnityEngine;
using TTSDK;
namespace YooAsset
{
internal class DownloadTiktokAssetBundleOperation : DownloadAssetBundleOperation
{
private ESteps _steps = ESteps.None;
internal DownloadTiktokAssetBundleOperation(PackageBundle bundle, DownloadFileOptions options) : base(bundle, options)
{
}
internal override void InternalStart()
{
_steps = ESteps.CreateRequest;
}
internal override void InternalUpdate()
{
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 需要验证抖音插件请求器的下载进度
//CheckRequestTimeout();
return;
}
// 检查网络错误
if (CheckRequestResult())
{
var downloadHanlder = (DownloadHandlerTTAssetBundle)_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 需要验证抖音插件请求器的下载进度
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 InternalAbort()
{
_steps = ESteps.Done;
DisposeWebRequest();
}
private void CreateWebRequest()
{
_webRequest = TTAssetBundle.GetAssetBundle(_requestURL);
_webRequest.disposeDownloadHandlerOnDispose = true;
_webRequest.SendWebRequest();
}
private void DisposeWebRequest()
{
if (_webRequest != null)
{
//注意引擎底层会自动调用Abort方法
_webRequest.Dispose();
_webRequest = null;
}
}
}
}
#endif

View File

@@ -0,0 +1,114 @@
#if UNITY_WEBGL && DOUYINMINIGAME
using UnityEngine;
namespace YooAsset
{
internal class LoadTiktokAssetBundleOperation : LoadWebAssetBundleOperation
{
protected enum ESteps
{
None,
CreateRequest,
CheckRequest,
TryAgain,
Done,
}
private readonly PackageBundle _bundle;
private readonly DownloadFileOptions _options;
private UnityTiktokAssetBundleRequestOperation _unityTiktokAssetBundleRequestOp;
private int _requestCount = 0;
private float _tryAgainTimer;
private int _failedTryAgain;
private ESteps _steps = ESteps.None;
internal LoadTiktokAssetBundleOperation(PackageBundle bundle, DownloadFileOptions options)
{
_bundle = bundle;
_options = options;
}
internal override void InternalStart()
{
_steps = ESteps.CreateRequest;
}
internal override void InternalUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
// 创建下载器
if (_steps == ESteps.CreateRequest)
{
string url = GetRequestURL();
_unityTiktokAssetBundleRequestOp = new UnityTiktokAssetBundleRequestOperation(_bundle, url);
_unityTiktokAssetBundleRequestOp.StartOperation();
AddChildOperation(_unityTiktokAssetBundleRequestOp);
_steps = ESteps.CheckRequest;
}
// 检测下载结果
if (_steps == ESteps.CheckRequest)
{
_unityTiktokAssetBundleRequestOp.UpdateOperation();
Progress = _unityTiktokAssetBundleRequestOp.Progress;
DownloadProgress = _unityTiktokAssetBundleRequestOp.DownloadProgress;
DownloadedBytes = (long)_unityTiktokAssetBundleRequestOp.DownloadedBytes;
if (_unityTiktokAssetBundleRequestOp.IsDone == false)
return;
if (_unityTiktokAssetBundleRequestOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
Result = _unityTiktokAssetBundleRequestOp.Result;
}
else
{
if (_failedTryAgain > 0)
{
_steps = ESteps.TryAgain;
YooLogger.Warning($"Failed download : {_unityTiktokAssetBundleRequestOp.URL} Try again !");
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _unityTiktokAssetBundleRequestOp.Error;
YooLogger.Error(Error);
}
}
}
// 重新尝试下载
if (_steps == ESteps.TryAgain)
{
_tryAgainTimer += Time.unscaledDeltaTime;
if (_tryAgainTimer > 1f)
{
_tryAgainTimer = 0f;
_failedTryAgain--;
Progress = 0f;
DownloadProgress = 0f;
DownloadedBytes = 0;
_steps = ESteps.CreateRequest;
}
}
}
/// <summary>
/// 获取网络请求地址
/// </summary>
private string GetRequestURL()
{
// 轮流返回请求地址
_requestCount++;
if (_requestCount % 2 == 0)
return _options.FallbackURL;
else
return _options.MainURL;
}
}
}
#endif

View File

@@ -0,0 +1,95 @@
#if UNITY_WEBGL && DOUYINMINIGAME
using UnityEngine.Networking;
using UnityEngine;
using TTSDK;
namespace YooAsset
{
internal class UnityTiktokAssetBundleRequestOperation : UnityWebRequestOperation
{
protected enum ESteps
{
None,
CreateRequest,
Download,
Done,
}
private readonly PackageBundle _packageBundle;
private UnityWebRequestAsyncOperation _requestOperation;
private ESteps _steps = ESteps.None;
/// <summary>
/// 请求结果
/// </summary>
public AssetBundle Result { private set; get; }
internal UnityTiktokAssetBundleRequestOperation(PackageBundle bundle, string url) : base(url)
{
_packageBundle = bundle;
}
internal override void InternalStart()
{
_steps = ESteps.CreateRequest;
}
internal override void InternalUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.CreateRequest)
{
CreateWebRequest();
_steps = ESteps.Download;
}
if (_steps == ESteps.Download)
{
DownloadProgress = _webRequest.downloadProgress;
DownloadedBytes = (long)_webRequest.downloadedBytes;
Progress = _requestOperation.progress;
if (_requestOperation.isDone == false)
return;
if (CheckRequestResult())
{
var downloadHanlder = (DownloadHandlerTTAssetBundle)_webRequest.downloadHandler;
AssetBundle assetBundle = downloadHanlder.assetBundle;
if (assetBundle == null)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"URL : {_requestURL} Download handler asset bundle object is null !";
}
else
{
_steps = ESteps.Done;
Result = assetBundle;
Status = EOperationStatus.Succeed;
//TODO 需要验证抖音插件请求器的下载进度
DownloadProgress = 1f;
DownloadedBytes = _packageBundle.FileSize;
Progress = 1f;
}
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
}
// 注意:最终释放请求器
DisposeRequest();
}
}
private void CreateWebRequest()
{
_webRequest = TTAssetBundle.GetAssetBundle(_requestURL);
_webRequest.disposeDownloadHandlerOnDispose = true;
_requestOperation = _webRequest.SendWebRequest();
}
}
}
#endif

View File

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

View File

@@ -136,8 +136,9 @@ internal class TiktokFileSystem : IFileSystem
}
public virtual FSDownloadFileOperation DownloadFileAsync(PackageBundle bundle, DownloadFileOptions options)
{
options.MainURL = RemoteServices.GetRemoteMainURL(bundle.FileName);
options.FallbackURL = RemoteServices.GetRemoteFallbackURL(bundle.FileName);
string mainURL = RemoteServices.GetRemoteMainURL(bundle.FileName);
string fallbackURL = RemoteServices.GetRemoteFallbackURL(bundle.FileName);
options.SetURL(mainURL, fallbackURL);
var operation = new TTFSDownloadFileOperation(this, bundle, options);
return operation;
}

View File

@@ -1,17 +1,30 @@
#if UNITY_WEBGL && WEIXINMINIGAME
using UnityEngine;
using UnityEngine.Networking;
using YooAsset;
using WeChatWASM;
internal class WXFSDownloadFileOperation : DefaultDownloadFileOperation
internal class WXFSDownloadFileOperation : FSDownloadFileOperation
{
private WechatFileSystem _fileSystem;
protected enum ESteps
{
None,
CreateRequest,
CheckRequest,
TryAgain,
Done,
}
private readonly WechatFileSystem _fileSystem;
private readonly DownloadFileOptions _options;
private UnityWebCacheRequestOperation _unityWebCacheRequestOp;
private int _requestCount = 0;
private float _tryAgainTimer;
private int _failedTryAgain;
private ESteps _steps = ESteps.None;
internal WXFSDownloadFileOperation(WechatFileSystem fileSystem, PackageBundle bundle, DownloadFileOptions options) : base(bundle, options)
internal WXFSDownloadFileOperation(WechatFileSystem fileSystem, PackageBundle bundle, DownloadFileOptions options) : base(bundle)
{
_fileSystem = fileSystem;
_options = options;
}
internal override void InternalStart()
{
@@ -22,34 +35,25 @@ internal class WXFSDownloadFileOperation : DefaultDownloadFileOperation
// 创建下载器
if (_steps == ESteps.CreateRequest)
{
// 获取请求地址
_requestURL = GetRequestURL();
// 重置变量
ResetRequestFiled();
// 创建下载器
CreateWebRequest();
string url = GetRequestURL();
_unityWebCacheRequestOp = new UnityWebCacheRequestOperation(url);
_unityWebCacheRequestOp.SetRequestHeader("wechatminigame-preload", "1");
_unityWebCacheRequestOp.StartOperation();
AddChildOperation(_unityWebCacheRequestOp);
_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();
_unityWebCacheRequestOp.UpdateOperation();
Progress = _unityWebCacheRequestOp.Progress;
DownloadProgress = _unityWebCacheRequestOp.DownloadProgress;
DownloadedBytes = (long)_unityWebCacheRequestOp.DownloadedBytes;
if (_unityWebCacheRequestOp.IsDone == false)
return;
}
// 检查网络错误
if (CheckRequestResult())
if (_unityWebCacheRequestOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
@@ -62,49 +66,48 @@ internal class WXFSDownloadFileOperation : DefaultDownloadFileOperation
}
else
{
_steps = ESteps.TryAgain;
if (_failedTryAgain > 0)
{
_steps = ESteps.TryAgain;
YooLogger.Warning($"Failed download : {_unityWebCacheRequestOp.URL} Try again !");
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _unityWebCacheRequestOp.Error;
YooLogger.Error(Error);
}
}
// 注意:最终释放请求器
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--;
_tryAgainTimer = 0f;
_failedTryAgain--;
Progress = 0f;
DownloadProgress = 0f;
DownloadedBytes = 0;
_steps = ESteps.CreateRequest;
YooLogger.Warning(Error);
}
}
}
private void CreateWebRequest()
/// <summary>
/// 获取网络请求地址
/// </summary>
private string GetRequestURL()
{
_webRequest = UnityWebRequest.Get(_requestURL);
_webRequest.SetRequestHeader("wechatminigame-preload", "1");
_webRequest.disposeDownloadHandlerOnDispose = true;
_webRequest.SendWebRequest();
}
private void DisposeWebRequest()
{
if (_webRequest != null)
{
//注意引擎底层会自动调用Abort方法
_webRequest.Dispose();
_webRequest = null;
}
// 轮流返回请求地址
_requestCount++;
if (_requestCount % 2 == 0)
return _options.FallbackURL;
else
return _options.MainURL;
}
}
#endif

View File

@@ -12,9 +12,9 @@ internal class WXFSLoadBundleOperation : FSLoadBundleOperation
private readonly WechatFileSystem _fileSystem;
private readonly PackageBundle _bundle;
private DownloadAssetBundleOperation _downloadAssetBundleOp;
private LoadWebAssetBundleOperation _loadWebAssetBundleOp;
private ESteps _steps = ESteps.None;
internal WXFSLoadBundleOperation(WechatFileSystem fileSystem, PackageBundle bundle)
{
_fileSystem = fileSystem;
@@ -31,54 +31,46 @@ internal class WXFSLoadBundleOperation : FSLoadBundleOperation
if (_steps == ESteps.DownloadAssetBundle)
{
if (_downloadAssetBundleOp == null)
if (_loadWebAssetBundleOp == null)
{
DownloadFileOptions options = new DownloadFileOptions(int.MaxValue, 60);
options.MainURL = _fileSystem.RemoteServices.GetRemoteMainURL(_bundle.FileName); ;
options.FallbackURL = _fileSystem.RemoteServices.GetRemoteFallbackURL(_bundle.FileName);
string mainURL = _fileSystem.RemoteServices.GetRemoteMainURL(_bundle.FileName);
string fallbackURL = _fileSystem.RemoteServices.GetRemoteFallbackURL(_bundle.FileName);
DownloadFileOptions options = new DownloadFileOptions(int.MaxValue);
options.SetURL(mainURL, fallbackURL);
if (_bundle.Encrypted)
{
_downloadAssetBundleOp = new DownloadEncryptAssetBundleOperation(false, _fileSystem.DecryptionServices, _bundle, options);
_downloadAssetBundleOp.StartOperation();
AddChildOperation(_downloadAssetBundleOp);
_loadWebAssetBundleOp = new LoadWebEncryptAssetBundleOperation(_bundle, options, _fileSystem.DecryptionServices);
_loadWebAssetBundleOp.StartOperation();
AddChildOperation(_loadWebAssetBundleOp);
}
else
{
_downloadAssetBundleOp = new DownloadWechatAssetBundleOperation(_bundle, options);
_downloadAssetBundleOp.StartOperation();
AddChildOperation(_downloadAssetBundleOp);
_loadWebAssetBundleOp = new LoadWechatAssetBundleOperation(_bundle, options);
_loadWebAssetBundleOp.StartOperation();
AddChildOperation(_loadWebAssetBundleOp);
}
}
_downloadAssetBundleOp.UpdateOperation();
DownloadProgress = _downloadAssetBundleOp.DownloadProgress;
DownloadedBytes = (long)_downloadAssetBundleOp.DownloadedBytes;
_loadWebAssetBundleOp.UpdateOperation();
DownloadProgress = _loadWebAssetBundleOp.DownloadProgress;
DownloadedBytes = (long)_loadWebAssetBundleOp.DownloadedBytes;
Progress = DownloadProgress;
if (_downloadAssetBundleOp.IsDone == false)
if (_loadWebAssetBundleOp.IsDone == false)
return;
if (_downloadAssetBundleOp.Status == EOperationStatus.Succeed)
if (_loadWebAssetBundleOp.Status == EOperationStatus.Succeed)
{
var assetBundle = _downloadAssetBundleOp.Result;
if (assetBundle == null)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"{nameof(DownloadAssetBundleOperation)} loaded asset bundle is null !";
}
else
{
_steps = ESteps.Done;
Result = new WXAssetBundleResult(_fileSystem, _bundle, assetBundle);
Status = EOperationStatus.Succeed;
}
var assetBundle = _loadWebAssetBundleOp.Result;
_steps = ESteps.Done;
Result = new WXAssetBundleResult(_fileSystem, _bundle, assetBundle);
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _downloadAssetBundleOp.Error;
Error = _loadWebAssetBundleOp.Error;
}
}
}

View File

@@ -1,128 +0,0 @@
#if UNITY_WEBGL && WEIXINMINIGAME
using UnityEngine;
using WeChatWASM;
namespace YooAsset
{
internal class DownloadWechatAssetBundleOperation : DownloadAssetBundleOperation
{
private ESteps _steps = ESteps.None;
internal DownloadWechatAssetBundleOperation(PackageBundle bundle, DownloadFileOptions options) : base(bundle, options)
{
}
internal override void InternalStart()
{
_steps = ESteps.CreateRequest;
}
internal override void InternalUpdate()
{
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 InternalAbort()
{
_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,120 @@
#if UNITY_WEBGL && WEIXINMINIGAME
using UnityEngine;
namespace YooAsset
{
internal class LoadWechatAssetBundleOperation : LoadWebAssetBundleOperation
{
protected enum ESteps
{
None,
CreateRequest,
CheckRequest,
TryAgain,
Done,
}
private readonly PackageBundle _bundle;
private readonly DownloadFileOptions _options;
private UnityWechatAssetBundleRequestOperation _unityWechatAssetBundleRequestOp;
private int _requestCount = 0;
private float _tryAgainTimer;
private int _failedTryAgain;
private ESteps _steps = ESteps.None;
internal LoadWechatAssetBundleOperation(PackageBundle bundle, DownloadFileOptions options)
{
_bundle = bundle;
_options = options;
}
internal override void InternalStart()
{
_steps = ESteps.CreateRequest;
}
internal override void InternalUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
// 创建下载器
if (_steps == ESteps.CreateRequest)
{
string url = GetRequestURL();
_unityWechatAssetBundleRequestOp = new UnityWechatAssetBundleRequestOperation(_bundle, url);
_unityWechatAssetBundleRequestOp.StartOperation();
AddChildOperation(_unityWechatAssetBundleRequestOp);
_steps = ESteps.CheckRequest;
}
// 检测下载结果
if (_steps == ESteps.CheckRequest)
{
_unityWechatAssetBundleRequestOp.UpdateOperation();
Progress = _unityWechatAssetBundleRequestOp.Progress;
DownloadProgress = _unityWechatAssetBundleRequestOp.DownloadProgress;
DownloadedBytes = (long)_unityWechatAssetBundleRequestOp.DownloadedBytes;
if (_unityWechatAssetBundleRequestOp.IsDone == false)
return;
if (_unityWechatAssetBundleRequestOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
Result = _unityWechatAssetBundleRequestOp.Result;
}
else
{
if (_failedTryAgain > 0)
{
_steps = ESteps.TryAgain;
YooLogger.Warning($"Failed download : {_unityWechatAssetBundleRequestOp.URL} Try again !");
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _unityWechatAssetBundleRequestOp.Error;
YooLogger.Error(Error);
}
}
}
// 重新尝试下载
if (_steps == ESteps.TryAgain)
{
_tryAgainTimer += Time.unscaledDeltaTime;
if (_tryAgainTimer > 1f)
{
_tryAgainTimer = 0f;
_failedTryAgain--;
Progress = 0f;
DownloadProgress = 0f;
DownloadedBytes = 0;
_steps = ESteps.CreateRequest;
}
}
}
internal override void InternalAbort()
{
_steps = ESteps.Done;
if (_unityWechatAssetBundleRequestOp != null)
_unityWechatAssetBundleRequestOp.AbortOperation();
}
/// <summary>
/// 获取网络请求地址
/// </summary>
protected string GetRequestURL()
{
// 轮流返回请求地址
_requestCount++;
if (_requestCount % 2 == 0)
return _options.FallbackURL;
else
return _options.MainURL;
}
}
}
#endif

View File

@@ -0,0 +1,96 @@
#if UNITY_WEBGL && WEIXINMINIGAME
using UnityEngine.Networking;
using UnityEngine;
using WeChatWASM;
namespace YooAsset
{
internal class UnityWechatAssetBundleRequestOperation : UnityWebRequestOperation
{
protected enum ESteps
{
None,
CreateRequest,
Download,
Done,
}
private readonly PackageBundle _packageBundle;
private UnityWebRequestAsyncOperation _requestOperation;
private ESteps _steps = ESteps.None;
/// <summary>
/// 请求结果
/// </summary>
public AssetBundle Result { private set; get; }
internal UnityWechatAssetBundleRequestOperation(PackageBundle bundle, string url) : base(url)
{
_packageBundle = bundle;
}
internal override void InternalStart()
{
_steps = ESteps.CreateRequest;
}
internal override void InternalUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.CreateRequest)
{
CreateWebRequest();
_steps = ESteps.Download;
}
if (_steps == ESteps.Download)
{
DownloadProgress = _webRequest.downloadProgress;
DownloadedBytes = (long)_webRequest.downloadedBytes;
Progress = _requestOperation.progress;
if (_requestOperation.isDone == false)
return;
if (CheckRequestResult())
{
var downloadHanlder = (DownloadHandlerWXAssetBundle)_webRequest.downloadHandler;
AssetBundle assetBundle = downloadHanlder.assetBundle;
if (assetBundle == null)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"URL : {_requestURL} 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 = _packageBundle.FileSize;
Progress = 1f;
}
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
}
// 注意:最终释放请求器
DisposeRequest();
}
}
private void CreateWebRequest()
{
_webRequest = WXAssetBundle.GetAssetBundle(_requestURL);
_webRequest.disposeDownloadHandlerOnDispose = true;
_requestOperation = _webRequest.SendWebRequest();
}
}
}
#endif

View File

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

View File

@@ -151,8 +151,9 @@ internal class WechatFileSystem : IFileSystem
}
public virtual FSDownloadFileOperation DownloadFileAsync(PackageBundle bundle, DownloadFileOptions options)
{
options.MainURL = RemoteServices.GetRemoteMainURL(bundle.FileName);
options.FallbackURL = RemoteServices.GetRemoteFallbackURL(bundle.FileName);
string mainURL = RemoteServices.GetRemoteMainURL(bundle.FileName);
string fallbackURL = RemoteServices.GetRemoteFallbackURL(bundle.FileName);
options.SetURL(mainURL, fallbackURL);
var operation = new WXFSDownloadFileOperation(this, bundle, options);
return operation;
}

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 4ce11d98eccfa4b44bf2c82f7d87d105
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,94 @@
using System;
using System.IO;
using System.Text;
using System.Collections;
using UnityEngine;
using YooAsset;
/// <summary>
/// 文件偏移加密方式
/// </summary>
public class TestFileOffsetEncryption : IEncryptionServices
{
public EncryptResult Encrypt(EncryptFileInfo fileInfo)
{
// 说明对TestRes3资源目录进行加密
if (fileInfo.BundleName.Contains("_testres3_"))
{
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 TestFileOffsetDecryption : IDecryptionServices
{
/// <summary>
/// 同步方式获取解密的资源包对象
/// 注意:加载流对象在资源包对象释放的时候会自动释放
/// </summary>
DecryptResult IDecryptionServices.LoadAssetBundle(DecryptFileInfo fileInfo)
{
DecryptResult decryptResult = new DecryptResult();
decryptResult.ManagedStream = null;
decryptResult.Result = AssetBundle.LoadFromFile(fileInfo.FileLoadPath, fileInfo.FileLoadCRC, GetFileOffset());
return decryptResult;
}
/// <summary>
/// 异步方式获取解密的资源包对象
/// 注意:加载流对象在资源包对象释放的时候会自动释放
/// </summary>
DecryptResult IDecryptionServices.LoadAssetBundleAsync(DecryptFileInfo fileInfo)
{
DecryptResult decryptResult = new DecryptResult();
decryptResult.ManagedStream = null;
decryptResult.CreateRequest = AssetBundle.LoadFromFileAsync(fileInfo.FileLoadPath, fileInfo.FileLoadCRC, GetFileOffset());
return decryptResult;
}
/// <summary>
/// 后备方式获取解密的资源包对象
/// </summary>
DecryptResult IDecryptionServices.LoadAssetBundleFallback(DecryptFileInfo fileInfo)
{
return new DecryptResult();
}
/// <summary>
/// 获取解密的字节数据
/// </summary>
byte[] IDecryptionServices.ReadFileData(DecryptFileInfo fileInfo)
{
throw new System.NotImplementedException();
}
/// <summary>
/// 获取解密的文本数据
/// </summary>
string IDecryptionServices.ReadFileText(DecryptFileInfo fileInfo)
{
throw new System.NotImplementedException();
}
private static ulong GetFileOffset()
{
return 32;
}
}

View File

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

View File

@@ -0,0 +1,155 @@
using System;
using System.IO;
using System.Text;
using System.Collections;
using UnityEngine;
using YooAsset;
/// <summary>
/// 资源文件解密流
/// </summary>
public class BundleStream : FileStream
{
public const byte KEY = 64;
public BundleStream(string path, FileMode mode, FileAccess access, FileShare share) : base(path, mode, access, share)
{
}
public BundleStream(string path, FileMode mode) : base(path, mode)
{
}
public override int Read(byte[] array, int offset, int count)
{
var index = base.Read(array, offset, count);
for (int i = 0; i < array.Length; i++)
{
array[i] ^= KEY;
}
return index;
}
}
/// <summary>
/// 文件流加密方式
/// </summary>
public class TestFileStreamEncryption : IEncryptionServices
{
public EncryptResult Encrypt(EncryptFileInfo fileInfo)
{
// 说明对TestRes3资源目录进行加密
if (fileInfo.BundleName.Contains("_testres3_"))
{
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>
public class TestFileStreamDecryption : IDecryptionServices
{
/// <summary>
/// 同步方式获取解密的资源包对象
/// 注意:加载流对象在资源包对象释放的时候会自动释放
/// </summary>
DecryptResult IDecryptionServices.LoadAssetBundle(DecryptFileInfo fileInfo)
{
BundleStream bundleStream = new BundleStream(fileInfo.FileLoadPath, FileMode.Open, FileAccess.Read, FileShare.Read);
DecryptResult decryptResult = new DecryptResult();
decryptResult.ManagedStream = bundleStream;
decryptResult.Result = AssetBundle.LoadFromStream(bundleStream, fileInfo.FileLoadCRC, GetManagedReadBufferSize());
return decryptResult;
}
/// <summary>
/// 异步方式获取解密的资源包对象
/// 注意:加载流对象在资源包对象释放的时候会自动释放
/// </summary>
DecryptResult IDecryptionServices.LoadAssetBundleAsync(DecryptFileInfo fileInfo)
{
BundleStream bundleStream = new BundleStream(fileInfo.FileLoadPath, FileMode.Open, FileAccess.Read, FileShare.Read);
DecryptResult decryptResult = new DecryptResult();
decryptResult.ManagedStream = bundleStream;
decryptResult.CreateRequest = AssetBundle.LoadFromStreamAsync(bundleStream, fileInfo.FileLoadCRC, GetManagedReadBufferSize());
return decryptResult;
}
/// <summary>
/// 后备方式获取解密的资源包对象
/// </summary>
DecryptResult IDecryptionServices.LoadAssetBundleFallback(DecryptFileInfo fileInfo)
{
return new DecryptResult();
}
/// <summary>
/// 获取解密的字节数据
/// </summary>
byte[] IDecryptionServices.ReadFileData(DecryptFileInfo fileInfo)
{
throw new System.NotImplementedException();
}
/// <summary>
/// 获取解密的文本数据
/// </summary>
string IDecryptionServices.ReadFileText(DecryptFileInfo fileInfo)
{
throw new System.NotImplementedException();
}
private static uint GetManagedReadBufferSize()
{
return 1024;
}
}
/// <summary>
/// WebGL平台解密类
/// 注意WebGL平台支持内存解密
/// </summary>
public class TestWebFileMemoryDecryption : IWebDecryptionServices
{
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] ^= BundleStream.KEY;
}
WebDecryptResult decryptResult = new WebDecryptResult();
decryptResult.Result = AssetBundle.LoadFromMemory(copyData);
return decryptResult;
*/
for (int i = 0; i < fileInfo.FileData.Length; i++)
{
fileInfo.FileData[i] ^= BundleStream.KEY;
}
WebDecryptResult decryptResult = new WebDecryptResult();
decryptResult.Result = AssetBundle.LoadFromMemory(fileInfo.FileData);
return decryptResult;
}
}

View File

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

View File

@@ -89,7 +89,7 @@ public class T0_InitYooAssets : IPrebuildSetup, IPostBuildCleanup
YooAsset.Editor.AssetBundleCollectorSettingData.CreateCollector(panelGroup, collector1);
}
// 预制体
// 预制体构建到一个AB
var prefabGroup = YooAsset.Editor.AssetBundleCollectorSettingData.CreateGroup(testPackage, "PrefabGroup");
{
var collector1 = new YooAsset.Editor.AssetBundleCollector();
@@ -169,14 +169,30 @@ public class T0_InitYooAssets : IPrebuildSetup, IPostBuildCleanup
collector1.CollectorGUID = "e082d492b9da65e499cee3495be3645d"; //TestRes3/music目录
collector1.CollectorType = YooAsset.Editor.ECollectorType.MainAssetCollector;
collector1.PackRuleName = nameof(YooAsset.Editor.PackSeparately);
YooAsset.Editor.AssetBundleCollectorSettingData.CreateCollector(referenceGroup, collector1);
YooAsset.Editor.AssetBundleCollectorSettingData.CreateCollector(encryptGroup, collector1);
var collector2 = new YooAsset.Editor.AssetBundleCollector();
collector2.CollectPath = "";
collector2.CollectorGUID = "8c5a1726d94498e4cbe30f5f510cc796"; //TestRes3/prefab目录
collector2.CollectorType = YooAsset.Editor.ECollectorType.MainAssetCollector;
collector2.PackRuleName = nameof(YooAsset.Editor.PackSeparately);
YooAsset.Editor.AssetBundleCollectorSettingData.CreateCollector(referenceGroup, collector2);
YooAsset.Editor.AssetBundleCollectorSettingData.CreateCollector(encryptGroup, collector2);
var collector3 = new YooAsset.Editor.AssetBundleCollector();
collector3.CollectPath = "";
collector3.CollectorGUID = "401af1ca0abf3ae4594631e5f71bfe27"; //TestRes3/unpack目录
collector3.CollectorType = YooAsset.Editor.ECollectorType.MainAssetCollector;
collector3.AssetTags = "unpack";
collector3.PackRuleName = nameof(YooAsset.Editor.PackSeparately);
YooAsset.Editor.AssetBundleCollectorSettingData.CreateCollector(encryptGroup, collector3);
var collector4 = new YooAsset.Editor.AssetBundleCollector();
collector4.CollectPath = "";
collector4.CollectorGUID = "35f454fb80a715047bcf0ce30c7c4f18"; //TestRes3/import目录
collector4.CollectorType = YooAsset.Editor.ECollectorType.MainAssetCollector;
collector4.AssetTags = "import";
collector4.PackRuleName = nameof(YooAsset.Editor.PackSeparately);
YooAsset.Editor.AssetBundleCollectorSettingData.CreateCollector(encryptGroup, collector4);
}
}
private static void CreateRawBundlePackageCollector()

View File

@@ -24,7 +24,7 @@ public class T1_TestEditorFileSystem : IPrebuildSetup, IPostBuildCleanup
simulateParams.InvokeAssmeblyName = "YooAsset.Test.Editor";
simulateParams.InvokeClassFullName = "TestPackageBuilder";
simulateParams.InvokeMethodName = "BuildPackage";
var simulateResult = PakcageInvokeBuilder.InvokeBuilder(simulateParams);
var simulateResult = PackageInvokeBuilder.InvokeBuilder(simulateParams);
UnityEditor.EditorPrefs.SetString(ASSET_BUNDLE_PACKAGE_ROOT_KEY, simulateResult.PackageRootDirectory);
}
@@ -35,7 +35,7 @@ public class T1_TestEditorFileSystem : IPrebuildSetup, IPostBuildCleanup
simulateParams.InvokeAssmeblyName = "YooAsset.Test.Editor";
simulateParams.InvokeClassFullName = "TestPackageBuilder";
simulateParams.InvokeMethodName = "BuildPackage";
var simulateResult = PakcageInvokeBuilder.InvokeBuilder(simulateParams);
var simulateResult = PackageInvokeBuilder.InvokeBuilder(simulateParams);
UnityEditor.EditorPrefs.SetString(RAW_BUNDLE_PACKAGE_ROOT_KEY, simulateResult.PackageRootDirectory);
}
#endif

View File

@@ -11,8 +11,8 @@ using YooAsset;
public class T2_TestBuldinFileSystem : IPrebuildSetup, IPostBuildCleanup
{
private const string ASSET_BUNDLE_PACKAGE_ROOT_KEY = "T2_ASSET_BUNDLE_PACKAGE_ROOT_KEY";
private const string RAW_BUNDLE_PACKAGE_ROOT_KEY = "T2_RAW_BUNDLE_PACKAGE_ROOT_KEY";
public const string ASSET_BUNDLE_PACKAGE_ROOT_KEY = "T2_ASSET_BUNDLE_PACKAGE_ROOT_KEY";
public const string RAW_BUNDLE_PACKAGE_ROOT_KEY = "T2_RAW_BUNDLE_PACKAGE_ROOT_KEY";
void IPrebuildSetup.Setup()
{
@@ -24,7 +24,7 @@ public class T2_TestBuldinFileSystem : IPrebuildSetup, IPostBuildCleanup
buildParams.InvokeAssmeblyName = "YooAsset.Test.Editor";
buildParams.InvokeClassFullName = "TestPackageBuilder";
buildParams.InvokeMethodName = "BuildPackage";
var simulateResult = PakcageInvokeBuilder.InvokeBuilder(buildParams);
var simulateResult = PackageInvokeBuilder.InvokeBuilder(buildParams);
UnityEditor.EditorPrefs.SetString(ASSET_BUNDLE_PACKAGE_ROOT_KEY, simulateResult.PackageRootDirectory);
}
@@ -35,7 +35,7 @@ public class T2_TestBuldinFileSystem : IPrebuildSetup, IPostBuildCleanup
buildParams.InvokeAssmeblyName = "YooAsset.Test.Editor";
buildParams.InvokeClassFullName = "TestPackageBuilder";
buildParams.InvokeMethodName = "BuildPackage";
var simulateResult = PakcageInvokeBuilder.InvokeBuilder(buildParams);
var simulateResult = PackageInvokeBuilder.InvokeBuilder(buildParams);
UnityEditor.EditorPrefs.SetString(RAW_BUNDLE_PACKAGE_ROOT_KEY, simulateResult.PackageRootDirectory);
}
#endif

View File

@@ -6,6 +6,9 @@ using UnityEngine;
using NUnit.Framework;
using YooAsset;
/// <summary>
/// 测试加载加密文件
/// </summary>
public class TestBundleEncryption
{
public IEnumerator RuntimeTester()
@@ -39,245 +42,27 @@ public class TestBundleEncryption
Assert.IsNotNull(audioSource.clip);
}
// 试听三秒钟
yield return new WaitForSeconds(3f);
yield return new WaitForSeconds(1f);
}
}
/// <summary>
/// 文件流加密方式
/// </summary>
public class TestFileStreamEncryption : IEncryptionServices
/* 资源代码流程
* 内置文件解压(加载器)
BuildinFileSystem::LoadBundleFile()
{
public EncryptResult Encrypt(EncryptFileInfo fileInfo)
{
// 说明对TestRes3资源目录进行加密
if (fileInfo.BundleName.Contains("_testres3_"))
{
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;
}
}
_unpackFileSystem.LoadBundleFile(bundle);
}
/// <summary>
/// 文件偏移加密方式
/// </summary>
public class TestFileOffsetEncryption : IEncryptionServices
UnpackFileSystem::LoadAssetBundleOperation()
{
public EncryptResult Encrypt(EncryptFileInfo fileInfo)
{
// 说明对TestRes3资源目录进行加密
if (fileInfo.BundleName.Contains("_testres3_"))
{
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;
}
}
DownloadFileOptions options = new DownloadFileOptions(int.MaxValue, 60);
_unpackFileSystem.DownloadFileAsync(_bundle, options);
}
/// <summary>
/// 资源文件解密流
/// </summary>
public class BundleStream : FileStream
UnpackFileSystem::DownloadFileAsync()
{
public const byte KEY = 64;
public BundleStream(string path, FileMode mode, FileAccess access, FileShare share) : base(path, mode, access, share)
{
}
public BundleStream(string path, FileMode mode) : base(path, mode)
{
}
public override int Read(byte[] array, int offset, int count)
{
var index = base.Read(array, offset, count);
for (int i = 0; i < array.Length; i++)
{
array[i] ^= KEY;
}
return index;
}
//RemoteServices返回内置文件路径
string mainURL = RemoteServices.GetRemoteMainURL(bundle.FileName);
string fallbackURL = RemoteServices.GetRemoteFallbackURL(bundle.FileName);
options.SetURL(mainURL, fallbackURL);
return new DownloadPackageBundleOperation(bundle, options);
}
/// <summary>
/// 资源文件流解密类
/// </summary>
public class TestFileStreamDecryption : IDecryptionServices
{
/// <summary>
/// 同步方式获取解密的资源包对象
/// 注意:加载流对象在资源包对象释放的时候会自动释放
/// </summary>
DecryptResult IDecryptionServices.LoadAssetBundle(DecryptFileInfo fileInfo)
{
BundleStream bundleStream = new BundleStream(fileInfo.FileLoadPath, FileMode.Open, FileAccess.Read, FileShare.Read);
DecryptResult decryptResult = new DecryptResult();
decryptResult.ManagedStream = bundleStream;
decryptResult.Result = AssetBundle.LoadFromStream(bundleStream, fileInfo.FileLoadCRC, GetManagedReadBufferSize());
return decryptResult;
}
/// <summary>
/// 异步方式获取解密的资源包对象
/// 注意:加载流对象在资源包对象释放的时候会自动释放
/// </summary>
DecryptResult IDecryptionServices.LoadAssetBundleAsync(DecryptFileInfo fileInfo)
{
BundleStream bundleStream = new BundleStream(fileInfo.FileLoadPath, FileMode.Open, FileAccess.Read, FileShare.Read);
DecryptResult decryptResult = new DecryptResult();
decryptResult.ManagedStream = bundleStream;
decryptResult.CreateRequest = AssetBundle.LoadFromStreamAsync(bundleStream, fileInfo.FileLoadCRC, GetManagedReadBufferSize());
return decryptResult;
}
/// <summary>
/// 后备方式获取解密的资源包对象
/// </summary>
DecryptResult IDecryptionServices.LoadAssetBundleFallback(DecryptFileInfo fileInfo)
{
return new DecryptResult();
}
/// <summary>
/// 获取解密的字节数据
/// </summary>
byte[] IDecryptionServices.ReadFileData(DecryptFileInfo fileInfo)
{
throw new System.NotImplementedException();
}
/// <summary>
/// 获取解密的文本数据
/// </summary>
string IDecryptionServices.ReadFileText(DecryptFileInfo fileInfo)
{
throw new System.NotImplementedException();
}
private static uint GetManagedReadBufferSize()
{
return 1024;
}
}
/// <summary>
/// 资源文件偏移解密类
/// </summary>
public class TestFileOffsetDecryption : IDecryptionServices
{
/// <summary>
/// 同步方式获取解密的资源包对象
/// 注意:加载流对象在资源包对象释放的时候会自动释放
/// </summary>
DecryptResult IDecryptionServices.LoadAssetBundle(DecryptFileInfo fileInfo)
{
DecryptResult decryptResult = new DecryptResult();
decryptResult.ManagedStream = null;
decryptResult.Result = AssetBundle.LoadFromFile(fileInfo.FileLoadPath, fileInfo.FileLoadCRC, GetFileOffset());
return decryptResult;
}
/// <summary>
/// 异步方式获取解密的资源包对象
/// 注意:加载流对象在资源包对象释放的时候会自动释放
/// </summary>
DecryptResult IDecryptionServices.LoadAssetBundleAsync(DecryptFileInfo fileInfo)
{
DecryptResult decryptResult = new DecryptResult();
decryptResult.ManagedStream = null;
decryptResult.CreateRequest = AssetBundle.LoadFromFileAsync(fileInfo.FileLoadPath, fileInfo.FileLoadCRC, GetFileOffset());
return decryptResult;
}
/// <summary>
/// 后备方式获取解密的资源包对象
/// </summary>
DecryptResult IDecryptionServices.LoadAssetBundleFallback(DecryptFileInfo fileInfo)
{
return new DecryptResult();
}
/// <summary>
/// 获取解密的字节数据
/// </summary>
byte[] IDecryptionServices.ReadFileData(DecryptFileInfo fileInfo)
{
throw new System.NotImplementedException();
}
/// <summary>
/// 获取解密的文本数据
/// </summary>
string IDecryptionServices.ReadFileText(DecryptFileInfo fileInfo)
{
throw new System.NotImplementedException();
}
private static ulong GetFileOffset()
{
return 32;
}
}
/// <summary>
/// WebGL平台解密类
/// 注意WebGL平台支持内存解密
/// </summary>
public class TestWebFileStreamDecryption : IWebDecryptionServices
{
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] ^= BundleStream.KEY;
}
WebDecryptResult decryptResult = new WebDecryptResult();
decryptResult.Result = AssetBundle.LoadFromMemory(copyData);
return decryptResult;
*/
for (int i = 0; i < fileInfo.FileData.Length; i++)
{
fileInfo.FileData[i] ^= BundleStream.KEY;
}
WebDecryptResult decryptResult = new WebDecryptResult();
decryptResult.Result = AssetBundle.LoadFromMemory(fileInfo.FileData);
return decryptResult;
}
}
*/

View File

@@ -0,0 +1,47 @@
using System;
using System.IO;
using System.Text;
using System.Collections;
using UnityEngine;
using NUnit.Framework;
using YooAsset;
/// <summary>
/// 测试内置文件解压
/// </summary>
public class TestBundleUnpacker
{
public IEnumerator RuntimeTester()
{
ResourcePackage package = YooAssets.GetPackage(TestDefine.AssetBundlePackageName);
Assert.IsNotNull(package);
var resourceUnpacker = package.CreateResourceUnpacker("unpack", 10, 1);
Assert.AreEqual(resourceUnpacker.TotalDownloadCount, 2);
yield return resourceUnpacker;
Assert.AreEqual(EOperationStatus.Succeed, resourceUnpacker.Status);
// 等待一秒
yield return new WaitForSeconds(1f);
}
}
/* 资源代码流程
* 内置文件解压(解压器)
BundleInfo::CreateDownloader()
{
return _buildFileSystem.DownloadFileAsync(Bundle, options);
}
BuildinFileSystem::DownloadFileAsync()
{
options.ImportFilePath = xxxxxx;
_unpackFileSystem.DownloadFileAsync(bundle, options);
}
UnpackFileSystem::DownloadFileAsync()
{
string mainURL = ConvertToWWWPath(options.ImportFilePath);
options.SetURL(mainURL, mainURL);
return new DownloadPackageBundleOperation(bundle, options);
}
*/

View File

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

View File

@@ -3,18 +3,143 @@ using System.IO;
using System.Text;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.U2D;
using UnityEngine.TestTools;
using NUnit.Framework;
using YooAsset;
public class T3_TestCacheFileSystem : IPrebuildSetup, IPostBuildCleanup
{
public void Setup()
{
throw new NotImplementedException();
}
public void Cleanup()
{
throw new NotImplementedException();
}
[UnityTest]
public IEnumerator A_InitializePackage()
{
// 清空旧的缓存目录
string projectPath = Path.GetDirectoryName(Application.dataPath);
string cacheRoot = $"{projectPath}/yoo";
Directory.Delete(cacheRoot, true);
// 拷贝打包资源到本地服务器
{
string packageRoot = string.Empty;
#if UNITY_EDITOR
packageRoot = UnityEditor.EditorPrefs.GetString(T2_TestBuldinFileSystem.ASSET_BUNDLE_PACKAGE_ROOT_KEY);
#endif
if (Directory.Exists(packageRoot) == false)
throw new Exception($"Not found package root : {packageRoot}");
string testServerDirectory = "C://xampp/htdocs/CDN/Android/Test";
CopyDirectory(packageRoot, testServerDirectory);
}
// 初始化资源包 ASSET_BUNDLE
{
var package = YooAssets.CreatePackage(TestDefine.AssetBundlePackageName);
// 初始化资源包
var initParams = new HostPlayModeParameters();
var fileDecryption = new TestFileStreamDecryption();
var manifestProcess = new TestProcessManifest();
string hostServerIP = "http://127.0.0.1/CDN/Android/Test/";
var remoteServices = new TestRemoteServices(hostServerIP);
initParams.BuildinFileSystemParameters = null;
initParams.CacheFileSystemParameters = FileSystemParameters.CreateDefaultCacheFileSystemParameters(remoteServices, fileDecryption);
initParams.CacheFileSystemParameters.AddParameter(FileSystemParametersDefine.MANIFEST_SERVICES, manifestProcess);
var initializeOp = package.InitializeAsync(initParams);
yield return initializeOp;
if (initializeOp.Status != EOperationStatus.Succeed)
Debug.LogError(initializeOp.Error);
Assert.AreEqual(EOperationStatus.Succeed, initializeOp.Status);
// 请求资源版本
var requetVersionOp = package.RequestPackageVersionAsync();
yield return requetVersionOp;
if (requetVersionOp.Status != EOperationStatus.Succeed)
Debug.LogError(requetVersionOp.Error);
Assert.AreEqual(EOperationStatus.Succeed, requetVersionOp.Status);
// 更新资源清单
var updateManifestOp = package.UpdatePackageManifestAsync(requetVersionOp.PackageVersion);
yield return updateManifestOp;
if (updateManifestOp.Status != EOperationStatus.Succeed)
Debug.LogError(updateManifestOp.Error);
Assert.AreEqual(EOperationStatus.Succeed, updateManifestOp.Status);
}
}
private class TestRemoteServices : IRemoteServices
{
private readonly string _localServerRoot;
public TestRemoteServices(string localServerRoot)
{
_localServerRoot = localServerRoot;
}
string IRemoteServices.GetRemoteMainURL(string fileName)
{
return $"{_localServerRoot}/{fileName}";
}
string IRemoteServices.GetRemoteFallbackURL(string fileName)
{
return $"{_localServerRoot}/{fileName}";
}
}
[UnityTest]
public IEnumerator B1_TestBundlePlaying()
{
var tester = new TestBundlePlaying();
yield return tester.RuntimeTester();
}
[UnityTest]
public IEnumerator B2_TestBundleImporter()
{
var tester = new TestBundleImporter();
yield return tester.RuntimeTester();
}
[UnityTest]
public IEnumerator B3_TestBundleDownloader()
{
var tester = new TestBundleDownloader();
yield return tester.RuntimeTester();
}
private static void CopyDirectory(string sourceDir, string targetDir)
{
// 检查源目录是否存在
if (!Directory.Exists(sourceDir))
{
throw new DirectoryNotFoundException($"源目录不存在: {sourceDir}");
}
// 创建目标目录(如果不存在)
Directory.CreateDirectory(targetDir);
// 拷贝所有文件
foreach (string file in Directory.GetFiles(sourceDir))
{
string fileName = Path.GetFileName(file);
string destFile = Path.Combine(targetDir, fileName);
File.Copy(file, destFile, true); // true 表示覆盖已存在文件
}
// 递归拷贝子目录
foreach (string subDir in Directory.GetDirectories(sourceDir))
{
string dirName = Path.GetFileName(subDir);
string newTargetDir = Path.Combine(targetDir, dirName);
CopyDirectory(subDir, newTargetDir);
}
}
}

View File

@@ -0,0 +1,47 @@
using System;
using System.IO;
using System.Text;
using System.Collections;
using UnityEngine;
using NUnit.Framework;
using YooAsset;
/// <summary>
/// 测试远端文件下载
/// </summary>
public class TestBundleDownloader
{
public IEnumerator RuntimeTester()
{
ResourcePackage package = YooAssets.GetPackage(TestDefine.AssetBundlePackageName);
Assert.IsNotNull(package);
var downloader = package.CreateResourceDownloader(10, 1);
if (downloader.TotalDownloadCount == 0)
{
Assert.Fail($"Test downloader not found any file !");
}
downloader.BeginDownload();
downloader.DownloadFinishCallback = (DownloaderFinishData data) =>
{
if (data.Succeed == false)
Assert.Fail($"Test downloader failed ! {downloader.Error}");
};
// 等待一秒
yield return new WaitForSeconds(1f);
}
}
/* 资源代码流程
* 远端文件下载(下载器)
CacheFileSystem::DownloadFileAsync()
{
//RemoteServices返回CDN文件路径
string mainURL = RemoteServices.GetRemoteMainURL(bundle.FileName);
string fallbackURL = RemoteServices.GetRemoteFallbackURL(bundle.FileName);
options.SetURL(mainURL, fallbackURL);
return new DownloadPackageBundleOperation(bundle, options);
}
*/

View File

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

View File

@@ -0,0 +1,57 @@
using System;
using System.IO;
using System.Text;
using System.Collections;
using UnityEngine;
using NUnit.Framework;
using YooAsset;
/// <summary>
/// 测试本地文件导入
/// </summary>
public class TestBundleImporter
{
public IEnumerator RuntimeTester()
{
ResourcePackage package = YooAssets.GetPackage(TestDefine.AssetBundlePackageName);
Assert.IsNotNull(package);
string packageRoot = UnityEditor.EditorPrefs.GetString(T2_TestBuldinFileSystem.ASSET_BUNDLE_PACKAGE_ROOT_KEY);
DirectoryInfo packageDir = new DirectoryInfo(packageRoot);
string fileRoot = $"{packageDir.Parent.FullName}/OutputCache";
ImportFileInfo fileInfoA = new ImportFileInfo();
fileInfoA.FilePath = $"{fileRoot}/assets_samples_test_sample_testres3_import_prefab_importa.bundle.encrypt";
fileInfoA.BundleName = "assets_samples_test_sample_testres3_import_prefab_importa.bundle";
ImportFileInfo fileInfoB = new ImportFileInfo();
fileInfoB.FilePath = $"{fileRoot}/assets_samples_test_sample_testres3_import_prefab_importb.bundle.encrypt";
fileInfoB.BundleName = "assets_samples_test_sample_testres3_import_prefab_importb.bundle";
ImportFileInfo[] importInfos = { fileInfoA, fileInfoB };
var unpacker = package.CreateResourceImporter(importInfos, 10, 1);
Assert.AreEqual(unpacker.TotalDownloadCount, 2);
unpacker.BeginDownload();
yield return unpacker;
Assert.AreEqual(EOperationStatus.Succeed, unpacker.Status);
// 等待一秒
yield return new WaitForSeconds(1f);
}
}
/* 资源代码流程
* 本地文件导入(导入器)
BundleInfo::CreateDownloader()
{
options.ImportFilePath = _importFilePath;
return _cacheFileSystem.DownloadFileAsync(Bundle, options);
}
CacheFileSystem::DownloadFileAsync()
{
string mainURL = ConvertToWWWPath(options.ImportFilePath);
options.SetURL(mainURL, mainURL);
return new DownloadPackageBundleOperation(bundle, options);
}
*/

View File

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

View File

@@ -0,0 +1,79 @@
using System;
using System.IO;
using System.Text;
using System.Collections;
using UnityEngine;
using NUnit.Framework;
using YooAsset;
/// <summary>
/// 测试边玩边下
/// </summary>
public class TestBundlePlaying
{
public IEnumerator RuntimeTester()
{
ResourcePackage package = YooAssets.GetPackage(TestDefine.AssetBundlePackageName);
Assert.IsNotNull(package);
if (package.IsNeedDownloadFromRemote("panel_a") == false)
{
Assert.Fail("Load bundle is already existed !");
}
if (package.IsNeedDownloadFromRemote("panel_b") == false)
{
Assert.Fail("Load bundle is already existed !");
}
// 测试异步加载
{
var assetsHandle = package.LoadAssetAsync<GameObject>("panel_a");
yield return assetsHandle;
Assert.AreEqual(EOperationStatus.Succeed, assetsHandle.Status);
}
// 测试同步加载
{
// 验证失败结果
UnityEngine.TestTools.LogAssert.ignoreFailingMessages = true;
var assetsHandle = package.LoadAssetSync<GameObject>("panel_b");
yield return assetsHandle;
Assert.AreEqual(EOperationStatus.Failed, assetsHandle.Status);
UnityEngine.TestTools.LogAssert.ignoreFailingMessages = false;
// 清理加载器
assetsHandle.Release();
package.UnloadUnusedAssetsAsync();
// 验证成功结果
yield return new WaitForSeconds(1f);
assetsHandle = package.LoadAssetSync<GameObject>("panel_b");
yield return assetsHandle;
Assert.AreEqual(EOperationStatus.Succeed, assetsHandle.Status);
}
// 等待一秒
yield return new WaitForSeconds(1f);
}
}
/* 资源代码流程
* 远端文件下载(加载器)
CacheFileSystem::LoadBundleFile()
{
_cacheFileSystem.LoadBundleFile(bundle);
}
CacheFileSystem::LoadAssetBundleOperation()
{
DownloadFileOptions options = new DownloadFileOptions(int.MaxValue, 60);
_cacheFileSystem.DownloadFileAsync(_bundle, options);
}
CacheFileSystem::DownloadFileAsync()
{
//RemoteServices返回CDN文件路径
string mainURL = RemoteServices.GetRemoteMainURL(bundle.FileName);
string fallbackURL = RemoteServices.GetRemoteFallbackURL(bundle.FileName);
options.SetURL(mainURL, fallbackURL);
return new DownloadPackageBundleOperation(bundle, options);
}
*/

View File

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

View File

@@ -1,6 +1,6 @@

public class TestDefine
{
public const string AssetBundlePackageName = "AssetBundlePackage";
public const string RawBundlePackageName = "RawBundlePackage";
public const string AssetBundlePackageName = "AssetBundleTestPackage";
public const string RawBundlePackageName = "RawBundleTestPackage";
}

View File

@@ -35,6 +35,7 @@ public class TestLoadPanel
Assert.IsNotNull(instantiateOp.Result);
TestLogger.Log(this, instantiateOp.Result.name);
GameObject.Destroy(instantiateOp.Result);
}
}
}

View File

@@ -42,6 +42,7 @@ public class TestLoadPrefab
Assert.IsNotNull(instantiateOp.Result);
TestLogger.Log(this, instantiateOp.Result.name);
GameObject.Destroy(instantiateOp.Result);
}
// 同步加载指定预制体
@@ -57,6 +58,7 @@ public class TestLoadPrefab
Assert.IsNotNull(instantiateOp.Result);
TestLogger.Log(this, instantiateOp.Result.name);
GameObject.Destroy(instantiateOp.Result);
}
}
}

View File

@@ -36,10 +36,6 @@ SpriteAtlas:
bindAsDefault: 1
isAtlasV2: 0
cachedData: {fileID: 0}
packedSpriteRenderDataKeys:
- 379c45e0c5ba5e54e8df03f52e4c7680: 21300000
- fcab35236e33438448805a9211b0cc19: 21300000
- 3b8db7241fc8ff54e9dea6fc64cfd7e5: 21300000
m_MasterAtlas: {fileID: 0}
m_PackedSprites:
- {fileID: 21300000, guid: 379c45e0c5ba5e54e8df03f52e4c7680, type: 3}

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 8125b69e669e19e41a4c67896f1f98b0
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 80285bcc262cc36488d96950c2a07504
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 35f454fb80a715047bcf0ce30c7c4f18
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

Some files were not shown because too many files have changed in this diff Show More