Skip to content

Commit 83e92df

Browse files
committed
resolve #7 by refactoring all luaL_error statements into fmt strings
1 parent 6aa48c3 commit 83e92df

2 files changed

Lines changed: 18 additions & 18 deletions

File tree

CodeFunctions.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ int luaExposeCode(lua_State* L) {
177177
}
178178

179179
if (argumentCount > RPS_ARGUMENT_LIMIT) {
180-
return luaL_error(L, (std::string("too many arguments specified, max is ") + std::to_string(RPS_ARGUMENT_LIMIT) + ": " + std::to_string(argumentCount)).c_str());
180+
return luaL_error(L, "too many arguments specified, max is %d but %d were specified", RPS_ARGUMENT_LIMIT, argumentCount);
181181
}
182182

183183
int adjustedArgCount = argumentCount - (int)(callingConvention == CallingConvention::THISCALL);
@@ -219,13 +219,13 @@ int luaHookCode(lua_State* L) {
219219
}
220220

221221
if (argumentCount > RPS_ARGUMENT_LIMIT) {
222-
return luaL_error(L, ("too many arguments specified, max is " + std::to_string(RPS_ARGUMENT_LIMIT) + ": " + std::to_string(argumentCount)).c_str());
222+
return luaL_error(L, "too many arguments specified, max is %d but %d were specified" , RPS_ARGUMENT_LIMIT, argumentCount);
223223
}
224224

225225
std::pair<std::map<DWORD, std::shared_ptr<LuaHook>>::const_iterator, bool> hi = hookMapping.insert(std::pair<DWORD, std::shared_ptr<LuaHook>>(address, std::make_shared<LuaHook>(address, hookSize, callingConvention, argumentCount, "", "")));
226226

227227
if (!hi.second) {
228-
return luaL_error(L, "a hook already exists for function at: " + address);
228+
return luaL_error(L, "a hook already exists for function at: 0x%X", address);
229229
}
230230

