Skip to content

Commit

Permalink
cfg fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rainlizard committed May 28, 2024
1 parent bfd9d31 commit f325b3a
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 102 deletions.
44 changes: 12 additions & 32 deletions Autoload/Cube.gd
Original file line number Diff line number Diff line change
Expand Up @@ -50,42 +50,22 @@ func clear_all_cube_data():
tex.clear()
names.clear()

func read_cubes_cfg(path):
var CODETIME_START = OS.get_ticks_msec()
var file = File.new()
if file.open(path, File.READ) != OK:
return

# Important to clear these because they get reloaded
clear_all_cube_data()

cubesCfgLastModifiedTime = get_cubescfg_modified_time()

func read_cubes_cfg(get_cfg_data):
var int_cfg_data = {}
for key in get_cfg_data:
int_cfg_data[int(key)] = get_cfg_data[key]

var massiveString = file.get_as_text()
file.close()
var max_cube_key = int_cfg_data.keys().max()

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

#while true:
var bigListOfLines = massiveString.split('\n',false)
for line in bigListOfLines:
var componentsOfLine = line.split(' ', false)
if componentsOfLine.size() >= 3 and componentsOfLine[0] == "Name":
names.append(componentsOfLine[2].capitalize().replace(" ",""))
if componentsOfLine.size() >= 8 and componentsOfLine[0] == "Textures":
tex.append([
int(componentsOfLine[2]),
int(componentsOfLine[3]),
int(componentsOfLine[4]),
int(componentsOfLine[5]),
int(componentsOfLine[6]),
int(componentsOfLine[7]),
])
for cubeID in int_cfg_data:
names[cubeID] = int_cfg_data[cubeID].get("Name", "")
tex[cubeID] = int_cfg_data[cubeID].get("Textures", [])

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

set_max_cubes()


func load_dk_original_cubes():
Expand Down
37 changes: 28 additions & 9 deletions Autoload/Utils.gd
Original file line number Diff line number Diff line change
Expand Up @@ -57,44 +57,63 @@ func get_filetype_in_directory(directory_path: String, file_extension: String) -
print("Failed to open directory: ", directory_path)
return files

func read_dkcfg_file(file_path) -> Dictionary: # Optimized
func read_dkcfg_file(file_path) -> Dictionary:
var config = {}
var current_section = ""

var file = File.new()
if not file.file_exists(file_path):
print("File not found: ", file_path)
return config

var CODETIME_START = OS.get_ticks_msec()

file.open(file_path, File.READ)
var lines = file.get_as_text().split("\n")
file.close()

for line in lines:
line = line.strip_edges()

if line.begins_with(";") or line.empty():
continue

if line.begins_with("[") and line.ends_with("]"):
current_section = line.substr(1, line.length() - 2)
config[current_section] = {}
else:
var delimiter_pos = line.find("=")
var delimiter_pos = line.find("=") # Splits by equals sign with substr()
if delimiter_pos != -1:
var key = line.substr(0, delimiter_pos).strip_edges()
var value = line.substr(delimiter_pos + 1).strip_edges()

if " " in value:
var items = value.split(" ")
if items.size() > 1:
var construct_new_value_array = []
for item in value.split(" "):
if item.is_valid_integer():
construct_new_value_array.append(int(item))
else:
construct_new_value_array.append(item)
for item in items:
item = item.strip_edges()
if not item.empty(): # This handles having multiple spaces in a row
if item.is_valid_integer():
construct_new_value_array.append(int(item))
else:
construct_new_value_array.append(item)
config[current_section][key] = construct_new_value_array
else:
if value.is_valid_integer():
config[current_section][key] = int(value)
else:
config[current_section][key] = value

print('Read ' + file_path.get_file() + ' dkcfg in : ' + str(OS.get_ticks_msec() - CODETIME_START) + 'ms')

return config

