From e6390c8c4190b59f1cc98bcec688e0037b84974d Mon Sep 17 00:00:00 2001 From: Mason Sciotti Date: Tue, 27 Nov 2018 09:19:14 -0800 Subject: [PATCH] Unity il2cpp support (#249) * Initial il2cpp support attempts * Fix crashes * Different variable name * Fix indenting * Change back unneeded stuff - callbackCalls didnt seem to do anything --- .../Assets/DiscordController.cs | 11 +---- examples/button-clicker/Assets/DiscordRpc.cs | 45 +++++++++++-------- 2 files changed, 27 insertions(+), 29 deletions(-) diff --git a/examples/button-clicker/Assets/DiscordController.cs b/examples/button-clicker/Assets/DiscordController.cs index 182d31d8..5b132158 100644 --- a/examples/button-clicker/Assets/DiscordController.cs +++ b/examples/button-clicker/Assets/DiscordController.cs @@ -14,7 +14,6 @@ public class DiscordController : MonoBehaviour public DiscordRpc.RichPresence presence = new DiscordRpc.RichPresence(); public string applicationId; public string optionalSteamId; - public int callbackCalls; public int clickCounter; public DiscordRpc.DiscordUser joinRequest; public UnityEngine.Events.UnityEvent onConnect; @@ -52,41 +51,35 @@ public void RequestRespondNo() public void ReadyCallback(ref DiscordRpc.DiscordUser connectedUser) { - ++callbackCalls; Debug.Log(string.Format("Discord: connected to {0}#{1}: {2}", connectedUser.username, connectedUser.discriminator, connectedUser.userId)); onConnect.Invoke(); } public void DisconnectedCallback(int errorCode, string message) { - ++callbackCalls; Debug.Log(string.Format("Discord: disconnect {0}: {1}", errorCode, message)); onDisconnect.Invoke(); } public void ErrorCallback(int errorCode, string message) { - ++callbackCalls; Debug.Log(string.Format("Discord: error {0}: {1}", errorCode, message)); } public void JoinCallback(string secret) { - ++callbackCalls; Debug.Log(string.Format("Discord: join ({0})", secret)); onJoin.Invoke(secret); } public void SpectateCallback(string secret) { - ++callbackCalls; Debug.Log(string.Format("Discord: spectate ({0})", secret)); onSpectate.Invoke(secret); } public void RequestCallback(ref DiscordRpc.DiscordUser request) { - ++callbackCalls; Debug.Log(string.Format("Discord: join request {0}#{1}: {2}", request.username, request.discriminator, request.userId)); joinRequest = request; onJoinRequest.Invoke(request); @@ -104,10 +97,8 @@ void Update() void OnEnable() { Debug.Log("Discord: init"); - callbackCalls = 0; - handlers = new DiscordRpc.EventHandlers(); - handlers.readyCallback = ReadyCallback; + handlers.readyCallback += ReadyCallback; handlers.disconnectedCallback += DisconnectedCallback; handlers.errorCallback += ErrorCallback; handlers.joinCallback += JoinCallback; diff --git a/examples/button-clicker/Assets/DiscordRpc.cs b/examples/button-clicker/Assets/DiscordRpc.cs index af82c1c2..b4a732f0 100644 --- a/examples/button-clicker/Assets/DiscordRpc.cs +++ b/examples/button-clicker/Assets/DiscordRpc.cs @@ -2,35 +2,42 @@ using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; +using AOT; public class DiscordRpc { - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - public delegate void ReadyCallback(ref DiscordUser connectedUser); + [MonoPInvokeCallback(typeof(OnReadyInfo))] + public static void ReadyCallback(ref DiscordUser connectedUser) { } + public delegate void OnReadyInfo(ref DiscordUser connectedUser); - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - public delegate void DisconnectedCallback(int errorCode, string message); + [MonoPInvokeCallback(typeof(OnDisconnectedInfo))] + public static void DisconnectedCallback(int errorCode, string message) { } + public delegate void OnDisconnectedInfo(int errorCode, string message); - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - public delegate void ErrorCallback(int errorCode, string message); + [MonoPInvokeCallback(typeof(OnErrorInfo))] + public static void ErrorCallback(int errorCode, string message) { } + public delegate void OnErrorInfo(int errorCode, string message); - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - public delegate void JoinCallback(string secret); + [MonoPInvokeCallback(typeof(OnJoinInfo))] + public static void JoinCallback(string secret) { } + public delegate void OnJoinInfo(string secret); - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - public delegate void SpectateCallback(string secret); + [MonoPInvokeCallback(typeof(OnSpectateInfo))] + public static void SpectateCallback(string secret) { } + public delegate void OnSpectateInfo(string secret); - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - public delegate void RequestCallback(ref DiscordUser request); + [MonoPInvokeCallback(typeof(OnRequestInfo))] + public static void RequestCallback(ref DiscordUser request) { } + public delegate void OnRequestInfo(ref DiscordUser request); public struct EventHandlers { - public ReadyCallback readyCallback; - public DisconnectedCallback disconnectedCallback; - public ErrorCallback errorCallback; - public JoinCallback joinCallback; - public SpectateCallback spectateCallback; - public RequestCallback requestCallback; + public OnReadyInfo readyCallback; + public OnDisconnectedInfo disconnectedCallback; + public OnErrorInfo errorCallback; + public OnJoinInfo joinCallback; + public OnSpectateInfo spectateCallback; + public OnRequestInfo requestCallback; } [Serializable, StructLayout(LayoutKind.Sequential)] @@ -160,7 +167,7 @@ private IntPtr StrToPtr(string input) var buffer = Marshal.AllocHGlobal(convbytecnt + 1); for (int i = 0; i < convbytecnt + 1; i++) { - Marshal.WriteByte(buffer, i , 0); + Marshal.WriteByte(buffer, i, 0); } _buffers.Add(buffer); Marshal.Copy(Encoding.UTF8.GetBytes(input), 0, buffer, convbytecnt);