Revamped scenario loader for cross platform compat

This commit is contained in:
Daniel Vestøl committed 2018-11-15 21:37:42 +01:00
1 parent 12a0ebf8d1
commit 18c486855d
1 file changed
+28 -15
+28 -15
View File
@@ -15,26 +15,38 @@ module.exports = class remoteCommands {
await fs.ensureDir(scenarioDir) await fs.ensureDir(scenarioDir)
let scenarios = await fs.readdir(scenarioDir); let scenarios = await fs.readdir(scenarioDir);
for(let i = 0; i < scenarios.length; i++){ for(let i = 0; i < scenarios.length; i++){
let startTime = Date.now();
let scenPath = path.join(scenarioDir, scenarios[i]); let scenPath = path.join(scenarioDir, scenarios[i]);
let stat = await fs.stat(scenPath); let stat = await fs.stat(scenPath);
if(stat.isDirectory){ if(stat.isDirectory){
// let scenario = await fs.readdir(scenPath, {withFileTypes: true}); async function loadFiles(dir, rootdir){
let returnFiles = [];
// this function is bad in terms of scope since it relies on a global for return values, should be refactored. let entries = await fs.readdir(path.join(rootdir, dir));
filesCollection = []; for(let o = 0; o < entries.length; o++){
readDirectorySynchronously(scenPath); let stat = await fs.stat(path.join(rootdir, dir, entries[o]));
let scenario = filesCollection; if(stat.isDirectory()){
let files = {}; // recurse
for(let o = 0; o < scenario.length; o++){ let subFolder = await loadFiles(path.join(dir, entries[o]), rootdir);
let fileName = scenario[o]; returnFiles = returnFiles.concat(subFolder);
if(fileName.split(".")[1] && fileName.split(".")[1].toLowerCase() == "lua"){ } else if(entries[o].split(".")[entries[o].split(".").length-1].toLowerCase() == "lua"){
messageInterface(`Loading `/*${scenarios[i]}*/+`${fileName}`); // if it is a Lua file, read its contents
let code = await clusterTools.getLua(/*path.join(scenPath, */fileName/*)*/, false); // let data = await fs.readFile(path.join(rootdir, dir, entries[o]));
// trim away external path from filenames let data = await clusterTools.getLua(path.join(rootdir, dir, entries[o]), false);
fileName = fileName.replace("scenarios\\factoriommoscenarios\\", "").replace("scenarios/factoriommoscenarios", ""); returnFiles.push({
files[fileName.split(".")[0]] = code; 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 = `\{`; let fileImportString = `\{`;
for(let k in files){ for(let k in files){
let name = k.replace(/\\/g, '/'); let name = k.replace(/\\/g, '/');
@@ -44,6 +56,7 @@ module.exports = class remoteCommands {
fileImportString += `\}`; fileImportString += `\}`;
if(files.control) var returnValue = await messageInterface(`/silent-command remote.call('hotpatch', 'update', '${scenarios[i]}', '1.0.0', '${files.control}', ${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); if(returnValue) messageInterface(returnValue);
messageInterface(`Loaded scenario ${scenarios[i]} in ${Math.floor(Date.now()-startTime)}ms`);
} }
} }
} }