local ffi = require 'ffi' -- Structure definition for CVector, as it is used in the…
- User
local ffi = require 'ffi'
-- Structure definition for CVector, as it is used in the function ffi.cdef[[ typedef struct { float x; float y; float z; } CVector; ]]
--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) jit.off(callback, true) --off jit compilation | thx FYP 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
-- -- Hook implementation for DrawHealthBar -- function CPlayerTagsDrawHealthBarHook(this, pPosition, fHealth, fArmour, fDistanceToCamera) -- -- Custom logic here -- -- For example, print the health and armour values
-- -- Call the original function -- -- return CPlayerTagsDrawHealthBar(this, pPosition, fHealth/2, fArmour+15, fDistanceToCamera)-- end
-- function main() -- local drawHealthBarFuncAddress = getModuleHandle('samp.dll') + 0x689C0 -- Address for CPlayerTags::DrawHealthBar
-- CPlayerTagsDrawHealthBar = hook.new( -- 'void(__thiscall *)(void*, CVector*, float, float, float)', -- CPlayerTagsDrawHealthBarHook, -- drawHealthBarFuncAddress -- ) -- print"hoke"-- end
local getBonePositionFunc = ffi.cast("int (__thiscall*)(void*, float*, int, bool)", 0x5E4280) local textId = 2014
-- Получить координаты части тела local function getBodyPartCoordinates(boneId, pedHandle) local pedPointer = getCharPointer(pedHandle) local coordinates = ffi.new("float[3]") getBonePositionFunc(ffi.cast("void*", pedPointer), coordinates, boneId, true) return coordinates[0], coordinates[1], coordinates[2] end
-- Define the hook function for CPlayerTags::DrawHealthBar function CPlayerTagsDrawHealthBarHook(this, pPosition, fHealth, fArmour, fDistanceToCamera) -- Custom logic can be inserted here print("Inside DrawHealthBar hook") print("Position: ", pPosition.x, pPosition.y, pPosition.z) print("Health: ", fHealth) print("Armour: ", this) print("Distance to Camera: ", fDistanceToCamera)
- Assistant
To call a function directly using
ffiin Lua without setting up hooks, you can define the function signature withffi.cdefand cast it usingffi.cast. Here's how you can achieve that with the given C++ function: - User
во первых, не надо вызывать мейн. во вторых, getModuleHandle('samp.dll') + 0x689C0 а не просто 0x689C0 . в третьих. почему то не работает
- Assistant
- User
-- Assume this is defined elsewhere in your environment -- If not, you should define it according to your FFI binding requirements local function getModuleHandle(moduleName) return ffi.C.GetModuleHandleA(moduleName) end
-- Define the function signature for CPlayerTags::DrawHealthBar ffi.cdef[[ void __thiscall CPlayerTags_DrawHealthBar(void* this_ptr, CVector* pPosition, float fHealth, float fArmour, float fDistanceToCamera); ]]
-- Function to call CPlayerTags::DrawHealthBar function callDrawHealthBar(pPosition, fHealth, fArmour, fDistanceToCamera) local sampModuleBase = getModuleHandle('samp.dll') if sampModuleBase == nil then print("Failed to get module handle for samp.dll") return end
local drawHealthBarFuncAddress = sampModuleBase + 0x689C0 local drawHealthBarFunc = ffi.cast("void(__thiscall*)(void*, CVector*, float, float, float)", drawHealthBarFuncAddress) local CPlayerTags = ffi.cast("void*", 0x03e08fa0) -- Call the function drawHealthBarFunc(CPlayerTags, pPosition, fHealth, fArmour, fDistanceToCamera)end
-- Example usage function main() local position = ffi.new("CVector") position.x, position.y, position.z = 0.0, 0.0, 10.0 while true do callDrawHealthBar(position, 100.0, 50.0, 15.0) wait(0) end end
-- Call the example usage -- exampleUsage()
- Assistant
Certainly! Let's refactor the script by consolidating the code into a more streamlined form without separating it into multiple functions. Here's a revised version:
local ffi = require 'ffi' -- Define C structures and necessary function signatures ffi.cdef[[ typedef struct { float x; float y; float z; } CVector; // Windows API function to get module handle uintptr_t GetModuleHandleA(const char*); ]] -- Get the base address of a module local sampModuleBase = ffi.C.GetModuleHandleA('samp.dll') if sampModuleBase == nil then error("Failed to get module handle for samp.dll") end -- Define the function address for CPlayerTags::DrawHealthBar local drawHealthBarFuncAddress = sampModuleBase + 0x689C0 local drawHealthBarFunc = ffi.cast("void(__thiscall*)(void*, CVector*, float, float, float)", drawHealthBarFuncAddress) -- Define the CPlayerTags instance local CPlayerTags = ffi.cast("void*", 0x03e08fa0) -- Main execution loop local position = ffi.new("CVector") position.x, position.y, position.z = 0.0, 0.0, 10.0 while true do drawHealthBarFunc(CPlayerTags, position, 100.0, 50.0, 15.0) wait(0) -- Assuming `wait` is defined elsewhere to pause execution end