Accepts decimal or 0x-hex GID input. gid=0 is rendered as the literal 'empty'; non-zero values print the three components in a parse-stable space-separated key=value form.
40 lines
822 B
JavaScript
40 lines
822 B
JavaScript
'use strict';
|
|
|
|
const { decodeGid } = require('../gid');
|
|
|
|
const USAGE = 'usage: sporel-mapper decode <gid>\n';
|
|
|
|
function parseGid(s) {
|
|
let n;
|
|
if (/^0x[0-9a-fA-F]+$/.test(s)) {
|
|
n = parseInt(s, 16);
|
|
} else if (/^\d+$/.test(s)) {
|
|
n = parseInt(s, 10);
|
|
} else {
|
|
throw new RangeError(`gid '${s}' is not a decimal or 0x-hex integer`);
|
|
}
|
|
return n;
|
|
}
|
|
|
|
function run(args, { out, err }) {
|
|
if (args.length !== 1) {
|
|
err.write(USAGE);
|
|
return 1;
|
|
}
|
|
try {
|
|
const gid = parseGid(args[0]);
|
|
if (gid === 0) {
|
|
out.write('empty\n');
|
|
return 0;
|
|
}
|
|
const { atlas, tile, rotation } = decodeGid(gid);
|
|
out.write(`atlas=${atlas} tile=${tile} rotation=${rotation}\n`);
|
|
return 0;
|
|
} catch (e) {
|
|
err.write(`error: ${e.message}\n`);
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
module.exports = { run };
|