Skip to content

Ffi

FFI

The FFI library allows calling external C functions and using C data structures from pure Lua code.

Examples: https://luajit.org/ext_ffi.html

Examples with Moonlight

WinAPI Test (disabled/ffi_test.lua)

-- Adding the FFI library.
local ffi = require( "ffi" )

-- Now declaring our static Lua script.
local ffi_test = {}

ffi.cdef[[
    int MessageBoxA(void *w, const char *txt, const char *cap, int type);
    int printf(const char *fmt, ...);
]]

function ffi_test.PostInitialize()

    -- Debug. Make MessageBox/Pop-up Show up. This
    ffi.C.MessageBoxA(nil, "Hey! This is a test.", "fantasy.cat", 0)
    ffi.C.printf("Hello. This is a fantasy.cat test from %s\n", "typedef")
end

return ffi_test

IsTakingScreenshot (disabled/hmm.lua)

-- hmm...
local ffi = require( "ffi" )

ffi.cdef[[
    typedef bool (__thiscall *is_taking_screenshot)(void*);
]]

local hmm = {
    engine = 0
}

function hmm.PostInitialize()
    hmm.engine = moonlight.game.create_interface( "interface_engine_client" )
end

function hmm.OnEndScene( device )
    -- PostInitialize not called yet. Wait before we do this.
    if hmm.engine ~= 0 then

        -- *(is_taking_screenshot **) - void***
        local pointer_prefix = ffi.typeof('void***')
        local IVEngineClient = ffi.cast( pointer_prefix, hmm.engine )

        -- https://github.com/ValveSoftware/source-sdk-2013/blob/0d8dceea4310fde5706b3ce1c70609d72a38efdf/mp/src/public/cdll_int.h#L425
        local IsTakingScreenshot = ffi.cast('is_taking_screenshot', IVEngineClient[0][92] )

        if IsTakingScreenshot( IVEngineClient ) == true then
            moonlight.log("Hmm..\n")
        end  
    end
end

return hmm