Files

218 lines
12 KiB
Lua

-- Private closure: DPAPI-backed account list. No credential CVar, SavedVariable or log.
local vault, panel, parent, intent, loginAccount, selected, page, installed
local rows, pageSize = {}, 8
local function query(op,row,chunk)
local fn=C_Login and C_Login.GetTurtleRealmSnapshot
if type(fn)~='function' then return -1 end
local ok,value=pcall(fn,op,row or 0,chunk or 0)
return ok and type(value)=='number' and value or -1
end
local function note(text) StaticPopup_Show('OKAY',text) end
local function empty() return {accounts={},locked=false,streamer=false} end
local function encode(v)
local parts={'GWC2',string.char(#v.accounts,(v.locked and 1 or 0)+(v.streamer and 2 or 0))}
for _,a in ipairs(v.accounts) do
local name,password,label,character=a.account,a.password,a.label or '',a.character or ''
local index=a.index or 0
if #name>32 or #password>64 or #label>64 or #character>64 or index>1000 then return end
parts[#parts+1]=string.char(#name,#password,#label,#character,index%256,math.floor(index/256))..name..password..label..character
end
return table.concat(parts)
end
local function decode(raw)
if #raw<6 or raw:sub(1,4)~='GWC2' then return end
local count,flags=raw:byte(5,6)
if count>16 or flags>3 then return end
local v=empty();v.locked=flags%2==1;v.streamer=flags>=2
local at=7
for i=1,count do
if at+5>#raw then return end
local n,p,l,c,lo,hi=raw:byte(at,at+5);at=at+6
if n==0 or p==0 or n>32 or p>64 or l>64 or c>64 or at+n+p+l+c-1>#raw then return end
local a={index=lo+hi*256};local lengths={n,p,l,c}
for j,key in ipairs({'account','password','label','character'}) do
a[key]=raw:sub(at,at+lengths[j]-1);at=at+lengths[j]
end
if a.index>1000 then return end
v.accounts[i]=a
end
if at~=#raw+1 then return end
return v
end
local function read()
local count=query(16)
if count==0 then return empty() end
if count<6 or count>4096 then return end
local parts={}
for offset=0,count-1,3 do
local word=query(17,offset)
if word<0 or word>16777215 then return end
for i=1,math.min(3,count-offset) do parts[#parts+1]=string.char(word%256);word=math.floor(word/256) end
end
return decode(table.concat(parts))
end
local function write(v)
local raw=encode(v)
if not raw or query(18,#raw,2)~=0 then return false end
for offset=1,#raw,3 do
local a,b,c=raw:byte(offset,offset+2)
if query(19,offset-1,a+(b or 0)*256+(c or 0)*65536)~=0 then return false end
end
return query(20)==0
end
local function find(v,name)
for i,a in ipairs(v.accounts) do if a.account:upper()==name:upper() then return i,a end end
end
local refresh
local function change(fn)
local current=read()
if not current then note('Saved accounts could not be opened.');return end
fn(current)
if not write(current) then note('Saved accounts could not be saved.');return end
vault=current;refresh()
end
local function button(owner,text,x,y,width,fn)
local b=CreateFrame('Button',nil,owner,'GlueButtonBigTemplate')
b:SetSize(width,26);b:SetPoint('TOPLEFT',owner,'TOPLEFT',x,y);b:SetText(text);b:SetScript('OnClick',fn)
b:SetNormalFontObject('GlueFontNormalSmall');b:SetHighlightFontObject('GlueFontHighlightSmall')
return b
end
local function charInfo(index)
local util=CharacterSelectListUtil
if not util or not CharacterSelectUtil then return end
local id=util.GetCharIDFromIndex(index)
if not id or id==0 then return end
return CharacterSelectUtil.GetCharacterInfoTable(id),id
end
local function rememberCharacter()
if not loginAccount then return end
local id=GetCharacterSelection()
local index=CharacterSelectListUtil and CharacterSelectListUtil.GetIndexFromCharID(id)
local info=index and charInfo(index)
if not info or not info.characterName then return end
local current=read();if not current then return end
local _,a=find(current,loginAccount)
if a then a.character=info.characterName;a.index=index;write(current) end
end
local function enterLast()
if not intent or not CharacterSelect or not CharacterSelect:IsShown() then return end
if GetTime()>intent.expires then intent=nil;return end
local wanted=intent
local count=math.min(GetNumCharacters(true) or 0,1000)
if count==0 then return end
local index
if wanted.character~='' then
for i=1,count do local info=charInfo(i);if info and info.characterName==wanted.character then index=i;break end end
-- Never enter an unrelated character when the saved character was removed.
if not index then intent=nil;note('The saved character is no longer available. Select a character to enter.');return end
elseif wanted.index>0 and wanted.index<=count and charInfo(wanted.index) then index=wanted.index
else
-- First use/migration has no local history: use the server-selected character.
index=CharacterSelectListUtil.GetIndexFromCharID(GetCharacterSelection())
if not index or not charInfo(index) then for i=1,count do if charInfo(i) then index=i;break end end end
if not index then return end
end
intent=nil -- one attempt, including repeated CHARACTER_LIST_UPDATE events
CharacterSelect_SelectCharacter(index,true)
if CharacterSelect_AllowedToEnterWorld and not CharacterSelect_AllowedToEnterWorld() then return end
CharacterSelect_EnterWorld() -- keep stock lock/confirmation checks and last-character hook
end
local function clickAccount(index,enter)
if not vault then return end
local a=vault.accounts[index];if not a then return end
parent.UI.AccountEditBox:SetText(a.account);parent.UI.PasswordEditBox:SetText(a.password)
-- Account button stops at character select; the last-character button enters the world.
intent=enter and {account=a.account,character=a.character or '',index=a.index or 0,expires=GetTime()+120} or nil
loginAccount=a.account
AccountLogin_Login() -- the original M priority gate remains authoritative
end
refresh=function()
if not panel or not vault then return end
page=math.max(1,math.min(page or 1,math.max(1,math.ceil(#vault.accounts/pageSize))))
panel.heading:SetText('Saved accounts '..page..'/'..math.max(1,math.ceil(#vault.accounts/pageSize)))
parent.UI.AccountEditBox:SetPassword(vault.streamer)
panel.lock:SetText(vault.locked and 'Unlock' or 'Lock')
panel.streamer:SetText(vault.streamer and 'Show names' or 'Streamer mode')
for n,r in ipairs(rows) do
local index=(page-1)*pageSize+n;local a=vault.accounts[index];r.index=index
if a then
r.login:SetText(vault.streamer and ('Account '..index) or (a.label~='' and a.label or a.account))
r.login:Show();r.edit:Show();r.up:Show();r.down:Show();r.remove:Show()
local c=a.character or ''
r.enter:SetText(vault.streamer and 'Last char' or c);r.enter:SetShown(c~='')
for _,b in ipairs({r.edit,r.up,r.down,r.remove}) do b:SetEnabled(not vault.locked) end
else for _,b in ipairs({r.login,r.enter,r.edit,r.up,r.down,r.remove}) do b:Hide() end end
end
panel.label:SetShown(not vault.locked and selected~=nil and not vault.streamer)
panel.apply:SetShown(not vault.locked and selected~=nil and not vault.streamer)
panel.save:SetEnabled(not vault.locked)
end
local function saveTyped(account,passwordBox)
local password=passwordBox:GetText()
if account=='' or password=='' then return true end
local current=read();if not current then note('Saved accounts could not be opened. Login can continue.');return true end
if current.locked then return true end
local _,a=find(current,account)
if not a then
if #current.accounts>=16 then note('Saved account list is full (16 accounts).');return true end
a={account=account,label='',character='',index=0};current.accounts[#current.accounts+1]=a
end
a.password=password
if not write(current) then note('This account could not be saved. Login can continue.') else vault=current;refresh() end
return true
end
local function beforeLogin(account,passwordBox)
loginAccount=account
if intent and intent.account:upper()~=account:upper() then intent=nil end
return saveTyped(account,passwordBox)
end
local function installHooks()
if installed then return end
if type(CharacterSelect_EnterWorld)~='function' or not CharacterSelect then return end
installed=true
-- Post-hooks only: never replace Blizzard globals (taint). enterLast is always
-- deferred by C_Timer.After(0), so clearing intent after the disconnect is in time.
hooksecurefunc('CharacterSelect_EnterWorld',rememberCharacter)
if type(C_Login.DisconnectFromServer)=='function' then
hooksecurefunc(C_Login,'DisconnectFromServer',function()intent=nil;loginAccount=nil end)
end
local watcher=CreateFrame('Frame')
watcher:RegisterEvent('CHARACTER_LIST_UPDATE')
watcher:SetScript('OnEvent',function(_,event)
C_Timer.After(0,enterLast)
end)
if CharacterSelect.HookScript then CharacterSelect:HookScript('OnShow',function()C_Timer.After(0,enterLast)end) end
-- These are user gestures; a saved-account click calls AccountLogin_Login directly.
for _,spec in ipairs({{parent.UI.LoginButton,'OnClick'},{parent.UI.AccountEditBox,'OnEnterPressed'},{parent.UI.PasswordEditBox,'OnEnterPressed'}}) do
local widget,event=spec[1],spec[2]
if widget then local original=widget:GetScript(event);widget:SetScript(event,function(...)intent=nil;if original then return original(...) end end) end
end
end
local function setup(owner)
parent=owner;intent=nil;loginAccount=nil;vault=read();selected=nil
if not vault then if panel then panel:Hide() end;note('Saved accounts could not be opened.');return end
if not panel then
panel=CreateFrame('Frame',nil,parent);panel:SetSize(420,390);panel:SetPoint('RIGHT',parent,'RIGHT',-24,0)
panel.heading=panel:CreateFontString(nil,'OVERLAY','GlueFontNormal');panel.heading:SetPoint('TOPLEFT',panel,'TOPLEFT',0,0)
for n=1,pageSize do
local r={};rows[n]=r;local y=-28-(n-1)*32
r.login=button(panel,'',0,y,124,function()clickAccount(r.index,false)end)
r.enter=button(panel,'',126,y,96,function()clickAccount(r.index,true)end)
r.edit=button(panel,'Label',224,y,56,function()selected=r.index;panel.label:SetText(vault.accounts[selected].label);refresh();panel.label:SetFocus()end)
r.up=button(panel,'Up',282,y,42,function()change(function(v)if not v.locked and r.index>1 then v.accounts[r.index],v.accounts[r.index-1]=v.accounts[r.index-1],v.accounts[r.index];selected=nil end end)end)
r.down=button(panel,'Dn',326,y,42,function()change(function(v)if not v.locked and r.index<#v.accounts then v.accounts[r.index],v.accounts[r.index+1]=v.accounts[r.index+1],v.accounts[r.index];selected=nil end end)end)
r.remove=button(panel,'X',370,y,36,function()change(function(v)if not v.locked then table.remove(v.accounts,r.index);selected=nil end end)end)
end
button(panel,'Previous',0,-292,96,function()page=math.max(1,(page or 1)-1);refresh()end)
button(panel,'Next',100,-292,96,function()page=math.min(math.max(1,math.ceil(#vault.accounts/pageSize)),(page or 1)+1);refresh()end)
panel.lock=button(panel,'Lock',200,-292,72,function()change(function(v)v.locked=not v.locked;selected=nil end)end)
panel.streamer=button(panel,'Streamer mode',276,-292,130,function()change(function(v)v.streamer=not v.streamer end)end)
panel.save=button(panel,'Save typed account',0,-324,196,function()saveTyped(parent.UI.AccountEditBox:GetText(),parent.UI.PasswordEditBox)end)
panel.label=CreateFrame('EditBox',nil,panel,'AccountLoginBackdropTemplate');panel.label:SetSize(280,28);panel.label:SetPoint('TOPLEFT',panel,'TOPLEFT',0,-356);panel.label:SetFontObject('GlueFontNormalSmall');panel.label:SetAutoFocus(false);panel.label:SetMaxLetters(64)
panel.apply=button(panel,'Save label',286,-356,120,function()change(function(v)if not v.locked and selected and v.accounts[selected] then v.accounts[selected].label=panel.label:GetText() end end)end)
parent:HookScript('OnHide',function()vault=nil end)
end
installHooks();refresh();panel:Show()
end
return {Setup=setup,BeforeLogin=beforeLogin}