Skip to content

Commit f325b3a

Browse files
committed
cfg fixes
1 parent bfd9d31 commit f325b3a

File tree

4 files changed

+97
-102
lines changed

4 files changed

+97
-102
lines changed

Autoload/Cube.gd

+12-32
Original file line numberDiff line numberDiff line change
@@ -50,42 +50,22 @@ func clear_all_cube_data():
5050
tex.clear()
5151
names.clear()
5252

53-
func read_cubes_cfg(path):
54-
var CODETIME_START = OS.get_ticks_msec()
55-
var file = File.new()
56-
if file.open(path, File.READ) != OK:
57-
return
58-
59-
# Important to clear these because they get reloaded
60-
clear_all_cube_data()
61-
62-
cubesCfgLastModifiedTime = get_cubescfg_modified_time()
53+
54+
func read_cubes_cfg(get_cfg_data):
55+
var int_cfg_data = {}
56+
for key in get_cfg_data:
57+
int_cfg_data[int(key)] = get_cfg_data[key]
6358

64-
var massiveString = file.get_as_text()
65-
file.close()
59+
var max_cube_key = int_cfg_data.keys().max()
6660

67-
# Replace any tabs with spaces, so the lines can be properly split().
68-
massiveString = massiveString.replace('\t', ' ')
61+
names.resize(max_cube_key + 1)
62+
tex.resize(max_cube_key + 1)
6963

70-
#while true:
71-
var bigListOfLines = massiveString.split('\n',false)
72-
for line in bigListOfLines:
73-
var componentsOfLine = line.split(' ', false)
74-
if componentsOfLine.size() >= 3 and componentsOfLine[0] == "Name":
75-
names.append(componentsOfLine[2].capitalize().replace(" ",""))
76-
if componentsOfLine.size() >= 8 and componentsOfLine[0] == "Textures":
77-
tex.append([
78-
int(componentsOfLine[2]),
79-
int(componentsOfLine[3]),
80-
int(componentsOfLine[4]),
81-
int(componentsOfLine[5]),
82-
int(componentsOfLine[6]),
83-
int(componentsOfLine[7]),
84-
])
64+
for cubeID in int_cfg_data:
65+
names[cubeID] = int_cfg_data[cubeID].get("Name", "")
66+
tex[cubeID] = int_cfg_data[cubeID].get("Textures", [])
8567

86-
set_max_cubes() # Run for both read_cubes_cfg() and at the bottom of load_dk_original_cubes()
87-
print('Cube names read in: ' + str(OS.get_ticks_msec() - CODETIME_START) + 'ms')
88-
68+
set_max_cubes()
8969

9070

9171
func load_dk_original_cubes():

Autoload/Utils.gd

+28-9
Original file line numberDiff line numberDiff line change
@@ -57,44 +57,63 @@ func get_filetype_in_directory(directory_path: String, file_extension: String) -
5757
print("Failed to open directory: ", directory_path)
5858
return files
5959

60-
func read_dkcfg_file(file_path) -> Dictionary: # Optimized
60+
func read_dkcfg_file(file_path) -> Dictionary:
6161
var config = {}
6262
var current_section = ""
6363

6464
var file = File.new()
6565
if not file.file_exists(file_path):
66-
print("File not found: ", file_path)
6766
return config
6867

68+
var CODETIME_START = OS.get_ticks_msec()
69+
6970
file.open(file_path, File.READ)
7071
var lines = file.get_as_text().split("\n")
7172
file.close()
7273

7374
for line in lines:
7475
line = line.strip_edges()
76+
7577
if line.begins_with(";") or line.empty():
7678
continue
7779

7880
if line.begins_with("[") and line.ends_with("]"):
7981
current_section = line.substr(1, line.length() - 2)
8082
config[current_section] = {}
8183
else:
82-
var delimiter_pos = line.find("=")
84+
var delimiter_pos = line.find("=") # Splits by equals sign with substr()
8385
if delimiter_pos != -1:
8486
var key = line.substr(0, delimiter_pos).strip_edges()
8587
var value = line.substr(delimiter_pos + 1).strip_edges()
8688

