Skip to content

Mod API

Noah edited this page Aug 24, 2023 · 1 revision

This page describes the Mod-API, which is exposed as a global variable to all mods.

Logger

This namespace holds all functions related to logging.

warn

Emits a warning message.

Example (init.lua)
function ready()
  mod_api.logger.warn("Some warning!")
end

info

Emits an info message.

error

Emits an error message, also implicitly includes the mod name of the callee.

debug

Emits an debug message, also implicitly includes the mod name of the callee.

logs

Returns all log-entries of the current session that are of type warning or higher.

Example (init.lua)
function ready()
  local logs = mod_api.logger.logs()

  for k, v in pairs(logs) do
    local message = v:message()
    local level = v:level() -- 3 = warning, 4 = error, critical = 5
  end
end

Mods

This namespace contains functions related to loaded mods.

all

Returns all loaded mods, regardless of whether they're enabled or not.

enabled

Returns all enabled mods.

Example (init.lua)
function ready()
  local mods = mod_api.mods.enabled()

  for k, v in pairs(mods) do
    local name = v:name()
    local author = v:author()
    local version = v:version()
    local requires_restart = v:requires_restart()    
    local description = v:description()
    local detailed_description = v:detailed_description()
    local dependencies = v:dependencies()
    local enabled = v:enabled()

    -- Disable mod
    v:enable(false)

    -- Enable mod
    v:enable(true)
  end
end

Hooks

This namespace contains functions useful for modding.

detour

Used to hook a function call.

Example
local Module = {}

function Module:give_number(n)
  return n + 10
end

local restore = mod_api.hooks.detour(Module, "give_number", function(original, self, n)
  return original(self, n - 10)
end)

print(Module:give_number(0)) -- Returns 0
restore()
print(Module:give_number(0)) -- Returns 10

intercept_require

Given a module and a callback, intercepts all require calls to the given module and invokes the callback with the value returned by the original require.

Game Code Example (init.lua)
local menu_builder = require("classes.modules.interface.menuBuilder")

-- do something with menu_builder
local remove_hook = mod_api.hooks.intercept_require("classes.modules.interface.menuBuilder", function(module)
  mod_api.hooks.detour(module, "some_func", ...)
end)

function unload()
  remove_hook()
end
Clone this wiki locally