|
| 1 | +using Hi3Helper.Plugin.Core.Management.PresetConfig; |
| 2 | +using Hi3Helper.Plugin.Core.Utility; |
| 3 | +using System; |
| 4 | +using System.Runtime.InteropServices; |
| 5 | +using System.Runtime.InteropServices.Marshalling; |
| 6 | +using static Hi3Helper.Plugin.Core.SharedStaticV1Ext; |
| 7 | + |
| 8 | +namespace Hi3Helper.Plugin.Core.DiscordPresence; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// This extension provides a method to get the Discord Presence information for the current game region. |
| 12 | +/// </summary> |
| 13 | +/// <remarks> |
| 14 | +/// This extension IS ONLY SUPPOSEDLY BE USED by the launcher, NOT by the plugin. |
| 15 | +/// </remarks> |
| 16 | +public static class DiscordPresenceExtension |
| 17 | +{ |
| 18 | + /// <summary> |
| 19 | + /// A context used to manage the context of Discord Presence information. |
| 20 | + /// </summary> |
| 21 | + public unsafe class DiscordPresenceContext : IDisposable |
| 22 | + { |
| 23 | + // Fields |
| 24 | + private DiscordPresenceInfo* _data; |
| 25 | + |
| 26 | + internal DiscordPresenceContext(IPluginPresetConfig presetConfig) : this(0, presetConfig) |
| 27 | + { |
| 28 | + } |
| 29 | + |
| 30 | + public DiscordPresenceContext(nint pluginHandle, IPluginPresetConfig presetConfig) |
| 31 | + { |
| 32 | + if (pluginHandle == nint.Zero) |
| 33 | + { |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + if (!pluginHandle.TryGetExport("GetCurrentDiscordPresenceInfo", |
| 38 | + out GetCurrentDiscordPresenceInfoDelegate method)) |
| 39 | + { |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + DiscordPresenceInfo** info = null; |
| 44 | + |
| 45 | + void* ptr = ComInterfaceMarshaller<IPluginPresetConfig>.ConvertToUnmanaged(presetConfig); |
| 46 | + HResult result = method(ptr, info); |
| 47 | + |
| 48 | + if (result == HResult.Ok && info != null && *info != null) |
| 49 | + { |
| 50 | + _data = *info; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Indicates whether the Discord Presence feature is available. |
| 56 | + /// </summary> |
| 57 | + public bool IsFeatureAvailable => _data != null; |
| 58 | + |
| 59 | + /// <inheritdoc cref="DiscordPresenceInfo.PresenceId"/> |
| 60 | + public ulong PresenceId => _data != null ? _data->PresenceId : 0; |
| 61 | + |
| 62 | + /// <inheritdoc cref="DiscordPresenceInfo.LargeIconUrl"/> |
| 63 | + public string? LargeIconUrl |
| 64 | + { |
| 65 | + get |
| 66 | + { |
| 67 | + if (_data == null || _data->LargeIconUrl == null) |
| 68 | + { |
| 69 | + return null; |
| 70 | + } |
| 71 | + |
| 72 | + return field ??= Utf16StringMarshaller.ConvertToManaged(_data->LargeIconUrl); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /// <inheritdoc cref="DiscordPresenceInfo.LargeIconTooltip"/> |
| 77 | + public string? LargeIconTooltip |
| 78 | + { |
| 79 | + get |
| 80 | + { |
| 81 | + if (_data == null || _data->LargeIconTooltip == null) |
| 82 | + { |
| 83 | + return null; |
| 84 | + } |
| 85 | + |
| 86 | + return field ??= Utf16StringMarshaller.ConvertToManaged(_data->LargeIconTooltip); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /// <inheritdoc cref="DiscordPresenceInfo.SmallIconUrl"/> |
| 91 | + public string? SmallIconUrl |
| 92 | + { |
| 93 | + get |
| 94 | + { |
| 95 | + if (_data == null || _data->SmallIconUrl == null) |
| 96 | + { |
| 97 | + return null; |
| 98 | + } |
| 99 | + |
| 100 | + return field ??= Utf16StringMarshaller.ConvertToManaged(_data->SmallIconUrl); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /// <inheritdoc cref="DiscordPresenceInfo.SmallIconTooltip"/> |
| 105 | + public string? SmallIconTooltip |
| 106 | + { |
| 107 | + get |
| 108 | + { |
| 109 | + if (_data == null || _data->SmallIconTooltip == null) |
| 110 | + { |
| 111 | + return null; |
| 112 | + } |
| 113 | + |
| 114 | + return field ??= Utf16StringMarshaller.ConvertToManaged(_data->SmallIconTooltip); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + /// <inheritdoc cref="IDisposable.Dispose"/> |
| 119 | + public void Dispose() |
| 120 | + { |
| 121 | + if (_data == null) |
| 122 | + { |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + _data->Dispose(); |
| 127 | + NativeMemory.Free(_data); |
| 128 | + |
| 129 | + _data = null; |
| 130 | + |
| 131 | + GC.SuppressFinalize(this); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments