Skip to content

Commit f45f509

Browse files
committed
Merge branch 'discord-sdk'
2 parents 178b299 + d9a5296 commit f45f509

File tree

13 files changed

+686
-81
lines changed

13 files changed

+686
-81
lines changed

Assets/PlayroomKit/PlayroomKit.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System;
44
using UBB;
5+
using Discord;
56

67

78
namespace Playroom
@@ -358,6 +359,24 @@ public void StartDiscordPurchase(string skuId, Action<string> responseCallback =
358359
CheckPlayRoomInitialized();
359360
_playroomService.StartDiscordPurchase(skuId, responseCallback);
360361
}
362+
363+
public void GetDiscordSkus(Action<List<DiscordSku>> callback)
364+
{
365+
CheckPlayRoomInitialized();
366+
_playroomService.GetDiscordSkus(callback);
367+
}
368+
369+
public void GetDiscordEntitlements(Action<List<DiscordEntitlement>> callback)
370+
{
371+
CheckPlayRoomInitialized();
372+
_playroomService.GetDiscordEntitlements(callback);
373+
}
374+
375+
public void DiscordPriceFormat(float price, string currency, string locale, Action<string> callback)
376+
{
377+
CheckPlayRoomInitialized();
378+
_playroomService.DiscordPriceFormat(price, currency, locale, callback);
379+
}
361380
#endregion
362381
}
363382
}

Assets/PlayroomKit/modules/Discord/DiscordEntitlement.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,15 @@ public static List<DiscordEntitlement> FromJSON(string jsonString)
8181
var list = new List<DiscordEntitlement>();
8282
var root = JSON.Parse(jsonString);
8383

84-
if (root.IsArray)
84+
if (root == null)
85+
throw new FormatException("Invalid JSON: root is null.");
86+
87+
if (root.HasKey("entitlements") && root["entitlements"].IsArray)
88+
{
89+
foreach (var item in root["entitlements"].AsArray)
90+
list.Add(FromJSONNode(item));
91+
}
92+
else if (root.IsArray)
8593
{
8694
foreach (var item in root.AsArray)
8795
list.Add(FromJSONNode(item));
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using SimpleJSON;
4+
5+
namespace Discord
6+
{
7+
public enum DiscordSkuType
8+
{
9+
UNHANDLED = -1,
10+
APPLICATION = 1,
11+
DLC = 2,
12+
CONSUMABLE = 3,
13+
BUNDLE = 4,
14+
SUBSCRIPTION = 5
15+
}
16+
17+
[Serializable]
18+
public class DiscordSkuPrice
19+
{
20+
public int Amount;
21+
public string Currency;
22+
23+
internal static DiscordSkuPrice FromJSONNode(JSONNode n)
24+
{
25+
return new DiscordSkuPrice
26+
{
27+
Amount = n["amount"].AsInt,
28+
Currency = n["currency"].Value
29+
};
30+
}
31+
}
32+
33+
[Serializable]
34+
public class DiscordSku
35+
{
36+
// Required
37+
public string Id;
38+
public string Name;
39+
public DiscordSkuType Type;
40+
public DiscordSkuPrice Price;
41+
public string ApplicationId;
42+
public int Flags;
43+
44+
// Optional
45+
public string? ReleaseDate;
46+
47+
internal static DiscordSku FromJSONNode(JSONNode n)
48+
{
49+
var rawType = n["type"].AsInt;
50+
if (!Enum.IsDefined(typeof(DiscordSkuType), rawType))
51+
throw new FormatException($"Unknown SkuType code: {rawType}");
52+
53+
var sku = new DiscordSku
54+
{
55+
Id = n["id"].Value,
56+
Name = n["name"].Value,
57+
Type = (DiscordSkuType)rawType,
58+
Price = DiscordSkuPrice.FromJSONNode(n["price"]),
59+
ApplicationId = n["application_id"].Value,
60+
Flags = n["flags"].AsInt
61+
};
62+
63+
if (n.HasKey("release_date"))
64+
{
65+
var rd = n["release_date"].Value;
66+
sku.ReleaseDate = string.IsNullOrEmpty(rd) ? null : rd;
67+
}
68+
69+
return sku;
70+
}
71+
72+
public static List<DiscordSku> FromJSON(string jsonString)
73+
{
74+
var list = new List<DiscordSku>();
75+
var root = JSON.Parse(jsonString);
76+
77+
if (root.HasKey("skus") && root["skus"].IsArray)
78+
{
79+
foreach (var item in root["skus"].AsArray)
80+
list.Add(FromJSONNode(item));
81+
}
82+
else if (root.IsArray)
83+
{
84+
foreach (var item in root.AsArray)
85+
list.Add(FromJSONNode(item));
86+
}
87+
else
88+
{
89+
list.Add(FromJSONNode(root));
90+
}
91+
92+
return list;
93+
}
94+
}
95+
}

Assets/PlayroomKit/modules/Discord/DiscordSkus.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/PlayroomKit/modules/Headers.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,16 @@ private static extern string GetPersistentDataInternal(string key,
172172

173173
[DllImport("__Internal")]
174174
private static extern void StartDiscordPurchaseInternal(string skuId, Action<string, string> callback);
175+
176+
[DllImport("__Internal")]
177+
private static extern void GetDiscordSkusInternal(Action<string> callback);
178+
179+
[DllImport("__Internal")]
180+
private static extern void GetDiscordEntitlementsInternal(Action<string> callback);
181+
182+
[DllImport("__Internal")]
183+
private static extern string DiscordPriceFormatInternal(float price, string currency, string locale, Action<string> callback);
184+
175185
#endregion
176186
}
177187
}

