Skip to content

refactor: ♻️ store Profile object instead of profile name #260

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
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
4 changes: 2 additions & 2 deletions addons/mod_loader/api/config.gd
Original file line number Diff line number Diff line change
Expand Up @@ -322,14 +322,14 @@ static func get_current_config(mod_id: String) -> ModConfig:
# The currently active configuration name for the given mod id or an empty string if not found.
static func get_current_config_name(mod_id: String) -> String:
# Check if user profile has been loaded
if not ModLoaderStore.user_profiles.has(ModLoaderStore.current_user_profile):
if not ModLoaderStore.user_profiles.has(ModLoaderStore.current_user_profile.name):
# Warn and return an empty string if the user profile has not been loaded
ModLoaderLog.warning("Can't get current mod config for \"%s\", because no current user profile is present." % mod_id, LOG_NAME)
return ""

# Retrieve the current user profile from ModLoaderStore
# *Can't use ModLoaderUserProfile because it causes a cyclic dependency*
var current_user_profile = ModLoaderStore.user_profiles[ModLoaderStore.current_user_profile]
var current_user_profile = ModLoaderStore.current_user_profile

# Check if the mod exists in the user profile's mod list and if it has a current config
if not current_user_profile.mod_list.has(mod_id) or not current_user_profile.mod_list[mod_id].has("current_config"):
Expand Down
83 changes: 38 additions & 45 deletions addons/mod_loader/api/profile.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,34 @@ extends Object
const LOG_NAME := "ModLoader:UserProfile"
const FILE_PATH_USER_PROFILES = "user://mod_user_profiles.json"

class Profile:
extends Reference

var name := ""
var mod_list := {}


# API profile functions
# =============================================================================

# Enables a mod - it will be loaded on the next game start
static func enable_mod(mod_id: String, profile_name := ModLoaderStore.current_user_profile) -> bool:
return _set_mod_state(mod_id, profile_name, true)
static func enable_mod(mod_id: String, user_profile := ModLoaderStore.current_user_profile) -> bool:
return _set_mod_state(mod_id, user_profile.name, true)


# Disables a mod - it will not be loaded on the next game start
static func disable_mod(mod_id: String, profile_name := ModLoaderStore.current_user_profile) -> bool:
return _set_mod_state(mod_id, profile_name, false)
static func disable_mod(mod_id: String, user_profile := ModLoaderStore.current_user_profile) -> bool:
return _set_mod_state(mod_id, user_profile.name, false)


# Sets the current config for a mod in a user profiles mod_list.
static func set_mod_current_config(mod_id: String, config_name: String, profile_name := ModLoaderStore.current_user_profile) -> bool:
static func set_mod_current_config(mod_id: String, mod_config: ModConfig, user_profile := ModLoaderStore.current_user_profile) -> bool:
# Verify whether the mod_id is present in the profile's mod_list.
if not _is_mod_id_in_mod_list(mod_id, profile_name):
return false

# Verify that the config_name exists
if not ModLoaderConfig.get_config(mod_id, config_name):
if not _is_mod_id_in_mod_list(mod_id, user_profile.name):
return false

# Update the current config in the mod_list of the user profile
get_profile(profile_name).mod_list[mod_id].current_config = config_name
user_profile.mod_list[mod_id].current_config = mod_config.name

# Store the new profile in the json file
var is_save_success := _save()

if is_save_success:
ModLoaderLog.debug("Set the \"current_config\" of \"%s\" to \"%s\" in user profile \"%s\" " % [mod_id, config_name, profile_name], LOG_NAME)
ModLoaderLog.debug("Set the \"current_config\" of \"%s\" to \"%s\" in user profile \"%s\" " % [mod_id, mod_config.name, user_profile.name], LOG_NAME)

return is_save_success

Expand All @@ -65,7 +55,7 @@ static func create_profile(profile_name: String) -> bool:
return false

# Set it as the current profile
ModLoaderStore.current_user_profile = profile_name
ModLoaderStore.current_user_profile = new_profile

# Store the new profile in the ModLoaderStore
ModLoaderStore.user_profiles[profile_name] = new_profile
Expand All @@ -79,62 +69,62 @@ static func create_profile(profile_name: String) -> bool:
return is_save_success


# Sets the current user profile to the specified profile_name.
static func set_profile(profile_name: String) -> bool:
# Check if there is a user profile with the specified name
if not ModLoaderStore.user_profiles.has(profile_name):
ModLoaderLog.error("User profile with name \"%s\" not found." % profile_name, LOG_NAME)
# Sets the current user profile to the profile with the specified profile_name.
static func set_profile(user_profile: ModUserProfile) -> bool:
# Check if the profile name is unique
if not ModLoaderStore.user_profiles.has(user_profile.name):
ModLoaderLog.error("User profile with name \"%s\" not found." % user_profile.name, LOG_NAME)
return false

# Update the current_user_profile in the ModLoaderStore
ModLoaderStore.current_user_profile = profile_name
ModLoaderStore.current_user_profile = user_profile

# Save changes in the json file
var is_save_success := _save()

if is_save_success:
ModLoaderLog.debug("Current user profile set to \"%s\"" % profile_name, LOG_NAME)
ModLoaderLog.debug("Current user profile set to \"%s\"" % user_profile.name, LOG_NAME)

return is_save_success


