123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- //------------------------------------------------------------
- // Game Framework
- // Copyright © 2013-2021 loyalsoft. All rights reserved.
- // Homepage: http://www.game7000.com/
- // Feedback: http://www.game7000.com/
- //------------------------------------------------------------
- using GameFramework;
- using GameFramework.Download;
- using System;
- #if UNITY_5_4_OR_NEWER
- using UnityEngine.Networking;
- #else
- using UnityEngine.Experimental.Networking;
- #endif
- using Utility = GameFramework.Utility;
- namespace UnityGameFramework.Runtime
- {
- /// <summary>
- /// 使用 UnityWebRequest 实现的下载代理辅助器。
- /// </summary>
- public partial class UnityWebRequestDownloadAgentHelper : DownloadAgentHelperBase, IDisposable
- {
- private const int CachedBytesLength = 0x1000;
- private readonly byte[] m_CachedBytes = new byte[CachedBytesLength];
- private UnityWebRequest m_UnityWebRequest = null;
- private bool m_Disposed = false;
- private EventHandler<DownloadAgentHelperUpdateBytesEventArgs> m_DownloadAgentHelperUpdateBytesEventHandler = null;
- private EventHandler<DownloadAgentHelperUpdateLengthEventArgs> m_DownloadAgentHelperUpdateLengthEventHandler = null;
- private EventHandler<DownloadAgentHelperCompleteEventArgs> m_DownloadAgentHelperCompleteEventHandler = null;
- private EventHandler<DownloadAgentHelperErrorEventArgs> m_DownloadAgentHelperErrorEventHandler = null;
- /// <summary>
- /// 下载代理辅助器更新数据流事件。
- /// </summary>
- public override event EventHandler<DownloadAgentHelperUpdateBytesEventArgs> DownloadAgentHelperUpdateBytes
- {
- add
- {
- m_DownloadAgentHelperUpdateBytesEventHandler += value;
- }
- remove
- {
- m_DownloadAgentHelperUpdateBytesEventHandler -= value;
- }
- }
- /// <summary>
- /// 下载代理辅助器更新数据大小事件。
- /// </summary>
- public override event EventHandler<DownloadAgentHelperUpdateLengthEventArgs> DownloadAgentHelperUpdateLength
- {
- add
- {
- m_DownloadAgentHelperUpdateLengthEventHandler += value;
- }
- remove
- {
- m_DownloadAgentHelperUpdateLengthEventHandler -= value;
- }
- }
- /// <summary>
- /// 下载代理辅助器完成事件。
- /// </summary>
- public override event EventHandler<DownloadAgentHelperCompleteEventArgs> DownloadAgentHelperComplete
- {
- add
- {
- m_DownloadAgentHelperCompleteEventHandler += value;
- }
- remove
- {
- m_DownloadAgentHelperCompleteEventHandler -= value;
- }
- }
- /// <summary>
- /// 下载代理辅助器错误事件。
- /// </summary>
- public override event EventHandler<DownloadAgentHelperErrorEventArgs> DownloadAgentHelperError
- {
- add
- {
- m_DownloadAgentHelperErrorEventHandler += value;
- }
- remove
- {
- m_DownloadAgentHelperErrorEventHandler -= value;
- }
- }
- /// <summary>
- /// 通过下载代理辅助器下载指定地址的数据。
- /// </summary>
- /// <param name="downloadUri">下载地址。</param>
- /// <param name="userData">用户自定义数据。</param>
- public override void Download(string downloadUri, object userData)
- {
- if (m_DownloadAgentHelperUpdateBytesEventHandler == null || m_DownloadAgentHelperUpdateLengthEventHandler == null || m_DownloadAgentHelperCompleteEventHandler == null || m_DownloadAgentHelperErrorEventHandler == null)
- {
- Log.Fatal("Download agent helper handler is invalid.");
- return;
- }
- m_UnityWebRequest = new UnityWebRequest(downloadUri);
- m_UnityWebRequest.downloadHandler = new DownloadHandler(this);
- #if UNITY_2017_2_OR_NEWER
- m_UnityWebRequest.SendWebRequest();
- #else
- m_UnityWebRequest.Send();
- #endif
- }
- /// <summary>
- /// 通过下载代理辅助器下载指定地址的数据。
- /// </summary>
- /// <param name="downloadUri">下载地址。</param>
- /// <param name="fromPosition">下载数据起始位置。</param>
- /// <param name="userData">用户自定义数据。</param>
- public override void Download(string downloadUri, long fromPosition, object userData)
- {
- if (m_DownloadAgentHelperUpdateBytesEventHandler == null || m_DownloadAgentHelperUpdateLengthEventHandler == null || m_DownloadAgentHelperCompleteEventHandler == null || m_DownloadAgentHelperErrorEventHandler == null)
- {
- Log.Fatal("Download agent helper handler is invalid.");
- return;
- }
- m_UnityWebRequest = new UnityWebRequest(downloadUri);
- m_UnityWebRequest.SetRequestHeader("Range", Utility.Text.Format("bytes={0}-", fromPosition.ToString()));
- m_UnityWebRequest.downloadHandler = new DownloadHandler(this);
- #if UNITY_2017_2_OR_NEWER
- m_UnityWebRequest.SendWebRequest();
- #else
- m_UnityWebRequest.Send();
- #endif
- }
- /// <summary>
- /// 通过下载代理辅助器下载指定地址的数据。
- /// </summary>
- /// <param name="downloadUri">下载地址。</param>
- /// <param name="fromPosition">下载数据起始位置。</param>
- /// <param name="toPosition">下载数据结束位置。</param>
- /// <param name="userData">用户自定义数据。</param>
- public override void Download(string downloadUri, long fromPosition, long toPosition, object userData)
- {
- if (m_DownloadAgentHelperUpdateBytesEventHandler == null || m_DownloadAgentHelperUpdateLengthEventHandler == null || m_DownloadAgentHelperCompleteEventHandler == null || m_DownloadAgentHelperErrorEventHandler == null)
- {
- Log.Fatal("Download agent helper handler is invalid.");
- return;
- }
- m_UnityWebRequest = new UnityWebRequest(downloadUri);
- m_UnityWebRequest.SetRequestHeader("Range", Utility.Text.Format("bytes={0}-{1}", fromPosition.ToString(), toPosition.ToString()));
- m_UnityWebRequest.downloadHandler = new DownloadHandler(this);
- #if UNITY_2017_2_OR_NEWER
- m_UnityWebRequest.SendWebRequest();
- #else
- m_UnityWebRequest.Send();
- #endif
- }
- /// <summary>
- /// 重置下载代理辅助器。
- /// </summary>
- public override void Reset()
- {
- if (m_UnityWebRequest != null)
- {
- m_UnityWebRequest.Abort();
- m_UnityWebRequest.Dispose();
- m_UnityWebRequest = null;
- }
- Array.Clear(m_CachedBytes, 0, CachedBytesLength);
- }
- /// <summary>
- /// 释放资源。
- /// </summary>
- public void Dispose()
- {
- Dispose(true);
- GC.SuppressFinalize(this);
- }
- /// <summary>
- /// 释放资源。
- /// </summary>
- /// <param name="disposing">释放资源标记。</param>
- protected virtual void Dispose(bool disposing)
- {
- if (m_Disposed)
- {
- return;
- }
- if (disposing)
- {
- if (m_UnityWebRequest != null)
- {
- m_UnityWebRequest.Dispose();
- m_UnityWebRequest = null;
- }
- }
- m_Disposed = true;
- }
- private void Update()
- {
- if (m_UnityWebRequest == null)
- {
- return;
- }
- if (!m_UnityWebRequest.isDone)
- {
- return;
- }
- bool isError = false;
- #if UNITY_2020_2_OR_NEWER
- isError = m_UnityWebRequest.result != UnityWebRequest.Result.Success;
- #elif UNITY_2017_1_OR_NEWER
- isError = m_UnityWebRequest.isNetworkError || m_UnityWebRequest.isHttpError;
- #else
- isError = m_UnityWebRequest.isError;
- #endif
- if (isError)
- {
- DownloadAgentHelperErrorEventArgs downloadAgentHelperErrorEventArgs = DownloadAgentHelperErrorEventArgs.Create(m_UnityWebRequest.responseCode == RangeNotSatisfiableErrorCode, m_UnityWebRequest.error);
- m_DownloadAgentHelperErrorEventHandler(this, downloadAgentHelperErrorEventArgs);
- ReferencePool.Release(downloadAgentHelperErrorEventArgs);
- }
- else
- {
- DownloadAgentHelperCompleteEventArgs downloadAgentHelperCompleteEventArgs = DownloadAgentHelperCompleteEventArgs.Create((long)m_UnityWebRequest.downloadedBytes);
- m_DownloadAgentHelperCompleteEventHandler(this, downloadAgentHelperCompleteEventArgs);
- ReferencePool.Release(downloadAgentHelperCompleteEventArgs);
- }
- }
- }
- }
|