Skip to content

Commit

Permalink
InitOption serialization fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
momintlh committed Jan 9, 2024
1 parent fef48d6 commit f6e7c65
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 45 deletions.
103 changes: 67 additions & 36 deletions Assets/PlayroomKit/PlayroomKit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ namespace Playroom
{
public class PlayroomKit
{

private static bool isPlayRoomInitialized;


/// <summary>
/// Required Mock Mode:
/// </summary>
Expand All @@ -30,23 +29,16 @@ public class InitOptions
public bool streamMode = false;
public bool allowGamepads = false;
public string baseUrl = "";

// avatars, roomCode, skipLobby, reconnectGracePeriod, maxPlayersPerRoom

public string[] avatars;

public string[] avatars = null;
public string roomCode = "";

public bool skipLobby = false;

public int reconnectGracePeriod = 0;
public int maxPlayersPerRoom;

public int? maxPlayersPerRoom;
}

private static Action InsertCoinCallback = null;

[DllImport("__Internal")]
[DllImport("__Internal")]
private static extern void InsertCoinInternal(Action callback, string options, Action<string> onPlayerJoinInternalCallback, Action<string> onQuitInternalCallback);

[MonoPInvokeCallback(typeof(Action))]
Expand All @@ -63,7 +55,8 @@ public static void InsertCoin(Action callback, InitOptions options = null)
isPlayRoomInitialized = true;
InsertCoinCallback = callback;
string optionsJson = null;
if (options != null) optionsJson = SerializeInitOptions(options);
if (options != null) { optionsJson = SerializeInitOptions(options); }
Debug.Log("C# " + optionsJson);
InsertCoinInternal(InvokeInsertCoin, optionsJson, __OnPlayerJoinCallbackHandler, __OnQuitInternalHandler);
}
else
Expand All @@ -72,7 +65,11 @@ public static void InsertCoin(Action callback, InitOptions options = null)

Debug.Log("Coin Inserted");

if (options != null && options.streamMode) mockIsStreamMode = options.streamMode;
// if (options != null && options.streamMode) mockIsStreamMode = options.streamMode;
string optionsJson = null;
if (options != null) optionsJson = SerializeInitOptions(options);

// Debug.Log(optionsJson);

callback?.Invoke();
}
Expand All @@ -82,7 +79,33 @@ private static string SerializeInitOptions(InitOptions options)
{
if (options == null) return null;

return JsonUtility.ToJson(options);
JSONNode node = JSON.Parse("{}");

node["streamMode"] = options.streamMode;
node["allowGamepads"] = options.allowGamepads;
node["baseUrl"] = options.baseUrl;

if (options.avatars != null)
{
JSONArray avatarsArray = new JSONArray();
foreach (string avatar in options.avatars)
{
avatarsArray.Add(avatar);
}
node["avatars"] = avatarsArray;
}

node["roomCode"] = options.roomCode;
node["skipLobby"] = options.skipLobby;
node["reconnectGracePeriod"] = options.reconnectGracePeriod;

// Check if maxPlayersPerRoom is provided, otherwise omit the property
if (options.maxPlayersPerRoom.HasValue)
{
node["maxPlayersPerRoom"] = options.maxPlayersPerRoom.Value;
}

return node.ToString();
}

// [DllImport("__Internal")]
Expand All @@ -98,7 +121,7 @@ private static void __OnPlayerJoinCallbackHandler(string id)
OnPlayerJoinWrapperCallback(id);
}


private static void OnPlayerJoinWrapperCallback(string id)
{
var player = GetPlayer(id);
Expand Down Expand Up @@ -248,6 +271,13 @@ public static Player Me()
[DllImport("__Internal")]
public static extern string GetRoomCode();

[MonoPInvokeCallback(typeof(Action))]
public static void OnDisconnect(Action callback)
{
callback.Invoke();
}


[DllImport("__Internal")]
private static extern void SetStateString(string key, string value, bool reliable = false);

Expand Down Expand Up @@ -663,7 +693,8 @@ private static T MockGetState<T>(string key)
}

