From 03511fc240ddf4fc2dd9a4bd916b276d65e6cb3d Mon Sep 17 00:00:00 2001 From: psihius Date: Thu, 19 Apr 2018 23:33:09 +0300 Subject: [PATCH] Hardened NaN checks, added file read delay for research-sync so factorio has time to finish writing to the file --- client.js | 28 +++++++----- sharedPlugins/researchSync/config.js | 3 +- sharedPlugins/researchSync/enableResearch.lua | 4 +- sharedPlugins/researchSync/index.js | 45 +++++++++++-------- 4 files changed, 47 insertions(+), 33 deletions(-) diff --git a/client.js b/client.js index d87966af..f4edcae7 100644 --- a/client.js +++ b/client.js @@ -476,17 +476,23 @@ function instanceManagement() { // send file contents to plugin for processing function fileChangeHandler(eventType, filename) { if(filename != null){ - // get array of lines in file - let stuff = fs.readFileSync(instancedirectory + "/script-output/" + filename, "utf8").split("\n"); - // if you found anything, reset the file - if (stuff[0]) { - fs.writeFileSync(instancedirectory + "/script-output/" + filename, ""); - } - for(let i = 0; i < stuff.length; i++) { - if(stuff[i] && !stuff[i].includes('\u0000\u0000')) { - plugins[I].scriptOutput(stuff[i]); - } - } + setTimeout( + ()=>{ + // get array of lines in file + let stuff = fs.readFileSync(instancedirectory + "/script-output/" + filename, "utf8").split("\n"); + + // if you found anything, reset the file + if (stuff[0]) { + fs.writeFileSync(instancedirectory + "/script-output/" + filename, ""); + } + for(let i = 0; i < stuff.length; i++) { + if(stuff[i] && !stuff[i].includes('\u0000\u0000')) { + plugins[I].scriptOutput(stuff[i]); + } + } + }, + pluginConfig.fileReadDelay || 0 + ); } } } diff --git a/sharedPlugins/researchSync/config.js b/sharedPlugins/researchSync/config.js index 63fa14d9..0c842f19 100644 --- a/sharedPlugins/researchSync/config.js +++ b/sharedPlugins/researchSync/config.js @@ -11,4 +11,5 @@ module.exports = { description: "syncs research across servers", // We'll send everything in this file to your stdin. Beware. scriptOutputFileSubscription: "researchSync.txt", -} \ No newline at end of file + fileReadDelay: 50 +} diff --git a/sharedPlugins/researchSync/enableResearch.lua b/sharedPlugins/researchSync/enableResearch.lua index 6f22718f..13004d13 100644 --- a/sharedPlugins/researchSync/enableResearch.lua +++ b/sharedPlugins/researchSync/enableResearch.lua @@ -1,10 +1,10 @@ /silent-command if game.forces['player'].technologies['{tech_name}'] then if game.forces['player'].technologies['{tech_name}'].researched ~= {tech_researched} then - game.forces['player'].technologies['{tech_name}'].researched={tech_researched} + game.forces['player'].technologies['{tech_name}'].researched = {tech_researched} end if game.forces['player'].technologies['{tech_name}'].level ~= {tech_level} then - game.forces['player'].technologies['{tech_name}'].level={tech_level } + game.forces['player'].technologies['{tech_name}'].level = {tech_level} end script.raise_event(defines.events.on_research_finished, {research=game.forces['player'].technologies['{tech_name}'], by_script=true}) game.play_sound({path="utility/research_completed"}) diff --git a/sharedPlugins/researchSync/index.js b/sharedPlugins/researchSync/index.js index 924a5dab..bdae8729 100644 --- a/sharedPlugins/researchSync/index.js +++ b/sharedPlugins/researchSync/index.js @@ -38,12 +38,15 @@ class ResearchSync { if (slaveData[instanceKey].unique === this.config.unique.toString()) { return; } - if (!slaveData[instanceKey].meta.hasOwnProperty('research')) { + if (!slaveData[instanceKey].hasOwnProperty('meta') || !slaveData[instanceKey].meta.hasOwnProperty('research')) { return; } let researchList = slaveData[instanceKey].meta.research; if(researchList){ Object.keys(researchList).forEach(researchName => { + if (isNaN(researchList[researchName].researched) || isNaN(researchList[researchName].level)) { + return; + } if (needResearch.hasOwnProperty(researchName)) { if (needResearch[researchName].researched === 0) { needResearch[researchName].researched = parseInt(researchList[researchName].researched); @@ -62,15 +65,18 @@ class ResearchSync { let difference = this.filterResearchDiff(this.research, needResearch); Object.keys(difference).forEach((key) => { - let command = this.functions.enableResearch; - while(command.includes("{tech_name}")){ - command = command.replace("{tech_name}", key); - command = command.replace("{tech_researched}", difference[key].researched); - command = command.replace("{tech_level}", difference[key].level); + if (this.research[key]) { + let command = this.functions.enableResearch; + while(command.includes("{tech_name}")){ + command = command.replace("{tech_name}", key); + command = command.replace("{tech_researched}", difference[key].researched); + command = command.replace("{tech_level}", difference[key].level); + } + this.messageInterface(command); + console.log('Unlocking '+ key + ': ' + (difference[key].researched === 0 ? 'false' : 'true') + ' and level ' + difference[key].level + ', was '+ (this.research[key].researched === 0 ? 'false' : 'true') + ' at level ' + this.research[key].level); + this.messageInterface("Unlocking research: " + key + " at research state = " + (difference[key].researched === 0 ? 'false' : 'true') + ' and level ' + difference[key].level); + this.research[key] = difference[key]; } - this.messageInterface(command); - this.messageInterface("Unlocking research: " + key + " at research state = " + (difference[key].researched === 0 ? 'false' : 'true') + ' and level ' + difference[key].level); - this.research[key] = difference[key]; }); needle.post(this.config.masterIP + ':' + this.config.masterPort + '/api/editSlaveMeta', { @@ -87,8 +93,10 @@ class ResearchSync { let diff = {}; Object.keys(localResearch).forEach((key) => { if (remoteResearch.hasOwnProperty(key)) { - if ((localResearch[key].researched === 0 && localResearch[key].researched !== remoteResearch[key].researched) || localResearch[key].level !== remoteResearch[key].level) { - diff[key] = remoteResearch[key]; + if ((localResearch[key].researched === 0 && localResearch[key].researched !== remoteResearch[key].researched) || localResearch[key].level < remoteResearch[key].level) { + if (!isNaN(remoteResearch[key].researched) && !isNaN(remoteResearch[key].level)) { + diff[key] = {researched: remoteResearch[key].researched, level: remoteResearch[key].level}; + } } } }); @@ -106,15 +114,14 @@ class ResearchSync { return fs.readFileSync("sharedPlugins/researchSync/" + path,'utf-8').replace(/\r?\n|\r/g,' '); } scriptOutput(data){ - try { - let kv = data.split(":"); - let name = kv[0]; - let researched = ('true' !== kv[1] - ? 0 - : 1); - let level = parseInt(kv[2]); + let kv = data.split(":"); + let name = kv[0]; + let researched = ('true' !== kv[1] + ? 0 + : 1); + let level = parseInt(kv[2]); + if (!isNaN(level) && !isNaN(researched)) { this.research[name] = {researched: researched, level: level}; - } catch (e) { } } }