Skip to content

Commit cd22903

Browse files
committed
added a table to avoid iterating through the load order every comparison
1 parent 09f6900 commit cd22903

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

addons/mod_loader/internal/script_extension.gd

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ static func handle_script_extensions() -> void:
3030
# a script extending script B if A is an ancestor of B.
3131
class InheritanceSorting:
3232
var stack_cache := {}
33-
33+
var load_order_table := {}
34+
var unpacked_dir = _ModLoaderPath.get_unpacked_mods_dir_path()
35+
36+
func _init():
37+
_populate_load_order_table()
38+
3439
# Comparator function. return true if a should go before b. This may
3540
# enforce conditions beyond the stated inheritance relationship.
3641
func _check_inheritances(extension_a: String, extension_b: String) -> bool:
@@ -72,17 +77,17 @@ class InheritanceSorting:
7277
# Secondary comparator function for resolving scripts extending the same vanilla script
7378
# Will return whether a comes before b in the load order
7479
func compare_mods_order(extension_a:String, extension_b:String)->bool:
75-
var mod_a_id = extension_a.trim_prefix(_ModLoaderPath.get_unpacked_mods_dir_path()).get_slice("/", 0)
76-
var mod_b_id = extension_b.trim_prefix(_ModLoaderPath.get_unpacked_mods_dir_path()).get_slice("/", 0)
80+
var mod_a_id:String = ModLoaderUtils.get_string_in_between(extension_a, unpacked_dir, "/")
81+
var mod_b_id:String = ModLoaderUtils.get_string_in_between(extension_b, unpacked_dir, "/")
7782

83+
return load_order_table[mod_a_id] < load_order_table[mod_b_id]
84+
85+
# Populate a load order dictionary for faster access and comparison between mod ids
86+
func _populate_load_order_table() -> void:
87+
var mod_index := 0
7888
for mod in ModLoaderStore.mod_load_order:
79-
if mod.dir_name == mod_a_id:
80-
return true
81-
elif mod.dir_name == mod_b_id:
82-
return false
83-
84-
# Should never happen
85-
return extension_a < extension_b
89+
load_order_table[mod.dir_name] = mod_index
90+
mod_index += 1
8691

8792

8893
static func apply_extension(extension_path: String) -> Script:

0 commit comments

Comments
 (0)