Files
WoW-Hybrid-gateway/ops/source_appearance_groups.lua

100 lines
5.3 KiB
Lua

-- Correlate original native controls through the maintained Source profile.
-- PROFILE_DATA
local GW_SourceAppearanceSyncing = false;
local function GW_SourceAppearanceOptions(data)
local options, order = {}, 0;
for _, category in ipairs(data or {}) do
for _, option in ipairs(category.options or {}) do
local choices = {};
for _, choice in ipairs(option.choices or {}) do choices[choice.id] = true; end
local current = option.choices and option.choices[option.currentChoiceIndex or 0];
order = order + 1;
options[option.id] = {node = option, available = choices, current = current and current.id, order = order};
end
end
return options;
end
GW_SourceCorrelateCustomizations = function(data, preferred, firstGroup, validateOnly)
if not GW_SourceSingleNameCreation() or type(data) ~= "table" or GW_SourceAppearanceSyncing then return data, false; end
local key = (C_CharacterCreation.GetSelectedRace() or 0) * 2 + (C_CharacterCreation.GetSelectedSex() or 0);
local profile = GW_SourceAppearanceGroups[key];
if not profile then return data, false, "appearance-profile"; end
local fields, options, wanted, allowed = profile[1], GW_SourceAppearanceOptions(data), {}, {};
for group = firstGroup or 1, 3 do
local relevant, covered = {}, {};
for option, field in pairs(fields) do
if options[option] and (field <= 2 and 1 or field <= 4 and 2 or 3) == group then
relevant[#relevant + 1] = option; covered[field] = true;
end
end
table.sort(relevant, function(a,b) return options[a].order < options[b].order; end);
local axes = group == 1 and {1,2} or group == 2 and {3,4} or {5};
for _, field in ipairs(axes) do if not covered[field] then return data, false, "appearance-incomplete"; end end
local candidates, signatures = {}, {};
for _, row in ipairs(profile[group + 1]) do
local choices = {};
for index = 1, #row[2], 2 do choices[row[2][index]] = row[2][index + 1]; end
local valid, score, signature, penalty = true, 0, {}, {};
for _, option in ipairs(relevant) do
local choice = choices[option];
if not choice or not options[option].available[choice] then valid = false; break; end
signature[#signature + 1] = tostring(choice);
penalty[#penalty + 1] = choice == options[option].current and "0" or "1";
if choice ~= options[option].current then score = score + 1; end
end
if valid then
local identity = table.concat(signature, ":");
signatures[identity] = (signatures[identity] or 0) + 1;
candidates[#candidates + 1] = {choices = choices, identity = identity, score = score, penalty = table.concat(penalty)};
end
end
local selected;
for _, candidate in ipairs(candidates) do
if signatures[candidate.identity] == 1 then
for _, option in ipairs(relevant) do
allowed[option] = allowed[option] or {}; allowed[option][candidate.choices[option]] = true;
end
local fits = not preferred or not fields[preferred[1]] or
(fields[preferred[1]] <= 2 and 1 or fields[preferred[1]] <= 4 and 2 or 3) ~= group or
candidate.choices[preferred[1]] == preferred[2];
if fits and (not selected or candidate.penalty < selected.penalty) then selected = candidate; end
end
end
if not selected then return data, false, "appearance-conflict"; end
for _, option in ipairs(relevant) do wanted[option] = selected.choices[option]; end
end
if validateOnly then
for option, choice in pairs(wanted) do
if options[option].current ~= choice then return data, false, "appearance-conflict"; end
end
return data, false;
end
local changed, ordered = false, {};
for option in pairs(wanted) do ordered[#ordered + 1] = option; end; table.sort(ordered);
GW_SourceAppearanceSyncing = true;
for _, option in ipairs(ordered) do
if wanted[option] ~= options[option].current then
local ok = pcall(C_CharacterCreation.SetCustomizationChoice, option, wanted[option]);
if not ok then GW_SourceAppearanceSyncing = false; return data, changed, "appearance-update"; end
changed = true;
end
end
GW_SourceAppearanceSyncing = false;
if changed then data = C_CharacterCreation.GetAvailableCustomizations(); options = GW_SourceAppearanceOptions(data); end
-- Confirm the original setters retained the coherent projection before Finish.
for option, choice in pairs(wanted) do
if not options[option] or options[option].current ~= choice then return data, changed, "appearance-update"; end
end
for option, choices in pairs(allowed) do
local original, kept, selected = options[option].node, {}, nil;
for _, choice in ipairs(original.choices or {}) do
if choices[choice.id] then
kept[#kept + 1] = choice;
if choice.id == wanted[option] then selected = #kept; end
end
end
original.choices, original.currentChoiceIndex = kept, selected;
end
return data, changed;
end