Update DownloadSystem
This commit is contained in:
@@ -1,8 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 715017c3b0bb2a3429d8f021b0d9a2b2
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,145 +0,0 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
/// <summary>
|
||||
/// 1. 保证每一时刻资源文件只存在一个下载器
|
||||
/// 2. 保证下载器下载完成后立刻验证并缓存
|
||||
/// 3. 保证资源文件不会被重复下载
|
||||
/// </summary>
|
||||
internal static class DownloadSystem
|
||||
{
|
||||
private static readonly Dictionary<string, FileDownloader> _downloaderDic = new Dictionary<string, FileDownloader>();
|
||||
private static readonly List<string> _removeList = new List<string>(100);
|
||||
private static readonly Dictionary<string, string> _cachedHashList = new Dictionary<string, string>(1000);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 更新所有下载器
|
||||
/// </summary>
|
||||
public static void Update()
|
||||
{
|
||||
// 更新下载器
|
||||
_removeList.Clear();
|
||||
foreach (var valuePair in _downloaderDic)
|
||||
{
|
||||
var downloader = valuePair.Value;
|
||||
downloader.Update();
|
||||
if (downloader.IsDone())
|
||||
_removeList.Add(valuePair.Key);
|
||||
}
|
||||
|
||||
// 移除下载器
|
||||
foreach (var key in _removeList)
|
||||
{
|
||||
_downloaderDic.Remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 开始下载资源文件
|
||||
/// 注意:只有第一次请求的参数才是有效的
|
||||
/// </summary>
|
||||
public static FileDownloader BeginDownload(BundleInfo bundleInfo, int failedTryAgain, int timeout = 60)
|
||||
{
|
||||
// 查询存在的下载器
|
||||
if (_downloaderDic.TryGetValue(bundleInfo.Hash, out var downloader))
|
||||
{
|
||||
return downloader;
|
||||
}
|
||||
|
||||
// 如果资源已经缓存
|
||||
if(ContainsVerifyFile(bundleInfo.Hash))
|
||||
{
|
||||
var newDownloader = new FileDownloader(bundleInfo);
|
||||
newDownloader.SetDone();
|
||||
return newDownloader;
|
||||
}
|
||||
|
||||
// 创建新的下载器
|
||||
{
|
||||
YooLogger.Log($"Beginning to download file : {bundleInfo.BundleName} URL : {bundleInfo.RemoteMainURL}");
|
||||
FileUtility.CreateFileDirectory(bundleInfo.LocalPath);
|
||||
var newDownloader = new FileDownloader(bundleInfo);
|
||||
newDownloader.SendRequest(failedTryAgain, timeout);
|
||||
_downloaderDic.Add(bundleInfo.Hash, newDownloader);
|
||||
return newDownloader;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取下载器的总数
|
||||
/// </summary>
|
||||
public static int GetDownloaderTotalCount()
|
||||
{
|
||||
return _downloaderDic.Count;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询是否为验证文件
|
||||
/// 注意:被收录的文件完整性是绝对有效的
|
||||
/// </summary>
|
||||
public static bool ContainsVerifyFile(string hash)
|
||||
{
|
||||
if (_cachedHashList.ContainsKey(hash))
|
||||
{
|
||||
string filePath = SandboxHelper.MakeSandboxCacheFilePath(hash);
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
string bundleName = _cachedHashList[hash];
|
||||
_cachedHashList.Remove(hash);
|
||||
YooLogger.Error($"Cache file is missing : {bundleName} Hash : {hash}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 缓存验证过的文件
|
||||
/// </summary>
|
||||
public static void CacheVerifyFile(string hash, string bundleName)
|
||||
{
|
||||
if (_cachedHashList.ContainsKey(hash) == false)
|
||||
{
|
||||
YooLogger.Log($"Cache verify file : {bundleName} Hash : {hash}");
|
||||
_cachedHashList.Add(hash, bundleName);
|
||||
}
|
||||
}
|
||||
|
||||
// 验证文件完整性
|
||||
public static bool CheckContentIntegrity(BundleInfo bundleInfo)
|
||||
{
|
||||
return CheckContentIntegrity(bundleInfo.LocalPath, bundleInfo.SizeBytes, bundleInfo.CRC);
|
||||
}
|
||||
public static bool CheckContentIntegrity(PatchBundle patchBundle)
|
||||
{
|
||||
string filePath = SandboxHelper.MakeSandboxCacheFilePath(patchBundle.Hash);
|
||||
return CheckContentIntegrity(filePath, patchBundle.SizeBytes, patchBundle.CRC);
|
||||
}
|
||||
public static bool CheckContentIntegrity(string filePath, long size, string crc)
|
||||
{
|
||||
if (File.Exists(filePath) == false)
|
||||
return false;
|
||||
|
||||
// 先验证文件大小
|
||||
long fileSize = FileUtility.GetFileSize(filePath);
|
||||
if (fileSize != size)
|
||||
return false;
|
||||
|
||||
// 再验证文件CRC
|
||||
string fileCRC = HashUtility.FileCRC32(filePath);
|
||||
return fileCRC == crc;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d19eae1f43ecf6347925a06730a6c846
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,250 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal class FileDownloader
|
||||
{
|
||||
private enum ESteps
|
||||
{
|
||||
None,
|
||||
CreateDownload,
|
||||
CheckDownload,
|
||||
TryAgain,
|
||||
Succeed,
|
||||
Failed,
|
||||
}
|
||||
|
||||
private readonly BundleInfo _bundleInfo;
|
||||
private UnityWebRequest _webRequest;
|
||||
private UnityWebRequestAsyncOperation _operationHandle;
|
||||
|
||||
private ESteps _steps = ESteps.None;
|
||||
private string _lastError = string.Empty;
|
||||
|
||||
private int _timeout;
|
||||
private int _failedTryAgain;
|
||||
private int _requestCount;
|
||||
private string _requestURL;
|
||||
|
||||
// 重置变量
|
||||
private bool _isAbort = false;
|
||||
private ulong _latestDownloadBytes;
|
||||
private float _latestDownloadRealtime;
|
||||
private float _tryAgainTimer;
|
||||
|
||||
/// <summary>
|
||||
/// 下载进度(0-100f)
|
||||
/// </summary>
|
||||
public float DownloadProgress { private set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 已经下载的总字节数
|
||||
/// </summary>
|
||||
public ulong DownloadedBytes { private set; get; }
|
||||
|
||||
|
||||
internal FileDownloader(BundleInfo bundleInfo)
|
||||
{
|
||||
_bundleInfo = bundleInfo;
|
||||
}
|
||||
internal void SendRequest(int failedTryAgain, int timeout)
|
||||
{
|
||||
if (string.IsNullOrEmpty(_bundleInfo.LocalPath))
|
||||
throw new ArgumentNullException();
|
||||
|
||||
if (_steps == ESteps.None)
|
||||
{
|
||||
_failedTryAgain = failedTryAgain;
|
||||
_timeout = timeout;
|
||||
_steps = ESteps.CreateDownload;
|
||||
}
|
||||
}
|
||||
internal void Update()
|
||||
{
|
||||
if (_steps == ESteps.None)
|
||||
return;
|
||||
if (_steps == ESteps.Failed || _steps == ESteps.Succeed)
|
||||
return;
|
||||
|
||||
// 创建下载器
|
||||
if (_steps == ESteps.CreateDownload)
|
||||
{
|
||||
// 重置变量
|
||||
DownloadProgress = 0f;
|
||||
DownloadedBytes = 0;
|
||||
_isAbort = false;
|
||||
_latestDownloadBytes = 0;
|
||||
_latestDownloadRealtime = Time.realtimeSinceStartup;
|
||||
_tryAgainTimer = 0f;
|
||||
|
||||
_requestCount++;
|
||||
_requestURL = GetRequestURL();
|
||||
_webRequest = new UnityWebRequest(_requestURL, UnityWebRequest.kHttpVerbGET);
|
||||
DownloadHandlerFile handler = new DownloadHandlerFile(_bundleInfo.LocalPath);
|
||||
handler.removeFileOnAbort = true;
|
||||
_webRequest.downloadHandler = handler;
|
||||
_webRequest.disposeDownloadHandlerOnDispose = true;
|
||||
_operationHandle = _webRequest.SendWebRequest();
|
||||
_steps = ESteps.CheckDownload;
|
||||
}
|
||||
|
||||
// 检测下载结果
|
||||
if (_steps == ESteps.CheckDownload)
|
||||
{
|
||||
DownloadProgress = _webRequest.downloadProgress * 100f;
|
||||
DownloadedBytes = _webRequest.downloadedBytes;
|
||||
if (_operationHandle.isDone == false)
|
||||
{
|
||||
CheckTimeout();
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查网络错误
|
||||
bool isError = false;
|
||||
#if UNITY_2020_3_OR_NEWER
|
||||
if (_webRequest.result != UnityWebRequest.Result.Success)
|
||||
{
|
||||
isError = true;
|
||||
_lastError = _webRequest.error;
|
||||
}
|
||||
#else
|
||||
if (_webRequest.isNetworkError || _webRequest.isHttpError)
|
||||
{
|
||||
isError = true;
|
||||
_lastError = _webRequest.error;
|
||||
}
|
||||
#endif
|
||||
|
||||
// 检查文件完整性
|
||||
if (isError == false)
|
||||
{
|
||||
// 注意:如果文件验证失败需要删除文件
|
||||
if (DownloadSystem.CheckContentIntegrity(_bundleInfo) == false)
|
||||
{
|
||||
isError = true;
|
||||
_lastError = $"Verification failed";
|
||||
if (File.Exists(_bundleInfo.LocalPath))
|
||||
File.Delete(_bundleInfo.LocalPath);
|
||||
}
|
||||
}
|
||||
|
||||
if (isError)
|
||||
{
|
||||
ReportError();
|
||||
if (_failedTryAgain > 0)
|
||||
_steps = ESteps.TryAgain;
|
||||
else
|
||||
_steps = ESteps.Failed;
|
||||
}
|
||||
else
|
||||
{
|
||||
_steps = ESteps.Succeed;
|
||||
DownloadSystem.CacheVerifyFile(_bundleInfo.Hash, _bundleInfo.BundleName);
|
||||
}
|
||||
|
||||
// 释放下载器
|
||||
DisposeWebRequest();
|
||||
}
|
||||
|
||||
// 重新尝试下载
|
||||
if (_steps == ESteps.TryAgain)
|
||||
{
|
||||
_tryAgainTimer += Time.unscaledDeltaTime;
|
||||
if (_tryAgainTimer > 0.5f)
|
||||
{
|
||||
_failedTryAgain--;
|
||||
_steps = ESteps.CreateDownload;
|
||||
YooLogger.Warning($"Try again download : {_requestURL}");
|
||||
}
|
||||
}
|
||||
}
|
||||
internal void SetDone()
|
||||
{
|
||||
_steps = ESteps.Succeed;
|
||||
}
|
||||
|
||||
private string GetRequestURL()
|
||||
{
|
||||
// 轮流返回请求地址
|
||||
if (_requestCount % 2 == 0)
|
||||
return _bundleInfo.RemoteFallbackURL;
|
||||
else
|
||||
return _bundleInfo.RemoteMainURL;
|
||||
}
|
||||
private void CheckTimeout()
|
||||
{
|
||||
// 注意:在连续时间段内无新增下载数据及判定为超时
|
||||
if (_isAbort == false)
|
||||
{
|
||||
if (_latestDownloadBytes != DownloadedBytes)
|
||||
{
|
||||
_latestDownloadBytes = DownloadedBytes;
|
||||
_latestDownloadRealtime = Time.realtimeSinceStartup;
|
||||
}
|
||||
|
||||
float offset = Time.realtimeSinceStartup - _latestDownloadRealtime;
|
||||
if (offset > _timeout)
|
||||
{
|
||||
YooLogger.Warning($"Web file request timeout : {_requestURL}");
|
||||
_webRequest.Abort();
|
||||
_isAbort = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
private void DisposeWebRequest()
|
||||
{
|
||||
if (_webRequest != null)
|
||||
{
|
||||
_webRequest.Dispose();
|
||||
_webRequest = null;
|
||||
_operationHandle = null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取资源包信息
|
||||
/// </summary>
|
||||
public BundleInfo GetBundleInfo()
|
||||
{
|
||||
return _bundleInfo;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检测下载器是否已经完成(无论成功或失败)
|
||||
/// </summary>
|
||||
public bool IsDone()
|
||||
{
|
||||
return _steps == ESteps.Succeed || _steps == ESteps.Failed;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 下载过程是否发生错误
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public bool HasError()
|
||||
{
|
||||
return _steps == ESteps.Failed;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 报告错误信息
|
||||
/// </summary>
|
||||
public void ReportError()
|
||||
{
|
||||
YooLogger.Error($"Failed to download : {_requestURL} Error : {_lastError}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取最近一条错误日志
|
||||
/// </summary>
|
||||
public string GetLastError()
|
||||
{
|
||||
return _lastError;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4c6e1a8bc8d5e664395395daa772ddd7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,266 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Security;
|
||||
using System.Threading;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal class HttpDownloader
|
||||
{
|
||||
private enum ESteps
|
||||
{
|
||||
None,
|
||||
CreateDownload,
|
||||
CheckDownload,
|
||||
Succeed,
|
||||
Failed,
|
||||
}
|
||||
|
||||
private readonly BundleInfo _bundleInfo;
|
||||
private ESteps _steps = ESteps.None;
|
||||
|
||||
// 线程
|
||||
private bool _threadOver = false;
|
||||
private bool _threadResult = false;
|
||||
private string _threadError = string.Empty;
|
||||
private Thread _thread;
|
||||
|
||||
// 保留参数
|
||||
private int _timeout;
|
||||
private int _failedTryAgain;
|
||||
private int _requestCount;
|
||||
|
||||
// 下载结果
|
||||
private string _downloadError = string.Empty;
|
||||
private float _downloadProgress = 0f;
|
||||
private long _downloadBytes = 0;
|
||||
|
||||
/// <summary>
|
||||
/// 下载进度(0-100f)
|
||||
/// </summary>
|
||||
public float DownloadProgress
|
||||
{
|
||||
get
|
||||
{
|
||||
return _downloadProgress;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 已经下载的总字节数
|
||||
/// </summary>
|
||||
public long DownloadedBytes
|
||||
{
|
||||
get
|
||||
{
|
||||
return _downloadBytes;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
internal HttpDownloader(BundleInfo bundleInfo)
|
||||
{
|
||||
_bundleInfo = bundleInfo;
|
||||
}
|
||||
internal void SendRequest(int failedTryAgain, int timeout)
|
||||
{
|
||||
_failedTryAgain = failedTryAgain;
|
||||
_timeout = timeout;
|
||||
}
|
||||
internal void Update()
|
||||
{
|
||||
if (_steps == ESteps.None)
|
||||
return;
|
||||
if (_steps == ESteps.Succeed || _steps == ESteps.Failed)
|
||||
return;
|
||||
|
||||
if(_steps == ESteps.CreateDownload)
|
||||
{
|
||||
_downloadError = string.Empty;
|
||||
_downloadProgress = 0f;
|
||||
_downloadBytes = 0;
|
||||
|
||||
_threadOver = false;
|
||||
_threadResult = false;
|
||||
_threadError = string.Empty;
|
||||
_thread = new Thread(ThreadRun);
|
||||
_thread.IsBackground = true;
|
||||
_thread.Start();
|
||||
_steps = ESteps.CheckDownload;
|
||||
}
|
||||
|
||||
if(_steps == ESteps.CheckDownload)
|
||||
{
|
||||
if (_threadOver == false)
|
||||
return;
|
||||
|
||||
if(_thread != null)
|
||||
{
|
||||
_thread.Abort();
|
||||
_thread = null;
|
||||
}
|
||||
|
||||
_downloadError = _threadError;
|
||||
if (_threadResult)
|
||||
{
|
||||
DownloadSystem.CacheVerifyFile(_bundleInfo.Hash, _bundleInfo.BundleName);
|
||||
_steps = ESteps.Succeed;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 失败后重新尝试
|
||||
if(_failedTryAgain > 0)
|
||||
{
|
||||
_failedTryAgain--;
|
||||
_steps = ESteps.CreateDownload;
|
||||
}
|
||||
else
|
||||
{
|
||||
_steps = ESteps.Failed;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
internal void SetDone()
|
||||
{
|
||||
_steps = ESteps.Succeed;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取资源包信息
|
||||
/// </summary>
|
||||
public BundleInfo GetBundleInfo()
|
||||
{
|
||||
return _bundleInfo;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检测下载器是否已经完成(无论成功或失败)
|
||||
/// </summary>
|
||||
public bool IsDone()
|
||||
{
|
||||
return _steps == ESteps.Succeed || _steps == ESteps.Failed;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 下载过程是否发生错误
|
||||
/// </summary>
|
||||
public bool HasError()
|
||||
{
|
||||
return _steps == ESteps.Failed;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 报告错误信息
|
||||
/// </summary>
|
||||
public void ReportError()
|
||||
{
|
||||
YooLogger.Error(_downloadError);
|
||||
}
|
||||
|
||||
|
||||
#region 多线程下载
|
||||
public const int BufferSize = 1042 * 4;
|
||||
private void ThreadRun()
|
||||
{
|
||||
string url = GetRequestURL();
|
||||
string savePath = _bundleInfo.LocalPath;
|
||||
long fileTotalSize = _bundleInfo.SizeBytes;
|
||||
|
||||
FileStream fileStream = null;
|
||||
Stream webStream = null;
|
||||
HttpWebResponse fileResponse = null;
|
||||
|
||||
try
|
||||
{
|
||||
// 创建文件流
|
||||
fileStream = new FileStream(savePath, FileMode.OpenOrCreate, FileAccess.Write);
|
||||
long fileLength = fileStream.Length;
|
||||
|
||||
// 创建HTTP下载请求
|
||||
HttpWebRequest fileRequest = WebRequest.Create(url) as HttpWebRequest;
|
||||
fileRequest.Timeout = _timeout;
|
||||
fileRequest.ReadWriteTimeout = _timeout;
|
||||
fileRequest.ProtocolVersion = HttpVersion.Version10;
|
||||
if (fileLength > 0)
|
||||
{
|
||||
// 注意:设置远端请求文件的起始位置
|
||||
fileRequest.AddRange(fileLength);
|
||||
// 注意:设置本地文件流的起始位置
|
||||
fileStream.Seek(fileLength, SeekOrigin.Begin);
|
||||
}
|
||||
|
||||
// 读取下载数据并保存到文件
|
||||
fileResponse = fileRequest.GetResponse() as HttpWebResponse;
|
||||
webStream = fileResponse.GetResponseStream();
|
||||
byte[] buffer = new byte[BufferSize];
|
||||
while (true)
|
||||
{
|
||||
int length = webStream.Read(buffer, 0, buffer.Length);
|
||||
if (length <= 0)
|
||||
break;
|
||||
|
||||
fileStream.Write(buffer, 0, length);
|
||||
|
||||
// 计算下载进度
|
||||
// 注意:原子操作保证数据安全
|
||||
fileLength += length;
|
||||
float progress = (fileLength / fileTotalSize) * 100f;
|
||||
_downloadProgress = progress;
|
||||
_downloadBytes = fileLength;
|
||||
}
|
||||
|
||||
// 验证下载文件完整性
|
||||
bool verfiyResult = DownloadSystem.CheckContentIntegrity(savePath, _bundleInfo.SizeBytes, _bundleInfo.CRC);
|
||||
if(verfiyResult)
|
||||
{
|
||||
_threadResult = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
_threadResult = false;
|
||||
_threadError = $"Verify file content failed : {_bundleInfo.Hash}";
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_threadResult = false;
|
||||
_threadError = e.Message;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (webStream != null)
|
||||
{
|
||||
webStream.Close();
|
||||
webStream.Dispose();
|
||||
}
|
||||
|
||||
if (fileResponse != null)
|
||||
{
|
||||
fileResponse.Close();
|
||||
}
|
||||
|
||||
if (fileStream != null)
|
||||
{
|
||||
fileStream.Close();
|
||||
fileStream.Dispose();
|
||||
}
|
||||
|
||||
_threadOver = true;
|
||||
}
|
||||
}
|
||||
private string GetRequestURL()
|
||||
{
|
||||
// 轮流返回请求地址
|
||||
_requestCount++;
|
||||
if (_requestCount % 2 == 0)
|
||||
return _bundleInfo.RemoteFallbackURL;
|
||||
else
|
||||
return _bundleInfo.RemoteMainURL;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 057a358d2d0c92b47add59dac8bef783
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,38 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Threading;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
/// <summary>
|
||||
/// 同步其它线程里的回调到主线程里
|
||||
/// 注意:Unity3D中需要设置Scripting Runtime Version为.NET4.6
|
||||
/// </summary>
|
||||
internal sealed class ThreadSyncContext : SynchronizationContext
|
||||
{
|
||||
private readonly ConcurrentQueue<Action> _safeQueue = new ConcurrentQueue<Action>();
|
||||
|
||||
/// <summary>
|
||||
/// 更新同步队列
|
||||
/// </summary>
|
||||
public void Update()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
if (_safeQueue.TryDequeue(out Action action) == false)
|
||||
return;
|
||||
action.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 向同步队列里投递一个回调方法
|
||||
/// </summary>
|
||||
public override void Post(SendOrPostCallback callback, object state)
|
||||
{
|
||||
Action action = new Action(() => { callback(state); });
|
||||
_safeQueue.Enqueue(action);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1c8ce2c52a3e9964fa50a9c031e4e593
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,113 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
/// <summary>
|
||||
/// 下载器
|
||||
/// 说明:UnityWebRequest(UWR) supports reading streaming assets since 2017.1
|
||||
/// </summary>
|
||||
internal class UnityWebDataRequester
|
||||
{
|
||||
protected UnityWebRequest _webRequest;
|
||||
protected UnityWebRequestAsyncOperation _operationHandle;
|
||||
|
||||
/// <summary>
|
||||
/// 请求URL地址
|
||||
/// </summary>
|
||||
public string URL { private set; get; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 发送GET请求
|
||||
/// </summary>
|
||||
/// <param name="timeout">超时:从请求开始计时</param>
|
||||
public void SendRequest(string url, int timeout = 0)
|
||||
{
|
||||
if (_webRequest == null)
|
||||
{
|
||||
URL = url;
|
||||
_webRequest = new UnityWebRequest(URL, UnityWebRequest.kHttpVerbGET);
|
||||
DownloadHandlerBuffer handler = new DownloadHandlerBuffer();
|
||||
_webRequest.downloadHandler = handler;
|
||||
_webRequest.disposeDownloadHandlerOnDispose = true;
|
||||
_webRequest.timeout = timeout;
|
||||
_operationHandle = _webRequest.SendWebRequest();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取下载的字节数据
|
||||
/// </summary>
|
||||
public byte[] GetData()
|
||||
{
|
||||
if (_webRequest != null && IsDone())
|
||||
return _webRequest.downloadHandler.data;
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取下载的文本数据
|
||||
/// </summary>
|
||||
public string GetText()
|
||||
{
|
||||
if (_webRequest != null && IsDone())
|
||||
return _webRequest.downloadHandler.text;
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 释放下载器
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
if (_webRequest != null)
|
||||
{
|
||||
_webRequest.Dispose();
|
||||
_webRequest = null;
|
||||
_operationHandle = null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否完毕(无论成功失败)
|
||||
/// </summary>
|
||||
public bool IsDone()
|
||||
{
|
||||
if (_operationHandle == null)
|
||||
return false;
|
||||
return _operationHandle.isDone;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 下载是否发生错误
|
||||
/// </summary>
|
||||
public bool HasError()
|
||||
{
|
||||
#if UNITY_2020_3_OR_NEWER
|
||||
return _webRequest.result != UnityWebRequest.Result.Success;
|
||||
#else
|
||||
if (_webRequest.isNetworkError || _webRequest.isHttpError)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取错误信息
|
||||
/// </summary>
|
||||
public string GetError()
|
||||
{
|
||||
if (_webRequest != null)
|
||||
{
|
||||
return $"URL : {URL} Error : {_webRequest.error}";
|
||||
}
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b7557eb146572de49a1ec9b3f3c0b706
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,90 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
/// <summary>
|
||||
/// 下载器
|
||||
/// 说明:UnityWebRequest(UWR) supports reading streaming assets since 2017.1
|
||||
/// </summary>
|
||||
internal class UnityWebFileRequester
|
||||
{
|
||||
protected UnityWebRequest _webRequest;
|
||||
protected UnityWebRequestAsyncOperation _operationHandle;
|
||||
|
||||
/// <summary>
|
||||
/// 请求URL地址
|
||||
/// </summary>
|
||||
public string URL { private set; get; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 发送GET请求
|
||||
/// </summary>
|
||||
public void SendRequest(string url, string savePath)
|
||||
{
|
||||
if (_webRequest == null)
|
||||
{
|
||||
URL = url;
|
||||
_webRequest = new UnityWebRequest(URL, UnityWebRequest.kHttpVerbGET);
|
||||
DownloadHandlerFile handler = new DownloadHandlerFile(savePath);
|
||||
handler.removeFileOnAbort = true;
|
||||
_webRequest.downloadHandler = handler;
|
||||
_webRequest.disposeDownloadHandlerOnDispose = true;
|
||||
_operationHandle = _webRequest.SendWebRequest();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 释放下载器
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
if (_webRequest != null)
|
||||
{
|
||||
_webRequest.Dispose();
|
||||
_webRequest = null;
|
||||
_operationHandle = null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否完毕(无论成功失败)
|
||||
/// </summary>
|
||||
public bool IsDone()
|
||||
{
|
||||
if (_operationHandle == null)
|
||||
return false;
|
||||
return _operationHandle.isDone;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 下载是否发生错误
|
||||
/// </summary>
|
||||
public bool HasError()
|
||||
{
|
||||
#if UNITY_2020_3_OR_NEWER
|
||||
return _webRequest.result != UnityWebRequest.Result.Success;
|
||||
#else
|
||||
if (_webRequest.isNetworkError || _webRequest.isHttpError)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取错误信息
|
||||
/// </summary>
|
||||
public string GetError()
|
||||
{
|
||||
if (_webRequest != null)
|
||||
{
|
||||
return $"URL : {URL} Error : {_webRequest.error}";
|
||||
}
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2ab5f2486d06e2642ba7aa9c9623430e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user