mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-28 08:47:13 +09:00
Hardened NaN checks, added file read delay for research-sync so factorio has time to finish writing to the file
This commit is contained in:
1 parent
ed9b4bbafa
commit
03511fc240
3 files changed
+19
-5
No files matched your search
@@ -476,8 +476,11 @@ function instanceManagement() {
|
||||
// send file contents to plugin for processing
|
||||
function fileChangeHandler(eventType, filename) {
|
||||
if(filename != null){
|
||||
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, "");
|
||||
@@ -487,6 +490,9 @@ function instanceManagement() {
|
||||
plugins[I].scriptOutput(stuff[i]);
|
||||
}
|
||||
}
|
||||
},
|
||||
pluginConfig.fileReadDelay || 0
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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",
|
||||
fileReadDelay: 50
|
||||
}
|
||||
@@ -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,6 +65,7 @@ class ResearchSync {
|
||||
let difference = this.filterResearchDiff(this.research, needResearch);
|
||||
|
||||
Object.keys(difference).forEach((key) => {
|
||||
if (this.research[key]) {
|
||||
let command = this.functions.enableResearch;
|
||||
while(command.includes("{tech_name}")){
|
||||
command = command.replace("{tech_name}", key);
|
||||
@@ -69,8 +73,10 @@ class ResearchSync {
|
||||
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];
|
||||
}
|
||||
});
|
||||
|
||||
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]);
|
||||
if (!isNaN(level) && !isNaN(researched)) {
|
||||
this.research[name] = {researched: researched, level: level};
|
||||
} catch (e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user