mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-22 00:13:12 +09:00
Restructured scenario_loader a bit
This commit is contained in:
1 parent
3a87aa2959
commit
7352ee3751
1 file changed
+77
-48
@@ -11,59 +11,69 @@ module.exports = class remoteCommands {
|
|||||||
let hotpatchInstallStatus = await this.checkHotpatchInstallation();
|
let hotpatchInstallStatus = await this.checkHotpatchInstallation();
|
||||||
messageInterface("Hotpach installation status: "+hotpatchInstallStatus);
|
messageInterface("Hotpach installation status: "+hotpatchInstallStatus);
|
||||||
if(hotpatchInstallStatus){
|
if(hotpatchInstallStatus){
|
||||||
let scenarioDir = "scenarios";
|
await this.loadScenariosFromFolder("scenarios") // located in project root folder (with sharedMods, sharedPlugins etc)
|
||||||
await fs.ensureDir(scenarioDir)
|
let plugins = new AsyncArray((await fs.readdir(path.join(__dirname, "../"))))
|
||||||
let scenarios = await fs.readdir(scenarioDir);
|
plugins.filterAsync(async plugin => {
|
||||||
for(let i = 0; i < scenarios.length; i++){
|
// check if plugin is a folder and if plugin has a scenarios folder
|
||||||
messageInterface(`Loading scenario ${scenarios[i]}`)
|
|
||||||
let startTime = Date.now();
|
})
|
||||||
let scenPath = path.join(scenarioDir, scenarios[i]);
|
for(let g = 0; g < plugins.length; g++){
|
||||||
let stat = await fs.stat(scenPath);
|
|
||||||
if(stat.isDirectory){
|
|
||||||
async function loadFiles(dir, rootdir){
|
|
||||||
let returnFiles = [];
|
|
||||||
let entries = await fs.readdir(path.join(rootdir, dir));
|
|
||||||
for(let o = 0; o < entries.length; o++){
|
|
||||||
let stat = await fs.stat(path.join(rootdir, dir, entries[o]));
|
|
||||||
if(stat.isDirectory()){
|
|
||||||
// recurse
|
|
||||||
let subFolder = await loadFiles(path.join(dir, entries[o]), rootdir);
|
|
||||||
returnFiles = returnFiles.concat(subFolder);
|
|
||||||
} else if(entries[o].split(".")[entries[o].split(".").length-1].toLowerCase() == "lua"){
|
|
||||||
// if it is a Lua file, read its contents
|
|
||||||
// let data = await fs.readFile(path.join(rootdir, dir, entries[o]));
|
|
||||||
let data = await clusterTools.getLua(path.join(rootdir, dir, entries[o]), false);
|
|
||||||
returnFiles.push({
|
|
||||||
name: path.join(dir, entries[o].split(".")[0]),
|
|
||||||
data,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return returnFiles;
|
|
||||||
}
|
|
||||||
let fileMap = await loadFiles("", scenPath);
|
|
||||||
let files = {};
|
|
||||||
fileMap.forEach(map => {
|
|
||||||
files[map.name] = map.data;
|
|
||||||
console.log(map.name);
|
|
||||||
});
|
|
||||||
|
|
||||||
let fileImportString = `\{`;
|
|
||||||
for(let k in files){
|
|
||||||
let name = k.replace(/\\/g, '/');
|
|
||||||
name = name.replace(/ /g, '');
|
|
||||||
// make sure *not* to include control.lua as it is provided as a seperate argument
|
|
||||||
if(name != "control") fileImportString += `["${name}"] = '${files[k]}', `;
|
|
||||||
}
|
|
||||||
fileImportString += `\}`;
|
|
||||||
if(files.control) var returnValue = await messageInterface(`/silent-command remote.call('hotpatch', 'update', '${scenarios[i]}', '1.0.0', '${files.control}', ${fileImportString})`);
|
|
||||||
if(returnValue) messageInterface(returnValue);
|
|
||||||
messageInterface(`Loaded scenario ${scenarios[i]} in ${Math.floor(Date.now()-startTime)}ms`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
}
|
}
|
||||||
|
async loadScenariosFromFolder(scenarioDir){
|
||||||
|
await fs.ensureDir(scenarioDir)
|
||||||
|
let scenarios = await fs.readdir(scenarioDir);
|
||||||
|
for(let i = 0; i < scenarios.length; i++){
|
||||||
|
messageInterface(`Loading scenario ${scenarios[i]}`)
|
||||||
|
let startTime = Date.now();
|
||||||
|
let scenPath = path.join(scenarioDir, scenarios[i]);
|
||||||
|
let stat = await fs.stat(scenPath);
|
||||||
|
if(stat.isDirectory){
|
||||||
|
async function loadFiles(dir, rootdir){
|
||||||
|
let returnFiles = [];
|
||||||
|
let entries = await fs.readdir(path.join(rootdir, dir));
|
||||||
|
for(let o = 0; o < entries.length; o++){
|
||||||
|
let stat = await fs.stat(path.join(rootdir, dir, entries[o]));
|
||||||
|
if(stat.isDirectory()){
|
||||||
|
// recurse
|
||||||
|
let subFolder = await loadFiles(path.join(dir, entries[o]), rootdir);
|
||||||
|
returnFiles = returnFiles.concat(subFolder);
|
||||||
|
} else if(entries[o].split(".")[entries[o].split(".").length-1].toLowerCase() == "lua"){
|
||||||
|
// if it is a Lua file, read its contents
|
||||||
|
// let data = await fs.readFile(path.join(rootdir, dir, entries[o]));
|
||||||
|
let data = await clusterTools.getLua(path.join(rootdir, dir, entries[o]), false);
|
||||||
|
returnFiles.push({
|
||||||
|
name: path.join(dir, entries[o].split(".")[0]),
|
||||||
|
data,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return returnFiles;
|
||||||
|
}
|
||||||
|
let fileMap = await loadFiles("", scenPath);
|
||||||
|
let files = {};
|
||||||
|
fileMap.forEach(map => {
|
||||||
|
files[map.name] = map.data;
|
||||||
|
console.log(map.name);
|
||||||
|
});
|
||||||
|
|
||||||
|
let fileImportString = `\{`;
|
||||||
|
for(let k in files){
|
||||||
|
let name = k.replace(/\\/g, '/');
|
||||||
|
name = name.replace(/ /g, '');
|
||||||
|
// make sure *not* to include control.lua as it is provided as a seperate argument
|
||||||
|
if(name != "control") fileImportString += `["${name}"] = '${files[k]}', `;
|
||||||
|
}
|
||||||
|
fileImportString += `\}`;
|
||||||
|
if(files.control) var returnValue = await messageInterface(`/silent-command remote.call('hotpatch', 'update', '${scenarios[i]}', '1.0.0', '${files.control}', ${fileImportString})`);
|
||||||
|
if(returnValue) messageInterface(returnValue);
|
||||||
|
messageInterface(`Loaded scenario ${scenarios[i]} in ${Math.floor(Date.now()-startTime)}ms`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
async checkHotpatchInstallation(){
|
async checkHotpatchInstallation(){
|
||||||
let yn = await this.messageInterface("/silent-command if remote.interfaces['hotpatch'] then rcon.print('true') else rcon.print('false') end");
|
let yn = await this.messageInterface("/silent-command if remote.interfaces['hotpatch'] then rcon.print('true') else rcon.print('false') end");
|
||||||
yn = yn.replace(/(\r\n\t|\n|\r\t)/gm, "");
|
yn = yn.replace(/(\r\n\t|\n|\r\t)/gm, "");
|
||||||
@@ -96,3 +106,22 @@ function readDirectorySynchronously(directory) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
class AsyncArray extends Array {
|
||||||
|
constructor(arr) {
|
||||||
|
// this.data = arr; // In place of Array subclassing
|
||||||
|
}
|
||||||
|
|
||||||
|
filterAsync(predicate) {
|
||||||
|
// Take a copy of the array, it might mutate by the time we've finished
|
||||||
|
const data = Array.from(this.data);
|
||||||
|
// Transform all the elements into an array of promises using the predicate
|
||||||
|
// as the promise
|
||||||
|
return Promise.all(data.map((element, index) => predicate(element, index, data)))
|
||||||
|
// Use the result of the promises to call the underlying sync filter function
|
||||||
|
.then(result => {
|
||||||
|
return data.filter((element, index) => {
|
||||||
|
return result[index];
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user