Skip to content

Commit 65df959

Browse files
committed
feat: openExternalLink
1 parent 8e32e75 commit 65df959

File tree

7 files changed

+63
-2
lines changed

7 files changed

+63
-2
lines changed

Assets/PlayroomKit/modules/Headers.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ private static extern string GetPersistentDataInternal(string key,
170170
[DllImport("__Internal")]
171171
private static extern void OpenDiscordInviteDialogInternal(Action callback = null);
172172

173+
[DllImport("__Internal")]
174+
private static extern void OpenDiscordExternalLinkInternal(string url, Action<string> callback = null);
175+
173176
[DllImport("__Internal")]
174177
private static extern void StartDiscordPurchaseInternal(string skuId, Action<string, string> callback, Action<string> onError = null);
175178

Assets/PlayroomKit/modules/Helpers/CallbackManager.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,17 @@ public static void InvokeCallback(string key, TurnData turnData)
101101
$"Callback with key {key} is of unsupported type or incorrect number of arguments: {turnData}!");
102102
}
103103
}
104+
105+
public static void InvokeCallback(string key, bool someBool)
106+
{
107+
if (callbacks.TryGetValue(key, out Delegate callback))
108+
{
109+
if (callback is Action<bool> action) action?.Invoke(someBool);
110+
else
111+
Debug.LogError(
112+
$"Callback with key {key} is of unsupported type or incorrect number of arguments: {someBool}!");
113+
}
114+
}
104115

105116

106117

Assets/PlayroomKit/modules/Interfaces/IPlayroomBase.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public void InsertCoin(InitOptions options = null, Action onLaunchCallBack = nul
7373

7474
#region Discord
7575
public void OpenDiscordInviteDialog(Action callback = null);
76+
public void OpenDiscordExternalLink(string url, Action<bool> callback = null);
7677
public void StartDiscordPurchase(string skuId, Action<string> callback, Action<string> onError = null);
7778
public void GetDiscordSkus(Action<List<DiscordSku>> callback);
7879
public void GetDiscordEntitlements(Action<List<DiscordEntitlement>> callback);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,12 @@ public void SubscribeDiscordEvent(SDKEvent eventName, Action<string> callback)
348348
{
349349
DebugLogger.LogWarning("[MockMode] Discord events only work inside discord!");
350350
}
351+
352+
public void OpenDiscordExternalLink(string url, Action<bool> callback = null)
353+
{
354+
DebugLogger.LogWarning("[MockMode] Discord external link is currently not supported in browser mock mode!");
355+
callback?.Invoke(true);
356+
}
351357
#endregion
352358
}
353359
#endif

Assets/PlayroomKit/modules/MockMode/LocalPlayroomService.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,12 @@ public void SubscribeDiscordEvent(SDKEvent eventName, Action<string> callback)
248248
{
249249
DebugLogger.LogWarning("[MockMode] Discord events only work inside discord!");
250250
}
251+
252+
public void OpenDiscordExternalLink(string url, Action<bool> callback = null)
253+
{
254+
DebugLogger.LogWarning("[MockMode] Discord external link is currently not supported in local mode!");
255+
callback?.Invoke(true);
256+
}
251257
#endregion
252258
}
253259
}

Assets/PlayroomKit/modules/PlayroomBuildService.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,20 @@ public void OpenDiscordInviteDialog(Action callback = null)
440440
_interop.OpenDiscordInviteDialogInternalWrapper(OpenDiscordInviteDialogCallbackInvoker);
441441
}
442442

443+
public void OpenDiscordExternalLink(string url, Action<bool> callback = null)
444+
{
445+
CheckPlayRoomInitialized();
446+
CallbackManager.RegisterCallback(callback, "discordExternalLink");
447+
OpenDiscordExternalLinkInternal(url, InvokeOpenDiscordExternalLinkCallback);
448+
}
449+
450+
[MonoPInvokeCallback(typeof(Action<string>))]
451+
private static void InvokeOpenDiscordExternalLinkCallback(string openedString)
452+
{
453+
bool opened = bool.TryParse(openedString, out bool result) && result;
454+
CallbackManager.InvokeCallback("discordExternalLink", opened);
455+
}
456+
443457
[MonoPInvokeCallback(typeof(Action<string, string>))]
444458
private static void StartDiscordPurchaseCallback(string skuId, string result)
445459
{
@@ -457,7 +471,7 @@ public void StartDiscordPurchase(string skuId, Action<string> callback = null, A
457471
CheckPlayRoomInitialized();
458472
CallbackManager.RegisterCallback(callback, skuId);
459473
CallbackManager.RegisterCallback(onError, "onError");
460-
474+
461475
StartDiscordPurchaseInternal(skuId, StartDiscordPurchaseCallback, OnErrorStartPurchase);
462476
}
463477

Assets/PlayroomKit/src/index.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,27 @@ mergeInto(LibraryManager.library, {
11101110
{{{ makeDynCall('v', 'callback') }}}()
11111111
})
11121112
.catch((error) => {
1113-
console.error("Failed to open Discord invite dialog:", error);
1113+
console.error("[JSLIB] Failed to open Discord invite dialog:", error);
1114+
});
1115+
},
1116+
1117+
OpenDiscordExternalLinkInternal: function (url, callback) {
1118+
if (!window.Playroom) {
1119+
console.error(
1120+
"Playroom library is not loaded. Please make sure to call InsertCoin first."
1121+
);
1122+
return;
1123+
}
1124+
1125+
Playroom.getDiscordClient().commands.openExternalLink(UTF8ToString(url))
1126+
.then((opened) => {
1127+
var returnData = _ConvertString(opened.toString());
1128+
console.log("Discord external link opened successfully: " + opened);
1129+
1130+
{{{ makeDynCall('vi', 'callback') }}}(returnData)
1131+
})
1132+
.catch((error) => {
1133+
console.error("[JSLIB] Failed to open external link:", error);
11141134
});
11151135
},
11161136

0 commit comments

Comments
 (0)