-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSharedStatic.V1Ext_Update2.cs
More file actions
131 lines (116 loc) · 5.67 KB
/
SharedStatic.V1Ext_Update2.cs
File metadata and controls
131 lines (116 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using Hi3Helper.Plugin.Core.DiscordPresence;
using Hi3Helper.Plugin.Core.Management.PresetConfig;
using Hi3Helper.Plugin.Core.Utility;
using Microsoft.Extensions.Logging;
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
namespace Hi3Helper.Plugin.Core;
public partial class SharedStaticV1Ext
{
// Update2
internal unsafe delegate HResult GetCurrentDiscordPresenceInfoDelegate(
void* presetConfigP,
DiscordPresenceInfo** presenceInfoP);
}
public partial class SharedStaticV1Ext<T>
{
private static unsafe void InitExtension_Update2Exports()
{
/* ----------------------------------------------------------------------
* Update 2 Feature Sets
* ----------------------------------------------------------------------
* This feature sets includes the following feature:
* - Discord Rich Presence
* - GetCurrentDiscordPresenceInfo
*/
// -> Plugin Discord Presence Info based on Specific Game Region.
TryRegisterApiExport<GetCurrentDiscordPresenceInfoDelegate>("GetCurrentDiscordPresenceInfo", GetCurrentDiscordPresenceInfo);
}
#region ABI Proxies
/// <summary>
/// This method is an ABI proxy function between the PInvoke Export and the actual plugin's method.<br/>
/// See the documentation for <see cref="SharedStaticV1Ext{T}.GetCurrentDiscordPresenceInfoCore"/> method for more information.
/// </summary>
private static unsafe HResult GetCurrentDiscordPresenceInfo(void* presetConfigP, DiscordPresenceInfo** presenceInfoP)
{
try
{
if (presetConfigP == null)
{
throw new NullReferenceException("Cannot cast IPluginPresetConfig from the pointer, hence it gives null!");
}
#if MANUALCOM
IPluginPresetConfig presetConfig = ComWrappers.ComInterfaceDispatch.GetInstance<IPluginPresetConfig>((ComWrappers.ComInterfaceDispatch*)presetConfigP);
#else
IPluginPresetConfig? presetConfig = ComInterfaceMarshaller<IPluginPresetConfig>.ConvertToManaged((void*)presetConfigP);
if (presetConfig == null)
{
return false;
}
#endif
DiscordPresenceExtension.DiscordPresenceContext context = new(presetConfig);
if (presenceInfoP == null)
{
return false;
}
bool isSupported = ThisExtensionExport
.GetCurrentDiscordPresenceInfoCore(context,
out ulong presenceId,
out string? largeIconUrl,
out string? largeIconTooltip,
out string? smallIconUrl,
out string? smallIconTooltip);
if (!isSupported)
{
return isSupported;
}
DiscordPresenceInfo* info = Mem.Alloc<DiscordPresenceInfo>();
info->PresenceId = presenceId;
info->LargeIconUrl = Utf16StringMarshaller.ConvertToUnmanaged(largeIconUrl);
info->LargeIconTooltip = Utf16StringMarshaller.ConvertToUnmanaged(largeIconTooltip);
info->SmallIconUrl = Utf16StringMarshaller.ConvertToUnmanaged(smallIconUrl);
info->SmallIconTooltip = Utf16StringMarshaller.ConvertToUnmanaged(smallIconTooltip);
*presenceInfoP = info;
return isSupported;
}
catch (Exception ex)
{
// ignored
InstanceLogger.LogError(ex, "An error has occurred while trying to call GetCurrentDiscordPresenceInfo() from the plugin!");
return Marshal.GetHRForException(ex);
}
}
#endregion
#region Core Methods
/// <summary>
/// Retrieves the current Discord Rich Presence information for the specified game region from its <paramref name="context"/>.
/// </summary>
/// <param name="context">The context containing details about the Discord presence request.</param>
/// <param name="presenceId">Unique identifier for the Discord presence, if available.</param>
/// <param name="largeIconUrl">URL of the large icon to display in the presence, or null if not set.</param>
/// <param name="largeIconTooltip">Tooltip text for the large icon, or null if not set.</param>
/// <param name="smallIconUrl">URL of the small icon to display in the presence, or null if not set.</param>
/// <param name="smallIconTooltip">Tooltip text for the small icon, or null if not set.</param>
/// <returns>
/// Returns <c>false</c> if the plugin doesn't have game launch mechanism (or API Standard is equal or lower than v0.1.2) or if this method isn't overriden.<br/>
/// Otherwise, <c>true</c> if the plugin supports game launch mechanism.
/// </returns>
protected virtual bool GetCurrentDiscordPresenceInfoCore(
DiscordPresenceExtension.DiscordPresenceContext context,
out ulong presenceId,
out string? largeIconUrl,
out string? largeIconTooltip,
out string? smallIconUrl,
out string? smallIconTooltip)
{
presenceId = 0;
Unsafe.SkipInit(out largeIconUrl);
Unsafe.SkipInit(out largeIconTooltip);
Unsafe.SkipInit(out smallIconUrl);
Unsafe.SkipInit(out smallIconTooltip);
return false;
}
#endregion
}