Skip to content

fix: 🐛 Transitive Comparator for script sorting #277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 28 additions & 27 deletions addons/mod_loader/internal/script_extension.gd
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ static func handle_script_extensions() -> void:
var parent_script: Script = child_script.get_base_script()
var parent_script_path: String = parent_script.resource_path

if not ModLoaderStore.loaded_vanilla_parents_cache.keys().has(parent_script_path):
ModLoaderStore.loaded_vanilla_parents_cache[parent_script_path] = parent_script

script_extension_data_array.push_back(
ScriptExtensionData.new(extension_path, parent_script_path, mod_id)
)
Expand All @@ -38,9 +35,6 @@ static func handle_script_extensions() -> void:
# Inheritance is more important so this called last
script_extension_data_array.sort_custom(InheritanceSorting, "_check_inheritances")

# This saved some bugs in the past.
ModLoaderStore.loaded_vanilla_parents_cache.clear()

# Load and install all extensions
for extension in script_extension_data_array:
var script: Script = apply_extension(extension.extension_path)
Expand All @@ -49,28 +43,35 @@ static func handle_script_extensions() -> void:

# Inner class so the sort function can be called by handle_script_extensions()
class InheritanceSorting:
# Go up extension_a's inheritance tree to find if any parent shares the same vanilla path as extension_b
static func _check_inheritances(extension_a: ScriptExtensionData, extension_b: ScriptExtensionData)->bool:
var a_child_script: Script

if ModLoaderStore.loaded_vanilla_parents_cache.keys().has(extension_a.parent_script_path):
a_child_script = ResourceLoader.load(extension_a.parent_script_path)
else:
a_child_script = ResourceLoader.load(extension_a.parent_script_path)
ModLoaderStore.loaded_vanilla_parents_cache[extension_a.parent_script_path] = a_child_script

var a_parent_script: Script = a_child_script.get_base_script()

if a_parent_script == null:

static func _check_inheritances(extension_a:ScriptExtensionData, extension_b:ScriptExtensionData)->bool:
var a_stack := []
var parent_script: Script = load(extension_a.extension_path)
while parent_script:
a_stack.push_front(parent_script.resource_path)
parent_script = parent_script.get_base_script()
a_stack.pop_back()

var b_stack := []
parent_script = load(extension_b.extension_path)
while parent_script:
b_stack.push_front(parent_script.resource_path)
parent_script = parent_script.get_base_script()
b_stack.pop_back()

var last_index: int
for index in a_stack.size():
if index >= b_stack.size():
return false
if a_stack[index] != b_stack[index]:
return a_stack[index] < b_stack[index]
last_index = index

if last_index < b_stack.size():
# 'a' has a shorter stack
return true

var a_parent_script_path = a_parent_script.resource_path
if a_parent_script_path == extension_b.parent_script_path:
return false

else:
return _check_inheritances(ScriptExtensionData.new(extension_a.extension_path, a_parent_script_path, extension_a.mod_id), extension_b)


return extension_a.extension_path < extension_b.extension_path

static func apply_extension(extension_path: String) -> Script:
# Check path to file exists
Expand Down
3 changes: 0 additions & 3 deletions addons/mod_loader/mod_loader_store.gd
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ var has_shown_editor_zips_warning := false
# Things to keep to ensure they are not garbage collected (used by `save_scene`)
var saved_objects := []

# Store vanilla classes for script extension sorting
var loaded_vanilla_parents_cache := {}

# Stores all the taken over scripts for restoration
var saved_scripts := {}

Expand Down