Assets/PlayroomKit/modules/Helpers/CallbackManager.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,17 @@ public static void InvokeCallback(string key, List<DiscordEntitlement> entitleme
126126
}
127127
}
128128

129+
public static void InvokeCallback(string key, List<DiscordSku> skus)
130+
{
131+
if (callbacks.TryGetValue(key, out Delegate callback))
132+
{
133+
if (callback is Action<List<DiscordSku>> action) action?.Invoke(skus);
134+
else
135+
Debug.LogError(
136+
$"Callback with key {key} is of unsupported type or incorrect number of arguments: {skus}!");
137+
}
138+
}
139+
129140
public static bool CheckCallback(string key)
130141
{
131142
return callbacks.TryGetValue(key, out Delegate callback);

Assets/PlayroomKit/modules/Interfaces/IPlayroomBase.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using AOT;
4+
using Discord;
45
using UnityEngine;
56

67
namespace Playroom
@@ -73,6 +74,9 @@ public void InsertCoin(InitOptions options = null, Action onLaunchCallBack = nul
7374
#region Discord
7475
public void OpenDiscordInviteDialog(Action callback = null);
7576
public void StartDiscordPurchase(string skuId, Action<string> callback = null);
77+
public void GetDiscordSkus(Action<List<DiscordSku>> callback);
78+
public void GetDiscordEntitlements(Action<List<DiscordEntitlement>> callback);
79+
public void DiscordPriceFormat(float price, string currency, string locale, Action<string> callback);
7680
#endregion
7781

7882

Assets/PlayroomKit/modules/MockMode/BrowserMode/BrowserMockService.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using System.Reflection;
44
using UBB;
55
using UnityEngine;
6+
using Discord;
7+
68

79
#if UNITY_EDITOR
810
using ParrelSync;
@@ -323,6 +325,24 @@ public void StartDiscordPurchase(string skuId, Action<string> callback = null)
323325
{
324326
DebugLogger.LogWarning("[MockMode] Discord purchase is currently not supported in browser mock mode!");
325327
}
328+
329+
public void GetDiscordSkus(Action<List<DiscordSku>> callback)
330+
{
331+
DebugLogger.LogWarning("[MockMode] Discord SKUs are currently not supported in browser mock mode!");
332+
callback?.Invoke(new List<DiscordSku>());
333+
}
334+
335+
public void GetDiscordEntitlements(Action<List<DiscordEntitlement>> callback)
336+
{
337+
DebugLogger.LogWarning("[MockMode] Discord SKUs are currently not supported in browser mock mode!");
338+
callback?.Invoke(new List<DiscordEntitlement>());
339+
}
340+
341+
public void DiscordPriceFormat(float price, string currency, string locale, Action<string> callback)
342+
{
343+
DebugLogger.LogWarning("[MockMode] Discord SKUs are currently not supported in browser mock mode!");
344+
callback?.Invoke("");
345+
}
326346
#endregion
327347
}
328348
#endif

Assets/PlayroomKit/modules/MockMode/LocalPlayroomService.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using Discord;
45
using UnityEngine;
56

67
namespace Playroom
@@ -224,6 +225,24 @@ public void StartDiscordPurchase(string skuId, Action<string> callback = null)
224225
{
225226
DebugLogger.LogWarning("[MockMode] Discord purchase is currently not supported in local mode!");
226227
}
228+
229+
public void GetDiscordSkus(Action<List<DiscordSku>> callback)
230+
{
231+
DebugLogger.LogWarning("[MockMode] Discord SKUs are currently not supported in local mode!");
232+
callback?.Invoke(new List<DiscordSku>());
233+
}
234+
235+
public void GetDiscordEntitlements(Action<List<DiscordEntitlement>> callback)
236+
{
237+
DebugLogger.LogWarning("[MockMode] Discord Entitlements are currently not supported in local mode!");
238+
callback?.Invoke(new List<DiscordEntitlement>());
239+
}
240+
241+
public void DiscordPriceFormat(float price, string currency, string locale, Action<string> callback)
242+
{
243+
DebugLogger.LogWarning("[MockMode] Discord SKUs are currently not supported in local mode!");
244+
callback?.Invoke($"${price}");
245+
}
227246
#endregion
228247
}
229248
}

