-
Notifications
You must be signed in to change notification settings - Fork 213
/
MapboxAccess.cs
202 lines (176 loc) · 4.53 KB
/
MapboxAccess.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
namespace Mapbox.Unity
{
using UnityEngine;
using System.IO;
using Mapbox.Utils;
using System;
using Mapbox.Geocoding;
using Mapbox.Directions;
using Mapbox.Platform;
using Mapbox.Platform.Cache;
using Mapbox.Unity.Telemetry;
/// <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);
static MapboxAccess _instance = new MapboxAccess();
ITelemetryLibrary _telemetryLibrary;
CachingWebFileSource _fileSource;
// Default on.
bool _isTelemetryEnabled = true;
/// <summary>
/// The singleton instance.
/// </summary>
public static MapboxAccess Instance
{
get
{
return _instance;
}
}
MapboxAccess()
{
ValidateMapboxAccessFile();
LoadAccessToken();
ConfigureFileSource();
ConfigureTelemetry();
}
void ConfigureFileSource()
{
_fileSource = new CachingWebFileSource(_accessToken).AddCache(new MemoryCache(500));
}
void ConfigureTelemetry()
{
// TODO: this will need to be settable at runtime as well?
_isTelemetryEnabled = GetTelemetryCollectionState();
#if UNITY_EDITOR
_telemetryLibrary = TelemetryDummy.Instance;
#elif UNITY_IOS
_telemetryLibrary = TelemetryIos.Instance;
#elif UNITY_ANDROID
_telemetryLibrary = TelemetryAndroid.Instance;
#else
_telemetryLibrary = TelemetryDummy.Instance;
#endif
_telemetryLibrary.Initialize(_accessToken);
_telemetryLibrary.SendTurnstile();
}
/// <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>
private string _accessToken;
public string AccessToken
{
get
{
return _accessToken;
}
private set
{
if (string.IsNullOrEmpty(value))
{
throw new InvalidTokenException("Please configure your access token in the menu!");
}
_accessToken = value;
}
}
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>
private void LoadAccessToken()
{
#if UNITY_EDITOR || !UNITY_ANDROID
AccessToken = File.ReadAllText(_accessPath);
#else
AccessToken = LoadMapboxAccess();
#endif
}
/// <summary>
/// Android-specific token file loading.
/// </summary>
private string LoadMapboxAccess()
{
var request = new WWW(_accessPath);
// 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.Thread.Sleep(10);
#endif
}
return request.text;
}
bool GetTelemetryCollectionState()
{
if (!PlayerPrefs.HasKey(Constants.Path.IS_TELEMETRY_ENABLED_KEY))
{
PlayerPrefs.SetInt(Constants.Path.IS_TELEMETRY_ENABLED_KEY, 1);
}
return PlayerPrefs.GetInt(Constants.Path.IS_TELEMETRY_ENABLED_KEY) != 0;
}
/// <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, int timeout = 10)
{
return _fileSource.Request(url, callback, timeout);
}
class InvalidTokenException : Exception
{
public InvalidTokenException(string message) : base(message)
{
}
}
/// <summary>
/// Lazy geocoder.
/// </summary>
Geocoder _geocoder;
public Geocoder Geocoder
{
get
{
if (_geocoder == null)
{
_geocoder = new Geocoder(new FileSource(AccessToken));
}
return _geocoder;
}
}
/// <summary>
/// Lazy Directions.
/// </summary>
Directions _directions;
public Directions Directions
{
get
{
if (_directions == null)
{
_directions = new Directions(new FileSource(AccessToken));
}
return _directions;
}
}
}
}