mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-21 16:13:12 +09:00
Multithreaded createMap, improved isValidInstance reliablity
This commit is contained in:
1 parent
507c0afa98
commit
f697d2a74c
2 files changed
+94
-32
No files matched your search
+45
-22
@@ -75,7 +75,7 @@ module.exports = class factorioServer {
|
|||||||
"/instanceMods/",
|
"/instanceMods/",
|
||||||
];
|
];
|
||||||
mkdirp.sync(path);
|
mkdirp.sync(path);
|
||||||
folders.forEach(folder => fs.mkdirSync(path + folder));
|
folders.forEach(folder => mkdirp.sync(path + folder));
|
||||||
|
|
||||||
// create files
|
// create files
|
||||||
let files = [
|
let files = [
|
||||||
@@ -116,30 +116,53 @@ module.exports = class factorioServer {
|
|||||||
// console.log(err)
|
// console.log(err)
|
||||||
resolve(false);
|
resolve(false);
|
||||||
} else {
|
} else {
|
||||||
// console.log(stat)
|
// the instance folder exists, do some more checks
|
||||||
resolve(true);
|
fs.readdir(path, (err, files) => {
|
||||||
|
if(files && Array.isArray(files)){
|
||||||
|
let foldersThatShouldBeThere = [
|
||||||
|
"script-output",
|
||||||
|
"saves",
|
||||||
|
"mods",
|
||||||
|
"instanceMods",
|
||||||
|
];
|
||||||
|
let instanceValidity = true;
|
||||||
|
foldersThatShouldBeThere.forEach(folder => {
|
||||||
|
if(!files.includes(folder)){
|
||||||
|
instanceValidity = false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
resolve(instanceValidity);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
async createMap(name = "map.zip"){
|
createMap(name = "map.zip"){
|
||||||
if(!await this.isValidInstance()){
|
return new Promise((resolve, reject) => {
|
||||||
await createInstance();
|
this.isValidInstance().then(isValid => {
|
||||||
}
|
if(!isValid){
|
||||||
this.onProgress("log", "Starting map creation");
|
return createInstance();
|
||||||
console.log(this.factorioDirectory + '/bin/x64/factorio')
|
}
|
||||||
let code = child_process.spawnSync(
|
}).then(()=>{
|
||||||
path.join(this.factorioDirectory, 'bin', 'x64', 'factorio'), [
|
this.onProgress("log", "Starting map creation");
|
||||||
'-c', path.join(this.instanceDirectory,'config.ini'),
|
// console.log(this.factorioDirectory + '/bin/x64/factorio')
|
||||||
'--create', path.join(this.instanceDirectory, 'saves',name),
|
let proc = child_process.spawn(
|
||||||
'--server-settings', path.join(this.instanceDirectory, 'server-settings.json'),
|
path.join(this.factorioDirectory, 'bin', 'x64', 'factorio'), [
|
||||||
],{
|
'-c', path.join(this.instanceDirectory,'config.ini'),
|
||||||
cwd:process.cwd(),
|
'--create', path.join(this.instanceDirectory, 'saves',name),
|
||||||
}
|
'--server-settings', path.join(this.instanceDirectory, 'server-settings.json'),
|
||||||
);
|
],{
|
||||||
console.log(code.stdout.toString("utf8"))
|
cwd:process.cwd(),
|
||||||
console.log(code)
|
}
|
||||||
this.onProgress("log", "Map created");
|
);
|
||||||
return true;
|
// console.log(proc.stdout.toString("utf8"));
|
||||||
|
proc.stdout.on("data", data => this.onProgress("log", data.toString("utf8")));
|
||||||
|
proc.on("close", code => {
|
||||||
|
this.onProgress("log", "Map created! "+code);
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+49
-10
@@ -3,14 +3,16 @@ const fs = require("fs");
|
|||||||
const path = require("path");
|
const path = require("path");
|
||||||
|
|
||||||
const appRootPath = require("app-root-path");
|
const appRootPath = require("app-root-path");
|
||||||
|
const parallel = require("mocha.parallel");
|
||||||
|
const mkdirp = require("mkdirp");
|
||||||
|
|
||||||
const factorioServer = require("./index.js");
|
const factorioServer = require("./index.js");
|
||||||
|
|
||||||
describe("factorioServer/index.js", ()=>{
|
describe("class factorioServer/index.js", ()=>{
|
||||||
it("is a class (or at least a function)", ()=>{
|
it("is a class (or at least a function)", ()=>{
|
||||||
assert.equal(typeof factorioServer, "function");
|
assert.equal(typeof factorioServer, "function");
|
||||||
});
|
});
|
||||||
describe(".deleteInstance()", ()=>{
|
describe(".deleteInstance(instanceName)", ()=>{
|
||||||
it("deletes an instance, defaulting to the classes instanceDirectory", async () => {
|
it("deletes an instance, defaulting to the classes instanceDirectory", async () => {
|
||||||
let server = new factorioServer({
|
let server = new factorioServer({
|
||||||
instanceDirectory: path.resolve(__dirname, "_testData_deleteInstance"),
|
instanceDirectory: path.resolve(__dirname, "_testData_deleteInstance"),
|
||||||
@@ -22,10 +24,10 @@ describe("factorioServer/index.js", ()=>{
|
|||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe(".isValidInstance()", ()=>{
|
describe(".isValidInstance(instanceName)", ()=>{
|
||||||
it("tells us whether an instance exists or not", async () => {
|
it("tells us whether an instance exists or not", async () => {
|
||||||
let server = new factorioServer({
|
let server = new factorioServer({
|
||||||
instanceDirectory: path.resolve(__dirname, "_testData_isValidInstance"),
|
instanceDirectory: path.join(__dirname, "_testData_isValidInstance"),
|
||||||
instanceName: "Unit testing instance",
|
instanceName: "Unit testing instance",
|
||||||
});
|
});
|
||||||
await server.deleteInstance();
|
await server.deleteInstance();
|
||||||
@@ -35,10 +37,27 @@ describe("factorioServer/index.js", ()=>{
|
|||||||
let status2 = await server.isValidInstance();
|
let status2 = await server.isValidInstance();
|
||||||
assert.equal(status2, true);
|
assert.equal(status2, true);
|
||||||
|
|
||||||
// clean up folder
|
await server.deleteInstance(); // clean up folder
|
||||||
await server.deleteInstance();
|
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
it("does not get tricked by an empty folder", async () => {
|
||||||
|
let server = new factorioServer({
|
||||||
|
instanceDirectory: path.join(__dirname, "_testData_isValidInstance_2"),
|
||||||
|
instanceName: "Unit testing instance",
|
||||||
|
});
|
||||||
|
assert.equal(await server.isValidInstance(), false);
|
||||||
|
|
||||||
|
mkdirp.sync(path.join(__dirname, "_testData_isValidInstance_2")); // create empty folder simulating instance
|
||||||
|
assert.equal(await server.isValidInstance(), false);
|
||||||
|
|
||||||
|
await server.initialize(); // create a proper instance
|
||||||
|
assert.equal(await server.isValidInstance(), true);
|
||||||
|
|
||||||
|
await server.deleteInstance(); // delete the instance and ensure we see that it is in fact deleted
|
||||||
|
assert.equal(await server.isValidInstance(), false);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}).timeout(5000);
|
||||||
});
|
});
|
||||||
describe(".initialize()", ()=>{
|
describe(".initialize()", ()=>{
|
||||||
it("creates an instance", done => {
|
it("creates an instance", done => {
|
||||||
@@ -53,22 +72,42 @@ describe("factorioServer/index.js", ()=>{
|
|||||||
}).catch(e => console.log(e));
|
}).catch(e => console.log(e));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe(".createMap()", ()=>{
|
describe(".createMap(mapName.zip)", ()=>{
|
||||||
it("creates a map file as instanceDirectory/saves/map.zip", async ()=>{
|
it("creates a map file as instanceDirectory/saves/map.zip", async function(){
|
||||||
|
this.timeout(20000); // these test often takes a lot of time in the map creation step (4500 ms on i3 dev system)
|
||||||
let relativePath = __dirname.replace(appRootPath, "").substr(1);
|
let relativePath = __dirname.replace(appRootPath, "").substr(1);
|
||||||
let server = new factorioServer({
|
let server = new factorioServer({
|
||||||
instanceDirectory: path.join(relativePath, "_testData_createMap"),
|
instanceDirectory: path.join(relativePath, "_testData_createMap"),
|
||||||
instanceName: "Unit testing instance",
|
instanceName: "Unit testing instance",
|
||||||
|
onProgress: false,
|
||||||
});
|
});
|
||||||
await server.initialize();
|
await server.initialize();
|
||||||
console.log("Relative path: "+relativePath)
|
|
||||||
let maps = fs.readdirSync(path.join(relativePath, "_testData_createMap", "saves"));
|
let maps = fs.readdirSync(path.join(relativePath, "_testData_createMap", "saves"));
|
||||||
assert.equal(maps.length, 0);
|
assert.equal(maps.length, 0);
|
||||||
await server.createMap();
|
await server.createMap();
|
||||||
let maps2 = fs.readdirSync(path.join(relativePath, "_testData_createMap", "saves"));
|
let maps2 = fs.readdirSync(path.join(relativePath, "_testData_createMap", "saves"));
|
||||||
assert(maps2.includes("map.zip"));
|
assert(maps2.includes("map.zip"));
|
||||||
|
|
||||||
server.deleteInstance();
|
await server.deleteInstance();
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
it("supports custom filenames", async function(){
|
||||||
|
this.timeout(20000);
|
||||||
|
let relativePath = __dirname.replace(appRootPath, "").substr(1);
|
||||||
|
let server = new factorioServer({
|
||||||
|
instanceDirectory: path.join(relativePath, "_testData_createMap_2"),
|
||||||
|
instanceName: "Unit testing instance",
|
||||||
|
onProgress: false,
|
||||||
|
});
|
||||||
|
await server.initialize();
|
||||||
|
|
||||||
|
let maps = fs.readdirSync(path.join(relativePath, "_testData_createMap_2", "saves"));
|
||||||
|
assert.equal(maps.length, 0);
|
||||||
|
await server.createMap("mapWithCustomName.zip");
|
||||||
|
let maps2 = fs.readdirSync(path.join(relativePath, "_testData_createMap_2", "saves"));
|
||||||
|
assert(maps2.includes("mapWithCustomName.zip"));
|
||||||
|
|
||||||
|
await server.deleteInstance();
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in new issue
Block a user