Skip to content

Commit 4145268

Browse files
committed
Merge branch 'patchUrlMapping'
2 parents 605eec3 + 3d8fe24 commit 4145268

File tree

14 files changed

+275
-107
lines changed

14 files changed

+275
-107
lines changed

Assets/PlayroomKit/PlayroomKit.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,24 @@ public void ClearTurns(Action callback = null)
348348

349349
#endregion
350350

351-
#region Discord
351+
#region Discord Helpers
352+
public static bool IsDicordContext()
353+
{
354+
return Application.absoluteURL.Contains("discord");
355+
}
356+
357+
private static bool ValidateDiscord(string warningMessage)
358+
{
359+
if (!IsDicordContext())
360+
{
361+
UnityEngine.Debug.LogWarning($"[PlayroomDiscord] {warningMessage}");
362+
return false;
363+
}
364+
return true;
365+
}
366+
#endregion
367+
368+
#region Discord
352369
public void OpenDiscordInviteDialog(Action callback = null)
353370
{
354371
CheckPlayRoomInitialized();
@@ -391,6 +408,14 @@ public void SubscribeDiscordEvent(SDKEvent eventName, Action<string> callback)
391408
_playroomService.SubscribeDiscordEvent(eventName, callback);
392409
}
393410

411+
public void PatchDiscordUrlMappings(List<Mapping> mappings, PatchUrlMappingsConfig config = null)
412+
{
413+
if (!ValidateDiscord("PatchUrlMappings only works inside discord."))
414+
return;
415+
416+
_playroomService.PatchDiscordUrlMappings(mappings, config);
417+
}
418+
394419
#endregion
395420
}
396421
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Collections.Generic;
2+
3+
namespace Discord
4+
{
5+
public class Mapping
6+
{
7+
public string Prefix { get; set; }
8+
public string Target { get; set; }
9+
}
10+
11+
public class RemapInput {
12+
public string URL { get; set; }
13+
public List<Mapping> Mappings { get; set; }
14+
}
15+
16+
public class PatchUrlMappingsConfig
17+
{
18+
public bool PatchFetch { get; set; } = true;
19+
public bool PatchWebSocket { get; set; } = true;
20+
public bool PatchXhr { get; set; } = true;
21+
public bool PatchSrcAttributes { get; set; } = false;
22+
}
23+
}

Assets/PlayroomKit/modules/Discord/Utils.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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ private static extern string GetPersistentDataInternal(string key,
187187

188188
[DllImport("__Internal")]
189189
private static extern void SubscribeDiscordInternal(string eventName, Action<string, string> callback);
190+
191+
[DllImport("__Internal")]
192+
private static extern void PatchDiscordUrlMappingsInternal(string prefix, string target);
190193
#endregion
191194
}
192195
}

Assets/PlayroomKit/modules/Interfaces/IPlayroomBase.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public void InsertCoin(InitOptions options = null, Action onLaunchCallBack = nul
7979
public void GetDiscordEntitlements(Action<List<DiscordEntitlement>> callback);
8080
public void DiscordPriceFormat(float price, string currency, string locale, Action<string> callback);
8181
public void SubscribeDiscordEvent(SDKEvent eventName, Action<string> callback);
82+
public void PatchDiscordUrlMappings(List<Mapping> mappings, PatchUrlMappingsConfig config = null);
8283
#endregion
8384

8485

Assets/PlayroomKit/modules/Interfaces/IPlayroomBuildExtensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
using Discord;
2+
using System.Collections.Generic;
3+
14
namespace Playroom
25
{
36
public interface IPlayroomBuildExtensions
@@ -7,5 +10,7 @@ public interface IPlayroomBuildExtensions
710
void SetState(string key, bool value, bool reliable = false);
811
void SetState(string key, float value, bool reliable = false);
912
void SetState(string key, object value, bool reliable = false);
13+
14+
void PatchDiscordUrlMappings(List<Mapping> mappings, PatchUrlMappingsConfig config = null);
1015
}
1116
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,11 @@ public void OpenDiscordExternalLink(string url, Action<string> callback = null)
354354
DebugLogger.LogWarning("[MockMode] Discord external link is currently not supported in browser mock mode!");
355355
callback?.Invoke("true");
356356
}
357+
358+
public void PatchDiscordUrlMappings(List<Mapping> mappings, PatchUrlMappingsConfig config = null)
359+
{
360+
DebugLogger.LogWarning("[MockMode] Patch Discord URL Mappings is currently not supported in browser mock mode!");
361+
}
357362
#endregion
358363
}
359364
#endif

