Показать сообщение отдельно

  #15  
Старый 19.06.2025, 20:34
nonelike
Познающий
Регистрация: 22.12.2018
Сообщений: 72
С нами: 3890632

Репутация: 63
По умолчанию

Цитата:
Сообщение от dmitry.karle  

render.hpp:





Код:
#pragma once
#ifndef RENDER_HPP
#define RENDER_HPP

#include 
#include 
#include 
#include 
#include 
#include 
#include 

#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9.lib")

class IDirect3DDevice9;

namespace render {
    class EventSystem {
    public:
        using Callback = std::function;
      
        void subscribe(Callback&& cb) {
            std::lock_guard lock(mutex_);
            callbacks_.emplace_back(std::move(cb));
        }

        void notify() {
            std::vector local_copy;
            {
                std::lock_guard lock(mutex_);
                local_copy = callbacks_;
            }
          
            for (auto& cb : local_copy) {
                if (cb) cb();
            }
        }

    private:
        std::mutex mutex_;
        std::vector callbacks_;
    };

    class DeviceManager {
    public:
        static DeviceManager& instance() {
            static DeviceManager inst;
            return inst;
        }

        bool initialize(IDirect3DDevice9* device);
        void shutdown();
      
        IDirect3DDevice9* device() const {
            std::lock_guard lock(device_mutex_);
            return device_;
        }

        bool is_initialized() const {
            return initialized_.load(std::memory_order_acquire);
        }

    private:
        DeviceManager() = default;
        ~DeviceManager() = default;

        mutable std::mutex device_mutex_;
        IDirect3DDevice9* device_{nullptr};
        std::atomic initialized_{false};
    };

    extern EventSystem on_present;
}

#endif


render.cpp:





Код:
#include "render.hpp"
#include 

namespace render {
    namespace {
        class D3DHooks {
        public:
            D3DHooks() {
                present_hook_.set_callback([](auto&& hook, auto&&... args) {
                    auto& dm = DeviceManager::instance();
                    if (!dm.is_initialized()) {
                        dm.initialize(std::get(std::forward_as_tuple(args...)));
                    }
                    on_present.notify();
                    return hook.call_trampoline(std::forward(args)...);
                });

                reset_hook_.set_callback([](auto&& hook, auto&&... args) {
                    DeviceManager::instance().shutdown();
                    return hook.call_trampoline(std::forward(args)...);
                });

                if (auto d3d9 = GetModuleHandle("d3d9.dll")) {
                    present_hook_.install(GetProcAddress(d3d9, "Direct3DCreate9"));
                    reset_hook_.install(GetProcAddress(d3d9, "Direct3DCreate9"));
                }
            }

            ~D3DHooks() = default;

        private:
            kthook::kthook_signal present_hook_;
            kthook::kthook_signal reset_hook_;
        };

        std::unique_ptr hooks;
    }

    EventSystem on_present;

    bool DeviceManager::initialize(IDirect3DDevice9* device) {
        std::lock_guard lock(device_mutex_);
        if (initialized_) return true;

        device_ = device;
        device_->AddRef();
        initialized_.store(true, std::memory_order_release);
      
        if (!hooks) {
            hooks = std::make_unique();
        }

        return true;
    }

    void DeviceManager::shutdown() {
        std::lock_guard lock(device_mutex_);
        if (device_) {
            device_->Release();
            device_ = nullptr;
        }
        initialized_.store(false, std::memory_order_release);
    }
}


overlay.hpp:





Код:
#pragma once
#ifndef OVERLAY_HPP
#define OVERLAY_HPP

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "render.hpp"

class Overlay : public CefClient, public CefRenderHandler, public CefLifeSpanHandler {
public:
    Overlay();
    ~Overlay() override;

    CefRefPtr GetRenderHandler() override { return this; }
    CefRefPtr GetLifeSpanHandler() override { return this; }

    void OnAfterCreated(CefRefPtr browser) override;

    void OnPaint(CefRefPtr browser, PaintElementType type, const RectList& dirty_rects, const void* buffer, int width, int height) override;
    void GetViewRect(CefRefPtr browser, CefRect& rect) override;

    CefRefPtr browser() const { return browser_; }

private:
    CefRefPtr browser_;
  
    IMPLEMENT_REFCOUNTING(Overlay);
    DISALLOW_COPY_AND_ASSIGN(Overlay);
};

class OverlaySystem {
public:
    static OverlaySystem& instance();

