"Modulize"

Allows modules to be added to the mod for easier development and management. See doc/modules.md.
This commit is contained in:
octacian 2017-02-14 19:35:06 -08:00
parent 888367b4f6
commit 69771ca5fc
7 changed files with 96 additions and 4 deletions

5
doc/README.md Normal file
View File

@ -0,0 +1,5 @@
# Documentation
The APIs provided by MicroExpansion are divided among several different files. Unless otherwise mentioned, the `.md` documentation file is labeled the same as the Lua file containing the code. However, for modules, documentation is found in a subdirectory. Below, the main documentation sections are found before being divided depending on the module.
### `modules.md`
Non-API portions of MicroExpansion are loaded as modules to allow them to be easily enabled or disabled. This documents the API for loading, configuring, and interacting with modules.

35
doc/modules.md Normal file
View File

@ -0,0 +1,35 @@
# Modules
Non-API portions of MicroExpansion are loaded as modules to allow them to be easily enabled or disabled. Modules can be manually loaded or required from the API or from another module. Specific modules can be disabled using `modules.conf`, as documented below.
## Managing Modules
Modules listed in the configuration file are automatically loaded at startup unless specifically disabled. For the purpose of listing and/or disabling mods, we've introduced the `modules.conf` file.
Each module is listed on a new line, as if setting a variable. A module can be disabled or enabled by setting this variable to `true` or `false`. If a module is not listed here, or is set to `false` (disabled), it will not be automatically loaded.
__Example:__
```lua
-- Enabled:
storage = true
-- Disabled:
storage = false
```
A small API is provided allowing modules to be loaded from another module or from the main API. A module can be force loaded (overrides configuration), or can be loaded with the configuration in mind.
## Module API
Modules are places a subdirectories of the `modules` directory. Each module must have the same name as its reference in the configuration file. Modules must have an `init.lua` file, where you can load other portions of the module with `dofile`, or use the API documented below.
#### `get_module_path(name)`
__Usage:__ `microexpansion.get_module_path(<module name (string)>)`
Returns the full path of the module or `nil` if it does not exist. This can be used to check for another module, or to easily access the path of the current module in preparation to load other files with `dofile` or the likes.
#### `load_module(name)`
__Usage:__ `microexpansion.load_module(<module name (string)>)`
Attempts to load a module. If the module path is `nil`, `nil` is returned to indicate that the module does not exist. Otherwise, a return value of `true` indicates a success or that the module has already been loaded. __Note:__ this function overrides any settings in `modules.conf`, meaning that it will be loaded even if it was disabled. For general use cases, use `require_module` instead.
#### `require_module(name)`
__Usage:__ `microexpansion.require_module(<module name (string)>)`
Passes name to `load_module` if the mod was not disabled in `modules.conf`. For further documentation, see `load_module`.

View File

@ -13,8 +13,50 @@ end
-- Load API -- Load API
dofile(modpath.."/api.lua") dofile(modpath.."/api.lua")
-- Load storage devices -------------------
dofile(modpath.."/storage.lua") ----- MODULES -----
-------------------
-- Load machines local loaded_modules = {}
dofile(modpath.."/machines.lua")
local settings = Settings(modpath.."/modules.conf"):to_table()
-- [function] Get module path
function microexpansion.get_module_path(name)
local module_path = modpath.."/modules/"..name
if io.open(module_path.."/init.lua") then
return module_path
end
end
-- [function] Load module (overrides modules.conf)
function microexpansion.load_module(name)
if loaded_modules[name] ~= false then
local module_init = microexpansion.get_module_path(name).."/init.lua"
if module_init then
dofile(module_init)
loaded_modules[name] = true
return true
else
microexpansion.log("Invalid module \""..name.."\". The module either does not exist "..
"or is missing an init.lua file.", "error")
end
else
return true
end
end
-- [function] Require module (does not override modules.conf)
function microexpansion.require_module(name)
if settings[name] and settings[name] ~= false then
return microexpansion.load_module(name)
end
end
for name,enabled in pairs(settings) do
if enabled ~= false then
microexpansion.load_module(name)
end
end

1
modules.conf Normal file
View File

@ -0,0 +1 @@
storage = true

9
modules/storage/init.lua Normal file
View File

@ -0,0 +1,9 @@
-- storage/init.lua
local module_path = microexpansion.get_module_path("storage")
-- Load storage devices
dofile(module_path.."/storage.lua")
-- Load machines
dofile(module_path.."/machines.lua")