Skip to content

Commit df89da5

Browse files
committed
fix(code): resolve #12 the quick way
1 parent c1ea374 commit df89da5

File tree

4 files changed

+65
-65
lines changed

4 files changed

+65
-65
lines changed

CodeFunctions.cpp

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -252,32 +252,32 @@ DWORD __stdcall executeLuaHook(unsigned long* args) {
252252
return 0;
253253
}
254254

255-
lua_rawgeti(L, LUA_REGISTRYINDEX, luaHookedFunctionReference);
255+
lua_rawgeti(LC, LUA_REGISTRYINDEX, luaHookedFunctionReference);
256256

257-
if (lua_isfunction(L, -1)) {
257+
if (lua_isfunction(LC, -1)) {
258258
int totalArgCount = luaHookedFunctionArgCount;
259259
if (luaCallingConvention == CallingConvention::THISCALL) {
260-
lua_pushnumber(L, currentECXValue);
260+
lua_pushnumber(LC, currentECXValue);
261261
totalArgCount += 1;
262262
}
263263
for (int i = 0; i < luaHookedFunctionArgCount; i++) {
264-
lua_pushnumber(L, args[i]);
264+
lua_pushnumber(LC, args[i]);
265265
}
266-
if (lua_pcall(L, totalArgCount, 1, 0) == LUA_OK) {
266+
if (lua_pcall(LC, totalArgCount, 1, 0) == LUA_OK) {
267267
luaErrorLevel = 0;
268268
luaErrorMsg = "";
269-
int ret = (DWORD)lua_tonumber(L, -1);
270-
lua_pop(L, 1); // pop off the return value;
269+
int ret = (DWORD)lua_tonumber(LC, -1);
270+
lua_pop(LC, 1); // pop off the return value;
271271
return ret;
272272
}
273273
else {
274274
luaErrorLevel = 1;
275-
luaErrorMsg = lua_tostring(L, -1);
276-
lua_pop(L, 1); // pop off the error message;
275+
luaErrorMsg = lua_tostring(LC, -1);
276+
lua_pop(LC, 1); // pop off the error message;
277277
}
278278
}
279279
else {
280-
lua_pop(L, 1); // we need this pop, because the getglobal does a push that would otherwise be popped by pcall.
280+
lua_pop(LC, 1); // we need this pop, because the getglobal does a push that would otherwise be popped by pcall.
281281
luaErrorLevel = 2;
282282
luaErrorMsg = std::string(luaHookedFunctionName) + " is not a function";
283283
}
@@ -737,54 +737,54 @@ void __stdcall GetDetourLuaTargetAndCallTheLuaFunction(DWORD address, DWORD* reg
737737
return;
738738
}
739739

740-
int before = lua_gettop(L);
740+
int before = lua_gettop(LC);
741741

742-
lua_rawgeti(L, LUA_REGISTRYINDEX, entry->luaFunctionRef);
742+
lua_rawgeti(LC, LUA_REGISTRYINDEX, entry->luaFunctionRef);
743743

744-
if (lua_isfunction(L, -1)) {
745-
lua_createtable(L, 0, 8);
744+
if (lua_isfunction(LC, -1)) {
745+
lua_createtable(LC, 0, 8);
746746

747747
for (int i = 0; i < order.size(); i++) {
748-
lua_pushstring(L, order[i].c_str());
748+
lua_pushstring(LC, order[i].c_str());
749749
if (order[i] == "ESP") {
750-
lua_pushinteger(L, registers[i]+8);
750+
lua_pushinteger(LC, registers[i]+8);
751751
}
752752
else {
753-
lua_pushinteger(L, registers[i]);
753+
lua_pushinteger(LC, registers[i]);
754754
}
755-
lua_settable(L, -3); /* 3rd element from the stack top */
755+
lua_settable(LC, -3); /* 3rd element from the stack top */
756756
}
757757

758758
// We call the function and pass 1 argument and expect 1 argument in return.
759-
if (lua_pcall(L, 1, 1, 0) == LUA_OK) {
759+
if (lua_pcall(LC, 1, 1, 0) == LUA_OK) {
760760
luaErrorLevel = 0;
761761
luaErrorMsg = "";
762-
if (lua_istable(L, -1)) {
762+
if (lua_istable(LC, -1)) {
763763
/* table is in the stack at index 't' */
764-
lua_pushnil(L); /* first key */
765-
while (lua_next(L, -2) != 0) {
764+
lua_pushnil(LC); /* first key */
765+
while (lua_next(LC, -2) != 0) {
766766
/* uses 'key' (at index -2) and 'value' (at index -1) */
767-
if (!lua_isstring(L, -2)) {
767+
if (!lua_isstring(LC, -2)) {
768768
luaErrorLevel = 6;
769769
luaErrorMsg = "The return value table must have string keys";
770770
std::cout << "[RPS]: " << std::string(luaErrorMsg) << std::endl;
771771
currentDetourReturn = retLoc;
772772

773-
lua_settop(L, before);
773+
lua_settop(LC, before);
774774
return;
775775
}
776-
if (!lua_isinteger(L, -1)) {
776+
if (!lua_isinteger(LC, -1)) {
777777
luaErrorLevel = 5;
778778
luaErrorMsg = "The return value table must have integer values";
779779
std::cout << "[RPS]: " << std::string(luaErrorMsg) << std::endl;
780780
currentDetourReturn = retLoc;
781781

782-
lua_settop(L, before);
782+
lua_settop(LC, before);
783783
return;
784784
}
785785

786-
std::string key = lua_tostring(L, -2);
787-
DWORD value = lua_tointeger(L, -1);
786+
std::string key = lua_tostring(LC, -2);
787+
DWORD value = lua_tointeger(LC, -1);
788788

789789
std::vector<std::string>::const_iterator it = find(order.begin(), order.end(), key);
790790
if (it == order.end()) {
@@ -793,7 +793,7 @@ void __stdcall GetDetourLuaTargetAndCallTheLuaFunction(DWORD address, DWORD* reg
793793
std::cout << "[RPS]: " << std::string(luaErrorMsg) << std::endl;
794794
currentDetourReturn = retLoc;
795795

796-
lua_settop(L, before);
796+
lua_settop(LC, before);
797797
return;
798798
}
799799

@@ -808,39 +808,39 @@ void __stdcall GetDetourLuaTargetAndCallTheLuaFunction(DWORD address, DWORD* reg
808808

809809

810810
/* removes 'value'; keeps 'key' for next iteration */
811-
lua_pop(L, 1);
811+
lua_pop(LC, 1);
812812
}
813-
lua_pop(L, 1); // pop off the return value;
813+
lua_pop(LC, 1); // pop off the return value;
814814
currentDetourReturn = retLoc;
815815

816-
lua_settop(L, before);
816+
lua_settop(LC, before);
817817
return;
818818
}
819819
else {
820820
luaErrorLevel = 3;
821821
luaErrorMsg = "Detour did not return a table";
822822
}
823-
lua_pop(L, 1); // pop off the return value;
823+
lua_pop(LC, 1); // pop off the return value;
824824
currentDetourReturn = retLoc;
825825

826-
lua_settop(L, before);
826+
lua_settop(LC, before);
827827
return;
828828
}
829829
else {
830830
luaErrorLevel = 1;
831-
luaErrorMsg = lua_tostring(L, -1);
832-
lua_pop(L, 1); // pop off the error message;
831+
luaErrorMsg = lua_tostring(LC, -1);
832+
lua_pop(LC, 1); // pop off the error message;
833833
}
834834
}
835835
else {
836-
lua_pop(L, 1); // I think we need this pop, because the getglobal does a push that would otherwise be popped by pcall.
836+
lua_pop(LC, 1); // I think we need this pop, because the getglobal does a push that would otherwise be popped by pcall.
837837
luaErrorLevel = 2;
838838
luaErrorMsg = std::string(entry->luaDetourFunctionName.c_str()) + " is not a function";
839839
}
840840

841841
std::cout << "[RPS]: " << std::string(luaErrorMsg) << std::endl;
842842

843-
lua_settop(L, before);
843+
lua_settop(LC, before);
844844
currentDetourReturn = retLoc;
845845
}
846846

RuntimePatchingSystem.cpp

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ RUNTIMEPATCHINGSYSTEM_API void RPS_initializePrintRedirect(lua_State* L) {
8080
}
8181

8282
RUNTIMEPATCHINGSYSTEM_API void RPS_initializePrintRedirect() {
83-
return RPS_initializePrintRedirect(L);
83+
return RPS_initializePrintRedirect(LC);
8484
}
8585

8686
RUNTIMEPATCHINGSYSTEM_API void RPS_initializeLuaAPI(lua_State* L, std::string apiNamespace) {
@@ -101,15 +101,15 @@ RUNTIMEPATCHINGSYSTEM_API void RPS_initializeLuaAPI(lua_State* L, std::string ap
101101
}
102102

103103
RUNTIMEPATCHINGSYSTEM_API void RPS_initializeLuaAPI(std::string apiNamespace) {
104-
return RPS_initializeLuaAPI(L, apiNamespace);
104+
return RPS_initializeLuaAPI(LC, apiNamespace);
105105
}
106106

107107
RUNTIMEPATCHINGSYSTEM_API void RPS_initializeLuaAPI(lua_State* L) {
108108
return RPS_initializeLuaAPI(L, "global");
109109
}
110110

111111
RUNTIMEPATCHINGSYSTEM_API void RPS_initializeLuaAPI() {
112-
return RPS_initializeLuaAPI(L);
112+
return RPS_initializeLuaAPI(LC);
113113
}
114114

115115
RUNTIMEPATCHINGSYSTEM_API void RPS_setupPackagePath(lua_State* L, std::string packagePath) {
@@ -121,7 +121,7 @@ RUNTIMEPATCHINGSYSTEM_API void RPS_setupPackagePath(lua_State* L, std::string pa
121121
}
122122

123123
RUNTIMEPATCHINGSYSTEM_API void RPS_setupPackagePath(std::string packagePath) {
124-
return RPS_setupPackagePath(L, packagePath);
124+
return RPS_setupPackagePath(LC, packagePath);
125125
}
126126

127127
RUNTIMEPATCHINGSYSTEM_API void RPS_setupPackageCPath(lua_State* L, std::string packageCPath) {
@@ -133,7 +133,7 @@ RUNTIMEPATCHINGSYSTEM_API void RPS_setupPackageCPath(lua_State* L, std::string p
133133
}
134134

135135
RUNTIMEPATCHINGSYSTEM_API void RPS_setupPackageCPath(std::string packageCPath) {
136-
return RPS_setupPackageCPath(L, packageCPath);
136+
return RPS_setupPackageCPath(LC, packageCPath);
137137
}
138138

139139
RUNTIMEPATCHINGSYSTEM_API void RPS_runBootstrapFile(lua_State* L, std::string bootstrapFilePath) {
@@ -161,81 +161,81 @@ RUNTIMEPATCHINGSYSTEM_API void RPS_runBootstrapFile(lua_State* L, std::string bo
161161
}
162162

163163
RUNTIMEPATCHINGSYSTEM_API void RPS_runBootstrapFile(std::string bootstrapFilePath) {
164-
return RPS_runBootstrapFile(L, bootstrapFilePath);
164+
return RPS_runBootstrapFile(LC, bootstrapFilePath);
165165
}
166166

167167
RUNTIMEPATCHINGSYSTEM_API void RPS_initializeLua() {
168-
L = luaL_newstate();
168+
LC = luaL_newstate();
169169
}
170170

171171
RUNTIMEPATCHINGSYSTEM_API void RPS_initializeLuaOpenLibs() {
172-
luaL_openlibs(L);
172+
luaL_openlibs(LC);
173173
}
174174

175175
RUNTIMEPATCHINGSYSTEM_API void RPS_initializeLuaOpenBase() {
176-
luaL_requiref(L, "base", luaopen_base, true);
176+
luaL_requiref(LC, "base", luaopen_base, true);
177177
}
178178

179179
RUNTIMEPATCHINGSYSTEM_API void RPS_initialize(std::string bootstrapFilePath, bool initializePrintRedirect) {
180180
RPS_initializeLua();
181181

182182
RPS_initializeLuaOpenLibs();
183183

184-
if (initializePrintRedirect) RPS_initializePrintRedirect(L);
184+
if (initializePrintRedirect) RPS_initializePrintRedirect(LC);
185185

186-
RPS_initializeLuaAPI(L, "global");
186+
RPS_initializeLuaAPI(LC, "global");
187187

188188

189-
RPS_runBootstrapFile(L, bootstrapFilePath);
189+
RPS_runBootstrapFile(LC, bootstrapFilePath);
190190
}
191191

192192
RUNTIMEPATCHINGSYSTEM_API void RPS_initialize(std::string bootstrapFilePath) {
193193
return RPS_initialize(bootstrapFilePath, true);
194194
}
195195

196196
RUNTIMEPATCHINGSYSTEM_API void RPS_deinitialize() {
197-
lua_close(L);
197+
lua_close(LC);
198198
}
199199

200200
RUNTIMEPATCHINGSYSTEM_API void RPS_executeSnippet(std::string code) {
201-
int before = lua_gettop(L);
202-
int r = luaL_dostring(L, code.c_str());
201+
int before = lua_gettop(LC);
202+
int r = luaL_dostring(LC, code.c_str());
203203
if (r == LUA_OK) {
204-
int after = lua_gettop(L);
204+
int after = lua_gettop(LC);
205205
if (after - before > 0) {
206206
for (int i = before; i < after; i++) { /* for each argument */
207207
size_t l;
208-
const char* s = luaL_tolstring(L, i, &l); /* convert it to string */
208+
const char* s = luaL_tolstring(LC, i, &l); /* convert it to string */
209209
if (i > 1) /* not the first element? */
210210
lua_writestring("\t", 1); /* add a tab before it */
211211
lua_writestring(s, l); /* print it */
212-
lua_pop(L, 1); /* pop result */
212+
lua_pop(LC, 1); /* pop result */
213213
}
214214
lua_writeline();
215215
}
216-
lua_pop(L, after - before);
216+
lua_pop(LC, after - before);
217217
}
218218
else {
219-
std::string errormsg = lua_tostring(L, -1);
219+
std::string errormsg = lua_tostring(LC, -1);
220220
std::cout << "[RPS]: error in lua snippet: " << errormsg << std::endl;
221-
lua_pop(L, 1); // pop off the error message;
221+
lua_pop(LC, 1); // pop off the error message;
222222
}
223223
}
224224

225225
RUNTIMEPATCHINGSYSTEM_API int RPS_getCurrentStackSize() {
226-
return lua_gettop(L);
226+
return lua_gettop(LC);
227227
}
228228

229229
RUNTIMEPATCHINGSYSTEM_API lua_State* RPS_getLuaState() {
230-
return L;
230+
return LC;
231231
}
232232

233233
RUNTIMEPATCHINGSYSTEM_API void RPS_setLuaState(lua_State* value) {
234-
L = value;
234+
LC = value;
235235
}
236236

237237
extern "C" RUNTIMEPATCHINGSYSTEM_API int luaopen_RPS(lua_State * L) {
238-
238+
RPS_setLuaState(L);
239239
luaL_newlib(L, RPS_LIB);
240240

241241
return 1;

UtilityFunctions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "UtilityFunctions.h"
22

3-
lua_State* L = 0;
3+
lua_State* LC = 0;
44

55
#ifdef _DEBUG
66

UtilityFunctions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <iostream>
55
#include "lua.hpp"
66

7-
extern lua_State* L;
7+
extern lua_State* LC;
88

99
#ifdef _DEBUG
1010

0 commit comments

Comments
 (0)