mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-11 23:04:52 +09:00
Implement require option for modules
Implement option allowing files to be required from control.lua be specified in modules.json.
This commit is contained in:
1 parent
e89e54ee27
commit
c091dc341e
5 files changed
+45
-13
No files matched your search
@@ -178,6 +178,7 @@ class Instance extends link.Link{
|
||||
dependencies: {},
|
||||
path: modulePath,
|
||||
load: [],
|
||||
require: [],
|
||||
}, module);
|
||||
modules.set(module.name, module);
|
||||
}
|
||||
@@ -204,6 +205,7 @@ class Instance extends link.Link{
|
||||
path: path.join('modules', entry.name),
|
||||
dependencies: {},
|
||||
load: [],
|
||||
require: [],
|
||||
}, module);
|
||||
modules.set(module.name, module);
|
||||
}
|
||||
|
||||
@@ -184,11 +184,9 @@ Modules are primarily used by plugins to inject code into Factorio games
|
||||
with the save patcher, though it's also possible to make stand alone
|
||||
modules that are loaded into the game if you don't need the capabilites
|
||||
of the plugin system. The save patcher puts modules into the `modules`
|
||||
folder of the Factorio save and adds code to `control.lua` to
|
||||
load the modules by requiring it and passing the result of the require
|
||||
call to the `add_lib` function of the event_handler lib. See the
|
||||
section on the [event_handler interface](#event_handler-interface) for a
|
||||
detailed description on how the event_handler lib works.
|
||||
folder of the Factorio save and adds code to `control.lua` to load the
|
||||
module according to the `load` and `require` options to the module.json
|
||||
file.
|
||||
|
||||
Stand alone modules are placed into the modules folder of Clusterio,
|
||||
plugin modules are located in the module folder of the plugin. In
|
||||
@@ -202,6 +200,7 @@ structure:
|
||||
"dependencies": {
|
||||
"foo": ">=0.4.2"
|
||||
},
|
||||
"require": ["bar.lua"],
|
||||
"load": ["foo.lua"]
|
||||
}
|
||||
```
|
||||
@@ -225,12 +224,35 @@ The following entries are supported in the module.json file:
|
||||
supported. The events for dependencies is invoked before the events
|
||||
of the dependent, and starting an instance will fail if the
|
||||
dependencies cannot be satisfied.
|
||||
- `require`:
|
||||
Lua files to require in `control.lua`. This should only really be
|
||||
needed if you want to make a global function available for use in
|
||||
Lua commands. The paths are relative to the module's own folder and
|
||||
should be specified using forward slashes as directory sepparators
|
||||
if it's located in a sub directory in the module.
|
||||
- `load`:
|
||||
Lua files to load with the `event_handler` lib. They should be
|
||||
specified as paths that's relative to the module and uses forward
|
||||
slashes as directory sepparators. Thus foo.lua will require a file
|
||||
by that name and feed the result of that require to the event
|
||||
handler lib.
|
||||
Lua files to load with the `event_handler` lib, the result of
|
||||
requiring the file will be passed to the `add_lib` function of the
|
||||
`event_handler` lib in `control.lua`. See the section on the
|
||||
[event_handler interface](#event_handler-interface) for a detailed
|
||||
description on how the `event_handler` lib works. The paths are
|
||||
relative to the module's own folder and should be specified using
|
||||
forward slashes as directory sepparators if it's located in a sub
|
||||
directory in the module.
|
||||
|
||||
It's recommended to use the new style of defining modules in Lua, as
|
||||
well as avoid the use of global variables as much as possible. This
|
||||
means always declearing your top level variables and functions local and
|
||||
exporting only the things you need in other files. You can require
|
||||
other files in your module by prefixing your require paths with
|
||||
`modules/your_module_name/`. It's also possible to require files from
|
||||
other modules this way too.
|
||||
|
||||
The global variables and data stored in the global table is shared by
|
||||
all Clusterio modules as well as the scenario, so it's important that
|
||||
you use unique names. It's recommended to prefix the global variables
|
||||
and data in the global table that your module has with the name of your
|
||||
module.
|
||||
|
||||
Because modules can be patched into an existing game you cannot rely on
|
||||
the `on_init` callback to be called in Clusterio Modules. Nor can you
|
||||
|
||||
@@ -82,10 +82,13 @@ function generateLoader(patchInfo) {
|
||||
|
||||
for (let module of patchInfo["modules"]) {
|
||||
for (let file of module["files"]) {
|
||||
let requirePath = `modules/${module.name}/${file["path"].slice(0, -4)}`;
|
||||
if (file["load"]) {
|
||||
let requirePath = `modules/${module.name}/${file["path"].slice(0, -4)}`;
|
||||
lines.push('event_handler.add_lib(require("'+requirePath+'"))');
|
||||
}
|
||||
if (file["require"]) {
|
||||
lines.push('require("'+requirePath+'")');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -251,6 +254,7 @@ async function patch(savePath, modules) {
|
||||
moduleInfo["files"].push({
|
||||
"path": relativePath,
|
||||
"load": module.load.includes(relativePath),
|
||||
"require": module.require.includes(relativePath),
|
||||
});
|
||||
|
||||
} else if (entry.isDirectory()) {
|
||||
|
||||
@@ -27,12 +27,14 @@ describe("Integration of lib/factorio/patch", function() {
|
||||
dependencies: {},
|
||||
path: "test/file/modules/test",
|
||||
load: [],
|
||||
require: [],
|
||||
}, {
|
||||
name: "subdir",
|
||||
version: "1.0.0",
|
||||
dependencies: {},
|
||||
path: "test/file/modules/subdir",
|
||||
load: [],
|
||||
require: [],
|
||||
}]);
|
||||
|
||||
let zip = await jszip.loadAsync(await fs.readFile(savePath));
|
||||
|
||||
@@ -60,6 +60,7 @@ describe("lib/factorio", function() {
|
||||
'',
|
||||
'-- Clusterio modules',
|
||||
'event_handler.add_lib(require("modules/spam/bar"))',
|
||||
'require("modules/spam/spam")',
|
||||
'',
|
||||
].join('\n');
|
||||
let patchInfo = {
|
||||
@@ -67,8 +68,9 @@ describe("lib/factorio", function() {
|
||||
"modules": [{
|
||||
"name": "spam",
|
||||
"files": [
|
||||
{ "path": "bar.lua", "load": true },
|
||||
{ "path": "excluded.lua", "load": false },
|
||||
{ "path": "bar.lua", "load": true, "require": false },
|
||||
{ "path": "spam.lua", "load": false, "require": true },
|
||||
{ "path": "excluded.lua", "load": false, "require": false },
|
||||
],
|
||||
}],
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user