fix the tooltips not going away
[QuestHelper.git] / db_get.lua
blobc54a33f152191f603d10fab8c7ad1c76ff7a3988
1 QuestHelper_File["db_get.lua"] = "Development Version"
2 QuestHelper_Loadtime["db_get.lua"] = GetTime()
4 -- yoink
5 --[[
6 local QHDB_temp = QHDB
7 QHDB = nil
8 local QHDB = QHDB_temp]]
9 QuestHelper: Assert(#QHDB == 4)
11 local weak_v = { __mode = 'v' }
12 local weak_k = { __mode = 'k' }
14 local cache = {}
16 local frequencies = setmetatable({}, weak_k)
18 -- guhhh just want this to work
19 local freq_group = setmetatable({}, weak_k)
20 local freq_id = setmetatable({}, weak_k)
22 local function DBC_Get(group, id)
23 if not cache[group] then return end
24 return cache[group][id]
25 end
27 local function DBC_Put(group, id, item)
28 if not cache[group] then cache[group] = setmetatable({}, weak_v) end
29 QuestHelper: Assert(not cache[group][id])
30 cache[group][id] = item
32 --DB_how_many_are_used()
33 end
35 local function mark(tab, tomark)
36 for k, v in pairs(tab) do
37 if type(v) == "table" then
38 mark(v, tomark)
39 end
40 end
41 tab.__owner = tomark
42 end
44 local initted = false
45 function DB_Init()
46 QuestHelper: Assert(not initted)
47 for _, db in ipairs(QHDB) do
48 for _, v in pairs(db) do
49 --print("db", not not v.__dictionary, not not v.__tokens)
50 if v.__dictionary and v.__tokens then
51 local redictix = v.__dictionary
52 if not redictix:find("\"") then redictix = redictix .. "\"" end
53 if not redictix:find(",") then redictix = redictix .. "," end
54 if not redictix:find("\\") then redictix = redictix .. "\\" end
55 local tokens = loadstring("return {" .. QH_LZW_Decompress_Dicts_Arghhacky(v.__tokens, redictix) .. "}")()
56 QuestHelper: Assert(tokens)
58 local _, _, prep = QH_LZW_Prepare_Arghhacky(v.__dictionary, tokens)
59 QuestHelper: Assert(prep)
61 QuestHelper: Assert(type(prep) == "table")
63 v.__tokens = prep
64 end
65 end
66 end
67 initted = true
68 end
70 function DB_Ready()
71 return initted
72 end
74 function DB_GetItem(group, id, silent, register)
75 QuestHelper: Assert(initted)
77 QuestHelper: Assert(group, string.format("%s %s", tostring(group), tostring(id)))
78 QuestHelper: Assert(id, string.format("%s %s", tostring(group), tostring(id)))
79 local ite = DBC_Get(group, id)
80 if not ite then
81 if type(id) == "string" then QuestHelper: Assert(not id:match("__.*")) end
83 --QuestHelper:TextOut(string.format("%s %d", group, id))
85 for _, db in ipairs(QHDB) do
86 --print(db, db[group], db[group] and db[group][id], type(group), type(id))
87 if db[group] and db[group][id] then
88 if not ite then ite = QuestHelper:CreateTable("db") end
90 local srctab
92 if type(db[group][id]) == "string" then
93 QuestHelper: Assert(db[group].__tokens == nil or type(db[group].__tokens) == "table")
94 srctab = loadstring("return {" .. QH_LZW_Decompress_Dicts_Prepared_Arghhacky(db[group][id], db[group].__dictionary, nil, db[group].__tokens) .. "}")()
95 elseif type(db[group][id]) == "table" then
96 srctab = db[group][id]
97 else
98 QuestHelper: Assert()
99 end
101 for k, v in pairs(srctab) do
102 QuestHelper: Assert(not ite[k])
103 ite[k] = v
107 --print("dbe", ite)
109 if ite then
110 mark(ite, ite)
112 DBC_Put(group, id, ite)
114 freq_group[ite] = group
115 freq_id[ite] = id
116 else
117 if not silent then
118 QuestHelper:TextOut(string.format("Tried to get %s/%s, failed", tostring(group), tostring(id)))
123 if ite then
124 frequencies[ite] = (frequencies[ite] or 0) + (register and 1 or 1000000000) -- effectively infinity
127 return ite
130 local function incinerate(ite, crunchy)
131 if not crunchy[ite] then
132 crunchy[ite] = true
134 for k, v in pairs(ite) do
135 if type(k) == "table" then incinerate(k, crunchy) end
136 if type(v) == "table" then incinerate(v, crunchy) end
141 function DB_ReleaseItem(ite)
142 QuestHelper: Assert(ite)
143 frequencies[ite] = frequencies[ite] - 1
145 if frequencies[ite] == 0 then
146 --print("incinerating", freq_group[ite], freq_id[ite])
147 cache[freq_group[ite]][freq_id[ite]] = nil
148 freq_group[ite] = nil
149 freq_id[ite] = nil
151 local incin = QuestHelper:CreateTable("incinerate")
152 incinerate(ite, incin)
153 for k, _ in pairs(incin) do
154 QuestHelper:ReleaseTable(k)
155 end -- burn baby burn
156 QuestHelper:ReleaseTable(incin)
160 function DB_ListItems(group)
161 local tab = {}
162 for _, db in ipairs(QHDB) do
163 if db[group] then for k, _ in pairs(db[group]) do
164 if type(k) ~= "string" or not k:match("__.*") then
165 tab[k] = true
167 end end
170 local rv = {}
171 for k, _ in pairs(tab) do
172 table.insert(rv, k)
175 return rv
178 function DB_how_many_are_used()
179 local count = 0
180 for k, v in pairs(cache) do
181 for k2, v2 in pairs(v) do
182 count = count + 1
185 print(count)
188 function DB_DumpItems()
189 local dt = {}
190 for k, v in pairs(freq_group) do
191 dt[string.format("%s/%s", freq_group[k], tostring(freq_id[k]))] = true
193 return dt