Skip to content

Commit 9879256

Browse files
feat: Add rename_profile() to ModLoaderUserProfile (#352)
* Added method to rename user profile * Corrected functional changes and indentation from review * Indentation * Corrected false indentation and error * Corrected false indentation and error * Updated some failed changes * refactor: ♻️ export props only exported properties are duplicated * refactor: 🔥 remove vars * fix: 🐛 wrong user_profiles ref --------- Co-authored-by: Kai <[email protected]>
1 parent 8e26af5 commit 9879256

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

addons/mod_loader/api/profile.gd

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,45 @@ static func create_profile(profile_name: String) -> bool:
9595
return is_save_success
9696

9797

98+
# Renames an existing user profile.
99+
#
100+
# Parameters:
101+
# - old_profile_name (String): The current name for the user profile (must be unique).
102+
# - new_profile_name (String): The new name for the user profile (must be unique).
103+
#
104+
# Returns: bool
105+
static func rename_profile(old_profile_name: String, new_profile_name: String) -> bool:
106+
# Verify that the old profile name is already in use
107+
if not ModLoaderStore.user_profiles.has(old_profile_name):
108+
ModLoaderLog.error("User profile with the name of \"%s\" does not exist." % old_profile_name, LOG_NAME)
109+
return false
110+
111+
# Verify that the new profile_name is not already in use
112+
if ModLoaderStore.user_profiles.has(new_profile_name):
113+
ModLoaderLog.error("User profile with the name of \"%s\" already exists." % new_profile_name, LOG_NAME)
114+
return false
115+
116+
# Rename user profile
117+
var profile_renamed := ModLoaderStore.user_profiles[old_profile_name].duplicate() as ModUserProfile
118+
profile_renamed.name = new_profile_name
119+
120+
# Remove old profile entry, replace it with new name entry in the ModLoaderStore
121+
ModLoaderStore.user_profiles.erase(old_profile_name)
122+
ModLoaderStore.user_profiles[new_profile_name] = profile_renamed
123+
124+
# Set it as the current profile if it was the current profile
125+
if ModLoaderStore.current_user_profile.name == old_profile_name:
126+
set_profile(profile_renamed)
127+
128+
# Store the new profile in the json file
129+
var is_save_success := _save()
130+
131+
if is_save_success:
132+
ModLoaderLog.debug("Renamed user profile from \"%s\" to \"%s\"" % [old_profile_name, new_profile_name], LOG_NAME)
133+
134+
return is_save_success
135+
136+
98137
# Sets the current user profile to the given user profile.
99138
#
100139
# Parameters:

addons/mod_loader/resources/mod_user_profile.gd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ class_name ModUserProfile
44

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

7-
var name := ""
8-
var mod_list := {}
7+
export var name := ""
8+
export var mod_list := {}
99

1010

1111
func _init(_name := "", _mod_list := {}) -> void:

0 commit comments

Comments
 (0)