diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b2a5ef6..a0f39e5b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,8 @@ Version 2.0.0 privileges requirements of symlinks. - Changed the per instance scenario folder to be linked instead of copied on instance creation. +- Instance id is no longer derived from the rcon password, instead it's + generated upon instance creation and stored in the instance config. ### Breaking Changes diff --git a/client.js b/client.js index 4a04b31f..49039f7d 100644 --- a/client.js +++ b/client.js @@ -17,7 +17,6 @@ const crypto = require("crypto"); // internal libraries const objectOps = require("lib/objectOps.js"); const fileOps = require("lib/fileOps"); -const stringUtils = require("lib/stringUtils.js"); const pluginManager = require("lib/manager/pluginManager"); const modManager = require("lib/manager/modManager"); const hashFile = require('lib/hash').hashFile; @@ -402,6 +401,7 @@ write-data=${ instance.path() }\r\n await symlinkMods(instance, "sharedMods", console); let instconf = { + "id": Math.random() * 2 ** 31 | 0, "factorioPort": args.port || process.env.FACTORIOPORT || randomDynamicPort(), "clientPort": args["rcon-port"] || process.env.RCONPORT || randomDynamicPort(), "__comment_clientPassword": "This is the rcon password. Its also used for making an instanceID. Make sure its unique and not blank.", @@ -490,7 +490,12 @@ write-data=${ instance.path() }\r\n async function startInstance(config, args, instance) { var instanceconfig = JSON.parse(await fs.readFile(instance.path("config.json"))); - instanceconfig.unique = stringUtils.hashCode(instanceconfig.clientPassword); + if (typeof instanceconfig.id !== "number" || isNaN(instanceconfig.id)) { + throw new Error(`${instance.path("config.json")} is missing id`); + } + // Temporary measure for backwards compatibility + instanceconfig.unique = instanceconfig.id; + if (process.env.FACTORIOPORT) { instanceconfig.factorioPort = process.env.FACTORIOPORT; } diff --git a/lib/stringUtils.js b/lib/stringUtils.js deleted file mode 100644 index 7febd82b..00000000 --- a/lib/stringUtils.js +++ /dev/null @@ -1,13 +0,0 @@ -function hashCode(string){ - if(typeof string !== "string") throw "ERROR stringUtils: Not a string"; - var hash = 0; - if (string.length == 0) return hash; - for (let i = 0; i < string.length; i++) { - hash = ((hash << 5) - hash) + string.charCodeAt(i); - hash = hash & hash; // Convert to 32bit integer - } - return hash; -} -module.exports = { - hashCode: hashCode, -} \ No newline at end of file diff --git a/sharedPlugins/clusterioMod/index.js b/sharedPlugins/clusterioMod/index.js index 6ecbb3a6..bc970562 100644 --- a/sharedPlugins/clusterioMod/index.js +++ b/sharedPlugins/clusterioMod/index.js @@ -3,7 +3,6 @@ const fs = require("fs-extra"); const objectOps = require("lib/objectOps.js"); const fileOps = require("lib/fileOps"); -const stringUtils = require("lib/stringUtils.js"); const checkHotpatchInstallation = require("lib/clusterTools/checkHotpatchInstallation"); const getLua = require("lib/clusterTools/getLua"); diff --git a/test/lib/stringUtils.js b/test/lib/stringUtils.js deleted file mode 100644 index 03760e08..00000000 --- a/test/lib/stringUtils.js +++ /dev/null @@ -1,48 +0,0 @@ -const assert = require("assert"); -const stringUtils = require("lib/stringUtils"); - -describe("stringUtils.js", function(){ - describe("hashCode", function(){ - it("Produces a 32bit signed int hash from a string", function(){ - let x = stringUtils.hashCode("whatever"); - let y = stringUtils.hashCode(" a much longer string to prove this works for long strings as well and also special characters &!¤(#)¤"); - - assert(typeof x == "number", "hashCode should be returning a 32 bit signed int"); - assert(x.toString() == "1934383586"); - assert(y.toString() == "1648886132"); - }); - let permutations = 1000; - it("Rarely produces same hash for different strings ("+permutations+" hashes with collision check)", function(){ - let keyHashPairs = []; - for(let i = 0; i < permutations; i++){ - let string = Math.random().toString(); - keyHashPairs.push({ - key:string, - hash:stringUtils.hashCode(string), - }); - } - - for(let i = 0; i < permutations; i++){ - for(let o = 0; o < permutations; o++){ - assert(i == o || keyHashPairs[o].key != keyHashPairs[i].key, "Woha, Math.random() generated 2 equal numbers???"); - assert(i == o || keyHashPairs[o].hash != keyHashPairs[i].hash, "Hash collision, as long at it doesn't happen again its fine."); - } - } - }); - it("Returns 0 for 0 length strings", function(){ - let x = stringUtils.hashCode(""); - assert.deepEqual(x, 0); - }); - it("Throws when supplied with anything other than a string", function(){ - assert.throws(function(){ - stringUtils.hashCode([]); - }, "ERROR: Not a string") - assert.throws(function(){ - stringUtils.hashCode({}); - }, "ERROR: Not a string") - assert.throws(function(){ - stringUtils.hashCode(0); - }, "ERROR: Not a string") - }); - }); -}); \ No newline at end of file