1
1
extends Node
2
2
onready var oGame = Nodelist .list ["oGame" ]
3
+ onready var oMessage = Nodelist .list ["oMessage" ]
4
+
3
5
4
6
# These are dictionaries containing dictionaries.
5
7
# objects_cfg["section_name"]["key"] will return the "value"
6
8
# If there's a space in the value string, then the value will be an array of strings or integers.
7
9
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
+
12
16
13
- func start ():
17
+ enum {
18
+ LOAD_CFG_FXDATA ,
19
+ LOAD_CFG_CAMPAIGN ,
20
+ LOAD_CFG_CURRENT_MAP ,
21
+ }
22
+ func start (mapPath ):
14
23
if Cube .tex .empty () == true :
15
24
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' )
22
25
23
26
var CODETIME_LOADCFG_START = OS .get_ticks_msec ()
24
- # Step 1: set object data to default
27
+
25
28
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
+
31
52
print ('Loaded things from cfg files: ' + str (OS .get_ticks_msec () - CODETIME_LOADCFG_START ) + 'ms' )
32
53
33
- func load_objects_data ():
54
+
55
+
56
+ func load_objects_data (path ):
57
+ var objects_cfg = Utils .read_dkcfg_file (path )
34
58
for section in objects_cfg :
35
59
if section .begins_with ("object" ):
36
60
var id = int (section )
@@ -45,37 +69,45 @@ func load_objects_data():
45
69
Things .DATA_OBJECT [id ] = [newName , newSprite , newEditorTab ]
46
70
47
71
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 ]
52
79
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 ]
54
81
55
82
56
- func load_trapdoor_data ():
83
+ func load_trapdoor_data (path ):
84
+ var trapdoor_cfg = Utils .read_dkcfg_file (path )
57
85
for section in trapdoor_cfg :
58
86
var id = int (section )
59
87
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
63
94
64
95
var data = trapdoor_cfg [section ]
65
96
var newName = data .get ("Name" , null )
66
97
var newSprite = get_sprite (newName , null )
67
98
var crateName = data .get ("Crate" , null )
99
+
68
100
if trapOrDoor == Things .TYPE .DOOR :
69
101
Things .DATA_DOOR [id ] = [newName , newSprite , Things .TAB_MISC ]
70
102
elif trapOrDoor == Things .TYPE .TRAP :
71
103
Things .DATA_TRAP [id ] = [newName , newSprite , Things .TAB_TRAP ]
72
104
73
105
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 )
79
111
80
112
81
113
func get_sprite (first_priority , second_priority ):
@@ -84,45 +116,16 @@ func get_sprite(first_priority, second_priority):
84
116
return null
85
117
86
118
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 {}
0 commit comments