Module PluginManagerLite
Helper for developing mod-like plugins and softmods.
Module Status: Work in progress.
Usage:
local PluginManager = require('__eradicators-library__/erlib/factorio/PluginManagerLite-1')()
Startup methods
Public.make_asset_getter(plugin_name[, mod_name]) | A function to convert relative asset paths into absolute file paths. |
Generic methods
Public.make_relative_require(plugin_name[, mod_name]) | Creates a require function that works relative to a plugins directory. |
Savedata management
Savedata | The save-load-cycle persistant data of a plugin. |
enable_savedata_management() | Starts the savedata management engine. |
manage_savedata(plugin_name, setter[, default]) | Automatically loads global Savedata and assigns local references. |
get_savedata_default(plugin_name) | Retrieves the previously supplied default table. |
classify_savedata(plugin_name, methods) | Automatically adds meta-methods to Savedata in on_load and on_config. |
manage_version(plugin_name) | Automatically deletes all Savedata when outdated. |
manage_garbage(plugin_name) | Automatically deletes player, force and surface related Savedata when it becomes invalid. |
Startup methods
- Public.make_asset_getter(plugin_name[, mod_name])
-
A function to convert relative asset paths into absolute file paths.
Parameters:
- plugin_name string
- mod_name string Defaults to the name of the mod that called this function. (optional)
Returns:
-
function
f(path)
Usage:
local f = PluginManager.make_asset_getter('my-plugin-name', 'my-mod-name') print(f('/my-file-name.png')) > "__my-mod-name__/assets/my-plugin-name/my-file-name.png"
Generic methods
- Public.make_relative_require(plugin_name[, mod_name])
-
Creates a require function that works relative to a plugins directory.
Parameters:
- plugin_name string
- mod_name string Defaults to the name of the mod that called this function. (optional)
Returns:
-
function
f(path)
The function will call require like this:
require("__my-mod-name__/plugins/my-plugin-name/"..path)
.Usage:
local import = PluginManager.make_relative_require('my-plugin-name', 'my-mod-name') local my_module = import('/my-module-name.lua')
Savedata management
- Savedata
-
The save-load-cycle persistant data of a plugin.
A sub-table of the global table called global, that contains all data created and stored by a single plugin.
- enable_savedata_management()
-
Starts the savedata management engine.
This requires unrestricted exclusive access to
global.plugin_manager
. Do not touch it.Note: Implicitly loads EventManagerLite. Can not be used without.
Note: The other savedata management methods do not exist before calling this. - manage_savedata(plugin_name, setter[, default])
-
Automatically loads global Savedata and assigns local references.
Multiple setters can be associated with the same plugin_name. This allows using local Savedata references in all files used by a plugin.
Can only be used after enable_savedata_management.
Parameters:
- plugin_name string
- setter function Will be called setter(Savedata) in on_load and on_config. (See EventManagerLite.boostrap_event_order regarding on_init.)
- default table The default layout for your Savedata. Use if you want certain subtables to always exist. In on_config any key that doesn't exist in the Savegame yet will be copied from this. This parameter may only be given once for each plugin_name. (optional)
Usage:
local Savedata PluginManager.manage_savedata( 'plugin_name', function(_) Savedata = _ end, {players = {}, forces = {}, surfaces = {}, map = {}} )
- get_savedata_default(plugin_name)
-
Retrieves the previously supplied default table.
Parameters:
- plugin_name string
Returns:
-
table
A reference to the
default
table given to PluginManagerLite.manage_savedata. Changing the table will directly affect the default for this plugin. It is an error if the plugin does not have adefault
table set yet. - classify_savedata(plugin_name, methods)
-
Automatically adds meta-methods to Savedata in on_load and on_config.
This only has to be called once per plugin_name as it affects all setters.
This also enables automatic creation-on-first-access of Savedata sub-tables 'players','forces','surfaces' and 'map' if the methods table has no metatable itself.
Can only be used after enable_savedata_management.
Parameters:
- manage_version(plugin_name)
-
Automatically deletes all Savedata when outdated.
When PluginManagerLite detects that during
on_config
the value ofSavedata.version
in a loaded map is not identical with the value ofdefault.version
then all Savedata is unconditionally deleted anddefault
values are restored.Can only be used after manage_savedata.
Can only be used after enable_savedata_management.Parameters:
- plugin_name string
- manage_garbage(plugin_name)
-
Automatically deletes player, force and surface related
Savedata when it becomes invalid.
Assumes Savedata contains subtables "players", "surfaces" and/or "forces", using the respective objects "index" as keys.
Because it's easy to forget to always subscribe to on_player_removed, on_forces_merged and on_surface_deleted.
Garbage collection is called after all other EventManagerLite event handlers have finished processing the respective event. Note: Be careful when you subscribe to one of the above events youself. If you create new data with the old index it will still be deleted at the end of the event.
Can only be used after enable_savedata_management.
Parameters:
- plugin_name string