"Modulify"

This introduces a small API allowing different portions of the mod (digicompute) to be divided among sub-mods (modules) in the modules directory. See documentation for further information (modules.md).
master
octacian 2017-01-30 19:40:30 -08:00
parent b155acb3c2
commit 9169209333
7 changed files with 98 additions and 6 deletions

View File

@ -1,6 +1,9 @@
# Documentation
The digicompute API is divided among several different files. Unless otherwise mentioned, the `.md` documentation file is labeled the same as the Lua file containin the code. For example, documentation of all the API functions introduced by `builtin.lua` can be found in `doc/builtin.md`. Below, an overview of each documentation can be found.
## `modules.md`
Non-API portions of digicompute are loaded as modules to allow them to be easily enabled or disabled. This documents the API for loading, configuring, and interacting with modules.
## `builtin.md`
This documents the API introduced by `builtin.lua`. Mostly containing file interaction APIs, __builtin__ contains functions that don't fit elsewhere.

35
doc/modules.md Normal file
View File

@ -0,0 +1,35 @@
# Modules
Non-API portions of digicompute 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:
computers = true
-- Disabled:
computers = 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:__ `digicompute.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:__ `digicompute.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:__ `digicompute.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

@ -24,8 +24,53 @@ digicompute.builtin.mkdir(digicompute.path)
-- Load environment utilities
dofile(modpath.."/env.lua")
-- Load API-like resources
dofile(modpath.."/c_api.lua") -- Computer API
-------------------
----- MODULES -----
-------------------
-- Load registration code
dofile(modpath.."/computers.lua") -- Computers
local loaded_modules = {}
local settings = Settings(modpath.."/modules.conf"):to_table()
-- [function] Get module path
function digicompute.get_module_path(name)
local module_path = modpath.."/modules/"..name
if digicompute.builtin.exists(module_path) then
return module_path
end
end
-- [function] Load module (overrides modules.conf)
function digicompute.load_module(name)
if loaded_modules[name] ~= false then
local module_path = digicompute.get_module_path(name)
if module_path then
if digicompute.builtin.exists(module_path.."/init.lua") then
dofile(module_path.."/init.lua")
loaded_modules[name] = true
return true
else
digicompute.log("Module ("..name..") missing init.lua, could not load", "error")
end
else
digicompute.log("Invalid module \""..name.."\"", "error")
end
else
return true
end
end
-- [function] Require module (does not override modules.conf)
function digicompute.require_module(name)
if settings[name] and settings[name] ~= false then
return digicompute.load_module(name)
end
end
for name,enabled in pairs(settings) do
if enabled ~= false then
digicompute.load_module(name)
end
end

1
modules.conf Normal file
View File

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

View File

@ -1,4 +1,4 @@
-- digicompute/c_api.lua
-- computers/api.lua
digicompute.c = {}

View File

@ -0,0 +1,8 @@
-- computers/init.lua
local module_path = digicompute.get_module_path("computers")
-- Load API
dofile(module_path.."/api.lua")
-- Load nodes (computers)
dofile(module_path.."/nodes.lua")

View File

@ -1,4 +1,4 @@
-- digicompute/computers.lua
-- computers/nodes.lua
digicompute.register_computer("default", {
description = "digicomputer",