matchCode and insertCode functions

dev
peshomir 2025-02-20 22:32:06 +02:00
parent eb216fd0e4
commit 3f3ad1a185
2 changed files with 34 additions and 10 deletions

View File

@ -34,6 +34,8 @@ class ModUtils {
this.matchRawCode = this.matchRawCode.bind(this);
this.replaceCode = this.replaceCode.bind(this);
this.waitForMinification = this.waitForMinification.bind(this);
this.matchCode = this.matchCode.bind(this);
this.insertCode = this.insertCode.bind(this);
}
/** @param {RegExp} expression */
@ -119,9 +121,37 @@ class ModUtils {
let expression = new RegExp(isForDictionary ? raw.replaceAll("@@", "@") : raw, "g");
return { expression, groups };
}
/**
* @typedef {Object} MatchCodeOptions
* @property {string[]} [addToDictionary]
*/
matchCode(code, /** @type {MatchCodeOptions} */ options) {
const result = this.matchRawCode(minifyCode(code));
if (options.addToDictionary !== undefined) {
options.addToDictionary.forEach(varName => {
if (result[varName] === undefined)
throw new Error(`matchCode addToDictionary error: ${varName} was undefined in the match results`)
this.addToDictionary(varName, result[varName]);
});
}
return result;
}
replaceCode(code, replacement, options) {
return this.replaceRawCode(minifyCode(code), replacement);
}
/**
* @param {string} code
* @param {string} codeToInsert
*/
insertCode(code, codeToInsert) {
const insertionPoint = "/* here */";
if (!code.includes(insertionPoint)) throw new Error("insertCode: No insertion point found");
return this.replaceCode(code.replace(insertionPoint, ""), code.replace(insertionPoint, codeToInsert));
}
waitForMinification(/** @type {Function} */ handler) {
this.postMinifyHandlers.push(handler);
}

View File

@ -1,23 +1,17 @@
import ModUtils from '../modUtils.js';
// Custom lobby patches
export default (/** @type {ModUtils} */ { replaceCode, replaceRawCode, dictionary: dict, waitForMinification }) => {
export default (/** @type {ModUtils} */ { insertCode, replaceRawCode, dictionary: dict, waitForMinification }) => {
// set player id correctly
replaceCode(`function aBG(aBE) {
insertCode(`function aBG(aBE) {
if (!Lobby.aAl) { return -1; }
/* here */
var s = aBE.length;
var qu = Lobby.aAl.qu;
for (var i = 0; i < s; i++) { if (aBE[i].qu === qu) { return i; } }
return -1;
}`, `function aBG(aBE) {
if (!Lobby.aAl) { return -1; }
if (__fx.customLobby.isActive()) return __fx.customLobby.getPlayerId();
var s = aBE.length;
var qu = Lobby.aAl.qu;
for (var i = 0; i < s; i++) { if (aBE[i].qu === qu) { return i; } }
return -1;
}`);
}`, `if (__fx.customLobby.isActive()) return __fx.customLobby.getPlayerId();`);
waitForMinification(() => {
replaceRawCode("this.aHm=function(){i___.rX(),aM.a7U(0),aM.init()}",