231231
// Store a reference to the hook function that is to be called later.
@@ -316,11 +316,12 @@ int luaCallMachineCode(lua_State* L) {
316316
int argumentCount = lua_tointeger(L, lua_upvalueindex(2));
317317
int callingConvention = lua_tointeger(L, lua_upvalueindex(3));
318318

319+
int actualArgCount = lua_gettop(L);
320+
319321
if (callingConvention == CallingConvention::THISCALL) {
320322
int totalArgCount = argumentCount + 1; // ecx is passed as the first parameter
321-
if (lua_gettop(L) != totalArgCount) {
322-
//std::cout << "[RPS]: calling function " << std::hex << functionLocation << " with too few arguments;" << std::endl;
323-
return luaL_error(L, ("[RPS]: calling function " + std::to_string(functionLocation) + " with too few arguments;").c_str());
323+
if (actualArgCount != totalArgCount) {
324+
return luaL_error(L, "[RPS]: error when invoking function 0x%X: expected %d arguments, but received %d", functionLocation, totalArgCount, actualArgCount);
324325
}
325326

326327
for (int i = 0; i < argumentCount; i++) {
@@ -331,20 +332,19 @@ int luaCallMachineCode(lua_State* L) {
331332
}
332333

333334
if (lua_type(L, 1) != LUA_TNUMBER) {
334-
return luaL_error(L, ("[RPS]: calling function " + std::to_string(functionLocation) + " ecx value is not an integer (or pointer);").c_str());
335+
return luaL_error(L, "[RPS]: calling function 0x%X ecx value is not an integer (or pointer)", functionLocation);
335336
}
336337

337338
currentECXValue = lua_tointeger(L, 1); // this parameter
338339
}
339340
else {
340341
if (lua_gettop(L) != argumentCount) { // + 0
341-
//std::cout << "[RPS]: calling function " << std::hex << functionLocation << " with too few arguments;" << std::endl;
342-
return luaL_error(L, ("[RPS]: calling function " + std::to_string(functionLocation) + " with too few arguments;").c_str());
342+
return luaL_error(L, "[RPS]: error when invoking function 0x%X: expected %d arguments, but received %d", functionLocation, argumentCount, actualArgCount);
343343
}
344344

345345
for (int i = 0; i < argumentCount; i++) {
346346
if (lua_type(L, i + 1) != LUA_TNUMBER) {
347-
return luaL_error(L, ("[RPS]: calling function " + std::to_string(functionLocation) + " argument #" + std::to_string(i+1) + " is not an integer (or pointer);").c_str());
347+
return luaL_error(L, "[RPS]: calling function 0x%X argument #%d is not an integer (or pointer)", functionLocation, i + 1);
348348
}
349349
fakeStack[i] = lua_tointeger(L, i + 1); // i + 1 to offset the 0-base
350350
}
@@ -713,7 +713,7 @@ int luaDetourCode(lua_State* L) {
713713

714714
std::pair<std::map<DWORD, std::shared_ptr<LuaDetour>>::const_iterator, bool> hi = detourTargetMap.insert(std::pair<DWORD, std::shared_ptr<LuaDetour>>(address, std::make_shared<LuaDetour>(address, ret)));
715715
if (!hi.second) {
716-
return luaL_error(L, ("detour already exists at this address: " + std::to_string(address)).c_str());
716+
return luaL_error(L, "detour already exists at this address: 0x%X", address);
717717
}
718718

719719
detourTargetMap[address]->luaFunctionRef = luaFunctionRef;

MemoryFunctions.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ int luaWriteString(lua_State* L) {
118118

119119
#ifdef _DEBUG
120120
if (!canWrite(address, size)) {
121-
return luaL_error(L, ("cannot write " + std::to_string(1) + " string to location: " + std::to_string(address)).c_str());
121+
return luaL_error(L, "cannot write %d string to location: 0x%X", 1, address);
122122
}
123123
#endif
124124

@@ -140,7 +140,7 @@ int luaWriteByte(lua_State* L) {
140140

141141
#ifdef _DEBUG
142142
if (!canWrite(address, 1)) {
143-
return luaL_error(L, ("cannot write " + std::to_string(1) + " bytes to location: " + std::to_string(address)).c_str());
143+
return luaL_error(L, "cannot write 1 bytes to location: 0x%X", address);
144144
}
145145
#endif
146146

@@ -161,7 +161,7 @@ int luaWriteSmallInteger(lua_State* L) {
161161

162162
#ifdef _DEBUG
163163
if (!canWrite(address, 2)) {
164-
return luaL_error(L, ("cannot write " + std::to_string(2) + " bytes to location: " + std::to_string(address)).c_str());
164+
return luaL_error(L, "cannot write 2 bytes to location: 0x%X", address);
165165
}
166166
#endif
167167

@@ -182,7 +182,7 @@ int luaWriteInteger(lua_State* L) {
182182

183183
#ifdef _DEBUG
184184
if (!canWrite(address, 4)) {
185-
return luaL_error(L, ("cannot write " + std::to_string(4) + " bytes to location: " + std::to_string(address)).c_str());
185+
return luaL_error(L, "cannot write 4 bytes to location: 0x%X", address);
186186
}
187187
#endif
188188

@@ -206,7 +206,7 @@ int luaWriteBytes(lua_State* L) {
206206
#ifdef _DEBUG
207207
int length = lua_rawlen(L, 2);
208208
if (!canWrite(address, length)) {
209-
return luaL_error(L, ("cannot write " + std::to_string(length) + " bytes to location: " + std::to_string(address)).c_str());
209+
return luaL_error(L, "cannot write %d bytes to location: 0x%X", length, address);
210210
}
211211
#endif
212212

@@ -254,7 +254,7 @@ int luaMemCpy(lua_State* L) {
254254

255255
#ifdef _DEBUG
256256
if (!canWrite(dst, size)) {
257-
return luaL_error(L, ("cannot write " + std::to_string(size) + " bytes to location: " + std::to_string(dst)).c_str());
257+
return luaL_error(L, "cannot write %d bytes to location: 0x%X", size, dst);
258258
}
259259
#endif
260260

@@ -288,7 +288,7 @@ int luaMemSet(lua_State* L) {
288288

289289
#ifdef _DEBUG
290290
if (!canWrite(dst, size)) {
291-
return luaL_error(L, ("cannot write " + std::to_string(size) + " bytes to location: " + std::to_string(dst)).c_str());
291+
return luaL_error(L, "cannot write %d bytes to location: 0x%X", size, dst);
292292
}
293293
#endif
294294

0 commit comments

Comments
 (0)