-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSharedStatic.V1Ext_Update3.cs
More file actions
182 lines (163 loc) · 7.93 KB
/
SharedStatic.V1Ext_Update3.cs
File metadata and controls
182 lines (163 loc) · 7.93 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
using Hi3Helper.Plugin.Core.Management;
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.Threading;
using System.Threading.Tasks;
using static Hi3Helper.Plugin.Core.Utility.GameManagerExtension;
#if !MANUALCOM
using System.Runtime.InteropServices.Marshalling;
#endif
namespace Hi3Helper.Plugin.Core;
public partial class SharedStaticV1Ext : SharedStatic
{
// Update3
internal delegate HResult StartResizableWindowHookAsyncDelegate(
nint gameManagerP,
nint presetConfigP,
nint executableName,
int executableNameLen,
int height,
int width,
nint executableDirectory,
int executableDirectoryLen,
ref Guid cancelToken,
out nint taskResult);
}
public partial class SharedStaticV1Ext<T>
{
private static void InitExtension_Update3Exports()
{
/* ----------------------------------------------------------------------
* Update 3 Feature Sets
* ----------------------------------------------------------------------
* This feature sets includes the following feature:
* - Game Launch
* - StartResizableWindowHookAsync
*/
// -> Plugin Async Resizable Window Hook Callback for Specific Game Region based on its IGameManager instance.
TryRegisterApiExport<StartResizableWindowHookAsyncDelegate>("StartResizableWindowHookAsync", StartResizableWindowHookAsync);
}
#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}.StartResizableWindowHookAsync(nint, nint, nint, int, int, int, nint, int, ref Guid, out nint)"/> method for more information.
/// </summary>
private static unsafe HResult StartResizableWindowHookAsync(nint gameManagerP,
nint presetConfigP,
nint exeName,
int exeNameLen,
int height,
int width,
nint exeDir,
int exeDirLen,
ref Guid cancelToken,
out nint taskResult)
{
taskResult = nint.Zero;
try
{
#if MANUALCOM
IGameManager? gameManager = ComWrappers.ComInterfaceDispatch.GetInstance<IGameManager>((ComWrappers.ComInterfaceDispatch*)gameManagerP);
IPluginPresetConfig? presetConfig = ComWrappers.ComInterfaceDispatch.GetInstance<IPluginPresetConfig>((ComWrappers.ComInterfaceDispatch*)presetConfigP);
#else
IGameManager? gameManager = ComInterfaceMarshaller<IGameManager>.ConvertToManaged((void*)gameManagerP);
IPluginPresetConfig? presetConfig = ComInterfaceMarshaller<IPluginPresetConfig>.ConvertToManaged((void*)presetConfigP);
#endif
if (ThisExtensionExport == null)
{
throw new NullReferenceException("The ThisPluginExport field is null!");
}
if (gameManager == null)
{
throw new NullReferenceException("Cannot cast IGameManager from the pointer, hence it gives null!");
}
if (presetConfig == null)
{
throw new NullReferenceException("Cannot cast IPluginPresetConfig from the pointer, hence it gives null!");
}
CancellationTokenSource? cts = null;
if (Unsafe.IsNullRef(ref cancelToken))
{
cts = ComCancellationTokenVault.RegisterToken(in cancelToken);
}
RunGameFromGameManagerContext context = new()
{
GameManager = gameManager,
PresetConfig = presetConfig,
Plugin = null!,
PrintGameLogCallback = null!,
PluginHandle = nint.Zero
};
string? executableName = null;
if (exeNameLen > 0)
{
char* exeNameP = (char*)exeName;
ReadOnlySpan<char> executableNameSpan = Mem.CreateSpanFromNullTerminated<char>(exeNameP);
if (executableNameSpan.Length > exeNameLen)
{
executableNameSpan = executableNameSpan[..exeNameLen];
}
executableName = executableNameSpan.IsEmpty ? null : executableNameSpan.ToString();
}
string? executableDirectory = null;
if (exeDirLen > 0)
{
char* exeDirP = (char*)exeDir;
ReadOnlySpan<char> executableDirectorySpan = Mem.CreateSpanFromNullTerminated<char>(exeDirP);
if (executableDirectorySpan.Length > exeDirLen)
{
executableDirectorySpan = executableDirectorySpan[..exeDirLen];
}
executableDirectory = executableDirectorySpan.IsEmpty ? null : executableDirectorySpan.ToString();
}
(bool isSupported, Task<bool> task) = ThisExtensionExport
.StartResizableWindowHookAsync(context,
executableName,
height == int.MinValue ? null : height,
width == int.MinValue ? null : width,
executableDirectory,
cts?.Token ?? CancellationToken.None);
taskResult = task.AsResult();
return isSupported;
}
catch (Exception ex)
{
// ignored
InstanceLogger.LogError(ex, "An error has occurred while trying to call StartResizableWindowHookAsync() from the plugin!");
return Marshal.GetHRForException(ex);
}
}
#endregion
#region Core Methods
/// <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>
protected virtual (bool IsSupported, Task<bool> Task) StartResizableWindowHookAsync(
RunGameFromGameManagerContext context,
string? executableName,
int? height,
int? width,
string? executableDirectory,
CancellationToken token)
{
return (false, Task.FromResult(false));
}
#endregion
}