Assets/PlayroomKit/modules/MockMode/LocalPlayroomService.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,11 @@ public void OpenDiscordExternalLink(string url, Action<string> callback = null)
254254
DebugLogger.LogWarning("[MockMode] Discord external link is currently not supported in local mode!");
255255
callback?.Invoke("true");
256256
}
257+
258+
public void PatchDiscordUrlMappings(List<Mapping> mappings, PatchUrlMappingsConfig config = null)
259+
{
260+
DebugLogger.LogWarning("[MockMode] Patch Discord URL Mappings is currently not supported in local mode!");
261+
}
257262
#endregion
258263
}
259264
}

Assets/PlayroomKit/modules/PlayroomBuildService.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,14 @@ private static void ClearTurnsCallback()
433433
#endregion
434434

435435
#region Discord API
436+
public void PatchDiscordUrlMappings(List<Mapping> mappings, PatchUrlMappingsConfig config = null)
437+
{
438+
for (int i = 0; i < mappings.Count; i++)
439+
{
440+
PatchDiscordUrlMappingsInternal(mappings[i].Prefix, mappings[i].Target);
441+
}
442+
}
443+
436444
public void OpenDiscordInviteDialog(Action callback = null)
437445
{
438446
CheckPlayRoomInitialized();
@@ -598,7 +606,6 @@ private static void InvokeOnErrorInsertCoin(string error)
598606
_onError?.Invoke(error);
599607
Debug.LogException(new Exception(error));
600608
}
601-
602609
#endregion
603610
}
604611
}

Assets/PlayroomKit/src/index.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,8 +1208,7 @@ mergeInto(LibraryManager.library, {
12081208

12091209
},
12101210

1211-
// callback variant
1212-
DiscordPriceFormatInternal: function(amount, currencyOrPtr, localeOrPtr, callbackPtr) {
1211+
DiscordPriceFormatInternal: function(amount, currencyOrPtr, localeOrPtr, callbackPtr) {
12131212
var currency = (typeof currencyOrPtr === 'string')
12141213
? currencyOrPtr
12151214
: UTF8ToString(currencyOrPtr);
@@ -1229,9 +1228,24 @@ mergeInto(LibraryManager.library, {
12291228
{{{ makeDynCall("vi", "callbackPtr") }}}(0);
12301229
});
12311230
},
1232-
1233-
//#endregion
1231+
1232+
PatchDiscordUrlMappingsInternal: function (prefix, target) {
1233+
prefix = UTF8ToString(prefix);
1234+
target = UTF8ToString(target);
12341235

1236+
console.log(`[JSLIB]: PatchingURL: ${prefix} and ${target}`);
1237+
1238+
try {
1239+
Playroom.getDiscordSDK().then(discordSDK => {
1240+
discordSDK.patchUrlMappings([{ prefix: prefix, target: target }]);
1241+
}).catch(err => {
1242+
console.error("Discord SDK load failed:", err);
1243+
});
1244+
} catch (error) {
1245+
console.error(`[JSLIB] error PatchUrlMappingsInternal: ${error}`);
1246+
}
1247+
},
1248+
//#endregion
12351249

12361250
//#region Utils
12371251
GetPlayroomTokenInternal: function () {

0 commit comments

Comments
 (0)