mirror of
https://github.com/git-learning-game/oh-my-git.git
synced 2024-12-24 20:33:19 +01:00
Add a simple level file parser
This commit is contained in:
parent
c178a5c63e
commit
08db98f41c
1 changed files with 40 additions and 0 deletions
40
helpers.gd
40
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
|
||||
|
|
Loading…
Reference in a new issue