Skip to content

Recommended Loader

89pleasure edited this page Jul 16, 2026 · 2 revisions

Recommended Loader

Include this file as Scripts/pleasure_lib_loader.lua in every consuming mod. It makes loading independent of mods.txt order.

The loader tries, in order:

  1. _G.RequirePleasureLib, when the shared mod already loaded
  2. _G.PleasureLib, when the global library already exists
  3. require("pleasure_lib")
  4. The neighboring PleasureLib/Scripts/pleasure_lib.lua file
local Loader = {}

local function script_directory()
    local ok, info = pcall(function()
        return debug.getinfo(1, "S")
    end)
    if not ok or not info or not info.source then return nil end

    local source = tostring(info.source)
    if source:sub(1, 1) == "@" then source = source:sub(2) end
    return source:match("^(.*[\\/])[^\\/]*$")
end

function Loader.load()
    if type(_G.RequirePleasureLib) == "function" then
        local lib = _G.RequirePleasureLib()
        if type(lib) == "table" then return lib end
    end
    if type(_G.PleasureLib) == "table" then return _G.PleasureLib end

    local ok, lib = pcall(require, "pleasure_lib")
    if ok and type(lib) == "table" then return lib end

    local dir = script_directory()
    if dir then
        ok, lib = pcall(dofile,
            dir .. "..\\..\\PleasureLib\\Scripts\\pleasure_lib.lua")
        if ok and type(lib) == "table" then return lib end
    end
    return nil
end

function Loader.load_or_log(mod_name)
    local lib = Loader.load()
    if type(lib) ~= "table" then
        print("[" .. tostring(mod_name or "Mod")
            .. "] PleasureLib is required but could not be loaded.\n")
        return nil
    end
    return lib
end

function Loader.new(mod_name, options)
    local lib = Loader.load_or_log(mod_name)
    if type(lib) ~= "table" then return nil end

    options = options or {}
    options.mod = options.mod or mod_name
    return lib.new(options)
end

return Loader

Use it from main.lua:

local pleasureLib = require("pleasure_lib_loader").new("MyMod")
if type(pleasureLib) ~= "table" then return end

The early return is important: it prevents later mod code from failing when the dependency is missing or installed in the wrong directory.

Clone this wiki locally