2 class_name DialogicResourceUtil
4 static var label_cache := {}
5 static var event_cache: Array[DialogicEvent] = []
7 static var special_resources := {}
10 static func update() -> void:
11 update_directory('.dch')
12 update_directory('.dtl')
16 #region RESOURCE DIRECTORIES
17 ################################################################################
19 static func get_directory(extension:String) -> Dictionary:
20 extension = extension.trim_prefix('.')
21 if Engine.has_meta(extension+'_directory'):
22 return Engine.get_meta(extension+'_directory', {})
24 var directory: Dictionary = ProjectSettings.get_setting("dialogic/directories/"+extension+'_directory', {})
25 Engine.set_meta(extension+'_directory', directory)
29 static func set_directory(extension:String, directory:Dictionary) -> void:
30 extension = extension.trim_prefix('.')
31 if Engine.is_editor_hint():
32 ProjectSettings.set_setting("dialogic/directories/"+extension+'_directory', directory)
33 ProjectSettings.save()
34 Engine.set_meta(extension+'_directory', directory)
37 static func update_directory(extension:String) -> void:
38 var directory := get_directory(extension)
40 for resource in list_resources_of_type(extension):
41 if not resource in directory.values():
42 directory = add_resource_to_directory(resource, directory)
44 var keys_to_remove := []
46 if not ResourceLoader.exists(directory[key]):
47 keys_to_remove.append(key)
48 for key in keys_to_remove:
51 set_directory(extension, directory)
54 static func add_resource_to_directory(file_path:String, directory:Dictionary) -> Dictionary:
55 var suggested_name := file_path.get_file().trim_suffix("."+file_path.get_extension())
56 while suggested_name in directory:
57 suggested_name = file_path.trim_suffix("/"+suggested_name+"."+file_path.get_extension()).get_file().path_join(suggested_name)
58 directory[suggested_name] = file_path
62 ## Returns the unique identifier for the given resource path.
63 ## Returns an empty string if no identifier was found.
64 static func get_unique_identifier(file_path:String) -> String:
65 var identifier: String = get_directory(file_path.get_extension()).find_key(file_path)
66 if typeof(identifier) == TYPE_STRING:
71 ## Returns the resource associated with the given unique identifier.
72 ## The expected extension is needed to use the right directory.
73 static func get_resource_from_identifier(identifier:String, extension:String) -> Resource:
74 var path: String = get_directory(extension).get(identifier, '')
75 if ResourceLoader.exists(path):
80 static func change_unique_identifier(file_path:String, new_identifier:String) -> void:
81 var directory := get_directory(file_path.get_extension())
82 var key: String = directory.find_key(file_path)
84 if key == new_identifier:
87 directory[new_identifier] = file_path
88 key = directory.find_key(file_path)
89 set_directory(file_path.get_extension(), directory)
92 static func change_resource_path(old_path:String, new_path:String) -> void:
93 var directory := get_directory(new_path.get_extension())
94 var key: String = directory.find_key(old_path)
96 directory[key] = new_path
97 key = directory.find_key(old_path)
98 set_directory(new_path.get_extension(), directory)
101 static func remove_resource(file_path:String) -> void:
102 var directory := get_directory(file_path.get_extension())
103 var key: String = directory.find_key(file_path)
106 key = directory.find_key(file_path)
107 set_directory(file_path.get_extension(), directory)
110 static func is_identifier_unused(extension:String, identifier:String) -> bool:
111 return not identifier in get_directory(extension)
116 ################################################################################
117 # The label cache is only for the editor so we don't have to scan all timelines
118 # whenever we want to suggest labels. This has no use in game and is not always perfect.
120 static func get_label_cache() -> Dictionary:
121 if not label_cache.is_empty():
124 label_cache = DialogicUtil.get_editor_setting('label_ref', {})
128 static func set_label_cache(cache:Dictionary) -> void:
132 static func update_label_cache() -> void:
133 var cache := get_label_cache()
134 var timelines := get_timeline_directory().values()
135 for timeline in cache:
136 if !timeline in timelines:
137 cache.erase(timeline)
138 set_label_cache(cache)
143 ################################################################################
145 ## Dialogic keeps a list that has each event once. This allows retrieval of that list.
146 static func get_event_cache() -> Array:
147 if not event_cache.is_empty():
150 event_cache = update_event_cache()
154 static func update_event_cache() -> Array:
156 for indexer in DialogicUtil.get_indexers():
158 for event in indexer._get_events():
159 if not ResourceLoader.exists(event):
161 if not 'event_end_branch.gd' in event and not 'event_text.gd' in event:
162 event_cache.append(load(event).new())
164 # Events are checked in order while testing them. EndBranch needs to be first, Text needs to be last
165 event_cache.push_front(DialogicEndBranchEvent.new())
166 event_cache.push_back(DialogicTextEvent.new())
172 #region SPECIAL RESOURCES
173 ################################################################################
175 static func update_special_resources() -> void:
176 special_resources.clear()
177 for indexer in DialogicUtil.get_indexers():
178 var additions := indexer._get_special_resources()
179 for resource_type in additions:
180 if not resource_type in special_resources:
181 special_resources[resource_type] = {}
182 special_resources[resource_type].merge(additions[resource_type])
185 static func list_special_resources(type:String, filter := {}) -> Dictionary:
186 if special_resources.is_empty():
187 update_special_resources()
188 if type in special_resources:
189 if filter.is_empty():
190 return special_resources[type]
193 for i in special_resources[type]:
194 if match_resource_filter(special_resources[type][i], filter):
195 results[i] = special_resources[type][i]
200 static func match_resource_filter(dict:Dictionary, filter:Dictionary) -> bool:
204 if typeof(filter[i]) == TYPE_ARRAY:
205 if not dict[i] in filter[i]:
208 if not dict[i] == filter[i]:
213 static func guess_special_resource(type: String, string: String, default := {}, filter := {}, ignores:PackedStringArray=[]) -> Dictionary:
214 if string.is_empty():
217 if special_resources.is_empty():
218 update_special_resources()
219 var resources := list_special_resources(type, filter)
220 if resources.is_empty():
221 printerr("[Dialogic] No ", type, "s found, but attempted to use one.")
224 if string.begins_with('res://'):
225 for i in resources.values():
228 printerr("[Dialogic] Unable to find ", type, " at path '", string, "'.")
231 string = string.to_lower()
233 if string in resources:
234 return resources[string]
236 if not ignores.is_empty():
237 var regex := RegEx.create_from_string(r" ?\b(" + "|".join(ignores) + r")\b")
238 for name in resources:
239 if regex.sub(name, "") == regex.sub(string, ""):
240 return resources[name]
242 ## As a last effort check against the unfiltered list
243 if string in special_resources[type]:
244 push_warning("[Dialogic] Using ", type, " '", string,"' when not supposed to.")
245 return special_resources[type][string]
247 printerr("[Dialogic] Unable to identify ", type, " based on string '", string, "'.")
253 ################################################################################
255 static func get_character_directory() -> Dictionary:
256 return get_directory('dch')
259 static func get_timeline_directory() -> Dictionary:
260 return get_directory('dtl')
263 static func get_timeline_resource(timeline_identifier:String) -> DialogicTimeline:
264 return get_resource_from_identifier(timeline_identifier, 'dtl')
267 static func get_character_resource(character_identifier:String) -> DialogicCharacter:
268 return get_resource_from_identifier(character_identifier, 'dch')
271 static func list_resources_of_type(extension:String) -> Array:
272 var all_resources := scan_folder('res://', extension)
276 static func scan_folder(path:String, extension:String) -> Array:
278 if DirAccess.dir_exists_absolute(path):
279 var dir := DirAccess.open(path)
281 var file_name := dir.get_next()
282 while file_name != "":
283 if dir.current_is_dir() and not file_name.begins_with("."):
284 list += scan_folder(path.path_join(file_name), extension)
286 if file_name.ends_with(extension):
287 list.append(path.path_join(file_name))
288 file_name = dir.get_next()