Skip to content

Commit

Permalink
cfg refactor wip 10
Browse files Browse the repository at this point in the history
  • Loading branch information
rainlizard committed May 14, 2024
1 parent 77bbaca commit 99897e6
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 93 deletions.
16 changes: 1 addition & 15 deletions Autoload/Graphics.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extends Node
func load_extra_images_from_harddrive():
var CODETIME_START = OS.get_ticks_msec()
var custom_images_dir = Settings.unearthdata.plus_file("custom-object-images")
var image_paths = get_png_files_in_dir(custom_images_dir)
var image_paths = Utils.get_filetype_in_directory(custom_images_dir, "png")
for image_path in image_paths:
var texture = load(image_path)
if texture is Texture:
Expand All @@ -13,20 +13,6 @@ func load_extra_images_from_harddrive():
print("Failed to load texture: ", image_path)
print('Loaded extra images from HDD: ' + str(OS.get_ticks_msec() - CODETIME_START) + 'ms')

func get_png_files_in_dir(path):
var png_files = []
var dir = Directory.new()
if dir.open(path) == OK:
dir.list_dir_begin()
var file_name = dir.get_next()
while file_name != "":
if not dir.current_is_dir() and file_name.get_extension().to_lower() == "png":
png_files.append(path.plus_file(file_name))
file_name = dir.get_next()
else:
print("An error occurred when trying to access the directory: ", path)
return png_files


#func load_custom_images_into_array(DATA_ARRAY, thingtypeImageFolder):
# print("Loading /thing-images/" + thingtypeImageFolder + " directory ...")
Expand Down
57 changes: 57 additions & 0 deletions Autoload/Utils.gd
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,60 @@ func string_has_letters(string):
if regex.search(string) == null:
return true
return false

func get_filetype_in_directory(directory_path: String, file_extension: String) -> Array:
var files = []
var directory = Directory.new()
if directory.open(directory_path) == OK:
directory.list_dir_begin()
var file_name = directory.get_next()
while file_name != "":
if not directory.current_is_dir() and file_name.get_extension().to_lower() == file_extension.to_lower():
files.append(directory_path.plus_file(file_name))
file_name = directory.get_next()
directory.list_dir_end()
else:
print("Failed to open directory: ", directory_path)
return files

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

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

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("=")
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 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)
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
return config
151 changes: 77 additions & 74 deletions Scenes/CfgLoader.gd
Original file line number Diff line number Diff line change
@@ -1,36 +1,60 @@
extends Node
onready var oGame = Nodelist.list["oGame"]
onready var oMessage = Nodelist.list["oMessage"]


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

var terrain_cfg : Dictionary
var objects_cfg : Dictionary
var creature_cfg : Dictionary
var trapdoor_cfg : Dictionary
#var campaign_cfg : Dictionary
#var terrain_cfg : Dictionary
#var objects_cfg : Dictionary
#var creature_cfg : Dictionary
#var trapdoor_cfg : Dictionary


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

var CODETIME_LOADCFG_START = OS.get_ticks_msec()
# Step 1: set object data to default

Things.reset_thing_data_to_default()
# Step 2: load data from /fxdata/ config files
load_objects_data()
load_creatures_data()
load_trapdoor_data()
# Step 3: load data from local config files (map00001.objects.cfg)
var campaign_cfg = load_campaign_data(mapPath)

var config_dirs = {
LOAD_CFG_FXDATA : oGame.DK_FXDATA_DIRECTORY,
LOAD_CFG_CAMPAIGN : oGame.GAME_DIRECTORY.plus_file(campaign_cfg.get("common", {}).get("CONFIGS_LOCATION", "")),
LOAD_CFG_CURRENT_MAP : "" # Add the directory for LOAD_CFG_CURRENT_MAP if needed
}

# Load data from each directory in the specified order
for i in [LOAD_CFG_FXDATA, LOAD_CFG_CAMPAIGN, LOAD_CFG_CURRENT_MAP]:
var cfg_dir = config_dirs[i]
if cfg_dir == "": continue

match i:
LOAD_CFG_FXDATA: oMessage.quick("Step 1: Data loaded from /fxdata/")
LOAD_CFG_CAMPAIGN: oMessage.quick("Step 2: Data loaded from campaign configs directory")
LOAD_CFG_CURRENT_MAP: oMessage.quick("Step 3?")

load_objects_data(cfg_dir.plus_file("objects.cfg"))
load_creatures_data(cfg_dir.plus_file("creature.cfg"))
load_trapdoor_data(cfg_dir.plus_file("trapdoor.cfg"))
load_terrain_data(cfg_dir.plus_file("terrain.cfg"))

