d3dhook unload

Smeruxa

t.me/smeruxa
Автор темы
Проверенный
1,395
736
Пытаюсь отгрузить d3dhook, подскажите пожалуйста, в чем может быть проблема, или же как правильно сделать отгрузку

d3dhook.cpp:
#include "d3dhook.h"
#include "config/config.h"

#include "fonts_compressed/fasolid.h"
#include "fonts_compressed/roboto.h"
#include "fonts_compressed/tahoma.h"

#include <vector>

HWND game_hwnd = [] {
    if (const HWND* hwnd_ptr = *reinterpret_cast<HWND**>(0xC17054); hwnd_ptr != nullptr) {
        return *hwnd_ptr;
    }
    game_instance_init_hook.after += [](const auto& hook, const HWND& return_value, HINSTANCE inst) {
        game_hwnd = return_value;
    };
    return static_cast<HWND>(nullptr);
}();

std::optional<HRESULT> d3dhook::on_present(const decltype(present_hook)& hook, IDirect3DDevice9* device_ptr, const RECT*, const RECT*, HWND wnd, const RGNDATA*) {
    static bool ImGui_inited = false;
    if (!ImGui_inited) {
        ImGui::CreateContext();
        const ImGuiIO& io = ImGui::GetIO();
        (void)io;
        ImGui_ImplWin32_Init(game_hwnd);
        ImGui_ImplDX9_Init(device_ptr);
        ImGui::GetIO().ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange;
        ImGui::GetIO().IniFilename = NULL;

        ImFontGlyphRangesBuilder builder;
        builder.AddRanges(ImGui::GetIO().Fonts->GetGlyphRangesCyrillic());
        builder.AddText("‚„…†‡€‰‹‘’“”•–—™›№");

        ImVector<ImWchar> defaultGlyphRanges;
        builder.BuildRanges(&defaultGlyphRanges);

        ImGui::GetIO().Fonts->AddFontFromMemoryCompressedTTF(Tahoma_compressed_data, Tahoma_compressed_size, 14, nullptr, defaultGlyphRanges.Data);

        changeStyleSheet();

        const auto latest_wndproc_ptr = GetWindowLongPtrA(game_hwnd, GWLP_WNDPROC);
        wndproc_hook.set_dest(latest_wndproc_ptr);
        wndproc_hook.set_cb(&keyhook::on_wndproc);
        wndproc_hook.install();
        ImGui_inited = true;
    }

    ImGui_ImplDX9_NewFrame();
    ImGui_ImplWin32_NewFrame();
    ImGui::NewFrame();

    if (variables.isMenuOpened) {
        gImguiDraw->Draw();
    } if (variables.tempMenuOpened != variables.isMenuOpened) {
        variables.tempMenuOpened = variables.isMenuOpened;
        showCursor(variables.isMenuOpened);
    }

    ImGui::EndFrame();
    ImGui::Render();
    ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData());
    return std::nullopt;
}

std::optional<HRESULT> d3dhook::on_lost(const decltype(reset_hook)& hook, IDirect3DDevice9* device_ptr, D3DPRESENT_PARAMETERS* parameters) {
    ImGui_ImplDX9_InvalidateDeviceObjects();
    return std::nullopt;
}

void d3dhook::on_reset(const decltype(reset_hook)& hook, HRESULT& return_value, IDirect3DDevice9* device_ptr, D3DPRESENT_PARAMETERS* parameters) {}

void* get_function_address(const int VTableIndex) {
    return (*reinterpret_cast<void***>(keyhook::find_device(0x128000)))[VTableIndex];
}

void d3dhook::showCursor(bool state) {
    using RwD3D9GetCurrentD3DDevice_t = LPDIRECT3DDEVICE9(__cdecl*)();

    const auto rwCurrentD3dDevice{ reinterpret_cast<
                                          RwD3D9GetCurrentD3DDevice_t>(0x7F9D50U)() };

    if (nullptr == rwCurrentD3dDevice) {
        return;
    }

    static DWORD
        updateMouseProtection,
        rsMouseSetPosProtFirst,
        rsMouseSetPosProtSecond;

    if (state)
    {
        VirtualProtect(reinterpret_cast<void*>(0x53F3C6U), 5U,
            PAGE_EXECUTE_READWRITE, &updateMouseProtection);

        VirtualProtect(reinterpret_cast<void*>(0x53E9F1U), 5U,
            PAGE_EXECUTE_READWRITE, &rsMouseSetPosProtFirst);

        VirtualProtect(reinterpret_cast<void*>(0x748A1BU), 5U,
            PAGE_EXECUTE_READWRITE, &rsMouseSetPosProtSecond);

        // NOP: CPad::UpdateMouse
        *reinterpret_cast<uint8_t*>(0x53F3C6U) = 0xE9U;
        *reinterpret_cast<uint32_t*>(0x53F3C6U + 1U) = 0x15BU;

        // NOP: RsMouseSetPos
        memset(reinterpret_cast<void*>(0x53E9F1U), 0x90, 5U);
        memset(reinterpret_cast<void*>(0x748A1BU), 0x90, 5U);

        rwCurrentD3dDevice->ShowCursor(TRUE);
    }
    else
    {
        // Original: CPad::UpdateMouse
        memcpy(reinterpret_cast<void*>(0x53F3C6U), "\xE8\x95\x6C\x20\x00", 5U);

        // Original: RsMouseSetPos
        memcpy(reinterpret_cast<void*>(0x53E9F1U), "\xE8\xAA\xAA\x0D\x00", 5U);
        memcpy(reinterpret_cast<void*>(0x748A1BU), "\xE8\x80\x0A\xED\xFF", 5U);

        using CPad_ClearMouseHistory_t = void(__cdecl*)();
        using CPad_UpdatePads_t = void(__cdecl*)();

        reinterpret_cast<CPad_ClearMouseHistory_t>(0x541BD0U)();
        reinterpret_cast<CPad_UpdatePads_t>(0x541DD0U)();

        VirtualProtect(reinterpret_cast<void*>(0x53F3C6U), 5U,
            updateMouseProtection, &updateMouseProtection);

        VirtualProtect(reinterpret_cast<void*>(0x53E9F1U), 5U,
            rsMouseSetPosProtFirst, &rsMouseSetPosProtFirst);

        VirtualProtect(reinterpret_cast<void*>(0x748A1BU), 5U,
            rsMouseSetPosProtSecond, &rsMouseSetPosProtSecond);

        rwCurrentD3dDevice->ShowCursor(FALSE);
    }
}

