Исходник Пример использования паттерна Singleton

Эксперт
Статус
Оффлайн
Регистрация
12 Июн 2014
Сообщения
999
Реакции[?]
1,209
Поинты[?]
3K
простой пример, который пришлось вынужденно использовать:
Код:
class IPanel
{
public:
    const char *GetName(unsigned int vguiPanel) {
        typedef const char *(__thiscall* FunFn)(void*, unsigned int);
        return vCall<FunFn>(this, 36)(this, vguiPanel);
    }
    static IPanel* Singleton() {

        static IPanel *inst = nullptr;
        if (!inst)
            inst = get_interface<IPanel>(get_module_factory(GetModuleHandleW(L"vgui2.dll")), "VGUI_Panel009");
        return inst;
        //return (IPanel*)0x0FD108C4; // = get interface "VGUI_Panel009"
    }
};
class ISurface
{
public:
    void  SetColor(int r, int g, int b, int a){
        typedef void(__thiscall* OriginalFn)(void*, int, int, int, int);
        return vCall<OriginalFn>(this, 15)(this, r, g, b, a);
    }
    void   FilledRect(int x0, int y0, int x1, int y1){
        typedef void(__thiscall* OriginalFn)(void*, int, int, int, int);
        return vCall<OriginalFn>(this, 16)(this, x0, y0, x1, y1);
    }


    static ISurface* Singleton() {
        static ISurface *inst = nullptr;
        if (!inst)
            inst = get_interface<ISurface>(get_module_factory(GetModuleHandleW(L"vguimatsurface.dll")), "VGUI_Surface031");
        return inst;
    }
    
};
Пример:

Код:
void __stdcall myPaintTraverse(unsigned int panel, bool forceRepaint, bool allowForce)
{
    oPaintTraverse(IPanel::Singleton(), panel, forceRepaint, allowForce);

    static unsigned int panelId = 0;
    if (!panelId) {
        const auto panelName = IPanel::Singleton()->GetName(panel);
        if (!strcmp(panelName, "FocusOverlayPanel")) {
            panelId = panel;
        }
    }
    else if (panelId == panel) {
        //todo
        ISurface::Singleton()->SetColor(255, 0, 0, 255);
        ISurface::Singleton()->FilledRect(10, 10, 150, 150);
    }
}
 
Сверху Снизу