Skip to content

Commit

Permalink
Moving out all PInvoke Methods to its Independent Hi3Helper.Win32 ass…
Browse files Browse the repository at this point in the history
…embly (#606)

# Main Goal
As you might already know, Collapse heavily relies on native invocation
for some libraries, especially when it is communicating with Win32 APIs.
For more than two years, we have been using the ``[DllImport]``
attribute to define the external method to call the native functions of
certain libraries (especially when it comes to `Win32` calls).

In .NET 7, ``[LibraryImport]`` was introduced, which is the
``[DllImport]`` alternative that provides the source-generated
invocation to native functions, instead of using a JIT-ed approach. The
``[LibraryImport]`` brings some invocation features which aren't
available for NativeAOT compilations, for example:
``GetLastWin32Error()``, which is useful when diagnosing what's wrong
when a `Win32` function is getting called.

Read more about ``[LibraryImport]`` here:

https://learn.microsoft.com/en-us/dotnet/standard/native-interop/pinvoke-source-generation

https://learn.microsoft.com/en-us/dotnet/standard/native-interop/best-practices

Another reason for these changes are for maintainability and to share
some of the same functionality between our projects. This changes can
improve the work time efficiency and reusability of the functions
between methods without making or implementing the same methods with the
same functionality between modules.

## PR Status :
- Overall Status : Completed
- Commits : Completed
- Synced to base (Collapse:main) : Yes
- Build status : OK
- Crashing : No
- Bug found caused by PR : Unsure

### Templates

<details>
  <summary>Changelog Prefixes</summary>
  
  ```
    **[New]**
    **[Imp]**
    **[Fix]**
    **[Loc]**
    **[Doc]**
  ```

</details>
  • Loading branch information
neon-nyan authored Nov 22, 2024
2 parents 094c61b + df75dda commit 8c3e282
Show file tree
Hide file tree
Showing 72 changed files with 914 additions and 3,562 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@
[submodule "H.NotifyIcon"]
path = H.NotifyIcon
url = https://github.com/CollapseLauncher/H.NotifyIcon
[submodule "Hi3Helper.Win32"]
path = Hi3Helper.Win32
url = https://github.com/CollapseLauncher/Hi3Helper.Win32
1 change: 1 addition & 0 deletions .idea/.idea.CollapseLauncher/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions CollapseLauncher.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@
<BuildType Solution="Publish|*" Project="Release" />
<Platform Project="x64" />
</Project>
<Project Path="Hi3Helper.Win32/Hi3Helper.Win32.csproj">
<BuildType Solution="Publish|*" Project="Debug" />
<Platform Project="x64" />
</Project>
<Project Path="ImageEx\ImageEx\ImageEx.csproj" Id="9c7abc1d-504e-45c4-ae76-98a9f6e28c80">
<BuildType Solution="Publish|*" Project="Release" />
<Platform Project="x64" />
Expand Down
7 changes: 5 additions & 2 deletions CollapseLauncher/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using Hi3Helper;
using Hi3Helper.SentryHelper;
using Hi3Helper.Shared.Region;
using Hi3Helper.Win32.Native;
using Hi3Helper.Win32.Native.Enums;
using Microsoft.UI;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
Expand All @@ -12,7 +14,6 @@
using System.Linq;
using Windows.UI;
using static CollapseLauncher.InnerLauncherConfig;
using static Hi3Helper.InvokeProp;
using static Hi3Helper.Logger;

namespace CollapseLauncher
Expand All @@ -37,6 +38,7 @@ public App()
DebugSettings.XamlResourceReferenceFailed += static (sender, args) =>
{
LogWriteLine($"[XAML_RES_REFERENCE] Sender: {sender}\r\n{args!.Message}", LogType.Error, true);
SentryHelper.ExceptionHandler(new Exception($"{args.Message}"), SentryHelper.ExceptionType.UnhandledXaml);
#if !DEBUG
MainEntryPoint.SpawnFatalErrorConsole(new Exception(args!.Message));
#endif
Expand All @@ -45,6 +47,7 @@ public App()
DebugSettings.BindingFailed += static (sender, args) =>
{
LogWriteLine($"[XAML_BINDING] Sender: {sender}\r\n{args!.Message}", LogType.Error, true);
SentryHelper.ExceptionHandler(new Exception($"{args.Message}"), SentryHelper.ExceptionType.UnhandledXaml);
#if !DEBUG
MainEntryPoint.SpawnFatalErrorConsole(new Exception(args!.Message));
#endif
Expand All @@ -64,7 +67,7 @@ public App()
}

RequestedTheme = IsAppThemeLight ? ApplicationTheme.Light : ApplicationTheme.Dark;
SetPreferredAppMode(ShouldAppsUseDarkMode() ? PreferredAppMode.AllowDark : PreferredAppMode.Default);
PInvoke.SetPreferredAppMode(PInvoke.ShouldAppsUseDarkMode() ? PreferredAppMode.AllowDark : PreferredAppMode.Default);

this.InitializeComponent();
}
Expand Down
176 changes: 0 additions & 176 deletions CollapseLauncher/Classes/CoCreateInstance.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
using CollapseLauncher.Dialogs;
using CollapseLauncher.Extension;
using CollapseLauncher.FileDialogCOM;
using CollapseLauncher.Helper;
using Hi3Helper;
using Hi3Helper.Data;
using Hi3Helper.Shared.Region;
using Hi3Helper.Win32.FileDialogCOM;
using Microsoft.UI.Text;
using Microsoft.UI.Xaml.Controls;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

#nullable enable
namespace CollapseLauncher.Classes.FileDialogCOM
namespace CollapseLauncher.FileDialogCOM
{
internal static class FileDialogHelper
{
Expand Down Expand Up @@ -95,7 +94,7 @@ await SpawnInvalidDialog(
return dirPath;


async Task SpawnInvalidDialog(string title, string message, string selectedPath, bool isUseLegacyFormatting = false)
async Task SpawnInvalidDialog(string dialogTitle, string message, string selectedPath, bool isUseLegacyFormatting = false)
{
TextBlock textBlock = new TextBlock()
{
Expand All @@ -113,7 +112,7 @@ async Task SpawnInvalidDialog(string title, string message, string selectedPath,
}

await SimpleDialogs.SpawnDialog(
isUseLegacyFormatting ? title : string.Format(Locale.Lang._Dialogs.InvalidGameDirNewTitleFormat, title),
isUseLegacyFormatting ? dialogTitle : string.Format(Locale.Lang._Dialogs.InvalidGameDirNewTitleFormat, dialogTitle),
textBlock,
(WindowUtility.CurrentWindow as MainWindow)?.Content,
Locale.Lang._Misc.Okay,
Expand Down
49 changes: 0 additions & 49 deletions CollapseLauncher/Classes/FileDialogCOM/Enums.cs

This file was deleted.

Loading

0 comments on commit 8c3e282

Please sign in to comment.