87-
if " " in value:
89+
var items = value.split(" ")
90+
if items.size() > 1:
8891
var construct_new_value_array = []
89-
for item in value.split(" "):
90-
if item.is_valid_integer():
91-
construct_new_value_array.append(int(item))
92-
else:
93-
construct_new_value_array.append(item)
92+
for item in items:
93+
item = item.strip_edges()
94+
if not item.empty(): # This handles having multiple spaces in a row
95+
if item.is_valid_integer():
96+
construct_new_value_array.append(int(item))
97+
else:
98+
construct_new_value_array.append(item)
9499
config[current_section][key] = construct_new_value_array
95100
else:
96101
if value.is_valid_integer():
97102
config[current_section][key] = int(value)
98103
else:
99104
config[current_section][key] = value
105+
106+
print('Read ' + file_path.get_file() + ' dkcfg in : ' + str(OS.get_ticks_msec() - CODETIME_START) + 'ms')
107+
100108
return config
109+
110+
func super_merge(dict1, dict2):
111+
var merged = {}
112+
for key in dict1:
113+
merged[key] = dict1[key]
114+
for key in dict2:
115+
if key in merged and typeof(merged[key]) == TYPE_DICTIONARY and typeof(dict2[key]) == TYPE_DICTIONARY:
116+
merged[key] = super_merge(merged[key], dict2[key])
117+
else:
118+
merged[key] = dict2[key]
119+
return merged

Scenes/CfgLoader.gd

+55-56
Original file line numberDiff line numberDiff line change
@@ -44,71 +44,79 @@ func start(mapPath):
4444
LOAD_CFG_CAMPAIGN: oGame.GAME_DIRECTORY.plus_file(campaign_cfg.get("common", {}).get("CONFIGS_LOCATION", "")),
4545
LOAD_CFG_CURRENT_MAP: mapPath.get_basename()
4646
}
47-
for cfg_type in [LOAD_CFG_FXDATA, LOAD_CFG_CAMPAIGN, LOAD_CFG_CURRENT_MAP]:
48-
var cfg_dir = config_dirs[cfg_type]
49-
for i in files_to_load.size():
50-
var file_name = files_to_load[i]
47+
48+
var file_exists = File.new()
49+
50+
for i in files_to_load.size():
51+
var CODETIME_START = OS.get_ticks_msec()
52+
var file_name = files_to_load[i]
53+
54+
var combined_cfg = {}
55+
56+
for cfg_type in [LOAD_CFG_FXDATA, LOAD_CFG_CAMPAIGN, LOAD_CFG_CURRENT_MAP]:
57+
var cfg_dir = config_dirs[cfg_type]
5158
var file_path = cfg_dir.plus_file(file_name)
5259
if cfg_type == LOAD_CFG_CURRENT_MAP:
5360
file_path = cfg_dir + "." + file_name
5461

