-
Notifications
You must be signed in to change notification settings - Fork 0
/
d2vdump.cpp
337 lines (278 loc) · 9.57 KB
/
d2vdump.cpp
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/**
* =============================================================================
* D2VDump
* Copyright (C) 2016 Nicholas Hastings
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 2.0 or later, as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, you are also granted permission to link the code
* of this program (as well as its derivative works) to "Dota 2," the
* "Source Engine, and any Game MODs that run on software by the Valve Corporation.
* You must obey the GNU General Public License in all respects for all other
* code used. Additionally, this exception is granted to all derivative works.
*/
// Self
#include "d2vdump.h"
// SDK
#include <filesystem.h>
#include <icvar.h>
#include <tier0/platform.h>
#include <tier1/fmtstr.h>
#include <tier1/iconvar.h>
static D2VDump g_D2VDump;
IFileSystem *filesystem;
static IScriptManager *scriptmgr;
SH_DECL_HOOK1(IScriptManager, CreateVM, SH_NOATTRIB, 0, IScriptVM *, ScriptLanguage_t);
SH_DECL_HOOK1_void(IScriptManager, DestroyVM, SH_NOATTRIB, 0, IScriptVM *);
SH_DECL_HOOK1_void(IScriptVM, RegisterFunction, SH_NOATTRIB, 0, ScriptFunctionBinding_t *);
SH_DECL_HOOK1(IScriptVM, RegisterScriptClass, SH_NOATTRIB, 0, bool, ScriptClassDesc_t *);
SH_DECL_HOOK2(IScriptVM, RegisterInstance, SH_NOATTRIB, 0, HSCRIPT, ScriptClassDesc_t *, void *);
SH_DECL_HOOK3(IScriptVM, SetValue, SH_NOATTRIB, 0, bool, HSCRIPT, const char *, const char *);
SH_DECL_HOOK3(IScriptVM, SetValue, SH_NOATTRIB, 1, bool, HSCRIPT, const char *, const ScriptVariant_t &);
SH_DECL_HOOK5(IScriptVM, SetEnumValue, SH_NOATTRIB, 0, bool, HSCRIPT, const char *, const char *, int, const char *);
static class BaseAccessor : public IConCommandBaseAccessor
{
public:
bool RegisterConCommandBase(ConCommandBase *pVar)
{
return META_REGCVAR(pVar);
}
} s_BaseAccessor;
PLUGIN_EXPOSE(D2VDump, g_D2VDump);
bool D2VDump::Load(PluginId id, ISmmAPI *ismm, char *error, size_t maxlen, bool late)
{
PLUGIN_SAVEVARS();
if (!InitGlobals(error, maxlen))
{
ismm->Format(error, maxlen, "Failed to setup globals");
return false;
}
memset(m_VMs, 0, sizeof(m_VMs));
m_Dumpers.push_back(new JSONScriptDumper());
if (!filesystem->IsDirectory("vdump", "DEFAULT_WRITE_PATH"))
filesystem->CreateDirHierarchy("vdump", "DEFAULT_WRITE_PATH");
InitHooks();
return true;
}
bool D2VDump::InitGlobals(char *error, size_t maxlen)
{
ISmmAPI *ismm = g_SMAPI;
GET_V_IFACE_CURRENT(GetFileSystemFactory, filesystem, IFileSystem, FILESYSTEM_INTERFACE_VERSION);
GET_V_IFACE_CURRENT(GetEngineFactory, scriptmgr, IScriptManager, VSCRIPT_INTERFACE_VERSION);
ICvar *icvar;
GET_V_IFACE_CURRENT(GetEngineFactory, icvar, ICvar, CVAR_INTERFACE_VERSION);
g_pCVar = icvar;
ConVar_Register(0, &s_BaseAccessor);
return true;
}
void D2VDump::InitHooks()
{
SH_ADD_HOOK(IScriptManager, CreateVM, scriptmgr, SH_MEMBER(this, &D2VDump::Hook_CreateVM), false);
SH_ADD_HOOK(IScriptManager, DestroyVM, scriptmgr, SH_MEMBER(this, &D2VDump::Hook_DestroyVM), false);
}
void D2VDump::ShutdownHooks()
{
SH_REMOVE_HOOK(IScriptManager, DestroyVM, scriptmgr, SH_MEMBER(this, &D2VDump::Hook_DestroyVM), false);
SH_REMOVE_HOOK(IScriptManager, CreateVM, scriptmgr, SH_MEMBER(this, &D2VDump::Hook_CreateVM), false);
}
bool D2VDump::Unload(char *error, size_t maxlen)
{
ShutdownHooks();
for (size_t i = 0; i < VM_Count; ++i)
{
for (auto d : m_Dumpers)
{
FileHandle_t f;
f = filesystem->Open(CFmtStr("vdump/out%u.%s", i, d->GetOutputTypeName()), "w", "DEFAULT_WRITE_PATH");
d->SaveFunctionsToDisk(f, VMType(i));
filesystem->Close(f);
f = filesystem->Open(CFmtStr("vdump/values%u.%s", i, d->GetOutputTypeName()), "w", "DEFAULT_WRITE_PATH");
d->SaveValuesToDisk(f, VMType(i));
filesystem->Close(f);
}
}
for (auto d : m_Dumpers)
delete d;
m_Dumpers.clear();
return true;
}
void D2VDump::Hook_RegisterFunction(ScriptFunctionBinding_t *pScriptFunction)
{
VMType v = VMToVMType(META_IFACEPTR(IScriptVM));
if (v != VM_Unknown)
{
for (auto d : m_Dumpers)
{
d->AddFunction(pScriptFunction->m_desc, v);
}
}
RETURN_META(MRES_IGNORED);
}
bool D2VDump::Hook_RegisterScriptClass(ScriptClassDesc_t *pClassDesc)
{
VMType v = VMToVMType(META_IFACEPTR(IScriptVM));
if (v != VM_Unknown)
{
for (auto d : m_Dumpers)
{
d->AddClass(*pClassDesc, v);
}
}
RETURN_META_VALUE(MRES_IGNORED, true);
}
HSCRIPT D2VDump::Hook_RegisterInstance(ScriptClassDesc_t *pDesc, void *pInstance)
{
VMType v = VMToVMType(META_IFACEPTR(IScriptVM));
if (v != VM_Unknown)
{
for (auto d : m_Dumpers)
{
d->AddClass(*pDesc, v);
}
}
RETURN_META_VALUE(MRES_IGNORED, INVALID_HSCRIPT);
}
IScriptVM *D2VDump::Hook_CreateVM(ScriptLanguage_t language)
{
IScriptVM *pVM = SH_CALL(scriptmgr, &IScriptManager::CreateVM)(language);
VMType vmType = VM_Unknown;
// Main VM is always (re)created first, at map start. Bot VM is created after lobby data is received, if lobby uses lua bots.
if (!m_VMs[VM_Main])
{
vmType = VM_Main;
}
else if (!m_VMs[VM_Bot])
{
vmType = VM_Bot;
}
else
{
DevMsg("D2V: Got new unknown lua VM at 0x%p\n", pVM);
}
if (vmType != VM_Unknown)
{
m_VMs[vmType] = pVM;
for (auto d : m_Dumpers)
{
d->Clear(vmType);
}
SH_ADD_HOOK(IScriptVM, RegisterFunction, pVM, SH_MEMBER(this, &D2VDump::Hook_RegisterFunction), false);
SH_ADD_HOOK(IScriptVM, RegisterScriptClass, pVM, SH_MEMBER(this, &D2VDump::Hook_RegisterScriptClass), false);
SH_ADD_HOOK(IScriptVM, RegisterInstance, pVM, SH_MEMBER(this, &D2VDump::Hook_RegisterInstance), false);
SH_ADD_HOOK(IScriptVM, SetValue, pVM, SH_MEMBER(this, &D2VDump::Hook_SetValue1), false);
SH_ADD_HOOK(IScriptVM, SetValue, pVM, SH_MEMBER(this, &D2VDump::Hook_SetValue2), false);
SH_ADD_HOOK(IScriptVM, SetEnumValue, pVM, SH_MEMBER(this, &D2VDump::Hook_SetEnumValue), false);
SH_ADD_HOOK(IScriptVM, SetEnumValue, pVM, SH_MEMBER(this, &D2VDump::Hook_SetEnumValue_Post), true);
}
RETURN_META_VALUE(MRES_SUPERCEDE, pVM);
}
void D2VDump::Hook_DestroyVM(IScriptVM *pVM)
{
VMType v = VMToVMType(pVM);
if (v != VM_Unknown)
{
SH_REMOVE_HOOK(IScriptVM, RegisterFunction, pVM, SH_MEMBER(this, &D2VDump::Hook_RegisterFunction), false);
SH_REMOVE_HOOK(IScriptVM, RegisterScriptClass, pVM, SH_MEMBER(this, &D2VDump::Hook_RegisterScriptClass), false);
SH_REMOVE_HOOK(IScriptVM, RegisterInstance, pVM, SH_MEMBER(this, &D2VDump::Hook_RegisterInstance), false);
SH_REMOVE_HOOK(IScriptVM, SetValue, pVM, SH_MEMBER(this, &D2VDump::Hook_SetValue1), false);
SH_REMOVE_HOOK(IScriptVM, SetValue, pVM, SH_MEMBER(this, &D2VDump::Hook_SetValue2), false);
SH_REMOVE_HOOK(IScriptVM, SetEnumValue, pVM, SH_MEMBER(this, &D2VDump::Hook_SetEnumValue), false);
SH_REMOVE_HOOK(IScriptVM, SetEnumValue, pVM, SH_MEMBER(this, &D2VDump::Hook_SetEnumValue_Post), true);
m_VMs[v] = nullptr;
}
RETURN_META(MRES_IGNORED);
}
bool D2VDump::Hook_SetValue1(HSCRIPT hScope, const char *pszKey, const char *pszValue)
{
if (!m_bInSetEnumValue)
{
DevMsg("SV!: (HSCRIPT: %p) (Name: \"%s\")\n", hScope, pszKey);
VMType v = VMToVMType(META_IFACEPTR(IScriptVM));
if (v != VM_Unknown)
{
for (auto d : m_Dumpers)
{
d->AddValue(pszKey, ScriptVariant_t(pszValue), v);
}
}
}
RETURN_META_VALUE(MRES_IGNORED, true);
}
bool D2VDump::Hook_SetValue2(HSCRIPT hScope, const char *pszKey, const ScriptVariant_t &value)
{
if (!m_bInSetEnumValue)
{
DevMsg("SV2: (HSCRIPT: %p) (Name: \"%s\")\n", hScope, pszKey);
VMType v = VMToVMType(META_IFACEPTR(IScriptVM));
if (v != VM_Unknown)
{
for (auto d : m_Dumpers)
{
d->AddValue(pszKey, value, v);
}
}
}
RETURN_META_VALUE(MRES_IGNORED, true);
}
bool D2VDump::Hook_SetEnumValue(HSCRIPT hScope, const char *pszEnumName, const char *pszValueName, int value, const char *pszDescription)
{
DevMsg("SEV: (HSCRIPT: %p) (Name: \"%s\") (Value: (\"%s\")\n", hScope, pszValueName, pszValueName);
m_bInSetEnumValue = true;
VMType v = VMToVMType(META_IFACEPTR(IScriptVM));
if (v != VM_Unknown)
{
for (auto d : m_Dumpers)
{
d->AddEnumValue(pszEnumName, pszValueName, pszDescription, value, v);
}
}
RETURN_META_VALUE(MRES_IGNORED, true);
}
bool D2VDump::Hook_SetEnumValue_Post(HSCRIPT hScope, const char *pszEnumName, const char *pszValueName, int value, const char *pszDescription)
{
m_bInSetEnumValue = false;
return true;
}
const char *D2VDump::GetLicense()
{
return "GPLv3";
}
const char *D2VDump::GetVersion()
{
return "1.0.0.0";
}
const char *D2VDump::GetDate()
{
return __DATE__;
}
const char *D2VDump::GetLogTag()
{
return "D2VDUMP";
}
const char *D2VDump::GetAuthor()
{
return "Nicholas Hastings";
}
const char *D2VDump::GetDescription()
{
return "Dumps Dota 2 VScript classes, functions, and variables.";
}
const char *D2VDump::GetName()
{
return "Dota 2 VScript Dumper";
}
const char *D2VDump::GetURL()
{
return "https://moddota.com";
}