print('Loaded things from cfg files: ' + str(OS.get_ticks_msec() - CODETIME_LOADCFG_START) + 'ms')

func load_objects_data():


func load_objects_data(path):
var objects_cfg = Utils.read_dkcfg_file(path)
for section in objects_cfg:
if section.begins_with("object"):
var id = int(section)
Expand All @@ -45,37 +69,45 @@ func load_objects_data():
Things.DATA_OBJECT[id] = [newName, newSprite, newEditorTab]


func load_creatures_data():
for id_number in creature_cfg["common"]["Creatures"].size():
if Things.DATA_CREATURE.has(id_number+1) == false:
var newName = creature_cfg["common"]["Creatures"][id_number]
func load_creatures_data(path):
var creature_cfg = Utils.read_dkcfg_file(path)
var creatures = creature_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[id_number + 1] = [newName, newSprite, Things.TAB_CREATURE]
Things.DATA_CREATURE[creature_id] = [newName, newSprite, Things.TAB_CREATURE]


func load_trapdoor_data():
func load_trapdoor_data(path):
var trapdoor_cfg = Utils.read_dkcfg_file(path)
for section in trapdoor_cfg:
var id = int(section)
var trapOrDoor = -1
if section.begins_with("door"): trapOrDoor = Things.TYPE.DOOR
if section.begins_with("trap"): trapOrDoor = Things.TYPE.TRAP
if id == 0 or trapOrDoor == -1: continue
if section.begins_with("door"):
trapOrDoor = Things.TYPE.DOOR
elif section.begins_with("trap"):
trapOrDoor = Things.TYPE.TRAP
else:
continue

var data = trapdoor_cfg[section]
var newName = data.get("Name", null)
var newSprite = get_sprite(newName, null)
var crateName = data.get("Crate", null)

if trapOrDoor == Things.TYPE.DOOR:
Things.DATA_DOOR[id] = [newName, newSprite, Things.TAB_MISC]
elif trapOrDoor == Things.TYPE.TRAP:
Things.DATA_TRAP[id] = [newName, newSprite, Things.TAB_TRAP]

var crate_id_number = Things.find_subtype_by_name(Things.TYPE.OBJECT, crateName)
Things.LIST_OF_BOXES[crate_id_number] = [
trapOrDoor,
id,
]
#print(Things.LIST_OF_BOXES)
Things.LIST_OF_BOXES[crate_id_number] = [trapOrDoor, id]


func load_terrain_data(path):
var terrain_cfg = Utils.read_dkcfg_file(path)


func get_sprite(first_priority, second_priority):
Expand All @@ -84,45 +116,16 @@ func get_sprite(first_priority, second_priority):
return null


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

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

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("=")
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 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)
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

return config
func load_campaign_data(mapPath):
var levelsDirPath = mapPath.get_base_dir().get_base_dir()
var parentDirFolderName = levelsDirPath.get_file()
if parentDirFolderName != "levels" and parentDirFolderName != "campgns":
return {}
var list_of_main_campaign_files = Utils.get_filetype_in_directory(levelsDirPath, "cfg")
for campaignPath in list_of_main_campaign_files:
var cfgDictionary = Utils.read_dkcfg_file(campaignPath)
var levelsLocation = cfgDictionary.get("common", {}).get("LEVELS_LOCATION", null)
if levelsLocation and oGame.GAME_DIRECTORY.plus_file(levelsLocation).to_lower() == mapPath.get_base_dir().to_lower():
#print(oGame.GAME_DIRECTORY.plus_file(levelsLocation).to_lower())
return cfgDictionary
return {}
2 changes: 1 addition & 1 deletion Scenes/Menu.gd
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func add_edit_menu_items():
edit_popup.add_item("Undo", 0)
edit_popup.add_separator()
edit_popup.add_item("Map columns", 1)
edit_popup.add_item("Custom objects", 2)
#edit_popup.add_item("Custom objects", 2)
edit_popup.add_item("Resize map", 3)
edit_popup.add_item("Update all slabs", 4)
edit_popup.add_separator()
Expand Down
5 changes: 2 additions & 3 deletions Scenes/OpenMap.gd
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,15 @@ func open_map(filePath):

var map = filePath.get_basename()

oCfgLoader.start()

# Open all map file types
oCurrentMap.currentFilePaths = get_accompanying_files(map)

compressedFiles.clear()
for i in oCurrentMap.currentFilePaths.values():
if oRNC.check_for_rnc_compression(i[oCurrentMap.PATHSTRING]) == true:
compressedFiles.append(i[oCurrentMap.PATHSTRING])

oCfgLoader.start(filePath)

if compressedFiles.empty() == true:
# Load files

Expand Down

0 comments on commit 99897e6

Please sign in to comment.