aboutsummaryrefslogtreecommitdiff
path: root/src/internal/convarproxy.h
blob: f2e1b15c897e6e28af370ae4ba5fc507c3dfa1a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#ifndef CONVARPROXY_H
#define CONVARPROXY_H

#include "ns_plugin.h"
#include "proxy.h"
#include "types.h"

class ConVarProxy: public ClassProxy<ConVar> {
private:
    const char* pszName;
    const char* pszDefaultValue;
    int nFlags;
    const char* pszHelpString;
    bool bMin;
    float fMin;
    bool bMax;
    float fMax;
    FnChangeCallback_t pCallback;

public:
    ConVarProxy(
        const char* pszName,
        const char* pszDefaultValue,
        int nFlags,
        const char* pszHelpString,
        bool bMin,
        float fMin,
        bool bMax,
        float fMax,
        FnChangeCallback_t pCallback
    ) :
        pszName(pszName),
        pszDefaultValue(pszDefaultValue),
        nFlags(nFlags),
        pszHelpString(pszHelpString),
        bMin(bMin),
        fMin(fMin),
        bMax(bMax),
        fMax(fMax),
        pCallback(pCallback)
    {}

    virtual void initialize(void* data)
    {
        PLUGIN_DATA_TYPES* plugin_data = static_cast<PLUGIN_DATA_TYPES*>(data);

        PluginInitFuncs* funcs = plugin_data->funcs;
        PluginEngineData* engine_data = plugin_data->engine_data;

        assert(funcs->createObject);
        assert(engine_data->ConCommandConstructor);

        this->ptr = static_cast<ConVar*>(funcs->createObject(ObjectType::CONVAR));

        spdlog::info("Registering Convar {}", pszName);

        this->ptr->m_ConCommandBase.m_pConCommandBaseVTable = engine_data->ConVar_Vtable;
        this->ptr->m_ConCommandBase.s_pConCommandBases = (ConCommandBase*)engine_data->IConVar_Vtable;

        engine_data->conVarMalloc(&(this->ptr->m_pMalloc), 0, 0);
        engine_data->conVarRegister(this->ptr, const_cast<char*>(pszName), const_cast<char*>(pszDefaultValue), nFlags, const_cast<char*>(pszHelpString), bMin, fMin, bMax, fMax, (void*)pCallback);
    }

    const char* GetString() const {
        if (!this->ptr)
            return this->pszDefaultValue;

        return this->ptr->m_Value.m_pszString;
    }

    bool Getbool() const {
        return !!GetInt();
    }

    int GetInt() const {
        if (!this->ptr)
            return atoi(this->pszDefaultValue);

        return this->ptr->m_Value.m_nValue;
    }

    float GetFloat() const {
        if (!this->ptr)
            return atof(this->pszDefaultValue);

        return this->ptr->m_Value.m_fValue;
    }
};

#endif