Module system for patches

dev
peshomir 2025-02-18 12:04:03 +02:00
parent 92980649eb
commit eb216fd0e4
3 changed files with 33 additions and 5 deletions

View File

@ -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(

22
patches/main.js 100644
View File

@ -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))
}

View File

@ -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;