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

feat: Add rename_profile() to ModLoaderUserProfile #352

Merged
merged 9 commits into from
Jan 26, 2025
39 changes: 39 additions & 0 deletions addons/mod_loader/api/profile.gd
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,45 @@ static func create_profile(profile_name: String) -> bool:
return is_save_success


# Renames an existing user profile.
#
# Parameters:
# - old_profile_name (String): The current name for the user profile (must be unique).
# - new_profile_name (String): The new name for the user profile (must be unique).
#
# Returns: bool
static func rename_profile(old_profile_name: String, new_profile_name: String) -> bool:
# Verify that the old profile name is already in use
if not ModLoaderStore.user_profiles.has(old_profile_name):
ModLoaderLog.error("User profile with the name of \"%s\" does not exist." % old_profile_name, LOG_NAME)
return false

# Verify that the new profile_name is not already in use
if ModLoaderStore.user_profiles.has(new_profile_name):
ModLoaderLog.error("User profile with the name of \"%s\" already exists." % new_profile_name, LOG_NAME)
return false

# Rename user profile
var profile_renamed := ModLoaderStore.user_profiles[old_profile_name].duplicate() as ModUserProfile
profile_renamed.name = new_profile_name

# Remove old profile entry, replace it with new name entry in the ModLoaderStore
ModLoaderStore.user_profiles.erase(old_profile_name)
ModLoaderStore.user_profiles[new_profile_name] = profile_renamed

# Set it as the current profile if it was the current profile
if ModLoaderStore.current_user_profile.name == old_profile_name:
set_profile(profile_renamed)

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

if is_save_success:
ModLoaderLog.debug("Renamed user profile from \"%s\" to \"%s\"" % [old_profile_name, new_profile_name], LOG_NAME)

return is_save_success


# Sets the current user profile to the given user profile.
#
# Parameters:
Expand Down
4 changes: 2 additions & 2 deletions addons/mod_loader/resources/mod_user_profile.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ class_name ModUserProfile

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

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


func _init(_name := "", _mod_list := {}) -> void:
Expand Down
Loading