128 lines
No EOL
2.9 KiB
JavaScript
128 lines
No EOL
2.9 KiB
JavaScript
var atlasEditor = ace.edit("atlas-config-editor");
|
|
atlasEditor.setTheme("ace/theme/monokai");
|
|
atlasEditor.session.setMode("ace/mode/text");
|
|
atlasEditor.session.setUseWrapMode(true);
|
|
|
|
var jsonEditor = ace.edit("json-config-editor");
|
|
jsonEditor.setTheme("ace/theme/monokai");
|
|
jsonEditor.session.setMode("ace/mode/json");
|
|
|
|
var parser;
|
|
|
|
$.get('js/atlas.pegjs', function (data) {
|
|
parser = PEG.buildParser(data);
|
|
});
|
|
|
|
ko.applyBindings({});
|
|
|
|
function fromAtlasToJson() {
|
|
var input = atlasEditor.getValue();
|
|
var output = parser.parse(input);
|
|
jsonEditor.setValue(ko.toJSON(output, null, 2));
|
|
}
|
|
|
|
function fromJsonToAtlas() {
|
|
var input = JSON.parse(jsonEditor.getValue());
|
|
|
|
console.log(input);
|
|
|
|
var output = deparseJson(input);
|
|
|
|
console.log(output);
|
|
atlasEditor.setValue(output);
|
|
}
|
|
|
|
function deparseJson(input) {
|
|
switch (typeof input) {
|
|
case "object":
|
|
return deparseObject(input);
|
|
case "number":
|
|
return deparseNumber(input);
|
|
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]);
|
|
|
|
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.
|
|
if (input[0] === "BlueprintGeneratedClass") {
|
|
return "BlueprintGeneratedClass" + "'" + input[1] + "'";
|
|
}
|
|
if (input[0] === "SoundWave") {
|
|
return "SoundWave" + "'" + 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) {
|
|
// All numbers seem to be a string to 6 decimal places
|
|
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';
|
|
}
|
|
} |