LUA snippets

Tuzas

Active member
Joined
Nov 1, 2019
Messages
150
Solutions
1
Reaction score
85
Location
null
[R5] Check if chat is active
PHP:
function chat()
samp10 = getModuleHandle('samp.dll')
samp10 = samp10 + 0x26EB84
samp10 = readMemory(samp10, 4, false)
samp10 = samp10 + 0x14E0
samp10 = readMemory(samp10, 4, false)
if samp10 == 0 then
    return true
else
    return false
end
end
[R5] Check if dialog is active
PHP:
function dialog()
    samp10 = getModuleHandle('samp.dll')
    samp10 = samp10 + 0x26EB50
    samp10 = readMemory(samp10, 4, false)
    samp10 = samp10 + 0x28
    samp10 = readMemory(samp10, 4, false)
    if samp10 == 0 then
        return true
    else
        return false
    end
    end
More snippets coming soon..
 

Tuzas

Active member
Joined
Nov 1, 2019
Messages
150
Solutions
1
Reaction score
85
Location
null
Thanks for the original snippets @Parazitas
Get samp version id
PHP:
require 'lib.moonloader'

function get_samp_version_id()
    local versionid = 0
    samp10 = getModuleHandle('samp.dll')
    samp10 = samp10 + 0x128
    samp9 = readMemory(samp10, 4, true)
    if samp9 == 0x5542F47A then
        versionid = 1 -- r1
    end
    if samp9 == 0x59C30C94 then
        versionid = 2 -- r2
    end
    if samp9 == 0x5542F47A then
        versionid = 3 -- 0.3DL
    end

    samp10 = samp10 - 8
    samp9 = readMemory(samp10, 4, true)
    if samp9 == 0x5C0B4243 then
        versionid = 4 -- r3
    end
    if samp9 == 0x5DD606CD then
        versionid = 5 -- R4
    end
    if samp9 == 0x6094ACAB then
        versionid = 6 -- R4-2
   end
        if samp9 == 0x6372C39E then
            versionid = 7 --R5
        end
   return versionid
    end
 

Tuzas

