Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvscript_patch.cpp
More file actions
Latest commit
296 lines (231 loc) · 7.68 KB
/
Copy pathvscript_patch.cpp
File metadata and controls
296 lines (231 loc) · 7.68 KB
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
#include<assert.h>
#include<Windows.h>
extern"C" {
#defineLUA_BUILD_AS_DLL
#include"lua.h"
#include"lualib.h"
#include"lauxlib.h"
}
extern"C" {
LUALIB_APIintluaopen_ffi(lua_State* L);
LUA_APIintluaJIT_setmode(lua_State* L, int idx, int mode);
}
typedefvoid* (*CreateInterfaceFn)(constchar *pName, int *pReturnCode);
structAppSystemInfo_t
{
constchar* m_pModuleName;
constchar* m_pInterfaceName;
};
enum InitReturnVal_t
{
INIT_FAILED = 0,
INIT_OK,
INIT_LAST_VAL,
};
enum AppSystemTier_t
{
APP_SYSTEM_TIER0 = 0,
APP_SYSTEM_TIER1,
APP_SYSTEM_TIER2,
APP_SYSTEM_TIER3,
APP_SYSTEM_TIER_OTHER,
};
enum BuildType_t
{
kBuildTypeRelease = 2
};
classIAppSystem
{
public:
// Here's where the app systems get to learn about each other
virtualboolConnect( CreateInterfaceFn factory ) = 0;
virtualvoidDisconnect() = 0;
// Here's where systems can access other interfaces implemented by this object
// Returns NULL if it doesn't implement the requested interface
virtualvoid *QueryInterface( constchar *pInterfaceName ) = 0;
// Init, shutdown
virtual InitReturnVal_t Init() = 0;
virtualvoidShutdown() = 0;
virtualvoidPreShutdown() = 0;
// Returns all dependent libraries
virtualconst AppSystemInfo_t* GetDependencies() = 0;
// Returns the tier
virtual AppSystemTier_t GetTier() = 0;
// Reconnect to a particular interface
virtualvoidReconnect(CreateInterfaceFn factory, constchar* pInterfaceName) = 0;
// Returns whether or not the app system is a singleton
virtualboolIsSingleton() = 0;
// Source 2 Added
virtual BuildType_t GetBuildType() = 0;
};
enum ScriptLanguage_t
{
SL_NONE,
SL_LUA,
SL_DEFAULT = SL_LUA
};
classIScriptVMPartA
{
public:
virtualvoidDeleteThis() = 0; // ?
virtualboolInit() = 0;
virtualboolConnectDebugger() = 0;
virtualvoidDisconnectDebugger() = 0;
virtual ScriptLanguage_t GetLanguage() = 0;
virtualconstchar* GetLanguageName() = 0;
virtualvoid* GetInternalVM() = 0;
// TODO : more
};
classIScriptVM;
classIScriptDebugger;
classIScriptManager : publicIAppSystem
{
public:
virtual IScriptVM *CreateVM( ScriptLanguage_t language = SL_DEFAULT ) = 0;
virtualvoidDestroyVM( IScriptVM * ) = 0;
virtual IScriptDebugger* GetDebugger() = 0;
};
#defineVSCRIPT_INTERFACE_VERSION"VScriptManager010"
CreateInterfaceFn GetOrigFactory()
{
auto dl = LoadLibrary("vscript2.dll");
return (CreateInterfaceFn)GetProcAddress(dl, "CreateInterface");
}
IScriptManager *g_pScriptManager = nullptr;
classCScriptManagerExt : publicIScriptManager
{
public:
boolConnect( CreateInterfaceFn factory ) override
{
return g_pScriptManager->Connect(factory);
}
voidDisconnect() override
{
return g_pScriptManager->Disconnect();
}
void *QueryInterface( constchar *pInterfaceName ) override
{
return g_pScriptManager->QueryInterface(pInterfaceName);
}
InitReturnVal_t Init() override
{
return g_pScriptManager->Init();
}
voidShutdown() override
{
return g_pScriptManager->Shutdown();
}
voidPreShutdown() override
{
return g_pScriptManager->PreShutdown();
}
const AppSystemInfo_t* GetDependencies() override
{
return g_pScriptManager->GetDependencies();
}
AppSystemTier_t GetTier() override
{
return g_pScriptManager->GetTier();
}
voidReconnect(CreateInterfaceFn factory, constchar* pInterfaceName) override
{
return g_pScriptManager->Reconnect(factory, pInterfaceName);
}
boolIsSingleton() override
{
return g_pScriptManager->IsSingleton();
}
BuildType_t GetBuildType() override
{
return g_pScriptManager->GetBuildType();
}
IScriptVM *CreateVM( ScriptLanguage_t language = SL_DEFAULT ) override
{
auto vm = g_pScriptManager->CreateVM(language);
auto vm2 = (IScriptVMPartA*)vm;
lua_State *L = (lua_State *)vm2->GetInternalVM();
// moemod : turn on jit again
#defineLUAJIT_MODE_OFF0x0000/* Turn feature off. */
#defineLUAJIT_MODE_ON0x0100/* Turn feature on. */
#defineLUAJIT_MODE_FLUSH0x0200/* Flush JIT-compiled code. */
luaJIT_setmode(L, 0, LUAJIT_MODE_ON);
lua_getglobal(L, "print"); // #1
lua_pushstring(L, "vscript patch: hello, world !!!"); // #2
lua_call(L, 1, 0); // #0
// moemod hack : restore package
// valve sets package.loaders[*] and package.loadlib to nil
// here we are going to restore them
lua_getglobal(L, "package"); // #1 = package
lua_getfield(L, -1, "loaders"); // #2 = package.loaders
lua_pushinteger(L, 2); // #3 = 2
lua_gettable(L, -2); // #3 = package.loaders[2]
int valve_loader_ref = luaL_ref(L, LUA_REGISTRYINDEX); // #2 = package.loaders
lua_pop(L, 2); // #0
lua_pushcfunction(L, luaopen_package); // #1
lua_call(L, 0, 0); // #0
lua_getglobal(L, "table"); // #1 = table
lua_getfield(L, -1, "insert"); // #2 = table.insert
lua_remove(L, -2); // #1 = table.insert
lua_getglobal(L, "package"); // #2 = package
lua_getfield(L, -1, "loaders"); // #3 = package.loaders
lua_remove(L, -2); // #2 = package.loaders
lua_pushinteger(L, 2); // #3 = 2
lua_rawgeti(L, LUA_REGISTRYINDEX, valve_loader_ref); // #4 = valve_loader
lua_call(L, 3, 0); // #0
luaL_unref(L, LUA_REGISTRYINDEX, valve_loader_ref);
// moemod hack : restore io
// not working, use local io = require("cio")
//lua_pushcfunction(L, luaopen_io);
//lua_pushstring(L, "io");
//lua_pcall(L, 1, 0, 0);
// moemod hack : restore ffi
// not working, use local io = require("cffi")
//luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD", 1);
//lua_pushcfunction(L, luaopen_ffi);
//lua_setfield(L, -2, "ffi");
//lua_pop(L, 1);
return vm;
}
voidDestroyVM( IScriptVM *vm ) override
{
return g_pScriptManager->DestroyVM(vm);
}
IScriptDebugger* GetDebugger() override
{
return g_pScriptManager->GetDebugger();
}
} g_ScriptManagerExt;
#if defined( _WIN32 )
// Used for dll exporting and importing
#defineDLL_EXPORTextern"C" __declspec( dllexport )
#defineDLL_IMPORTextern"C" __declspec( dllimport )
// Can't use extern "C" when DLL exporting a class
#defineDLL_CLASS_EXPORT__declspec( dllexport )
#defineDLL_CLASS_IMPORT__declspec( dllimport )
// Can't use extern "C" when DLL exporting a global
#defineDLL_GLOBAL_EXPORTextern__declspec( dllexport )
#defineDLL_GLOBAL_IMPORTextern__declspec( dllimport )
#elif defined(_LINUX) || defined(__APPLE__)
// Used for dll exporting and importing
#defineDLL_EXPORTextern"C" __attribute__ ((visibility("default")))
#defineDLL_IMPORTextern"C"
// Can't use extern "C" when DLL exporting a class
#defineDLL_CLASS_EXPORT__attribute__ ((visibility("default")))
#defineDLL_CLASS_IMPORT
// Can't use extern "C" when DLL exporting a global
#defineDLL_GLOBAL_EXPORTextern__attribute ((visibility("default")))
#defineDLL_GLOBAL_IMPORTextern
#else
#error "Unsupported Platform."
#endif
// EXPORT
DLL_EXPORTvoid * CreateInterface(constchar *pName, int *pReturnCode)
{
static CreateInterfaceFn vscript_factory = GetOrigFactory();
if(!strcmp(pName, VSCRIPT_INTERFACE_VERSION))
{
g_pScriptManager = (IScriptManager *)vscript_factory(pName, pReturnCode);
return &g_ScriptManagerExt;
}
returnvscript_factory(pName, pReturnCode);
}