-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGameManagerExtension.cs
More file actions
448 lines (386 loc) · 20.2 KB
/
Copy pathGameManagerExtension.cs
File metadata and controls
448 lines (386 loc) · 20.2 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
using Hi3Helper.Plugin.Core.Management;
using Hi3Helper.Plugin.Core.Management.PresetConfig;
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
using System.Threading;
using System.Threading.Tasks;
namespace Hi3Helper.Plugin.Core.Utility;
/// <summary>
/// This extension provides a method to launch the game using the plugin's game launch mechanism if available.
/// </summary>
/// <remarks>
/// This extension IS ONLY SUPPOSEDLY BE USED by the launcher, NOT by the plugin.
/// </remarks>
public static class GameManagerExtension
{
public unsafe delegate void PrintGameLog(char* logString, int logStringLen, int isStringCanFree);
/// <summary>
/// A context used to manage the game launch routine using plugin's functionality.
/// </summary>
public class RunGameFromGameManagerContext
{
// Fields
private bool? _isFeatureAvailable;
/// <summary>
/// The game manager instance which handles the game launch.
/// </summary>
public required IGameManager GameManager { get; init; }
/// <summary>
/// The instance of the plugin.
/// </summary>
public required IPlugin Plugin { get; init; }
/// <summary>
/// The preset config for the region of the game.
/// </summary>
public required IPluginPresetConfig PresetConfig { get; init; }
/// <summary>
/// The pointer to the plugin's library handle.
/// </summary>
public required nint PluginHandle { get; init; }
/// <summary>
/// A delegate which is pointed to a callback to print game log while the game is running.
/// </summary>
public required PrintGameLog? PrintGameLogCallback { get; set; }
/// <summary>
/// Indicates whether the Game Launch API is supported on the plugin.
/// </summary>
public bool IsFeatureAvailable => _isFeatureAvailable ??= this.IsGameRunning(out _, out _, out _);
/// <summary>
/// Indicates whether the game is currently running.
/// </summary>
// ReSharper disable once MemberHidesStaticFromOuterClass
public bool IsGameRunning => IsFeatureAvailable && this.IsGameRunning(out bool running, out _, out _) && running;
/// <summary>
/// Indicates when the game launched. If the game isn't running, it will return a default value.
/// </summary>
public DateTime GameLaunchStartTime
{
get
{
if (!IsFeatureAvailable)
{
return default;
}
if (!this.IsGameRunning(out bool running, out DateTime dateTime, out _) ||
!running)
{
return default;
}
return dateTime;
}
}
}
/// <summary>
/// Asynchronously launch the game using plugin's built-in game launch mechanism and wait until the game exit.
/// </summary>
/// <param name="context">The context to launch the game from <see cref="IGameManager"/>.</param>
/// <param name="startArgument">The additional argument string to run the game executable (Default: null).</param>
/// <param name="isRunBoosted">Based on <see cref="Process.PriorityBoostEnabled"/>, boost the process temporarily when the game window is focused (Default: false).</param>
/// <param name="processPriority">Based on <see cref="Process.PriorityClass"/>, run the game process with specific priority (Default: <see cref="ProcessPriorityClass.Normal"/>).</param>
/// <param name="token">
/// Cancellation token to pass into the plugin's game launch mechanism.<br/>
/// If cancellation is requested, it will cancel the awaiting but not killing the game process.
/// </param>
/// <returns>
/// Returns <c>IsSuccess=false</c> if the plugin doesn't have game launch mechanism (or API Standard is equal or lower than v0.1.0), hence fallback to launcher's game launch mechanism.<br/>
/// Otherwise, <c>IsSuccess=true</c> if the plugin does support game launch mechanism and the game ran successfully or await process is cancelled.
/// </returns>
public static async Task<(bool IsSuccess, Exception? Error)>
RunGameFromGameManagerAsync(this RunGameFromGameManagerContext context,
string? startArgument = null,
bool isRunBoosted = false,
ProcessPriorityClass processPriority = ProcessPriorityClass.Normal,
CancellationToken token = default)
{
ArgumentNullException.ThrowIfNull(context, nameof(context));
if (!context.PluginHandle.TryGetExport("LaunchGameFromGameManagerAsync", out SharedStaticV1Ext.LaunchGameFromGameManagerAsyncDelegate launchGameFromGameManagerAsyncCallback))
{
return (false, new NotSupportedException("Plugin doesn't have LaunchGameFromGameManagerAsync export in its API definition!"));
}
PrintGameLog? printGameLogCallback = context.PrintGameLogCallback;
nint gameManagerP = GetPointerFromInterface(context.GameManager);
nint pluginP = GetPointerFromInterface(context.Plugin);
nint presetConfigP = GetPointerFromInterface(context.PresetConfig);
nint printGameLogCallbackP = printGameLogCallback == null ? nint.Zero : Marshal.GetFunctionPointerForDelegate(printGameLogCallback);
if (gameManagerP == nint.Zero)
{
return (false, new COMException("Cannot cast IGameManager interface to pointer!"));
}
if (pluginP == nint.Zero)
{
return (false, new COMException("Cannot cast IPlugin interface to pointer!"));
}
if (presetConfigP == nint.Zero)
{
return (false, new COMException("Cannot cast IPluginPresetConfig interface to pointer!"));
}
nint argumentsP = startArgument.GetPinnableStringPointerSafe();
int argumentsLen = startArgument?.Length ?? 0;
Guid cancelTokenGuid = Guid.CreateVersion7();
int hResult = launchGameFromGameManagerAsyncCallback(gameManagerP,
pluginP,
presetConfigP,
printGameLogCallbackP,
argumentsP,
argumentsLen,
isRunBoosted ? 1 : 0,
(int)processPriority,
ref cancelTokenGuid,
out nint taskResult);
if (taskResult == nint.Zero)
{
return (false, new NullReferenceException("ComAsyncResult pointer in taskReturn argument shouldn't return a null pointer!"));
}
if (hResult != 0)
{
return (false, Marshal.GetExceptionForHR(hResult));
}
try
{
return await ExecuteSuccessAsyncTask(context.Plugin, taskResult, cancelTokenGuid, token);
}
finally
{
GC.KeepAlive(printGameLogCallback);
}
}
/// <summary>
/// Check if the game from the current <see cref="IGameManager"/> is running or not.
/// </summary>
/// <param name="context">The context to launch the game from <see cref="IGameManager"/>.</param>
/// <param name="isGameRunning">Whether the game is currently running or not.</param>
/// <param name="errorException">Represents an exception from HRESULT of the plugin's function.</param>
/// <param name="gameStartTime">The date time stamp of when the process was started.</param>
/// <returns>
/// To find the actual return value, please use <paramref name="isGameRunning"/> out-argument.<br/><br/>
///
/// Returns <c>false</c> if the plugin doesn't have game launch mechanism (or API Standard is equal or lower than v0.1.0).<br/>
/// Otherwise, <c>true</c> if the plugin supports game launch mechanism.
/// </returns>
public static bool IsGameRunning(this RunGameFromGameManagerContext context,
out bool isGameRunning,
out DateTime gameStartTime,
[NotNullWhen(false)] out Exception? errorException)
{
ArgumentNullException.ThrowIfNull(context, nameof(context));
isGameRunning = false;
errorException = null;
gameStartTime = default;
if (!context.PluginHandle.TryGetExport("IsGameRunning", out SharedStaticV1Ext.IsGameRunningDelegate isGameRunningCallback))
{
errorException = new NotSupportedException("Plugin doesn't have IsGameRunning export in its API definition!");
return false;
}
nint gameManagerP = GetPointerFromInterface(context.GameManager);
nint presetConfigP = GetPointerFromInterface(context.PresetConfig);
if (gameManagerP == nint.Zero)
{
errorException = new COMException("Cannot cast IGameManager interface to pointer!");
return false;
}
if (presetConfigP == nint.Zero)
{
errorException = new COMException("Cannot cast IPluginPresetConfig interface to pointer!");
return false;
}
int hResult = isGameRunningCallback(gameManagerP, presetConfigP, out int isGameRunningInt, out gameStartTime);
errorException = Marshal.GetExceptionForHR(hResult);
if (errorException != null)
{
return false;
}
isGameRunning = isGameRunningInt == 1;
return true;
}
/// <summary>
/// Asynchronously wait currently running game until it exit.
/// </summary>
/// <param name="context">The context to launch the game from <see cref="IGameManager"/>.</param>
/// <param name="token">
/// Cancellation token to pass into the plugin's game launch mechanism.<br/>
/// If cancellation is requested, it will cancel the awaiting but not killing the game process.
/// </param>
/// <returns>
/// Returns <c>IsSuccess=false</c> if the plugin doesn't have game launch mechanism (or API Standard is equal or lower than v0.1.0), hence fallback to launcher's game launch mechanism.<br/>
/// Otherwise, <c>IsSuccess=true</c> if the plugin does support game launch mechanism and the game ran successfully.
/// </returns>
public static async Task<(bool IsSuccess, Exception? Error)>
WaitRunningGameAsync(this RunGameFromGameManagerContext context,
CancellationToken token)
{
ArgumentNullException.ThrowIfNull(context, nameof(context));
if (!context.PluginHandle.TryGetExport("WaitRunningGameAsync", out SharedStaticV1Ext.WaitRunningGameAsyncDelegate waitRunningGameAsyncCallback))
{
return (false, new NotSupportedException("Plugin doesn't have WaitRunningGameAsync export in its API definition!"));
}
nint gameManagerP = GetPointerFromInterface(context.GameManager);
nint pluginP = GetPointerFromInterface(context.Plugin);
nint presetConfigP = GetPointerFromInterface(context.PresetConfig);
if (gameManagerP == nint.Zero)
{
return (false, new COMException("Cannot cast IGameManager interface to pointer!"));
}
if (pluginP == nint.Zero)
{
return (false, new COMException("Cannot cast IPlugin interface to pointer!"));
}
if (presetConfigP == nint.Zero)
{
return (false, new COMException("Cannot cast IPluginPresetConfig interface to pointer!"));
}
Guid cancelTokenGuid = Guid.CreateVersion7();
int hResult = waitRunningGameAsyncCallback(gameManagerP, pluginP, presetConfigP, ref cancelTokenGuid, out nint taskResult);
if (taskResult == nint.Zero)
{
return (false, new NullReferenceException("ComAsyncResult pointer in taskReturn argument shouldn't return a null pointer!"));
}
if (hResult != 0)
{
return (false, Marshal.GetExceptionForHR(hResult));
}
return await ExecuteSuccessAsyncTask(context.Plugin, taskResult, cancelTokenGuid, token);
}
/// <summary>
/// Kill the process of the currently running game.
/// </summary>
/// <param name="context">The context to launch the game from <see cref="IGameManager"/>.</param>
/// <param name="wasGameRunning">Whether to indicate that the game was running or not.</param>
/// <param name="gameStartTime">The date time stamp of when the process was started.</param>
/// <param name="errorException">Represents an exception from HRESULT of the plugin's function.</param>
/// <returns>
/// To find the actual return value, please use <paramref name="wasGameRunning"/> out-argument.<br/><br/>
///
/// Returns <c>false</c> if the plugin doesn't have game launch mechanism (or API Standard is equal or lower than v0.1.0).<br/>
/// Otherwise, <c>true</c> if the plugin supports game launch mechanism.
/// </returns>
public static bool KillRunningGame(this RunGameFromGameManagerContext context,
out bool wasGameRunning,
out DateTime gameStartTime,
[NotNullWhen(false)] out Exception? errorException)
{
ArgumentNullException.ThrowIfNull(context, nameof(context));
errorException = null;
wasGameRunning = false;
gameStartTime = default;
if (!context.PluginHandle.TryGetExport("KillRunningGame", out SharedStaticV1Ext.IsGameRunningDelegate killRunningGameCallback))
{
errorException = new NotSupportedException("Plugin doesn't have KillRunningGame export in its API definition!");
return false;
}
nint gameManagerP = GetPointerFromInterface(context.GameManager);
nint presetConfigP = GetPointerFromInterface(context.PresetConfig);
if (gameManagerP == nint.Zero)
{
errorException = new COMException("Cannot cast IGameManager interface to pointer!");
return false;
}
if (presetConfigP == nint.Zero)
{
errorException = new COMException("Cannot cast IPluginPresetConfig interface to pointer!");
return false;
}
int hResult = killRunningGameCallback(gameManagerP, presetConfigP, out int wasGameRunningInt, out gameStartTime);
errorException = Marshal.GetExceptionForHR(hResult);
if (errorException != null)
{
return false;
}
wasGameRunning = wasGameRunningInt == 1;
return true;
}
/// <summary>
/// Asynchronously hook to the game process making the window resizable and wait until the game exit.
/// </summary>
/// <param name="context">The context to launch the game from <see cref="IGameManager"/>.</param>
/// <param name="executableName">The name of the game executable.</param>
/// <param name="height">Height of the host screen.</param>
/// <param name="width">Width of the host screen.</param>
/// <param name="executableDirectory">The path to the directory where the game executable is located.</param>
/// <param name="token">
/// Cancellation token to pass into the plugin's game launch mechanism.<br/>
/// If cancellation is requested, it will cancel the awaiting but not killing the game process.
/// </param>
/// <returns>
/// Returns <c>IsSupported.false</c> if the plugin's API Standard is equal or lower than v0.1.3 or if this method isn't overriden.<br/>
/// Otherwise, <c>IsSupported.true</c> if the plugin supports game launch mechanism and this method.
/// </returns>
public static async Task<(bool IsSuccess, Exception? Error)>
StartResizableWindowHookAsync(this RunGameFromGameManagerContext context,
string? executableName = null,
int? height = null,
int? width = null,
string? executableDirectory = null,
CancellationToken token = default)
{
ArgumentNullException.ThrowIfNull(context, nameof(context));
if (!context.PluginHandle.TryGetExport("StartResizableWindowHookAsync", out SharedStaticV1Ext.StartResizableWindowHookAsyncDelegate startResizableWindowHookAsyncCallback))
{
return (false, new NotSupportedException("Plugin doesn't have StartResizableWindowHookAsync export in its API definition!"));
}
nint gameManagerP = GetPointerFromInterface(context.GameManager);
nint presetConfigP = GetPointerFromInterface(context.PresetConfig);
if (gameManagerP == nint.Zero)
{
return (false, new COMException("Cannot cast IGameManager interface to pointer!"));
}
if (presetConfigP == nint.Zero)
{
return (false, new COMException("Cannot cast IPluginPresetConfig interface to pointer!"));
}
nint exeNameP = executableName.GetPinnableStringPointerSafe();
int exeNameLen = executableName?.Length ?? 0;
nint exeDirP = executableDirectory.GetPinnableStringPointerSafe();
int exeDirLen = executableDirectory?.Length ?? 0;
Guid cancelTokenGuid = Guid.CreateVersion7();
int hResult = startResizableWindowHookAsyncCallback(gameManagerP,
presetConfigP,
exeNameP,
exeNameLen,
height ?? int.MinValue,
width ?? int.MinValue,
exeDirP,
exeDirLen,
ref cancelTokenGuid,
out nint taskResult);
if (taskResult == nint.Zero)
{
return (false, new NullReferenceException("ComAsyncResult pointer in taskReturn argument shouldn't return a null pointer!"));
}
if (hResult != 0)
{
return (false, Marshal.GetExceptionForHR(hResult));
}
return await ExecuteSuccessAsyncTask(context.Plugin, taskResult, cancelTokenGuid, token);
}
private static unsafe nint GetPointerFromInterface<T>(this T interfaceSource)
where T : class
=> (nint)ComInterfaceMarshaller<T>.ConvertToUnmanaged(interfaceSource);
private static async Task<(bool IsSuccess, Exception? Error)>
ExecuteSuccessAsyncTask(IPlugin pluginInstance,
nint taskResult,
Guid cancelTokenGuid,
CancellationToken token)
{
try
{
token.Register(() => pluginInstance.CancelAsync(in cancelTokenGuid));
bool isSuccess = await taskResult.AsTask<bool>();
if (isSuccess)
{
return (true, null);
}
throw new Exception("Failed to await ComAsyncResult as Task<bool>!");
}
catch (OperationCanceledException ex)
{
return (true, ex);
}
catch (Exception ex)
{
return (false, ex);
}
}
}