Fix script needlessly being recompiled (#932)

* Fix nsjs recompiling needlesly.
This commit is contained in:
hydroflame 2021-05-03 18:53:10 -04:00 committed by GitHub
parent 5613d371c9
commit 3cbf225c98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 2 deletions

@ -1453,6 +1453,11 @@ function NetscriptFunctions(workerScript) {
if (scriptname == destServer.scripts[i].filename) { if (scriptname == destServer.scripts[i].filename) {
workerScript.log("scp", `WARNING: File '${scriptname}' overwritten on '${destServer.hostname}'`); workerScript.log("scp", `WARNING: File '${scriptname}' overwritten on '${destServer.hostname}'`);
const oldScript = destServer.scripts[i]; const oldScript = destServer.scripts[i];
// If it's the exact same file don't actually perform the
// copy to avoid recompiling uselessly. Players tend to scp
// liberally.
if(oldScript.code === sourceScript.code)
return true;
oldScript.code = sourceScript.code; oldScript.code = sourceScript.code;
oldScript.ramUsage = sourceScript.ramUsage; oldScript.ramUsage = sourceScript.ramUsage;
oldScript.markUpdated(); oldScript.markUpdated();

@ -20,6 +20,7 @@ export async function executeJSScript(scripts = [], workerScript) {
let urls = null; let urls = null;
let script = workerScript.getScript(); let script = workerScript.getScript();
if (shouldCompile(script, scripts)) { if (shouldCompile(script, scripts)) {
console.log("recompiling");
// The URL at the top is the one we want to import. It will // The URL at the top is the one we want to import. It will
// recursively import all the other modules in the urlStack. // recursively import all the other modules in the urlStack.
// //
@ -27,6 +28,7 @@ export async function executeJSScript(scripts = [], workerScript) {
// but not really behaves like import. Particularly, it cannot // but not really behaves like import. Particularly, it cannot
// load fully dynamic content. So we hide the import from webpack // load fully dynamic content. So we hide the import from webpack
// by placing it inside an eval call. // by placing it inside an eval call.
script.markUpdated();
urls = _getScriptUrls(script, scripts, []); urls = _getScriptUrls(script, scripts, []);
script.url = urls[urls.length - 1].url; script.url = urls[urls.length - 1].url;
script.module = new Promise(resolve => resolve(eval('import(urls[urls.length - 1].url)'))); script.module = new Promise(resolve => resolve(eval('import(urls[urls.length - 1].url)')));
@ -58,14 +60,15 @@ export async function executeJSScript(scripts = [], workerScript) {
*/ */
function shouldCompile(script, scripts) { function shouldCompile(script, scripts) {
if (script.module === "") return true; if (script.module === "") return true;
console.log(script.dependencies);
return script.dependencies.some(dep => { return script.dependencies.some(dep => {
const depScript = scripts.find(s => s.filename == dep.url); const depScript = scripts.find(s => s.filename == dep.filename);
// If the script is not present on the server, we should recompile, if only to get any necessary // If the script is not present on the server, we should recompile, if only to get any necessary
// compilation errors. // compilation errors.
if (!depScript) return true; if (!depScript) return true;
const depIsMoreRecent = depScript.moduleSequenceNumber > script.moduleSequenceNumber const depIsMoreRecent = depScript.moduleSequenceNumber > script.moduleSequenceNumber;
return depIsMoreRecent; return depIsMoreRecent;
}); });
} }