Rework module loading

Change module loading to use a module.json file describing the module
and patch in all files in a directory path passed to the patcher instead
of having to pass the files names and their content.  This allows the
specification of which files should be passed to the event_handler lib
and an easy implementation of loading stand alone modules.
This commit is contained in:
Hornwitser committed 2020-01-04 17:34:16 +01:00
1 parent 11992d1839
commit 80bd75ba16
11 files changed
+120 -47

No files matched your search

+21 -8
View File
@@ -7,6 +7,7 @@
const events = require("events");
const fs = require("fs-extra");
const jszip = require("jszip");
const path = require("path");
const hash = require("lib/hash");
@@ -81,7 +82,7 @@ function generateLoader(patchInfo) {
for (let module of patchInfo["modules"]) {
for (let file of module["files"]) {
if (file["load"]) {
let requirePath = "modules/" + file["path"].slice(0, -4);
let requirePath = `modules/${module.name}/${file["path"].slice(0, -4)}`;
lines.push('event_handler.add_lib(require("'+requirePath+'"))');
}
}
@@ -135,19 +136,31 @@ async function patch(savePath, modules) {
}
// Add the modules to the save.
let modulesDir = root.folder("modules");
for (let module of modules) {
let moduleDir = root.folder(`modules/${module.name}`);
let moduleInfo = {
"name": module.name,
"files": [],
}
for (let file of module.files) {
modulesDir.file(file.path, file.content);
moduleInfo["files"].push({
"path": file.path,
"load": Boolean(file.load),
});
let dirs = [[module.path, '']];
while (dirs.length) {
let [dir, relativeDir] = dirs.pop();
for (let entry of await fs.readdir(dir, { withFileTypes: true })) {
let fsPath = path.join(dir, entry.name);
let relativePath = path.posix.join(relativeDir, entry.name);
if (entry.isFile()) {
moduleDir.file(relativePath, await fs.readFile(fsPath));
moduleInfo["files"].push({
"path": relativePath,
"load": module.load.includes(relativePath),
});
} else if (entry.isDirectory()) {
dirs.push([fsPath, relativePath]);
}
}
}
patchInfo["modules"].push(moduleInfo);