From d9a36437b6bfe1b25ab3416dd89a1ed6cb953a79 Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Mon, 26 Aug 2019 13:22:06 +0100 Subject: [PATCH] First pass parser testing --- .idea/dictionaries/Tom.xml | 7 ++ js/main.js | 2 +- js/parser_test.js | 128 +++++++++++++++++++++++++++++++++++++ parser_test.html | 68 ++++++++++++++++++++ 4 files changed, 204 insertions(+), 1 deletion(-) create mode 100644 .idea/dictionaries/Tom.xml create mode 100644 js/parser_test.js create mode 100644 parser_test.html diff --git a/.idea/dictionaries/Tom.xml b/.idea/dictionaries/Tom.xml new file mode 100644 index 0000000..05ad3b8 --- /dev/null +++ b/.idea/dictionaries/Tom.xml @@ -0,0 +1,7 @@ + + + + deparse + + + \ No newline at end of file diff --git a/js/main.js b/js/main.js index 1809079..5610c65 100644 --- a/js/main.js +++ b/js/main.js @@ -19,7 +19,7 @@ ko.bindingHandlers.numericValue = { var editor = ace.edit("config-editor"); editor.setTheme("ace/theme/monokai"); -editor.session.setMode("ace/mode/javascript"); +editor.session.setMode("ace/mode/json"); var parser; diff --git a/js/parser_test.js b/js/parser_test.js new file mode 100644 index 0000000..3633a65 --- /dev/null +++ b/js/parser_test.js @@ -0,0 +1,128 @@ +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'; + } +} \ No newline at end of file diff --git a/parser_test.html b/parser_test.html new file mode 100644 index 0000000..35a1a32 --- /dev/null +++ b/parser_test.html @@ -0,0 +1,68 @@ + + + + + Parser Testing + + + + + + + +
+
+
+ Atlas Crazy Config +
(SoundOverrides=(NavalCombatMusicDay=SoundWave'/Game/Atlas/Music/ATL_SeaCombatArcticDay_v1.ATL_SeaCombatArcticDay_v1',NavalCombatMusicNight=SoundWave'/Game/Atlas/Music/ATL_SeaCombatArcticNight_v1.ATL_SeaCombatArcticNight_v1',CombatMusicDay=SoundWave'/Game/Atlas/Music/ATL_LandCombatArcticDay_v2.ATL_LandCombatArcticDay_v2',CombatMusicNight=SoundWave'/Game/Atlas/Music/ATL_LandCombatArcticNight_v1.ATL_LandCombatArcticNight_v1',CombatMusicDay_Heavy=SoundWave'/Game/Atlas/Music/ATL_LandCombatArcticDay_v2.ATL_LandCombatArcticDay_v2',CombatMusicNight_Heavy=SoundWave'/Game/Atlas/Music/ATL_LandCombatArcticNight_v1.ATL_LandCombatArcticNight_v1',Sound_TransitionToMorning=SoundWave'/Game/Atlas/Stingers/ATL_Sting_ArcticMorning.ATL_Sting_ArcticMorning',Sound_TransitionToMidDay=SoundWave'/Game/Atlas/Stingers/ATL_Sting_ArcticNoon.ATL_Sting_ArcticNoon',Sound_TransitionToNight=SoundWave'/Game/Atlas/Stingers/ATL_Sting_ArcticNight.ATL_Sting_ArcticNight',Sound_SetSail=SoundWave'/Game/Atlas/Stingers/ATL_Sting_ArcticSetSail.ATL_Sting_ArcticSetSail',Sound_Respawn=None,Sound_CreateNewCharacter=None),OceanHarvestEntriesOverrides=((RenderComponent=BlueprintGeneratedClass'/Game/Atlas/AtlasCoreBP/Render_Components/OceanHarvestable_Seaweed_Tall_35.OceanHarvestable_Seaweed_Tall_35_C',HarvestComponent=BlueprintGeneratedClass'/Game/Atlas/AtlasCoreBP/HarvestComponents/00_OceanFloor/HerbHarvestComponent_RedAlgae.HerbHarvestComponent_RedAlgae_C',Weight=0.100000,RandomOffsetPercentageOfPlacementInterval=3.000000),(RenderComponent=BlueprintGeneratedClass'/Game/Atlas/AtlasCoreBP/Render_Components/OceanHarvestable_Seaweed_Tall_36.OceanHarvestable_Seaweed_Tall_36_C',HarvestComponent=BlueprintGeneratedClass'/Game/Atlas/AtlasCoreBP/HarvestComponents/00_OceanFloor/HerbHarvestComponent_RedAlgae.HerbHarvestComponent_RedAlgae_C',Weight=0.010000,RandomOffsetPercentageOfPlacementInterval=5.400000),(RenderComponent=BlueprintGeneratedClass'/Game/Atlas/AtlasCoreBP/Render_Components/OceanHarvestable_Seaweed_Tall_37.OceanHarvestable_Seaweed_Tall_37_C',HarvestComponent=BlueprintGeneratedClass'/Game/Atlas/AtlasCoreBP/HarvestComponents/00_OceanFloor/FiberHarvestComponent_Seaweed.FiberHarvestComponent_Seaweed_C',Weight=0.200000,RandomOffsetPercentageOfPlacementInterval=2.100000),(RenderComponent=BlueprintGeneratedClass'/Game/Atlas/AtlasCoreBP/Render_Components/OceanHarvestable_Seaweed_Short_16.OceanHarvestable_Seaweed_Short_16_C',HarvestComponent=BlueprintGeneratedClass'/Game/Atlas/AtlasCoreBP/HarvestComponents/00_OceanFloor/FiberHarvestComponent_Seaweed.FiberHarvestComponent_Seaweed_C',Weight=0.010000,RandomOffsetPercentageOfPlacementInterval=1.400000),(RenderComponent=BlueprintGeneratedClass'/Game/Atlas/AtlasCoreBP/Render_Components/OceanHarvestable_Seaweed_Short_40.OceanHarvestable_Seaweed_Short_40_C',HarvestComponent=BlueprintGeneratedClass'/Game/Atlas/AtlasCoreBP/HarvestComponents/00_OceanFloor/FiberHarvestComponent_Seaweed.FiberHarvestComponent_Seaweed_C',Weight=0.050000,RandomOffsetPercentageOfPlacementInterval=4.000000),(RenderComponent=BlueprintGeneratedClass'/Game/Atlas/AtlasCoreBP/Render_Components/OceanHarvestable_Seaweed_Short_41.OceanHarvestable_Seaweed_Short_41_C',HarvestComponent=BlueprintGeneratedClass'/Game/Atlas/AtlasCoreBP/HarvestComponents/00_OceanFloor/FiberHarvestComponent_Seaweed.FiberHarvestComponent_Seaweed_C',Weight=0.400000,RandomOffsetPercentageOfPlacementInterval=0.600000),(RenderComponent=BlueprintGeneratedClass'/Game/Atlas/AtlasCoreBP/Render_Components/OceanHarvestable_Seaweed_Short_42.OceanHarvestable_Seaweed_Short_42_C',HarvestComponent=BlueprintGeneratedClass'/Game/Atlas/AtlasCoreBP/HarvestComponents/00_OceanFloor/FiberHarvestComponent_Seaweed.FiberHarvestComponent_Seaweed_C',Weight=0.800000,RandomOffsetPercentageOfPlacementInterval=0.800000),(RenderComponent=BlueprintGeneratedClass'/Game/Atlas/AtlasCoreBP/Render_Components/OceanHarvestable_Seaweed_Short_43.OceanHarvestable_Seaweed_Short_43_C',HarvestComponent=BlueprintGeneratedClass'/Game/Atlas/AtlasCoreBP/HarvestComponents/00_OceanFloor/FiberHarvestComponent_Seaweed.FiberHarvestComponent_Seaweed_C',Weight=0.400000,RandomOffsetPercentageOfPlacementInterval=0.500000),(RenderComponent=BlueprintGeneratedClass'/Game/Atlas/AtlasCoreBP/Render_Components/OceanHarvestable_Coral_Brain.OceanHarvestable_Coral_Brain_C',HarvestComponent=BlueprintGeneratedClass'/Game/Atlas/AtlasCoreBP/HarvestComponents/00_OceanFloor/CoralHarvestComponent_Brain.CoralHarvestComponent_Brain_C',Weight=0.500000,RandomOffsetPercentageOfPlacementInterval=10.000000)),bDisableClaimFlags=False)
+
+
+
+
+ + +
+
+
+
+ JSON Interpretation +
+
+
+
+ + + + + + + + + + + + \ No newline at end of file