Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 86 additions & 63 deletions sdkproject/Assets/Mapbox/Core/Unity/MapboxAccess.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Object for retrieving an API token and making http requests.
/// Contains a lazy <see cref="T:Mapbox.Geocoding.Geocoder">Geocoder</see> and a lazy <see cref="T:Mapbox.Directions.Directions">Directions</see> for convenience.
/// </summary>
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();


/// <summary>
/// The singleton instance.
/// </summary>
public static MapboxAccess Instance
{
get
{
public static MapboxAccess Instance {
get {
return _instance;
}
}

MapboxAccess()
{
MapboxAccess() {
#if !NETFX_CORE
ServicePointManager.ServerCertificateValidationCallback = MyRemoteCertificateValidationCallback;
#endif
ValidateMapboxAccessFile();
LoadAccessToken();
}


/// <summary>
/// The Mapbox API access token.
/// See <see href="https://www.mapbox.com/mapbox-unity-sdk/docs/01-mapbox-api-token.html">Mapbox API Congfiguration in Unity</see>.
/// </summary>
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
}


/// <summary>
/// Loads the access token from <see href="https://docs.unity3d.com/Manual/StreamingAssets.html">StreamingAssets</see>.
/// </summary>
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
}


/// <summary>
/// Android-specific token file loading.
/// </summary>
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;
}


/// <summary>
/// Makes an asynchronous url query.
/// </summary>
/// <returns>The request.</returns>
/// <param name="url">URL.</param>
/// <param name="callback">Callback.</param>
public IAsyncRequest Request(string url, Action<Response> callback)
{
var uriBuilder = new UriBuilder(url);
public IAsyncRequest Request(string url, Action<Response> 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;

/// <summary>
/// Lazy geocoder.
/// </summary>
public Geocoder Geocoder
{
get
{
if (_geocoder == null)
{
Geocoder _geocoder;
public Geocoder Geocoder {
get {
if (_geocoder == null) {
_geocoder = new Geocoder(this);
}
return _geocoder;
}
}

Directions _directions;

/// <summary>
/// Lazy Directions.
/// </summary>
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
}
}
110 changes: 55 additions & 55 deletions sdkproject/Assets/Mapbox/Core/Unity/Utilities/Singleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,67 +17,67 @@

//#define SINGLETONS_VISIBLE

namespace Mapbox.Unity.Utilities
{
using UnityEngine;
using System;

/// <summary>
/// Singleton pattern class. This class detects if T is a MonoBehavior and will
/// make a containing GameObject.
/// </summary>
/// <typeparam name="T">The typename of the class to create as a singleton object.</typeparam>
public class Singleton<T> where T : class
{
#region Private Data
static private T sm_Instance = null;
#endregion
namespace Mapbox.Unity.Utilities {

#region Public Properties
/// <summary>
/// Returns the Singleton instance of T.
/// </summary>
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

/// <summary>
/// Singleton pattern class. This class detects if T is a MonoBehavior and will
/// make a containing GameObject.
/// </summary>
/// <typeparam name="T">The typename of the class to create as a singleton object.</typeparam>
public class Singleton<T> where T : class {
#region Private Data
static private T sm_Instance = null;
#endregion

#region Public Properties
/// <summary>
/// Returns the Singleton instance of T.
/// </summary>
public static T Instance {
get {
if (sm_Instance == null)
CreateInstance();
return sm_Instance;
}
}
#endregion

#region Singleton Creation
/// <summary>
/// Create the singleton instance.
/// </summary>
private static void CreateInstance()
{
if (typeof(MonoBehaviour).IsAssignableFrom(typeof(T)))
{
string singletonName = "_" + typeof(T).Name;
#region Singleton Creation
/// <summary>
/// Create the singleton instance.
/// </summary>
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<T>();
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<T>();
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
}
}
4 changes: 1 addition & 3 deletions sdkproject/ProjectSettings/EditorBuildSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@
EditorBuildSettings:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Scenes:
- enabled: 1
path: Assets/Mapbox/Examples/LocationProvider/LocationProvider.unity
m_Scenes: []