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

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

Merged
merged 3 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
21 changes: 18 additions & 3 deletions src/Classes/ImportTab.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ReplaceDiscrimintorSafely(accountName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this behave fine when saving previously imported accounts in settings?

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"
Expand Down Expand Up @@ -468,9 +469,9 @@ function ImportTabClass:DownloadCharacterList()
self.charImportMode = "GETSESSIONID"
return
end
realAccountName = realAccountName:gsub("-", "#")
self.controls.accountName:SetText(realAccountName)
realAccountName = ReplaceDiscrimintorSafely(realAccountName)
accountName = realAccountName
self.controls.accountName:SetText(realAccountName)
self.charImportStatus = "Character list successfully retrieved."
self.charImportMode = "SELECTCHAR"
self.lastRealm = realm.id
Expand Down Expand Up @@ -1138,6 +1139,20 @@ function HexToChar(x)
return string.char(tonumber(x, 16))
end

function ReplaceDiscrimintorSafely(accountName)
reversedAccountName = string.reverse(accountName)
discriminatorIndex = 0
for i = 1, #reversedAccountName do
local c = reversedAccountName:sub(i,i)
if c == "#" or c == "-" then
discriminatorIndex = i
break
end
end
discriminatorIndex = discriminatorIndex * -1
Copy link
Member

@Wires77 Wires77 Nov 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, you don't have to reverse the string and the index here, just put your for loop in reverse instead

I just realized this can all be done in a single gsub

Suggested change
reversedAccountName = string.reverse(accountName)
discriminatorIndex = 0
for i = 1, #reversedAccountName do
local c = reversedAccountName:sub(i,i)
if c == "#" or c == "-" then
discriminatorIndex = i
break
end
end
discriminatorIndex = discriminatorIndex * -1
return accountName:gsub("(.*)[#%-]", "%1#")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't know that was possible in lua, cool stuff, but I see you removed it anyways.

gsub not working with regex totally did not click in my brain, props for finding a better shorthand.

return ReplaceCharAtIndex(accountName, discriminatorIndex, "#")
end

function UrlDecode(url)
if url == nil then
return
Expand Down
6 changes: 5 additions & 1 deletion src/Modules/Common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -947,4 +947,8 @@ function ImportBuild(importLink, callback)
-- try to decode input buffer
callback(Inflate(common.base64.decode(importLink:gsub("-", "+"):gsub("_", "/"))), nil)
end
end
end

function ReplaceCharAtIndex(str, pos, r)
return ("%s%s%s"):format(str:sub(1,pos-1), r, str:sub(pos+1))
end