Skip to content

Instantly share code, notes, and snippets.

@PROPHESSOR
Last active December 16, 2018 04:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PROPHESSOR/e5a61ff10439af620e93269c48b7ce36 to your computer and use it in GitHub Desktop.
Save PROPHESSOR/e5a61ff10439af620e93269c48b7ce36 to your computer and use it in GitHub Desktop.
Text parser from Doom 3 Map
/*
* Doom 3 Map Text parser
*
* Copyright (c) PROPHESSOR 08.12.2018
* MIT License
*
*/
const fs = require('fs');
const REGEX = {
textTip: /"text_tip"\s+"(.*)"\n/g,
itemName: /"item_name"\s+"(.*)"\n/g,
itemAction: /"item_action"\s+"(.*)"\n/g,
guiParm: /"gui_parm(\d+)"\s+"(.*)"\n/g,
text: /"text"\s+"(.*)"\n/g,
objectiveTitle: /"objectivetitle"\s+"(.*)"\n/g,
objectiveText: /"objectivetext"\s+"(.*)"\n/g,
invName: /"inv_name"\s+"(.*)"\n/g,
npcName: /"npc_name"\s+"(.*)"\n/g
}
function parse(file, filename) {
const out = [];
let match;
while ((match = REGEX.textTip.exec(file)) !== null) {
if(match[1].trim() && !/^[0-9]+$/.test(match[1]) && !match[1].includes('::') && !match[1].includes('#str_')) {
console.log('Text tip: ', match[1]);
out.push(['text_tip', match[1].replace(/\n/g, '\\n'), [], []]);
}
}
while ((match = REGEX.itemName.exec(file)) !== null) {
if(match[1].trim() && !/^[0-9]+$/.test(match[1]) && !match[1].includes('::') && !match[1].includes('#str_')) {
console.log('Item name: ', match[1]);
out.push(['item_name', match[1].replace(/\n/g, '\\n'), [], []]);
}
}
while ((match = REGEX.itemAction.exec(file)) !== null) {
if(match[1].trim() && !/^[0-9]+$/.test(match[1]) && !match[1].includes('::') && !match[1].includes('#str_')) {
console.log('Item action: ', match[1]);
out.push(['item_action', match[1].replace(/\n/g, '\\n'), [], []]);
}
}
while ((match = REGEX.guiParm.exec(file)) !== null) {
if(match[2].trim() && !/^[0-9]+$/.test(match[2]) && !match[2].includes('::') && !match[2].includes('#str_')) {
console.log('GUI Param ', match[1], ': ', match[2]);
out.push(['gui_parm' + match[1], match[2].replace(/\n/g, '\\n'), [], []]);
}
}
while ((match = REGEX.text.exec(file)) !== null) {
if(match[1].trim() && !/^[0-9]+$/.test(match[1]) && !match[1].includes('::') && !match[1].includes('#str_')) {
console.log('Text: ', match[1]);
out.push(['text', match[1].replace(/\n/g, '\\n'), [], []]);
}
}
while ((match = REGEX.objectiveTitle.exec(file)) !== null) {
if(match[1].trim() && !/^[0-9]+$/.test(match[1]) && !match[1].includes('::') && !match[1].includes('#str_')) {
console.log('Objective title: ', match[1]);
out.push(['objectivetitle', match[1].replace(/\n/g, '\\n'), [], []]);
}
}
while ((match = REGEX.objectiveText.exec(file)) !== null) {
if(match[1].trim() && !/^[0-9]+$/.test(match[1]) && !match[1].includes('::') && !match[1].includes('#str_')) {
console.log('Objective text: ', match[1]);
out.push(['objectivetext', match[1].replace(/\n/g, '\\n'), [], []]);
}
}
while ((match = REGEX.invName.exec(file)) !== null) {
if(match[1].trim() && !/^[0-9]+$/.test(match[1]) && !match[1].includes('::') && !match[1].includes('#str_')) {
console.log('Inv. name: ', match[1]);
out.push(['inv_name', match[1].replace(/\n/g, '\\n'), [], []]);
}
}
while ((match = REGEX.invName.exec(file)) !== null) {
if(match[1].trim() && !/^[0-9]+$/.test(match[1]) && !match[1].includes('::') && !match[1].includes('#str_')) {
console.log('NPC name: ', match[1]);
out.push(['npc_name', match[1].replace(/\n/g, '\\n'), [], []]);
}
}
return out;
}
const filelist = [
// Put here pathes to .map files
];
function main() {
let output = [];
for(const filename of filelist) {
const parsed = parse(fs.readFileSync(filename, 'utf8'), filename);
output = output.concat(parsed);
fs.writeFileSync(filename + '.json', JSON.stringify(parsed, 0, 4), 'utf8');
}
fs.writeFileSync('parsed.json', JSON.stringify(output, 0, 4), 'utf8');
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment