1
0
Fork 0
This repository has been archived on 2024-03-23. You can view files and clone it, but cannot push or open issues or pull requests.
citra-games-wiki/validation/app.js

416 lines
13 KiB
JavaScript
Raw Normal View History

2017-06-07 02:59:59 +00:00
const fs = require('fs');
const path = require('path');
const config = require('./config.json');
const groupBy = require('group-by');
const sizeOf = require('image-size');
const readChunk = require('read-chunk');
const imageType = require('image-type');
const toml = require('toml');
let currentGame = null;
let errors = [];
2017-06-11 10:12:29 +00:00
// Catch non-formatting errors
let miscError = false;
2017-06-07 02:59:59 +00:00
function getDirectories(srcpath) {
return fs.readdirSync(srcpath)
.filter(file => fs.lstatSync(path.join(srcpath, file)).isDirectory())
}
function getFiles(srcpath) {
return fs.readdirSync(srcpath)
.filter(file => fs.lstatSync(path.join(srcpath, file)).isFile())
}
2017-06-11 10:12:29 +00:00
/// Check that a filename matches the following:
/// [any of a-z, A-Z, 0-9, a '-' or a '_'](one or more) . [a-z](two or three)
function isValidFilename(name) {
return name.match(/^([a-zA-Z0-9_\-])+\.([a-z]){2,3}$/);
}
2017-06-07 02:59:59 +00:00
/// Validates that a image is correctly sized and of the right format.
function validateImage(path, config) {
if (fs.existsSync(path) === false) {
validationError(`Image \"${path}\"' was not found at ${path}.`);
} else {
// Read first 12 bytes (enough for '%PNG', etc)
const buffer = readChunk.sync(path, 0, 12);
const type = imageType(buffer).mime;
if (type !== config.type) {
validationError(`Incorrect format of image (${type} != ${config.type})`);
}
let dimensions = sizeOf(path);
2017-06-11 10:43:45 +00:00
for (const sizeIndex in config.sizes) {
const size = config.sizes[sizeIndex];
if (dimensions.width === size.width && dimensions.height === size.height) {
return;
}
2017-06-11 10:43:45 +00:00
}
2017-06-11 10:43:45 +00:00
// Build our error message
let possibleSizes = config.sizes.reduce((acc, curVal) => {
if (acc.length !== 0) {
acc += ", ";
}
2017-06-11 10:43:45 +00:00
acc += `${curVal.width} x ${curVal.height}`;
return acc;
}, "");
validationError(`Image \"${path}\"'s dimensions are ${dimensions.width} x ${dimensions.height} ` +
2017-06-11 10:43:45 +00:00
`instead of the any of the following: ${possibleSizes}`);
2017-06-07 02:59:59 +00:00
}
}
/// Validates that a folder (if it exists) of images contains images that are
// correctly sized and of the right format.
function validateDirImages(path, config) {
// TODO: Do we want to enforce having screenshots?
if (fs.existsSync(path)) {
const files = getFiles(path);
2017-06-11 10:12:29 +00:00
files.forEach(file => {
if (!isValidFilename(file)) {
validationError(`File \"${file}\" contains bad characters!`);
2017-06-11 10:43:45 +00:00
} else {
validateImage(`${path}/${file}`, config);
2017-06-11 10:12:29 +00:00
}
});
2017-06-07 02:59:59 +00:00
}
}
// TODO: Could these errors be prefixed with the section/line in which they come from?
/// Validates the existance of a particular entry in a structure
function validateExists(struct, name) {
if (struct[name] === undefined) {
validationError("Field \"" + name + "\" missing");
return false;
} else {
return true;
}
}
/// Validates the existence of a particular entry in a structure, and
/// ensures that it meets a particular set of criteria.
function validateContents(struct, name, test) {
if (validateExists(struct, name)) {
test(struct[name]);
}
}
/// Validates the existence of a particular entry in a structure, and
/// ensures that it is not a empty string.
function validateNotEmpty(struct, name) {
validateContents(struct, name, field => {
if (typeof field !== "string") {
validationError("Field \"" + name + "\" is not a string");
} else if (field === "") {
validationError("Field \"" + name + "\" is empty");
}
});
}
/// Validates the existence of a particular entry in a structure, and
/// ensures that it is not a empty string.
function validateIsBoolean(struct, name) {
if (struct[name] !== false && struct[name] !== true) {
validationError("Field \"" + name + "\" is not a boolean");
}
}
/// Validates pattern of YYYY-MM-DD in a field of a structure.
function validateIsDate(struct, name) {
validateContents(struct, name, field => {
2017-06-11 10:12:29 +00:00
if (!field.match(/^[0-9]{4}-((0[1-9])|(1[0-2]))-((0[1-9])|([1-2][0-9])|(3[0-1]))$/)) {
2017-06-07 02:59:59 +00:00
validationError(`\"${name}\" is not a valid date (\"${field}\").`);
}
});
}
function validateFileExists(dir) {
if (fs.existsSync(dir) === false) {
validationError(`\"${dir}\" does not exist!`);
return false;
}
return true;
}
/// Validates a TOML document
function validateTOML(path) {
if (fs.existsSync(path) === false) {
validationError(`TOML was not found at ${path}.`);
return;
}
let rawContents = fs.readFileSync(path);
let tomlDoc;
try {
tomlDoc = toml.parse(rawContents);
} catch (e) {
validationError("TOML parse error (" + e.line + "): " + e.message);
return;
}
// Check the global header section
validateNotEmpty(tomlDoc, "title");
validateNotEmpty(tomlDoc, "description");
if (tomlDoc["github_issues"] !== undefined) {
validateContents(tomlDoc, "github_issues", field => {
if (Array.isArray(field) === false) {
validationError("Github issues field is not an array!")
} else {
// Validate each individual entry
2017-06-11 10:43:45 +00:00
field.forEach(elem => {
if (typeof elem !== "number") {
2017-06-07 02:59:59 +00:00
validationError("Github issues entry is not a number!")
}
2017-06-11 10:43:45 +00:00
});
2017-06-07 02:59:59 +00:00
}
});
}
if (tomlDoc["gametypes"] !== undefined) {
validateContents(tomlDoc, "gametypes", field => {
if (config.gametypes.indexOf(field) === -1) {
validationError(`Could not find gametype \"${field}\"!`);
}
if (field === "vc") {
validateContents(tomlDoc, "vc_system", field => {
2017-06-11 10:30:44 +00:00
if (config.vc_systems.indexOf(field) === -1) {
validationError(`Could not find VC console \"${field}\"!`);
}
});
}
});
}
2017-06-07 02:59:59 +00:00
let section;
// Check each release individually
if (tomlDoc["releases"] !== undefined) {
section = tomlDoc["releases"];
2017-06-11 10:43:45 +00:00
section.forEach(release => {
2017-06-07 02:59:59 +00:00
validateContents(release, "title", field => {
if (field.length !== 16) {
validationError(`Release: Game title ID has an invalid length`);
2018-01-02 21:19:52 +00:00
} else if (!field.match(/^([A-Z0-9]){16}$/)) {
validationError(`Release: Game title ID is not a hexadecimal ID`);
2017-06-07 02:59:59 +00:00
}
});
validateContents(release, "region", field => {
if (config.regions.indexOf(field) === -1) {
validationError(`Release: Invalid region ${field}`);
2017-06-07 02:59:59 +00:00
}
});
validateIsDate(release, "release_date");
2017-06-11 10:43:45 +00:00
});
2017-06-07 02:59:59 +00:00
} else {
validationError("No releases.")
}
let maxCompatibility = 999;
// Check each testcase individually
if (tomlDoc["testcases"] !== undefined) {
section = tomlDoc["testcases"];
2017-06-11 10:43:45 +00:00
section.forEach(testcase => {
validateContents(testcase, "title", field => {
if (field.length !== 16) {
validationError(`Testcase: Game title ID has an invalid length`);
2018-01-02 21:19:52 +00:00
} else if (!field.match(/^([A-Z0-9]){16}$/)) {
validationError(`Testcase: Game title ID is not a hexadecimal ID`);
}
});
2017-06-07 02:59:59 +00:00
validateNotEmpty(testcase, "compatibility");
if (testcase["compatibility"] !== undefined) {
let compat = parseInt(testcase["compatibility"]);
if (compat < maxCompatibility) {
maxCompatibility = compat;
}
}
validateIsDate(testcase, "date");
validateContents(testcase, "version", test => {
if (test.length !== 12) {
validationError(`Testcase: Version is of incorrect length`);
2017-06-07 02:59:59 +00:00
} else if (!test.startsWith("HEAD-")) {
validationError(`Testcase: Unknown version commit source`);
2017-06-07 02:59:59 +00:00
}
});
validateNotEmpty(testcase, "author");
validateNotEmpty(testcase, "cpu");
validateNotEmpty(testcase, "gpu");
validateNotEmpty(testcase, "os");
2017-06-11 10:43:45 +00:00
});
// Validate dates are properly ordered
section.reduce(function(previousValue, currentValue) {
if (typeof previousValue === "undefined" || previousValue.date <= currentValue.date) {
return currentValue;
}
validationError("Test case dates are not properly sorted in ascending order.");
});
2017-06-07 02:59:59 +00:00
}
// We only check these if we have a known test result (we cannot know if a game needs
// resources if it doesn't even run!)
if (maxCompatibility < 5) {
validateIsBoolean(tomlDoc, "needs_system_files");
validateIsBoolean(tomlDoc, "needs_shared_font");
}
}
/// Validates the basic structure of a save game's TOML. Assumes it exists.
function validateSaveTOML(path) {
let rawContents = fs.readFileSync(path);
let tomlDoc;
try {
tomlDoc = toml.parse(rawContents);
} catch (e) {
validationError("TOML parse error (" + e.line + "): " + e.message);
return;
}
// Check the global header section
validateNotEmpty(tomlDoc, "title");
validateNotEmpty(tomlDoc, "description");
validateNotEmpty(tomlDoc, "author");
validateContents(tomlDoc, "title_id", field => {
if (field.length !== 16) {
validationError(`Game save data: Game title ID has an invalid length`);
2018-01-02 21:19:52 +00:00
} else if (!field.match(/^([A-Z0-9]){16}$/)) {
2017-06-07 02:59:59 +00:00
validationError(`Game save data: Game title ID is not a hexadecimal ID`);
}
});
}
/// Validates that a save is actually a .zip.
function validateSaveZip(path) {
// TODO: Would a node library MIME check be better?
const zipHeader = Buffer.from([0x50, 0x4B, 0x03, 0x04]);
const data = readChunk.sync(path, 0, 4);
if (zipHeader.compare(data) !== 0) {
validationError(`File ${path} is not a .zip!`)
}
}
/// Validates a folder of game saves.
function validateSaves(dir) {
if (fs.existsSync(dir) === false) {
return;
}
const files = getFiles(dir);
2017-06-11 10:12:29 +00:00
files.forEach(file => {
if (!isValidFilename(file)) {
validationError(`File \"${file}\" contains bad characters!`);
}
});
2017-06-07 02:59:59 +00:00
// Strip extensions, so we know what save 'groups' we are dealing with
const strippedFiles = files.map(file => {
return file.substr(0, file.lastIndexOf("."))
});
const groups = strippedFiles.filter((element, i) => {
return strippedFiles.indexOf(element) === i
});
// Check each group
2017-06-11 10:43:45 +00:00
groups.forEach(group => {
2017-06-07 02:59:59 +00:00
if (validateFileExists(`${dir}/${group}.dat`)) {
validateSaveTOML(`${dir}/${group}.dat`);
}
if (validateFileExists(`${dir}/${group}.zip`)) {
validateSaveZip(`${dir}/${group}.zip`);
}
2017-06-11 10:43:45 +00:00
});
2017-06-07 02:59:59 +00:00
}
function validationError(err) {
errors.push({game: currentGame, error: err});
}
// Loop through each game folder, validating each game.
getDirectories(config.directory).forEach(function (game) {
try {
if (game === '.git' || game === '_validation') {
return;
}
let inputDirectoryGame = `${config.directory}/${game}`;
currentGame = game;
2017-06-11 10:30:44 +00:00
// Check that everything is lowercase and is a known file.
2017-06-11 10:12:29 +00:00
getFiles(inputDirectoryGame).forEach(file => {
2017-06-11 10:30:44 +00:00
if (config.permitted_files.indexOf(file) === -1) {
validationError(`Unknown file \"${file}\"!`);
} else if (!isValidFilename(file)) {
2017-06-11 10:12:29 +00:00
validationError(`File \"${file}\" contains bad characters!`);
}
});
2017-06-11 10:30:44 +00:00
// Check that all directories are known.
getDirectories(inputDirectoryGame).forEach(file => {
if (config.permitted_dirs.indexOf(file) === -1) {
validationError(`Unknown directory \"${file}\"!`);
}
});
2017-06-07 02:59:59 +00:00
// Verify the game's boxart.
validateImage(`${inputDirectoryGame}/${config.boxart.filename}`, config.boxart);
// Verify the game's image.
validateImage(`${inputDirectoryGame}/${config.icon.filename}`, config.icon);
// Verify the game's metadata.
validateTOML(`${inputDirectoryGame}/${config.data.filename}`);
// Verify the game's screenshots.
validateDirImages(`${inputDirectoryGame}/${config.screenshots.dirname}`,
config.screenshots);
// Verify the game's save files.
validateSaves(`${inputDirectoryGame}/${config.saves.dirname}`);
} catch (ex) {
console.warn(`${game} has encountered an unexpected error.`);
console.error(ex);
2017-06-11 10:12:29 +00:00
miscError = true;
2017-06-07 02:59:59 +00:00
}
});
2017-06-11 10:12:29 +00:00
if (errors.length > 0 || miscError) {
2017-06-07 02:59:59 +00:00
console.warn('Validation completed with errors.');
const groups = groupBy(errors, "game");
for (let key in groups) {
let group = groups[key];
console.info(` ${key}:`);
2017-06-11 10:43:45 +00:00
group.forEach(issue => {
console.info(` - ${issue.error}`);
});
2017-06-07 02:59:59 +00:00
}
process.exit(1);
} else {
console.info('Validation completed without errors.');
process.exit(0);
}