From 08db98f41cae733e910275ea2fe9997495401125 Mon Sep 17 00:00:00 2001 From: Sebastian Morr Date: Wed, 30 Sep 2020 17:46:43 +0200 Subject: [PATCH] Add a simple level file parser --- helpers.gd | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/helpers.gd b/helpers.gd index 3b9b341..4467fec 100644 --- a/helpers.gd +++ b/helpers.gd @@ -84,3 +84,43 @@ func careful_delete(path_inside): else: game.global_shell.cd(game.tmp_prefix_inside) game.global_shell.run("rm -rf '%s'" % path_inside) + +func parse(file): + var text = read_file(file) + var result = {} + var current_section + + var section_regex = RegEx.new() + section_regex.compile("^\\[(.*)\\]$") + + var assignment_regex = RegEx.new() + assignment_regex.compile("^([a-z ]+)=(.*)$") + + for line in text.split("\n"): + # Skip comments. + if line.substr(0, 1) == ";": + continue + + # Parse a [section name]. + var m = section_regex.search(line) + if m: + current_section = m.get_string(1) + result[current_section] = "" + continue + + # Parse a direct=assignment. + m = assignment_regex.search(line) + if m: + var key = m.get_string(1).strip_edges() + var value = m.get_string(2).strip_edges() + result[key] = value + continue + + # At this point, the line is just content belonging to the current section. + if current_section: + result[current_section] += line + "\n" + + for key in result: + result[key] = result[key].strip_edges() + + return result