Skip to content

Commit

Permalink
Merge pull request #62 from asadm/getters-fixed
Browse files Browse the repository at this point in the history
Fix Fixed issue with getting built-in types
  • Loading branch information
momintlh authored Apr 29, 2024
2 parents b98550e + cc0c267 commit c3d31a4
Show file tree
Hide file tree
Showing 100 changed files with 16,027 additions and 779 deletions.
56 changes: 35 additions & 21 deletions Assets/PlayroomKit/PlayroomKit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1392,7 +1392,8 @@ public void SetState(string key, object value, bool reliable = false)
{
if (IsRunningInBrowser())
{
SetStateString(key, JsonUtility.ToJson(value), reliable);
string jsonString = JsonUtility.ToJson(value);
SetPlayerStateStringById(id, key, jsonString, reliable);
}
else
{
Expand All @@ -1412,26 +1413,15 @@ public T GetState<T>(string key)
{
if (IsRunningInBrowser())
{
if (typeof(T) == typeof(int))
{
return (T)(object)GetPlayerStateIntById(id, key);
}
else if (typeof(T) == typeof(float))
{
return (T)(object)GetPlayerStateFloatById(id, key);
}
else if (typeof(T) == typeof(bool))
{
return (T)(object)GetStateBool(key);
}
else if (typeof(T) == typeof(string))
{
return (T)(object)GetPlayerStateStringById(id, key);
}
else
{
return JsonUtility.FromJson<T>(GetStateString(key));
}
Type type = typeof(T);
if (type == typeof(int)) return (T)(object)GetPlayerStateIntById(id, key);
else if (type == typeof(float)) return (T)(object)GetPlayerStateFloatById(id, key);
else if (type == typeof(bool)) return (T)(object)GetPlayerStateBoolById(id, key);
else if (type == typeof(string)) return (T)(object)GetPlayerStateStringById(id, key);
else if (type == typeof(Vector3)) return (T)(object)JsonUtility.FromJson<Vector3>(GetPlayerStateStringById(id, key));
else if (type == typeof(Vector2)) return (T)(object)JsonUtility.FromJson<Vector2>(GetPlayerStateStringById(id, key));
else if (type == typeof(Quaternion)) return (T)(object)JsonUtility.FromJson<Quaternion>(GetPlayerStateStringById(id, key));
else throw new NotSupportedException($"Type {typeof(T)} is not supported by GetState");
}
else
{
Expand All @@ -1447,6 +1437,7 @@ public T GetState<T>(string key)
}
}


public Dictionary<string, T> GetStateDict<T>(string key)
{
if (IsRunningInBrowser())
Expand Down Expand Up @@ -1772,6 +1763,29 @@ private static void InvokeKickCallBack()
private static extern void WaitForPlayerStateInternal(string playerID, string stateKey, Action onStateSetCallback = null);


public static bool GetPlayerStateBoolById(string id, string key)
{
if (IsRunningInBrowser())
{
var stateValue = GetPlayerStateIntById(id, key);
return stateValue == 1 ? true :
stateValue == 0 ? false :
throw new InvalidOperationException($"GetStateBool: {key} is not a bool");
}
else
{
if (!isPlayRoomInitialized)
{
Debug.LogError("[Mock Mode] Playroom not initialized yet! Please call InsertCoin.");
return false;
}
else
{
return MockGetState<bool>(key);
}
}
}

[DllImport("__Internal")]
private static extern int GetPlayerStateIntById(string playerID, string key);

Expand Down
19 changes: 14 additions & 5 deletions Assets/PlayroomKit/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -600,11 +600,20 @@ mergeInto(LibraryManager.library, {
}

if (typeof playerState.getState === "function") {
var stateVal = playerState.getState(UTF8ToString(key));
var bufferSize = lengthBytesUTF8(stateVal) + 1;
var buffer = _malloc(bufferSize);
stringToUTF8(stateVal, buffer, bufferSize);
return buffer;
try {
var stateVal = playerState.getState(UTF8ToString(key));

if (stateVal === undefined) {
return null;
}

var bufferSize = lengthBytesUTF8(stateVal) + 1;
var buffer = _malloc(bufferSize);
stringToUTF8(stateVal, buffer, bufferSize);
return buffer;
} catch (error) {
console.log("There was an error: " + error);
}
} else {
console.error(
'The player state object does not have a "getState" method.'
Expand Down
8 changes: 8 additions & 0 deletions Assets/Prefabs.meta

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

151 changes: 151 additions & 0 deletions Assets/Prefabs/IsometricPlayer.prefab
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &2603315019977810228
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 8050795998415906310}
- component: {fileID: 2169756223537510908}
- component: {fileID: 2829385134996929394}
- component: {fileID: 6524546417377390673}
- component: {fileID: 5791266554322587629}
- component: {fileID: 7868697053123563144}
m_Layer: 0
m_Name: IsometricPlayer
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &8050795998415906310
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2603315019977810228}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 2, z: 0}
m_LocalScale: {x: 0.6, y: 0.6, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!33 &2169756223537510908
MeshFilter:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2603315019977810228}
m_Mesh: {fileID: 10202, guid: 0000000000000000e000000000000000, type: 0}
--- !u!23 &2829385134996929394
MeshRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2603315019977810228}
m_Enabled: 1
m_CastShadows: 1
m_ReceiveShadows: 1
m_DynamicOccludee: 1
m_StaticShadowCaster: 0
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RayTracingMode: 2
m_RayTraceProcedural: 0
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:
- {fileID: 2100000, guid: 7aabbde4381f93240a8176e9281d8533, type: 2}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_ReceiveGI: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 1
m_SelectedEditorRenderState: 3
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
m_AdditionalVertexStreams: {fileID: 0}
--- !u!114 &6524546417377390673
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2603315019977810228}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 04d8d418385e58d4689d3dcb9081ee41, type: 3}
m_Name:
m_EditorClassIdentifier:
_rb: {fileID: 5791266554322587629}
_speed: 5
_turnSpeed: 360
--- !u!54 &5791266554322587629
Rigidbody:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2603315019977810228}
serializedVersion: 4
m_Mass: 1
m_Drag: 0
m_AngularDrag: 0.05
m_CenterOfMass: {x: 0, y: 0, z: 0}
m_InertiaTensor: {x: 1, y: 1, z: 1}
m_InertiaRotation: {x: 0, y: 0, z: 0, w: 1}
m_IncludeLayers:
serializedVersion: 2
m_Bits: 0
m_ExcludeLayers:
serializedVersion: 2
m_Bits: 0
m_ImplicitCom: 1
m_ImplicitTensor: 1
m_UseGravity: 1
m_IsKinematic: 0
m_Interpolate: 0
m_Constraints: 0
m_CollisionDetection: 0
--- !u!65 &7868697053123563144
BoxCollider:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2603315019977810228}
m_Material: {fileID: 0}
m_IncludeLayers:
serializedVersion: 2
m_Bits: 0
m_ExcludeLayers:
serializedVersion: 2
m_Bits: 0
m_LayerOverridePriority: 0
m_IsTrigger: 0
m_ProvidesContacts: 0
m_Enabled: 1
serializedVersion: 3
m_Size: {x: 1, y: 0.99999994, z: 0.99999994}
m_Center: {x: 0, y: 0, z: 0}
7 changes: 7 additions & 0 deletions Assets/Prefabs/IsometricPlayer.prefab.meta

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

Loading

0 comments on commit c3d31a4

Please sign in to comment.