From 26a80ef52b65e8a748db13a14f0220fb02dc4223 Mon Sep 17 00:00:00 2001
From: Clemens Himmer <jackyboy@live.at>
Date: Fri, 22 Nov 2024 06:16:46 +0100
Subject: [PATCH] fix: allow account names with dashed in them to be imported
 correctly (#8370)

* fix: allow account names with dashed in them to be imported correctly

* move ReplaceCharAtIndex to Common.lua and fix indent

* Simplify logic

---------

Co-authored-by: Clemens Himmer <clemens@himmer.software>
Co-authored-by: Wires77 <Wires77@users.noreply.github.com>
---
 src/Classes/ImportTab.lua | 11 ++++++++---
 src/Modules/Common.lua    |  2 +-
 2 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/src/Classes/ImportTab.lua b/src/Classes/ImportTab.lua
index ac783a44be..f7f86967f0 100644
--- a/src/Classes/ImportTab.lua
+++ b/src/Classes/ImportTab.lua
@@ -424,7 +424,8 @@ function ImportTabClass:DownloadCharacterList()
 		accountName = self.controls.accountName.buf:gsub("^[%s?]+", ""):gsub("[%s?]+$", ""):gsub("%s", "+")
 	end
 	local sessionID = #self.controls.sessionInput.buf == 32 and self.controls.sessionInput.buf or (main.gameAccounts[accountName] and main.gameAccounts[accountName].sessionID)
-	launch:DownloadPage(realm.hostName.."character-window/get-characters?accountName="..accountName:gsub("-", "%%23"):gsub("#", "%%23").."&realm="..realm.realmCode, function(response, errMsg)
+	accountName = ReplaceDiscriminatorSafely(accountName)
+	launch:DownloadPage(realm.hostName.."character-window/get-characters?accountName="..accountName:gsub("#", "%%23").."&realm="..realm.realmCode, function(response, errMsg)
 		if errMsg == "Response code: 401" then
 			self.charImportStatus = colorCodes.NEGATIVE.."Sign-in is required."
 			self.charImportMode = "GETSESSIONID"
@@ -468,9 +469,9 @@ function ImportTabClass:DownloadCharacterList()
 				self.charImportMode = "GETSESSIONID"
 				return
 			end
-			realAccountName = realAccountName:gsub("-", "#")
-			self.controls.accountName:SetText(realAccountName)
+			realAccountName = ReplaceDiscriminatorSafely(realAccountName)
 			accountName = realAccountName
+			self.controls.accountName:SetText(realAccountName)
 			self.charImportStatus = "Character list successfully retrieved."
 			self.charImportMode = "SELECTCHAR"
 			self.lastRealm = realm.id
@@ -1138,6 +1139,10 @@ function HexToChar(x)
 	return string.char(tonumber(x, 16))
 end
 
+function ReplaceDiscriminatorSafely(accountName)
+	return accountName:gsub("(.*)[#%-]", "%1#")
+end
+
 function UrlDecode(url)
 	if url == nil then
 		return
diff --git a/src/Modules/Common.lua b/src/Modules/Common.lua
index 270e653d95..679b3d8ce1 100644
--- a/src/Modules/Common.lua
+++ b/src/Modules/Common.lua
@@ -947,4 +947,4 @@ function ImportBuild(importLink, callback)
 		-- try to decode input buffer
 		callback(Inflate(common.base64.decode(importLink:gsub("-", "+"):gsub("_", "/"))), nil)
 	end
-end
\ No newline at end of file
+end