1
0
forked from mirrors/0ad

JS file listing cleanup, refs #4868.

Add global filelisting helper.
Remove getXMLFileList and getJSONFileList from the unsorted
gui/common/functions_utility.js.
Make hidden maps prefix more transparent.
Remove hardcoded magic numbers that are string lengths.
Shorten loading screen code and separate pathnames from code.

Differential Revision: https://code.wildfiregames.com/D1107
Reviewed By: bb
This was SVN commit r20692.
This commit is contained in:
elexis
2017-12-26 22:44:37 +00:00
parent fb444d4165
commit 58b2e2baf4
6 changed files with 40 additions and 69 deletions
@@ -59,3 +59,12 @@ function basename(path)
{
return path.slice(path.lastIndexOf("/") + 1);
}
/**
* 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));
}
@@ -7,36 +7,6 @@ var g_SoundNotifications = {
"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.
*/
@@ -1444,14 +1444,13 @@ function getFilteredMaps(filterFunc)
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);
@@ -1,3 +1,18 @@
/**
* Path to a file containing quotes of historical figures.
*/
var g_QuotesFile = "gui/text/quotes.txt";
/**
* Directory storing txt files containing the gameplay tips.
*/
var g_TipsTextPath = "gui/text/tips/";
/**
* Directory storing the PNG images with filenames corresponding to the tip text files.
*/
var g_TipsImagePath = "loading/tips/";
var g_Data;
var g_EndPieceWidth = 16;
@@ -7,29 +22,14 @@ function init(data)
Engine.SetCursor("cursor-wait");
// Get tip image and corresponding tip text
let tipTextLoadingArray = Engine.ListDirectoryFiles("gui/text/tips/", "*.txt", false);
let tipFile = pickRandom(listFiles(g_TipsTextPath, ".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(g_TipsTextPath + tipFile + ".txt")).split("\n");
Engine.GetGUIObjectByName("tipTitle").caption = tipText.shift();
Engine.GetGUIObjectByName("tipText").caption = tipText.join("\n");
Engine.GetGUIObjectByName("tipImage").sprite = "stretched:" + g_TipsImagePath + tipFile + ".png";
}
else
error("Failed to find any matching tips for the loading screen.");
@@ -51,18 +51,13 @@ function init(data)
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(g_QuotesFile).filter(line => line)));
}
function displayProgress()
@@ -462,12 +462,10 @@ var g_NotificationsTypes =
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;
@@ -50,12 +50,12 @@ DataTemplateManager.prototype.GetAuraTemplate = function(template)
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()