require "lib.moonloader"
local ffi = require 'ffi'
local imgui = require('imgui')
local encoding = require 'encoding'
encoding.default = 'CP1251'
u8 = encoding.UTF8
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
local Variables = {}
function RegisterVariables(name,text)
table.insert(Variables,{ '%$'..name..'%$', text })
end
RegisterVariables('1','Текст 1')
RegisterVariables('2','Текст 2')
local window = imgui.ImBool(false)
local vab = imgui.ImBuffer('vab', 256)
local leader = imgui.ImBuffer('leader text', 256)
function main()
if not isSampfuncsLoaded() or not isSampLoaded() then return end
while not isSampAvailable() do wait(100) end
sampChatHook = hook.new('void(__thiscall *)(void *this, uint32_t type, const char* text, const char* prefix, uint32_t color, uint32_t pcolor)', ChatHook, getModuleHandle('samp.dll') + 0x64010)
sampRegisterChatCommand('vab',function()
window.v = not window.v
end)
while true do
wait(0)
imgui.Process = window.v
end
end
function ChatHook(HOOKED_CHAT_THIS, HOOKED_CHAT_TYPE, HOOKED_CHAT_TEXT, HOOKED_CHAT_PREFIX, HOOKED_CHAT_COLOR, HOOKED_CHAT_PCOLOR) -- хук сообщений с чата
local ReplaceText = ffi.string(HOOKED_CHAT_TEXT)
for k,v in ipairs(Variables) do
if ReplaceText:match(v[1]) then
ReplaceText = ReplaceText:gsub(v[1],v[2])
end
end
--local text = ffi.string(HOOKED_CHAT_TEXT)..'Leon'
print(ReplaceText)
return sampChatHook(HOOKED_CHAT_THIS, HOOKED_CHAT_TYPE, ReplaceText, HOOKED_CHAT_PREFIX, HOOKED_CHAT_COLOR, HOOKED_CHAT_PCOLOR)
end
function imgui.OnDrawFrame()
local x,y = getScreenResolution()
if window.v then
imgui.SetNextWindowPos(imgui.ImVec2(x/3, y/3), imgui.Cond.FirstUseEver)
imgui.SetNextWindowSize(imgui.ImVec2(900.0, 300.0), imgui.Cond.FirstUseEver)
imgui.Begin('Window Title', window)
imgui.InputText(u8'Здесть пиши то, на что будет заменено $leader$', vab)
if imgui.Button(u8'Зарегистрировать Паттерн') then RegisterVariables('leader',u8:decode(vab.v)) end
imgui.InputText(u8'Здесь вывод текста в чат например', leader)
if imgui.Button(u8'Вывести') then sampSendChat(u8:decode(leader.v)) end
imgui.End()
end
end