Atlas-Config-Generator/js/atlas_reparser.js

110 lines
No EOL
2.7 KiB
JavaScript

function deparseJson(input, key) {
switch (typeof input) {
case "object":
return deparseObject(input);
case "number":
return deparseNumber(input, key);
case "string":
return deparseString(input);
case "undefined":
return "";
case "boolean":
return deparseBoolean(input);
default:
console.log("Unknown type for input", input);
return "";
}
}
function deparseObject(input) {
if (Array.isArray(input)) {
return deparseArray(input);
}
if (input === null) {
return "None";
}
var beginObject = '(';
var endObject = ')';
var nameSeparator = '=';
var valueSeparator = ',';
var output = '';
output += beginObject;
Object.keys(input).forEach(function (key, idx, array) {
output += key;
output += nameSeparator;
output += deparseJson(input[key], key);
if (idx !== array.length - 1) {
// Not the last item
output += valueSeparator
}
});
output += endObject;
return output;
}
function deparseArray(input) {
// We have one funky array option, where its 2 values and one of them is a recognised class.
// These are here for easier maintenance, as adding any new ones should just work. These all
// Work off string equality, see found below.
var specialCases = [
'BlueprintGeneratedClass',
'Blueprint',
'SoundWave',
'Texture2D'
];
var found = specialCases.indexOf(input[0]);
if (found > -1) {
// We found one of our special cases!
var val = specialCases[found];
return val + "'" + input[1] + "'";
}
var beginObject = '(';
var endObject = ')';
var valueSeparator = ',';
var output = '';
output += beginObject;
input.forEach(function (data, idx, array) {
output += deparseJson(data);
if (idx !== array.length - 1) {
output += valueSeparator;
}
});
output += endObject;
return output;
}
function deparseNumber(input, key) {
// Any number which has a decimal is stored to 6 decimal places
// Except for IDs. Unless they actually have such a number... erm...
if (key.substr(key.length - 2, 2) === "ID") {
if (input % 1 === 0) {
return Number(input).toString();
}
// Else drop out and return as normal
}
return Number(input).toFixed(6);
}
function deparseString(input) {
// We need to re-encode the strings properly
return JSON.stringify(input);
}
function deparseBoolean(input) {
if ( input ) {
return 'True';
} else {
return 'False';
}
}