Skip to content

Commit c9582b6

Browse files
aandre-dedalordrandomPoison
authored andcommitted
Check at runtime if Kongregate's JavaScript API is present (#6)
* Add logic to gracefully handle the case that the Kongregate API is not present. * Add new Status property to indicate if the API is uninitialized, unavailable, or ready.
1 parent 6de6d1e commit c9582b6

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

Runtime/KongregateWeb.cs

100644100755
Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,15 @@ public class KongregateUserItem
6565
[DisallowMultipleComponent]
6666
public class KongregateWeb : MonoBehaviour
6767
{
68-
private static KongregateWeb _instance;
68+
public enum ApiStatus
69+
{
70+
Uninitialized,
71+
Unavailable,
72+
Ready
73+
}
6974

70-
private bool _kongregateApiLoaded = false;
75+
private static KongregateWeb _instance;
76+
private ApiStatus _status = ApiStatus.Uninitialized;
7177
private string _username;
7278
private int _userId;
7379
private string _gameAuthToken;
@@ -236,12 +242,21 @@ public static event Action<bool> AdClosed
236242
}
237243
}
238244

245+
public static ApiStatus Status
246+
{
247+
get
248+
{
249+
AssertInstanceExists();
250+
return _instance._status;
251+
}
252+
}
253+
239254
public static bool IsReady
240255
{
241256
get
242257
{
243258
AssertInstanceExists();
244-
return _instance._kongregateApiLoaded;
259+
return _instance._status == ApiStatus.Ready;
245260
}
246261
}
247262

@@ -418,7 +433,7 @@ private static void AssertIsReady()
418433
{
419434
AssertInstanceExists();
420435

421-
if (!_instance._kongregateApiLoaded)
436+
if (_instance._status != ApiStatus.Ready)
422437
{
423438
throw new Exception($"Do not call any methods on {typeof(KongregateWeb).Name} until the Kongregate web API has finished loading");
424439
}
@@ -428,7 +443,7 @@ private static void AssertIsReady()
428443
#region Callbacks from JS
429444
private void OnInitSucceeded()
430445
{
431-
_kongregateApiLoaded = true;
446+
_status = ApiStatus.Ready;
432447

433448
if (!isGuest())
434449
{
@@ -441,6 +456,11 @@ private void OnInitSucceeded()
441456
_onBecameReady = null;
442457
}
443458

459+
private void OnInitFailed()
460+
{
461+
_status = ApiStatus.Unavailable;
462+
}
463+
444464
private void OnLogin(string userInfo)
445465
{
446466
_userId = getUserId();
@@ -541,7 +561,7 @@ private void OnAdClosed(int completed)
541561
[DllImport("__Internal")]
542562
private static extern void submitStats(string statisticName, int value);
543563
#else
544-
private static void initKongregateAPI (string gameObjectName) { }
564+
private static void initKongregateAPI (string gameObjectName) { _instance._status = ApiStatus.Unavailable; }
545565
private static bool isGuest() { return true; }
546566
private static int getUserId() { return 0; }
547567
private static string getUsername() { return null; }

Runtime/KongregateWeb.jslib

100644100755
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ var LibraryKongregate = {
2727
// Save the name of the Unity GameObject that we will send messages to.
2828
instance.gameObjectName = Pointer_stringify(gameObjectName);
2929

30+
if (typeof kongregateAPI === 'undefined' || kongregateAPI === null) {
31+
instance.sendMessage('OnInitFailed');
32+
return;
33+
}
34+
3035
kongregateAPI.loadAPI(function () {
3136
instance.kongregate = kongregateAPI.getAPI();
3237

@@ -146,4 +151,4 @@ var LibraryKongregate = {
146151
autoAddDeps(LibraryKongregate, '$instance');
147152
autoAddDeps(LibraryKongregate, '$stringToBuffer');
148153
autoAddDeps(LibraryKongregate, '$parseJSON');
149-
mergeInto(LibraryManager.library, LibraryKongregate);
154+
mergeInto(LibraryManager.library, LibraryKongregate);

0 commit comments

Comments
 (0)