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.singleton.KillTimer(this._token); this._token = XSingleton.singleton.SetTimer(0.1f, this._progressCb, null); this._downloader = new WWW(url); yield return this._downloader; XSingleton.singleton.KillTimer(this._token); this.Progress(null); XSingleton.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.singleton.KillTimer(this._token); this._token = XSingleton.singleton.SetTimer(0.1f, this._progressCb, null); this._downloader = new WWW(url); yield return this._downloader; XSingleton.singleton.KillTimer(this._token); this.Progress(null); XSingleton.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.singleton.XPlatform.SetNoBackupFlag(awr.Location); awr = null; } bool flag2 = error; if (flag2) { this._log.Length = 0; this._log.AppendFormat(XSingleton.singleton.GetString("XUPDATE_ERROR_DOWNLOADRESFAILED"), bundle.Name); XSingleton.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.singleton.GetString("XUPDATE_INFO_DOWNLOADING"), num); } else { this._log.AppendFormat(XSingleton.singleton.GetString("XUPDATE_INFO_EXTRACTING"), num); } XSingleton.singleton.SetStatus(this._log.ToString(), byte.MaxValue, byte.MaxValue, byte.MaxValue); this._token = XSingleton.singleton.SetTimer(0.1f, this._progressCb, o); } } }