|
| 1 | +/////////////////////////////////////////////////////////////////////////////// |
| 2 | +// GNU Lesser General Public License v3 (LGPL v3) |
| 3 | +// |
| 4 | +// Copyright (c) 2020 nyfrk <nyfrk@gmx.net> |
| 5 | +// |
| 6 | +// This file is part of S4ModApi. |
| 7 | +// |
| 8 | +// S4ModApi is free software: you can redistribute it and/or modify |
| 9 | +// it under the terms of the GNU General Public License as published by |
| 10 | +// the Free Software Foundation, either version 3 of the License, or |
| 11 | +// (at your option) any later version. |
| 12 | +// |
| 13 | +// S4ModApi is distributed in the hope that it will be useful, |
| 14 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | +// GNU General Public License for more details. |
| 17 | +// |
| 18 | +// You should have received a copy of the GNU General Public License |
| 19 | +// along with S4ModApi. If not, see <https://www.gnu.org/licenses/lgpl-3.0>. |
| 20 | +/////////////////////////////////////////////////////////////////////////////// |
| 21 | + |
| 22 | +#include "globals.h" // g_isGE, hProcess |
| 23 | +#include "hlib.h" // JmpPatch |
| 24 | +#include "patterns.h" |
| 25 | + |
| 26 | +#include "CBltHook.h" |
| 27 | + |
| 28 | +static hlib::CallPatch OnBltHook; |
| 29 | + |
| 30 | +BOOL __stdcall CBltHook::OnBlt(DWORD caller, DWORD ecx, DWORD edx, DWORD _0, DWORD _1, DWORD _2, DWORD _3, DWORD _4, DWORD _5, DWORD _6) { |
| 31 | + //TRACE; // we do not log this as it will be a mayor performance hit |
| 32 | + mutex.lock(); |
| 33 | + auto observers = GetInstance().observers; // obtain a copy since the callbacks may modify the vector |
| 34 | + mutex.unlock(); |
| 35 | + BOOL discard = false; |
| 36 | + for (auto& observer : observers) { |
| 37 | + discard |= ((LPS4BLTCALLBACK)observer.callback)(ecx, edx, _0, _1, _2, _3, _4, _5, _6, discard, caller); |
| 38 | + } |
| 39 | + return discard; |
| 40 | +} |
| 41 | + |
| 42 | +static void __declspec(naked) __onBlt() { |
| 43 | + __asm { |
| 44 | + push edx |
| 45 | + push ecx |
| 46 | + |
| 47 | + sub esp, 16 |
| 48 | + movdqu[esp], xmm0 |
| 49 | + sub esp, 16 |
| 50 | + movdqu[esp], xmm1 |
| 51 | + sub esp, 16 |
| 52 | + movdqu[esp], xmm2 |
| 53 | + sub esp, 16 |
| 54 | + movdqu[esp], xmm3 |
| 55 | + sub esp, 16 |
| 56 | + movdqu[esp], xmm4 |
| 57 | + sub esp, 16 |
| 58 | + movdqu[esp], xmm5 |
| 59 | + sub esp, 16 |
| 60 | + movdqu[esp], xmm6 |
| 61 | + sub esp, 16 |
| 62 | + movdqu[esp], xmm7 |
| 63 | + sub esp, 128 |
| 64 | + fsave [esp] |
| 65 | + |
| 66 | + push [esp + 296] |
| 67 | + push [esp + 296] |
| 68 | + push [esp + 296] |
| 69 | + push [esp + 296] |
| 70 | + push [esp + 296] |
| 71 | + push [esp + 296] |
| 72 | + push [esp + 296] |
| 73 | + push edx |
| 74 | + push ecx |
| 75 | + push [esp + 304] |
| 76 | + call CBltHook::OnBlt |
| 77 | + |
| 78 | + frstor [esp] |
| 79 | + add esp, 128 |
| 80 | + movdqu xmm7, [esp] |
| 81 | + add esp, 16 |
| 82 | + movdqu xmm6, [esp] |
| 83 | + add esp, 16 |
| 84 | + movdqu xmm5, [esp] |
| 85 | + add esp, 16 |
| 86 | + movdqu xmm4, [esp] |
| 87 | + add esp, 16 |
| 88 | + movdqu xmm3, [esp] |
| 89 | + add esp, 16 |
| 90 | + movdqu xmm2, [esp] |
| 91 | + add esp, 16 |
| 92 | + movdqu xmm1, [esp] |
| 93 | + add esp, 16 |
| 94 | + movdqu xmm0, [esp] |
| 95 | + add esp, 16 |
| 96 | + |
| 97 | + test eax, eax |
| 98 | + |
| 99 | + pop ecx |
| 100 | + pop edx |
| 101 | + pop eax |
| 102 | + jnz lbl_skip_original |
| 103 | + |
| 104 | + // not skip original blt function |
| 105 | + |
| 106 | + inc eax |
| 107 | + push ebp // repair replaced instructions |
| 108 | + mov ebp, esp |
| 109 | + push eax // must push here since the next replaced instruction will overwrite eax |
| 110 | + mov eax, [ebp+8] |
| 111 | + |
| 112 | + lbl_skip_original: |
| 113 | + ret |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +CBltHook& CBltHook::GetInstance() { |
| 118 | + static CBltHook inst; |
| 119 | + return inst; |
| 120 | +} |
| 121 | + |
| 122 | +bool CBltHook::Init() { |
| 123 | + TRACE; |
| 124 | + DWORD addr = g_Patterns.OnBltHook; |
| 125 | + if (!addr) return false; |
| 126 | + |
| 127 | + OnBltHook = hlib::CallPatch(addr, (DWORD)__onBlt); |
| 128 | + |
| 129 | + return true; |
| 130 | +} |
| 131 | + |
| 132 | +void CBltHook::Patch() { |
| 133 | + TRACE; |
| 134 | + OnBltHook.patch(); |
| 135 | +} |
| 136 | + |
| 137 | +void CBltHook::Unpatch() { |
| 138 | + TRACE; |
| 139 | + OnBltHook.unpatch(); |
| 140 | +} |
| 141 | + |
| 142 | +CBltHook::CBltHook() { TRACE; } |
0 commit comments