From 40555651472d04c3f967eacc3f90b3e94e33c17c Mon Sep 17 00:00:00 2001 From: sll552 Date: Thu, 22 Feb 2018 20:47:18 +0100 Subject: [PATCH] Update C# wrapper with visual C# compatible version (#126) - Custom serializer to fix utf-8 strings in C# --- .../Assets/DiscordController.cs | 4 +- examples/button-clicker/Assets/DiscordRpc.cs | 162 ++++++++++++++++-- 2 files changed, 148 insertions(+), 18 deletions(-) diff --git a/examples/button-clicker/Assets/DiscordController.cs b/examples/button-clicker/Assets/DiscordController.cs index fea49da..3294767 100644 --- a/examples/button-clicker/Assets/DiscordController.cs +++ b/examples/button-clicker/Assets/DiscordController.cs @@ -11,7 +11,7 @@ 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() + { + if (_buffers.Count > 0) + { + FreeMem(); + } + + _presence.state = StrToPtr(state, 128); + _presence.details = StrToPtr(details, 128); + _presence.startTimestamp = startTimestamp; + _presence.endTimestamp = endTimestamp; + _presence.largeImageKey = StrToPtr(largeImageKey, 32); + _presence.largeImageText = StrToPtr(largeImageText, 128); + _presence.smallImageKey = StrToPtr(smallImageKey, 32); + _presence.smallImageText = StrToPtr(smallImageText, 128); + _presence.partyId = StrToPtr(partyId, 128); + _presence.partySize = partySize; + _presence.partyMax = partyMax; + _presence.matchSecret = StrToPtr(matchSecret, 128); + _presence.joinSecret = StrToPtr(joinSecret, 128); + _presence.spectateSecret = StrToPtr(spectateSecret, 128); + _presence.instance = instance; + + return _presence; + } + + /// + /// Returns a pointer to a representation of the given string with a size of maxbytes + /// + /// String to convert + /// Max number of bytes to use + /// Pointer to the UTF-8 representation of + private IntPtr StrToPtr(string input, int maxbytes) + { + if (string.IsNullOrEmpty(input)) return IntPtr.Zero; + var convstr = StrClampBytes(input, maxbytes); + var convbytecnt = Encoding.UTF8.GetByteCount(convstr); + var buffer = Marshal.AllocHGlobal(convbytecnt); + _buffers.Add(buffer); + Marshal.Copy(Encoding.UTF8.GetBytes(convstr), 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)); + } + + /// + /// Clamp the string to the given byte length preserving null termination + /// + /// string to clamp + /// max bytes the resulting string should have (including null termination) + /// null terminated string with a byte length less or equal to + private static string StrClampBytes(string toclamp, int maxbytes) + { + var str = StrToUtf8NullTerm(toclamp); + var strbytes = Encoding.UTF8.GetBytes(str); + + if (strbytes.Length <= maxbytes) + { + return str; + } + + var newstrbytes = new byte[] { }; + Array.Copy(strbytes, 0, newstrbytes, 0, maxbytes - 1); + newstrbytes[newstrbytes.Length - 1] = 0; + newstrbytes[newstrbytes.Length - 2] = 0; + + return Encoding.UTF8.GetString(newstrbytes); + } + + /// + /// 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