Skip to content

Commit

Permalink
feat: added _reset_extension (#195)
Browse files Browse the repository at this point in the history
* feat: added _reset_extension

* fix: comments and erase saved parent script after resetting
  • Loading branch information
otDan authored Mar 30, 2023
1 parent 429f8c5 commit c0a5624
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions addons/mod_loader/mod_loader.gd
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ var loaded_vanilla_parents_cache := {}
# Helps to decide whether a script extension should go through the _handle_script_extensions process
var is_initializing := true

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

# Main
# =============================================================================
Expand Down Expand Up @@ -703,12 +705,49 @@ func _apply_extension(extension_path)->Script:

var parent_script:Script = child_script.get_base_script()
var parent_script_path:String = parent_script.resource_path

# We want to save scripts for resetting later
# All the scripts are saved in order already
if not _saved_scripts.has(parent_script_path):
_saved_scripts[parent_script_path] = []
# The first entry in the script path array will be the copy of the base script
_saved_scripts[parent_script_path].append(parent_script.duplicate())
_saved_scripts[parent_script_path].append(child_script)
ModLoaderUtils.log_error("base script: %s" % str(_saved_scripts[parent_script_path]), LOG_NAME)

ModLoaderUtils.log_info("Installing script extension: %s <- %s" % [parent_script_path, extension_path], LOG_NAME)
child_script.take_over_path(parent_script_path)

return child_script


# Used to fully reset the provided script to a state prior of any extension
func _reset_extension(parent_script_path: String)->Script:
# Check path to file exists
if not File.new().file_exists(parent_script_path):
ModLoaderUtils.log_error("The parent script path '%s' does not exist" % [parent_script_path], LOG_NAME)
return null

# Check if the script to reset has been extended
if not _saved_scripts.has(parent_script_path):
ModLoaderUtils.log_error("The parent script path '%s' has not been extended" % [parent_script_path], LOG_NAME)
return null

# Check if the script to reset has anything actually saved
# If we ever encounter this it means something went very wrong in extending
if not _saved_scripts[parent_script_path].size() > 0:
ModLoaderUtils.log_error("The parent script path '%s' does not have the base script saved, this should never happen, if you encounter this please create an issue in the github repository" % [parent_script_path], LOG_NAME)
return null

var parent_script = _saved_scripts[parent_script_path][0]
parent_script.take_over_path(parent_script_path)

# Remove the script after it has been reset so we do not do it again
_saved_scripts.erase(parent_script_path)

return parent_script


# Helpers
# =============================================================================

Expand Down

0 comments on commit c0a5624

Please sign in to comment.