    bool initialize();
    void shutdown();
  
    bool create_browser(const std::string& url);
    bool process_message(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);

    bool is_initialized() const { return initialized_.load(); }
    bool is_browser_created() const { return browser_created_.load(); }

private:
    OverlaySystem() = default;
    ~OverlaySystem() = default;

    std::mutex mutex_;
    std::unique_ptr overlay_;
    IDirect3DTexture9* texture_{nullptr};
    std::atomic texture_width_{0};
    std::atomic texture_height_{0};
    std::atomic needs_update_{false};
    std::atomic initialized_{false};
    std::atomic browser_created_{false};
};

#endif


overlay.cpp:





Код:
#include "overlay.hpp"
#include 
#include 
#include 

namespace {
    int get_cef_modifiers() {
        std::array modifiers = {
            std::make_pair(VK_SHIFT, EVENTFLAG_SHIFT_DOWN),
            std::make_pair(VK_CONTROL, EVENTFLAG_CONTROL_DOWN),
            std::make_pair(VK_MENU, EVENTFLAG_ALT_DOWN),
            std::make_pair(VK_CAPITAL, EVENTFLAG_CAPS_LOCK_ON)
        };

        int result = 0;
        for (const auto& [key, flag] : modifiers) {
            if (GetKeyState(key) & 0x8000) {
                result |= flag;
            }
        }
        return result;
    }
}

Overlay::Overlay() = default;
Overlay::~Overlay() {
    if (browser_) {
        browser_->GetHost()->CloseBrowser(true);
    }
}

void Overlay::OnAfterCreated(CefRefPtr browser) {
    browser_ = browser;
    browser_->GetHost()->SetWindowlessFrameRate(60);
    OverlaySystem::instance().is_browser_created().store(true);
}

