1 QuestHelper_File
["db_get.lua"] = "Development Version"
2 QuestHelper_Loadtime
["db_get.lua"] = GetTime()
8 local QHDB = QHDB_temp]]
9 QuestHelper
: Assert(#QHDB
== 4)
11 local weak_v
= { __mode
= 'v' }
12 local weak_k
= { __mode
= 'k' }
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
]
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()
35 local function mark(tab
, tomark
)
36 for k
, v
in pairs(tab
) do
37 if type(v
) == "table" then
44 local function read_adaptint(data
, offset
)
48 local v
= strbyte(data
, offset
)
49 QuestHelper
: Assert(v
, string.format("%d %d", #data
, offset
))
50 stx
= stx
+ acu
* math
.floor(v
/ 2)
53 if mod(v
, 2) == 0 then break end
58 local function search_index(index
, data
, item
)
61 Itemid (0 for endnode)
67 assert(index
and type(index
) == "string")
68 assert(data
and type(data
) == "string")
71 local idx
, ofs
, len
, rlink
72 idx
, cofs
= read_adaptint(index
, cofs
)
73 if idx
== 0 then return end
74 ofs
, cofs
= read_adaptint(index
, cofs
)
75 len
, cofs
= read_adaptint(index
, cofs
)
76 rlink
, cofs
= read_adaptint(index
, cofs
)
79 return strsub(data
, ofs
, ofs
+ len
)
82 if idx
< item
then cofs
= cofs
+ rlink
end
88 QuestHelper
: Assert(not initted
)
89 for _
, db
in ipairs(QHDB
) do
90 for _
, v
in pairs(db
) do
91 --print("db", not not v.__dictionary, not not v.__tokens)
92 if v
.__dictionary
and v
.__tokens
then
93 local redictix
= v
.__dictionary
94 if not redictix
:find("\"") then redictix
= redictix
.. "\"" end
95 if not redictix
:find(",") then redictix
= redictix
.. "," end
96 if not redictix
:find("\\") then redictix
= redictix
.. "\\" end
97 local tokens
= loadstring("return {" .. QH_LZW_Decompress_Dicts_Arghhacky(v
.__tokens
, redictix
) .. "}")()
98 QuestHelper
: Assert(tokens
)
100 local _
, _
, prep
= QH_LZW_Prepare_Arghhacky(v
.__dictionary
, tokens
)
101 QuestHelper
: Assert(prep
)
103 QuestHelper
: Assert(type(prep
) == "table")
116 function DB_GetItem(group
, id
, silent
, register
)
117 QuestHelper
: Assert(initted
)
119 QuestHelper
: Assert(group
, string.format("%s %s", tostring(group
), tostring(id
)))
120 QuestHelper
: Assert(id
, string.format("%s %s", tostring(group
), tostring(id
)))
121 local ite
= DBC_Get(group
, id
)
123 if type(id
) == "string" then QuestHelper
: Assert(not id
:match("__.*")) end
125 --QuestHelper:TextOut(string.format("%s %d", group, id))
127 for _
, db
in ipairs(QHDB
) do
128 --print(db, db[group], db[group] and db[group][id], type(group), type(id))
130 if not ite
then ite
= QuestHelper
:CreateTable("db") end
135 if db
[group
][id
] then
139 --print(not dat, type(id), id, not not db[group].__serialize_index, not not db[group].__serialize_index)
140 if not dat
and type(id
) == "number" and id
> 0 and db
[group
].__serialize_index
and db
[group
].__serialize_data
then
141 dat
= search_index(db
[group
].__serialize_index
, db
[group
].__serialize_data
, id
)
145 if type(dat
) == "string" then
146 QuestHelper
: Assert(db
[group
].__tokens
== nil or type(db
[group
].__tokens
) == "table")
147 srctab
= loadstring("return {" .. QH_LZW_Decompress_Dicts_Prepared_Arghhacky(dat
, db
[group
].__dictionary
, nil, db
[group
].__tokens
) .. "}")()
148 elseif type(dat
) == "table" then
151 QuestHelper
: Assert()
154 for k
, v
in pairs(srctab
) do
155 QuestHelper
: Assert(not ite
[k
])
166 DBC_Put(group
, id
, ite
)
168 freq_group
[ite
] = group
172 QuestHelper
:TextOut(string.format("Tried to get %s/%s, failed", tostring(group
), tostring(id
)))
178 frequencies
[ite
] = (frequencies
[ite
] or 0) + (register
and 1 or 1000000000) -- effectively infinity
184 local function incinerate(ite
, crunchy
)
185 if not crunchy
[ite
] then
188 for k
, v
in pairs(ite
) do
189 if type(k
) == "table" then incinerate(k
, crunchy
) end
190 if type(v
) == "table" then incinerate(v
, crunchy
) end
195 function DB_ReleaseItem(ite
)
196 QuestHelper
: Assert(ite
)
197 frequencies
[ite
] = frequencies
[ite
] - 1
199 if frequencies
[ite
] == 0 then
200 --print("incinerating", freq_group[ite], freq_id[ite])
201 cache
[freq_group
[ite]]
[freq_id
[ite]]
= nil
202 freq_group
[ite
] = nil
205 local incin
= QuestHelper
:CreateTable("incinerate")
206 incinerate(ite
, incin
)
207 for k
, _
in pairs(incin
) do
208 QuestHelper
:ReleaseTable(k
)
209 end -- burn baby burn
210 QuestHelper
:ReleaseTable(incin
)
214 function DB_ListItems(group
)
216 for _
, db
in ipairs(QHDB
) do
218 QuestHelper
: Assert(not db
.__serialize_index
and not db
.__serialize_data
)
219 for k
, _
in pairs(db
[group
]) do
220 if type(k
) ~= "string" or not k
:match("__.*") then
228 for k
, _
in pairs(tab
) do
235 function DB_how_many_are_used()
237 for k
, v
in pairs(cache
) do
238 for k2
, v2
in pairs(v
) do
245 function DB_DumpItems()
247 for k
, v
in pairs(freq_group
) do
248 dt
[string.format("%s/%s", freq_group
[k
], tostring(freq_id
[k
]))] = true