Skip to content

Commit bb42f97

Browse files
committed
Add new arguments on GameManagerExtension.RunGameFromGameManagerAsync
1 parent 47eee72 commit bb42f97

File tree

3 files changed

+58
-7
lines changed

3 files changed

+58
-7
lines changed

SharedStatic.Generic.cs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Hi3Helper.Plugin.Core.Utility;
44
using Microsoft.Extensions.Logging;
55
using System;
6+
using System.Diagnostics;
67
using System.Runtime.CompilerServices;
78
using System.Runtime.InteropServices;
89
using System.Threading;
@@ -45,9 +46,18 @@ static SharedStatic()
4546

4647
/// <summary>
4748
/// This method is an ABI proxy function between the PInvoke Export and the actual plugin's method.<br/>
48-
/// See the documentation for <see cref="SharedStatic.LaunchGameFromGameManagerCoreAsync(RunGameFromGameManagerContext, CancellationToken)"/> method for more information.
49+
/// See the documentation for <see cref="SharedStatic.LaunchGameFromGameManagerCoreAsync(RunGameFromGameManagerContext, string?, bool, ProcessPriorityClass, CancellationToken)"/> method for more information.
4950
/// </summary>
50-
public static unsafe int LaunchGameFromGameManagerAsync(nint gameManagerP, nint pluginP, nint presetConfigP, nint printGameLogCallbackP, ref Guid cancelToken, out nint taskResult)
51+
public static unsafe int LaunchGameFromGameManagerAsync(nint gameManagerP,
52+
nint pluginP,
53+
nint presetConfigP,
54+
nint printGameLogCallbackP,
55+
nint arguments,
56+
int argumentsLen,
57+
int runBoostedInt,
58+
int processPriorityInt,
59+
ref Guid cancelToken,
60+
out nint taskResult)
5161
{
5262
taskResult = nint.Zero;
5363
try
@@ -104,8 +114,24 @@ public static unsafe int LaunchGameFromGameManagerAsync(nint gameManagerP, nint
104114
PluginHandle = nint.Zero
105115
};
106116

117+
bool isRunBoosted = runBoostedInt == 1;
118+
ProcessPriorityClass processPriorityClass = (ProcessPriorityClass)processPriorityInt;
119+
120+
string? startArguments = null;
121+
if (argumentsLen > 0)
122+
{
123+
char* argumentsP = (char*)arguments;
124+
ReadOnlySpan<char> argumentsSpan = Mem.CreateSpanFromNullTerminated<char>(argumentsP);
125+
if (argumentsSpan.Length > argumentsLen)
126+
{
127+
argumentsSpan = argumentsSpan[..argumentsLen];
128+
}
129+
130+
startArguments = argumentsSpan.IsEmpty ? null : argumentsSpan.ToString();
131+
}
132+
107133
taskResult = ThisPluginExport
108-
.LaunchGameFromGameManagerCoreAsync(context, cts?.Token ?? CancellationToken.None)
134+
.LaunchGameFromGameManagerCoreAsync(context, startArguments, isRunBoosted, processPriorityClass, cts?.Token ?? CancellationToken.None)
109135
.AsResult();
110136
return 0;
111137
}

SharedStatic.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44
using Microsoft.Extensions.Logging;
55
using System;
66
using System.Collections.Generic;
7+
using System.Diagnostics;
78
using System.Runtime.InteropServices;
89
using System.Runtime.InteropServices.Marshalling;
910
using System.Threading;
1011
using System.Threading.Tasks;
12+
1113
using static Hi3Helper.Plugin.Core.Utility.GameManagerExtension;
14+
1215
// ReSharper disable CommentTypo
1316

1417
#if MANUALCOM
@@ -124,7 +127,7 @@ static unsafe SharedStatic()
124127
protected unsafe delegate void GetPluginUpdateCdnListDelegate(int* count, ushort*** ptr);
125128
protected delegate void SetCallbackPointerDelegate(nint callbackP);
126129

127-
internal delegate int LaunchGameFromGameManagerAsyncDelegate(nint gameManagerP, nint pluginP, nint presetConfigP, nint printGameLogCallbackP, ref Guid cancelToken, out nint taskResult);
130+
internal delegate int LaunchGameFromGameManagerAsyncDelegate(nint gameManagerP, nint pluginP, nint presetConfigP, nint printGameLogCallbackP, nint arguments, int argumentsLen, int runBoostedInt, int processPriorityInt, ref Guid cancelToken, out nint taskResult);
128131
internal delegate int WaitRunningGameAsyncDelegate(nint gameManagerP, nint pluginP, nint presetConfigP, ref Guid cancelToken, out nint taskResult);
129132
internal delegate int IsGameRunningDelegate(nint gameManagerP, nint presetConfigP, out int isGameRunning);
130133

