419 lines
17 KiB
Lua
419 lines
17 KiB
Lua
-- Merges QuestieDB's Forever quest data into plain rows, without running its
|
|
-- generator. Each downloaded file runs in an empty environment: no io, os,
|
|
-- require, dofile or loadstring.
|
|
-- usage: lua extract.lua <qdb dir> <questie dir> <output file>
|
|
|
|
local qdb, questie, outPath = arg[1], arg[2], arg[3]
|
|
|
|
local function proxy(name)
|
|
local mt = {}
|
|
mt.__index = function(t, k)
|
|
local child = proxy(name .. "." .. tostring(k))
|
|
rawset(t, k, child)
|
|
return child
|
|
end
|
|
mt.__call = function() return proxy(name .. "()") end
|
|
mt.__add = function(a) return a end
|
|
mt.__sub = mt.__add
|
|
mt.__mul = mt.__add
|
|
mt.__unm = mt.__add
|
|
return setmetatable({}, mt)
|
|
end
|
|
|
|
local function sandbox(extra)
|
|
local env = {
|
|
pairs = pairs, ipairs = ipairs, type = type, tostring = tostring, tonumber = tonumber,
|
|
select = select, unpack = unpack, next = next, assert = assert, error = error,
|
|
rawget = rawget, rawset = rawset, setmetatable = setmetatable, getmetatable = getmetatable,
|
|
math = math, string = { format = string.format, find = string.find, match = string.match,
|
|
gsub = string.gsub, sub = string.sub, lower = string.lower, upper = string.upper,
|
|
byte = string.byte, char = string.char, rep = string.rep, len = string.len, gmatch = string.gmatch },
|
|
table = table,
|
|
}
|
|
for k, v in pairs(extra or {}) do env[k] = v end
|
|
return env
|
|
end
|
|
|
|
local function read(path)
|
|
local f = assert(io.open(path, "rb"), path)
|
|
local s = f:read("*a")
|
|
f:close()
|
|
assert(s:sub(1, 1) ~= "\27", "bytecode refused: " .. path)
|
|
return s
|
|
end
|
|
|
|
local function run(path, env, ...)
|
|
local chunk = assert(loadstring(read(path), "@" .. path))
|
|
setfenv(chunk, env)
|
|
return chunk(...)
|
|
end
|
|
|
|
-- Enums, read from QuestieDB's own definition files.
|
|
local Lib = { Enum = {} }
|
|
local function enumFile(name) run(qdb .. "/src/corrections/enum/" .. name .. ".lua", sandbox(), "QuestieDB", Lib) end
|
|
for _, name in ipairs({ "constants", "quests", "zones", "professions", "factions", "expansions" }) do
|
|
local ok, err = pcall(enumFile, name)
|
|
if not ok then io.stderr:write("enum ", name, ": ", tostring(err), "\n") end
|
|
end
|
|
local forever = assert(Lib.Enum.byExpansion and Lib.Enum.byExpansion.Forever, "no Forever enum")
|
|
forever.classKeys = forever.classKeys or assert(Lib.Enum.byExpansion.Classic.classKeys)
|
|
|
|
-- Raw rows and field indexes.
|
|
local modules = {}
|
|
local function module(name)
|
|
if not modules[name] then
|
|
modules[name] = proxy(name)
|
|
end
|
|
return modules[name]
|
|
end
|
|
local loader = {
|
|
CreateModule = function(_, name) rawset(modules, name, {}) return modules[name] end,
|
|
ImportModule = function(_, name) return module(name) end,
|
|
}
|
|
|
|
local rawQuestDB = {}
|
|
modules.QuestieDB = rawQuestDB
|
|
run(qdb .. "/data/Forever/foreverQuestDB.lua", sandbox({ QuestieLoader = loader }))
|
|
local realKeys = assert(rawQuestDB.questKeys)
|
|
local dataText = assert(rawQuestDB.questData)
|
|
assert(dataText:sub(1, 6) == "return")
|
|
local dataChunk = assert(loadstring(dataText, "=questData"))
|
|
setfenv(dataChunk, {})
|
|
local quests = dataChunk()
|
|
|
|
local ADD, REMOVE = 1000, 2000
|
|
local keyProxy = setmetatable({}, { __index = function(_, k)
|
|
local base, op = k:match("^(.-)_(add)$")
|
|
if not base then base, op = k:match("^(.-)_(remove)$") end
|
|
if base then
|
|
return assert(realKeys[base], k) + (op == "add" and ADD or REMOVE)
|
|
end
|
|
return assert(realKeys[k], k)
|
|
end })
|
|
|
|
modules = {
|
|
QuestieDB = { questKeys = keyProxy, raceKeys = forever.raceKeys, classKeys = forever.classKeys,
|
|
sortKeys = Lib.Enum.sortKeys, specialFlags = Lib.Enum.specialFlags, factionIDs = Lib.Enum.factionIDs },
|
|
ZoneDB = { zoneIDs = Lib.Enum.zoneIDs },
|
|
QuestieProfessions = { professionKeys = Lib.Enum.professionKeys or proxy("prof"),
|
|
specializationKeys = Lib.Enum.specializationKeys or proxy("spec") },
|
|
}
|
|
for k, v in pairs(modules) do
|
|
setmetatable(v, { __index = function(t, key) local p = proxy(k .. "." .. key); rawset(t, key, p); return p end })
|
|
end
|
|
|
|
local WANT = { "name", "questLevel", "requiredRaces", "requiredClasses", "preQuestGroup", "preQuestSingle",
|
|
"zoneOrSort", "nextQuestInChain", "breadcrumbForQuestId", "breadcrumbs", "objectives", "triggerEnd", "finishedBy", "specialFlags",
|
|
"startedBy", "requiredLevel", "exclusiveTo", "requiredSkill", "requiredMinRep", "requiredMaxRep", "requiredSpell",
|
|
"requiredSpecialization", "requiredRanks", "requiredMaxLevel", "availableUntilCompleted", "availableStartingWith",
|
|
"disabledByQuest", "parentQuest" }
|
|
local wanted = {}
|
|
for _, n in ipairs(WANT) do wanted[realKeys[n]] = true end
|
|
|
|
-- These hold one list per kind of thing, and an edit can skip a kind.
|
|
local grouped = { [realKeys.objectives] = true, [realKeys.finishedBy] = true, [realKeys.startedBy] = true }
|
|
|
|
local function contains(list, v) for _, x in ipairs(list) do if x == v then return true end end return false end
|
|
|
|
local changed = {}
|
|
local function apply(id, fixes)
|
|
local row = quests[id]
|
|
if not row then row = {}; quests[id] = row end
|
|
local removes, sets, adds = {}, {}, {}
|
|
for k, v in pairs(fixes) do
|
|
if k > REMOVE then removes[k - REMOVE] = v
|
|
elseif k > ADD then adds[k - ADD] = v
|
|
else sets[k] = v end
|
|
end
|
|
for k, v in pairs(removes) do
|
|
if grouped[k] and type(row[k]) == "table" and type(v) == "table" then
|
|
for g, list in pairs(v) do
|
|
if type(row[k][g]) == "table" then
|
|
local kept = {}
|
|
for _, x in ipairs(row[k][g]) do if not contains(list, x) then kept[#kept + 1] = x end end
|
|
row[k][g] = kept
|
|
end
|
|
end
|
|
elseif wanted[k] and type(row[k]) == "table" and type(v) == "table" then
|
|
local kept = {}
|
|
for _, x in ipairs(row[k]) do if not contains(v, x) then kept[#kept + 1] = x end end
|
|
row[k] = kept
|
|
end
|
|
end
|
|
for k, v in pairs(sets) do row[k] = v end
|
|
for k, v in pairs(adds) do
|
|
if grouped[k] and type(v) == "table" then
|
|
row[k] = type(row[k]) == "table" and row[k] or {}
|
|
for g, list in pairs(v) do
|
|
row[k][g] = row[k][g] or {}
|
|
for _, x in ipairs(list) do
|
|
if not contains(row[k][g], x) then table.insert(row[k][g], x) end
|
|
end
|
|
end
|
|
elseif wanted[k] and type(v) == "table" then
|
|
row[k] = type(row[k]) == "table" and row[k] or {}
|
|
for _, x in ipairs(v) do if not contains(row[k], x) then table.insert(row[k], x) end end
|
|
end
|
|
end
|
|
end
|
|
|
|
local LAYERS = {
|
|
{ "Forever/legacy/classicQuestFixes.lua", "QuestieQuestFixes" },
|
|
{ "Forever/generated/foreverBaseQuest.lua", "ForeverBaseQuest" },
|
|
{ "Forever/traces/foreverQuestTraces.lua", "ForeverQuestTraces" },
|
|
{ "Forever/foreverQuestFixes.lua", "ForeverQuestFixes" },
|
|
}
|
|
local layerLoader = {
|
|
CreateModule = function(_, name) local m = {}; rawset(modules, name, m); return m end,
|
|
ImportModule = function(_, name) return modules[name] or proxy(name) end,
|
|
}
|
|
local stubs = { QuestieLoader = layerLoader, UnitFactionGroup = function() return "Alliance" end,
|
|
Questie = proxy("Questie") }
|
|
for _, layer in ipairs(LAYERS) do
|
|
run(qdb .. "/src/corrections/" .. layer[1], sandbox(stubs))
|
|
local fixes = modules[layer[2]]:Load()
|
|
local n = 0
|
|
for id, row in pairs(fixes) do apply(id, row); n = n + 1 end
|
|
io.stderr:write(layer[2], ": ", n, " rows\n")
|
|
end
|
|
|
|
-- Quest tags. Questie treats Forever as Era content here.
|
|
local tags = {}
|
|
do
|
|
local private = {}
|
|
local Expansions = { Era = 1, Tbc = 2, Wotlk = 3, Cata = 4, MoP = 5, Current = 1 }
|
|
local tagLoader = { ImportModule = function(_, name)
|
|
if name == "QuestieDB" then return { private = private } end
|
|
if name == "Expansions" then return Expansions end
|
|
if name == "l10n" then return function(s) return s end end
|
|
return proxy(name)
|
|
end }
|
|
run(questie .. "/Database/Corrections/questTagInfoCorrections.lua", sandbox({ QuestieLoader = tagLoader, Questie = proxy("Questie") }))
|
|
private.InitializeQuestTagInfoCorrections()
|
|
for id, tag in pairs(private.questTagCorrections) do
|
|
if type(tag) == "table" and tag[1] == 81 then tags[id] = true end
|
|
end
|
|
end
|
|
|
|
-- Where quest givers stand: base rows, then the same correction layers as quests.
|
|
local function spawnsFor(dbFile, keysName, dataName, layers)
|
|
local raw = {}
|
|
run(dbFile, sandbox({ QuestieLoader = { ImportModule = function() return raw end, CreateModule = function() return raw end } }))
|
|
local real = assert(raw[keysName], keysName)
|
|
local chunk = assert(loadstring(assert(raw[dataName], dataName), "=" .. dataName))
|
|
setfenv(chunk, {})
|
|
local rows = chunk()
|
|
local spawnKey = assert(real.spawns)
|
|
local spawns = {}
|
|
for id, row in pairs(rows) do spawns[id] = row[spawnKey] end
|
|
|
|
modules.QuestieDB[keysName] = setmetatable({}, { __index = function(_, k)
|
|
local base, op = k:match("^(.-)_(add)$")
|
|
if not base then base, op = k:match("^(.-)_(remove)$") end
|
|
if base then return (real[base] or 900) + (op == "add" and ADD or REMOVE) end
|
|
return real[k] or 900
|
|
end })
|
|
|
|
for _, layer in ipairs(layers) do
|
|
run(qdb .. "/src/corrections/" .. layer[1], sandbox(stubs))
|
|
local n = 0
|
|
for id, row in pairs(modules[layer[2]]:Load()) do
|
|
n = n + 1
|
|
if type(row[spawnKey]) == "table" then spawns[id] = row[spawnKey] end
|
|
local add = row[spawnKey + ADD]
|
|
if type(add) == "table" then
|
|
spawns[id] = spawns[id] or {}
|
|
for zone, list in pairs(add) do
|
|
spawns[id][zone] = spawns[id][zone] or {}
|
|
for _, c in ipairs(list) do table.insert(spawns[id][zone], c) end
|
|
end
|
|
end
|
|
end
|
|
io.stderr:write(layer[2], ": ", n, " rows\n")
|
|
end
|
|
return spawns
|
|
end
|
|
|
|
modules.QuestieDB.npcFlags = forever.npcFlags
|
|
local npcSpawns = spawnsFor(qdb .. "/data/Forever/foreverNpcDB.lua", "npcKeys", "npcData", {
|
|
{ "Forever/legacy/classicNPCFixes.lua", "QuestieNPCFixes" },
|
|
{ "Forever/generated/foreverBaseNpc.lua", "ForeverBaseNpc" },
|
|
{ "Forever/traces/foreverNpcTraces.lua", "ForeverNpcTraces" },
|
|
{ "Forever/foreverNPCFixes.lua", "ForeverNpcFixes" },
|
|
})
|
|
local objectSpawns = spawnsFor(qdb .. "/data/Forever/foreverObjectDB.lua", "objectKeys", "objectData", {
|
|
{ "Forever/legacy/classicObjectFixes.lua", "QuestieObjectFixes" },
|
|
{ "Forever/generated/foreverBaseObject.lua", "ForeverBaseObject" },
|
|
{ "Forever/traces/foreverObjectTraces.lua", "ForeverObjectTraces" },
|
|
{ "Forever/foreverObjectFixes.lua", "ForeverObjectFixes" },
|
|
})
|
|
|
|
local areaToMap = {}
|
|
do
|
|
local zonePrivate = { private = {} }
|
|
run(qdb .. "/support/Forever/Zones/areaIdToUiMapId.lua", sandbox({ QuestieLoader = { ImportModule = function() return zonePrivate end } }))
|
|
for _, field in ipairs({ "areaIdToUiMapId", "areaIdToUiMapIdOverride" }) do
|
|
local text = zonePrivate.private[field]
|
|
if text then
|
|
local chunk = assert(loadstring(text, "=" .. field))
|
|
setfenv(chunk, {})
|
|
for area, map in pairs(chunk()) do areaToMap[area] = map end
|
|
end
|
|
end
|
|
end
|
|
|
|
-- Names and classes of quest objective items, for the shopping list.
|
|
local itemInfo = {}
|
|
do
|
|
local raw = {}
|
|
run(qdb .. "/data/Forever/foreverItemDB.lua", sandbox({ QuestieLoader = { ImportModule = function() return raw end, CreateModule = function() return raw end } }))
|
|
local real = assert(raw.itemKeys)
|
|
local chunk = assert(loadstring(assert(raw.itemData), "=itemData"))
|
|
setfenv(chunk, {})
|
|
for id, row in pairs(chunk()) do
|
|
itemInfo[id] = { name = row[real.name], class = row[real.class] }
|
|
end
|
|
|
|
local keys = setmetatable({}, { __index = function(_, k)
|
|
local base, op = k:match("^(.-)_(add)$")
|
|
if not base then base, op = k:match("^(.-)_(remove)$") end
|
|
if base then return (real[base] or 900) + (op == "add" and ADD or REMOVE) end
|
|
return real[k] or 900
|
|
end })
|
|
modules.QuestieDB.itemKeys = keys
|
|
|
|
for _, layer in ipairs({
|
|
{ "Forever/legacy/classicItemFixes.lua", "QuestieItemFixes" },
|
|
{ "Forever/generated/foreverBaseItem.lua", "ForeverBaseItem" },
|
|
{ "Forever/traces/foreverItemTraces.lua", "ForeverItemTraces" },
|
|
{ "Forever/foreverItemFixes.lua", "ForeverItemFixes" },
|
|
}) do
|
|
run(qdb .. "/src/corrections/" .. layer[1], sandbox(stubs))
|
|
local n = 0
|
|
for id, row in pairs(modules[layer[2]]:Load(keys)) do
|
|
n = n + 1
|
|
local name, class = row[real.name], row[real.class]
|
|
if type(name) == "string" or type(class) == "number" then
|
|
itemInfo[id] = itemInfo[id] or {}
|
|
if type(name) == "string" then itemInfo[id].name = name end
|
|
if type(class) == "number" then itemInfo[id].class = class end
|
|
end
|
|
end
|
|
io.stderr:write(layer[2], ": ", n, " rows\n")
|
|
end
|
|
end
|
|
|
|
-- Dungeon list is parsed as text, not run.
|
|
local zoneNames, allNames = {}, {}
|
|
for line in read(qdb .. "/support/Forever/Zones/dungeons.lua"):gmatch("[^\n]+") do
|
|
local id, name, rest = line:match('^%s*%[(%d+)%]%s*=%s*{"([^"]+)",(.*)')
|
|
if id then
|
|
allNames[#allNames + 1] = name
|
|
zoneNames[tonumber(id)] = name
|
|
local alts = rest:match("^{([%d,%s]+)}")
|
|
if alts then for a in alts:gmatch("%d+") do zoneNames[tonumber(a)] = name end end
|
|
end
|
|
end
|
|
table.sort(allNames)
|
|
|
|
local q = {}
|
|
for id, row in pairs(quests) do
|
|
local r = {}
|
|
for _, n in ipairs(WANT) do r[n] = row[realKeys[n]] end
|
|
q[id] = r
|
|
end
|
|
|
|
local function ser(v)
|
|
if type(v) == "table" then
|
|
local parts, n = {}, 0
|
|
for k in pairs(v) do if type(k) == "number" and k > n then n = k end end
|
|
for i = 1, n do parts[i] = ser(v[i]) end
|
|
return "{" .. table.concat(parts, ",") .. "}"
|
|
elseif type(v) == "string" then return string.format("%q", v)
|
|
elseif v == nil then return "nil"
|
|
else return tostring(v) end
|
|
end
|
|
|
|
local out = assert(io.open(outPath, "w"))
|
|
out:write("return {quests={\n")
|
|
local ids = {}
|
|
for id in pairs(q) do ids[#ids + 1] = id end
|
|
table.sort(ids)
|
|
for _, id in ipairs(ids) do
|
|
local r = q[id]
|
|
out:write("[", id, "]={")
|
|
-- A quest handed in at an object means going there, usually inside the dungeon.
|
|
local endsAtObject = type(r.finishedBy) == "table" and type(r.finishedBy[2]) == "table" and next(r.finishedBy[2]) ~= nil
|
|
local hasWork = r.triggerEnd ~= nil or endsAtObject
|
|
if type(r.objectives) == "table" then
|
|
for _, group in pairs(r.objectives) do
|
|
if type(group) == "table" and next(group) ~= nil then hasWork = true end
|
|
end
|
|
end
|
|
if hasWork then out:write("hasWork=true,") end
|
|
for _, n in ipairs(WANT) do
|
|
if r[n] ~= nil and n ~= "objectives" and n ~= "triggerEnd" and n ~= "finishedBy" then out:write(n, "=", ser(r[n]), ",") end
|
|
end
|
|
out:write("},\n")
|
|
end
|
|
out:write("},\ntags={")
|
|
for id in pairs(tags) do out:write("[", id, "]=true,") end
|
|
out:write("},\nzoneNames={")
|
|
for id, name in pairs(zoneNames) do out:write("[", id, "]=", string.format("%q", name), ",") end
|
|
out:write("},\nallDungeons=", ser(allNames), ",\n")
|
|
out:write("ALL_ALLIANCE=", string.format("%.0f", forever.raceKeys.ALL_ALLIANCE), ",ALL_HORDE=", string.format("%.0f", forever.raceKeys.ALL_HORDE), ",\n")
|
|
out:write("classKeys=", "{")
|
|
for k, v in pairs(forever.classKeys) do out:write(k, "=", string.format("%.0f", v), ",") end
|
|
out:write("}")
|
|
local function writeSpawns(label, source, group)
|
|
local wantedIds = {}
|
|
for _, r in pairs(q) do
|
|
local list = type(r.startedBy) == "table" and r.startedBy[group]
|
|
if type(list) == "table" then
|
|
for _, id in ipairs(list) do wantedIds[id] = true end
|
|
end
|
|
end
|
|
out:write(",\n", label, "={\n")
|
|
for id in pairs(wantedIds) do
|
|
local zones = source[id]
|
|
if type(zones) == "table" then
|
|
out:write("[", id, "]={")
|
|
for zone, list in pairs(zones) do
|
|
if type(zone) == "number" and type(list) == "table" then
|
|
out:write("[", zone, "]=", ser(list), ",")
|
|
end
|
|
end
|
|
out:write("},\n")
|
|
end
|
|
end
|
|
out:write("}")
|
|
end
|
|
writeSpawns("npcs", npcSpawns, 1)
|
|
writeSpawns("objects", objectSpawns, 2)
|
|
do
|
|
local seen = {}
|
|
out:write(",\nobjectiveItems={\n")
|
|
for _, r in pairs(q) do
|
|
local group = type(r.objectives) == "table" and r.objectives[3]
|
|
if type(group) == "table" then
|
|
for _, objective in ipairs(group) do
|
|
local id = type(objective) == "table" and objective[1]
|
|
local info = id and itemInfo[id]
|
|
if info and info.name and not seen[id] then
|
|
seen[id] = true
|
|
out:write("[", id, "]={name=", string.format("%q", info.name), ",class=", tostring(info.class or 0), "},\n")
|
|
end
|
|
end
|
|
end
|
|
end
|
|
out:write("}")
|
|
end
|
|
out:write(",\nareaToMap={")
|
|
for area, map in pairs(areaToMap) do out:write("[", area, "]=", map, ",") end
|
|
out:write("},\nraceBits={")
|
|
for race, bit in pairs(Lib.Enum.raceMaskById) do out:write("[", race, "]=", string.format("%.0f", bit), ",") end
|
|
out:write("}\n}\n")
|
|
out:close()
|
|
io.stderr:write("quests: ", #ids, ", dungeon tags: ")
|
|
local c = 0; for _ in pairs(tags) do c = c + 1 end
|
|
io.stderr:write(c, "\n")
|