Skip to content

Commit bffce16

Browse files
committed
Add IPluginPresetConfig argument for Game launch APIs
1 parent ce290b4 commit bffce16

File tree

3 files changed

+169
-65
lines changed

3 files changed

+169
-65
lines changed

SharedStatic.Generic.cs

Lines changed: 83 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Hi3Helper.Plugin.Core.Management;
2+
using Hi3Helper.Plugin.Core.Management.PresetConfig;
23
using Hi3Helper.Plugin.Core.Utility;
34
using Microsoft.Extensions.Logging;
45
using System;
@@ -44,19 +45,21 @@ static SharedStatic()
4445

4546
/// <summary>
4647
/// This method is an ABI proxy function between the PInvoke Export and the actual plugin's method.<br/>
47-
/// See the documentation for <see cref="SharedStatic.LaunchGameFromGameManagerCoreAsync(IGameManager, IPlugin, PrintGameLog, CancellationToken)"/> method for more information.
48+
/// See the documentation for <see cref="SharedStatic.LaunchGameFromGameManagerCoreAsync(RunGameFromGameManagerContext, CancellationToken)"/> method for more information.
4849
/// </summary>
49-
public static unsafe int LaunchGameFromGameManagerAsync(nint gameManagerP, nint pluginP, nint printGameLogCallbackP, ref Guid cancelToken, out nint taskResult)
50+
public static unsafe int LaunchGameFromGameManagerAsync(nint gameManagerP, nint pluginP, nint presetConfigP, nint printGameLogCallbackP, ref Guid cancelToken, out nint taskResult)
5051
{
5152
taskResult = nint.Zero;
5253
try
5354
{
5455
#if MANUALCOM
55-
IPlugin? plugin = ComWrappers.ComInterfaceDispatch.GetInstance<IPlugin>((ComWrappers.ComInterfaceDispatch*)gameManagerP);
56+
IPlugin? plugin = ComWrappers.ComInterfaceDispatch.GetInstance<IPlugin>((ComWrappers.ComInterfaceDispatch*)pluginP);
5657
IGameManager? gameManager = ComWrappers.ComInterfaceDispatch.GetInstance<IGameManager>((ComWrappers.ComInterfaceDispatch*)gameManagerP);
58+
IPluginPresetConfig? presetConfig = ComWrappers.ComInterfaceDispatch.GetInstance<IPluginPresetConfig>((ComWrappers.ComInterfaceDispatch*)presetConfigP);
5759
#else
5860
IPlugin? plugin = ComInterfaceMarshaller<IPlugin>.ConvertToManaged((void*)pluginP);
5961
IGameManager? gameManager = ComInterfaceMarshaller<IGameManager>.ConvertToManaged((void*)gameManagerP);
62+
IPluginPresetConfig? presetConfig = ComInterfaceMarshaller<IPluginPresetConfig>.ConvertToManaged((void*)presetConfigP);
6063
#endif
6164

6265
PrintGameLog printGameLogCallback = Marshal.GetDelegateForFunctionPointer<PrintGameLog>(printGameLogCallbackP);
@@ -76,6 +79,11 @@ public static unsafe int LaunchGameFromGameManagerAsync(nint gameManagerP, nint
7679
throw new NullReferenceException("Cannot cast IPlugin from the pointer, hence it gives null!");
7780
}
7881

82+
if (presetConfig == null)
83+
{
84+
throw new NullReferenceException("Cannot cast IPluginPresetConfig from the pointer, hence it gives null!");
85+
}
86+
7987
if (printGameLogCallback == null)
8088
{
8189
throw new NullReferenceException("Cannot cast PrintGameLog callback from the pointer, hence it gives null!");
@@ -87,10 +95,18 @@ public static unsafe int LaunchGameFromGameManagerAsync(nint gameManagerP, nint
8795
cts = ComCancellationTokenVault.RegisterToken(in cancelToken);
8896
}
8997

90-
taskResult = ThisPluginExport.LaunchGameFromGameManagerCoreAsync(gameManager,
91-
plugin,
92-
printGameLogCallback,
93-
cts?.Token ?? CancellationToken.None).AsResult();
98+
RunGameFromGameManagerContext context = new RunGameFromGameManagerContext
99+
{
100+
GameManager = gameManager,
101+
Plugin = plugin,
102+
PresetConfig = presetConfig,
103+
PrintGameLogCallback = null!,
104+
PluginHandle = nint.Zero
105+
};
106+
107+
taskResult = ThisPluginExport
108+
.LaunchGameFromGameManagerCoreAsync(context, cts?.Token ?? CancellationToken.None)
109+
.AsResult();
94110
return 0;
95111
}
96112
catch (Exception ex)
@@ -103,26 +119,42 @@ public static unsafe int LaunchGameFromGameManagerAsync(nint gameManagerP, nint
103119

104120
/// <summary>
105121
/// This method is an ABI proxy function between the PInvoke Export and the actual plugin's method.<br/>
106-
/// See the documentation for <see cref="SharedStatic.IsGameRunningCore(IGameManager, out bool)"/> method for more information.
122+
/// See the documentation for <see cref="SharedStatic.IsGameRunningCore(RunGameFromGameManagerContext, out bool)"/> method for more information.
107123
/// </summary>
108-
public static unsafe int IsGameRunning(nint gameManagerP, out int isGameRunningInt)
124+
public static unsafe int IsGameRunning(nint gameManagerP, nint presetConfigP, out int isGameRunningInt)
109125
{
110126
isGameRunningInt = 0;
111127

112128
try
113129
{
114130
#if MANUALCOM
115131
IGameManager? gameManager = ComWrappers.ComInterfaceDispatch.GetInstance<IGameManager>((ComWrappers.ComInterfaceDispatch*)gameManagerP);
132+
IPluginPresetConfig? presetConfig = ComWrappers.ComInterfaceDispatch.GetInstance<IPluginPresetConfig>((ComWrappers.ComInterfaceDispatch*)presetConfigP);
116133
#else
117134
IGameManager? gameManager = ComInterfaceMarshaller<IGameManager>.ConvertToManaged((void*)gameManagerP);
135+
IPluginPresetConfig? presetConfig = ComInterfaceMarshaller<IPluginPresetConfig>.ConvertToManaged((void*)presetConfigP);
118136
#endif
119137

120138
if (gameManager == null)
121139
{
122140
throw new NullReferenceException("Cannot cast IGameManager from the pointer, hence it gives null!");
123141
}
124142

125-
bool isSupported = ThisPluginExport.IsGameRunningCore(gameManager, out bool isGameRunning);
143+
if (presetConfig == null)
144+
{
145+
throw new NullReferenceException("Cannot cast IPluginPresetConfig from the pointer, hence it gives null!");
146+
}
147+
148+
RunGameFromGameManagerContext context = new RunGameFromGameManagerContext
149+
{
150+
GameManager = gameManager,
151+
Plugin = null!,
152+
PresetConfig = presetConfig,
153+
PrintGameLogCallback = null!,
154+
PluginHandle = nint.Zero
155+
};
156+
157+
bool isSupported = ThisPluginExport.IsGameRunningCore(context, out bool isGameRunning);
126158
isGameRunningInt = isGameRunning ? 1 : 0;
127159
return isSupported ? 0 : throw new NotSupportedException("Method isn't overriden, yet");
128160
}
@@ -136,19 +168,21 @@ public static unsafe int IsGameRunning(nint gameManagerP, out int isGameRunningI
136168

137169
/// <summary>
138170
/// This method is an ABI proxy function between the PInvoke Export and the actual plugin's method.<br/>
139-
/// See the documentation for <see cref="SharedStatic.WaitRunningGameCoreAsync(IGameManager, IPlugin, CancellationToken)"/> method for more information.
171+
/// See the documentation for <see cref="SharedStatic.WaitRunningGameCoreAsync(RunGameFromGameManagerContext, CancellationToken)"/> method for more information.
140172
/// </summary>
141-
public static unsafe int WaitRunningGameAsync(nint gameManagerP, nint pluginP, ref Guid cancelToken, out nint taskResult)
173+
public static unsafe int WaitRunningGameAsync(nint gameManagerP, nint pluginP, nint presetConfigP, ref Guid cancelToken, out nint taskResult)
142174
{
143175
taskResult = nint.Zero;
144176
try
145177
{
146178
#if MANUALCOM
147-
IPlugin? plugin = ComWrappers.ComInterfaceDispatch.GetInstance<IPlugin>((ComWrappers.ComInterfaceDispatch*)gameManagerP);
179+
IPlugin? plugin = ComWrappers.ComInterfaceDispatch.GetInstance<IPlugin>((ComWrappers.ComInterfaceDispatch*)pluginP);
148180
IGameManager? gameManager = ComWrappers.ComInterfaceDispatch.GetInstance<IGameManager>((ComWrappers.ComInterfaceDispatch*)gameManagerP);
181+
IPluginPresetConfig? presetConfig = ComWrappers.ComInterfaceDispatch.GetInstance<IPluginPresetConfig>((ComWrappers.ComInterfaceDispatch*)presetConfigP);
149182
#else
150183
IPlugin? plugin = ComInterfaceMarshaller<IPlugin>.ConvertToManaged((void*)pluginP);
151184
IGameManager? gameManager = ComInterfaceMarshaller<IGameManager>.ConvertToManaged((void*)gameManagerP);
185+
IPluginPresetConfig? presetConfig = ComInterfaceMarshaller<IPluginPresetConfig>.ConvertToManaged((void*)presetConfigP);
152186
#endif
153187

154188
if (ThisPluginExport == null)
@@ -166,15 +200,29 @@ public static unsafe int WaitRunningGameAsync(nint gameManagerP, nint pluginP, r
166200
throw new NullReferenceException("Cannot cast IPlugin from the pointer, hence it gives null!");
167201
}
168202

203+
if (presetConfig == null)
204+
{
205+
throw new NullReferenceException("Cannot cast IPluginPresetConfig from the pointer, hence it gives null!");
206+
}
207+
169208
CancellationTokenSource? cts = null;
170209
if (Unsafe.IsNullRef(ref cancelToken))
171210
{
172211
cts = ComCancellationTokenVault.RegisterToken(in cancelToken);
173212
}
174213

175-
taskResult = ThisPluginExport.WaitRunningGameCoreAsync(gameManager,
176-
plugin,
177-
cts?.Token ?? CancellationToken.None).AsResult();
214+
RunGameFromGameManagerContext context = new RunGameFromGameManagerContext
215+
{
216+
GameManager = gameManager,
217+
Plugin = plugin,
218+
PresetConfig = presetConfig,
219+
PrintGameLogCallback = null!,
220+
PluginHandle = nint.Zero
221+
};
222+
223+
taskResult = ThisPluginExport
224+
.WaitRunningGameCoreAsync(context, cts?.Token ?? CancellationToken.None)
225+
.AsResult();
178226
return 0;
179227
}
180228
catch (Exception ex)
@@ -187,26 +235,42 @@ public static unsafe int WaitRunningGameAsync(nint gameManagerP, nint pluginP, r
187235

188236
/// <summary>
189237
/// This method is an ABI proxy function between the PInvoke Export and the actual plugin's method.<br/>
190-
/// See the documentation for <see cref="SharedStatic.KillRunningGameCore(IGameManager, out bool)"/> method for more information.
238+
/// See the documentation for <see cref="SharedStatic.KillRunningGameCore(RunGameFromGameManagerContext, out bool)"/> method for more information.
191239
/// </summary>
192-
public static unsafe int KillRunningGame(nint gameManagerP, out int wasGameRunningInt)
240+
public static unsafe int KillRunningGame(nint gameManagerP, nint presetConfigP, out int wasGameRunningInt)
193241
{
194242
wasGameRunningInt = 0;
195243

196244
try
197245
{
198246
#if MANUALCOM
199247
IGameManager? gameManager = ComWrappers.ComInterfaceDispatch.GetInstance<IGameManager>((ComWrappers.ComInterfaceDispatch*)gameManagerP);
248+
IPluginPresetConfig? presetConfig = ComWrappers.ComInterfaceDispatch.GetInstance<IPluginPresetConfig>((ComWrappers.ComInterfaceDispatch*)presetConfigP);
200249
#else
201250
IGameManager? gameManager = ComInterfaceMarshaller<IGameManager>.ConvertToManaged((void*)gameManagerP);
251+
IPluginPresetConfig? presetConfig = ComInterfaceMarshaller<IPluginPresetConfig>.ConvertToManaged((void*)presetConfigP);
202252
#endif
203253

204254
if (gameManager == null)
205255
{
206256
throw new NullReferenceException("Cannot cast IGameManager from the pointer, hence it gives null!");
207257
}
208258

209-
bool isSupported = ThisPluginExport.KillRunningGameCore(gameManager, out bool wasGameRunning);
259+
if (presetConfig == null)
260+
{
261+
throw new NullReferenceException("Cannot cast IPluginPresetConfig from the pointer, hence it gives null!");
262+
}
263+
264+
RunGameFromGameManagerContext context = new RunGameFromGameManagerContext
265+
{
266+
GameManager = gameManager,
267+
Plugin = null!,
268+
PresetConfig = presetConfig,
269+
PrintGameLogCallback = null!,
270+
PluginHandle = nint.Zero
271+
};
272+
273+
bool isSupported = ThisPluginExport.KillRunningGameCore(context, out bool wasGameRunning);
210274
wasGameRunningInt = wasGameRunning ? 1 : 0;
211275
return isSupported ? 0 : throw new NotSupportedException("Method isn't overriden which mark this plugin doesn't support this feature!");
212276
}

SharedStatic.cs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,9 @@ static unsafe SharedStatic()
124124
protected unsafe delegate void GetPluginUpdateCdnListDelegate(int* count, ushort*** ptr);
125125
protected delegate void SetCallbackPointerDelegate(nint callbackP);
126126

127-
internal delegate int LaunchGameFromGameManagerAsyncDelegate(nint gameManagerP, nint pluginP, nint printGameLogCallbackP, ref Guid cancelToken, out nint taskResult);
128-
internal delegate int WaitRunningGameAsyncDelegate(nint gameManagerP, nint pluginP, ref Guid cancelToken, out nint taskResult);
129-
internal delegate int IsGameRunningDelegate(nint gameManagerP, out int isGameRunning);
127+
internal delegate int LaunchGameFromGameManagerAsyncDelegate(nint gameManagerP, nint pluginP, nint presetConfigP, nint printGameLogCallbackP, ref Guid cancelToken, out nint taskResult);
128+
internal delegate int WaitRunningGameAsyncDelegate(nint gameManagerP, nint pluginP, nint presetConfigP, ref Guid cancelToken, out nint taskResult);
129+
internal delegate int IsGameRunningDelegate(nint gameManagerP, nint presetConfigP, out int isGameRunning);
130130

131131
/// <summary>
132132
/// Gets the array of CDN URLs used by the launcher to perform an update.
@@ -373,9 +373,7 @@ public static unsafe int TryGetApiExportPointer(char* apiExportName, void** dele
373373
/// <summary>
374374
/// Asynchronously launch the game using plugin's built-in game launch mechanism and wait until the game exit.
375375
/// </summary>
376-
/// <param name="manager">The game manager instance which handles the game launch.</param>
377-
/// <param name="pluginInstance">The instance of the plugin.</param>
378-
/// <param name="printGameLogCallback">A callback to send the log of the currently running game.</param>
376+
/// <param name="context">The context to launch the game from <see cref="IGameManager"/>.</param>
379377
/// <param name="token">
380378
/// Cancellation token to pass into the plugin's game launch mechanism.<br/>
381379
/// If cancellation is requested, it will cancel the awaiting but not killing the game process.
@@ -384,23 +382,23 @@ public static unsafe int TryGetApiExportPointer(char* apiExportName, void** dele
384382
/// Returns <c>false</c> if the plugin doesn't have game launch mechanism (or API Standard is equal or lower than v0.1.0) or if this method isn't overriden.<br/>
385383
/// Otherwise, <c>true</c> if the plugin supports game launch mechanism.
386384
/// </returns>
387-
public virtual Task<bool> LaunchGameFromGameManagerCoreAsync(IGameManager manager, IPlugin pluginInstance, PrintGameLog printGameLogCallback, CancellationToken token)
385+
public virtual Task<bool> LaunchGameFromGameManagerCoreAsync(RunGameFromGameManagerContext context, CancellationToken token)
388386
{
389387
return Task.FromResult(false);
390388
}
391389

392390
/// <summary>
393391
/// Check if the game from the current <see cref="IGameManager"/> is running or not.
394392
/// </summary>
395-
/// <param name="manager">Game manager of the current game region to check.</param>
393+
/// <param name="context">The context to launch the game from <see cref="IGameManager"/>.</param>
396394
/// <param name="isGameRunning">Whether the game is currently running or not.</param>
397395
/// <returns>
398396
/// To find the actual return value, please use <paramref name="isGameRunning"/> out-argument.<br/><br/>
399397
///
400398
/// Returns <c>false</c> if the plugin doesn't have game launch mechanism (or API Standard is equal or lower than v0.1.0) or if this method isn't overriden.<br/>
401399
/// Otherwise, <c>true</c> if the plugin supports game launch mechanism.
402400
/// </returns>
403-
public virtual bool IsGameRunningCore(IGameManager manager, out bool isGameRunning)
401+
public virtual bool IsGameRunningCore(RunGameFromGameManagerContext context, out bool isGameRunning)
404402
{
405403
isGameRunning = false;
406404
return false;
@@ -409,8 +407,7 @@ public virtual bool IsGameRunningCore(IGameManager manager, out bool isGameRunni
409407
/// <summary>
410408
/// Asynchronously wait currently running game until it exit.
411409
/// </summary>
412-
/// <param name="manager">The game manager instance which handles the game launch.</param>
413-
/// <param name="pluginInstance">The instance of the plugin.</param>
410+
/// <param name="context">The context to launch the game from <see cref="IGameManager"/>.</param>
414411
/// <param name="token">
415412
/// Cancellation token to pass into the plugin's game launch mechanism.<br/>
416413
/// If cancellation is requested, it will cancel the awaiting but not killing the game process.
@@ -419,23 +416,23 @@ public virtual bool IsGameRunningCore(IGameManager manager, out bool isGameRunni
419416
/// Returns <c>false</c> if the plugin doesn't have game launch mechanism (or API Standard is equal or lower than v0.1.0) or if this method isn't overriden.<br/>
420417
/// Otherwise, <c>true</c> if the plugin does support game launch mechanism and the game ran successfully.
421418
/// </returns>
422-
public virtual Task<bool> WaitRunningGameCoreAsync(IGameManager manager, IPlugin pluginInstance, CancellationToken token)
419+
public virtual Task<bool> WaitRunningGameCoreAsync(RunGameFromGameManagerContext context, CancellationToken token)
423420
{
424421
return Task.FromResult(false);
425422
}
426423

427424
/// <summary>
428425
/// Kill the process of the currently running game.
429426
/// </summary>
430-
/// <param name="manager">The game manager instance which handles the game launch.</param>
427+
/// <param name="context">The context to launch the game from <see cref="IGameManager"/>.</param>
431428
/// <param name="wasGameRunning">Whether to indicate that the game was running or not.</param>
432429
/// <returns>
433430
/// To find the actual return value, please use <paramref name="wasGameRunning"/> out-argument.<br/><br/>
434431
///
435432
/// Returns <c>false</c> if the plugin doesn't have game launch mechanism (or API Standard is equal or lower than v0.1.0) or if this method isn't overriden.<br/>
436433
/// Otherwise, <c>true</c> if the plugin supports game launch mechanism.
437434
/// </returns>
438-
public virtual bool KillRunningGameCore(IGameManager manager, out bool wasGameRunning)
435+
public virtual bool KillRunningGameCore(RunGameFromGameManagerContext context, out bool wasGameRunning)
439436
{
440437
wasGameRunning = false;
441438
return false;

0 commit comments

Comments
 (0)