diff --git a/client.js b/client.js index 09c12488..26841776 100644 --- a/client.js +++ b/client.js @@ -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); } diff --git a/docs/developing-for-clusterio.md b/docs/developing-for-clusterio.md index 41e9ac5f..31aaafc5 100644 --- a/docs/developing-for-clusterio.md +++ b/docs/developing-for-clusterio.md @@ -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 diff --git a/lib/factorio/patch.js b/lib/factorio/patch.js index 93c51bc0..0940a932 100644 --- a/lib/factorio/patch.js +++ b/lib/factorio/patch.js @@ -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()) { diff --git a/test/integration/patch.js b/test/integration/patch.js index 8fc96c23..2d342ffd 100644 --- a/test/integration/patch.js +++ b/test/integration/patch.js @@ -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)); diff --git a/test/lib/factorio/patch.js b/test/lib/factorio/patch.js index 75f07553..bcbf2dbe 100644 --- a/test/lib/factorio/patch.js +++ b/test/lib/factorio/patch.js @@ -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 }, ], }], };