Skip to content

Commit

Permalink
feat: separated native functions and mockmode
Browse files Browse the repository at this point in the history
  • Loading branch information
momintlh committed Jun 11, 2024
1 parent 38b3130 commit f139a5d
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 108 deletions.
90 changes: 90 additions & 0 deletions Assets/PlayroomKit/Headers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System;
using System.Runtime.InteropServices;


namespace Playroom
{
public partial class PlayroomKit
{
[DllImport("__Internal")]
private static extern void InsertCoinInternal(string options,
Action<string> onLaunchCallback,
Action<string> onQuitInternalCallback,
Action<string> onDisconnectCallback,
Action<string> onError,
string onLaunchCallBackKey,
string onDisconnectCallBackKey);

[DllImport("__Internal")]
private static extern string OnPlayerJoinInternal(Action<string> callback);

[DllImport("__Internal")]
private static extern void UnsubscribeOnPlayerJoinInternal(string id);

[DllImport("__Internal")]
private static extern bool IsHostInternal();

[DllImport("__Internal")]
private static extern bool IsStreamModeInternal();

[DllImport("__Internal")]
private static extern string MyPlayerInternal();

[DllImport("__Internal")]
public static extern string GetRoomCode();

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

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

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

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

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

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

[DllImport("__Internal")]
private static extern string GetStateStringInternal(string key);

[DllImport("__Internal")]
private static extern int GetStateIntInternal(string key);

[DllImport("__Internal")]
private static extern float GetStateFloatInternal(string key);

[DllImport("__Internal")]
private static extern string GetStateDictionaryInternal(string key);

[DllImport("__Internal")]
private static extern void WaitForStateInternal(string stateKey, Action<string, string> onStateSetCallback);

[DllImport("__Internal")]
private static extern void WaitForPlayerStateInternal(string playerID, string StateKey, Action onStateSetCallback);

[DllImport("__Internal")]
private static extern void ResetStatesInternal(string keysToExclude = null, Action OnStatesReset = null);

[DllImport("__Internal")]
private static extern void ResetPlayersStatesInternal(string keysToExclude, Action OnPlayersStatesReset = null);

[DllImport("__Internal")]
private static extern void UnsubscribeOnQuitInternal();

[DllImport("__Internal")]
private static extern void CreateJoystickInternal(string joyStickOptionsJson);

[DllImport("__Internal")]
private static extern string DpadJoystickInternal();

[DllImport("__Internal")]
private static extern void StartMatchmakingInternal(Action callback);
}
}
11 changes: 11 additions & 0 deletions Assets/PlayroomKit/Headers.cs.meta

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

42 changes: 42 additions & 0 deletions Assets/PlayroomKit/MockMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Buffers.Text;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Playroom
{
public partial class PlayroomKit
{
private const string PlayerId = "mockPlayer";
private static bool mockIsStreamMode;

/*
This is private, instead of public, to prevent tampering in Mock Mode.
Reason: In Mock Mode, only a single player can be tested.
Ref: https://docs.joinplayroom.com/usage/unity#mock-mode
*/
private static Dictionary<string, object> MockDictionary = new();

public static void MockSetState(string key, object value)
{
if (MockDictionary.ContainsKey(key))
MockDictionary[key] = value;
else
MockDictionary.Add(key, value);
}

public static T MockGetState<T>(string key)
{
if (MockDictionary.TryGetValue(key, out var value) && value is T typedValue)
{
return typedValue;
}
else
{
Debug.LogWarning($"No {key} in States or value is not of type {typeof(T)}");
return default;
}
}
}

}
11 changes: 11 additions & 0 deletions Assets/PlayroomKit/MockMode.cs.meta

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

Loading

0 comments on commit f139a5d

Please sign in to comment.