Fix incorrect property checks in researchSync

Fix naive checks for properties being set of the form obj[key] into the
proper form Object.hasOwnProperty.call(obj, key).  The reason for this
is that if the key has a value of a built-in property like 'constructor'
then a check of the form obj[key] will always return true even if that
property hasn't been set on the object.
This commit is contained in:
Hornwitser committed 2019-10-12 17:07:07 +02:00
1 parent a9cc743106
commit 6a6274e85c
3 files changed
+46 -32

No files matched your search

+7
View File
@@ -1,6 +1,13 @@
Changelog Changelog
========= =========
Version 1.2.2
-------------
- Fixed possible crash with modded technologies named the same as a built-in
Object prototype property in researchSync.
Version 1.2.1 Version 1.2.1
------------- -------------
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "clusterio", "name": "clusterio",
"version": "1.2.1", "version": "1.2.2-alpha",
"description": "Interserver communication for factorio", "description": "Interserver communication for factorio",
"repository": "https://github.com/Danielv123/factorioClusterio", "repository": "https://github.com/Danielv123/factorioClusterio",
"main": "master.js", "main": "master.js",
+38 -31
View File
@@ -170,10 +170,11 @@ class ResearchSync {
} }
clear_contribution_to_researched_techs() { clear_contribution_to_researched_techs() {
// if between updates tech was researched
if (!this.prev_research)
return
for (let [name, research] of Object.entries(this.research)) { for (let [name, research] of Object.entries(this.research)) {
if (!Object.hasOwnProperty.call(this.prev_research, name)) {
continue;
}
let researched let researched
if (research.infinite) if (research.infinite)
researched = this.prev_research[name].level < research.level researched = this.prev_research[name].level < research.level
@@ -193,7 +194,7 @@ class ResearchSync {
if (isNaN(node_tech.researched) || isNaN(node_tech.level) || isNaN(node_tech.infinite)) if (isNaN(node_tech.researched) || isNaN(node_tech.level) || isNaN(node_tech.infinite))
continue continue
if (cluster_techs[name]) { if (Object.hasOwnProperty.call(cluster_techs, name)) {
if (cluster_techs[name].infinite === 1 && cluster_techs[name].level < node_tech.level) { if (cluster_techs[name].infinite === 1 && cluster_techs[name].level < node_tech.level) {
cluster_techs[name].level = node_tech.level cluster_techs[name].level = node_tech.level
} else if (node_tech.researched > cluster_techs[name].researched) { } else if (node_tech.researched > cluster_techs[name].researched) {
@@ -213,8 +214,9 @@ class ResearchSync {
for (let slave_data of slaves_data) { for (let slave_data of slaves_data) {
for (let [name, research] of Object.entries(slave_data.meta.research)) { for (let [name, research] of Object.entries(slave_data.meta.research)) {
if (!cluster_researches[name]) if (!Object.hasOwnProperty.call(cluster_researches, name)) {
continue continue;
}
if (isNaN(cluster_researches[name].progress) if (isNaN(cluster_researches[name].progress)
|| isNaN(research.contribution) || isNaN(research.contribution)
|| isNaN(research.level) || isNaN(research.level)
@@ -238,35 +240,37 @@ class ResearchSync {
} }
filter_researched_techs(cluster_researches) { filter_researched_techs(cluster_researches) {
let local_researches = this.research
let result = {}; let result = {};
for (let key in local_researches) { for (let [name, tech] of Object.entries(this.research)) {
if (!cluster_researches[key]) if (!Object.hasOwnProperty.call(cluster_researches, name)) {
continue continue;
if (isNaN(cluster_researches[key].researched) || isNaN(cluster_researches[key].level)) }
if (isNaN(cluster_researches[name].researched) || isNaN(cluster_researches[name].level))
continue continue
let researched let researched
if (local_researches[key].infinite) if (tech.infinite) {
researched = local_researches[key].level < cluster_researches[key].level researched = tech.level < cluster_researches[name].level;
else } else {
researched = local_researches[key].researched < cluster_researches[key].researched researched = tech.researched < cluster_researches[name].researched;
}
if (researched) if (researched)
result[key] = cluster_researches[key] result[name] = cluster_researches[name]
} }
return result; return result;
} }
filter_updated_techs(cluster_techs, to_research) { filter_updated_techs(cluster_techs, to_research) {
let local_techs = this.research
let result = {} let result = {}
for (let name in local_techs) { for (let [name, tech] of Object.entries(this.research)) {
if (name in to_research || !cluster_techs[name]) if (Object.hasOwnProperty.call(to_research, name) || !Object.hasOwnProperty.call(cluster_techs, name)) {
continue continue;
}
if (isNaN(cluster_techs[name].progress)) if (isNaN(cluster_techs[name].progress))
continue continue
if (local_techs[name].progress < cluster_techs[name].progress) if (tech.progress < cluster_techs[name].progress)
result[name] = cluster_techs[name] result[name] = cluster_techs[name]
} }
return result return result
@@ -274,8 +278,9 @@ class ResearchSync {
research_technologies(to_research) { research_technologies(to_research) {
for (let name of Object.keys(to_research)) for (let name of Object.keys(to_research))
if (!this.research[name]) if (!Object.hasOwnProperty.call(this.research, name)) {
delete to_research[name] delete to_research[name];
}
const notify = Object.keys(to_research).length === 1 const notify = Object.keys(to_research).length === 1
for (let [name, tech] of Object.entries(to_research)) { for (let [name, tech] of Object.entries(to_research)) {
@@ -299,8 +304,9 @@ class ResearchSync {
update_technologies_progress(to_update) { update_technologies_progress(to_update) {
for (let [name, tech] of Object.entries(to_update)) { for (let [name, tech] of Object.entries(to_update)) {
if (!this.research[name]) if (!Object.hasOwnProperty.call(this.research, name)) {
continue continue;
}
let progress = this.research[name].progress let progress = this.research[name].progress
if (progress === null) if (progress === null)
progress = 'nil' progress = 'nil'
@@ -317,11 +323,10 @@ class ResearchSync {
} }
print_own_contribution() { print_own_contribution() {
if (!this.prev_research)
return
for (let [name, tech] of Object.entries(this.research)) { for (let [name, tech] of Object.entries(this.research)) {
if (!this.prev_research[name]) if (!Object.hasOwnProperty.call(this.prev_research, name)) {
continue continue;
}
let diff = this.research[name].contribution - this.prev_research[name].contribution let diff = this.research[name].contribution - this.prev_research[name].contribution
if (Math.abs(diff) > Number.EPSILON * 1000) if (Math.abs(diff) > Number.EPSILON * 1000)
this.log(`Own research ${name}: ${this.research[name].progress} += ${diff}`) this.log(`Own research ${name}: ${this.research[name].progress} += ${diff}`)
@@ -346,8 +351,10 @@ class ResearchSync {
if (isNaN(level) || isNaN(researched)) if (isNaN(level) || isNaN(researched))
return return
this.prev_research[name] = this.research[name]
if (!this.prev_research[name]) { if (Object.hasOwnProperty.call(this.research, name)) {
this.prev_research[name] = this.research[name]
} else {
this.prev_research[name] = { this.prev_research[name] = {
researched: null, researched: null,
level: null, level: null,