Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion Assets/PlayroomKit/PlayroomKit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,24 @@ public void ClearTurns(Action callback = null)

#endregion

#region Discord
#region Discord Helpers
public static bool IsDicordContext()
{
return Application.absoluteURL.Contains("discord");
}

private static bool ValidateDiscord(string warningMessage)
{
if (!IsDicordContext())
{
UnityEngine.Debug.LogWarning($"[PlayroomDiscord] {warningMessage}");
return false;
}
return true;
}
#endregion

#region Discord
public void OpenDiscordInviteDialog(Action callback = null)
{
CheckPlayRoomInitialized();
Expand Down Expand Up @@ -391,6 +408,14 @@ public void SubscribeDiscordEvent(SDKEvent eventName, Action<string> callback)
_playroomService.SubscribeDiscordEvent(eventName, callback);
}

public void PatchDiscordUrlMappings(List<Mapping> mappings, PatchUrlMappingsConfig config = null)
{
if (!ValidateDiscord("PatchUrlMappings only works inside discord."))
return;

_playroomService.PatchDiscordUrlMappings(mappings, config);
}

#endregion
}
}
23 changes: 23 additions & 0 deletions Assets/PlayroomKit/modules/Discord/Utils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Collections.Generic;

namespace Discord
{
public class Mapping
{
public string Prefix { get; set; }
public string Target { get; set; }
}

public class RemapInput {
public string URL { get; set; }
public List<Mapping> Mappings { get; set; }
}

public class PatchUrlMappingsConfig
{
public bool PatchFetch { get; set; } = true;
public bool PatchWebSocket { get; set; } = true;
public bool PatchXhr { get; set; } = true;
public bool PatchSrcAttributes { get; set; } = false;
}
}
11 changes: 11 additions & 0 deletions Assets/PlayroomKit/modules/Discord/Utils.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Assets/PlayroomKit/modules/Headers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ private static extern string GetPersistentDataInternal(string key,

[DllImport("__Internal")]
private static extern void SubscribeDiscordInternal(string eventName, Action<string, string> callback);

[DllImport("__Internal")]
private static extern void PatchDiscordUrlMappingsInternal(string prefix, string target);
#endregion
}
}
1 change: 1 addition & 0 deletions Assets/PlayroomKit/modules/Interfaces/IPlayroomBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public void InsertCoin(InitOptions options = null, Action onLaunchCallBack = nul
public void GetDiscordEntitlements(Action<List<DiscordEntitlement>> callback);
public void DiscordPriceFormat(float price, string currency, string locale, Action<string> callback);
public void SubscribeDiscordEvent(SDKEvent eventName, Action<string> callback);
public void PatchDiscordUrlMappings(List<Mapping> mappings, PatchUrlMappingsConfig config = null);
#endregion


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using Discord;
using System.Collections.Generic;

namespace Playroom
{
public interface IPlayroomBuildExtensions
Expand All @@ -7,5 +10,7 @@ public interface IPlayroomBuildExtensions
void SetState(string key, bool value, bool reliable = false);
void SetState(string key, float value, bool reliable = false);
void SetState(string key, object value, bool reliable = false);

void PatchDiscordUrlMappings(List<Mapping> mappings, PatchUrlMappingsConfig config = null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,11 @@ public void OpenDiscordExternalLink(string url, Action<string> callback = null)
DebugLogger.LogWarning("[MockMode] Discord external link is currently not supported in browser mock mode!");
callback?.Invoke("true");
}

public void PatchDiscordUrlMappings(List<Mapping> mappings, PatchUrlMappingsConfig config = null)
{
DebugLogger.LogWarning("[MockMode] Patch Discord URL Mappings is currently not supported in browser mock mode!");
}
#endregion
}
#endif
Expand Down
5 changes: 5 additions & 0 deletions Assets/PlayroomKit/modules/MockMode/LocalPlayroomService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,11 @@ public void OpenDiscordExternalLink(string url, Action<string> callback = null)
DebugLogger.LogWarning("[MockMode] Discord external link is currently not supported in local mode!");
callback?.Invoke("true");
}

public void PatchDiscordUrlMappings(List<Mapping> mappings, PatchUrlMappingsConfig config = null)
{
DebugLogger.LogWarning("[MockMode] Patch Discord URL Mappings is currently not supported in local mode!");
}
#endregion
}
}
9 changes: 8 additions & 1 deletion Assets/PlayroomKit/modules/PlayroomBuildService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,14 @@ private static void ClearTurnsCallback()
#endregion

#region Discord API
public void PatchDiscordUrlMappings(List<Mapping> mappings, PatchUrlMappingsConfig config = null)
{
for (int i = 0; i < mappings.Count; i++)
{
PatchDiscordUrlMappingsInternal(mappings[i].Prefix, mappings[i].Target);
}
}

public void OpenDiscordInviteDialog(Action callback = null)
{
CheckPlayRoomInitialized();
Expand Down Expand Up @@ -598,7 +606,6 @@ private static void InvokeOnErrorInsertCoin(string error)
_onError?.Invoke(error);
Debug.LogException(new Exception(error));
}

