mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-16 23:30:41 +09:00
Add create-scenario command to clusteriohost
Creates a scenario directory with the Clusterio modules patched into it so the modules can be used outside of Clusterio. The command reuses the save patching by zipping the scenario, running patch() on it and extracting the result. Defaults to the freeplay scenario from the Factorio install in the host config and all plugins enabled there. Module loading in Instance.prepareSave is moved to patch.loadModules so the command and the instance share it, and findVersion is exported from server.ts so the command can find the freeplay scenario. Zipped scenarios were tried as an output form but Factorio does not pick them up from the scenarios folder, so only directories are written. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
1 parent
7987a5d925
commit
c419f3e9b2
10 files changed
+349
-41
No files matched your search
@@ -91,7 +91,11 @@ export class SaveModule {
|
||||
}
|
||||
|
||||
static async fromPlugin(plugin: BaseInstancePlugin) {
|
||||
let pluginPackagePath = require.resolve(path.posix.join(plugin.info.requirePath, "package.json"));
|
||||
return SaveModule.fromPluginInfo(plugin.info);
|
||||
}
|
||||
|
||||
static async fromPluginInfo(info: lib.PluginNodeEnvInfo) {
|
||||
let pluginPackagePath = require.resolve(path.posix.join(info.requirePath, "package.json"));
|
||||
let moduleDirectory = path.join(path.dirname(pluginPackagePath), "module");
|
||||
try {
|
||||
await fs.access(moduleDirectory);
|
||||
@@ -103,17 +107,17 @@ export class SaveModule {
|
||||
let moduleJson;
|
||||
try {
|
||||
moduleJson = {
|
||||
name: plugin.info.name,
|
||||
version: plugin.info.version,
|
||||
name: info.name,
|
||||
version: info.version,
|
||||
dependencies: { "clusterio": "*" },
|
||||
...JSON.parse(await fs.readFile(moduleJsonPath, "utf8")),
|
||||
};
|
||||
} catch (err: any) {
|
||||
throw new Error(`Loading module/module.json in plugin ${plugin.info.name} failed: ${err.message}`);
|
||||
throw new Error(`Loading module/module.json in plugin ${info.name} failed: ${err.message}`);
|
||||
}
|
||||
if (!lib.ModuleInfo.validate(moduleJson)) {
|
||||
throw new Error(
|
||||
`module/module.json in plugin ${plugin.info.name} failed validation:\n` +
|
||||
`module/module.json in plugin ${info.name} failed validation:\n` +
|
||||
`${JSON.stringify(lib.ModuleInfo.validate.errors, null, "\t")}`
|
||||
);
|
||||
}
|
||||
@@ -149,6 +153,40 @@ export class SaveModule {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the modules to patch into a save
|
||||
*
|
||||
* Collects the modules provided by the given plugins together with the
|
||||
* modules bundled with the host package.
|
||||
*
|
||||
* @param pluginInfos - Plugins to load modules from.
|
||||
* @returns Map of module name to module.
|
||||
*/
|
||||
export async function loadModules(pluginInfos: Iterable<lib.PluginNodeEnvInfo>) {
|
||||
let modules: Map<string, SaveModule> = new Map();
|
||||
for (let pluginInfo of pluginInfos) {
|
||||
let module = await SaveModule.fromPluginInfo(pluginInfo);
|
||||
if (!module) {
|
||||
continue;
|
||||
}
|
||||
modules.set(module.info.name, module);
|
||||
}
|
||||
|
||||
// Find stand alone modules to load
|
||||
// XXX for now only the included clusterio module is loaded
|
||||
let modulesDirectory = path.join(__dirname, "..", "..", "..", "modules");
|
||||
for (let entry of await fs.readdir(modulesDirectory, { withFileTypes: true })) {
|
||||
if (entry.isDirectory()) {
|
||||
if (modules.has(entry.name)) {
|
||||
throw new Error(`Module with name ${entry.name} already exists in a plugin`);
|
||||
}
|
||||
let module = await SaveModule.fromDirectory(path.join(modulesDirectory, entry.name));
|
||||
modules.set(module.info.name, module);
|
||||
}
|
||||
}
|
||||
return modules;
|
||||
}
|
||||
|
||||
export class PatchInfo {
|
||||
static currentVersion = 1;
|
||||
|
||||
|
||||
Reference in new issue
Block a user