diff --git a/sdkproject/Assets/Mapbox/Core/Unity/MapboxAccess.cs b/sdkproject/Assets/Mapbox/Core/Unity/MapboxAccess.cs index ec76f8f1c..526762788 100644 --- a/sdkproject/Assets/Mapbox/Core/Unity/MapboxAccess.cs +++ b/sdkproject/Assets/Mapbox/Core/Unity/MapboxAccess.cs @@ -1,158 +1,181 @@ -namespace Mapbox.Unity -{ - using System.IO; +namespace Mapbox.Unity { using UnityEngine; + using System.IO; using System; - using System.Collections; + using System.Net; using Mapbox.Geocoding; using Mapbox.Directions; using Mapbox.Platform; using Mapbox.Unity.Utilities; +#if !NETFX_CORE + using System.Security.Cryptography.X509Certificates; +#endif + using System.Net.Security; /// /// Object for retrieving an API token and making http requests. /// Contains a lazy Geocoder and a lazy Directions for convenience. /// - public class MapboxAccess : IFileSource - { - readonly string _accessPath = Path.Combine(Application.streamingAssetsPath, Constants.Path.TOKEN_FILE); + public class MapboxAccess : IFileSource { + + private readonly string _accessPath = Path.Combine(Application.streamingAssetsPath, Constants.Path.TOKEN_FILE); static MapboxAccess _instance = new MapboxAccess(); + /// /// The singleton instance. /// - public static MapboxAccess Instance - { - get - { + public static MapboxAccess Instance { + get { return _instance; } } - MapboxAccess() - { + MapboxAccess() { +#if !NETFX_CORE + ServicePointManager.ServerCertificateValidationCallback = MyRemoteCertificateValidationCallback; +#endif ValidateMapboxAccessFile(); LoadAccessToken(); } + /// /// The Mapbox API access token. /// See Mapbox API Congfiguration in Unity. /// - string _accessToken; - public string AccessToken - { - get - { + private string _accessToken; + public string AccessToken { + get { return _accessToken; } - set - { - if (string.IsNullOrEmpty(value)) - { + set { + if (string.IsNullOrEmpty(value)) { throw new InvalidTokenException("Please configure your access token in the menu!"); } _accessToken = value; } } - void ValidateMapboxAccessFile() - { - if (!Directory.Exists(Application.streamingAssetsPath) || !File.Exists(_accessPath)) - { + + private void ValidateMapboxAccessFile() { +#if !UNITY_ANDROID + if (!Directory.Exists(Application.streamingAssetsPath) || !File.Exists(_accessPath)) { throw new InvalidTokenException("Please configure your access token in the menu!"); } +#endif } + /// /// Loads the access token from StreamingAssets. /// - void LoadAccessToken() - { -#if UNITY_EDITOR || !UNITY_ANDROID + private void LoadAccessToken() { +#if UNITY_EDITOR || !UNITY_ANDROID AccessToken = File.ReadAllText(_accessPath); #else - Runnable.Run(LoadMapboxAccess()); + AccessToken = LoadMapboxAccess(); #endif } + /// /// Android-specific token file loading. /// - IEnumerator LoadMapboxAccess() - { + private string LoadMapboxAccess() { + var request = new WWW(_accessPath); - while (!request.isDone) - { - yield return 0; + // Implement a custom timeout - just in case + var timeout = Time.realtimeSinceStartup + 5f; + while (!request.isDone) { + if (Time.realtimeSinceStartup > timeout) { + throw new InvalidTokenException("Could not load access token!"); + } +#if NETFX_CORE + System.Threading.Tasks.Task.Delay(10).Wait(); +#else + System.Threading.Thread.Sleep(10); +#endif } - AccessToken = request.text; + return request.text; } + /// /// Makes an asynchronous url query. /// /// The request. /// URL. /// Callback. - public IAsyncRequest Request(string url, Action callback) - { - var uriBuilder = new UriBuilder(url); + public IAsyncRequest Request(string url, Action callback) { + var uriBuilder = new UriBuilder(url); string accessTokenQuery = "access_token=" + AccessToken; - if (uriBuilder.Query != null && uriBuilder.Query.Length > 1) - { + if (uriBuilder.Query != null && uriBuilder.Query.Length > 1) { uriBuilder.Query = uriBuilder.Query.Substring(1) + "&" + accessTokenQuery; - } - else - { + } else { uriBuilder.Query = accessTokenQuery; } - return new HTTPRequest(uriBuilder.ToString(), callback); } - class InvalidTokenException : Exception - { - public InvalidTokenException(string message) : base(message) - { + + class InvalidTokenException : Exception { + public InvalidTokenException(string message) : base(message) { } } - Geocoder _geocoder; /// /// Lazy geocoder. /// - public Geocoder Geocoder - { - get - { - if (_geocoder == null) - { + Geocoder _geocoder; + public Geocoder Geocoder { + get { + if (_geocoder == null) { _geocoder = new Geocoder(this); } return _geocoder; } } - Directions _directions; /// /// Lazy Directions. /// - public Directions Directions - { - get - { - if (_directions == null) - { + Directions _directions; + public Directions Directions { + get { + if (_directions == null) { _directions = new Directions(this); } return _directions; } } + + +#if !NETFX_CORE + public bool MyRemoteCertificateValidationCallback(System.Object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { + bool isOk = true; + // If there are errors in the certificate chain, look at each error to determine the cause. + if (sslPolicyErrors != SslPolicyErrors.None) { + for (int i = 0; i < chain.ChainStatus.Length; i++) { + if (chain.ChainStatus[i].Status != X509ChainStatusFlags.RevocationStatusUnknown) { + chain.ChainPolicy.RevocationFlag = X509RevocationFlag.EntireChain; + chain.ChainPolicy.RevocationMode = X509RevocationMode.Online; + chain.ChainPolicy.UrlRetrievalTimeout = new TimeSpan(0, 1, 0); + chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllFlags; + bool chainIsValid = chain.Build((X509Certificate2)certificate); + if (!chainIsValid) { + isOk = false; + } + } + } + } + return isOk; + } +#endif } } diff --git a/sdkproject/Assets/Mapbox/Core/Unity/Utilities/Singleton.cs b/sdkproject/Assets/Mapbox/Core/Unity/Utilities/Singleton.cs index 8a2809a87..4c289ee71 100644 --- a/sdkproject/Assets/Mapbox/Core/Unity/Utilities/Singleton.cs +++ b/sdkproject/Assets/Mapbox/Core/Unity/Utilities/Singleton.cs @@ -17,67 +17,67 @@ //#define SINGLETONS_VISIBLE -namespace Mapbox.Unity.Utilities -{ - using UnityEngine; - using System; - - /// - /// Singleton pattern class. This class detects if T is a MonoBehavior and will - /// make a containing GameObject. - /// - /// The typename of the class to create as a singleton object. - public class Singleton where T : class - { - #region Private Data - static private T sm_Instance = null; - #endregion +namespace Mapbox.Unity.Utilities { - #region Public Properties - /// - /// Returns the Singleton instance of T. - /// - public static T Instance - { - get - { - if (sm_Instance == null) - CreateInstance(); - return sm_Instance; - } - } - #endregion + using UnityEngine; + using System; +#if NETFX_CORE + using System.Reflection; +#endif + + /// + /// Singleton pattern class. This class detects if T is a MonoBehavior and will + /// make a containing GameObject. + /// + /// The typename of the class to create as a singleton object. + public class Singleton where T : class { + #region Private Data + static private T sm_Instance = null; + #endregion + + #region Public Properties + /// + /// Returns the Singleton instance of T. + /// + public static T Instance { + get { + if (sm_Instance == null) + CreateInstance(); + return sm_Instance; + } + } + #endregion - #region Singleton Creation - /// - /// Create the singleton instance. - /// - private static void CreateInstance() - { - if (typeof(MonoBehaviour).IsAssignableFrom(typeof(T))) - { - string singletonName = "_" + typeof(T).Name; + #region Singleton Creation + /// + /// Create the singleton instance. + /// + private static void CreateInstance() { +#if NETFX_CORE + if (typeof(MonoBehaviour).GetTypeInfo().IsAssignableFrom(typeof(T).GetTypeInfo())) { +#else + if (typeof(MonoBehaviour).IsAssignableFrom(typeof(T))) { +#endif + string singletonName = "_" + typeof(T).Name; - GameObject singletonObject = GameObject.Find(singletonName); - if (singletonObject == null) - singletonObject = new GameObject(singletonName); + GameObject singletonObject = GameObject.Find(singletonName); + if (singletonObject == null) + singletonObject = new GameObject(singletonName); #if SINGLETONS_VISIBLE singletonObject.hideFlags = HideFlags.DontSave; #else - singletonObject.hideFlags = HideFlags.HideAndDontSave; + singletonObject.hideFlags = HideFlags.HideAndDontSave; #endif - sm_Instance = singletonObject.GetComponent(); - if (sm_Instance == null) - sm_Instance = singletonObject.AddComponent(typeof(T)) as T; - } - else - { - sm_Instance = Activator.CreateInstance(typeof(T)) as T; - } + sm_Instance = singletonObject.GetComponent(); + if (sm_Instance == null) + sm_Instance = singletonObject.AddComponent(typeof(T)) as T; + } else { + sm_Instance = Activator.CreateInstance(typeof(T)) as T; + } - if (sm_Instance == null) - throw new Exception("Failed to create instance " + typeof(T).Name); - } - #endregion - } + if (sm_Instance == null) + throw new Exception("Failed to create instance " + typeof(T).Name); + } + #endregion + } } \ No newline at end of file diff --git a/sdkproject/ProjectSettings/EditorBuildSettings.asset b/sdkproject/ProjectSettings/EditorBuildSettings.asset index e8f9a8784..6dc24f7df 100644 --- a/sdkproject/ProjectSettings/EditorBuildSettings.asset +++ b/sdkproject/ProjectSettings/EditorBuildSettings.asset @@ -4,6 +4,4 @@ EditorBuildSettings: m_ObjectHideFlags: 0 serializedVersion: 2 - m_Scenes: - - enabled: 1 - path: Assets/Mapbox/Examples/LocationProvider/LocationProvider.unity + m_Scenes: []