func super_merge(dict1, dict2):
var merged = {}
for key in dict1:
merged[key] = dict1[key]
for key in dict2:
if key in merged and typeof(merged[key]) == TYPE_DICTIONARY and typeof(dict2[key]) == TYPE_DICTIONARY:
merged[key] = super_merge(merged[key], dict2[key])
else:
merged[key] = dict2[key]
return merged
111 changes: 55 additions & 56 deletions Scenes/CfgLoader.gd
Original file line number Diff line number Diff line change
Expand Up @@ -44,71 +44,79 @@ func start(mapPath):
LOAD_CFG_CAMPAIGN: oGame.GAME_DIRECTORY.plus_file(campaign_cfg.get("common", {}).get("CONFIGS_LOCATION", "")),
LOAD_CFG_CURRENT_MAP: mapPath.get_basename()
}
for cfg_type in [LOAD_CFG_FXDATA, LOAD_CFG_CAMPAIGN, LOAD_CFG_CURRENT_MAP]:
var cfg_dir = config_dirs[cfg_type]
for i in files_to_load.size():
var file_name = files_to_load[i]

var file_exists = File.new()

for i in files_to_load.size():
var CODETIME_START = OS.get_ticks_msec()
var file_name = files_to_load[i]

var combined_cfg = {}

for cfg_type in [LOAD_CFG_FXDATA, LOAD_CFG_CAMPAIGN, LOAD_CFG_CURRENT_MAP]:
var cfg_dir = config_dirs[cfg_type]
var file_path = cfg_dir.plus_file(file_name)
if cfg_type == LOAD_CFG_CURRENT_MAP:
file_path = cfg_dir + "." + file_name

print("CFG loader: ", file_path)

if File.new().file_exists(file_path):
if file_exists.file_exists(file_path):
# ALL OTHER FILES GET DEFAULTED TO BEING LOADED AS A DKCFG FILE
match file_name:
"objects.cfg":
load_objects_data(file_path)
"creature.cfg":
load_creatures_data(file_path)
"trapdoor.cfg":
load_trapdoor_data(file_path)
"terrain.cfg":
load_terrain_data(file_path)
"cubes.cfg":
load_cubes_data(file_path)
"slabset.toml":
load_slabset_data(file_path)
"columnset.toml":
load_columnset_data(file_path)

"slabset.toml": Slabset.import_toml_slabset(file_path) # .toml import gets run multiple times instead of combining
"columnset.toml": Columnset.import_toml_columnset(file_path)
_:
var cfgData = Utils.read_dkcfg_file(file_path)
combined_cfg = Utils.super_merge(combined_cfg, cfgData)

paths_loaded[cfg_type].resize(files_to_load.size())
paths_loaded[cfg_type][i] = file_path
else:
if cfg_type == LOAD_CFG_FXDATA:
match file_name:
"cubes.cfg":
Cube.load_dk_original_cubes()
"slabset.toml":
Slabset.load_default_original_slabset()
"columnset.toml":
Columnset.load_default_original_columnset()

"cubes.cfg": Cube.load_dk_original_cubes()
"slabset.toml": Slabset.load_default_original_slabset()
"columnset.toml": Columnset.load_default_original_columnset()

#if file_name == "objects.cfg":
# print(combined_cfg)

# Only load cfg after they've been combined (they're combined so they'll automatically have fallbacks)
match file_name:
"objects.cfg": load_objects_data(combined_cfg)
"creature.cfg": load_creatures_data(combined_cfg)
"trapdoor.cfg": load_trapdoor_data(combined_cfg)
"terrain.cfg": load_terrain_data(combined_cfg)
"cubes.cfg": Cube.read_cubes_cfg(combined_cfg)

print('Loaded all .cfg and .toml files: ' + str(OS.get_ticks_msec() - CODETIME_LOADCFG_START) + 'ms')
if oConfigFilesListWindow.visible == true:
Utils.popup_centered(oConfigFilesListWindow)

oCustomSlabSystem.load_unearth_custom_slabs_file()

func load_objects_data(path): # 10ms
var objects_cfg = Utils.read_dkcfg_file(path)
for section in objects_cfg:

func load_objects_data(cfg): # 10ms
for section in cfg:
if section.begins_with("object"):
var id = int(section)
if id == 0: continue
if id >= 136 or id in [100, 101, 102, 103, 104, 105]: # Dummy Boxes should be overwritten
var objSection = objects_cfg[section]
var objSection = cfg[section]
var newName
var animID
var newSprite
var newEditorTab
var newGenre

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.)