55-
print("CFG loader: ", file_path)
56-
57-
if File.new().file_exists(file_path):
62+
if file_exists.file_exists(file_path):
63+
64+
# ALL OTHER FILES GET DEFAULTED TO BEING LOADED AS A DKCFG FILE
5865
match file_name:
59-
"objects.cfg":
60-
load_objects_data(file_path)
61-
"creature.cfg":
62-
load_creatures_data(file_path)
63-
"trapdoor.cfg":
64-
load_trapdoor_data(file_path)
65-
"terrain.cfg":
66-
load_terrain_data(file_path)
67-
"cubes.cfg":
68-
load_cubes_data(file_path)
69-
"slabset.toml":
70-
load_slabset_data(file_path)
71-
"columnset.toml":
72-
load_columnset_data(file_path)
73-
66+
"slabset.toml": Slabset.import_toml_slabset(file_path) # .toml import gets run multiple times instead of combining
67+
"columnset.toml": Columnset.import_toml_columnset(file_path)
68+
_:
69+
var cfgData = Utils.read_dkcfg_file(file_path)
70+
combined_cfg = Utils.super_merge(combined_cfg, cfgData)
71+
7472
paths_loaded[cfg_type].resize(files_to_load.size())
7573
paths_loaded[cfg_type][i] = file_path
7674
else:
7775
if cfg_type == LOAD_CFG_FXDATA:
7876
match file_name:
79-
"cubes.cfg":
80-
Cube.load_dk_original_cubes()
81-
"slabset.toml":
82-
Slabset.load_default_original_slabset()
83-
"columnset.toml":
84-
Columnset.load_default_original_columnset()
85-
77+
"cubes.cfg": Cube.load_dk_original_cubes()
78+
"slabset.toml": Slabset.load_default_original_slabset()
79+
"columnset.toml": Columnset.load_default_original_columnset()
80+
81+
#if file_name == "objects.cfg":
82+
# print(combined_cfg)
83+
84+
# Only load cfg after they've been combined (they're combined so they'll automatically have fallbacks)
85+
match file_name:
86+
"objects.cfg": load_objects_data(combined_cfg)
87+
"creature.cfg": load_creatures_data(combined_cfg)
88+
"trapdoor.cfg": load_trapdoor_data(combined_cfg)
89+
"terrain.cfg": load_terrain_data(combined_cfg)
90+
"cubes.cfg": Cube.read_cubes_cfg(combined_cfg)
91+
8692
print('Loaded all .cfg and .toml files: ' + str(OS.get_ticks_msec() - CODETIME_LOADCFG_START) + 'ms')
8793
if oConfigFilesListWindow.visible == true:
8894
Utils.popup_centered(oConfigFilesListWindow)
8995

9096
oCustomSlabSystem.load_unearth_custom_slabs_file()
9197

92-
func load_objects_data(path): # 10ms
93-
var objects_cfg = Utils.read_dkcfg_file(path)
94-
for section in objects_cfg:
98+
99+
func load_objects_data(cfg): # 10ms
100+
for section in cfg:
95101
if section.begins_with("object"):
96102
var id = int(section)
97103
if id == 0: continue
98104
if id >= 136 or id in [100, 101, 102, 103, 104, 105]: # Dummy Boxes should be overwritten
99-
var objSection = objects_cfg[section]
105+
var objSection = cfg[section]
100106
var newName
101107
var animID
102108
var newSprite
103109
var newEditorTab
104110
var newGenre
105111

106-
if Things.DATA_OBJECT.has(id) == true: # Existing values are written by other calls to load_objects_data() (can be from /fxdata/, campaign, local map, etc.)
112+
113+
if Things.DATA_OBJECT.has(id) == true:
107114
newName = objSection.get("Name", Things.DATA_OBJECT[id][Things.NAME_ID])
108115

109116
animID = objSection.get("AnimationID")
110117
newSprite = get_sprite(animID, newName)
111-
if newSprite == null: newSprite = Things.DATA_OBJECT[id][Things.SPRITE]
118+
if newSprite == null:
119+
newSprite = Things.DATA_OBJECT[id][Things.SPRITE]
112120

113121
newGenre = objSection.get("Genre")
114122
newEditorTab = Things.GENRE_TO_TAB.get(newGenre, Things.DATA_OBJECT[id][Things.EDITOR_TAB])
@@ -121,18 +129,21 @@ func load_objects_data(path): # 10ms
121129
newGenre = objSection.get("Genre")
122130
newEditorTab = Things.GENRE_TO_TAB.get(newGenre, Things.TAB_DECORATION)
123131

132+
if id == 161:
133+
print('-------------------------')
134+
print(newGenre)
135+
print('-------------------------')
124136

125137
Things.DATA_OBJECT[id] = [newName, newSprite, newEditorTab]
126138

