Files
WoW-Hybrid-gateway/compat/glue/source_realm_provider.lua
2026-11-23 03:28:32 +02:00

219 lines
9.6 KiB
Lua

-- Source-backed data adapter for the original realm-list view.
-- This factory does not register APIs, show a frame, or authenticate an account.
return function(backend)
assert(type(backend) == "table" and type(backend.GetSnapshot) == "function"
and type(backend.RequestRealm) == "function", "Owned Source provider required");
local provider = {};
local snapshot, status, realms, infos;
local category = "octo-source"; -- An owned presentation group, not a native category ID.
local failure = "not-refreshed";
local function integer(value, minimum, maximum)
return type(value) == "number" and value == math.floor(value)
and value >= minimum and value <= maximum;
end
local function text(value, maximum)
return type(value) == "string" and #value > 0 and #value <= maximum
and not value:find("[%z\1-\31\127]");
end
local function populationText(row)
if type(row.population_text) == "string" and row.population_bits == nil then
return row.population_text;
end
if row.population_text ~= nil or not integer(row.population_bits, -2147483648, 4294967295) then
return nil;
end
local bits = row.population_bits;
if bits < 0 then bits = bits + 4294967296; end
local sign = bits >= 2147483648 and -1 or 1;
local exponent = math.floor(bits / 8388608) % 256;
local fraction = bits % 8388608;
if exponent == 255 then return nil; end
local value;
if exponent == 0 then value = sign * fraction * 2^-149;
else value = sign * (1 + fraction / 8388608) * 2^(exponent - 127); end
return string.format("%.9g", value);
end
local function publicHeader(value)
if type(value) ~= "table" or value.abi ~= 1 or value.size ~= 64
or not integer(value.generation, 0, 4294967295)
or (value.endpoint_kind ~= 0 and value.endpoint_kind ~= 1) then
return nil, "snapshot-contract";
end
local result = {abi=1, size=64, generation=value.generation, endpoint_kind=value.endpoint_kind};
for _, key in ipairs({"state", "error", "auth_result", "discovery_result",
"server_proof_verified", "realm_count", "selected_index", "transport_prepared",
"cancel", "genuine_discovery"}) do
if not integer(value[key], -2147483648, 2147483647) then return nil, "header-contract"; end
result[key] = value[key];
end
if not integer(value.auth_port, 0, 65535) or not integer(value.copied_realms, 0, 64) then
return nil, "header-contract";
end
-- Optional opaque SHA-256 of the authenticated game account, supplied
-- by a generation-bound snapshot. Older numeric providers omit it.
local key = value.account_preference_key;
if type(key) == "string" and #key == 64 and key:match("^[0-9a-f]+$") then
result.account_preference_key = key;
end
result.auth_port, result.copied_realms = value.auth_port, value.copied_realms;
return result;
end
local function validate(value, header)
local reason;
if not header then header, reason = publicHeader(value); end
if not header then return nil, reason; end
local portBound = value.auth_port == 3724 or (value.endpoint_kind == 1
and value.source_bound_profile == 5 and value.source_bound_auth_port == value.auth_port
and value.source_bound_generation == value.generation and integer(value.auth_port, 1, 65535));
if value.genuine_discovery ~= 1 or value.server_proof_verified ~= 1
or value.auth_result ~= 0 or value.discovery_result ~= 0
or value.error ~= 0 or value.cancel ~= 0 or not portBound
or value.generation == 0 or (value.state ~= 2 and value.state ~= 5) then
return nil, "discovery-not-proven";
end
if not integer(value.realm_count, 1, 64) or value.copied_realms ~= value.realm_count
or not integer(value.selected_index, -1, value.realm_count - 1) then
return nil, "incomplete-discovery";
end
local indices, records = {}, {};
for index = 0, value.realm_count - 1 do
local row = value["r"..index];
if type(row) ~= "table" or not text(row.name, 127) or not text(row.address, 255)
or not integer(row.type, 0, 255) or not integer(row.flags, 0, 255)
or not integer(row.characters, 0, 255) or not integer(row.category, 0, 255)
or not integer(row.wire_id, 0, 255) then
return nil, "realm-contract";
end
local population_text = populationText(row);
local population = population_text and tonumber(population_text);
if not population or population ~= population or math.abs(population) == math.huge then
return nil, "population-contract";
end
if row.type ~= 0 and row.type ~= 1 and row.type ~= 6 and row.type ~= 8 then
return nil, "realm-type-unobserved";
end
-- Preserve advertised Source values. No native category/population enum is inferred.
local offline = math.floor(row.flags / 2) % 2 == 1;
records[index] = {
name = row.name, numCharacters = row.characters,
isPvP = row.type == 1 or row.type == 8,
isRP = row.type == 6 or row.type == 8,
populationState = offline and "OFFLINE" or "SOURCE",
sourceIndex = index, sourceType = row.type, sourceFlags = row.flags,
sourceCategory = row.category, sourceWireID = row.wire_id,
sourcePopulation = population_text,
sourceCompatibilityUnobserved = true,
sourceAddress = row.address,
};
indices[#indices + 1] = index;
end
return {header = header, indices = indices, records = records};
end
function provider.Refresh()
local ok, value = pcall(backend.GetSnapshot);
local parsed, reason;
status = nil;
if ok then status, reason = publicHeader(value); else reason = "snapshot-unavailable"; end
if status then parsed, reason = validate(value, status); end
if not parsed then
snapshot, realms, infos = nil, nil, nil;
failure = reason;
return false, reason;
end
snapshot, realms, infos = parsed.header, parsed.indices, parsed.records;
failure = nil;
return true;
end
-- Retire the waiting Source login (user cancel); true when accepted.
function provider.Cancel()
if type(backend.Cancel) ~= "function" then return false; end
local ok, result = pcall(backend.Cancel);
return ok and result == 0;
end
function provider.GetStatus()
return snapshot ~= nil, failure, status and status.generation;
end
function provider.GetLoginStatus()
if not status then return nil; end
local result = {};
for key, value in pairs(status) do result[key] = value; end
return result;
end
function provider.IsRealmListComplete()
return snapshot ~= nil;
end
function provider.GetAvailableCategories()
return snapshot and {category} or {};
end
function provider.GetCategoryInfo(key)
if snapshot and key == category then return "Octo realms", false, false; end
end
function provider.GetRealmsInCategory(key)
local result = {};
if snapshot and key == category then
for _, index in ipairs(realms) do result[#result + 1] = index; end
end
return result;
end
function provider.GetRealmInfo(index)
local row = infos and infos[index];
if not row then return nil; end
local result = {};
for key, value in pairs(row) do result[key] = value; end
return result;
end
function provider.ConnectToRealm(index)
local row = infos and infos[index];
if not row or row.populationState == "OFFLINE" or snapshot.state ~= 5
or snapshot.selected_index ~= -1 or snapshot.transport_prepared ~= 0 then
return false, "selection-not-pending";
end
local generation = snapshot.generation;
local ok, current = pcall(backend.GetSnapshot);
local parsed = ok and validate(current);
local fresh = parsed and parsed.records[index];
if not fresh or current.generation ~= generation or current.auth_port ~= snapshot.auth_port or current.state ~= 5
or current.selected_index ~= -1 or current.transport_prepared ~= 0
or fresh.name ~= row.name or fresh.sourceAddress ~= row.sourceAddress
or fresh.sourceWireID ~= row.sourceWireID or fresh.sourceType ~= row.sourceType
or fresh.sourceFlags ~= row.sourceFlags then
return false, "selection-stale";
end
local requested, result = pcall(backend.RequestRealm, index, generation);
if not requested or result ~= 0 then return false, "selection-held"; end
-- Queue admission is not server connection, authentication, or character readiness.
return true, "requested";
end
function provider.ClearRealmList()
snapshot, status, realms, infos = nil, nil, nil, nil;
failure = "view-cleared";
end
function provider.RequestChangeRealmList()
return false, "account-realm-transition-unverified";
end
function provider.GetPopulationLabel(info)
return info and info.sourcePopulation;
end
return provider;
end