Active member
Joined
Nov 1, 2019
Messages
150
Solutions
1
Reaction score
85
Location
null
[R5] Add message to the chat, thanks for the help @SIGKILL
PHP:
    function ChatMsg(text)
        --[[
            ChatMsg("{FFFF00}Hello!")
        --]]
        local samp10 = getModuleHandle('samp.dll')
        local samp_chat_info_offset = samp10 + 0x26EB80
        local ADDR_FUNC_ADD_MSG = samp10 + 0x68070
        local pChat = readMemory(samp_chat_info_offset, 4, false)
        local func = ffi.cast("void(__cdecl *)(void *, char *)", ADDR_FUNC_ADD_MSG)
        if func ~= 0 then
            local chatPointer = ffi.cast("void*", pChat)
            local c_str = ffi.new("char[?]", #text + 1)
            ffi.copy(c_str, text)
            func(chatPointer, c_str)
            end
            return true
        end
 

Tuzas

Active member
Joined
Nov 1, 2019
Messages
150
Solutions
1
Reaction score
85
Location
null
[R5] Send server command
PHP:
function SendCommand(text)
    --[[
       0.3.7 - R5
        SendCommand('/hello')
    ]]
    local samp10 = getModuleHandle('samp.dll')
    local ADDR_FUNC_SEND_COMMAND = samp10 + 0x69900
    local send = ffi.cast("void (__stdcall*)(char*)", ADDR_FUNC_SEND_COMMAND)
    if send ~= 0 then
        local c_str = ffi.new("char[?]", #text + 1)
        ffi.copy(c_str, text)
        send(c_str)
    end
end
 

Tuzas

Active member
Joined
Nov 1, 2019
Messages
150
Solutions
1
Reaction score
85
Location
null
[R5] Set current dialog list item
PHP:
function SetDialogList(item)
    --[[
        0.3.7 - R5
        SetDialogList(5)
    ]]
    local samp10 = getModuleHandle('samp.dll')
    local samp_dialog_info_offset = samp10 + 0x26EB50
    samp_dialog_info_offset = readMemory(samp_dialog_info_offset, 4, false)
    local samp_dialog_list_box_offset = samp_dialog_info_offset + 0x20
    samp_dialog_list_box_offset = readMemory(samp_dialog_list_box_offset, 4, false)
    local samp_set_dialog_list_item_offset = samp10 + 0x8A9F0
    local setdialog = ffi.cast("void(__thiscall*)(void*, int)", samp_set_dialog_list_item_offset)
    if setdialog ~= 0 then
        local dialogPointer = ffi.cast("void*", samp_dialog_list_box_offset)
        local c_item = ffi.new("int", item)
        setdialog(dialogPointer, c_item)
    end
end
 

Tuzas

Active member
Joined
Nov 1, 2019
Messages
150
Solutions
1
Reaction score
85
Location
null
[R5] Show or hide the dialog | Enable or disable cursor, thanks to @SobFoX for CURSOR offsets.
PHP:
function enableDialog(bool)
    SAMP_DIALOG_INFO_PTR = getModuleHandle('samp.dll') + 0x26EB50
    SAMP_DIALOG_INFO_PTR = readMemory(SAMP_DIALOG_INFO_PTR, 4, false)
    SAMP_DIALOG_INFO_PTR = SAMP_DIALOG_INFO_PTR + 0x28
    memory.setint32(SAMP_DIALOG_INFO_PTR, bool and 1 or 0, false)
    sampToggleCursor(bool)
end

function sampToggleCursor(bool)
    SAMP_MISC_INFO = 0x26EBAC
    SAMP_FUNC_TOGGLECURSOR = 0xA06F0
    SAMP_FUNC_CURSORUNLOCKACTORCAM = 0xA05D0
    samp10 = getModuleHandle('samp.dll')
    SAMP_MISC_INFO_PTR = memory.getint32(samp10 + SAMP_MISC_INFO)
    if type(bool) ~= 'boolean' then
        bool = false
    end
    local obj = ffi.cast('void**', SAMP_MISC_INFO_PTR)
    local show = ffi.cast('void (__thiscall*)(void*, int, bool)', samp10 + SAMP_FUNC_TOGGLECURSOR)
    show(obj, bool and 3 or 0, bool)
    if not bool then
        ffi.cast('void (__thiscall*)(void*)', samp10 + SAMP_FUNC_CURSORUNLOCKACTORCAM)(obj)
    end
end
 

Tuzas

Active member
Joined
Nov 1, 2019
Messages
150
Solutions
1
Reaction score
85
Location
null
[ANY VERSION] Read string from memory
PHP:
function readString(address, size)
    local result = ''
    for i = address, address + size - 1 do
        local char = string.char(readMemory(i, 1))
        if char == '\0' then
            break
        end
        result = result .. char
    end
    return result
end
 

Tuzas

Active member
Joined
Nov 1, 2019
Messages
150
Solutions
1
Reaction score
85
Location
null
[ANY VERSION] Remove dot from numbers. (5.000 - - > 5000)
PHP:
function convert(str)
    if string.find(str, "%.") then
        local result = string.gsub(str, "%.", "")
        return tonumber(result)
    else
        return tonumber(str)
    end
end
 

Tuzas

Active member
Joined
Nov 1, 2019
Messages
150
Solutions
1
Reaction score
85
Location
null
P.S These snippets haven't been checked throughly, so if you encounter a problem, feel free to message me on discord.
 

Tuzas

Active member
Joined
Nov 1, 2019
Messages
150
Solutions
1
Reaction score
85
Location
null
[ANY VERSION] Fake AFK
PHP:
function desync(bool)
    local CHAR_PTR = getCharPointer(PLAYER_PED)
    CHAR_PTR = CHAR_PTR + 1328
    if bool == true then
        writeMemory(CHAR_PTR, 4, 50, false)
    else
        writeMemory(CHAR_PTR, 4, 1, false)
    end
end
 

Tuzas

Active member
Joined
Nov 1, 2019
Messages
150
Solutions
1
Reaction score
85
Location
null
[ANY VERSION] Get random value from table
PHP:
function RandomValue(table)
  local randomIndex = math.random(#table)
  return table[randomIndex]
end
 

Tuzas

Active member
Joined
Nov 1, 2019
Messages
150
Solutions
1
Reaction score
85
Location
null
[R5] Remove SAMP /fontsize and etc. arg limits
PHP:
function NoFontLimit()
    samp = getModuleHandle('samp.dll')
    samp10 = samp + 0x686A0
    writeMemory(samp10, 2, 0x9090, true)
    --
    samp9 = samp + 0x6869B
    writeMemory(samp9, 2, 0x9090, true)
end

function NoPageLimit()
    samp = getModuleHandle('samp.dll')
    samp10 = samp + 0x6861D
    writeMemory(samp10, 2, 0x9090, true)
    samp9 = samp + 0x68622
    writeMemory(samp9, 2, 0x9090, true)
end

function NoFpsLimit()
    samp = getModuleHandle('samp.dll')
    samp = samp + 0x68902
    writeMemory(samp, 2, 0x9090, true)
end
 

Tuzas

Active member
Joined
Nov 1, 2019
Messages
150
Solutions
1
Reaction score
85
Location
null
[R5] Enable / Disable SAMP Chat input
JavaScript:
function sampSetChatInputEnabled(bool)
--[[
  sampSetChatInputEnabled(true)
]]
    local SAMP_DLL = getModuleHandle('samp.dll')
    local SAMP_CHAT_INPUT_INFO_OFFSET = SAMP_DLL + 0x26EB84
    local SAMP_CHAT_INPUT_INFO_PTR = readMemory(SAMP_CHAT_INPUT_INFO_OFFSET, 4, false)
    if bool == true then
        SAMP_CHAT_INPUT_INFO_OFFSET = SAMP_DLL + 0x69480
    else
        SAMP_CHAT_INPUT_INFO_OFFSET = SAMP_DLL + 0x69580
        local func = ffi.cast('void(__fastcall*)(int)', SAMP_CHAT_INPUT_INFO_OFFSET)
        if func ~= 0 then
            local obj = ffi.cast('int', SAMP_CHAT_INPUT_INFO_PTR)
            func(obj)

        end
    end
 

Tuzas

Active member
Joined
Nov 1, 2019
Messages
150
Solutions
1
Reaction score
85
Location
null
[R5] Return player nickname from id
JavaScript:
local memory = require 'memory'

function FUN_100175c0(param_1_00, param_1)
--[[
first param - SAMP_PPOOL_PLAYER_PTR
second param - PLAYER_ID
]]
    if param_1 == memory.getuint16(param_1_00 + 4, false) then
        if memory.getuint32(param_1_00 + 0x1e, false) > 0xf then
            return readString(memory.getint32(param_1_00 + 10, false), 32, false)
        end
        return readString(param_1_00 + 10, 32, false)
    end

    local addr = memory.getint32(param_1_00 + 0x1f8a + param_1 * 4, false)
    if param_1 < 0x3ed and addr ~= 0 then
        if memory.getuint32(addr + 0x2c, false) > 0xf then
            return readString(memory.getint32(addr + 0x18, false), 32, false)
        end
        return readString(addr + 0x18, 32, false)
    end

    return ""
end
 

Tuzas

Active member
Joined
Nov 1, 2019
Messages
150
Solutions
1
Reaction score
85
Location
null
[R5] Set Dialog Input
JavaScript:
function SetDialogInput(input, button)
    local samp = getModuleHandle('samp.dll')
    local SAMP_DIALOG_INFO_OFFSET = samp + 0x26EB50
    local pDialog = readMemory(SAMP_DIALOG_INFO_OFFSET, 4, false)
    local pDialogpEditBox = pDialog + 0x24
    local pEditBox = readMemory(pDialogpEditBox, 4, false)
    local CDXUTEditBox = samp + 0x85580
    local SAMP_DIALOG_CLOSE_OFFSET = samp + 0x70630
    local func = ffi.cast('void (__thiscall*)(void *, char *)', CDXUTEditBox)
    if func ~= nil then
        local p1 = ffi.cast('void *', pEditBox)
        local p3 = ffi.new("char[?]", #input + 1)
        ffi.copy(p3, input)
        func(p1, p3)
        local funcc = ffi.cast('void (__thiscall*)(void *, char *)', SAMP_DIALOG_CLOSE_OFFSET)
        local p4 = ffi.cast('void *', pDialog)
        local buttonBuf = ffi.new('char[?]', #button + 1)
        ffi.copy(buttonBuf, button)
        funcc(p4, buttonBuf)

    end
end
 

Tuzas

Active member
Joined
Nov 1, 2019
Messages
150
Solutions
1
Reaction score
85
Location
null
[R5] Some lua functions i used in my scripts, didn't really bother to tidy them up, may it be as example, of how functions decompiled with ghidra / ida could look like in Lua
JavaScript:
local memory = require 'memory'
--translated to lua by tuzas:)
--check if player is online by id ( FUN_100010b0 )
--first argument - pointer, second - int ( id )

--get player nickname by id ( FUN_100175c0 )
--first argument -- pointer, second -- int ( id )


function FUN_100010b0(param_1_00, param_1)

    if (1003 < param_1) then
        return 0;
    end
    status = memory.getuint32(param_1_00 + 0x2a + param_1 * 4, false)
    if status > 0 then
        return true
    else
        return false
    end
end

function FUN_100175c0(param_1_00, param_1)
    if param_1 == memory.getuint16(param_1_00 + 4, false) then
        if memory.getuint32(param_1_00 + 0x1e, false) > 0xf then
            return readString(memory.getint32(param_1_00 + 10, false), 32, false)
        end
        return readString(param_1_00 + 10, 32, false)
    end

    local addr = memory.getint32(param_1_00 + 0x1f8a + param_1 * 4, false)
    if param_1 < 0x3ed and addr ~= 0 then
        if memory.getuint32(addr + 0x2c, false) > 0xf then
            return readString(memory.getint32(addr + 0x18, false), 32, false)
        end
        return readString(addr + 0x18, 32, false)
    end

    return ""
end

function readString(address, size, other)
    local result = ''
    for i = address, address + size - 1 do
        local char = string.char(readMemory(i, 1))
        if char == '\0' then
            break
        end
        result = result .. char
    end
    return result
end
 

Tuzas

Active member
Joined
Nov 1, 2019
Messages
150
Solutions
1
Reaction score
85
Location
null
[ANY VERSION] Check the distance between two points in 3D space.
JavaScript:
--[[
    calculateDistance - Calculates the Euclidean distance between two points in 3D space.

    Parameters:
        x1, y1, z1 (number): Coordinates of the first point.
        x2, y2, z2 (number): Coordinates of the second point.

    Returns:
        number: The Euclidean distance between the two points.
]]
function calculateDistance(x1, y1, z1, x2, y2, z2)
    local dx = x2 - x1
    local dy = y2 - y1
    local dz = z2 - z1

    return math.sqrt(dx^2 + dy^2 + dz^2)
end
 

Tuzas

Active member
Joined
Nov 1, 2019
Messages
150
Solutions
1
Reaction score
85
Location
null
[ANY-VERSION] Process Chat Input

JavaScript:
local offsets = {
    SAMP_CHAT_INPUT_INFO_OFFSET = {
        [1] = 0x21a0e8,-- r1
        [2] = 0x21a0f0,
        [3] = 0x21a0e8,
        [4] = 0x26e8cc,
        [5] = 0x26e9fc,
        [6] = 0x26e9fc,
        [7] = 0x26EB84

    },
    pChatInput = {
        [1] = 0x8,
        [2] = 0x8,
        [3] = 0x8,
        [4] = 0x8,
        [5] = 0x8,
        [6] = 0x8,
        [7] = 0x8

    },
    CDXUTEditBox = {
        [1] = 0x80f60,
        [2] = 0x81000,
        [3] = 0x80f60,
        [4] = 0x84e70,
        [5] = 0x855b0,
        [6] = 0x0855e0,
        [7] = 0x85580

    },
    PROCESS_INPUT = {
        [1] = 0x65d30, -- r1
        [2] = 0x65e00, -- r2
        [3] = 0x65d30, -- 0.3dl
        [4] = 0x69260, -- r3
        [5] = 0x69990, -- r4
        [6] = 0x699d0, --r42
        [7] = 0x699D0 -- r5
    }
}

function getSampVersion()
    local version = 0
    local samp = getModuleHandle('samp.dll') + 0x128
    local samp1 = readMemory(samp, 4, true)

    if samp1 == 0x5542F47A then
        version = 1
    end
    if samp1 == 0x59C30C94 then
        version = 2
    end
    if samp1 == 0x5A6A3130 then
        version = 3
    end

    samp = samp - 8
    samp1 = readMemory(samp, 4, true)

    if samp1 == 0x5C0B4243 then
        version = 4
    end
    if samp1 == 0x5DD606CD then
        version = 5
    end
    if samp1 == 0x6094ACAB then
        version = 6
    end
    if samp1 == 0x6372C39E then
        version = 7
    end

    return version
end

function sampProcessChatInput(text)
    local samp_dll = getModuleHandle('samp.dll')
    local version = getSampVersion()
    local offset_SAMP_CHAT_INPUT_INFO_OFFSET = offsets.SAMP_CHAT_INPUT_INFO_OFFSET[version]
    local offset_pchat = offsets.pChatInput[version]
    local offset_peditbox = offsets.CDXUTEditBox[version]
    local offset_processinput = offsets.PROCESS_INPUT[version]
    local SAMP_CHAT_INPUT_INFO_OFFSET = samp_dll + offset_SAMP_CHAT_INPUT_INFO_OFFSET

    local pChatInput = readMemory(SAMP_CHAT_INPUT_INFO_OFFSET, 4, false)
    local pEditBox0 = pChatInput + offset_pchat
    local pEditBox = readMemory(pEditBox0, 4, false)
    local CDXUTEditBox = samp_dll + offset_peditbox
    local func = ffi.cast("void(__thiscall *)(void *, void*,  char *)", CDXUTEditBox)
    if func ~= 0 then
        local TextPointer = ffi.cast("void*", pEditBox)
        local EditBox = ffi.cast('void*', CDXUTEditBox)
        local c_str = ffi.new("char[?]", #text + 1)
        ffi.copy(c_str, text)
        func(TextPointer, c_str, EditBox)
    end
    local PROCESS_INPUT = samp_dll + offset_processinput
    local funcc = ffi.cast('void(__fastcall *)(void *)', PROCESS_INPUT)
    if funcc ~= 0 then
        local text_ptr = ffi.cast("void*", pChatInput)
        funcc(text_ptr)
    end
end
 
Top