# Deletes a user profile with the given profile_name.
static func delete_profile(profile_name: String) -> bool:
# If the current_profile is about to get deleted change it to default
if ModLoaderStore.current_user_profile == profile_name:
static func delete_profile(user_profile: ModUserProfile) -> bool:
# If the current_profile is about to get deleted log an error
if ModLoaderStore.current_user_profile.name == user_profile.name:
ModLoaderLog.error(str(
"You cannot delete the currently selected user profile \"%s\" " +
"because it is currently in use. Please switch to a different profile before deleting this one.") % profile_name,
"because it is currently in use. Please switch to a different profile before deleting this one.") % user_profile.name,
LOG_NAME)
return false

# Deleting the default profile is not allowed
if profile_name == ModLoaderConfig.DEFAULT_CONFIG_NAME:
if user_profile.name == "default":
ModLoaderLog.error("You can't delete the default profile", LOG_NAME)
return false

# Delete the user profile
if not ModLoaderStore.user_profiles.erase(profile_name):
if not ModLoaderStore.user_profiles.erase(user_profile.name):
# Erase returns false if the the key is not present in user_profiles
ModLoaderLog.error("User profile with name \"%s\" not found." % profile_name, LOG_NAME)
ModLoaderLog.error("User profile with name \"%s\" not found." % user_profile.name, LOG_NAME)
return false

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

if is_save_success:
ModLoaderLog.debug("Deleted user profile \"%s\"" % profile_name, LOG_NAME)
ModLoaderLog.debug("Deleted user profile \"%s\"" % user_profile.name, LOG_NAME)

return is_save_success


# Returns the current user profile
static func get_current() -> Profile:
return ModLoaderStore.user_profiles[ModLoaderStore.current_user_profile]
static func get_current() -> ModUserProfile:
return ModLoaderStore.current_user_profile


# Return the user profile with the given name
static func get_profile(profile_name: String) -> Profile:
static func get_profile(profile_name: String) -> ModUserProfile:
if not ModLoaderStore.user_profiles.has(profile_name):
ModLoaderLog.error("User profile with name \"%s\" not found." % profile_name, LOG_NAME)
return null
Expand All @@ -161,10 +151,10 @@ static func get_all_as_array() -> Array:
# 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: Profile
var current_user_profile: ModUserProfile

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

Expand Down Expand Up @@ -192,7 +182,7 @@ static func _update_mod_lists() -> bool:

# Iterate over all user profiles
for profile_name in ModLoaderStore.user_profiles.keys():
var profile: Profile = ModLoaderStore.user_profiles[profile_name]
var profile: ModUserProfile = ModLoaderStore.user_profiles[profile_name]

# Merge the profiles mod_list with the previously created current_mod_list
profile.mod_list.merge(current_mod_list)
Expand Down Expand Up @@ -322,8 +312,8 @@ static func _is_mod_id_in_mod_list(mod_id: String, profile_name: String) -> bool

# Creates a new Profile with the given name and mod list.
# Returns the newly created Profile object.
static func _create_new_profile(profile_name: String, mod_list: Dictionary) -> Profile:
var new_profile := Profile.new()
static func _create_new_profile(profile_name: String, mod_list: Dictionary) -> ModUserProfile:
var new_profile := ModUserProfile.new()

# If no name is provided, log an error and return null
if profile_name == "":
Expand Down Expand Up @@ -355,7 +345,10 @@ static func _load() -> bool:
return false

# Set the current user profile to the one specified in the data
ModLoaderStore.current_user_profile = data.current_profile
var current_user_profile: ModUserProfile = ModUserProfile.new()
current_user_profile.name = data.current_profile
current_user_profile.mod_list = data.profiles[data.current_profile].mod_list
ModLoaderStore.current_user_profile = current_user_profile

# Loop through each profile in the data and add them to ModLoaderStore
for profile_name in data.profiles.keys():
Expand All @@ -378,11 +371,11 @@ static func _save() -> bool:
}

# Set the current profile name in the save_dict
save_dict.current_profile = ModLoaderStore.current_user_profile
save_dict.current_profile = ModLoaderStore.current_user_profile.name

# Serialize the mod_list data for each user profile and add it to the save_dict
for profile_name in ModLoaderStore.user_profiles.keys():
var profile: Profile = ModLoaderStore.user_profiles[profile_name]
var profile: ModUserProfile = ModLoaderStore.user_profiles[profile_name]

# Init the profile dict
save_dict.profiles[profile.name] = {}
Expand Down
2 changes: 1 addition & 1 deletion addons/mod_loader/classes/mod_data.gd
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func _load_config(config_file_path: String) -> void:

# Update the mod_list of the current user profile
func _set_current_config(new_current_config: ModConfig) -> void:
ModLoaderUserProfile.set_mod_current_config(dir_name, new_current_config.name)
ModLoaderUserProfile.set_mod_current_config(dir_name, new_current_config)
current_config = new_current_config
ModLoader.emit_signal("current_config_changed", new_current_config)

Expand Down
13 changes: 13 additions & 0 deletions addons/mod_loader/classes/mod_user_profile.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
extends Resource
class_name ModUserProfile


# This Class is used to represent a User Profile for the ModLoader.

var name := ""
var mod_list := {}


func _init(_name := "", _mod_list := {}) -> void:
name = _name
mod_list = _mod_list
2 changes: 1 addition & 1 deletion addons/mod_loader/mod_loader_store.gd
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ var logged_messages := {
}

# Active user profile
var current_user_profile := ""
var current_user_profile: ModUserProfile
# List of user profiles loaded from user://mods.json
var user_profiles := {}

Expand Down