While trying to figure out wich unambigious Chat Category Constant name relates to wich category I ran into the issue that the Constant Names are the keys in the global table. They can be identified via Pattern Matching, but they are not a subtable that you can easily itterate over.
Doing excessive pattern matching is bad for performance. Also there are other similary organised groups of values (like the Chat_Channels or all the Event ID's) you might want to find an easy match for.
So I started development on this library. It consists of two files:
mappings.lua
Lua Code:
ConstantMapperMappings = {
{ mapping = "ChatCategories", pattern = "^CHAT_CATEGORY_", exclude = "^CHAT_CATEGORY_HEADER" }
}
Just a table. It contains the mapping (wich Key the table will be saved under and will be requestable), the pattern to match (in this case for chat categories) and a pattern to not include (in this case I want all the entries starting with "CHAT_CATEGORY_", except the ones starting wiht "CHAT_CATEGORY_HEADER").
If I also want the chat channels using this library, I just have to modify it like this:
Lua Code:
ConstantMapperMappings = {
{ mapping = "ChatCategories", pattern = "^CHAT_CATEGORY_", exclude = "^CHAT_CATEGORY_HEADER" },
{ mapping = "ChatChannels", pattern = "^CHAT_CHANNEL_"}
}
main.lua
Lua Code:
local Mappings = ConstantMapperMappings
local data = { }
--Make certain data contains a table for each mapping
for i=1, #Mappings, 1 do
local currentMap = Mappings[i]
if(data[currentMap.mapping] == nil) then
data[currentMap.mapping] = {}
end
end
--For every entry in the Global table
for key, value in zo_insecurePairs(_G) do
--Compare it to every mapping
for i=1, #Mappings, 1 do
local currentMap = Mappings[i]
if (key):find(currentMap.pattern) and (currentMap.exclude == nil or not(key):find(currentMap.exclude)) then
--found a Value for this mapping, so store it in the table
local newentry = { key = key, value = value}
table.insert(data[currentMap.mapping], newentry)
end
end
end
I go over all entries in the global table. Compare each entry to each mapping. And if it matches, I store the value as a subtable in Data.
That way I get a easy to itterate over table containing wich ConstantName matches wich Category.
Going over the whoel global table is a lot faster then I anticipated so maybe I switch to a "retreive on demand, but cache" approach instead.
Of course the whole thing will use libStub to avoid multiple execution in the final version. I just wanted to ask if there are any mistakes in this code I have to look out for.