Fix indenting
This commit is contained in:
parent
a7c81fcc80
commit
c9bf828cc6
@ -11,108 +11,108 @@ public class DiscordJoinRequestEvent : UnityEngine.Events.UnityEvent<DiscordRpc.
|
|||||||
|
|
||||||
public class DiscordController : MonoBehaviour
|
public class DiscordController : MonoBehaviour
|
||||||
{
|
{
|
||||||
public DiscordRpc.RichPresence presence = new DiscordRpc.RichPresence();
|
public DiscordRpc.RichPresence presence = new DiscordRpc.RichPresence();
|
||||||
public string applicationId;
|
public string applicationId;
|
||||||
public string optionalSteamId;
|
public string optionalSteamId;
|
||||||
public int clickCounter;
|
public int clickCounter;
|
||||||
public DiscordJoinEvent onJoin;
|
public DiscordJoinEvent onJoin;
|
||||||
public DiscordJoinEvent onSpectate;
|
public DiscordJoinEvent onSpectate;
|
||||||
public DiscordJoinRequestEvent onJoinRequest;
|
public DiscordJoinRequestEvent onJoinRequest;
|
||||||
public UnityEngine.Events.UnityEvent onConnect;
|
public UnityEngine.Events.UnityEvent onConnect;
|
||||||
public UnityEngine.Events.UnityEvent onDisconnect;
|
public UnityEngine.Events.UnityEvent onDisconnect;
|
||||||
public UnityEngine.Events.UnityEvent hasResponded;
|
public UnityEngine.Events.UnityEvent hasResponded;
|
||||||
public DiscordRpc.DiscordUser joinRequest;
|
public DiscordRpc.DiscordUser joinRequest;
|
||||||
|
|
||||||
DiscordRpc.EventHandlers handlers;
|
DiscordRpc.EventHandlers handlers;
|
||||||
|
|
||||||
public void OnClick()
|
public void OnClick()
|
||||||
{
|
{
|
||||||
Debug.Log("Discord: on click!");
|
Debug.Log("Discord: on click!");
|
||||||
clickCounter++;
|
clickCounter++;
|
||||||
presence.details = string.Format("Button clicked {0} times", clickCounter);
|
presence.details = string.Format("Button clicked {0} times", clickCounter);
|
||||||
DiscordRpc.UpdatePresence(presence);
|
DiscordRpc.UpdatePresence(presence);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RequestRespondYes()
|
public void RequestRespondYes()
|
||||||
{
|
{
|
||||||
Debug.Log("Discord: responding yes to Ask to Join request");
|
Debug.Log("Discord: responding yes to Ask to Join request");
|
||||||
DiscordRpc.Respond(joinRequest.userId, DiscordRpc.Reply.Yes);
|
DiscordRpc.Respond(joinRequest.userId, DiscordRpc.Reply.Yes);
|
||||||
hasResponded.Invoke();
|
hasResponded.Invoke();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RequestRespondNo()
|
public void RequestRespondNo()
|
||||||
{
|
{
|
||||||
Debug.Log("Discord: responding no to Ask to Join request");
|
Debug.Log("Discord: responding no to Ask to Join request");
|
||||||
DiscordRpc.Respond(joinRequest.userId, DiscordRpc.Reply.No);
|
DiscordRpc.Respond(joinRequest.userId, DiscordRpc.Reply.No);
|
||||||
hasResponded.Invoke();
|
hasResponded.Invoke();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ReadyCallback(ref DiscordRpc.DiscordUser connectedUser)
|
public void ReadyCallback(ref DiscordRpc.DiscordUser connectedUser)
|
||||||
{
|
{
|
||||||
Debug.Log(string.Format("Discord: connected to {0}#{1}: {2}", connectedUser.username, connectedUser.discriminator, connectedUser.userId));
|
Debug.Log(string.Format("Discord: connected to {0}#{1}: {2}", connectedUser.username, connectedUser.discriminator, connectedUser.userId));
|
||||||
onConnect.Invoke();
|
onConnect.Invoke();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DisconnectedCallback(int errorCode, string message)
|
public void DisconnectedCallback(int errorCode, string message)
|
||||||
{
|
{
|
||||||
Debug.Log(string.Format("Discord: disconnect {0}: {1}", errorCode, message));
|
Debug.Log(string.Format("Discord: disconnect {0}: {1}", errorCode, message));
|
||||||
onDisconnect.Invoke();
|
onDisconnect.Invoke();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ErrorCallback(int errorCode, string message)
|
public void ErrorCallback(int errorCode, string message)
|
||||||
{
|
{
|
||||||
Debug.Log(string.Format("Discord: error {0}: {1}", errorCode, message));
|
Debug.Log(string.Format("Discord: error {0}: {1}", errorCode, message));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void JoinCallback(string secret)
|
public void JoinCallback(string secret)
|
||||||
{
|
{
|
||||||
Debug.Log(string.Format("Discord: join ({0})", secret));
|
Debug.Log(string.Format("Discord: join ({0})", secret));
|
||||||
onJoin.Invoke(secret);
|
onJoin.Invoke(secret);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SpectateCallback(string secret)
|
public void SpectateCallback(string secret)
|
||||||
{
|
{
|
||||||
Debug.Log(string.Format("Discord: spectate ({0})", secret));
|
Debug.Log(string.Format("Discord: spectate ({0})", secret));
|
||||||
onSpectate.Invoke(secret);
|
onSpectate.Invoke(secret);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RequestCallback(ref DiscordRpc.DiscordUser request)
|
public void RequestCallback(ref DiscordRpc.DiscordUser request)
|
||||||
{
|
{
|
||||||
Debug.Log(string.Format("Discord: join request {0}#{1}: {2}", request.username, request.discriminator, request.userId));
|
Debug.Log(string.Format("Discord: join request {0}#{1}: {2}", request.username, request.discriminator, request.userId));
|
||||||
joinRequest = request;
|
joinRequest = request;
|
||||||
onJoinRequest.Invoke(request);
|
onJoinRequest.Invoke(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Start()
|
void Start()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void Update()
|
void Update()
|
||||||
{
|
{
|
||||||
DiscordRpc.RunCallbacks();
|
DiscordRpc.RunCallbacks();
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnEnable()
|
void OnEnable()
|
||||||
{
|
{
|
||||||
Debug.Log("Discord: init");
|
Debug.Log("Discord: init");
|
||||||
handlers = new DiscordRpc.EventHandlers();
|
handlers = new DiscordRpc.EventHandlers();
|
||||||
handlers.readyCallback += ReadyCallback;
|
handlers.readyCallback += ReadyCallback;
|
||||||
handlers.disconnectedCallback += DisconnectedCallback;
|
handlers.disconnectedCallback += DisconnectedCallback;
|
||||||
handlers.errorCallback += ErrorCallback;
|
handlers.errorCallback += ErrorCallback;
|
||||||
handlers.joinCallback += JoinCallback;
|
handlers.joinCallback += JoinCallback;
|
||||||
handlers.spectateCallback += SpectateCallback;
|
handlers.spectateCallback += SpectateCallback;
|
||||||
handlers.requestCallback += RequestCallback;
|
handlers.requestCallback += RequestCallback;
|
||||||
DiscordRpc.Initialize(applicationId, ref handlers, true, optionalSteamId);
|
DiscordRpc.Initialize(applicationId, ref handlers, true, optionalSteamId);
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnDisable()
|
void OnDisable()
|
||||||
{
|
{
|
||||||
Debug.Log("Discord: shutdown");
|
Debug.Log("Discord: shutdown");
|
||||||
DiscordRpc.Shutdown();
|
DiscordRpc.Shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnDestroy()
|
void OnDestroy()
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,200 +6,200 @@ using AOT;
|
|||||||
|
|
||||||
public class DiscordRpc
|
public class DiscordRpc
|
||||||
{
|
{
|
||||||
[MonoPInvokeCallback(typeof(OnReadyInfo))]
|
[MonoPInvokeCallback(typeof(OnReadyInfo))]
|
||||||
public static void ReadyCallback(ref DiscordUser connectedUser) { }
|
public static void ReadyCallback(ref DiscordUser connectedUser) { }
|
||||||
public delegate void OnReadyInfo(ref DiscordUser connectedUser);
|
public delegate void OnReadyInfo(ref DiscordUser connectedUser);
|
||||||
|
|
||||||
[MonoPInvokeCallback(typeof(OnDisconnectedInfo))]
|
[MonoPInvokeCallback(typeof(OnDisconnectedInfo))]
|
||||||
public static void DisconnectedCallback(int errorCode, string message) { }
|
public static void DisconnectedCallback(int errorCode, string message) { }
|
||||||
public delegate void OnDisconnectedInfo(int errorCode, string message);
|
public delegate void OnDisconnectedInfo(int errorCode, string message);
|
||||||
|
|
||||||
[MonoPInvokeCallback(typeof(OnErrorInfo))]
|
[MonoPInvokeCallback(typeof(OnErrorInfo))]
|
||||||
public static void ErrorCallback(int errorCode, string message) { }
|
public static void ErrorCallback(int errorCode, string message) { }
|
||||||
public delegate void OnErrorInfo(int errorCode, string message);
|
public delegate void OnErrorInfo(int errorCode, string message);
|
||||||
|
|
||||||
[MonoPInvokeCallback(typeof(OnJoinInfo))]
|
[MonoPInvokeCallback(typeof(OnJoinInfo))]
|
||||||
public static void JoinCallback(string secret) { }
|
public static void JoinCallback(string secret) { }
|
||||||
public delegate void OnJoinInfo(string secret);
|
public delegate void OnJoinInfo(string secret);
|
||||||
|
|
||||||
[MonoPInvokeCallback(typeof(OnSpectateInfo))]
|
[MonoPInvokeCallback(typeof(OnSpectateInfo))]
|
||||||
public static void SpectateCallback(string secret) { }
|
public static void SpectateCallback(string secret) { }
|
||||||
public delegate void OnSpectateInfo(string secret);
|
public delegate void OnSpectateInfo(string secret);
|
||||||
|
|
||||||
[MonoPInvokeCallback(typeof(OnRequestInfo))]
|
[MonoPInvokeCallback(typeof(OnRequestInfo))]
|
||||||
public static void RequestCallback(ref DiscordUser request) { }
|
public static void RequestCallback(ref DiscordUser request) { }
|
||||||
public delegate void OnRequestInfo(ref DiscordUser request);
|
public delegate void OnRequestInfo(ref DiscordUser request);
|
||||||
|
|
||||||
public struct EventHandlers
|
public struct EventHandlers
|
||||||
{
|
|
||||||
public OnReadyInfo readyCallback;
|
|
||||||
public OnDisconnectedInfo disconnectedCallback;
|
|
||||||
public OnErrorInfo errorCallback;
|
|
||||||
public OnJoinInfo joinCallback;
|
|
||||||
public OnSpectateInfo spectateCallback;
|
|
||||||
public OnRequestInfo requestCallback;
|
|
||||||
}
|
|
||||||
|
|
||||||
[Serializable, StructLayout(LayoutKind.Sequential)]
|
|
||||||
public struct RichPresenceStruct
|
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
[Serializable]
|
|
||||||
public struct DiscordUser
|
|
||||||
{
|
|
||||||
public string userId;
|
|
||||||
public string username;
|
|
||||||
public string discriminator;
|
|
||||||
public string avatar;
|
|
||||||
}
|
|
||||||
|
|
||||||
public enum Reply
|
|
||||||
{
|
|
||||||
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<IntPtr> _buffers = new List<IntPtr>(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;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Get the <see cref="RichPresenceStruct"/> reprensentation of this instance
|
|
||||||
/// </summary>
|
|
||||||
/// <returns><see cref="RichPresenceStruct"/> reprensentation of this instance</returns>
|
|
||||||
internal RichPresenceStruct GetStruct()
|
|
||||||
{
|
{
|
||||||
if (_buffers.Count > 0)
|
public OnReadyInfo readyCallback;
|
||||||
{
|
public OnDisconnectedInfo disconnectedCallback;
|
||||||
FreeMem();
|
public OnErrorInfo errorCallback;
|
||||||
}
|
public OnJoinInfo joinCallback;
|
||||||
|
public OnSpectateInfo spectateCallback;
|
||||||
_presence.state = StrToPtr(state);
|
public OnRequestInfo requestCallback;
|
||||||
_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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
[Serializable, StructLayout(LayoutKind.Sequential)]
|
||||||
/// Returns a pointer to a representation of the given string with a size of maxbytes
|
public struct RichPresenceStruct
|
||||||
/// </summary>
|
|
||||||
/// <param name="input">String to convert</param>
|
|
||||||
/// <returns>Pointer to the UTF-8 representation of <see cref="input"/></returns>
|
|
||||||
private IntPtr StrToPtr(string input)
|
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(input)) return IntPtr.Zero;
|
public IntPtr state; /* max 128 bytes */
|
||||||
var convbytecnt = Encoding.UTF8.GetByteCount(input);
|
public IntPtr details; /* max 128 bytes */
|
||||||
var buffer = Marshal.AllocHGlobal(convbytecnt + 1);
|
public long startTimestamp;
|
||||||
for (int i = 0; i < convbytecnt + 1; i++)
|
public long endTimestamp;
|
||||||
{
|
public IntPtr largeImageKey; /* max 32 bytes */
|
||||||
Marshal.WriteByte(buffer, i, 0);
|
public IntPtr largeImageText; /* max 128 bytes */
|
||||||
}
|
public IntPtr smallImageKey; /* max 32 bytes */
|
||||||
_buffers.Add(buffer);
|
public IntPtr smallImageText; /* max 128 bytes */
|
||||||
Marshal.Copy(Encoding.UTF8.GetBytes(input), 0, buffer, convbytecnt);
|
public IntPtr partyId; /* max 128 bytes */
|
||||||
return buffer;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
[Serializable]
|
||||||
/// Convert string to UTF-8 and add null termination
|
public struct DiscordUser
|
||||||
/// </summary>
|
|
||||||
/// <param name="toconv">string to convert</param>
|
|
||||||
/// <returns>UTF-8 representation of <see cref="toconv"/> with added null termination</returns>
|
|
||||||
private static string StrToUtf8NullTerm(string toconv)
|
|
||||||
{
|
{
|
||||||
var str = toconv.Trim();
|
public string userId;
|
||||||
var bytes = Encoding.Default.GetBytes(str);
|
public string username;
|
||||||
if (bytes.Length > 0 && bytes[bytes.Length - 1] != 0)
|
public string discriminator;
|
||||||
{
|
public string avatar;
|
||||||
str += "\0\0";
|
|
||||||
}
|
|
||||||
return Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(str));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
public enum Reply
|
||||||
/// Free the allocated memory for conversion to <see cref="RichPresenceStruct"/>
|
|
||||||
/// </summary>
|
|
||||||
internal void FreeMem()
|
|
||||||
{
|
{
|
||||||
for (var i = _buffers.Count - 1; i >= 0; i--)
|
No = 0,
|
||||||
{
|
Yes = 1,
|
||||||
Marshal.FreeHGlobal(_buffers[i]);
|
Ignore = 2
|
||||||
_buffers.RemoveAt(i);
|
}
|
||||||
}
|
|
||||||
|
[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<IntPtr> _buffers = new List<IntPtr>(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;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get the <see cref="RichPresenceStruct"/> reprensentation of this instance
|
||||||
|
/// </summary>
|
||||||
|
/// <returns><see cref="RichPresenceStruct"/> reprensentation of this instance</returns>
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns a pointer to a representation of the given string with a size of maxbytes
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="input">String to convert</param>
|
||||||
|
/// <returns>Pointer to the UTF-8 representation of <see cref="input"/></returns>
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Convert string to UTF-8 and add null termination
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="toconv">string to convert</param>
|
||||||
|
/// <returns>UTF-8 representation of <see cref="toconv"/> with added null termination</returns>
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Free the allocated memory for conversion to <see cref="RichPresenceStruct"/>
|
||||||
|
/// </summary>
|
||||||
|
internal void FreeMem()
|
||||||
|
{
|
||||||
|
for (var i = _buffers.Count - 1; i >= 0; i--)
|
||||||
|
{
|
||||||
|
Marshal.FreeHGlobal(_buffers[i]);
|
||||||
|
_buffers.RemoveAt(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user