Skip to content

Commit 21dea2a

Browse files
authored
refactor: ♻️ reworked disabling mods via user profile (#297)
1 parent 54018b2 commit 21dea2a

File tree

3 files changed

+27
-19
lines changed

3 files changed

+27
-19
lines changed

addons/mod_loader/api/profile.gd

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -196,34 +196,32 @@ static func get_all_as_array() -> Array:
196196
# Example: If "Mod-TestMod" is set in disabled_mods via the editor, the mod will appear disabled in the user profile.
197197
# If the user then enables the mod in the profile the entry in disabled_mods will be removed.
198198
static func _update_disabled_mods() -> void:
199-
var user_profile_disabled_mods := []
200199
var current_user_profile: ModUserProfile
201200

201+
current_user_profile = get_current()
202+
202203
# Check if a current user profile is set
203-
if not ModLoaderStore.current_user_profile:
204+
if not current_user_profile:
204205
ModLoaderLog.info("There is no current user profile. The \"default\" profile will be created.", LOG_NAME)
205206
return
206207

207-
current_user_profile = get_current()
208-
209208
# Iterate through the mod list in the current user profile to find disabled mods
210209
for mod_id in current_user_profile.mod_list:
211-
if not current_user_profile.mod_list[mod_id].is_active:
212-
user_profile_disabled_mods.push_back(mod_id)
213-
214-
# Append the disabled mods to the global list of disabled mods
215-
ModLoaderStore.ml_options.disabled_mods.append_array(user_profile_disabled_mods)
210+
var mod_list_entry: Dictionary = current_user_profile.mod_list[mod_id]
211+
ModLoaderStore.mod_data[mod_id].is_active = mod_list_entry.is_active
216212

217213
ModLoaderLog.debug(
218-
"Updated the global list of disabled mods \"%s\", based on the current user profile \"%s\""
219-
% [ModLoaderStore.ml_options.disabled_mods, current_user_profile.name],
214+
"Updated the active state of all mods, based on the current user profile \"%s\""
215+
% current_user_profile.name,
220216
LOG_NAME)
221217

222218

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

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

248246

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

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

257-
# If mod data is accessible and the mod is loaded
258-
if not mod_data.empty() and mod_data.has(mod_id):
259-
mod_list_entry = _generate_mod_list_entry(mod_id, true)
260-
261258
# If mod data is accessible and the mod is not loaded
262259
if not mod_data.empty() and not mod_data.has(mod_id):
263260
# Check if the mod_dir for the mod-id exists
@@ -327,7 +324,10 @@ static func _set_mod_state(mod_id: String, profile_name: String, activate: bool)
327324
return false
328325

329326
# Handle mod state
327+
# Set state for user profile
330328
ModLoaderStore.user_profiles[profile_name].mod_list[mod_id].is_active = activate
329+
# Set state in the ModData
330+
ModLoaderStore.mod_data[mod_id].is_active = activate
331331

332332
# Save profiles to the user profiles JSON file
333333
var is_save_success := _save()

addons/mod_loader/mod_loader.gd

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ func _init() -> void:
6161

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

6765
_load_mods()
6866

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

102+
# Update active state of mods based on the current user profile
103+
ModLoaderUserProfile._update_disabled_mods()
104+
104105
# Loop over all loaded mods via their entry in mod_data. Verify that they
105106
# have all the required files (REQUIRED_MOD_FILES), load their meta data
106107
# (from their manifest.json file), and verify that the meta JSON has all
@@ -154,6 +155,11 @@ func _load_mods() -> void:
154155
# Instance every mod and add it as a node to the Mod Loader
155156
for mod in ModLoaderStore.mod_load_order:
156157
mod = mod as ModData
158+
159+
# Continue if mod is disabled
160+
if not mod.is_active:
161+
continue
162+
157163
ModLoaderLog.info("Initializing -> %s" % mod.manifest.get_mod_id(), LOG_NAME)
158164
_init_mod(mod)
159165

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

268-
# Init the mod data
274+
# Init the mod data for each mod
269275
_init_mod_data(mod_dir_name)
270276
unpacked_mods_count += 1
271277

addons/mod_loader/resources/mod_data.gd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ var is_loadable := true
3333
var is_overwrite := false
3434
# True if mod can't be disabled or enabled in a user profile
3535
var is_locked := false
36+
# Flag indicating whether the mod should be loaded
37+
var is_active := true
3638
# Is increased for every mod depending on this mod. Highest importance is loaded first
3739
var importance := 0
3840
# Contents of the manifest

0 commit comments

Comments
 (0)