Skip to content

Instantly share code, notes, and snippets.

@MarvNC
Created September 1, 2022 07:25
Show Gist options
  • Save MarvNC/c9591ca3ba108eeb40dc30ea24eeed5e to your computer and use it in GitHub Desktop.
Save MarvNC/c9591ca3ba108eeb40dc30ea24eeed5e to your computer and use it in GitHub Desktop.
madosoft raspberry cube/hamidashi ast script parse
const fs = require('fs');
const args = process.argv.slice(2);
if (args.length < 2) {
console.log('Usage: node astParse.js <input folder> <output folder>');
process.exit(1);
} else {
const inputFolder = args[0];
const outputFolder = args[1];
const scriptFiles = fs.readdirSync(inputFolder);
const partRegex = /([^\d-]+)(-?(\d|\w|_|.)+).ast/;
const routes = {};
for (const fileName of scriptFiles) {
const match = fileName.match(partRegex);
let route, part;
if (match) {
route = match[1];
part = match[2];
} else {
throw new Error(`Invalid file name: ${fileName}`);
}
if (!routes[route]) routes[route] = [];
routes[route].push(part);
}
console.log(routes);
const outputScripts = {};
for (const route of Object.keys(routes)) {
const parts = routes[route];
outputScripts[route] = '';
for (const part of parts) {
const file = `${inputFolder}\\${route}${part}.ast`;
console.log(`Reading ${file}`);
const text = fs.readFileSync(file, 'utf8');
// detect newline type
const newlineChar = text.indexOf('\r\n') > -1 ? '\r\n' : '\n';
const lines = text.split(newlineChar);
while (lines.length > 0) {
const line = lines.shift();
if (line.startsWith('ja={')) {
let newLine = lines.shift();
// go until end of object thing
while (!newLine.startsWith('}')) {
// only read strings
if (newLine.startsWith('"')) {
const text = newLine.split('"')[1];
outputScripts[route] += text;
}
newLine = lines.shift();
}
// there can be multiple strings part of the same line, so linebreak separately after done reading
outputScripts[route] += '\n';
}
}
}
}
for (const route of Object.keys(outputScripts)) {
if (!fs.existsSync(outputFolder)) {
fs.mkdirSync(outputFolder);
}
const file = `${outputFolder}\\${route}.txt`;
console.log(`Writing ${file}`);
fs.writeFileSync(file, outputScripts[route], 'utf8');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment