Skip to content

Commit

Permalink
Added some deeper hooks for CriticalSection
Browse files Browse the repository at this point in the history
  • Loading branch information
nike4613 committed Aug 29, 2019
1 parent 5b10f84 commit d9d5771
Show file tree
Hide file tree
Showing 4 changed files with 1,139 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Doorstop
Submodule Doorstop updated 3 files
+2 −1 Proxy/hook.h
+127 −4 Proxy/main.c
+2 −0 Proxy/proxy.def
1 change: 1 addition & 0 deletions IPA.Loader/IPA.Loader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
<Compile Include="Updating\BeatMods\Updater.cs" />
<Compile Include="Utilities\Extensions.cs" />
<Compile Include="Utilities\Utils.cs" />
<Compile Include="Utilities\Win32.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Ionic.Zip">
Expand Down
72 changes: 58 additions & 14 deletions IPA.Loader/Utilities/CriticalSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,74 @@ internal static void Configure()

#region Execute section

private static readonly Win32.EventHandler registeredHandler = HandleExit;
private static readonly Win32.ConsoleCtrlDelegate registeredHandler = HandleExit;
internal static void ResetExitHandlers()
{
Win32.SetConsoleCtrlHandler(registeredHandler, false);
Win32.SetConsoleCtrlHandler(registeredHandler, true);
WinHttp.SetPeekMessageHook(PeekMessageHook);
}

private static partial class Win32 {
[DllImport("Kernel32")]
public static extern bool SetConsoleCtrlHandler(EventHandler handler, bool add);
private static class WinHttp
{
public delegate bool PeekMessageHook(
bool isW,
uint result,
[MarshalAs(UnmanagedType.LPStruct)]
in Win32.MSG message,
IntPtr hwnd,
uint filterMin,
uint filterMax,
ref Win32.PeekMessageParams removeMsg);

[DllImport("winhttp")]
public static extern void SetPeekMessageHook(
[MarshalAs(UnmanagedType.FunctionPtr)]
PeekMessageHook hook);
}

public enum CtrlType
private static Win32.ConsoleCtrlDelegate _handler = null;
private static volatile bool isInExecuteSection = false;

// returns true to continue looping and calling PeekMessage
private static bool PeekMessageHook(
bool isW,
uint result,
[MarshalAs(UnmanagedType.LPStruct)]
in Win32.MSG message,
IntPtr hwnd,
uint filterMin,
uint filterMax,
ref Win32.PeekMessageParams removeMsg)
{
if (isInExecuteSection)
{
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT = 1,
CTRL_CLOSE_EVENT = 2,
CTRL_LOGOFF_EVENT = 5,
CTRL_SHUTDOWN_EVENT = 6
if (result == 0) return false;

switch (message.message)
{
case Win32.WM.CLOSE:
if (removeMsg != Win32.PeekMessageParams.PM_REMOVE)
{
removeMsg = Win32.PeekMessageParams.PM_REMOVE;
exitRecieved = true;
return true;
}
else
{
removeMsg = Win32.PeekMessageParams.PM_NOREMOVE;
return true;
}

default:
return false;
}
}

public delegate bool EventHandler(CtrlType sig);
return false;
}

private static Win32.EventHandler _handler = null;

private static bool HandleExit(Win32.CtrlType type)
private static bool HandleExit(Win32.CtrlTypes type)
{
if (_handler != null)
return _handler(type);
Expand All @@ -72,6 +114,7 @@ public static void EnterExecuteSection()

exitRecieved = false;
_handler = sig => exitRecieved = true;
isInExecuteSection = true;
}

/// <summary>
Expand All @@ -85,6 +128,7 @@ public static void EnterExecuteSection()
public static void ExitExecuteSection()
{
_handler = null;
isInExecuteSection = false;

if (exitRecieved)
Environment.Exit(1);
Expand Down
Loading

0 comments on commit d9d5771

Please sign in to comment.