mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
TEngine全面更新,升级YooAsset2.1.1、UniTask、UIWindow、I2Localization
TEngine全面更新,升级YooAsset2.1.1、UniTask、UIWindow、I2Localization
This commit is contained in:
@@ -0,0 +1,145 @@
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal class AssetBundleWebRequest : IWebRequester
|
||||
{
|
||||
private UnityWebRequest _webRequest;
|
||||
private DownloadHandlerAssetBundle _downloadhandler;
|
||||
private AssetBundle _cacheAssetBundle;
|
||||
private bool _getAssetBundle = false;
|
||||
|
||||
public ERequestStatus Status { private set; get; } = ERequestStatus.None;
|
||||
public float DownloadProgress { private set; get; }
|
||||
public ulong DownloadedBytes { private set; get; }
|
||||
public string RequestNetError { private set; get; }
|
||||
public long RequestHttpCode { private set; get; }
|
||||
|
||||
public AssetBundleWebRequest() { }
|
||||
public void Create(string requestURL, BundleInfo bundleInfo, params object[] args)
|
||||
{
|
||||
if (Status != ERequestStatus.None)
|
||||
throw new System.Exception("Should never get here !");
|
||||
|
||||
if (args.Length == 0)
|
||||
throw new System.Exception("Not found param value");
|
||||
|
||||
// 解析附加参数
|
||||
_getAssetBundle = (bool)args[0];
|
||||
|
||||
// 创建下载器
|
||||
_webRequest = DownloadHelper.NewRequest(requestURL);
|
||||
if (CacheHelper.DisableUnityCacheOnWebGL)
|
||||
{
|
||||
uint crc = bundleInfo.Bundle.UnityCRC;
|
||||
_downloadhandler = new DownloadHandlerAssetBundle(requestURL, crc);
|
||||
}
|
||||
else
|
||||
{
|
||||
uint crc = bundleInfo.Bundle.UnityCRC;
|
||||
var hash = Hash128.Parse(bundleInfo.Bundle.FileHash);
|
||||
_downloadhandler = new DownloadHandlerAssetBundle(requestURL, hash, crc);
|
||||
}
|
||||
#if UNITY_2020_3_OR_NEWER
|
||||
_downloadhandler.autoLoadAssetBundle = false;
|
||||
#endif
|
||||
_webRequest.downloadHandler = _downloadhandler;
|
||||
_webRequest.disposeDownloadHandlerOnDispose = true;
|
||||
_webRequest.SendWebRequest();
|
||||
Status = ERequestStatus.InProgress;
|
||||
}
|
||||
public void Update()
|
||||
{
|
||||
if (Status == ERequestStatus.None)
|
||||
return;
|
||||
if (IsDone())
|
||||
return;
|
||||
|
||||
DownloadProgress = _webRequest.downloadProgress;
|
||||
DownloadedBytes = _webRequest.downloadedBytes;
|
||||
if (_webRequest.isDone == false)
|
||||
return;
|
||||
|
||||
// 检查网络错误
|
||||
#if UNITY_2020_3_OR_NEWER
|
||||
RequestHttpCode = _webRequest.responseCode;
|
||||
if (_webRequest.result != UnityWebRequest.Result.Success)
|
||||
{
|
||||
RequestNetError = _webRequest.error;
|
||||
Status = ERequestStatus.Error;
|
||||
}
|
||||
else
|
||||
{
|
||||
Status = ERequestStatus.Success;
|
||||
}
|
||||
#else
|
||||
RequestHttpCode = _webRequest.responseCode;
|
||||
if (_webRequest.isNetworkError || _webRequest.isHttpError)
|
||||
{
|
||||
RequestNetError = _webRequest.error;
|
||||
Status = ERequestStatus.Error;
|
||||
}
|
||||
else
|
||||
{
|
||||
Status = ERequestStatus.Success;
|
||||
}
|
||||
#endif
|
||||
|
||||
// 缓存加载的AssetBundle对象
|
||||
if (Status == ERequestStatus.Success)
|
||||
{
|
||||
if (_getAssetBundle)
|
||||
{
|
||||
_cacheAssetBundle = _downloadhandler.assetBundle;
|
||||
if (_cacheAssetBundle == null)
|
||||
{
|
||||
RequestNetError = "assetBundle is null";
|
||||
Status = ERequestStatus.Error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 最终释放下载器
|
||||
DisposeWebRequest();
|
||||
}
|
||||
public void Abort()
|
||||
{
|
||||
// 如果下载任务还未开始
|
||||
if (Status == ERequestStatus.None)
|
||||
{
|
||||
RequestNetError = "user cancel";
|
||||
Status = ERequestStatus.Error;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 注意:为了防止同一个文件强制停止之后立马创建新的请求,应该让进行中的请求自然终止。
|
||||
if (_webRequest != null)
|
||||
{
|
||||
if (_webRequest.isDone == false)
|
||||
_webRequest.Abort(); // If in progress, halts the UnityWebRequest as soon as possible.
|
||||
}
|
||||
}
|
||||
}
|
||||
public bool IsDone()
|
||||
{
|
||||
if (Status == ERequestStatus.Success || Status == ERequestStatus.Error)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
public object GetRequestObject()
|
||||
{
|
||||
return _cacheAssetBundle;
|
||||
}
|
||||
private void DisposeWebRequest()
|
||||
{
|
||||
if (_webRequest != null)
|
||||
{
|
||||
_webRequest.Dispose();
|
||||
_webRequest = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 34efa85440da15247b570a98b0971282
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,87 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
/// <summary>
|
||||
/// 支持Unity2018版本的断点续传下载器
|
||||
/// </summary>
|
||||
internal class DownloadHandlerFileRange : DownloadHandlerScript
|
||||
{
|
||||
private string _fileSavePath;
|
||||
private long _fileTotalSize;
|
||||
private UnityWebRequest _webRequest;
|
||||
private FileStream _fileStream;
|
||||
|
||||
private long _localFileSize = 0;
|
||||
private long _curFileSize = 0;
|
||||
|
||||
|
||||
public DownloadHandlerFileRange(string fileSavePath, long fileTotalSize, UnityWebRequest webRequest) : base(new byte[1024 * 1024])
|
||||
{
|
||||
_fileSavePath = fileSavePath;
|
||||
_fileTotalSize = fileTotalSize;
|
||||
_webRequest = webRequest;
|
||||
|
||||
if (File.Exists(fileSavePath))
|
||||
{
|
||||
FileInfo fileInfo = new FileInfo(fileSavePath);
|
||||
_localFileSize = fileInfo.Length;
|
||||
}
|
||||
|
||||
_fileStream = new FileStream(_fileSavePath, FileMode.Append, FileAccess.Write);
|
||||
_curFileSize = _localFileSize;
|
||||
}
|
||||
protected override bool ReceiveData(byte[] data, int dataLength)
|
||||
{
|
||||
if (data == null || dataLength == 0 || _webRequest.responseCode >= 400)
|
||||
return false;
|
||||
|
||||
if (_fileStream == null)
|
||||
return false;
|
||||
|
||||
_fileStream.Write(data, 0, dataLength);
|
||||
_curFileSize += dataLength;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// UnityWebRequest.downloadHandler.data
|
||||
/// </summary>
|
||||
protected override byte[] GetData()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// UnityWebRequest.downloadHandler.text
|
||||
/// </summary>
|
||||
protected override string GetText()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// UnityWebRequest.downloadProgress
|
||||
/// </summary>
|
||||
protected override float GetProgress()
|
||||
{
|
||||
return _fileTotalSize == 0 ? 0 : ((float)_curFileSize) / _fileTotalSize;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 释放下载句柄
|
||||
/// </summary>
|
||||
public void Cleanup()
|
||||
{
|
||||
if (_fileStream != null)
|
||||
{
|
||||
_fileStream.Flush();
|
||||
_fileStream.Dispose();
|
||||
_fileStream = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 94254ab8e4496214884c11a891c131c6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,107 @@
|
||||
using System.IO;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal class FileGeneralRequest : IWebRequester
|
||||
{
|
||||
private UnityWebRequest _webRequest;
|
||||
|
||||
public ERequestStatus Status { private set; get; } = ERequestStatus.None;
|
||||
public float DownloadProgress { private set; get; }
|
||||
public ulong DownloadedBytes { private set; get; }
|
||||
public string RequestNetError { private set; get; }
|
||||
public long RequestHttpCode { private set; get; }
|
||||
|
||||
public FileGeneralRequest() { }
|
||||
public void Create(string requestURL, BundleInfo bundleInfo, params object[] args)
|
||||
{
|
||||
if (Status != ERequestStatus.None)
|
||||
throw new System.Exception("Should never get here !");
|
||||
|
||||
string tempFilePath = bundleInfo.TempDataFilePath;
|
||||
|
||||
// 删除临时文件
|
||||
if (File.Exists(tempFilePath))
|
||||
File.Delete(tempFilePath);
|
||||
|
||||
// 创建下载器
|
||||
_webRequest = DownloadHelper.NewRequest(requestURL);
|
||||
DownloadHandlerFile handler = new DownloadHandlerFile(tempFilePath);
|
||||
handler.removeFileOnAbort = true;
|
||||
_webRequest.downloadHandler = handler;
|
||||
_webRequest.disposeDownloadHandlerOnDispose = true;
|
||||
_webRequest.SendWebRequest();
|
||||
Status = ERequestStatus.InProgress;
|
||||
}
|
||||
public void Update()
|
||||
{
|
||||
if (Status == ERequestStatus.None)
|
||||
return;
|
||||
if (IsDone())
|
||||
return;
|
||||
|
||||
DownloadProgress = _webRequest.downloadProgress;
|
||||
DownloadedBytes = _webRequest.downloadedBytes;
|
||||
if (_webRequest.isDone == false)
|
||||
return;
|
||||
|
||||
// 检查网络错误
|
||||
#if UNITY_2020_3_OR_NEWER
|
||||
RequestHttpCode = _webRequest.responseCode;
|
||||
if (_webRequest.result != UnityWebRequest.Result.Success)
|
||||
{
|
||||
RequestNetError = _webRequest.error;
|
||||
Status = ERequestStatus.Error;
|
||||
}
|
||||
else
|
||||
{
|
||||
Status = ERequestStatus.Success;
|
||||
}
|
||||
#else
|
||||
RequestHttpCode = _webRequest.responseCode;
|
||||
if (_webRequest.isNetworkError || _webRequest.isHttpError)
|
||||
{
|
||||
RequestNetError = _webRequest.error;
|
||||
Status = ERequestStatus.Error;
|
||||
}
|
||||
else
|
||||
{
|
||||
Status = ERequestStatus.Success;
|
||||
}
|
||||
#endif
|
||||
|
||||
// 最终释放下载器
|
||||
DisposeWebRequest();
|
||||
}
|
||||
public void Abort()
|
||||
{
|
||||
DisposeWebRequest();
|
||||
if (IsDone() == false)
|
||||
{
|
||||
RequestNetError = "user abort";
|
||||
RequestHttpCode = 0;
|
||||
Status = ERequestStatus.Error;
|
||||
}
|
||||
}
|
||||
public bool IsDone()
|
||||
{
|
||||
if (Status == ERequestStatus.Success || Status == ERequestStatus.Error)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
public object GetRequestObject()
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
private void DisposeWebRequest()
|
||||
{
|
||||
if (_webRequest != null)
|
||||
{
|
||||
_webRequest.Dispose(); //注意:引擎底层会自动调用Abort方法
|
||||
_webRequest = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b2f339136a4269343a3b5ab80450b889
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,150 @@
|
||||
using System.IO;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal class FileResumeRequest : IWebRequester
|
||||
{
|
||||
private string _tempFilePath;
|
||||
private UnityWebRequest _webRequest;
|
||||
private DownloadHandlerFileRange _downloadHandle;
|
||||
private ulong _fileOriginLength = 0;
|
||||
|
||||
public ERequestStatus Status { private set; get; } = ERequestStatus.None;
|
||||
public float DownloadProgress { private set; get; }
|
||||
public ulong DownloadedBytes { private set; get; }
|
||||
public string RequestNetError { private set; get; }
|
||||
public long RequestHttpCode { private set; get; }
|
||||
|
||||
public FileResumeRequest() { }
|
||||
public void Create(string requestURL, BundleInfo bundleInfo, params object[] args)
|
||||
{
|
||||
if (Status != ERequestStatus.None)
|
||||
throw new System.Exception("Should never get here !");
|
||||
|
||||
_tempFilePath = bundleInfo.TempDataFilePath;
|
||||
long fileBytes = bundleInfo.Bundle.FileSize;
|
||||
|
||||
// 获取下载的起始位置
|
||||
long fileLength = -1;
|
||||
if (File.Exists(_tempFilePath))
|
||||
{
|
||||
FileInfo fileInfo = new FileInfo(_tempFilePath);
|
||||
fileLength = fileInfo.Length;
|
||||
_fileOriginLength = (ulong)fileLength;
|
||||
DownloadedBytes = _fileOriginLength;
|
||||
}
|
||||
|
||||
// 检测下载起始位置是否有效
|
||||
if (fileLength >= fileBytes)
|
||||
{
|
||||
if (File.Exists(_tempFilePath))
|
||||
File.Delete(_tempFilePath);
|
||||
}
|
||||
|
||||
// 创建下载器
|
||||
_webRequest = DownloadHelper.NewRequest(requestURL);
|
||||
#if UNITY_2019_4_OR_NEWER
|
||||
var handler = new DownloadHandlerFile(_tempFilePath, true);
|
||||
handler.removeFileOnAbort = false;
|
||||
#else
|
||||
var handler = new DownloadHandlerFileRange(tempFilePath, _bundleInfo.Bundle.FileSize, _webRequest);
|
||||
_downloadHandle = handler;
|
||||
#endif
|
||||
_webRequest.downloadHandler = handler;
|
||||
_webRequest.disposeDownloadHandlerOnDispose = true;
|
||||
if (fileLength > 0)
|
||||
_webRequest.SetRequestHeader("Range", $"bytes={fileLength}-");
|
||||
_webRequest.SendWebRequest();
|
||||
Status = ERequestStatus.InProgress;
|
||||
}
|
||||
public void Update()
|
||||
{
|
||||
if (Status == ERequestStatus.None)
|
||||
return;
|
||||
if (IsDone())
|
||||
return;
|
||||
|
||||
DownloadProgress = _webRequest.downloadProgress;
|
||||
DownloadedBytes = _fileOriginLength + _webRequest.downloadedBytes;
|
||||
if (_webRequest.isDone == false)
|
||||
return;
|
||||
|
||||
// 检查网络错误
|
||||
#if UNITY_2020_3_OR_NEWER
|
||||
RequestHttpCode = _webRequest.responseCode;
|
||||
if (_webRequest.result != UnityWebRequest.Result.Success)
|
||||
{
|
||||
RequestNetError = _webRequest.error;
|
||||
Status = ERequestStatus.Error;
|
||||
}
|
||||
else
|
||||
{
|
||||
Status = ERequestStatus.Success;
|
||||
}
|
||||
#else
|
||||
RequestHttpCode = _webRequest.responseCode;
|
||||
if (_webRequest.isNetworkError || _webRequest.isHttpError)
|
||||
{
|
||||
RequestNetError = _webRequest.error;
|
||||
Status = ERequestStatus.Error;
|
||||
}
|
||||
else
|
||||
{
|
||||
Status = ERequestStatus.Success;
|
||||
}
|
||||
#endif
|
||||
|
||||
// 注意:下载断点续传文件发生特殊错误码之后删除文件
|
||||
if (Status == ERequestStatus.Error)
|
||||
{
|
||||
if (DownloadHelper.ClearFileResponseCodes != null)
|
||||
{
|
||||
if (DownloadHelper.ClearFileResponseCodes.Contains(RequestHttpCode))
|
||||
{
|
||||
if (File.Exists(_tempFilePath))
|
||||
File.Delete(_tempFilePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 最终释放下载器
|
||||
DisposeWebRequest();
|
||||
}
|
||||
public void Abort()
|
||||
{
|
||||
DisposeWebRequest();
|
||||
if (IsDone() == false)
|
||||
{
|
||||
RequestNetError = "user abort";
|
||||
RequestHttpCode = 0;
|
||||
Status = ERequestStatus.Error;
|
||||
}
|
||||
}
|
||||
public bool IsDone()
|
||||
{
|
||||
if (Status == ERequestStatus.Success || Status == ERequestStatus.Error)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
public object GetRequestObject()
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
private void DisposeWebRequest()
|
||||
{
|
||||
if (_downloadHandle != null)
|
||||
{
|
||||
_downloadHandle.Cleanup();
|
||||
_downloadHandle = null;
|
||||
}
|
||||
|
||||
if (_webRequest != null)
|
||||
{
|
||||
_webRequest.Dispose();
|
||||
_webRequest = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 816bf5396d7570e4ab28f1af407bc10f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,65 @@
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal enum ERequestStatus
|
||||
{
|
||||
None,
|
||||
InProgress,
|
||||
Error,
|
||||
Success,
|
||||
}
|
||||
|
||||
internal interface IWebRequester
|
||||
{
|
||||
/// <summary>
|
||||
/// 任务状态
|
||||
/// </summary>
|
||||
public ERequestStatus Status { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 下载进度(0f~1f)
|
||||
/// </summary>
|
||||
public float DownloadProgress { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 已经下载的总字节数
|
||||
/// </summary>
|
||||
public ulong DownloadedBytes { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 返回的网络错误
|
||||
/// </summary>
|
||||
public string RequestNetError { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 返回的HTTP CODE
|
||||
/// </summary>
|
||||
public long RequestHttpCode { get; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 创建任务
|
||||
/// </summary>
|
||||
public void Create(string url, BundleInfo bundleInfo, params object[] args);
|
||||
|
||||
/// <summary>
|
||||
/// 更新任务
|
||||
/// </summary>
|
||||
public void Update();
|
||||
|
||||
/// <summary>
|
||||
/// 终止任务
|
||||
/// </summary>
|
||||
public void Abort();
|
||||
|
||||
/// <summary>
|
||||
/// 是否已经完成(无论成功或失败)
|
||||
/// </summary>
|
||||
public bool IsDone();
|
||||
|
||||
/// <summary>
|
||||
/// 获取请求的对象
|
||||
/// </summary>
|
||||
public object GetRequestObject();
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4d80b965d56870b43af1bf6943015914
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user