|
| 1 | +using Hi3Helper.Plugin.Core.Management; |
| 2 | +using Hi3Helper.Plugin.Core.Management.PresetConfig; |
| 3 | +using Hi3Helper.Plugin.Core.Utility; |
| 4 | +using Microsoft.Extensions.Logging; |
| 5 | +using System; |
| 6 | +using System.Runtime.CompilerServices; |
| 7 | +using System.Runtime.InteropServices; |
| 8 | +using System.Threading; |
| 9 | +using System.Threading.Tasks; |
| 10 | + |
| 11 | +using static Hi3Helper.Plugin.Core.Utility.GameManagerExtension; |
| 12 | + |
| 13 | +#if !MANUALCOM |
| 14 | +using System.Runtime.InteropServices.Marshalling; |
| 15 | +#endif |
| 16 | + |
| 17 | +namespace Hi3Helper.Plugin.Core; |
| 18 | + |
| 19 | +public partial class SharedStaticV1Ext<T> |
| 20 | +{ |
| 21 | + private static void InitExtension_Update3Exports() |
| 22 | + { |
| 23 | + /* ---------------------------------------------------------------------- |
| 24 | + * Update 3 Feature Sets |
| 25 | + * ---------------------------------------------------------------------- |
| 26 | + * This feature sets includes the following feature: |
| 27 | + * - Game Launch |
| 28 | + * - StartResizableWindowHookAsync |
| 29 | + */ |
| 30 | + |
| 31 | + // -> Plugin Async Resizable Window Hook Callback for Specific Game Region based on its IGameManager instance. |
| 32 | + TryRegisterApiExport<StartResizableWindowHookAsyncDelegate>("StartResizableWindowHookAsync", StartResizableWindowHookAsync); |
| 33 | + } |
| 34 | + |
| 35 | + #region ABI Proxies |
| 36 | + /// <summary> |
| 37 | + /// This method is an ABI proxy function between the PInvoke Export and the actual plugin's method.<br/> |
| 38 | + /// See the documentation for <see cref="SharedStaticV1Ext{T}.StartResizableWindowHookAsync(RunGameFromGameManagerContext, string?, int, int, string?, CancellationToken)"/> method for more information. |
| 39 | + /// </summary> |
| 40 | + private static unsafe HResult StartResizableWindowHookAsync(nint gameManagerP, |
| 41 | + nint presetConfigP, |
| 42 | + nint exeName, |
| 43 | + int exeNameLen, |
| 44 | + int height, |
| 45 | + int width, |
| 46 | + nint exeDir, |
| 47 | + int exeDirLen, |
| 48 | + ref Guid cancelToken, |
| 49 | + out nint taskResult) |
| 50 | + { |
| 51 | + taskResult = nint.Zero; |
| 52 | + try |
| 53 | + { |
| 54 | +#if MANUALCOM |
| 55 | + IGameManager? gameManager = ComWrappers.ComInterfaceDispatch.GetInstance<IGameManager>((ComWrappers.ComInterfaceDispatch*)gameManagerP); |
| 56 | + IPluginPresetConfig? presetConfig = ComWrappers.ComInterfaceDispatch.GetInstance<IPluginPresetConfig>((ComWrappers.ComInterfaceDispatch*)presetConfigP); |
| 57 | +#else |
| 58 | + IGameManager? gameManager = ComInterfaceMarshaller<IGameManager>.ConvertToManaged((void*)gameManagerP); |
| 59 | + IPluginPresetConfig? presetConfig = ComInterfaceMarshaller<IPluginPresetConfig>.ConvertToManaged((void*)presetConfigP); |
| 60 | +#endif |
| 61 | + |
| 62 | + if (ThisExtensionExport == null) |
| 63 | + { |
| 64 | + throw new NullReferenceException("The ThisPluginExport field is null!"); |
| 65 | + } |
| 66 | + |
| 67 | + if (gameManager == null) |
| 68 | + { |
| 69 | + throw new NullReferenceException("Cannot cast IGameManager from the pointer, hence it gives null!"); |
| 70 | + } |
| 71 | + |
| 72 | + if (presetConfig == null) |
| 73 | + { |
| 74 | + throw new NullReferenceException("Cannot cast IPluginPresetConfig from the pointer, hence it gives null!"); |
| 75 | + } |
| 76 | + |
| 77 | + CancellationTokenSource? cts = null; |
| 78 | + if (Unsafe.IsNullRef(ref cancelToken)) |
| 79 | + { |
| 80 | + cts = ComCancellationTokenVault.RegisterToken(in cancelToken); |
| 81 | + } |
| 82 | + |
| 83 | + RunGameFromGameManagerContext context = new() |
| 84 | + { |
| 85 | + GameManager = gameManager, |
| 86 | + PresetConfig = presetConfig, |
| 87 | + Plugin = null!, |
| 88 | + PrintGameLogCallback = null!, |
| 89 | + PluginHandle = nint.Zero |
| 90 | + }; |
| 91 | + |
| 92 | + string? executableName = null; |
| 93 | + if (exeNameLen > 0) |
| 94 | + { |
| 95 | + char* exeNameP = (char*)exeName; |
| 96 | + ReadOnlySpan<char> executableNameSpan = Mem.CreateSpanFromNullTerminated<char>(exeNameP); |
| 97 | + if (executableNameSpan.Length > exeNameLen) |
| 98 | + { |
| 99 | + executableNameSpan = executableNameSpan[..exeNameLen]; |
| 100 | + } |
| 101 | + |
| 102 | + executableName = executableNameSpan.IsEmpty ? null : executableNameSpan.ToString(); |
| 103 | + } |
| 104 | + |
| 105 | + string? executableDirectory = null; |
| 106 | + if (exeDirLen > 0) |
| 107 | + { |
| 108 | + char* exeDirP = (char*)exeDir; |
| 109 | + ReadOnlySpan<char> executableDirectorySpan = Mem.CreateSpanFromNullTerminated<char>(exeDirP); |
| 110 | + if (executableDirectorySpan.Length > exeDirLen) |
| 111 | + { |
| 112 | + executableDirectorySpan = executableDirectorySpan[..exeDirLen]; |
| 113 | + } |
| 114 | + |
| 115 | + executableDirectory = executableDirectorySpan.IsEmpty ? null : executableDirectorySpan.ToString(); |
| 116 | + } |
| 117 | + |
| 118 | + (bool isSupported, Task<bool> task) = ThisExtensionExport |
| 119 | + .StartResizableWindowHookAsync(context, |
| 120 | + executableName, |
| 121 | + height == int.MinValue ? null : height, |
| 122 | + width == int.MinValue ? null : width, |
| 123 | + executableDirectory, |
| 124 | + cts?.Token ?? CancellationToken.None); |
| 125 | + |
| 126 | + taskResult = task.AsResult(); |
| 127 | + return isSupported; |
| 128 | + } |
| 129 | + catch (Exception ex) |
| 130 | + { |
| 131 | + // ignored |
| 132 | + InstanceLogger.LogError(ex, "An error has occurred while trying to call StartResizableWindowHookAsync() from the plugin!"); |
| 133 | + return Marshal.GetHRForException(ex); |
| 134 | + } |
| 135 | + } |
| 136 | + #endregion |
| 137 | + |
| 138 | + #region Core Methods |
| 139 | + /// <summary> |
| 140 | + /// Asynchronously hook to the game process making the window resizable and wait until the game exit. |
| 141 | + /// </summary> |
| 142 | + /// <param name="context">The context to launch the game from <see cref="IGameManager"/>.</param> |
| 143 | + /// <param name="executableName">The name of the game executable.</param> |
| 144 | + /// <param name="height">Height of the host screen.</param> |
| 145 | + /// <param name="width">Width of the host screen.</param> |
| 146 | + /// <param name="executableDirectory">The path to the directory where the game executable is located.</param> |
| 147 | + /// <param name="token"> |
| 148 | + /// Cancellation token to pass into the plugin's game launch mechanism.<br/> |
| 149 | + /// If cancellation is requested, it will cancel the awaiting but not killing the game process. |
| 150 | + /// </param> |
| 151 | + /// <returns> |
| 152 | + /// 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/> |
| 153 | + /// Otherwise, <c>IsSupported.true</c> if the plugin supports game launch mechanism and this method. |
| 154 | + /// </returns> |
| 155 | + protected virtual (bool IsSupported, Task<bool> Task) StartResizableWindowHookAsync( |
| 156 | + RunGameFromGameManagerContext context, |
| 157 | + string? executableName, |
| 158 | + int? height, |
| 159 | + int? width, |
| 160 | + string? executableDirectory, |
| 161 | + CancellationToken token) |
| 162 | + { |
| 163 | + return (false, Task.FromResult(false)); |
| 164 | + } |
| 165 | + #endregion |
| 166 | +} |
0 commit comments