void Overlay::OnPaint(CefRefPtr browser, PaintElementType type, const RectList& dirty_rects, const void* buffer, int width, int height) {
    if (width  lock(system.mutex_);

    if (!system.texture_ || width != system.texture_width_ || height != system.texture_height_) {
        if (system.texture_) {
            system.texture_->Release();
            system.texture_ = nullptr;
        }

        if (FAILED(D3DXCreateTexture(device, width, height, 1, D3DUSAGE_DYNAMIC, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &system.texture_))) {
            return;
        }

        system.texture_width_ = width;
        system.texture_height_ = height;
    }

    D3DLOCKED_RECT locked_rect;
    if (SUCCEEDED(system.texture_->LockRect(0, &locked_rect, nullptr, D3DLOCK_DISCARD))) {
        const auto* src = static_cast(buffer);
        auto* dst = static_cast(locked_rect.pBits);

        if (locked_rect.Pitch == width * 4) {memcpy(dst, src, width * height * 4);
        } else {
            for (int y = 0; y UnlockRect(0);
        system.needs_update_ = true;
    }
}

void Overlay::GetViewRect(CefRefPtr browser, CefRect& rect) {
    rect = CefRect(0, 0, 840, 660);
}

OverlaySystem& OverlaySystem::instance() {
    static OverlaySystem inst;
    return inst;
}

bool OverlaySystem::initialize() {
    if (initialized_) return true;

    CefMainArgs main_args(GetModuleHandle(nullptr));
    CefSettings settings;

    settings.windowless_rendering_enabled = true;
    settings.no_sandbox = true;
    settings.multi_threaded_message_loop = true;
    settings.background_color = 0x00000000;
    settings.log_severity = LOGSEVERITY_DISABLE;

    if (!CefInitialize(main_args, settings, nullptr, nullptr)) {
        return false;
    }

    overlay_ = std::make_unique();
    initialized_.store(true);
    return true;
}

void OverlaySystem::shutdown() {
    std::lock_guard lock(mutex_);
  
    if (texture_) {
        texture_->Release();
        texture_ = nullptr;
    }

    if (overlay_) {
        overlay_.reset();
    }

    if (initialized_) {
        CefShutdown();
        initialized_.store(false);
    }
}

bool OverlaySystem::create_browser(const std::string& url) {
    if (!initialized_ || browser_created_) return false;

    CefWindowInfo window_info;
    CefBrowserSettings browser_settings;

    browser_settings.windowless_frame_rate = 60;
    browser_settings.background_color = 0x00000000;
    browser_settings.webgl = STATE_DISABLED;

    window_info.SetAsWindowless(nullptr);
    window_info.shared_texture_enabled = false;
    window_info.external_begin_frame_enabled = false;

    return CefBrowserHost::CreateBrowser(window_info, overlay_.get(), url, browser_settings, nullptr, nullptr);
}

bool OverlaySystem::process_message(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
    if (!browser_created_ || !overlay_ || !overlay_->browser()) {
        return false;
    }

    auto host = overlay_->browser()->GetHost();
    if (!host) return false;

    if (msg >= WM_MOUSEFIRST && msg SendMouseMoveEvent(mouse_event, false);
                return true;
            case WM_LBUTTONDOWN:
                host->SendMouseClickEvent(mouse_event, MBT_LEFT, false, 1);
                return true;
            case WM_LBUTTONUP:
                host->SendMouseClickEvent(mouse_event, MBT_LEFT, true, 1);
                return true;
            case WM_RBUTTONDOWN:
                host->SendMouseClickEvent(mouse_event, MBT_RIGHT, false, 1);
                return true;
            case WM_RBUTTONUP:
                host->SendMouseClickEvent(mouse_event, MBT_RIGHT, true, 1);
                return true;
            case WM_MOUSEWHEEL:
                host->SendMouseWheelEvent(mouse_event, 0, GET_WHEEL_DELTA_WPARAM(wparam));
                return true;
        }
    }

    if (msg >= WM_KEYFIRST && msg (wparam);
        event.native_key_code = static_cast(lparam);
        event.modifiers = get_cef_modifiers();

        if (msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN) {
            event.type = KEYEVENT_RAWKEYDOWN;
        } else if (msg == WM_KEYUP || msg == WM_SYSKEYUP) {
            event.type = KEYEVENT_KEYUP;
        } else if (msg == WM_CHAR) {
            event.type = KEYEVENT_CHAR;
        }

        host->SendKeyEvent(event);
        return true;
    }

    return false;
}


plugin.hpp:





Код:
#pragma once
#ifndef PLUGIN_HPP
#define PLUGIN_HPP

#include 
#include 
#include 
#include "overlay.hpp"

class Plugin {
public:Plugin(); ~Plugin();

private:
    void update_hooked(const kthook::kthook_simple& hook);
    HRESULT wnd_proc_hooked(const kthook::kthook_simple& hook, HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);

    kthook::kthook_simple update_hook_;
    kthook::kthook_simple wnd_proc_hook_;
    bool initialized_{false};
};

#endif


plugin.cpp:





Код:
#include "plugin.hpp"
#include "render.hpp"

Plugin::Plugin() :
    update_hook_(reinterpret_cast(0x561B10)),
    wnd_proc_hook_(reinterpret_cast(0x747EB0)) {
  
    update_hook_.set_callback([this](auto&& hook) { update_hooked(hook); });
    wnd_proc_hook_.set_callback([this](auto&& hook, auto&&... args) {
        return wnd_proc_hooked(hook, std::forward(args)...);
    });
}

Plugin::~Plugin() {
    rakhook::destroy();
    OverlaySystem::instance().shutdown();
    render::DeviceManager::instance().shutdown();
}

void Plugin::update_hooked(const kthook::kthook_simple& hook) {
    if (!initialized_ && c_chat().reference() && rakhook::initialize()) {
        initialized_ = true;
      
        if (OverlaySystem::instance().initialize()) {
            OverlaySystem::instance().create_browser("data:text/html,Loading...");
          
            render::on_present.subscribe([]() {
                auto& overlay = OverlaySystem::instance();
                auto* device = render::DeviceManager::instance().device();
              
                if (!overlay.is_browser_created() || !device || !overlay.texture_) {
                    return;
                }

                IDirect3DStateBlock9* state_block = nullptr;
                if (FAILED(device->CreateStateBlock(D3DSBT_ALL, &state_block))) {
                    return;
                }

                device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
                device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
                device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
                device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
                device->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);
                device->SetRenderState(D3DRS_ALPHAREF, 0x08);
                device->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL);
                device->SetRenderState(D3DRS_LIGHTING, FALSE);
                device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
                device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);

                device->SetTexture(0, overlay.texture_);
                device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
                device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
                device->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
                device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
                device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
                device->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
                device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
                device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);

                struct Vertex {
                    FLOAT x, y, z, rhw;
                    DWORD color;
                    FLOAT tu, tv;
                };

                const std::array vertices = {{
                    { 0.0f,   0.0f,   0.0f, 1.0f, 0xFFFFFFFF, 0.0f, 0.0f },
                    { 840.0f, 0.0f,   0.0f, 1.0f, 0xFFFFFFFF, 1.0f, 0.0f },
                    { 0.0f,   660.0f, 0.0f, 1.0f, 0xFFFFFFFF, 0.0f, 1.0f },
                    { 840.0f, 660.0f, 0.0f, 1.0f, 0xFFFFFFFF, 1.0f, 1.0f }
                }};

                device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1);
                device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, vertices.data(), sizeof(Vertex));

                if (state_block) {
                    state_block->Apply();
                    state_block->Release();
                }
            });
        }
    }
    hook.call_trampoline();
}