#endregion
}
}
Expand Down
22 changes: 18 additions & 4 deletions Assets/PlayroomKit/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1208,8 +1208,7 @@ mergeInto(LibraryManager.library, {

},

// callback variant
DiscordPriceFormatInternal: function(amount, currencyOrPtr, localeOrPtr, callbackPtr) {
DiscordPriceFormatInternal: function(amount, currencyOrPtr, localeOrPtr, callbackPtr) {
var currency = (typeof currencyOrPtr === 'string')
? currencyOrPtr
: UTF8ToString(currencyOrPtr);
Expand All @@ -1229,9 +1228,24 @@ mergeInto(LibraryManager.library, {
{{{ makeDynCall("vi", "callbackPtr") }}}(0);
});
},

//#endregion

PatchDiscordUrlMappingsInternal: function (prefix, target) {
prefix = UTF8ToString(prefix);
target = UTF8ToString(target);

console.log(`[JSLIB]: PatchingURL: ${prefix} and ${target}`);

try {
Playroom.getDiscordSDK().then(discordSDK => {
discordSDK.patchUrlMappings([{ prefix: prefix, target: target }]);
}).catch(err => {
console.error("Discord SDK load failed:", err);
});
} catch (error) {
console.error(`[JSLIB] error PatchUrlMappingsInternal: ${error}`);
}
},
//#endregion

//#region Utils
GetPlayroomTokenInternal: function () {
Expand Down
32 changes: 14 additions & 18 deletions Assets/Scenes/TestScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -266,31 +266,14 @@ GameObject:
serializedVersion: 6
m_Component:
- component: {fileID: 1054460207}
- component: {fileID: 1054460206}
- component: {fileID: 1054460208}
m_Layer: 0
m_Name: GameManager
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1054460206
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1054460205}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: dc3a2b9cdc24aab40906ce4bcdee9943, type: 3}
m_Name:
m_EditorClassIdentifier:
baseUrl: https://ws.joinplayroom.com/api/store
gameApiKey:
text: {fileID: 1547749}
skus: []
entitlements: []
--- !u!4 &1054460207
Transform:
m_ObjectHideFlags: 0
Expand All @@ -306,6 +289,19 @@ Transform:
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1054460208
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1054460205}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 75cfd53c3bec10f4cbfc44d61b3f260c, type: 3}
m_Name:
m_EditorClassIdentifier:
responseText: {fileID: 1547749}
--- !u!1001 &1407290422
PrefabInstance:
m_ObjectHideFlags: 0
Expand Down
58 changes: 40 additions & 18 deletions Assets/Scripts/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public class GameManager : MonoBehaviour

#endregion


#region Debug UI
[Header("Debug UI")]
private bool showDebugWindow = false;
Expand All @@ -58,6 +57,13 @@ public class GameManager : MonoBehaviour
private string discordSkusText = "";
private string hqEntitlementsText = "";
private string hqSkusText = "";


List<Mapping> mappings = new()
{
new Mapping() { Prefix = "json", Target = "jsonplaceholder.typicode.com", }
};

#endregion

#region Custom Classes
Expand All @@ -70,19 +76,27 @@ public class CustomMetadataClass

#region Unity Lifecycle



void Awake()
IEnumerator GetRequest(string url, Action<string> onComplete)
{
if (Application.absoluteURL.Contains("discord"))
using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
{
baseUrl = ".proxy/_ws/api/store";
}
else
{
baseUrl = "https://ws.joinplayroom.com/api/store";
yield return webRequest.SendWebRequest();
if (webRequest.result == UnityWebRequest.Result.Success)
{
Debug.Log("Response: " + webRequest.downloadHandler.text);
onComplete?.Invoke(webRequest.downloadHandler.text);
}
else
{
Debug.LogError("Error: " + webRequest.error);
onComplete?.Invoke(webRequest.downloadHandler.text);
}
}
}


void Awake()
{
// Initialize fake Discord SKUs
discordSkus = new List<DiscordSku>
{
Expand Down Expand Up @@ -157,14 +171,15 @@ private void Update()

if (Input.GetKeyDown(KeyCode.P))
{
playroomKit.StartDiscordPurchase(skuId, (response) =>
{
discordEntitlements = DiscordEntitlement.FromJSON(response);
debugText = "Purchase completed!\n" + response;
}, (onErrorResponse) =>

Debug.LogWarning("Patching Discord URL Mappings");

playroomKit.PatchDiscordUrlMappings(new()
{
text.text = onErrorResponse;
new Mapping() { Prefix = ".proxy/json", Target = "jsonplaceholder.typicode.com", },
});


}

if (Input.GetKeyDown(KeyCode.T))
Expand Down Expand Up @@ -221,6 +236,15 @@ private void Update()
});
}

if (Input.GetKeyDown(KeyCode.J))
{
StartCoroutine(GetRequest("https://jsonplaceholder.typicode.com/todos/1", (response) =>
{
text.text = "Response from JSON Placeholder: " + response;
Debug.Log("Response: " + response);
}));
}

}
#endregion

Expand Down Expand Up @@ -461,6 +485,4 @@ GUIStyle EditorLabelBold()
return style;
}
#endregion


}
Loading