From 18f2e16bb0febe03191fdbf97dc65767caa663eb Mon Sep 17 00:00:00 2001 From: FunkyFr3sh Date: Tue, 6 Aug 2019 04:37:06 +0200 Subject: [PATCH] make bnet moveable --- inc/hook.h | 8 ++++-- inc/mouse.h | 5 ++++ src/hook.c | 10 +++++++ src/main.c | 73 ++++++++++++++++++++++++++------------------------- src/mouse.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++- src/surface.c | 3 ++- 6 files changed, 130 insertions(+), 40 deletions(-) diff --git a/inc/hook.h b/inc/hook.h index 199eddc..8da0fec 100644 --- a/inc/hook.h +++ b/inc/hook.h @@ -3,8 +3,6 @@ #include -typedef HFONT(__stdcall* CREATEFONTINDIRECTA)(CONST LOGFONT*); - typedef BOOL (WINAPI* GETCURSORPOSPROC)(LPPOINT); typedef BOOL(WINAPI* CLIPCURSORPROC)(const RECT*); typedef int (WINAPI* SHOWCURSORPROC)(BOOL); @@ -22,6 +20,9 @@ typedef BOOL (WINAPI* SETWINDOWPOSPROC)(HWND, HWND, int, int, int, int, UINT); typedef BOOL (WINAPI* MOVEWINDOWPROC)(HWND, int, int, int, int, BOOL); typedef LRESULT (WINAPI* SENDMESSAGEAPROC)(HWND, UINT, WPARAM, LPARAM); typedef LONG (WINAPI* SETWINDOWLONGAPROC)(HWND, int, LONG); +typedef BOOL (WINAPI* ENABLEWINDOWPROC)(HWND, BOOL); +typedef HWND (WINAPI* CREATEWINDOWEXAPROC)(DWORD, LPCSTR, LPCSTR, DWORD, int, int, int, int, HWND, HMENU, HINSTANCE, LPVOID); +typedef BOOL (WINAPI* DESTROYWINDOWPROC)(HWND); extern GETCURSORPOSPROC real_GetCursorPos; extern CLIPCURSORPROC real_ClipCursor; @@ -40,6 +41,9 @@ extern SETWINDOWPOSPROC real_SetWindowPos; extern MOVEWINDOWPROC real_MoveWindow; extern SENDMESSAGEAPROC real_SendMessageA; extern SETWINDOWLONGAPROC real_SetWindowLongA; +extern ENABLEWINDOWPROC real_EnableWindow; +extern CREATEWINDOWEXAPROC real_CreateWindowExA; +extern DESTROYWINDOWPROC real_DestroyWindow; extern BOOL Hook_Active; diff --git a/inc/mouse.h b/inc/mouse.h index dd18506..a11fd3d 100644 --- a/inc/mouse.h +++ b/inc/mouse.h @@ -23,5 +23,10 @@ BOOL WINAPI fake_SetWindowPos(HWND hWnd, HWND hWndInsertAfter, int X, int Y, int BOOL WINAPI fake_MoveWindow(HWND hWnd, int X, int Y, int nWidth, int nHeight, BOOL bRepaint); LRESULT WINAPI fake_SendMessageA(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam); LONG WINAPI fake_SetWindowLongA(HWND hWnd, int nIndex, LONG dwNewLong); +BOOL WINAPI fake_EnableWindow(HWND hWnd, BOOL bEnable); +BOOL WINAPI fake_DestroyWindow(HWND hWnd); +HWND WINAPI fake_CreateWindowExA( + DWORD dwExStyle, LPCSTR lpClassName, LPCSTR lpWindowName, DWORD dwStyle, int X, int Y, + int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam); #endif diff --git a/src/hook.c b/src/hook.c index 5698f0e..3755bed 100644 --- a/src/hook.c +++ b/src/hook.c @@ -22,6 +22,10 @@ SETWINDOWPOSPROC real_SetWindowPos = SetWindowPos; MOVEWINDOWPROC real_MoveWindow = MoveWindow; SENDMESSAGEAPROC real_SendMessageA = SendMessageA; SETWINDOWLONGAPROC real_SetWindowLongA = SetWindowLongA; +ENABLEWINDOWPROC real_EnableWindow = EnableWindow; +CREATEWINDOWEXAPROC real_CreateWindowExA = CreateWindowExA; +DESTROYWINDOWPROC real_DestroyWindow = DestroyWindow; + void Hook_PatchIAT(HMODULE hMod, char *moduleName, char *functionName, PROC newFunction) { @@ -146,6 +150,9 @@ void Hook_TryHotPatch(char *moduleName, char *functionName, PROC newFunction, PR else { Hook_PatchIAT(GetModuleHandle(NULL), moduleName, functionName, newFunction); + + if (ddraw->bnetHack) + Hook_PatchIAT(GetModuleHandle("storm.dll"), moduleName, functionName, newFunction); } } @@ -172,6 +179,9 @@ void Hook_Init() Hook_TryHotPatch("user32.dll", "MoveWindow", (PROC)fake_MoveWindow, (PROC *)&real_MoveWindow); Hook_TryHotPatch("user32.dll", "SendMessageA", (PROC)fake_SendMessageA, (PROC *)&real_SendMessageA); Hook_TryHotPatch("user32.dll", "SetWindowLongA", (PROC)fake_SetWindowLongA, (PROC *)&real_SetWindowLongA); + Hook_TryHotPatch("user32.dll", "EnableWindow", (PROC)fake_EnableWindow, (PROC *)&real_EnableWindow); + Hook_TryHotPatch("user32.dll", "CreateWindowExA", (PROC)fake_CreateWindowExA, (PROC *)&real_CreateWindowExA); + Hook_TryHotPatch("user32.dll", "DestroyWindow", (PROC)fake_DestroyWindow, (PROC *)&real_DestroyWindow); //Hook_PatchIAT(GetModuleHandle(NULL), "user32.dll", "GetCursorPos", (PROC)fake_GetCursorPos); } diff --git a/src/main.c b/src/main.c index 611e120..78076d1 100644 --- a/src/main.c +++ b/src/main.c @@ -196,6 +196,28 @@ void LimitGameTicks() } } +void UpdateBnetPos(int oldX, int oldY, int newX, int newY) +{ + HWND hWnd = FindWindowEx(HWND_DESKTOP, NULL, "SDlgDialog", NULL); + + while (hWnd != NULL) + { + RECT rc; + real_GetWindowRect(hWnd, &rc); + + real_SetWindowPos( + hWnd, + 0, + rc.left + (newX - oldX), + rc.top + (newY - oldY), + 0, + 0, + SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE); + + hWnd = FindWindowEx(HWND_DESKTOP, hWnd, "SDlgDialog", NULL); + } +} + HRESULT __stdcall ddraw_Compact(IDirectDrawImpl *This) { printf("DirectDraw::Compact(This=%p) ???\n", This); @@ -776,7 +798,6 @@ HRESULT __stdcall ddraw_SetDisplayMode(IDirectDrawImpl *This, DWORD width, DWORD int y = (WindowRect.top != -32000) ? WindowRect.top : (This->mode.dmPelsHeight / 2) - (This->render.height / 2); RECT dst = { x, y, This->render.width + x, This->render.height + y }; AdjustWindowRect(&dst, GetWindowLong(This->hWnd, GWL_STYLE), FALSE); - real_SetWindowPos(ddraw->hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); real_MoveWindow(This->hWnd, dst.left, dst.top, (dst.right - dst.left), (dst.bottom - dst.top), TRUE); if (This->renderer == render_d3d9_main) @@ -833,6 +854,9 @@ HRESULT __stdcall ddraw_SetDisplayMode2(IDirectDrawImpl *This, DWORD width, DWOR void ToggleFullscreen() { + if (ddraw->bnetHack) + return; + if (ddraw->windowed) { mouse_unlock(); @@ -1083,6 +1107,9 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) if (CopyRect(&clientrc, windowrc) && UnadjustWindowRectEx(&clientrc, GetWindowLong(hWnd, GWL_STYLE), FALSE, GetWindowLong(hWnd, GWL_EXSTYLE))) { + if (ddraw->bnetHack && WindowRect.left != -32000) + UpdateBnetPos(WindowRect.left, WindowRect.top, clientrc.left, clientrc.top); + WindowRect.left = clientrc.left; WindowRect.top = clientrc.top; WindowRect.right = clientrc.right - clientrc.left; @@ -1129,11 +1156,17 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { if (ddraw->windowed) { + int x = (int)(short)LOWORD(lParam); + int y = (int)(short)HIWORD(lParam); + + if (x != -32000) + { + if (ddraw->bnetHack && WindowRect.left != -32000) + UpdateBnetPos(WindowRect.left, WindowRect.top, x, y); + } + if (inSizeMove || ddraw->wine) { - int x = (int)(short)LOWORD(lParam); - int y = (int)(short)HIWORD(lParam); - if (x != -32000) WindowRect.left = x; // -32000 = Exit/Minimize @@ -1182,38 +1215,6 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) mouse_unlock(); return 0; - - case WM_ENABLE: - { - if (ddraw->bnetHack) - { - if (wParam) - mouse_lock(); - else - mouse_unlock(); - - HWND hWnd = FindWindowEx(HWND_DESKTOP, NULL, "SDlgDialog", NULL); - if (hWnd) - { - BOOL hideCursor = TRUE; - - do - { - RECT rc; - if (real_GetWindowRect(hWnd, &rc) && rc.bottom - rc.top == 479) - hideCursor = FALSE; - - } while ((hWnd = FindWindowEx(HWND_DESKTOP, hWnd, "SDlgDialog", NULL))); - - if (hideCursor) - while (real_ShowCursor(FALSE) > 0); - } - - ddraw->bnetActive = !wParam; - } - break; - } - case WM_ACTIVATE: { return 0; diff --git a/src/mouse.c b/src/mouse.c index d9d19bb..b50e639 100644 --- a/src/mouse.c +++ b/src/mouse.c @@ -142,6 +142,9 @@ void mouse_lock() { RECT rc; + if (ddraw->bnetHack && ddraw->bnetActive) + return; + if (ddraw->devmode) { if (ddraw->handlemouse) @@ -277,7 +280,14 @@ BOOL WINAPI fake_GetWindowRect(HWND hWnd, LPRECT lpRect) } else { - return real_GetWindowRect(hWnd, lpRect) && MapWindowPoints(HWND_DESKTOP, ddraw->hWnd, (LPPOINT)lpRect, 2); + if (real_GetWindowRect(hWnd, lpRect)) + { + MapWindowPoints(HWND_DESKTOP, ddraw->hWnd, (LPPOINT)lpRect, 2); + + return TRUE; + } + + return FALSE; } } @@ -398,3 +408,62 @@ LONG WINAPI fake_SetWindowLongA(HWND hWnd, int nIndex, LONG dwNewLong) return real_SetWindowLongA(hWnd, nIndex, dwNewLong); } + +BOOL WINAPI fake_EnableWindow(HWND hWnd, BOOL bEnable) +{ + if (ddraw && ddraw->hWnd == hWnd) + { + if (ddraw->bnetHack) + return FALSE; + } + + return real_EnableWindow(hWnd, bEnable); +} + +BOOL WINAPI fake_DestroyWindow(HWND hWnd) +{ + BOOL result = real_DestroyWindow(hWnd); + + if (ddraw && ddraw->hWnd != hWnd && ddraw->bnetActive && ddraw->bnetHack && !FindWindowEx(HWND_DESKTOP, NULL, "SDlgDialog", NULL)) + { + ddraw->bnetActive = FALSE; + mouse_lock(); + } + + return result; +} + +HWND WINAPI fake_CreateWindowExA( + DWORD dwExStyle, LPCSTR lpClassName, LPCSTR lpWindowName, DWORD dwStyle, int X, int Y, + int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam) +{ + HWND hWnd = real_CreateWindowExA( + dwExStyle, + lpClassName, + lpWindowName, + dwStyle | WS_CLIPCHILDREN, + X, + Y, + nWidth, + nHeight, + hWndParent, + hMenu, + hInstance, + lpParam); + + if (_strcmpi(lpClassName, "SDlgDialog") == 0 && ddraw && ddraw->bnetHack) + { + if (!ddraw->bnetActive) + { + ddraw->bnetActive = TRUE; + mouse_unlock(); + } + + POINT pt = { 0, 0 }; + real_ClientToScreen(ddraw->hWnd, &pt); + + real_SetWindowPos(hWnd, 0, pt.x + X, pt.y + Y, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE); + } + + return hWnd; +} diff --git a/src/surface.c b/src/surface.c index b1cdc98..e0e5932 100644 --- a/src/surface.c +++ b/src/surface.c @@ -20,6 +20,7 @@ #include "main.h" #include "hook.h" #include "surface.h" +#include "mouse.h" #include "scale_pattern.h" // enables redraw via blt/unlock if there wasn't any flip for X ms @@ -1010,7 +1011,7 @@ HRESULT __stdcall ddraw_surface_Unlock(IDirectDrawSurfaceImpl *This, LPVOID lpRe do { RECT rc; - if (real_GetWindowRect(hWnd, &rc)) + if (fake_GetWindowRect(hWnd, &rc)) { if (rc.bottom - rc.top == 479) erase = TRUE;