Skip to content
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

refactor: ♻️ reworked disabling mods via user profile #297

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
32 changes: 16 additions & 16 deletions addons/mod_loader/api/profile.gd
Original file line number Diff line number Diff line change
Expand Up @@ -196,34 +196,32 @@ static func get_all_as_array() -> Array:
# Example: If "Mod-TestMod" is set in disabled_mods via the editor, the mod will appear disabled in the user profile.
# If the user then enables the mod in the profile the entry in disabled_mods will be removed.
static func _update_disabled_mods() -> void:
var user_profile_disabled_mods := []
var current_user_profile: ModUserProfile

current_user_profile = get_current()

# Check if a current user profile is set
if not ModLoaderStore.current_user_profile:
if not current_user_profile:
ModLoaderLog.info("There is no current user profile. The \"default\" profile will be created.", LOG_NAME)
return

current_user_profile = get_current()

# Iterate through the mod list in the current user profile to find disabled mods
for mod_id in current_user_profile.mod_list:
if not current_user_profile.mod_list[mod_id].is_active:
user_profile_disabled_mods.push_back(mod_id)

# Append the disabled mods to the global list of disabled mods
ModLoaderStore.ml_options.disabled_mods.append_array(user_profile_disabled_mods)
var mod_list_entry: Dictionary = current_user_profile.mod_list[mod_id]
ModLoaderStore.mod_data[mod_id].is_active = mod_list_entry.is_active

ModLoaderLog.debug(
"Updated the global list of disabled mods \"%s\", based on the current user profile \"%s\""
% [ModLoaderStore.ml_options.disabled_mods, current_user_profile.name],
"Updated the active state of all mods, based on the current user profile \"%s\""
% current_user_profile.name,
LOG_NAME)


# This function updates the mod lists of all user profiles with newly loaded mods that are not already present.
# It does so by comparing the current set of loaded mods with the mod list of each user profile, and adding any missing mods.
# Additionally, it checks for and deletes any mods from each profile's mod list that are no longer installed on the system.
static func _update_mod_lists() -> bool:
# Generate a list of currently present mods by combining the mods
# in mod_data and ml_options.disabled_mods from ModLoaderStore.
var current_mod_list := _generate_mod_list()

# Iterate over all user profiles
Expand All @@ -246,18 +244,17 @@ static func _update_mod_lists() -> bool:
return is_save_success


# Updates the mod list by checking the validity of each mod entry and making necessary modifications.
# This function takes a mod_list dictionary and optional mod_data dictionary as input and returns
# an updated mod_list dictionary. It iterates over each mod ID in the mod list, checks if the mod
# is still installed and if the current_config is present. If the mod is not installed or the current
# config is missing, the mod is removed or its current_config is reset to the default configuration.
static func _update_mod_list(mod_list: Dictionary, mod_data := ModLoaderStore.mod_data) -> Dictionary:
var updated_mod_list := mod_list.duplicate(true)

# Iterate over each mod ID in the mod list
for mod_id in updated_mod_list:
var mod_list_entry: Dictionary = updated_mod_list[mod_id]

# If mod data is accessible and the mod is loaded
if not mod_data.empty() and mod_data.has(mod_id):
mod_list_entry = _generate_mod_list_entry(mod_id, true)

# If mod data is accessible and the mod is not loaded
if not mod_data.empty() and not mod_data.has(mod_id):
# Check if the mod_dir for the mod-id exists
Expand Down Expand Up @@ -327,7 +324,10 @@ static func _set_mod_state(mod_id: String, profile_name: String, activate: bool)
return false

# Handle mod state
# Set state for user profile
ModLoaderStore.user_profiles[profile_name].mod_list[mod_id].is_active = activate
# Set state in the ModData
ModLoaderStore.mod_data[mod_id].is_active = activate

# Save profiles to the user profiles JSON file
var is_save_success := _save()
Expand Down
12 changes: 9 additions & 3 deletions addons/mod_loader/mod_loader.gd
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ func _init() -> void:

# Load user profiles into ModLoaderStore
var _success_user_profile_load := ModLoaderUserProfile._load()
# Update the list of disabled mods in ModLoaderStore based on the current user profile
ModLoaderUserProfile._update_disabled_mods()

_load_mods()

Expand Down Expand Up @@ -101,6 +99,9 @@ func _load_mods() -> void:
else:
ModLoaderLog.info("No mods were setup", LOG_NAME)

# Update active state of mods based on the current user profile
ModLoaderUserProfile._update_disabled_mods()

# Loop over all loaded mods via their entry in mod_data. Verify that they
# have all the required files (REQUIRED_MOD_FILES), load their meta data
# (from their manifest.json file), and verify that the meta JSON has all
Expand Down Expand Up @@ -154,6 +155,11 @@ func _load_mods() -> void:
# Instance every mod and add it as a node to the Mod Loader
for mod in ModLoaderStore.mod_load_order:
mod = mod as ModData

# Continue if mod is disabled
if not mod.is_active:
continue

ModLoaderLog.info("Initializing -> %s" % mod.manifest.get_mod_id(), LOG_NAME)
_init_mod(mod)

Expand Down Expand Up @@ -265,7 +271,7 @@ func _setup_mods() -> int:
ModLoaderLog.info("Skipped setting up mod: \"%s\"" % mod_dir_name, LOG_NAME)
continue

# Init the mod data
# Init the mod data for each mod
_init_mod_data(mod_dir_name)
unpacked_mods_count += 1

Expand Down
2 changes: 2 additions & 0 deletions addons/mod_loader/resources/mod_data.gd
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ var is_loadable := true
var is_overwrite := false
# True if mod can't be disabled or enabled in a user profile
var is_locked := false
# Flag indicating whether the mod should be loaded
var is_active := true
# Is increased for every mod depending on this mod. Highest importance is loaded first
var importance := 0
# Contents of the manifest
Expand Down