HRESULT Plugin::wnd_proc_hooked(const kthook::kthook_simple& hook, HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
    if (OverlaySystem::instance().process_message(hwnd, msg, wparam, lparam)) {
        return TRUE;
    }
    return hook.call_trampoline(hwnd, msg, wparam, lparam);
}


в прошлом там реально косяк с пастой (проверок нет, косяк в обработке буфера), проверь этот вариант или вернись к прошлой версии которая работает и там надо искать проблему с рендером
крч засыпало меня ошибками на этапе компиляции, у тебя код походу под старый ктхук, я не шарю какие там были старые методы, чтоб актуализировать и еще всякий кайф с member is inaccessible.

эрроры:





Код:
member
"OverlaySystem::texture_"
(
declared at line
55
of
"D:\Bounce Project\client\cefBounce\overlay\source\overlay.hpp"
)
is inaccessible
identifier
"c_plugin"
is undefined
expression must have
class
type
but it has type
"bool"
member
"OverlaySystem::mutex_"
(
declared at line
53
of
"D:\Bounce Project\client\cefBounce\overlay\source\overlay.hpp"
)
is inaccessible
member
"OverlaySystem::texture_"
(
declared at line
55
of
"D:\Bounce Project\client\cefBounce\overlay\source\overlay.hpp"
)
is inaccessible
member
"OverlaySystem::texture_width_"
(
declared at line
56
of
"D:\Bounce Project\client\cefBounce\overlay\source\overlay.hpp"
)
is inaccessible
member
"OverlaySystem::texture_height_"
(
declared at line
57
of
"D:\Bounce Project\client\cefBounce\overlay\source\overlay.hpp"
)
is inaccessible
member
"OverlaySystem::texture_"
(
declared at line
55
of
"D:\Bounce Project\client\cefBounce\overlay\source\overlay.hpp"
)
is inaccessible
member
"OverlaySystem::texture_"
(
declared at line
55
of
"D:\Bounce Project\client\cefBounce\overlay\source\overlay.hpp"
)
is inaccessible
member
"OverlaySystem::texture_"
(
declared at line
55
of
"D:\Bounce Project\client\cefBounce\overlay\source\overlay.hpp"
)
is inaccessible
member
"OverlaySystem::texture_"
(
declared at line
55
of
"D:\Bounce Project\client\cefBounce\overlay\source\overlay.hpp"
)
is inaccessible
member
"OverlaySystem::texture_width_"
(
declared at line
56
of
"D:\Bounce Project\client\cefBounce\overlay\source\overlay.hpp"
)
is inaccessible
member
"OverlaySystem::texture_height_"
(
declared at line
57
of
"D:\Bounce Project\client\cefBounce\overlay\source\overlay.hpp"
)
is inaccessible
member
"OverlaySystem::texture_"
(
declared at line
55
of
"D:\Bounce Project\client\cefBounce\overlay\source\overlay.hpp"
)
is inaccessible
member
"OverlaySystem::texture_"
(
declared at line
55
of
"D:\Bounce Project\client\cefBounce\overlay\source\overlay.hpp"
)
is inaccessible
member
"OverlaySystem::needs_update_"
(
declared at line
58
of
"D:\Bounce Project\client\cefBounce\overlay\source\overlay.hpp"
)
is inaccessible
class
"kthook::kthook_simple"
has no member
"set_callback"
class
"kthook::kthook_simple"
has no member
"set_callback"
identifier
"c_chat"
is undefined
member
"OverlaySystem::texture_"
(
declared at line
55
of
"D:\Bounce Project\client\cefBounce\overlay\source\overlay.hpp"
)
is inaccessible
class
"kthook::kthook_signal"
has no member
"set_callback"
class
"kthook::kthook_signal"
has no member
"set_callback"
too many arguments in function call
too many arguments in function call
 
Ответить с цитированием