127139

128140
var keeperfx_edited_slabs = [Slabs.GEMS] # This is to help with backwards compatibility for previous keeperfx versions that don't have these edits.
129-
func load_terrain_data(path): # 4ms
130-
var terrain_cfg = Utils.read_dkcfg_file(path)
131-
for section in terrain_cfg:
141+
func load_terrain_data(cfg): # 4ms
142+
for section in cfg:
132143
if section.begins_with("slab"):
133144
var id = int(section)
134145
if id >= 55 or id in keeperfx_edited_slabs:
135-
var slabSection = terrain_cfg[section]
146+
var slabSection = cfg[section]
136147

137148
var setName = slabSection.get("Name", "UNKNOWN")
138149

@@ -188,19 +199,17 @@ func load_terrain_data(path): # 4ms
188199
]
189200

190201

191-
func load_creatures_data(path): # 3ms
192-
var creature_cfg = Utils.read_dkcfg_file(path)
193-
var creatures = creature_cfg.get("common", {}).get("Creatures", [])
202+
func load_creatures_data(cfg): # 3ms
203+
var creatures = cfg.get("common", {}).get("Creatures", [])
194204
for id_number in creatures.size():
195205
var creature_id = id_number + 1
196206
if not Things.DATA_CREATURE.has(creature_id):
197207
var newName = creatures[id_number]
198208
var newSprite = get_sprite(newName, null)
199209
Things.DATA_CREATURE[creature_id] = [newName, newSprite, Things.TAB_CREATURE]
200210

201-
func load_trapdoor_data(path): # 1ms
202-
var trapdoor_cfg = Utils.read_dkcfg_file(path)
203-
for section in trapdoor_cfg:
211+
func load_trapdoor_data(cfg): # 1ms
212+
for section in cfg:
204213
var id = int(section)
205214
if id == 0: continue
206215
var trapOrDoor = -1
@@ -211,7 +220,7 @@ func load_trapdoor_data(path): # 1ms
211220
else:
212221
continue
213222

214-
var data = trapdoor_cfg[section]
223+
var data = cfg[section]
215224
var newName = data.get("Name", null)
216225
var newSprite = get_sprite(newName, null)
217226
var crateName = data.get("Crate", null)
@@ -247,13 +256,3 @@ func load_campaign_data(mapPath):
247256
#print(oGame.GAME_DIRECTORY.plus_file(levelsLocation).to_lower())
248257
return cfgDictionary
249258
return {}
250-
251-
252-
func load_cubes_data(file_path): # 6ms
253-
Cube.read_cubes_cfg(file_path)
254-
255-
func load_slabset_data(file_path): # 33ms
256-
Slabset.import_toml_slabset(file_path)
257-
258-
func load_columnset_data(file_path): # 29ms
259-
Columnset.import_toml_columnset(file_path)

Scenes/OpenMap.gd

+2-5
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,8 @@ func start():
7070
#for i in 200:
7171
# yield(get_tree(), "idle_frame")
7272
#oCurrentMap.clear_map()
73-
#open_map("D:/Dungeon Keeper/levels/personal/map00002.slb")
74-
open_map("D:/Dungeon Keeper/campgns/dpthshdw/map00014.slb")
75-
pass
73+
open_map("D:/Dungeon Keeper/levels/personal/map00002.slb")
74+
#open_map("D:/Dungeon Keeper/campgns/dpthshdw/map00014.slb")
7675
else:
7776
# initialize a cleared map
7877
oCurrentMap.clear_map()
@@ -101,8 +100,6 @@ func open_map(filePath):
101100
oMessage.quick("Error: Cannot open map because textures haven't been loaded")
102101
return
103102

104-
105-
106103
print("----------- Opening map ------------")
107104
TOTAL_TIME_TO_OPEN_MAP = OS.get_ticks_msec()
108105

0 commit comments

Comments
 (0)