using UnityEngine; #if NETFX_CORE || BUILD_FOR_WP8 using System.Threading.Tasks; #endif namespace BestHTTP { /// /// Will route some U3D calls to the HTTPManager. /// [ExecuteInEditMode] public sealed class HTTPUpdateDelegator : MonoBehaviour { #region Public Properties /// /// The singleton instance of the HTTPUpdateDelegator /// public static HTTPUpdateDelegator Instance { get; private set; } /// /// True, if the Instance property should hold a valid value. /// public static bool IsCreated { get; private set; } /// /// Set it true before any CheckInstance() call, or before any request send to dispatch callbacks on another thread. /// public static bool IsThreaded { get; set; } /// /// It's true if the dispatch thread running. /// public static bool IsThreadRunning { get; private set; } /// /// How much time the plugin should wait between two update call. Its default value 100 ms. /// public static int ThreadFrequencyInMS { get; set; } /// /// Called in the OnApplicationQuit function. If this function returns False, the plugin will not start to /// shut down itself. /// public static System.Func OnBeforeApplicationQuit; #endregion private static bool IsSetupCalled; static HTTPUpdateDelegator() { ThreadFrequencyInMS = 100; } /// /// Will create the HTTPUpdateDelegator instance and set it up. /// public static void CheckInstance() { try { if (!IsCreated) { GameObject go = GameObject.Find("HTTP Update Delegator"); if (go != null) Instance = go.GetComponent(); if (Instance == null) { go = new GameObject("HTTP Update Delegator"); go.hideFlags = HideFlags.HideAndDontSave; #if UNITY_EDITOR if (UnityEditor.EditorApplication.isPlaying) GameObject.DontDestroyOnLoad(go); #endif Instance = go.AddComponent(); } IsCreated = true; #if UNITY_EDITOR if (!UnityEditor.EditorApplication.isPlaying) { UnityEditor.EditorApplication.update -= Instance.Update; UnityEditor.EditorApplication.update += Instance.Update; } UnityEditor.EditorApplication.playmodeStateChanged -= Instance.OnPlayModeStateChanged; UnityEditor.EditorApplication.playmodeStateChanged += Instance.OnPlayModeStateChanged; #endif } } catch { HTTPManager.Logger.Error("HTTPUpdateDelegator", "Please call the BestHTTP.HTTPManager.Setup() from one of Unity's event(eg. awake, start) before you send any request!"); } } private void Setup() { #if !BESTHTTP_DISABLE_CACHING && (!UNITY_WEBGL || UNITY_EDITOR) Caching.HTTPCacheService.SetupCacheFolder(); #endif #if !BESTHTTP_DISABLE_COOKIES && (!UNITY_WEBGL || UNITY_EDITOR) Cookies.CookieJar.SetupFolder(); Cookies.CookieJar.Load(); #endif #if UNITY_WEBGL // Threads are not implemented in WEBGL builds, disable it for now. IsThreaded = false; #endif if (IsThreaded) { #if NETFX_CORE Windows.System.Threading.ThreadPool.RunAsync(ThreadFunc); #else new System.Threading.Thread(ThreadFunc) .Start(); #endif } IsSetupCalled = true; } #if NETFX_CORE async #endif void ThreadFunc(object obj) { HTTPManager.Logger.Information ("HTTPUpdateDelegator", "Update Thread Started"); try { IsThreadRunning = true; while (IsThreadRunning) { HTTPManager.OnUpdate(); #if NETFX_CORE await Task.Delay(ThreadFrequencyInMS); #else System.Threading.Thread.Sleep(ThreadFrequencyInMS); #endif } } finally { HTTPManager.Logger.Information("HTTPUpdateDelegator", "Update Thread Ended"); } } void Update() { if (!IsSetupCalled) { IsSetupCalled = true; Setup(); } if (!IsThreaded) HTTPManager.OnUpdate(); } #if UNITY_EDITOR void OnPlayModeStateChanged() { if (UnityEditor.EditorApplication.isPlaying) UnityEditor.EditorApplication.update -= Update; else if (!UnityEditor.EditorApplication.isPlaying) UnityEditor.EditorApplication.update += Update; } #endif void OnDisable() { #if UNITY_EDITOR if (UnityEditor.EditorApplication.isPlaying) #endif OnApplicationQuit(); } void OnApplicationQuit() { if (OnBeforeApplicationQuit != null) { try { if (!OnBeforeApplicationQuit()) { HTTPManager.Logger.Information("HTTPUpdateDelegator", "OnBeforeApplicationQuit call returned false, postponing plugin shutdown."); return; } } catch(System.Exception ex) { HTTPManager.Logger.Exception("HTTPUpdateDelegator", string.Empty, ex); } } IsThreadRunning = false; if (!IsCreated) return; IsCreated = false; HTTPManager.OnQuit(); #if UNITY_EDITOR UnityEditor.EditorApplication.update -= Update; UnityEditor.EditorApplication.playmodeStateChanged -= OnPlayModeStateChanged; #endif } } }