From dc5885f8d0a943a63292c72c233e7b929e114902 Mon Sep 17 00:00:00 2001 From: Alon Farchy <34091646+afarchy@users.noreply.github.com> Date: Fri, 12 Jul 2024 19:09:32 -0700 Subject: [PATCH] com.virtualmaker.rpc 1.0.0-preview.3 (#3) - Fixes to make WebGL RPC work, add logging --------- Co-authored-by: Stephen Hodgson --- .../com.virtualmaker.rpc/Runtime/UnityRpc.cs | 36 +++++++++++++---- .../com.virtualmaker.rpc/Runtime/UnityRpc.ts | 12 ++++-- .../Runtime/UnityRpcLog.cs | 26 ++++++++++++ .../Runtime/UnityRpcLog.cs.meta | 2 + .../Runtime/UnityRpcTransportNone.cs | 40 +++++++++---------- .../Runtime/UnityRpcTransportWebGL.cs | 2 + .../Runtime/UnityRpcTransportWebGL.jslib | 2 + .../com.virtualmaker.rpc/package.json | 2 +- 8 files changed, 90 insertions(+), 32 deletions(-) create mode 100644 VirtualMaker.RPC/Packages/com.virtualmaker.rpc/Runtime/UnityRpcLog.cs create mode 100644 VirtualMaker.RPC/Packages/com.virtualmaker.rpc/Runtime/UnityRpcLog.cs.meta diff --git a/VirtualMaker.RPC/Packages/com.virtualmaker.rpc/Runtime/UnityRpc.cs b/VirtualMaker.RPC/Packages/com.virtualmaker.rpc/Runtime/UnityRpc.cs index 7706781..f3e1a0d 100644 --- a/VirtualMaker.RPC/Packages/com.virtualmaker.rpc/Runtime/UnityRpc.cs +++ b/VirtualMaker.RPC/Packages/com.virtualmaker.rpc/Runtime/UnityRpc.cs @@ -60,13 +60,16 @@ public void Stop() { _cancellationTokenSource?.Cancel(); _cancellationTokenSource?.Dispose(); + _cancellationTokenSource = null; } - public Task CallAsync(string evt, params object[] args) + public Task CallAsync(string func, params object[] args) { + UnityRpcLog.Info($"Calling function: {func}"); + var message = new Message { - Event = evt, + Event = func, Args = args.Select(JToken.FromObject).ToArray(), RpcId = _rpcId++ }; @@ -84,6 +87,8 @@ public Task CallAsync(string evt, params object[] args) public void RaiseEvent(string evt, params object[] args) { + UnityRpcLog.Info($"Raising event: {evt}"); + var message = new Message { Event = evt, @@ -96,6 +101,8 @@ public void RaiseEvent(string evt, params object[] args) public void SubscribeDelegate(string evt, Delegate callback) { + UnityRpcLog.Info($"Subscribing to event: {evt}"); + if (!_subscriptions.TryGetValue(evt, out var callbacks)) { callbacks = new List(); @@ -107,6 +114,8 @@ public void SubscribeDelegate(string evt, Delegate callback) public void UnsubscribeDelegate(string evt, Delegate callback) { + UnityRpcLog.Info($"Unsubscribing from event: {evt}"); + if (_subscriptions == null) { return; @@ -158,12 +167,19 @@ public void RemoveRpc(string evt, Func private async void ProcessQueue(CancellationToken cancellationToken) { - while (_transport.ReceiveQueue.TryDequeue(out var message) && - !cancellationToken.IsCancellationRequested) + while (!cancellationToken.IsCancellationRequested) { await Awaiters.UnityMainThread; - if (cancellationToken.IsCancellationRequested) { return; } + if (cancellationToken.IsCancellationRequested) + { + break; + } + + if (!_transport.ReceiveQueue.TryDequeue(out var message)) + { + continue; + } try { @@ -173,15 +189,18 @@ private async void ProcessQueue(CancellationToken cancellationToken) { if (_rpcs == null || !_rpcs.Remove(parsed.ResponseId.Value, out var callback)) { + UnityRpcLog.Error($"No response callback found for RPC ID: {parsed.ResponseId}"); continue; } + UnityRpcLog.Info($"Calling response callback for RPC ID {parsed.ResponseId} with value {parsed.Args[0]}"); callback(parsed.Args[0]); continue; } if (_subscriptions == null || !_subscriptions.TryGetValue(parsed.Event, out var callbacks)) { + UnityRpcLog.Info($"No subscribers for event {parsed.Event}"); continue; } @@ -200,6 +219,7 @@ private async void ProcessQueue(CancellationToken cancellationToken) args[i] = arg.ToObject(parameter.ParameterType); } + UnityRpcLog.Info($"Calling subscriber with for event {parsed.Event} with RPC ID {parsed.RpcId} args {string.Join(", ", args)}"); var result = callback.DynamicInvoke(args); if (result == null) { continue; } @@ -211,18 +231,20 @@ private async void ProcessQueue(CancellationToken cancellationToken) ResponseId = parsed.RpcId }; + UnityRpcLog.Info($"Responding to {parsed.Event} with RPC ID {parsed.RpcId} with value {result}"); + var json = JsonConvert.SerializeObject(response); _transport.SendMessage(json); } catch (Exception e) { - Debug.LogException(e); + UnityRpcLog.Exception(e); } } } catch (Exception e) { - Debug.LogException(e); + UnityRpcLog.Exception(e); } } } diff --git a/VirtualMaker.RPC/Packages/com.virtualmaker.rpc/Runtime/UnityRpc.ts b/VirtualMaker.RPC/Packages/com.virtualmaker.rpc/Runtime/UnityRpc.ts index f1866de..3c79945 100644 --- a/VirtualMaker.RPC/Packages/com.virtualmaker.rpc/Runtime/UnityRpc.ts +++ b/VirtualMaker.RPC/Packages/com.virtualmaker.rpc/Runtime/UnityRpc.ts @@ -1,6 +1,6 @@ // Licensed under the MIT License. See LICENSE in the project root for license information. -import { writable } from "svelte/store"; +import { get, writable } from "svelte/store"; interface UnityRpcMessage { event: string; @@ -22,8 +22,7 @@ class UnityRpc { this.rpcId = 1; this.eventHandlers = {}; this.rpcs = {}; - this.window.addEventListener('merciv-unity-to-js', this.onUnityMessage.bind(this) as EventListener); - this.started.set(true); + this.window.addEventListener('unity-to-js', this.onUnityMessage.bind(this) as EventListener); } public async call(event: string, ...args: any[]): Promise { @@ -59,13 +58,18 @@ class UnityRpc { } private send(message: UnityRpcMessage) { - const event = new CustomEvent('merciv-js-to-unity', { detail: JSON.stringify(message) }); + const event = new CustomEvent('js-to-unity', { detail: JSON.stringify(message) }); this.window?.dispatchEvent(event); } private onUnityMessage(event: CustomEvent) { // Update the type of the event parameter const customEvent = event as CustomEvent; + if (!get(this.started) && customEvent.detail === 'unity-ready') { + this.started.set(true); + return; + } + const message = JSON.parse(customEvent.detail); if (message['response-id']) { diff --git a/VirtualMaker.RPC/Packages/com.virtualmaker.rpc/Runtime/UnityRpcLog.cs b/VirtualMaker.RPC/Packages/com.virtualmaker.rpc/Runtime/UnityRpcLog.cs new file mode 100644 index 0000000..8770f0a --- /dev/null +++ b/VirtualMaker.RPC/Packages/com.virtualmaker.rpc/Runtime/UnityRpcLog.cs @@ -0,0 +1,26 @@ +// Licensed under the MIT License. See LICENSE in the project root for license information. + +using System; +using UnityEngine; + +namespace VirtualMaker.RPC +{ + internal static class UnityRpcLog + { + [System.Diagnostics.Conditional("UNITY_RPC_DEBUG")] + public static void Info(string message) + { + Debug.Log($"[UnityRpc] {message}"); + } + + public static void Error(string message) + { + Debug.LogError($"[UnityRpc] {message}"); + } + + public static void Exception(Exception exception) + { + Debug.LogException(exception); + } + } +} \ No newline at end of file diff --git a/VirtualMaker.RPC/Packages/com.virtualmaker.rpc/Runtime/UnityRpcLog.cs.meta b/VirtualMaker.RPC/Packages/com.virtualmaker.rpc/Runtime/UnityRpcLog.cs.meta new file mode 100644 index 0000000..4f30a2c --- /dev/null +++ b/VirtualMaker.RPC/Packages/com.virtualmaker.rpc/Runtime/UnityRpcLog.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: e524219b1e319e64791504bccfd50710 \ No newline at end of file diff --git a/VirtualMaker.RPC/Packages/com.virtualmaker.rpc/Runtime/UnityRpcTransportNone.cs b/VirtualMaker.RPC/Packages/com.virtualmaker.rpc/Runtime/UnityRpcTransportNone.cs index 0134012..7a50572 100644 --- a/VirtualMaker.RPC/Packages/com.virtualmaker.rpc/Runtime/UnityRpcTransportNone.cs +++ b/VirtualMaker.RPC/Packages/com.virtualmaker.rpc/Runtime/UnityRpcTransportNone.cs @@ -1,20 +1,20 @@ -// Licensed under the MIT License. See LICENSE in the project root for license information. - -using System.Collections.Concurrent; -using UnityEngine; - -namespace VirtualMaker.RPC -{ - public class UnityRpcTransportNone : IUnityRpcTransport - { - public ConcurrentQueue ReceiveQueue { get; } = new(); - - public void SendMessage(string message) - { - } - - private void OnMessageInstance(string message) - { - } - } -} +// Licensed under the MIT License. See LICENSE in the project root for license information. + +using System.Collections.Concurrent; +using UnityEngine; + +namespace VirtualMaker.RPC +{ + public class UnityRpcTransportNone : IUnityRpcTransport + { + public ConcurrentQueue ReceiveQueue { get; } = new(); + + public void SendMessage(string message) + { + } + + private void OnMessageInstance(string message) + { + } + } +} diff --git a/VirtualMaker.RPC/Packages/com.virtualmaker.rpc/Runtime/UnityRpcTransportWebGL.cs b/VirtualMaker.RPC/Packages/com.virtualmaker.rpc/Runtime/UnityRpcTransportWebGL.cs index e7f1934..0749d25 100644 --- a/VirtualMaker.RPC/Packages/com.virtualmaker.rpc/Runtime/UnityRpcTransportWebGL.cs +++ b/VirtualMaker.RPC/Packages/com.virtualmaker.rpc/Runtime/UnityRpcTransportWebGL.cs @@ -36,11 +36,13 @@ public static void OnMessage(string message) public void SendMessage(string message) { + UnityRpcLog.Info($"[UnityRpcTransportWebGL] Send: {message}"); UnityRpcTransportWebGLSend(message); } private void OnMessageInstance(string message) { + UnityRpcLog.Info($"[UnityRpcTransportWebGL] Receive: {message}"); ReceiveQueue.Enqueue(message); } } diff --git a/VirtualMaker.RPC/Packages/com.virtualmaker.rpc/Runtime/UnityRpcTransportWebGL.jslib b/VirtualMaker.RPC/Packages/com.virtualmaker.rpc/Runtime/UnityRpcTransportWebGL.jslib index f58b4dc..782b328 100644 --- a/VirtualMaker.RPC/Packages/com.virtualmaker.rpc/Runtime/UnityRpcTransportWebGL.jslib +++ b/VirtualMaker.RPC/Packages/com.virtualmaker.rpc/Runtime/UnityRpcTransportWebGL.jslib @@ -17,6 +17,8 @@ const UnityRpcTransportWebGL = { _free(buffer); } }); + + window.dispatchEvent(new CustomEvent('unity-to-js', { detail: 'unity-ready' })); }, UnityRpcTransportWebGLSend: function(data) { diff --git a/VirtualMaker.RPC/Packages/com.virtualmaker.rpc/package.json b/VirtualMaker.RPC/Packages/com.virtualmaker.rpc/package.json index db2a1da..0a590c0 100644 --- a/VirtualMaker.RPC/Packages/com.virtualmaker.rpc/package.json +++ b/VirtualMaker.RPC/Packages/com.virtualmaker.rpc/package.json @@ -3,7 +3,7 @@ "displayName": "VirtualMaker.RPC", "description": "A Bidirectional RPC package for Unity (UPM)", "keywords": [], - "version": "1.0.0-preview.1", + "version": "1.0.0-preview.3", "unity": "2021.3", "documentationUrl": "https://github.com/virtual-maker-net/com.virtualmaker.rpc#documentation", "changelogUrl": "https://github.com/virtual-maker-net/com.virtualmaker.rpc/releases",