From c9bf828cc66201d925ccf8693de055ede35347b0 Mon Sep 17 00:00:00 2001 From: msciotti Date: Tue, 20 Nov 2018 18:40:54 -0800 Subject: [PATCH] Fix indenting --- .../Assets/DiscordController.cs | 176 ++++----- examples/button-clicker/Assets/DiscordRpc.cs | 358 +++++++++--------- 2 files changed, 267 insertions(+), 267 deletions(-) diff --git a/examples/button-clicker/Assets/DiscordController.cs b/examples/button-clicker/Assets/DiscordController.cs index 65f44a1..3866ece 100644 --- a/examples/button-clicker/Assets/DiscordController.cs +++ b/examples/button-clicker/Assets/DiscordController.cs @@ -11,108 +11,108 @@ public class DiscordJoinRequestEvent : UnityEngine.Events.UnityEvent _buffers = new List(10); - - public string state; /* max 128 bytes */ - public string details; /* max 128 bytes */ - public long startTimestamp; - public long endTimestamp; - public string largeImageKey; /* max 32 bytes */ - public string largeImageText; /* max 128 bytes */ - public string smallImageKey; /* max 32 bytes */ - public string smallImageText; /* max 128 bytes */ - public string partyId; /* max 128 bytes */ - public int partySize; - public int partyMax; - public string matchSecret; /* max 128 bytes */ - public string joinSecret; /* max 128 bytes */ - public string spectateSecret; /* max 128 bytes */ - public bool instance; - - /// - /// Get the reprensentation of this instance - /// - /// reprensentation of this instance - internal RichPresenceStruct GetStruct() + public struct EventHandlers { - if (_buffers.Count > 0) - { - FreeMem(); - } - - _presence.state = StrToPtr(state); - _presence.details = StrToPtr(details); - _presence.startTimestamp = startTimestamp; - _presence.endTimestamp = endTimestamp; - _presence.largeImageKey = StrToPtr(largeImageKey); - _presence.largeImageText = StrToPtr(largeImageText); - _presence.smallImageKey = StrToPtr(smallImageKey); - _presence.smallImageText = StrToPtr(smallImageText); - _presence.partyId = StrToPtr(partyId); - _presence.partySize = partySize; - _presence.partyMax = partyMax; - _presence.matchSecret = StrToPtr(matchSecret); - _presence.joinSecret = StrToPtr(joinSecret); - _presence.spectateSecret = StrToPtr(spectateSecret); - _presence.instance = instance; - - return _presence; + public OnReadyInfo readyCallback; + public OnDisconnectedInfo disconnectedCallback; + public OnErrorInfo errorCallback; + public OnJoinInfo joinCallback; + public OnSpectateInfo spectateCallback; + public OnRequestInfo requestCallback; } - /// - /// Returns a pointer to a representation of the given string with a size of maxbytes - /// - /// String to convert - /// Pointer to the UTF-8 representation of - private IntPtr StrToPtr(string input) + [Serializable, StructLayout(LayoutKind.Sequential)] + public struct RichPresenceStruct { - if (string.IsNullOrEmpty(input)) return IntPtr.Zero; - var convbytecnt = Encoding.UTF8.GetByteCount(input); - var buffer = Marshal.AllocHGlobal(convbytecnt + 1); - for (int i = 0; i < convbytecnt + 1; i++) - { - Marshal.WriteByte(buffer, i, 0); - } - _buffers.Add(buffer); - Marshal.Copy(Encoding.UTF8.GetBytes(input), 0, buffer, convbytecnt); - return buffer; + public IntPtr state; /* max 128 bytes */ + public IntPtr details; /* max 128 bytes */ + public long startTimestamp; + public long endTimestamp; + public IntPtr largeImageKey; /* max 32 bytes */ + public IntPtr largeImageText; /* max 128 bytes */ + public IntPtr smallImageKey; /* max 32 bytes */ + public IntPtr smallImageText; /* max 128 bytes */ + public IntPtr partyId; /* max 128 bytes */ + public int partySize; + public int partyMax; + public IntPtr matchSecret; /* max 128 bytes */ + public IntPtr joinSecret; /* max 128 bytes */ + public IntPtr spectateSecret; /* max 128 bytes */ + public bool instance; } - /// - /// Convert string to UTF-8 and add null termination - /// - /// string to convert - /// UTF-8 representation of with added null termination - private static string StrToUtf8NullTerm(string toconv) + [Serializable] + public struct DiscordUser { - var str = toconv.Trim(); - var bytes = Encoding.Default.GetBytes(str); - if (bytes.Length > 0 && bytes[bytes.Length - 1] != 0) - { - str += "\0\0"; - } - return Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(str)); + public string userId; + public string username; + public string discriminator; + public string avatar; } - /// - /// Free the allocated memory for conversion to - /// - internal void FreeMem() + public enum Reply { - for (var i = _buffers.Count - 1; i >= 0; i--) - { - Marshal.FreeHGlobal(_buffers[i]); - _buffers.RemoveAt(i); - } + No = 0, + Yes = 1, + Ignore = 2 + } + + [DllImport("discord-rpc", EntryPoint = "Discord_Initialize", CallingConvention = CallingConvention.Cdecl)] + public static extern void Initialize(string applicationId, ref EventHandlers handlers, bool autoRegister, string optionalSteamId); + + [DllImport("discord-rpc", EntryPoint = "Discord_Shutdown", CallingConvention = CallingConvention.Cdecl)] + public static extern void Shutdown(); + + [DllImport("discord-rpc", EntryPoint = "Discord_RunCallbacks", CallingConvention = CallingConvention.Cdecl)] + public static extern void RunCallbacks(); + + [DllImport("discord-rpc", EntryPoint = "Discord_UpdatePresence", CallingConvention = CallingConvention.Cdecl)] + private static extern void UpdatePresenceNative(ref RichPresenceStruct presence); + + [DllImport("discord-rpc", EntryPoint = "Discord_ClearPresence", CallingConvention = CallingConvention.Cdecl)] + public static extern void ClearPresence(); + + [DllImport("discord-rpc", EntryPoint = "Discord_Respond", CallingConvention = CallingConvention.Cdecl)] + public static extern void Respond(string userId, Reply reply); + + [DllImport("discord-rpc", EntryPoint = "Discord_UpdateHandlers", CallingConvention = CallingConvention.Cdecl)] + public static extern void UpdateHandlers(ref EventHandlers handlers); + + public static void UpdatePresence(RichPresence presence) + { + var presencestruct = presence.GetStruct(); + UpdatePresenceNative(ref presencestruct); + presence.FreeMem(); + } + + public class RichPresence + { + private RichPresenceStruct _presence; + private readonly List _buffers = new List(10); + + public string state; /* max 128 bytes */ + public string details; /* max 128 bytes */ + public long startTimestamp; + public long endTimestamp; + public string largeImageKey; /* max 32 bytes */ + public string largeImageText; /* max 128 bytes */ + public string smallImageKey; /* max 32 bytes */ + public string smallImageText; /* max 128 bytes */ + public string partyId; /* max 128 bytes */ + public int partySize; + public int partyMax; + public string matchSecret; /* max 128 bytes */ + public string joinSecret; /* max 128 bytes */ + public string spectateSecret; /* max 128 bytes */ + public bool instance; + + /// + /// Get the reprensentation of this instance + /// + /// reprensentation of this instance + internal RichPresenceStruct GetStruct() + { + if (_buffers.Count > 0) + { + FreeMem(); + } + + _presence.state = StrToPtr(state); + _presence.details = StrToPtr(details); + _presence.startTimestamp = startTimestamp; + _presence.endTimestamp = endTimestamp; + _presence.largeImageKey = StrToPtr(largeImageKey); + _presence.largeImageText = StrToPtr(largeImageText); + _presence.smallImageKey = StrToPtr(smallImageKey); + _presence.smallImageText = StrToPtr(smallImageText); + _presence.partyId = StrToPtr(partyId); + _presence.partySize = partySize; + _presence.partyMax = partyMax; + _presence.matchSecret = StrToPtr(matchSecret); + _presence.joinSecret = StrToPtr(joinSecret); + _presence.spectateSecret = StrToPtr(spectateSecret); + _presence.instance = instance; + + return _presence; + } + + /// + /// Returns a pointer to a representation of the given string with a size of maxbytes + /// + /// String to convert + /// Pointer to the UTF-8 representation of + private IntPtr StrToPtr(string input) + { + if (string.IsNullOrEmpty(input)) return IntPtr.Zero; + var convbytecnt = Encoding.UTF8.GetByteCount(input); + var buffer = Marshal.AllocHGlobal(convbytecnt + 1); + for (int i = 0; i < convbytecnt + 1; i++) + { + Marshal.WriteByte(buffer, i, 0); + } + _buffers.Add(buffer); + Marshal.Copy(Encoding.UTF8.GetBytes(input), 0, buffer, convbytecnt); + return buffer; + } + + /// + /// Convert string to UTF-8 and add null termination + /// + /// string to convert + /// UTF-8 representation of with added null termination + private static string StrToUtf8NullTerm(string toconv) + { + var str = toconv.Trim(); + var bytes = Encoding.Default.GetBytes(str); + if (bytes.Length > 0 && bytes[bytes.Length - 1] != 0) + { + str += "\0\0"; + } + return Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(str)); + } + + /// + /// Free the allocated memory for conversion to + /// + internal void FreeMem() + { + for (var i = _buffers.Count - 1; i >= 0; i--) + { + Marshal.FreeHGlobal(_buffers[i]); + _buffers.RemoveAt(i); + } + } } - } } \ No newline at end of file