require('moonloader')
local ffi = require('ffi')
local wm = require('windows.message')
local state = false
--HOOKS
local hook = {hooks = {}}
addEventHandler('onScriptTerminate', function(scr)
if scr == script.this then
for i, hook in ipairs(hook.hooks) do
if hook.status then
hook.stop()
end
end
end
end)
ffi.cdef [[
int VirtualProtect(void* lpAddress, unsigned long dwSize, unsigned long flNewProtect, unsigned long* lpflOldProtect);
]]
function hook.new(cast, callback, hook_addr, size)
local size = size or 5
local new_hook = {}
local detour_addr = tonumber(ffi.cast('intptr_t', ffi.cast('void*', ffi.cast(cast, callback))))
local void_addr = ffi.cast('void*', hook_addr)
local old_prot = ffi.new('unsigned long[1]')
local org_bytes = ffi.new('uint8_t[?]', size)
ffi.copy(org_bytes, void_addr, size)
local hook_bytes = ffi.new('uint8_t[?]', size, 0x90)
hook_bytes[0] = 0xE9
ffi.cast('uint32_t*', hook_bytes + 1)[0] = detour_addr - hook_addr - 5
new_hook.call = ffi.cast(cast, hook_addr)
new_hook.status = false
local function set_status(bool)
new_hook.status = bool
ffi.C.VirtualProtect(void_addr, size, 0x40, old_prot)
ffi.copy(void_addr, bool and hook_bytes or org_bytes, size)
ffi.C.VirtualProtect(void_addr, size, old_prot[0], old_prot)
end
new_hook.stop = function() set_status(false) end
new_hook.start = function() set_status(true) end
new_hook.start()
table.insert(hook.hooks, new_hook)
return setmetatable(new_hook, {
__call = function(self, ...)
self.stop()
local res = self.call(...)
self.start()
return res
end
})
end
--HOOKS
function main()
if (not isSampLoaded()) then
return
end
while (not isSampAvailable()) do
wait(100)
end
fpHkEntry = hook.new('void(__thiscall *)(void* this, int nType, const char* szText, const char* szPrefix, unsigned long textColor, unsigned long prefixColor)', HOOK__Entry, getModuleHandle('samp.dll') + 0x64010)
wait(-1)
end
function HOOK__Entry(this, nType, szText, szPrefix, textColor, prefixColor)
if (state) then
if (string.find(ffi.string(szText), '%[R%]<ВОПРОС> (%w+)%[(%d+)%]: (.+)')) then
local nickname, id, question = string.match(ffi.string(szText), '%[R%]<ВОПРОС> (%w+)%[(%d+)%]: (.+)')
sampAddChatMessage(string.format('[ВОПРОС] %s[%d]: %s ', nickname, id, question), -1)
sampSendChat('/ot')
end
if (string.find(ffi.string(szText), '%[R%]<ЖАЛОБА> (%w+)%[(%d+)%]: (.+)')) then
local nickname, id, complaint = string.match(ffi.string(szText), '%[R%]<ЖАЛОБА> (%w+)%[(%d+)%]: (.+)')
sampAddChatMessage(string.format('[ЖАЛОБА] %s[%d]: %s ', nickname, id, complaint), -1)
sampSendChat('/ot')
end
end
return fpHkEntry(this, nType, szText, szPreifx, textColor, prefixColor)
end
addEventHandler('onWindowMessage', function(uMsg, wParam, lParam)
if (uMsg == wm.WM_KEYDOWN or uMsg == wm.WM_SYSKEYDOWN) then
if (wParam == VK_X) then
state = not state
sampAddChatMessage(state and 'Включен' or 'Выключен', -1)
end
end
end)