-- 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 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", "specialFlags" } local wanted = {} for _, n in ipairs(WANT) do wanted[realKeys[n]] = true end 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 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 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 -- 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, "]={") for _, n in ipairs(WANT) do if r[n] ~= nil and n ~= "objectives" 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("}}\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")