Thread Tools Display Modes
Prev Previous Post   Next Post Next
07/08/14, 09:11 AM   #1
zgrssd
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 280
LibConstantMapper

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:
  1. ConstantMapperMappings = {
  2.     { mapping = "ChatCategories", pattern = "^CHAT_CATEGORY_", exclude = "^CHAT_CATEGORY_HEADER" }
  3. }
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:
  1. ConstantMapperMappings = {
  2.     { mapping = "ChatCategories", pattern = "^CHAT_CATEGORY_", exclude = "^CHAT_CATEGORY_HEADER" },
  3.     { mapping = "ChatChannels", pattern = "^CHAT_CHANNEL_"}
  4. }

main.lua
Lua Code:
  1. local Mappings = ConstantMapperMappings
  2. local data = { }
  3.  
  4. --Make certain data contains a table for each mapping
  5. for i=1, #Mappings, 1 do
  6.     local currentMap = Mappings[i]
  7.    
  8.     if(data[currentMap.mapping] == nil) then
  9.         data[currentMap.mapping] = {}
  10.     end
  11. end
  12.  
  13. --For every entry in the Global table
  14. for key, value in zo_insecurePairs(_G) do
  15.     --Compare it to every mapping
  16.     for i=1, #Mappings, 1 do
  17.         local currentMap = Mappings[i]
  18.        
  19.         if (key):find(currentMap.pattern) and (currentMap.exclude == nil or not(key):find(currentMap.exclude)) then
  20.             --found a Value for this mapping, so store it in the table
  21.             local newentry = { key = key, value = value}
  22.            
  23.             table.insert(data[currentMap.mapping], newentry)
  24.         end
  25.     end
  26. 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.
  Reply With Quote
 

ESOUI » AddOns » Released AddOns » LibConstantMapper


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off