Portal64/tools/generate_level_list.js

40 lines
1.2 KiB
JavaScript
Raw Normal View History

2022-04-17 22:35:32 -04:00
const fs = require('fs');
const path = require('path');
function generateInclude(outputLocation, headerLocation) {
return `#include "${path.relative(path.dirname(outputLocation), headerLocation)}"`
}
2022-05-06 00:49:52 -04:00
const InvalidTokenCharacter = /[^A-Za-z0-9_]/gim;
function generateLevelName(outputLocation, headerLocation) {
const relative = path.relative(path.dirname(outputLocation), headerLocation).slice(0, -2);
return relative.replace(InvalidTokenCharacter, '_') + '_level';
}
function generateLevelList(outputLocation, headerLocations) {
return `struct LevelDefinition* gLevelList[] = {
${headerLocations.map(headerLocation => ` &${generateLevelName(outputLocation, headerLocation)},`).join('\n')}
};
`;
}
2022-04-17 22:35:32 -04:00
function generateData(outputLocation, headerLocations) {
return `#ifndef __BUILD_LEVEL_LIST_H__
#define __BUILD_LEVEL_LIST_H__
2022-05-06 00:49:52 -04:00
#include "levels/level_definition.h"
2022-04-17 22:35:32 -04:00
${headerLocations.map(headerLocation => generateInclude(outputLocation, headerLocation)).join('\n')}
2022-05-06 00:49:52 -04:00
#define LEVEL_COUNT ${headerLocations.length}
${generateLevelList(outputLocation, headerLocations)}
2022-04-17 22:35:32 -04:00
#endif
`;
}
const output = process.argv[2];
fs.writeFileSync(output, generateData(output, process.argv.slice(3)));