//------------------------------------------------------------ // Game Framework // Copyright © 2013-2021 loyalsoft. All rights reserved. // Homepage: http://www.game7000.com/ // Feedback: http://www.game7000.com/ //------------------------------------------------------------ #if !UNITY_2018_3_OR_NEWER using GameFramework; using GameFramework.Download; using System; using System.Collections.Generic; using UnityEngine; namespace UnityGameFramework.Runtime { /// /// WWW 下载代理辅助器。 /// public class WWWDownloadAgentHelper : DownloadAgentHelperBase, IDisposable { private WWW m_WWW = null; private int m_LastDownloadedSize = 0; private bool m_Disposed = false; private EventHandler m_DownloadAgentHelperUpdateBytesEventHandler = null; private EventHandler m_DownloadAgentHelperUpdateLengthEventHandler = null; private EventHandler m_DownloadAgentHelperCompleteEventHandler = null; private EventHandler m_DownloadAgentHelperErrorEventHandler = null; /// /// 下载代理辅助器更新数据流事件。 /// public override event EventHandler DownloadAgentHelperUpdateBytes { add { m_DownloadAgentHelperUpdateBytesEventHandler += value; } remove { m_DownloadAgentHelperUpdateBytesEventHandler -= value; } } /// /// 下载代理辅助器更新数据大小事件。 /// public override event EventHandler DownloadAgentHelperUpdateLength { add { m_DownloadAgentHelperUpdateLengthEventHandler += value; } remove { m_DownloadAgentHelperUpdateLengthEventHandler -= value; } } /// /// 下载代理辅助器完成事件。 /// public override event EventHandler DownloadAgentHelperComplete { add { m_DownloadAgentHelperCompleteEventHandler += value; } remove { m_DownloadAgentHelperCompleteEventHandler -= value; } } /// /// 下载代理辅助器错误事件。 /// public override event EventHandler DownloadAgentHelperError { add { m_DownloadAgentHelperErrorEventHandler += value; } remove { m_DownloadAgentHelperErrorEventHandler -= value; } } /// /// 通过下载代理辅助器下载指定地址的数据。 /// /// 下载地址。 /// 用户自定义数据。 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_WWW = new WWW(downloadUri); } /// /// 通过下载代理辅助器下载指定地址的数据。 /// /// 下载地址。 /// 下载数据起始位置。 /// 用户自定义数据。 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; } Dictionary header = new Dictionary { { "Range", Utility.Text.Format("bytes={0}-", fromPosition.ToString()) } }; m_WWW = new WWW(downloadUri, null, header); } /// /// 通过下载代理辅助器下载指定地址的数据。 /// /// 下载地址。 /// 下载数据起始位置。 /// 下载数据结束位置。 /// 用户自定义数据。 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; } Dictionary header = new Dictionary { { "Range", Utility.Text.Format("bytes={0}-{1}", fromPosition.ToString(), toPosition.ToString()) } }; m_WWW = new WWW(downloadUri, null, header); } /// /// 重置下载代理辅助器。 /// public override void Reset() { if (m_WWW != null) { m_WWW.Dispose(); m_WWW = null; } m_LastDownloadedSize = 0; } /// /// 释放资源。 /// public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } /// /// 释放资源。 /// /// 释放资源标记。 protected virtual void Dispose(bool disposing) { if (m_Disposed) { return; } if (disposing) { if (m_WWW != null) { m_WWW.Dispose(); m_WWW = null; } } m_Disposed = true; } private void Update() { if (m_WWW == null) { return; } int deltaLength = m_WWW.bytesDownloaded - m_LastDownloadedSize; if (deltaLength > 0) { m_LastDownloadedSize = m_WWW.bytesDownloaded; DownloadAgentHelperUpdateLengthEventArgs downloadAgentHelperUpdateLengthEventArgs = DownloadAgentHelperUpdateLengthEventArgs.Create(deltaLength); m_DownloadAgentHelperUpdateLengthEventHandler(this, downloadAgentHelperUpdateLengthEventArgs); ReferencePool.Release(downloadAgentHelperUpdateLengthEventArgs); } if (m_WWW == null) { return; } if (!m_WWW.isDone) { return; } if (!string.IsNullOrEmpty(m_WWW.error)) { DownloadAgentHelperErrorEventArgs dodwnloadAgentHelperErrorEventArgs = DownloadAgentHelperErrorEventArgs.Create(m_WWW.error.StartsWith(RangeNotSatisfiableErrorCode.ToString(), StringComparison.Ordinal), m_WWW.error); m_DownloadAgentHelperErrorEventHandler(this, dodwnloadAgentHelperErrorEventArgs); ReferencePool.Release(dodwnloadAgentHelperErrorEventArgs); } else { byte[] bytes = m_WWW.bytes; DownloadAgentHelperUpdateBytesEventArgs downloadAgentHelperUpdateBytesEventArgs = DownloadAgentHelperUpdateBytesEventArgs.Create(bytes, 0, bytes.Length); m_DownloadAgentHelperUpdateBytesEventHandler(this, downloadAgentHelperUpdateBytesEventArgs); ReferencePool.Release(downloadAgentHelperUpdateBytesEventArgs); DownloadAgentHelperCompleteEventArgs downloadAgentHelperCompleteEventArgs = DownloadAgentHelperCompleteEventArgs.Create(bytes.LongLength); m_DownloadAgentHelperCompleteEventHandler(this, downloadAgentHelperCompleteEventArgs); ReferencePool.Release(downloadAgentHelperCompleteEventArgs); } } } } #endif