Plugin library
Plugin loads plugins and the schema and dispatches hooks to them.
A plugin is a folder with a plugin.yml and a plugin subfolder, or a single .lua file;
the schema is loaded in much the same way and takes part in hooks like one more plugin.
While a plugin is being included the PLUGIN global holds its PluginInstance.
The root of a plugin's plugin folder holds only Lua files of three kinds: its main file
sh_plugin.lua (or sh_<name>.lua, named after the plugin) with cl_plugin.lua and
sv_plugin.lua, the hook handlers in cl_hooks.lua, sh_hooks.lua and sv_hooks.lua,
and the network message handlers in cl_nethooks.lua, sh_nethooks.lua and
sv_nethooks.lua. The loader includes the main file, which includes the others with
require_relative. Everything else lives in extra folders that are included
automatically before the main file, in this order: the libraries in lib, lib/meta and
lib/classes, then models, classes, meta, config, languages, controllers,
views, views/lumen, tools, themes, entities, migrations and the folders added with
Plugin.add_extra, such as commands, items or factions. The files of a folder are
included in alphabetical order, each in the realm its cl_, sh_ or sv_ prefix names,
so a library cannot rely on anything the main file sets up while it loads. The schema has
the same layout in its schema folder, with sh_schema.lua, cl_schema.lua and
sv_schema.lua in place of the plugin files; its main file is included before its extra
folders.
Every function of a registered plugin is put into the hook cache under its name, and
hook.Call is overridden to run those functions before the regular hooks, so defining
function PLUGIN:PlayerSpawn(target) is all it takes to handle a hook. Plugin.call runs
a hook without the gamemode's own handlers, and Plugin.add_hooks registers a table of
handlers that does not belong to a plugin.
Plugins can be disabled. A disabled plugin is not included at all, neither on the server
nor on the clients, and a plugin that depends on it is not loaded either, while the schema
simply goes without it. The list of disabled plugins is read from the 'disabled_plugins'
data file when the server starts and stays the same until it stops:
Plugin.set_disabled, Plugin.disable and Plugin.enable only change what is saved, so
THEIR EFFECT IS DELAYED UNTIL THE SERVER RESTARTS (a change of the map restarts the
gamemode and counts as well). Plugin.is_disabled tells whether a plugin is disabled right
now, Plugin.disabled_on_restart whether it will be after the restart, and
Plugin.known lists every plugin together with both states. Plugins are
referred to by the name they are required by in depends: the name of the folder, or the
file name of a single-file plugin without the realm prefix and the extension (see
Plugin.normalize_id).
Summary
Functions
Plugin.known.Functions
Adds a folder to the list of extra folders that are included for every plugin. A folder that is in the list already is not added again, so it is never included twice.
Parameters
- extra String folder name relative to the plugin's folder, e.g. 'items'
Registers the functions of a table as hook handlers.
local hooks = {}
function hooks:PlayerButtonDown(actor, key)
Cable.send(actor, 'fl_bind_pressed', key)
end
Plugin.add_hooks('FLBinds', hooks)
Parameters
- id String unique ID of this set of hooks
- obj Map table with functions named after hooks
Returns all registered plugins, the schema included.
Returns
- Map plugin path to plugin object
Adds every function of a table to the hook cache under the key it is stored at, so that hook.Call calls it with the table as self.
Parameters
- obj Map table with functions named after hooks, e.g. a plugin object
- id String optional, defaults to
nilID to store with the entries, see Plugin.remove_hooks
Calls a hook and returns what its handlers returned. This function DOES NOT call GM: (gamemode) hooks! It only calls plugin, schema and hook.Add'ed hooks!
Parameters
- name String hook name
- ... Vararg arguments to pass to the handlers
Returns
- Any values returned by the first handler that returned non-nil
Clears the hook and load caches and resets the extra folders, ready for a code refresh. Does nothing before Flux has initialized. On a lite refresh only the entries of plugins whose path does not contain 'flux' are dropped.
Resets the list of extra plugin folders to the defaults.
Forgets which plugins have been loaded, so that they can be included again.
Lists the schema and the plugins that depend on a plugin, by name. Plugins that are saved as disabled do not count.
Parameters
- id String plugin path, folder, ID or dependency name, see Plugin.normalize_id
Returns
- List<String> names of the dependents, the schema's name first; empty if there are none
Disables a plugin. Serverside only. Shorthand for Plugin.set_disabled(id, true, force).
The plugin keeps running until the server restarts or changes the map.
Parameters
- id String plugin path, folder, ID or dependency name, see Plugin.normalize_id
- force Boolean optional, defaults to
falsedisable the plugin even if something depends on it
Returns
- Boolean whether the plugin is saved as disabled
- String language phrase of the error
- List<String> names of the dependents
See also
Checks whether a plugin is saved as disabled, that is whether it will be disabled after
the next restart of the server. Differs from Plugin.is_disabled between a call of
Plugin.set_disabled and the restart. On the client this follows the server.
Parameters
- id String plugin path, folder, ID or dependency name, see Plugin.normalize_id
Returns
- Boolean
Enables a disabled plugin. Serverside only. Shorthand for
Plugin.set_disabled(id, false). The plugin is loaded when the server restarts or
changes the map, provided that the plugins it depends on are enabled as well.
Parameters
- id String plugin path, folder, ID or dependency name, see Plugin.normalize_id
Returns
- Boolean whether the plugin is saved as enabled
- String language phrase of the error
See also
Finds a registered plugin by its path, ID, folder or name.
Parameters
- id String plugin path, ID, folder or name
Returns
- Plugin the plugin
- String the path it is stored under; nothing if not found
Returns the hook cache.
Returns
- Map hook name to a List of entries shaped { callback, object, id = id }
Includes a plugin from a folder or from a single .lua file and registers it. Reads plugin.yml on the server, checks the plugin's environment and dependencies, includes its extra folders and its main file. The PLUGIN global is set while the plugin loads. A disabled plugin is skipped, and so is a plugin with a dependency that is disabled or was not loaded because of its own dependencies.
Parameters
- path String plugin folder or .lua file, relative to the LUA search path
Returns
- Map plugin info, or nil if the plugin was not loaded (already loaded, disabled, wrong environment or missing dependency)
Includes and registers the scripted weapons, entities and effects found in a folder. Looks for 'weapons', 'entities' and 'effects' subfolders, each containing single .lua files or folders with shared.lua, init.lua and cl_init.lua.
Parameters
- folder String path of a plugin's 'entities' folder, without a trailing slash
Includes all extra folders (lib, classes, config, entities, themes and so on) of a plugin or schema. A plugin can take over a folder by returning a non-nil value from the PluginIncludeFolder hook.
Parameters
- folder String plugin or schema folder, without a trailing slash
Includes all plugins inside a folder: single-file plugins first, then plugin folders.
Parameters
- folder String folder relative to the LUA search path, without a trailing slash
Includes the active schema: loads its dependencies, sh_schema.lua, extra folders and plugins, then registers it. Sets the SCHEMA global. On the server the list of disabled plugins is loaded first; a dependency of the schema that is disabled is left out without stopping the schema from loading. Runs the PreLoadPlugins, OnPluginsLoaded and OnSchemaLoaded hooks.
Checks whether a plugin is disabled in this session, that is whether the loader skips it.
This is the state the server has started with: disabling or enabling a plugin does not
change it until the server restarts, see Plugin.disabled_on_restart for the saved state.
Parameters
- id String plugin path, folder, ID or dependency name, see Plugin.normalize_id
Returns
- Boolean
Lists every plugin the loader has come across in this session, loaded or not, with its state. The schema is not on the list, and neither are plugins that nothing has tried to load. Works on both the server and the client, which is what a plugin manager page is meant to be built from. Every entry has the fields:
id: normalized ID, the one to pass toPlugin.set_disabled.name,description,author,version,path,single_file: plugin info. The name falls back to the ID where the plugin does not set one on this realm.depends: List of the names of the plugins and packages it depends on.loaded: whether the server has loaded the plugin in this session.reason: why it was not loaded: 'disabled', 'dependency' (a dependency is disabled or missing) or 'environment' (not meant for this environment); nil if it was loaded.disabled: whether the plugin is saved as disabled, seePlugin.disabled_on_restart.restart_required: true ifdisableddiffers from the state the plugin has in this session, which means that the server has to restart for the change to take effect.required_by: List of the names of the schema and the plugins that depend on it, seePlugin.dependents.schema_dependency: whether the schema depends on it.
Returns
- List<Map> plugin entries sorted by name
Reads the list of disabled plugins from the 'disabled_plugins' data file into Flux.shared.disabled_plugins, from where it reaches the clients. Serverside only. The file is read once per start of the server: later calls, including the ones made when the code is refreshed, return the list that is already in effect.
Returns
- Map normalized plugin ID to true
Checks whether a plugin has already been loaded.
Parameters
- obj Plugin/String plugin object or plugin ID
Returns
- Boolean true if loaded; nil if not; false if obj is neither a table nor a string
Turns a plugin path, folder, file name or dependency name into the ID that the plugin has
on the list of disabled plugins and in Plugin.known. That is the name a plugin is
required by in depends: the name of its folder, or the file name of a single-file
plugin without the realm prefix and the extension.
Plugin.normalize_id('flux/plugins/doors') -- 'doors'
Plugin.normalize_id('flux/plugins/doors/plugin') -- 'doors'
Plugin.normalize_id('flux/plugins/cl_crosshair.lua') -- 'crosshair'
Plugin.normalize_id('crosshair') -- 'crosshair'
Parameters
- id String plugin path, folder, file name or dependency name
Returns
- String normalized ID, or nil if id is not a string or nothing is left of it
Caches the hooks of an existing plugin again. Calls the plugin's on_recache method first, if it has one.
Parameters
- id String plugin path, ID, folder or name
Registers a plugin: caches its hooks, calls its OnPluginLoaded method, stores it under its path and marks it as loaded. On the server this also imports the schema's .yml config when the schema is registered, and shares the info of single-file plugins with clients.
Parameters
- obj Plugin plugin object to register
Removes a plugin entirely: unhooks it and deletes it from the registered plugins. Calls the plugin's on_removed method first, if it has one.
Parameters
- id String plugin path, ID, folder or name
Unhooks a plugin by removing its functions from the hook cache. Calls the plugin's on_unhook method first, if it has one.
Parameters
- id String/Plugin plugin path, ID, folder or name, or the plugin object itself
Removes all hook handlers that have been registered under an ID. The remaining handlers of each hook move up and keep their order, so none of them is skipped when the hook runs.
Parameters
- id String ID that was passed to Plugin.add_hooks
Makes sure that a plugin (or a package) is loaded, including it if it has not been yet. Plugins are looked up in the Flux, cloud and schema plugin folders, single-file plugins by their file name with or without the realm prefix and the extension.
Parameters
- name String plugin or package name
Returns
- Boolean true if the dependency is loaded or could be found; false if it could not, if the plugin is disabled, or if it was left out because one of its own dependencies is disabled or missing
Sends the list of plugins that are saved as disabled to a player, so that their client knows about the changes made since the server has started. Serverside only.
Parameters
- target Player player to send the list to; every player if nil
Disables or enables a plugin and saves the choice. Serverside only.
THE CHANGE TAKES EFFECT AFTER THE SERVER HAS RESTARTED OR CHANGED THE MAP: the plugin
stays loaded (or unloaded) until then, Plugin.is_disabled keeps returning the old
state and Plugin.disabled_on_restart returns the new one. Once it is in effect a
disabled plugin is not included on the server or on the clients, and neither are the
plugins that depend on it.
A plugin that the schema or a plugin that is not disabled depends on is not disabled unless force is true; the names of the dependents are returned instead. The function does not check who is asking: do that before calling it. The new state is sent to all clients and the OnPluginStateChanged hook is run.
local success, phrase, dependents = Plugin.set_disabled('doors', true)
if !success then
actor:notify(phrase, { plugins = table.concat(dependents or {}, ', ') })
end
Parameters
- id String plugin path, folder, ID or dependency name, see Plugin.normalize_id
- disabled Boolean true to disable the plugin, false to enable it
- force Boolean optional, defaults to
falsedisable the plugin even if something depends on it
Returns
- Boolean whether the state is saved as asked
- String language phrase of the error ('error.plugin.unknown' or 'error.plugin.required')
- List<String> names of the dependents if the error is 'error.plugin.required'