mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-19 15:33:12 +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: {},
|
dependencies: {},
|
||||||
path: modulePath,
|
path: modulePath,
|
||||||
load: [],
|
load: [],
|
||||||
|
require: [],
|
||||||
}, module);
|
}, module);
|
||||||
modules.set(module.name, module);
|
modules.set(module.name, module);
|
||||||
}
|
}
|
||||||
@@ -204,6 +205,7 @@ class Instance extends link.Link{
|
|||||||
path: path.join('modules', entry.name),
|
path: path.join('modules', entry.name),
|
||||||
dependencies: {},
|
dependencies: {},
|
||||||
load: [],
|
load: [],
|
||||||
|
require: [],
|
||||||
}, module);
|
}, module);
|
||||||
modules.set(module.name, 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
|
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
|
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`
|
of the plugin system. The save patcher puts modules into the `modules`
|
||||||
folder of the Factorio save and adds code to `control.lua` to
|
folder of the Factorio save and adds code to `control.lua` to load the
|
||||||
load the modules by requiring it and passing the result of the require
|
module according to the `load` and `require` options to the module.json
|
||||||
call to the `add_lib` function of the event_handler lib. See the
|
file.
|
||||||
section on the [event_handler interface](#event_handler-interface) for a
|
|
||||||
detailed description on how the event_handler lib works.
|
|
||||||
|
|
||||||
Stand alone modules are placed into the modules folder of Clusterio,
|
Stand alone modules are placed into the modules folder of Clusterio,
|
||||||
plugin modules are located in the module folder of the plugin. In
|
plugin modules are located in the module folder of the plugin. In
|
||||||
@@ -202,6 +200,7 @@ structure:
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"foo": ">=0.4.2"
|
"foo": ">=0.4.2"
|
||||||
},
|
},
|
||||||
|
"require": ["bar.lua"],
|
||||||
"load": ["foo.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
|
supported. The events for dependencies is invoked before the events
|
||||||
of the dependent, and starting an instance will fail if the
|
of the dependent, and starting an instance will fail if the
|
||||||
dependencies cannot be satisfied.
|
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`:
|
- `load`:
|
||||||
Lua files to load with the `event_handler` lib. They should be
|
Lua files to load with the `event_handler` lib, the result of
|
||||||
specified as paths that's relative to the module and uses forward
|
requiring the file will be passed to the `add_lib` function of the
|
||||||
slashes as directory sepparators. Thus foo.lua will require a file
|
`event_handler` lib in `control.lua`. See the section on the
|
||||||
by that name and feed the result of that require to the event
|
[event_handler interface](#event_handler-interface) for a detailed
|
||||||
handler lib.
|
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
|
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
|
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 module of patchInfo["modules"]) {
|
||||||
for (let file of module["files"]) {
|
for (let file of module["files"]) {
|
||||||
|
let requirePath = `modules/${module.name}/${file["path"].slice(0, -4)}`;
|
||||||
if (file["load"]) {
|
if (file["load"]) {
|
||||||
let requirePath = `modules/${module.name}/${file["path"].slice(0, -4)}`;
|
|
||||||
lines.push('event_handler.add_lib(require("'+requirePath+'"))');
|
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({
|
moduleInfo["files"].push({
|
||||||
"path": relativePath,
|
"path": relativePath,
|
||||||
"load": module.load.includes(relativePath),
|
"load": module.load.includes(relativePath),
|
||||||
|
"require": module.require.includes(relativePath),
|
||||||
});
|
});
|
||||||
|
|
||||||
} else if (entry.isDirectory()) {
|
} else if (entry.isDirectory()) {
|
||||||
|
|||||||
@@ -27,12 +27,14 @@ describe("Integration of lib/factorio/patch", function() {
|
|||||||
dependencies: {},
|
dependencies: {},
|
||||||
path: "test/file/modules/test",
|
path: "test/file/modules/test",
|
||||||
load: [],
|
load: [],
|
||||||
|
require: [],
|
||||||
}, {
|
}, {
|
||||||
name: "subdir",
|
name: "subdir",
|
||||||
version: "1.0.0",
|
version: "1.0.0",
|
||||||
dependencies: {},
|
dependencies: {},
|
||||||
path: "test/file/modules/subdir",
|
path: "test/file/modules/subdir",
|
||||||
load: [],
|
load: [],
|
||||||
|
require: [],
|
||||||
}]);
|
}]);
|
||||||
|
|
||||||
let zip = await jszip.loadAsync(await fs.readFile(savePath));
|
let zip = await jszip.loadAsync(await fs.readFile(savePath));
|
||||||
|
|||||||
@@ -60,6 +60,7 @@ describe("lib/factorio", function() {
|
|||||||
'',
|
'',
|
||||||
'-- Clusterio modules',
|
'-- Clusterio modules',
|
||||||
'event_handler.add_lib(require("modules/spam/bar"))',
|
'event_handler.add_lib(require("modules/spam/bar"))',
|
||||||
|
'require("modules/spam/spam")',
|
||||||
'',
|
'',
|
||||||
].join('\n');
|
].join('\n');
|
||||||
let patchInfo = {
|
let patchInfo = {
|
||||||
@@ -67,8 +68,9 @@ describe("lib/factorio", function() {
|
|||||||
"modules": [{
|
"modules": [{
|
||||||
"name": "spam",
|
"name": "spam",
|
||||||
"files": [
|
"files": [
|
||||||
{ "path": "bar.lua", "load": true },
|
{ "path": "bar.lua", "load": true, "require": false },
|
||||||
{ "path": "excluded.lua", "load": false },
|
{ "path": "spam.lua", "load": false, "require": true },
|
||||||
|
{ "path": "excluded.lua", "load": false, "require": false },
|
||||||
],
|
],
|
||||||
}],
|
}],
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in new issue
Block a user