143 lines
5.7 KiB
Lua
143 lines
5.7 KiB
Lua
-- File-local data adapter for the original RealmList controls.
|
|
-- Screen selection, auth dialogs and Source account/realm changes stay outside it.
|
|
return function(source, native, enabled, preference)
|
|
local adapter = {};
|
|
local active, requested, generation = false, false, nil;
|
|
local autoAttemptGeneration;
|
|
|
|
-- Match a remembered identity against the current authenticated rows,
|
|
-- never against a saved list index. Percent encoding keeps the CVar value
|
|
-- printable without relying on the realm name's punctuation.
|
|
local function identity(info)
|
|
if not info or type(info.name) ~= "string" or type(info.sourceAddress) ~= "string"
|
|
or type(info.sourceWireID) ~= "number" then return nil; end
|
|
local function escape(value)
|
|
return (value:gsub("[^%w_.-]", function(char)
|
|
return string.format("%%%02X", string.byte(char));
|
|
end));
|
|
end
|
|
local value = "v1|" .. escape(info.name) .. "|" .. escape(info.sourceAddress)
|
|
.. "|" .. tostring(info.sourceWireID);
|
|
return #value <= 255 and value or nil;
|
|
end
|
|
|
|
local function readPreference()
|
|
if type(preference) ~= "table" or type(preference.Read) ~= "function" then return nil; end
|
|
local ok, value = pcall(preference.Read);
|
|
return ok and type(value) == "string" and #value <= 255 and value or nil;
|
|
end
|
|
|
|
local function remember(info)
|
|
local value = identity(info);
|
|
if value and type(preference) == "table" and type(preference.Write) == "function" then
|
|
pcall(preference.Write, value);
|
|
end
|
|
end
|
|
|
|
function adapter.Refresh()
|
|
if enabled ~= true then return false, false; end
|
|
source.Refresh();
|
|
local valid = source.GetStatus();
|
|
local status = source.GetLoginStatus();
|
|
local pending = valid and status and status.state == 5
|
|
and status.selected_index == -1 and status.transport_prepared == 0;
|
|
if not active and pending then active = true; end
|
|
if status then
|
|
if generation ~= status.generation then
|
|
requested = false;
|
|
autoAttemptGeneration = nil;
|
|
end
|
|
generation = status.generation;
|
|
end
|
|
if pending and valid and not requested and autoAttemptGeneration ~= generation then
|
|
autoAttemptGeneration = generation;
|
|
local saved = readPreference();
|
|
if saved then
|
|
local match, count;
|
|
count = 0;
|
|
for _, category in ipairs(source.GetAvailableCategories()) do
|
|
for _, index in ipairs(source.GetRealmsInCategory(category)) do
|
|
local row = source.GetRealmInfo(index);
|
|
if row and row.populationState ~= "OFFLINE" and identity(row) == saved then
|
|
match, count = index, count + 1;
|
|
end
|
|
end
|
|
end
|
|
if count == 1 then
|
|
local ok = source.ConnectToRealm(match);
|
|
if ok then requested = true; end
|
|
end
|
|
end
|
|
end
|
|
-- Once this view displayed genuine Source rows, loss of that proof
|
|
-- leaves an empty/held Source view, never another account's native cache.
|
|
return active, valid;
|
|
end
|
|
|
|
function adapter.IsSource() return active; end
|
|
|
|
function adapter.CanSelect()
|
|
local valid = source.GetStatus();
|
|
local status = source.GetLoginStatus();
|
|
return active and valid and not requested and status and status.state == 5
|
|
and status.selected_index == -1 and status.transport_prepared == 0;
|
|
end
|
|
|
|
function adapter.GetRealmInfo(key)
|
|
if not active then return native.GetRealmInfo(key); end
|
|
local original = source.GetRealmInfo(key);
|
|
local info;
|
|
if original then
|
|
info = {};
|
|
for name, value in pairs(original) do info[name] = value; end
|
|
end
|
|
if info then
|
|
info.sourceSelectable = not not adapter.CanSelect()
|
|
and info.populationState ~= "OFFLINE";
|
|
end
|
|
return info;
|
|
end
|
|
|
|
function adapter.ConnectToRealm(key)
|
|
if not active then return native.ConnectToRealm(key); end
|
|
if not adapter.CanSelect() then return false, "selection-not-pending"; end
|
|
local info = source.GetRealmInfo(key);
|
|
local ok, reason = source.ConnectToRealm(key);
|
|
if ok then
|
|
requested = true;
|
|
remember(info);
|
|
end
|
|
return ok, reason;
|
|
end
|
|
|
|
function adapter.ComparePopulation(first, second)
|
|
if not active then return nil; end
|
|
local a, b = source.GetRealmInfo(first), source.GetRealmInfo(second);
|
|
if not a or not b then return 0; end
|
|
if a.populationState == "OFFLINE" then
|
|
return b.populationState == "OFFLINE" and 0 or 1;
|
|
elseif b.populationState == "OFFLINE" then
|
|
return -1;
|
|
end
|
|
-- This is the advertised Source scalar, not a native population enum.
|
|
return tonumber(a.sourcePopulation) - tonumber(b.sourcePopulation);
|
|
end
|
|
|
|
local methods = {"IsRealmListComplete", "GetAvailableCategories", "GetCategoryInfo",
|
|
"GetRealmsInCategory", "ClearRealmList"};
|
|
for _, name in ipairs(methods) do
|
|
local method = name;
|
|
adapter[method] = function(...)
|
|
if active then return source[method](...); end
|
|
return native[method](...);
|
|
end;
|
|
end
|
|
|
|
function adapter.RequestChangeRealmList(...)
|
|
if active then return source.RequestChangeRealmList(...); end
|
|
return native.RequestChangeRealmList(...);
|
|
end
|
|
|
|
return adapter;
|
|
end
|