From 8b01bb6642029b661ebe345bc90e309f35a0606a Mon Sep 17 00:00:00 2001 From: peshomir <80340328+peshomir@users.noreply.github.com> Date: Sun, 18 Aug 2024 16:33:41 +0300 Subject: [PATCH] Experimental fix for game version ^1.99.5.2 --- build.js | 24 +++++++++++++++++------- package-lock.json | 19 ++++++++++++++++++- package.json | 3 ++- 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/build.js b/build.js index 5bc0ac1..fc021bd 100644 --- a/build.js +++ b/build.js @@ -1,5 +1,6 @@ import beautifier from 'js-beautify'; const { js: beautify } = beautifier; +import UglifyJS from 'uglify-js'; import fs from 'fs'; if (!fs.existsSync("./build")) fs.mkdirSync("./build"); @@ -9,6 +10,22 @@ fs.cpSync("./src/fx_core.js", "./build/fx_core.js"); fs.writeFileSync("./build/index.html", fs.readFileSync("./build/index.html").toString().replace(/buildTimestamp/g, Date.now())); let script = fs.readFileSync('./game/latest.js', { encoding: 'utf8' }).replace("\n", "").trim(); +const exposeVarsToGlobalScope = true; +// need to first remove the iife wrapper so the top-level functions aren't inlined +if (exposeVarsToGlobalScope && script.startsWith("\"use strict\"; (function () {") && script.endsWith("})();")) + script = script.slice("\"use strict\"; (function () {".length, -"})();".length); +if (exposeVarsToGlobalScope && script.startsWith("(function () {") && script.endsWith("})();")) + script = script.slice("(function () {".length, -"})();".length); + +// for versions ^1.99.5.2 +const minificationResult = UglifyJS.minify(script, { + "compress": { "arrows": false }, + "mangle": false +}); +if (minificationResult.error) console.log(minificationResult.error); +if (minificationResult.warnings) console.log(minificationResult.warnings); +script = minificationResult.code; + const replaceOne = (expression, replaceValue) => { const result = matchOne(expression); // this (below) works correctly because expression.lastIndex gets reset above in matchOne when there is no match @@ -104,13 +121,6 @@ applyPatches({ replace, replaceOne, replaceRawCode, dictionary, matchOne, matchR console.log("Formatting code..."); -const exposeVarsToGlobalScope = true; - -if (exposeVarsToGlobalScope && script.startsWith("\"use strict\"; (function () {") && script.endsWith("})();")) - script = script.slice("\"use strict\"; (function () {".length, -"})();".length); -if (exposeVarsToGlobalScope && script.startsWith("(function () {") && script.endsWith("})();")) - script = script.slice("(function () {".length, -"})();".length); - script = beautify(script, { "indent_size": "1", "indent_char": "\t", diff --git a/package-lock.json b/package-lock.json index 342933a..c3a4f3a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,8 @@ "version": "0.5.3", "license": "ISC", "dependencies": { - "js-beautify": "^1.14.11" + "js-beautify": "^1.14.11", + "uglify-js": "^3.19.2" } }, "node_modules/@isaacs/cliui": { @@ -463,6 +464,17 @@ "node": ">=8" } }, + "node_modules/uglify-js": { + "version": "3.19.2", + "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.19.2.tgz", + "integrity": "sha512-S8KA6DDI47nQXJSi2ctQ629YzwOVs+bQML6DAtvy0wgNdpi+0ySpQK0g2pxBq2xfF2z3YCscu7NNA8nXT9PlIQ==", + "bin": { + "uglifyjs": "bin/uglifyjs" + }, + "engines": { + "node": ">=0.8.0" + } + }, "node_modules/which": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", @@ -878,6 +890,11 @@ } } }, + "uglify-js": { + "version": "3.19.2", + "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.19.2.tgz", + "integrity": "sha512-S8KA6DDI47nQXJSi2ctQ629YzwOVs+bQML6DAtvy0wgNdpi+0ySpQK0g2pxBq2xfF2z3YCscu7NNA8nXT9PlIQ==" + }, "which": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", diff --git a/package.json b/package.json index a072d2f..43e623e 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ }, "homepage": "https://github.com/fxclient/FXclient#readme", "dependencies": { - "js-beautify": "^1.14.11" + "js-beautify": "^1.14.11", + "uglify-js": "^3.19.2" } }