diff --git a/doc/README.md b/doc/README.md new file mode 100644 index 0000000..47f41a8 --- /dev/null +++ b/doc/README.md @@ -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. \ No newline at end of file diff --git a/doc/modules.md b/doc/modules.md new file mode 100644 index 0000000..d7c31c8 --- /dev/null +++ b/doc/modules.md @@ -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()` + +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()` + +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()` + +Passes name to `load_module` if the mod was not disabled in `modules.conf`. For further documentation, see `load_module`. \ No newline at end of file diff --git a/init.lua b/init.lua index 0b4d73b..cb25a74 100644 --- a/init.lua +++ b/init.lua @@ -13,8 +13,50 @@ end -- Load API dofile(modpath.."/api.lua") --- Load storage devices -dofile(modpath.."/storage.lua") +------------------- +----- MODULES ----- +------------------- --- Load machines -dofile(modpath.."/machines.lua") +local loaded_modules = {} + +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 diff --git a/modules.conf b/modules.conf new file mode 100644 index 0000000..7716aa4 --- /dev/null +++ b/modules.conf @@ -0,0 +1 @@ +storage = true diff --git a/modules/storage/init.lua b/modules/storage/init.lua new file mode 100644 index 0000000..c43490b --- /dev/null +++ b/modules/storage/init.lua @@ -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") diff --git a/machines.lua b/modules/storage/machines.lua similarity index 100% rename from machines.lua rename to modules/storage/machines.lua diff --git a/storage.lua b/modules/storage/storage.lua similarity index 100% rename from storage.lua rename to modules/storage/storage.lua