summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/XUtliPoolLib/XUpdater/XDownloader.cs
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2021-01-25 14:28:30 +0800
committerchai <chaifix@163.com>2021-01-25 14:28:30 +0800
commit6eb915c129fc90c6f4c82ae097dd6ffad5239efc (patch)
tree7dd2be50edf41f36b60fac84696e731c13afe617 /Client/Assets/Scripts/XUtliPoolLib/XUpdater/XDownloader.cs
+scripts
Diffstat (limited to 'Client/Assets/Scripts/XUtliPoolLib/XUpdater/XDownloader.cs')
-rw-r--r--Client/Assets/Scripts/XUtliPoolLib/XUpdater/XDownloader.cs149
1 files changed, 149 insertions, 0 deletions
diff --git a/Client/Assets/Scripts/XUtliPoolLib/XUpdater/XDownloader.cs b/Client/Assets/Scripts/XUtliPoolLib/XUpdater/XDownloader.cs
new file mode 100644
index 00000000..00e347ae
--- /dev/null
+++ b/Client/Assets/Scripts/XUtliPoolLib/XUpdater/XDownloader.cs
@@ -0,0 +1,149 @@
+using System;
+using System.Collections;
+using System.Text;
+using UnityEngine;
+using XUtliPoolLib;
+
+namespace XUpdater
+{
+ public sealed class XDownloader : MonoBehaviour
+ {
+ private StringBuilder _log = new StringBuilder();
+
+ private WWW _downloader = null;
+
+ private bool _download = true;
+
+ private uint _token = 0u;
+
+ private float _total_percent = 0f;
+
+ private string _current_name = null;
+
+ private XTimerMgr.ElapsedEventHandler _progressCb = null;
+
+ public delegate void HandleBytesDownload(WWW www, string error);
+
+ private void Awake()
+ {
+ this._progressCb = new XTimerMgr.ElapsedEventHandler(this.Progress);
+ }
+
+ internal void GetBundle(XBundleData bundle, string url, HandleBundleDownload callback1, HandleFetchBundle callback2, float percent)
+ {
+ this._download = url.Contains("?token=");
+ this._current_name = bundle.Name;
+ this._total_percent = percent;
+ base.StartCoroutine(this.Download(bundle, url, callback1, callback2));
+ }
+
+ public void GetMeta(string url, string name, XDownloader.HandleBytesDownload callback, float percent)
+ {
+ this._download = true;
+ this._current_name = name;
+ this._total_percent = percent;
+ base.StartCoroutine(this.MetaDownload(url, callback));
+ }
+
+ public void GetBytes(string url, XDownloader.HandleBytesDownload callback)
+ {
+ base.StartCoroutine(this.BytesDownload(url, callback));
+ }
+
+ private IEnumerator MetaDownload(string url, XDownloader.HandleBytesDownload callback)
+ {
+ XSingleton<XTimerMgr>.singleton.KillTimer(this._token);
+ this._token = XSingleton<XTimerMgr>.singleton.SetTimer(0.1f, this._progressCb, null);
+ this._downloader = new WWW(url);
+ yield return this._downloader;
+ XSingleton<XTimerMgr>.singleton.KillTimer(this._token);
+ this.Progress(null);
+ XSingleton<XTimerMgr>.singleton.KillTimer(this._token);
+ bool flag = callback != null;
+ if (flag)
+ {
+ callback(this._downloader, this._downloader.error);
+ }
+ this._downloader.Dispose();
+ this._downloader = null;
+ yield break;
+ }
+
+ private IEnumerator BytesDownload(string url, XDownloader.HandleBytesDownload callback)
+ {
+ WWW www = new WWW(url);
+ yield return www;
+ bool flag = callback != null;
+ if (flag)
+ {
+ callback(www, www.error);
+ }
+ www.Dispose();
+ www = null;
+ yield break;
+ }
+
+ private IEnumerator Download(XBundleData bundle, string url, HandleBundleDownload callback1, HandleFetchBundle callback2)
+ {
+ XSingleton<XTimerMgr>.singleton.KillTimer(this._token);
+ this._token = XSingleton<XTimerMgr>.singleton.SetTimer(0.1f, this._progressCb, null);
+ this._downloader = new WWW(url);
+ yield return this._downloader;
+ XSingleton<XTimerMgr>.singleton.KillTimer(this._token);
+ this.Progress(null);
+ XSingleton<XTimerMgr>.singleton.KillTimer(this._token);
+ bool error = false;
+ bool flag = callback1 != null;
+ if (flag)
+ {
+ AsyncWriteRequest awr = callback1(this._downloader, bundle, this._downloader.error);
+ while (!awr.IsDone)
+ {
+ bool hasError = awr.HasError;
+ if (hasError)
+ {
+ error = true;
+ break;
+ }
+ yield return null;
+ }
+ XSingleton<XUpdater>.singleton.XPlatform.SetNoBackupFlag(awr.Location);
+ awr = null;
+ }
+ bool flag2 = error;
+ if (flag2)
+ {
+ this._log.Length = 0;
+ this._log.AppendFormat(XSingleton<XStringTable>.singleton.GetString("XUPDATE_ERROR_DOWNLOADRESFAILED"), bundle.Name);
+ XSingleton<XLoadingUI>.singleton.SetStatus(this._log.ToString(), byte.MaxValue, byte.MaxValue, byte.MaxValue);
+ }
+ else
+ {
+ bool flag3 = callback2 != null;
+ if (flag3)
+ {
+ callback2(this._downloader, this._downloader.bytes, bundle, this._download);
+ }
+ }
+ this._downloader = null;
+ yield break;
+ }
+
+ private void Progress(object o)
+ {
+ this._log.Remove(0, this._log.Length);
+ int num = Mathf.FloorToInt(this._total_percent * 100f);
+ bool download = this._download;
+ if (download)
+ {
+ this._log.AppendFormat(XSingleton<XStringTable>.singleton.GetString("XUPDATE_INFO_DOWNLOADING"), num);
+ }
+ else
+ {
+ this._log.AppendFormat(XSingleton<XStringTable>.singleton.GetString("XUPDATE_INFO_EXTRACTING"), num);
+ }
+ XSingleton<XLoadingUI>.singleton.SetStatus(this._log.ToString(), byte.MaxValue, byte.MaxValue, byte.MaxValue);
+ this._token = XSingleton<XTimerMgr>.singleton.SetTimer(0.1f, this._progressCb, o);
+ }
+ }
+}