Assets/PlayroomKit/modules/PlayroomBuildService.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using SimpleJSON;
55
using System;
66
using System.Collections.Generic;
7+
using Discord;
78

89
namespace Playroom
910
{
@@ -451,6 +452,63 @@ public void StartDiscordPurchase(string skuId, Action<string> callback = null)
451452
CallbackManager.RegisterCallback(callback, skuId);
452453
StartDiscordPurchaseInternal(skuId, DiscordPurchaseCallback);
453454
}
455+
456+
public void GetDiscordSkus(Action<List<DiscordSku>> callback)
457+
{
458+
CheckPlayRoomInitialized();
459+
CallbackManager.RegisterCallback(callback, "discordSkus");
460+
GetDiscordSkusInternal(DiscordSkusCallback);
461+
}
462+
463+
[MonoPInvokeCallback(typeof(Action<string>))]
464+
private static void DiscordSkusCallback(string data)
465+
{
466+
try
467+
{
468+
List<DiscordSku> list = DiscordSku.FromJSON(data);
469+
CallbackManager.InvokeCallback("discordSkus", list);
470+
}
471+
catch (Exception e)
472+
{
473+
Debug.LogError($"[Unity]: Error in Discord SKU: {e}");
474+
throw;
475+
}
476+
}
477+
478+
public void GetDiscordEntitlements(Action<List<DiscordEntitlement>> callback)
479+
{
480+
CheckPlayRoomInitialized();
481+
CallbackManager.RegisterCallback(callback, "discordEntitlements");
482+
GetDiscordEntitlementsInternal(DiscordEntitlementsCallback);
483+
}
484+
485+
[MonoPInvokeCallback(typeof(Action<string>))]
486+
private static void DiscordEntitlementsCallback(string data)
487+
{
488+
try
489+
{
490+
List<DiscordEntitlement> list = DiscordEntitlement.FromJSON(data);
491+
CallbackManager.InvokeCallback("discordEntitlements", list);
492+
}
493+
catch (Exception e)
494+
{
495+
Debug.LogError($"[Unity]: Error in discord Entitlements: {e}");
496+
throw;
497+
}
498+
}
499+
500+
public void DiscordPriceFormat(float price, string currency, string locale, Action<string> callback)
501+
{
502+
CheckPlayRoomInitialized();
503+
CallbackManager.RegisterCallback(callback, "formattedPrice");
504+
DiscordPriceFormatInternal(price, currency, locale, DiscordPriceFormatCallbackInvoker);
505+
}
506+
507+
[MonoPInvokeCallback(typeof(Action<string>))]
508+
private static void DiscordPriceFormatCallbackInvoker(string formattedPrice)
509+
{
510+
CallbackManager.InvokeCallback("formattedPrice", formattedPrice);
511+
}
454512
#endregion
455513

456514
#region Callbacks

0 commit comments

Comments
 (0)