Index: binaries/data/mods/public/globalscripts/Templates.js =================================================================== --- binaries/data/mods/public/globalscripts/Templates.js +++ binaries/data/mods/public/globalscripts/Templates.js @@ -1,4 +1,12 @@ /** + * Returns names of files found in the given directory, stripping the directory path and file extension. + */ +function listFiles(path, extension, recurse) +{ + return Engine.ListDirectoryFiles(path, "*" + extension, recurse).map(filename => filename.slice(path.length, -extension.length)); +} + +/** * Loads history and gameplay data of all civs. * * @param selectableOnly {boolean} - Only load civs that can be selected Index: binaries/data/mods/public/gui/common/functions_utility.js =================================================================== --- binaries/data/mods/public/gui/common/functions_utility.js +++ binaries/data/mods/public/gui/common/functions_utility.js @@ -7,36 +7,6 @@ "nick": { "soundfile": "audio/interface/ui/chat_alert.ogg", "threshold": 3000 } }; -// Get list of XML files in pathname with recursion, excepting those starting with _ -function getXMLFileList(pathname) -{ - var files = Engine.ListDirectoryFiles(pathname, "*.xml", true); - - var result = []; - - // Get only subpath from filename and discard extension - for (var i = 0; i < files.length; ++i) - { - var file = files[i]; - file = file.substring(pathname.length, file.length - 4); - - // Split path into directories so we can check for beginning _ character - var tokens = file.split("/"); - - if (tokens[tokens.length - 1][0] != "_") - result.push(file); - } - - return result; -} - -function getJSONFileList(pathname) -{ - // Remove the path and extension from each name, since we just want the filename - return Engine.ListDirectoryFiles(pathname, "*.json", false).map( - filename => filename.substring(pathname.length, filename.length - 5)); -} - /** * Returns translated history and gameplay data of all civs, optionally including a mock gaia civ. */ Index: binaries/data/mods/public/gui/gamesetup/gamesetup.js =================================================================== --- binaries/data/mods/public/gui/gamesetup/gamesetup.js +++ binaries/data/mods/public/gui/gamesetup/gamesetup.js @@ -1441,14 +1441,13 @@ return []; } - let mapFiles = g_GameAttributes.mapType == "random" ? - getJSONFileList(g_GameAttributes.mapPath) : - getXMLFileList(g_GameAttributes.mapPath); - let maps = []; // TODO: Should verify these are valid maps before adding to list - for (let mapFile of mapFiles) + for (let mapFile of listFiles(g_GameAttributes.mapPath, g_GameAttributes.mapType == "random" ? ".json" : ".xml", false)) { + if (mapFile.startsWith("_")) + continue; + let file = g_GameAttributes.mapPath + mapFile; let mapData = loadMapData(file); Index: binaries/data/mods/public/gui/loading/loading.js =================================================================== --- binaries/data/mods/public/gui/loading/loading.js +++ binaries/data/mods/public/gui/loading/loading.js @@ -7,32 +7,16 @@ Engine.SetCursor("cursor-wait"); - // Get tip image and corresponding tip text - let tipTextLoadingArray = Engine.ListDirectoryFiles("gui/text/tips/", "*.txt", false); + let tipsPath = "gui/text/tips/"; + let tipFile = pickRandom(listFiles(tipsPath, ".txt", false)); - if (tipTextLoadingArray.length > 0) + if (tipFile) { - // Set tip text - let tipTextFilePath = pickRandom(tipTextLoadingArray); - let tipText = Engine.TranslateLines(Engine.ReadFile(tipTextFilePath)); - - if (tipText) - { - let index = tipText.indexOf("\n"); - let tipTextTitle = tipText.substring(0, index); - let tipTextMessage = tipText.substring(index); - Engine.GetGUIObjectByName("tipTitle").caption = tipTextTitle ? tipTextTitle : ""; - Engine.GetGUIObjectByName("tipText").caption = tipTextMessage ? tipTextMessage : ""; - } - - // Set tip image - let fileName = tipTextFilePath.substring(tipTextFilePath.lastIndexOf("/") + 1).replace(".txt", ".png"); - let tipImageFilePath = "loading/tips/" + fileName; - let sprite = "stretched:" + tipImageFilePath; - Engine.GetGUIObjectByName("tipImage").sprite = sprite ? sprite : ""; + let tipText = Engine.TranslateLines(Engine.ReadFile(tipsPath + tipFile + ".txt")).split("\n"); + Engine.GetGUIObjectByName("tipTitle").caption = tipText.shift(); + Engine.GetGUIObjectByName("tipText").caption = tipText.join("\n") + Engine.GetGUIObjectByName("tipImage").sprite = "stretched:loading/tips/" + tipFile + ".png"; } - else - error("Failed to find any matching tips for the loading screen."); // janwas: main loop now sets progress / description, but that won't // happen until the first timeslice completes, so set initial values. @@ -51,18 +35,13 @@ case "random": loadingMapName.caption = sprintf(translate("Generating ā€œ%(map)sā€"), { "map": mapName }); break; - - default: - error("Unknown map type: " + data.attribs.mapType); } } Engine.GetGUIObjectByName("progressText").caption = ""; Engine.GetGUIObjectByName("progressbar").caption = 0; - // Pick a random quote of the day (each line is a separate tip). - let quoteArray = Engine.ReadFileLines("gui/text/quotes.txt").filter(line => line); - Engine.GetGUIObjectByName("quoteText").caption = translate(pickRandom(quoteArray)); + Engine.GetGUIObjectByName("quoteText").caption = translate(pickRandom(Engine.ReadFileLines("gui/text/quotes.txt").filter(line => line))); } function displayProgress() Index: binaries/data/mods/public/gui/session/messages.js =================================================================== --- binaries/data/mods/public/gui/session/messages.js +++ binaries/data/mods/public/gui/session/messages.js @@ -462,12 +462,10 @@ function getCheatsData() { let cheats = {}; - for (let fileName of getJSONFileList("simulation/data/cheats/")) + for (let fileName of Engine.ListDirectoryFiles("simulation/data/cheats/", "*.json", false)) { - let currentCheat = Engine.ReadJSONFile("simulation/data/cheats/" + fileName + ".json"); - if (!currentCheat) - continue; - if (Object.keys(cheats).indexOf(currentCheat.Name) !== -1) + let currentCheat = Engine.ReadJSONFile(fileName); + if (cheats[currentCheat.Name]) warn("Cheat name '" + currentCheat.Name + "' is already present"); else cheats[currentCheat.Name] = currentCheat.Data; Index: binaries/data/mods/public/simulation/components/DataTemplateManager.js =================================================================== --- binaries/data/mods/public/simulation/components/DataTemplateManager.js +++ binaries/data/mods/public/simulation/components/DataTemplateManager.js @@ -50,12 +50,12 @@ DataTemplateManager.prototype.ListAllTechs = function() { - return Engine.ListDirectoryFiles(this.technologiesPath, "*.json", true).map(file => file.slice(this.technologiesPath.length, -".json".length)); + return listFiles(this.technologiesPath, ".json", true); }; DataTemplateManager.prototype.ListAllAuras = function() { - return Engine.ListDirectoryFiles(this.aurasPath, "*.json", true).map(file => file.slice(this.aurasPath.length, -".json".length)); + return listFiles(this.aurasPath, ".json", true); }; DataTemplateManager.prototype.GetAllTechs = function()