| 2368 | #endif |
| 2369 | |
| 2370 | static int memory_registerHook(lua_State* L, LuaMemHookType hookType, int defaultSize) |
| 2371 | { |
| 2372 | // get first argument: address |
| 2373 | unsigned int addr = luaL_checkinteger(L,1); |
| 2374 | //if((addr & ~0xFFFFFF) == ~0xFFFFFF) |
| 2375 | // addr &= 0xFFFFFF; |
| 2376 | |
| 2377 | // get optional second argument: size |
| 2378 | int size = defaultSize; |
| 2379 | int funcIdx = 2; |
| 2380 | if(lua_isnumber(L,2)) |
| 2381 | { |
| 2382 | size = luaL_checkinteger(L,2); |
| 2383 | if(size < 0) |
| 2384 | { |
| 2385 | size = -size; |
| 2386 | addr -= size; |
| 2387 | } |
| 2388 | funcIdx++; |
| 2389 | } |
| 2390 | |
| 2391 | // check last argument: callback function |
| 2392 | bool clearing = lua_isnil(L,funcIdx); |
| 2393 | if(!clearing) |
| 2394 | luaL_checktype(L, funcIdx, LUA_TFUNCTION); |
| 2395 | lua_settop(L,funcIdx); |
| 2396 | |
| 2397 | // get the address-to-callback table for this hook type of the current script |
| 2398 | lua_getfield(L, LUA_REGISTRYINDEX, luaMemHookTypeStrings[hookType]); |
| 2399 | |
| 2400 | // count how many callback functions we'll be displacing |
| 2401 | int numFuncsAfter = clearing ? 0 : size; |
| 2402 | int numFuncsBefore = 0; |
| 2403 | for(unsigned int i = addr; i != addr+size; i++) |
| 2404 | { |
| 2405 | lua_rawgeti(L, -1, i); |
| 2406 | if(lua_isfunction(L, -1)) |
| 2407 | numFuncsBefore++; |
| 2408 | lua_pop(L,1); |
| 2409 | } |
| 2410 | |
| 2411 | // put the callback function in the address slots |
| 2412 | for(unsigned int i = addr; i != addr+size; i++) |
| 2413 | { |
| 2414 | lua_pushvalue(L, -2); |
| 2415 | lua_rawseti(L, -2, i); |
| 2416 | } |
| 2417 | |
| 2418 | // adjust the count of active hooks |
| 2419 | //LuaContextInfo& info = GetCurrentInfo(); |
| 2420 | /*info.*/ numMemHooks += numFuncsAfter - numFuncsBefore; |
| 2421 | |
| 2422 | // re-cache regions of hooked memory across all scripts |
| 2423 | CalculateMemHookRegions(hookType); |
| 2424 | |
| 2425 | //StopScriptIfFinished(luaStateToUIDMap[L]); |
| 2426 | return 0; |
| 2427 | } |
no test coverage detected