|
| 1 | +extends Node |
| 2 | +onready var oGame = Nodelist.list["oGame"] |
| 3 | + |
| 4 | +# These are dictionaries containing dictionaries. |
| 5 | +# objects_cfg["section_name"]["key"] will return the "value" |
| 6 | +# If there's a space in the value string, then the value will be an array of strings or integers. |
| 7 | + |
| 8 | +var terrain_cfg:Dictionary |
| 9 | +var objects_cfg:Dictionary |
| 10 | +var creature_cfg:Dictionary |
| 11 | +var trapdoor_cfg:Dictionary |
| 12 | + |
| 13 | + |
| 14 | +func start(): |
| 15 | + var CODETIME_START = OS.get_ticks_msec() |
| 16 | + terrain_cfg = read_dkcfg_file(oGame.DK_FXDATA_DIRECTORY.plus_file("terrain.cfg")) |
| 17 | + objects_cfg = read_dkcfg_file(oGame.DK_FXDATA_DIRECTORY.plus_file("objects.cfg")) |
| 18 | + creature_cfg = read_dkcfg_file(oGame.DK_FXDATA_DIRECTORY.plus_file("creature.cfg")) |
| 19 | + trapdoor_cfg = read_dkcfg_file(oGame.DK_FXDATA_DIRECTORY.plus_file("trapdoor.cfg")) |
| 20 | + print('Parsed all dkcfg files: ' + str(OS.get_ticks_msec() - CODETIME_START) + 'ms') |
| 21 | + |
| 22 | + |
| 23 | +func read_dkcfg_file(file_path) -> Dictionary: |
| 24 | + var config = {} |
| 25 | + var current_section = "" |
| 26 | + |
| 27 | + var file = File.new() |
| 28 | + if not file.file_exists(file_path): |
| 29 | + print("File not found: ", file_path) |
| 30 | + return config |
| 31 | + |
| 32 | + file.open(file_path, File.READ) |
| 33 | + var lines = file.get_as_text().split("\n") |
| 34 | + file.close() |
| 35 | + |
| 36 | + for line in lines: |
| 37 | + line = line.strip_edges() |
| 38 | + if line.begins_with(";") or line.empty(): |
| 39 | + continue |
| 40 | + |
| 41 | + if line.begins_with("[") and line.ends_with("]"): |
| 42 | + current_section = line.substr(1, line.length() - 2) |
| 43 | + config[current_section] = {} |
| 44 | + else: |
| 45 | + var delimiter_pos = line.find("=") |
| 46 | + if delimiter_pos != -1: |
| 47 | + var key = line.substr(0, delimiter_pos).strip_edges() |
| 48 | + var value = line.substr(delimiter_pos + 1).strip_edges() |
| 49 | + |
| 50 | + if " " in value: |
| 51 | + var construct_new_value_array = [] |
| 52 | + for item in value.split(" "): |
| 53 | + if item.is_valid_integer(): |
| 54 | + construct_new_value_array.append(int(item)) |
| 55 | + else: |
| 56 | + construct_new_value_array.append(item) |
| 57 | + config[current_section][key] = construct_new_value_array |
| 58 | + else: |
| 59 | + if value.is_valid_integer(): |
| 60 | + config[current_section][key] = int(value) |
| 61 | + else: |
| 62 | + config[current_section][key] = value |
| 63 | + |
| 64 | + return config |
0 commit comments