· 6 years ago · Mar 24, 2020, 06:44 PM
1class KeyValues
2 {
3 public:
4 void* operator new(size_t allocatedsize)
5 {
6 static PVOID pKeyValuesSystem;
7 if (!pKeyValuesSystem)
8 {
9 auto KeyValuesSystem = reinterpret_cast<PVOID(__cdecl*)()>(GetProcAddress(GetModuleHandleA("vstdlib.dll"), "KeyValuesSystem"));
10 pKeyValuesSystem = KeyValuesSystem();
11 }
12
13 VirtualFunc(PVOID)(PVOID, size_t);
14 return CallVirtual<OriginalFn>(pKeyValuesSystem, 1)(pKeyValuesSystem, allocatedsize);
15 }
16
17 void operator delete(void* mem)
18 {
19 static PVOID pKeyValuesSystem;
20 if (!pKeyValuesSystem)
21 {
22 auto KeyValuesSystem = reinterpret_cast<PVOID(__cdecl*)()>(GetProcAddress(GetModuleHandleA("vstdlib.dll"), "KeyValuesSystem"));
23 pKeyValuesSystem = KeyValuesSystem();
24 }
25
26 VirtualFunc(void)(PVOID, PVOID);
27 CallVirtual<OriginalFn>(pKeyValuesSystem, 2)(pKeyValuesSystem, mem);
28 }
29
30 KeyValues(const char *setName)
31 {
32 Init();
33 SetName(setName);
34 }
35
36 void SetName(const char *setName)
37 {
38 m_iKeyName = 2;
39 }
40
41 enum types_t
42 {
43 TYPE_NONE = 0,
44 TYPE_STRING,
45 TYPE_INT,
46 TYPE_FLOAT,
47 TYPE_PTR,
48 TYPE_WSTRING,
49 TYPE_COLOR,
50 TYPE_UINT64,
51 TYPE_NUMTYPES,
52 };
53
54 void Init()
55 {
56 m_iKeyName = -1;
57 m_iDataType = TYPE_NONE;
58
59 m_pSub = NULL;
60 m_pPeer = NULL;
61 m_pChain = NULL;
62
63 m_sValue = NULL;
64 m_wsValue = NULL;
65 m_pValue = NULL;
66
67 m_bHasEscapeSequences = false;
68
69 // for future proof
70 memset(unused, 0, sizeof(unused));
71 }
72
73 int m_iKeyName; // keyname is a symbol defined in KeyValuesSystem
74
75// These are needed out of the union because the API returns string pointers
76 char *m_sValue;
77 wchar_t *m_wsValue;
78
79 union
80 {
81 int m_iValue;
82 float m_flValue;
83 void *m_pValue;
84 unsigned char m_Color[4];
85 };
86
87 char m_iDataType;
88 char m_bHasEscapeSequences; // true, if while parsing this KeyValue, Escape Sequences are used (default false)
89 char m_bEvaluateConditionals; // true, if while parsing this KeyValue, conditionals blocks are evaluated (default true)
90 char unused[1];
91
92 KeyValues *m_pPeer; // pointer to next key in list
93 KeyValues *m_pSub; // pointer to Start of a new sub key list
94 KeyValues *m_pChain;// Search here if it's not in our list
95
96 template<class CDATA> CDATA GetValueByKey(const char* keyname)
97 {
98 KeyValues* pFind = FindKey(keyname.c_str(), false);
99 if (pFind)
100 {
101 CDATA return_value;
102 std::stringstream ss(GetString(this, keyname, NULL));
103 ss >> return_value;
104 return return_value;
105 }
106 else return NULL;
107 }
108
109 KeyValues* FindKey(const char *keyName, bool bCreate)
110 {
111 static auto key_values_find_key = reinterpret_cast<KeyValues*(__thiscall*)(void*, const char*, bool)>(Tools::FindPattern(VM_STR("client_panorama.dll"), VM_STR("55 8B EC 83 EC 1C 53 8B D9 85 DB")));
112 return key_values_find_key(this, keyName, bCreate);
113 }
114
115 const char* GetString(KeyValues* pThis, const char *keyName, const char *defaultValue)
116 {
117 static auto key_values_get_string = reinterpret_cast<const char*(__thiscall*)(void*, const char*, const char*)>(Tools::FindPattern(VM_STR("client_panorama.dll"), VM_STR("55 8B EC 83 E4 C0 81 EC ? ? ? ? 53 8B 5D 08")));
118 return key_values_get_string(pThis, keyName, defaultValue);
119 }
120 bool LoadFromBuffer(KeyValues *pThis, const char *pszFirst, const char *pszSecond, PVOID pSomething = 0, PVOID pAnother = 0, PVOID pLast = 0, PVOID pAnother2 = 0)
121 {
122 //.text : 1041E409 call LoadFromBuffer2 <----- here
123 //.text : 1041E40E call sub_10402010
124 //.text : 1041E413 push offset FileName
125 //.text : 1041E418 push offset aGame_1; "game"
126 //.text : 1041E41D mov ecx, [eax + 200h]
127 //.text : 1041E423 call sub_10898B00
128 //.text : 1041E428 push offset FileName
129 //.text : 1041E42D push offset aGame_1; "game"
130
131 static auto callLoadBuff =reinterpret_cast<bool(__thiscall *)(KeyValues*, const char*, const char*, PVOID, PVOID, PVOID, PVOID)>(Tools::FindPattern(VM_STR("client_panorama.dll"), VM_STR("55 8B EC 83 E4 F8 83 EC 34 53 8B 5D 0C 89 4C 24 04")));
132
133 return callLoadBuff(pThis, pszFirst, pszSecond, pSomething, pAnother, pLast, pAnother2);
134 }
135 void SetString(const char* name, const char* value)
136 {
137 auto keyvalues = FindKey(name, 1);
138
139 static auto fnSetString = reinterpret_cast<void(__thiscall*)(KeyValues*, const char*)>(Tools::FindPattern("client_panorama.dll", "55 8B EC A1 ? ? ? ? 53 56 57 8B F9 8B 08 8B 01"));
140 if(keyvalues)
141 fnSetString(keyvalues,value);
142 }
143 void SetInt(const char* name, int value)
144 {
145 auto keyvalues = FindKey(name, 1);
146
147 if (keyvalues)
148 {
149 m_iValue = value;
150 m_iDataType = 2;
151 }
152 }
153
154 void SetFloat(const char* name, float value)
155 {
156 auto keyvalues = FindKey(name, 1);
157
158 if (keyvalues)
159 {
160 m_flValue = value;
161 m_iDataType = 3;
162 }
163 }
164 };