mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-20 23:53: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
4 files changed
+46
-32
No files matched your search
@@ -476,17 +476,23 @@ function instanceManagement() {
|
|||||||
// send file contents to plugin for processing
|
// send file contents to plugin for processing
|
||||||
function fileChangeHandler(eventType, filename) {
|
function fileChangeHandler(eventType, filename) {
|
||||||
if(filename != null){
|
if(filename != null){
|
||||||
// get array of lines in file
|
setTimeout(
|
||||||
let stuff = fs.readFileSync(instancedirectory + "/script-output/" + filename, "utf8").split("\n");
|
()=>{
|
||||||
// if you found anything, reset the file
|
// get array of lines in file
|
||||||
if (stuff[0]) {
|
let stuff = fs.readFileSync(instancedirectory + "/script-output/" + filename, "utf8").split("\n");
|
||||||
fs.writeFileSync(instancedirectory + "/script-output/" + filename, "");
|
|
||||||
}
|
// if you found anything, reset the file
|
||||||
for(let i = 0; i < stuff.length; i++) {
|
if (stuff[0]) {
|
||||||
if(stuff[i] && !stuff[i].includes('\u0000\u0000')) {
|
fs.writeFileSync(instancedirectory + "/script-output/" + filename, "");
|
||||||
plugins[I].scriptOutput(stuff[i]);
|
}
|
||||||
}
|
for(let i = 0; i < stuff.length; i++) {
|
||||||
}
|
if(stuff[i] && !stuff[i].includes('\u0000\u0000')) {
|
||||||
|
plugins[I].scriptOutput(stuff[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
pluginConfig.fileReadDelay || 0
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,4 +11,5 @@ module.exports = {
|
|||||||
description: "syncs research across servers",
|
description: "syncs research across servers",
|
||||||
// We'll send everything in this file to your stdin. Beware.
|
// We'll send everything in this file to your stdin. Beware.
|
||||||
scriptOutputFileSubscription: "researchSync.txt",
|
scriptOutputFileSubscription: "researchSync.txt",
|
||||||
|
fileReadDelay: 50
|
||||||
}
|
}
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
/silent-command
|
/silent-command
|
||||||
if game.forces['player'].technologies['{tech_name}'] then
|
if game.forces['player'].technologies['{tech_name}'] then
|
||||||
if game.forces['player'].technologies['{tech_name}'].researched ~= {tech_researched} 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
|
end
|
||||||
if game.forces['player'].technologies['{tech_name}'].level ~= {tech_level} then
|
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
|
end
|
||||||
script.raise_event(defines.events.on_research_finished, {research=game.forces['player'].technologies['{tech_name}'], by_script=true})
|
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"})
|
game.play_sound({path="utility/research_completed"})
|
||||||
|
|||||||
@@ -38,12 +38,15 @@ class ResearchSync {
|
|||||||
if (slaveData[instanceKey].unique === this.config.unique.toString()) {
|
if (slaveData[instanceKey].unique === this.config.unique.toString()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!slaveData[instanceKey].meta.hasOwnProperty('research')) {
|
if (!slaveData[instanceKey].hasOwnProperty('meta') || !slaveData[instanceKey].meta.hasOwnProperty('research')) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let researchList = slaveData[instanceKey].meta.research;
|
let researchList = slaveData[instanceKey].meta.research;
|
||||||
if(researchList){
|
if(researchList){
|
||||||
Object.keys(researchList).forEach(researchName => {
|
Object.keys(researchList).forEach(researchName => {
|
||||||
|
if (isNaN(researchList[researchName].researched) || isNaN(researchList[researchName].level)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (needResearch.hasOwnProperty(researchName)) {
|
if (needResearch.hasOwnProperty(researchName)) {
|
||||||
if (needResearch[researchName].researched === 0) {
|
if (needResearch[researchName].researched === 0) {
|
||||||
needResearch[researchName].researched = parseInt(researchList[researchName].researched);
|
needResearch[researchName].researched = parseInt(researchList[researchName].researched);
|
||||||
@@ -62,15 +65,18 @@ class ResearchSync {
|
|||||||
let difference = this.filterResearchDiff(this.research, needResearch);
|
let difference = this.filterResearchDiff(this.research, needResearch);
|
||||||
|
|
||||||
Object.keys(difference).forEach((key) => {
|
Object.keys(difference).forEach((key) => {
|
||||||
let command = this.functions.enableResearch;
|
if (this.research[key]) {
|
||||||
while(command.includes("{tech_name}")){
|
let command = this.functions.enableResearch;
|
||||||
command = command.replace("{tech_name}", key);
|
while(command.includes("{tech_name}")){
|
||||||
command = command.replace("{tech_researched}", difference[key].researched);
|
command = command.replace("{tech_name}", key);
|
||||||
command = command.replace("{tech_level}", difference[key].level);
|
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', {
|
needle.post(this.config.masterIP + ':' + this.config.masterPort + '/api/editSlaveMeta', {
|
||||||
@@ -87,8 +93,10 @@ class ResearchSync {
|
|||||||
let diff = {};
|
let diff = {};
|
||||||
Object.keys(localResearch).forEach((key) => {
|
Object.keys(localResearch).forEach((key) => {
|
||||||
if (remoteResearch.hasOwnProperty(key)) {
|
if (remoteResearch.hasOwnProperty(key)) {
|
||||||
if ((localResearch[key].researched === 0 && localResearch[key].researched !== remoteResearch[key].researched) || localResearch[key].level !== remoteResearch[key].level) {
|
if ((localResearch[key].researched === 0 && localResearch[key].researched !== remoteResearch[key].researched) || localResearch[key].level < remoteResearch[key].level) {
|
||||||
diff[key] = remoteResearch[key];
|
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,' ');
|
return fs.readFileSync("sharedPlugins/researchSync/" + path,'utf-8').replace(/\r?\n|\r/g,' ');
|
||||||
}
|
}
|
||||||
scriptOutput(data){
|
scriptOutput(data){
|
||||||
try {
|
let kv = data.split(":");
|
||||||
let kv = data.split(":");
|
let name = kv[0];
|
||||||
let name = kv[0];
|
let researched = ('true' !== kv[1]
|
||||||
let researched = ('true' !== kv[1]
|
? 0
|
||||||
? 0
|
: 1);
|
||||||
: 1);
|
let level = parseInt(kv[2]);
|
||||||
let level = parseInt(kv[2]);
|
if (!isNaN(level) && !isNaN(researched)) {
|
||||||
this.research[name] = {researched: researched, level: level};
|
this.research[name] = {researched: researched, level: level};
|
||||||
} catch (e) {
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user