@@ -374,6 +377,9 @@ public static unsafe int TryGetApiExportPointer(char* apiExportName, void** dele
374377
/// Asynchronously launch the game using plugin's built-in game launch mechanism and wait until the game exit.
375378
/// </summary>
376379
/// <param name="context">The context to launch the game from <see cref="IGameManager"/>.</param>
380+
/// <param name="startArgument">The additional argument to run the game executable.</param>
381+
/// <param name="isRunBoosted">Based on <see cref="Process.PriorityBoostEnabled"/>, boost the process temporarily when the game window is focused (Default: false).</param>
382+
/// <param name="processPriority">Based on <see cref="Process.PriorityClass"/>, run the game process with specific priority (Default: <see cref="ProcessPriorityClass.Normal"/>).</param>
377383
/// <param name="token">
378384
/// Cancellation token to pass into the plugin's game launch mechanism.<br/>
379385
/// If cancellation is requested, it will cancel the awaiting but not killing the game process.
@@ -382,7 +388,7 @@ public static unsafe int TryGetApiExportPointer(char* apiExportName, void** dele
382388
/// 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/>
383389
/// Otherwise, <c>true</c> if the plugin supports game launch mechanism.
384390
/// </returns>
385-
public virtual Task<bool> LaunchGameFromGameManagerCoreAsync(RunGameFromGameManagerContext context, CancellationToken token)
391+
public virtual Task<bool> LaunchGameFromGameManagerCoreAsync(RunGameFromGameManagerContext context, string? startArgument, bool isRunBoosted, ProcessPriorityClass processPriority, CancellationToken token)
386392
{
387393
return Task.FromResult(false);
388394
}

Utility/GameManagerExtension.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Hi3Helper.Plugin.Core.Management;
22
using Hi3Helper.Plugin.Core.Management.PresetConfig;
33
using System;
4+
using System.Diagnostics;
45
using System.Diagnostics.CodeAnalysis;
56
using System.Runtime.InteropServices;
67
using System.Runtime.InteropServices.Marshalling;
@@ -54,6 +55,9 @@ public class RunGameFromGameManagerContext
5455
/// Asynchronously launch the game using plugin's built-in game launch mechanism and wait until the game exit.
5556
/// </summary>
5657
/// <param name="context">The context to launch the game from <see cref="IGameManager"/>.</param>
58+
/// <param name="startArgument">The additional argument string to run the game executable (Default: null).</param>
59+
/// <param name="isRunBoosted">Based on <see cref="Process.PriorityBoostEnabled"/>, boost the process temporarily when the game window is focused (Default: false).</param>
60+
/// <param name="processPriority">Based on <see cref="Process.PriorityClass"/>, run the game process with specific priority (Default: <see cref="ProcessPriorityClass.Normal"/>).</param>
5761
/// <param name="token">
5862
/// Cancellation token to pass into the plugin's game launch mechanism.<br/>
5963
/// If cancellation is requested, it will cancel the awaiting but not killing the game process.
@@ -64,7 +68,10 @@ public class RunGameFromGameManagerContext
6468
/// </returns>
6569
public static async Task<(bool IsSuccess, Exception? Error)>
6670
RunGameFromGameManagerAsync(this RunGameFromGameManagerContext context,
67-
CancellationToken token)
71+
string? startArgument = null,
72+
bool isRunBoosted = false,
73+
ProcessPriorityClass processPriority = ProcessPriorityClass.Normal,
74+
CancellationToken token = default)
6875
{
6976
ArgumentNullException.ThrowIfNull(context, nameof(context));
7077
if (!context.PluginHandle.TryGetExport("LaunchGameFromGameManagerAsync", out SharedStatic.LaunchGameFromGameManagerAsyncDelegate launchGameFromGameManagerAsyncCallback))
@@ -97,8 +104,20 @@ public class RunGameFromGameManagerContext
97104
return (false, new COMException("Cannot cast PrintGameLog delegate/callback to pointer!"));
98105
}
99106

107+
nint argumentsP = startArgument.GetPinnableStringPointerSafe();
108+
int argumentsLen = startArgument?.Length ?? 0;
109+
100110
Guid cancelTokenGuid = Guid.CreateVersion7();
101-
int hResult = launchGameFromGameManagerAsyncCallback(gameManagerP, pluginP, presetConfigP, printGameLogCallbackP, ref cancelTokenGuid, out nint taskResult);
111+
int hResult = launchGameFromGameManagerAsyncCallback(gameManagerP,
112+
pluginP,
113+
presetConfigP,
114+
printGameLogCallbackP,
115+
argumentsP,
116+
argumentsLen,
117+
isRunBoosted ? 1 : 0,
118+
(int)processPriority,
119+
ref cancelTokenGuid,
120+
out nint taskResult);
102121

103122
if (taskResult == nint.Zero)
104123
{

0 commit comments

Comments
 (0)