diff --git a/build.js b/build.js index d18236d..851c524 100644 --- a/build.js +++ b/build.js @@ -5,7 +5,6 @@ import UglifyJS from 'uglify-js'; import fs from 'fs'; import webpack from 'webpack'; import path from 'path'; -import applyPatches from './patches/patches.js'; import ModUtils, { minifyCode } from './modUtils.js'; if (!fs.existsSync("./build")) fs.mkdirSync("./build"); @@ -56,8 +55,8 @@ script = script.replace(/\bS\[(\d+)\]/g, (_match, index) => `"${stringArray[inde const modUtils = new ModUtils(minifyCode(script)); -import customLobbyPatches from './patches/customLobby.js'; -customLobbyPatches(modUtils); +import applyPatches from './patches/main.js'; +applyPatches(modUtils); // for versions ^1.99.5.2 const minificationResult = UglifyJS.minify(modUtils.script, { @@ -101,9 +100,10 @@ rawCodeSegments.forEach(code => { }); modUtils.executePostMinifyHandlers(); -applyPatches(modUtils); script = modUtils.script; +console.log("Building client code...") + await buildClientCode(); // the dictionary should maybe get embedded into one of the files in the bundle fs.writeFileSync( diff --git a/patches/main.js b/patches/main.js new file mode 100644 index 0000000..c401a1d --- /dev/null +++ b/patches/main.js @@ -0,0 +1,22 @@ +import fs from 'fs' +import ModUtils from '../modUtils.js'; + +const modules = await Promise.all(fs.readdirSync("./patches").flatMap(fileName => { + if (fileName === "main.js") return []; + else return import("./" + fileName); +})); + +const requiredVariables = new Set(modules.map(module => module.requiredVariables ?? []).flat()); + +export default function applyPatches(/** @type {ModUtils} */ modUtils) { + const dictionary = modUtils.dictionary; + + requiredVariables.forEach(varName => { + if (!dictionary.hasOwnProperty(varName)) { + throw new Error(`"${varName}" is required by a module but not defined the dictionary`); + } + }); + + // apply patches (default exported function) + modules.forEach(module => module.default(modUtils)) +} diff --git a/patches/patches.js b/patches/patches.js index 9dce3b3..3ddc594 100644 --- a/patches/patches.js +++ b/patches/patches.js @@ -1,6 +1,12 @@ import assets from '../assets.js'; import ModUtils from '../modUtils.js'; -export default (/** @type {ModUtils} */ { replace, replaceOne, replaceRawCode, dictionary, matchOne, matchRawCode, escapeRegExp }) => { + +export default (/** @type {ModUtils} */ modUtils) => { + modUtils.waitForMinification(() => applyPatches(modUtils)) +} +//export const requiredVariables = ["game", "playerId", "playerData", "rawPlayerNames", "gIsSingleplayer", "playerTerritories"]; + +function applyPatches(/** @type {ModUtils} */ { replace, replaceOne, replaceRawCode, dictionary, matchOne, matchRawCode, escapeRegExp }) { // Constants for easy usage of otherwise long variable access expressions const dict = dictionary;