if Things.DATA_OBJECT.has(id) == true:
newName = objSection.get("Name", Things.DATA_OBJECT[id][Things.NAME_ID])

animID = objSection.get("AnimationID")
newSprite = get_sprite(animID, newName)
if newSprite == null: newSprite = Things.DATA_OBJECT[id][Things.SPRITE]
if newSprite == null:
newSprite = Things.DATA_OBJECT[id][Things.SPRITE]

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

if id == 161:
print('-------------------------')
print(newGenre)
print('-------------------------')

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


var keeperfx_edited_slabs = [Slabs.GEMS] # This is to help with backwards compatibility for previous keeperfx versions that don't have these edits.
func load_terrain_data(path): # 4ms
var terrain_cfg = Utils.read_dkcfg_file(path)
for section in terrain_cfg:
func load_terrain_data(cfg): # 4ms
for section in cfg:
if section.begins_with("slab"):
var id = int(section)
if id >= 55 or id in keeperfx_edited_slabs:
var slabSection = terrain_cfg[section]
var slabSection = cfg[section]

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

Expand Down Expand Up @@ -188,19 +199,17 @@ func load_terrain_data(path): # 4ms
]


func load_creatures_data(path): # 3ms
var creature_cfg = Utils.read_dkcfg_file(path)
var creatures = creature_cfg.get("common", {}).get("Creatures", [])
func load_creatures_data(cfg): # 3ms
var creatures = cfg.get("common", {}).get("Creatures", [])
for id_number in creatures.size():
var creature_id = id_number + 1
if not Things.DATA_CREATURE.has(creature_id):
var newName = creatures[id_number]
var newSprite = get_sprite(newName, null)
Things.DATA_CREATURE[creature_id] = [newName, newSprite, Things.TAB_CREATURE]

func load_trapdoor_data(path): # 1ms
var trapdoor_cfg = Utils.read_dkcfg_file(path)
for section in trapdoor_cfg:
func load_trapdoor_data(cfg): # 1ms
for section in cfg:
var id = int(section)
if id == 0: continue
var trapOrDoor = -1
Expand All @@ -211,7 +220,7 @@ func load_trapdoor_data(path): # 1ms
else:
continue

var data = trapdoor_cfg[section]
var data = cfg[section]
var newName = data.get("Name", null)
var newSprite = get_sprite(newName, null)
var crateName = data.get("Crate", null)
Expand Down Expand Up @@ -247,13 +256,3 @@ func load_campaign_data(mapPath):
#print(oGame.GAME_DIRECTORY.plus_file(levelsLocation).to_lower())
return cfgDictionary
return {}


func load_cubes_data(file_path): # 6ms
Cube.read_cubes_cfg(file_path)

func load_slabset_data(file_path): # 33ms
Slabset.import_toml_slabset(file_path)

func load_columnset_data(file_path): # 29ms
Columnset.import_toml_columnset(file_path)
7 changes: 2 additions & 5 deletions Scenes/OpenMap.gd
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,8 @@ func start():
#for i in 200:
# yield(get_tree(), "idle_frame")
#oCurrentMap.clear_map()
#open_map("D:/Dungeon Keeper/levels/personal/map00002.slb")
open_map("D:/Dungeon Keeper/campgns/dpthshdw/map00014.slb")
pass
open_map("D:/Dungeon Keeper/levels/personal/map00002.slb")
#open_map("D:/Dungeon Keeper/campgns/dpthshdw/map00014.slb")
else:
# initialize a cleared map
oCurrentMap.clear_map()
Expand Down Expand Up @@ -101,8 +100,6 @@ func open_map(filePath):
oMessage.quick("Error: Cannot open map because textures haven't been loaded")
return



print("----------- Opening map ------------")
TOTAL_TIME_TO_OPEN_MAP = OS.get_ticks_msec()

Expand Down

0 comments on commit f325b3a

Please sign in to comment.