Skip to content

Commit 99897e6

Browse files
committed
cfg refactor wip 10
1 parent 77bbaca commit 99897e6

File tree

5 files changed

+138
-93
lines changed

5 files changed

+138
-93
lines changed

Autoload/Graphics.gd

+1-15
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ extends Node
33
func load_extra_images_from_harddrive():
44
var CODETIME_START = OS.get_ticks_msec()
55
var custom_images_dir = Settings.unearthdata.plus_file("custom-object-images")
6-
var image_paths = get_png_files_in_dir(custom_images_dir)
6+
var image_paths = Utils.get_filetype_in_directory(custom_images_dir, "png")
77
for image_path in image_paths:
88
var texture = load(image_path)
99
if texture is Texture:
@@ -13,20 +13,6 @@ func load_extra_images_from_harddrive():
1313
print("Failed to load texture: ", image_path)
1414
print('Loaded extra images from HDD: ' + str(OS.get_ticks_msec() - CODETIME_START) + 'ms')
1515

16-
func get_png_files_in_dir(path):
17-
var png_files = []
18-
var dir = Directory.new()
19-
if dir.open(path) == OK:
20-
dir.list_dir_begin()
21-
var file_name = dir.get_next()
22-
while file_name != "":
23-
if not dir.current_is_dir() and file_name.get_extension().to_lower() == "png":
24-
png_files.append(path.plus_file(file_name))
25-
file_name = dir.get_next()
26-
else:
27-
print("An error occurred when trying to access the directory: ", path)
28-
return png_files
29-
3016

3117
#func load_custom_images_into_array(DATA_ARRAY, thingtypeImageFolder):
3218
# print("Loading /thing-images/" + thingtypeImageFolder + " directory ...")

Autoload/Utils.gd

+57
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,60 @@ func string_has_letters(string):
3434
if regex.search(string) == null:
3535
return true
3636
return false
37+
38+
func get_filetype_in_directory(directory_path: String, file_extension: String) -> Array:
39+
var files = []
40+
var directory = Directory.new()
41+
if directory.open(directory_path) == OK:
42+
directory.list_dir_begin()
43+
var file_name = directory.get_next()
44+
while file_name != "":
45+
if not directory.current_is_dir() and file_name.get_extension().to_lower() == file_extension.to_lower():
46+
files.append(directory_path.plus_file(file_name))
47+
file_name = directory.get_next()
48+
directory.list_dir_end()
49+
else:
50+
print("Failed to open directory: ", directory_path)
51+
return files
52+
53+
func read_dkcfg_file(file_path) -> Dictionary: # Optimized
54+
var config = {}
55+
var current_section = ""
56+
57+
var file = File.new()
58+
if not file.file_exists(file_path):
59+
print("File not found: ", file_path)
60+
return config
61+
62+
file.open(file_path, File.READ)
63+
var lines = file.get_as_text().split("\n")
64+
file.close()
65+
66+
for line in lines:
67+
line = line.strip_edges()
68+
if line.begins_with(";") or line.empty():
69+
continue
70+
71+
if line.begins_with("[") and line.ends_with("]"):
72+
current_section = line.substr(1, line.length() - 2)
73+
config[current_section] = {}
74+
else:
75+
var delimiter_pos = line.find("=")
76+
if delimiter_pos != -1:
77+
var key = line.substr(0, delimiter_pos).strip_edges()
78+
var value = line.substr(delimiter_pos + 1).strip_edges()
79+
80+
if " " in value:
81+
var construct_new_value_array = []
82+
for item in value.split(" "):
83+
if item.is_valid_integer():
84+
construct_new_value_array.append(int(item))
85+
else:
86+
construct_new_value_array.append(item)
87+
config[current_section][key] = construct_new_value_array
88+
else:
89+
if value.is_valid_integer():
90+
config[current_section][key] = int(value)
91+
else:
92+
config[current_section][key] = value
93+
return config

Scenes/CfgLoader.gd

+77-74
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,60 @@
11
extends Node
22
onready var oGame = Nodelist.list["oGame"]
3+
onready var oMessage = Nodelist.list["oMessage"]
4+
35

46
# These are dictionaries containing dictionaries.
57
# objects_cfg["section_name"]["key"] will return the "value"
68
# If there's a space in the value string, then the value will be an array of strings or integers.
79

8-
var terrain_cfg : Dictionary
9-
var objects_cfg : Dictionary
10-
var creature_cfg : Dictionary
11-
var trapdoor_cfg : Dictionary
10+
#var campaign_cfg : Dictionary
11+
#var terrain_cfg : Dictionary
12+
#var objects_cfg : Dictionary
13+
#var creature_cfg : Dictionary
14+
#var trapdoor_cfg : Dictionary
15+
1216