[MonoPInvokeCallback(typeof(Action<string>))]
private static void __OnQuitInternalHandler(string playerId) {
private static void __OnQuitInternalHandler(string playerId)
{
if (Players.TryGetValue(playerId, out Player player))
{
player.OnQuitWrapperCallback();
Expand All @@ -673,8 +704,8 @@ private static void __OnQuitInternalHandler(string playerId) {
Debug.LogError("[__OnQuitInternalHandler] Couldn't find player with id " + playerId);
}
}


// Joystick
[DllImport("__Internal")]
private static extern void CreateJoystickInternal(string joyStickOptionsJson);
Expand Down Expand Up @@ -735,8 +766,8 @@ private static JSONNode ConvertButtonOptionsToJson(ButtonOptions button)
buttonJson["icon"] = button.icon;
return buttonJson;
}


public class JoystickOptions
{
public string type = "angular"; // default = angular, can be dpad
Expand All @@ -751,28 +782,28 @@ public class ButtonOptions
public string id = null;
public string label = "";
public string icon = null;
}
}

public class ZoneOptions
{
public ButtonOptions up = null;
public ButtonOptions down = null;
public ButtonOptions left = null;
public ButtonOptions right = null;
}
}


[System.Serializable]
public class Dpad
{
public string x;
public string y;
}

// Player class
public class Player
{


[Serializable]
public class Profile
Expand All @@ -783,7 +814,7 @@ public class Profile
public JsonColor jsonColor;
public string name;
public string photo;

[Serializable]
public class JsonColor
{
Expand All @@ -793,9 +824,9 @@ public class JsonColor
public string hexString;
public int hex;
}



}


Expand Down Expand Up @@ -823,7 +854,7 @@ public Player(string id)

private List<Action<string>> OnQuitCallbacks = new();


private void OnQuitDefaultCallback()
{
if (!isPlayRoomInitialized)
Expand Down Expand Up @@ -1212,7 +1243,7 @@ private static Profile ParseProfile(string json)

return profileData;
}

public Profile GetProfile()
{
if (IsRunningInBrowser())
Expand Down Expand Up @@ -1244,7 +1275,7 @@ public Profile GetProfile()
name = "CoolPlayTest",
jsonColor = mockJsonColor,
photo = "testPhoto"

};
return testProfile;
}
Expand Down Expand Up @@ -1290,5 +1321,5 @@ private void SetStateHelper<T>(string id, string key, Dictionary<string, T> valu
}
}


}
37 changes: 28 additions & 9 deletions Assets/Plugins/PlayroomPlugin.jslib
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
mergeInto(LibraryManager.library, {
/**
* @description Inserts a coin into the game by loading the required scripts and initializing the Playroom.
* @param {function} callback - A callback function to execute after the Playroom is loaded.
* @param {function} onLaunchCallBack - A callback function to execute after the Playroom is loaded.
* @param {function} onQuitInternalCallback - (internal) This C# callback function calls an OnQuit wrapper on C# side, with the player's ID.
*/
InsertCoinInternal: function (
callback,
onLaunchCallBack,
optionsJson,
onJoinCallback,
onQuitInternalCallback
onQuitInternalCallback,
// onLaunchCallback
) {
function embedScript(src) {
return new Promise((resolve, reject) => {
Expand All @@ -21,6 +22,9 @@ mergeInto(LibraryManager.library, {
});
}

var options = optionsJson ? JSON.parse(UTF8ToString(optionsJson)) : {};
// console.log(options)

Promise.all([
embedScript("https://unpkg.com/[email protected]/umd/react.development.js"),
embedScript(
Expand All @@ -36,13 +40,9 @@ mergeInto(LibraryManager.library, {
return;
}

console.info("Playroom has loaded.");

var options = optionsJson ? JSON.parse(optionsJson) : {};

Playroom.insertCoin(options)
.then(() => {
dynCall("v", callback, []);
dynCall("v", onLaunchCallBack, []);

Playroom.onPlayerJoin((player) => {
var id = player.id;
Expand All @@ -65,6 +65,8 @@ mergeInto(LibraryManager.library, {
});
},



/**
* @description Checks whether the player is the host of the game.
* @returns {boolean} True if the local player is the host, otherwise false.
Expand Down Expand Up @@ -639,5 +641,22 @@ mergeInto(LibraryManager.library, {
var buffer = _malloc(bufferSize);
stringToUTF8(roomCode, buffer, bufferSize);
return buffer;
}
},

OnDisconnect: function (callback) {
if (!window.Playroom) {
console.error(
"Playroom library is not loaded. Please make sure to call InsertCoin first."
);
return;
}

Playroom.onDisconnect((e) => {
console.log(`Disconnected!`, e.code, e.reason);
dynCall("v", callback, [])
});

},


});

0 comments on commit f6e7c65

Please sign in to comment.