-
Notifications
You must be signed in to change notification settings - Fork 0
Detach ResizableWindowHook implementation for plugins #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gablm
wants to merge
4
commits into
net-10
Choose a base branch
from
detach-resize-hook
base: net-10
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| 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<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(RunGameFromGameManagerContext, string?, int, int, string?, CancellationToken)"/> 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 > exeNameLen) | ||
| { | ||
| 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 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.