13-
func start():
17+
enum {
18+
LOAD_CFG_FXDATA,
19+
LOAD_CFG_CAMPAIGN,
20+
LOAD_CFG_CURRENT_MAP,
21+
}
22+
func start(mapPath):
1423
if Cube.tex.empty() == true:
1524
Cube.read_cubes_cfg()
16-
var CODETIME_START = OS.get_ticks_msec()
17-
terrain_cfg = read_dkcfg_file(oGame.DK_FXDATA_DIRECTORY.plus_file("terrain.cfg"))
18-
objects_cfg = read_dkcfg_file(oGame.DK_FXDATA_DIRECTORY.plus_file("objects.cfg"))
19-
creature_cfg = read_dkcfg_file(oGame.DK_FXDATA_DIRECTORY.plus_file("creature.cfg"))
20-
trapdoor_cfg = read_dkcfg_file(oGame.DK_FXDATA_DIRECTORY.plus_file("trapdoor.cfg"))
21-
print('Parsed all dkcfg files: ' + str(OS.get_ticks_msec() - CODETIME_START) + 'ms')
2225

2326
var CODETIME_LOADCFG_START = OS.get_ticks_msec()
24-
# Step 1: set object data to default
27+
2528
Things.reset_thing_data_to_default()
26-
# Step 2: load data from /fxdata/ config files
27-
load_objects_data()
28-
load_creatures_data()
29-
load_trapdoor_data()
30-
# Step 3: load data from local config files (map00001.objects.cfg)
29+
var campaign_cfg = load_campaign_data(mapPath)
30+
31+
var config_dirs = {
32+
LOAD_CFG_FXDATA : oGame.DK_FXDATA_DIRECTORY,
33+
LOAD_CFG_CAMPAIGN : oGame.GAME_DIRECTORY.plus_file(campaign_cfg.get("common", {}).get("CONFIGS_LOCATION", "")),
34+
LOAD_CFG_CURRENT_MAP : "" # Add the directory for LOAD_CFG_CURRENT_MAP if needed
35+
}
36+
37+
# Load data from each directory in the specified order
38+
for i in [LOAD_CFG_FXDATA, LOAD_CFG_CAMPAIGN, LOAD_CFG_CURRENT_MAP]:
39+
var cfg_dir = config_dirs[i]
40+
if cfg_dir == "": continue
41+
42+
match i:
43+
LOAD_CFG_FXDATA: oMessage.quick("Step 1: Data loaded from /fxdata/")
44+
LOAD_CFG_CAMPAIGN: oMessage.quick("Step 2: Data loaded from campaign configs directory")
45+
LOAD_CFG_CURRENT_MAP: oMessage.quick("Step 3?")
46+
47+
load_objects_data(cfg_dir.plus_file("objects.cfg"))
48+
load_creatures_data(cfg_dir.plus_file("creature.cfg"))
49+
load_trapdoor_data(cfg_dir.plus_file("trapdoor.cfg"))
50+
load_terrain_data(cfg_dir.plus_file("terrain.cfg"))
51+
3152
print('Loaded things from cfg files: ' + str(OS.get_ticks_msec() - CODETIME_LOADCFG_START) + 'ms')
3253

33-
func load_objects_data():
54+
55+
56+
func load_objects_data(path):
57+
var objects_cfg = Utils.read_dkcfg_file(path)
3458
for section in objects_cfg:
3559
if section.begins_with("object"):
3660
var id = int(section)
@@ -45,37 +69,45 @@ func load_objects_data():
4569
Things.DATA_OBJECT[id] = [newName, newSprite, newEditorTab]
4670

4771

48-
func load_creatures_data():
49-
for id_number in creature_cfg["common"]["Creatures"].size():
50-
if Things.DATA_CREATURE.has(id_number+1) == false:
51-
var newName = creature_cfg["common"]["Creatures"][id_number]
72+
func load_creatures_data(path):
73+
var creature_cfg = Utils.read_dkcfg_file(path)
74+
var creatures = creature_cfg.get("common", {}).get("Creatures", [])
75+
for id_number in creatures.size():
76+
var creature_id = id_number + 1
77+
if not Things.DATA_CREATURE.has(creature_id):
78+
var newName = creatures[id_number]
5279
var newSprite = get_sprite(newName, null)
53-
Things.DATA_CREATURE[id_number + 1] = [newName, newSprite, Things.TAB_CREATURE]
80+
Things.DATA_CREATURE[creature_id] = [newName, newSprite, Things.TAB_CREATURE]
5481

5582

56-
func load_trapdoor_data():
83+
func load_trapdoor_data(path):
84+
var trapdoor_cfg = Utils.read_dkcfg_file(path)
5785
for section in trapdoor_cfg:
5886
var id = int(section)
5987
var trapOrDoor = -1
60-
if section.begins_with("door"): trapOrDoor = Things.TYPE.DOOR
61-
if section.begins_with("trap"): trapOrDoor = Things.TYPE.TRAP
62-
if id == 0 or trapOrDoor == -1: continue
88+
if section.begins_with("door"):
89+
trapOrDoor = Things.TYPE.DOOR
90+
elif section.begins_with("trap"):
91+
trapOrDoor = Things.TYPE.TRAP
92+
else:
93+
continue
6394

