2024-02-15 01:41:20 +01:00
|
|
|
|
using CustomResolution.WndProcHookManagerProxyApi;
|
2024-02-16 00:05:51 +01:00
|
|
|
|
using Serilog.Events;
|
2024-02-15 01:41:20 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection.Emit;
|
|
|
|
|
using TerraFX.Interop.Windows;
|
2024-03-16 21:29:16 +01:00
|
|
|
|
using static TerraFX.Interop.Windows.Windows;
|
2024-02-15 01:41:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace CustomResolution.Hooks;
|
|
|
|
|
|
|
|
|
|
// THIS. IS. UGLY.
|
2024-03-16 21:29:16 +01:00
|
|
|
|
public sealed unsafe class WndProcHook : IDisposable
|
2024-02-15 01:41:20 +01:00
|
|
|
|
{
|
|
|
|
|
private static WndProcHook? _instance;
|
|
|
|
|
|
|
|
|
|
private readonly WndProcHookManager _manager;
|
|
|
|
|
|
|
|
|
|
private bool _applied = false;
|
|
|
|
|
private DynamicMethod? _genMethod;
|
|
|
|
|
private Delegate? _genDelegate;
|
|
|
|
|
|
|
|
|
|
public WndProcHook()
|
|
|
|
|
{
|
|
|
|
|
_instance = this;
|
|
|
|
|
|
|
|
|
|
_manager = new();
|
|
|
|
|
|
|
|
|
|
Apply();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
if (!_applied)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_manager.PreWndProc is not { } preWndProcProp)
|
|
|
|
|
{
|
|
|
|
|
Service.PluginLog.Information("CustomResolution couldn't obtain the PreWndProc event.");
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
preWndProcProp.RemoveEventHandler(_manager.ProxiedValue, _genDelegate);
|
|
|
|
|
|
|
|
|
|
_applied = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void InvokeStatic(object args)
|
|
|
|
|
{
|
|
|
|
|
if (_instance is { } instance)
|
|
|
|
|
{
|
|
|
|
|
instance.Invoke(new WndProcEventArgs(instance._manager, args));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void ParamToCoords(LPARAM param, out int x, out int y)
|
|
|
|
|
{
|
2024-02-16 00:05:51 +01:00
|
|
|
|
x = (short) (ushort) (param.Value & 0xFFFF);
|
|
|
|
|
y = (short) (ushort) (param.Value >> 16 & 0xFFFF);
|
2024-02-15 01:41:20 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static LPARAM CoordsToParam(int x, int y)
|
|
|
|
|
{
|
|
|
|
|
nint value = 0;
|
2024-02-16 00:05:51 +01:00
|
|
|
|
value |= ((ushort) (short) x) & 0xFFFF;
|
|
|
|
|
value |= ((ushort) (short) y) >> 16 & 0xFFFF;
|
2024-02-15 01:41:20 +01:00
|
|
|
|
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Invoke(WndProcEventArgs args)
|
|
|
|
|
{
|
2024-03-16 21:29:16 +01:00
|
|
|
|
if (Service.Plugin is not { } plugin)
|
2024-02-15 01:41:20 +01:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-16 21:29:16 +01:00
|
|
|
|
if (WM.WM_MOUSEFIRST <= args.Message && args.Message <= WM.WM_MOUSELAST)
|
|
|
|
|
{
|
|
|
|
|
ParamToCoords(args.LParam, out int x, out int y);
|
2024-02-15 01:41:20 +01:00
|
|
|
|
|
2024-02-16 00:05:51 +01:00
|
|
|
|
#if false
|
2024-03-16 21:29:16 +01:00
|
|
|
|
Service.PluginLog.Debug($"WM_MOUSE A @ {x} {y}");
|
2024-02-16 00:05:51 +01:00
|
|
|
|
#endif
|
|
|
|
|
|
2024-03-16 21:29:16 +01:00
|
|
|
|
plugin.ConvertCoordsWinToGame(ref x, ref y);
|
2024-02-15 01:41:20 +01:00
|
|
|
|
|
2024-02-16 00:05:51 +01:00
|
|
|
|
#if false
|
2024-03-16 21:29:16 +01:00
|
|
|
|
Service.PluginLog.Debug($"WM_MOUSE B @ {x} {y}");
|
2024-02-16 00:05:51 +01:00
|
|
|
|
#endif
|
|
|
|
|
|
2024-03-16 21:29:16 +01:00
|
|
|
|
args.LParam = CoordsToParam(x, y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (args.Message == WM.WM_NCCALCSIZE && args.WParam != 0 && plugin.CurrentBorderlessFullscreen &&
|
|
|
|
|
Service.Config.DXVKDWMHackMode >= DXVKDWMHackMode.SetClientEdge)
|
|
|
|
|
{
|
|
|
|
|
NCCALCSIZE_PARAMS* ncsize = (NCCALCSIZE_PARAMS*) args.LParam;
|
|
|
|
|
MONITORINFO monitorInfo = new()
|
|
|
|
|
{
|
|
|
|
|
cbSize = (uint) sizeof(MONITORINFO)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (MonitorFromWindow(plugin.CurrentHWND, MONITOR.MONITOR_DEFAULTTONEAREST) is { } monitor && monitor != HMONITOR.NULL &&
|
|
|
|
|
GetMonitorInfo(monitor, &monitorInfo))
|
|
|
|
|
{
|
|
|
|
|
ncsize->rgrc[0] = monitorInfo.rcMonitor;
|
2024-03-16 21:37:50 +01:00
|
|
|
|
ncsize->rgrc[0].bottom += 1;
|
2024-03-16 21:29:16 +01:00
|
|
|
|
|
|
|
|
|
args.SuppressCall = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-15 01:41:20 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Apply()
|
|
|
|
|
{
|
|
|
|
|
if (!_manager.Refresh())
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_applied)
|
|
|
|
|
{
|
|
|
|
|
Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_manager.PreWndProc is not { } preWndProcProp)
|
|
|
|
|
{
|
|
|
|
|
Service.PluginLog.Information("CustomResolution couldn't obtain the PreWndProc event.");
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_genDelegate is null)
|
|
|
|
|
{
|
|
|
|
|
var delegateType = preWndProcProp.EventHandlerType!;
|
|
|
|
|
var delegateInvoke = delegateType.GetMethod("Invoke")!;
|
|
|
|
|
|
|
|
|
|
_genMethod = new DynamicMethod(
|
|
|
|
|
"CustomResolution_PreWndProc",
|
|
|
|
|
delegateInvoke.ReturnType,
|
|
|
|
|
delegateInvoke.GetParameters().Select(p => p.ParameterType).ToArray()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
var il = _genMethod.GetILGenerator();
|
|
|
|
|
|
|
|
|
|
il.Emit(OpCodes.Ldarg_0);
|
|
|
|
|
il.Emit(OpCodes.Call, typeof(WndProcHook).GetMethod("InvokeStatic")!);
|
|
|
|
|
il.Emit(OpCodes.Ret);
|
|
|
|
|
|
|
|
|
|
_genDelegate = _genMethod.CreateDelegate(delegateType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
preWndProcProp.AddEventHandler(_manager.ProxiedValue, _genDelegate);
|
|
|
|
|
|
|
|
|
|
_applied = true;
|
|
|
|
|
}
|
|
|
|
|
}
|