d3dhook.h:
#ifndef D3D9HOOK_FILE_H
#define D3D9HOOK_FILE_H

#include <kthook/kthook.hpp>
#include <d3d9.h>
#include <windows.h>

#include <imgui.h>
#include <imgui_impl_win32.h>
#include <imgui_impl_dx9.h>

#include "main.h"
#include "keyhook.h"
#include "imgui/imguisdraw.h"
#include "imgui/FaAwesome/IconsFontAwesome6.h"

void* get_function_address(int VTableIndex);

using PresentSignature = HRESULT(__stdcall*)(IDirect3DDevice9*, const RECT*, const RECT*, HWND, const RGNDATA*);
using ResetSignature = HRESULT(__stdcall*)(IDirect3DDevice9*, D3DPRESENT_PARAMETERS*);
using InitGameInstance = HWND(__cdecl*)(HINSTANCE);
inline kthook::kthook_signal<PresentSignature> present_hook{ get_function_address(17) };
inline kthook::kthook_signal<ResetSignature> reset_hook{ get_function_address(16) };
inline kthook::kthook_signal<InitGameInstance> game_instance_init_hook{ 0x745560 };

namespace d3dhook {
    std::optional<HRESULT> on_present(const decltype(present_hook)& hook, IDirect3DDevice9* device_ptr, const RECT*, const RECT*, HWND, const RGNDATA*);
    std::optional<HRESULT> on_lost(const decltype(reset_hook)& hook, IDirect3DDevice9* device_ptr, D3DPRESENT_PARAMETERS* parameters);
    void on_reset(const decltype(reset_hook)& hook, HRESULT& return_value, IDirect3DDevice9* device_ptr, D3DPRESENT_PARAMETERS* parameters);
    void showCursor(bool state);
    void changeStyleSheet();
};

#endif

keyhook.cpp:
#include "keyhook.h"
#include "main.h"
#include <imgui.h>

extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

HRESULT __stdcall keyhook::on_wndproc(const decltype(wndproc_hook) &hook, HWND hwnd, const UINT uMsg,
                                      const WPARAM wParam, const LPARAM lParam) {
    ImGui_ImplWin32_WndProcHandler(hwnd, uMsg, wParam, lParam);

    if (uMsg == WM_KEYUP && wParam == VK_F5)
        variables.isMenuOpened ^= true;

    if (const auto &io = ImGui::GetIO(); io.WantCaptureKeyboard || io.WantCaptureMouse)
        return 1;
    return hook.get_trampoline()(hwnd, uMsg, wParam, lParam);
}

std::uintptr_t keyhook::find_device(const std::uint32_t LenId) {
    static std::uintptr_t base = [](const std::size_t Len) {
        std::string path_to(MAX_PATH, '\0');
        if (const auto size = GetSystemDirectoryA(path_to.data(), MAX_PATH)) {
            path_to.resize(size);
            path_to += "\\d3d9.dll";
            auto dwObjBase = reinterpret_cast<std::uintptr_t>(LoadLibraryA(path_to.c_str()));
            while (dwObjBase++ < dwObjBase + Len) {
                if (*reinterpret_cast<std::uint16_t *>(dwObjBase + 0x00) == 0x06C7 &&
                    *reinterpret_cast<std::uint16_t *>(dwObjBase + 0x06) == 0x8689 &&
                    *reinterpret_cast<std::uint16_t *>(dwObjBase + 0x0C) == 0x8689) {
                    dwObjBase += 2;
                    break;
                }
            }
            return dwObjBase;
        }
        return static_cast<std::uintptr_t>(0);
    }(LenId);
    return base;
}

keyhook.h:
#ifndef KEYHOOK_FILE_H
#define KEYHOOK_FILE_H

#include <Windows.h>
#include <kthook/kthook.hpp>

inline kthook::kthook_simple<WNDPROC> wndproc_hook{};

namespace keyhook {
    HRESULT __stdcall on_wndproc(const decltype(wndproc_hook) &hook, HWND hwnd, const UINT uMsg, const WPARAM wParam,
                                 const LPARAM lParam);
    std::uintptr_t find_device(std::uint32_t LenId);
};

#endif

lpPlugin.cpp:
lpPlugin::~lpPlugin() {
    wndproc_hook.reset();
    present_hook.reset();
    reset_hook.reset();
    game_instance_init_hook.reset();

    delete gConfig;
    delete gRakhook;
    delete gImguiDraw;

    hookCTimerUpdate.reset();
}

Ну и детач плагина
C++:
case DLL_PROCESS_DETACH:
    if (plugin)
        plugin.reset();

    ImGui_ImplDX9_Shutdown();
    ImGui_ImplWin32_Shutdown();
    ImGui::DestroyContext();
    break;
}

Спасибо всем, кто уделит внимание
 

whyega52

Eblang головного мозга
Модератор
2,818
2,709
  • Влюблен
Реакции: Smeruxa