6495
var data = trapdoor_cfg[section]
6596
var newName = data.get("Name", null)
6697
var newSprite = get_sprite(newName, null)
6798
var crateName = data.get("Crate", null)
99+
68100
if trapOrDoor == Things.TYPE.DOOR:
69101
Things.DATA_DOOR[id] = [newName, newSprite, Things.TAB_MISC]
70102
elif trapOrDoor == Things.TYPE.TRAP:
71103
Things.DATA_TRAP[id] = [newName, newSprite, Things.TAB_TRAP]
72104

73105
var crate_id_number = Things.find_subtype_by_name(Things.TYPE.OBJECT, crateName)
74-
Things.LIST_OF_BOXES[crate_id_number] = [
75-
trapOrDoor,
76-
id,
77-
]
78-
#print(Things.LIST_OF_BOXES)
106+
Things.LIST_OF_BOXES[crate_id_number] = [trapOrDoor, id]
107+
108+
109+
func load_terrain_data(path):
110+
var terrain_cfg = Utils.read_dkcfg_file(path)
79111

80112

81113
func get_sprite(first_priority, second_priority):
@@ -84,45 +116,16 @@ func get_sprite(first_priority, second_priority):
84116
return null
85117

86118

87-
func read_dkcfg_file(file_path) -> Dictionary: # Optimized
88-
var config = {}
89-
var current_section = ""
90-
91-
var file = File.new()
92-
if not file.file_exists(file_path):
93-
print("File not found: ", file_path)
94-
return config
95-
96-
file.open(file_path, File.READ)
97-
var lines = file.get_as_text().split("\n")
98-
file.close()
99-
100-
for line in lines:
101-
line = line.strip_edges()
102-
if line.begins_with(";") or line.empty():
103-
continue
104-
105-
if line.begins_with("[") and line.ends_with("]"):
106-
current_section = line.substr(1, line.length() - 2)
107-
config[current_section] = {}
108-
else:
109-
var delimiter_pos = line.find("=")
110-
if delimiter_pos != -1:
111-
var key = line.substr(0, delimiter_pos).strip_edges()
112-
var value = line.substr(delimiter_pos + 1).strip_edges()
113-
114-
if " " in value:
115-
var construct_new_value_array = []
116-
for item in value.split(" "):
117-
if item.is_valid_integer():
118-
construct_new_value_array.append(int(item))
119-
else:
120-
construct_new_value_array.append(item)
121-
config[current_section][key] = construct_new_value_array
122-
else:
123-
if value.is_valid_integer():
124-
config[current_section][key] = int(value)
125-
else:
126-
config[current_section][key] = value
127-
128-
return config
119+
func load_campaign_data(mapPath):
120+
var levelsDirPath = mapPath.get_base_dir().get_base_dir()
121+
var parentDirFolderName = levelsDirPath.get_file()
122+
if parentDirFolderName != "levels" and parentDirFolderName != "campgns":
123+
return {}
124+
var list_of_main_campaign_files = Utils.get_filetype_in_directory(levelsDirPath, "cfg")
125+
for campaignPath in list_of_main_campaign_files:
126+
var cfgDictionary = Utils.read_dkcfg_file(campaignPath)
127+
var levelsLocation = cfgDictionary.get("common", {}).get("LEVELS_LOCATION", null)
128+
if levelsLocation and oGame.GAME_DIRECTORY.plus_file(levelsLocation).to_lower() == mapPath.get_base_dir().to_lower():
129+
#print(oGame.GAME_DIRECTORY.plus_file(levelsLocation).to_lower())
130+
return cfgDictionary
131+
return {}

Scenes/Menu.gd

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func add_edit_menu_items():
7878
edit_popup.add_item("Undo", 0)
7979
edit_popup.add_separator()
8080
edit_popup.add_item("Map columns", 1)
81-
edit_popup.add_item("Custom objects", 2)
81+
#edit_popup.add_item("Custom objects", 2)
8282
edit_popup.add_item("Resize map", 3)
8383
edit_popup.add_item("Update all slabs", 4)
8484
edit_popup.add_separator()

Scenes/OpenMap.gd

+2-3
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,15 @@ func open_map(filePath):
111111

112112
var map = filePath.get_basename()
113113

114-
oCfgLoader.start()
115-
116114
# Open all map file types
117115
oCurrentMap.currentFilePaths = get_accompanying_files(map)
118-
119116
compressedFiles.clear()
120117
for i in oCurrentMap.currentFilePaths.values():
121118
if oRNC.check_for_rnc_compression(i[oCurrentMap.PATHSTRING]) == true:
122119
compressedFiles.append(i[oCurrentMap.PATHSTRING])
123120

121+
oCfgLoader.start(filePath)
122+
124123
if compressedFiles.empty() == true:
125124
# Load files
126125

0 commit comments

Comments
 (0)