View Single Post
07/11/14, 06:20 AM   #10
zgrssd
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 280
I ultimatively decided against making the mapping and the actuall working library seperate.
I added a function that allows you to get the data based on the name of a map. This function will also be expanded to include caching later.
List Maps only gives the names of maps, not the entire map table

I have also expanded the mappings a bit so it now includes all events, as well as several event subgroups. But that part is still WIP.

I was not able to run this on my client yet and it is not an actuall release, so there are propably still some errors in there:
Lua Code:
  1. --Use lib stub to figure out if the same or higher version was already used
  2. local major, minor = "LibConstantMapper", 0.2
  3. local lib = LibStub:NewLibrary(major, minor)
  4.  
  5. if not lib then
  6.     return
  7. end
  8.  
  9. function lib:getMappedData(pattern, exclude)
  10.     --Sanity check on values
  11.     assert(pattern ~= nil and type(pattern) == "string", "pattern must not be nil and of type string")
  12.     assert(exlcude == nil or type(exclude) == "string", "exclude must be nil or of type string")
  13.    
  14.     local result = {}
  15.  
  16.     --For every entry in the Global table
  17.     for key, value in zo_insecurePairs(_G) do
  18.         --Compare it to the given mapping
  19.         if (key):find(pattern) and (exclude == nil or not(key):find(exclude)) then
  20.             --found a Value for this mapping, so store it in the result table
  21.             local newentry = { key = key, value = value}
  22.            
  23.             table.insert(result, newentry)
  24.         end
  25.     end
  26.  
  27.     return result
  28. end
  29.  
  30. function lib:getDataByMapping(map_name)
  31.     local map = self:getMap(map_name)
  32.     assert (map ~= nil and map ~= {}, "A mapping named '" .. map_name .. "' could not be found")
  33.    
  34.     --If all worked out so far, let getMappedData do it's job
  35.     return self:getMappedData(map.pattern, map.exclude)
  36. end
  37.  
  38. --Functions and Data related to the maps
  39. function lib:listMaps()
  40.     local result = {}
  41.  
  42.     for key, value in pairs(self.__maps) do
  43.         table.insert(result, key)
  44.     end
  45.    
  46.     return result
  47. end
  48.  
  49. function lib:addMap(map, pattern, exclude)
  50.     if(self.__maps == nil) then
  51.         self.__maps = {}
  52.     end
  53.  
  54.     assert(map ~= nil or type(map) == "string", "map must not be nil and of type string")
  55.     assert(pattern ~= nil and type(pattern) == "string", "pattern must not be nil and of type string")
  56.     assert(exlcude == nil or type(exclude) == "string", "exclude must be nil or of type string")
  57.    
  58.     self.__maps[map] = { pattern = pattern, exclude = exclude }
  59. end
  60.  
  61. function lib:getMap(map)
  62.     assert(map ~= nil or type(map) == "string", "map must not be nil and of type string")
  63.    
  64.     return self.__maps[map]
  65. end
  66.  
  67. --Predefined mappings
  68. local ConstantMapperMappings = {
  69.     { mapping = "ChatCategories", pattern = "^CHAT_CATEGORY_", exclude = "^CHAT_CATEGORY_HEADER" },
  70.     { mapping = "ChatChannels", pattern = "^CHAT_CHANNEL_"},
  71.     { mapping = "Events", pattern = "^EVENT_"},
  72.     { mapping = "EventsAbility", pattern = "^EVENT_ABILITY_" },
  73.     { mapping = "EventsAchievement", pattern = "^EVENT_ACHIEVEMENT_"},
  74.     { mapping = "EventsAction", pattern = "^EVENT_ACTION_"},
  75.     { mapping = "EventsActive", pattern = "^EVENT_ACTIVE_"},
  76.     { mapping = "EventsAddOn", pattern "^EVENT_ADD_ON_" },
  77.     { mapping = "EventsAgentChat", pattern "^EVENT_AGENT_CHAT_" },
  78.     { mapping = "EventsAlliancePoint", pattern "^EVENT_ALLIANCE_POINT_" },
  79.     { mapping = "EventsArtifact", pattern "^EVENT_ARTIFACT_" },
  80.     { mapping = "EventsAssignedCampaign", pattern "^EVENT_ASSIGNED_CAMPAIGN_" },
  81.     { mapping = "EventsAttribute", pattern "^EVENT_ATTRIBUTE_" },
  82.     { mapping = "EventsAvenge", pattern "^EVENT_AVENGE_" },
  83.     { mapping = "EventsBankIs", pattern "^EVENT_BANK_IS_" },
  84.     { mapping = "EventsBankedMoney", pattern "^EVENT_BANKED_MONEY_" },
  85.     { mapping = "EventsBattleStandards", pattern "^EVENT_BATTLE_STANDARDS_" },
  86.     { mapping = "EventsBegin", pattern "^EVENT_BEGIN_" },
  87.     { mapping = "EventsBosses", pattern "^EVENT_BOSSES_" },
  88.     { mapping = "EventsBroadcast", pattern "^EVENT_BROADCAST" },
  89.     { mapping = "EventsBuy", pattern "^EVENT_BUY_" },
  90.     { mapping = "EventsBuyback", pattern "^EVENT_BUYBACK_" },
  91.     { mapping = "EventsCampaign", pattern "^EVENT_CAMPAIGN_" },
  92.     { mapping = "EventsCancel", pattern "^EVENT_CANCEL_" },
  93.     { mapping = "EventsCannot", pattern "^EVENT_CANNOT_" },
  94.     { mapping = "EventsCapsLock", pattern "^EVENT_CAPS_LOCK_STATE_" },
  95.     { mapping = "EventsCaptureArea", pattern "^EVENT_CAPTURE_AREA_" },
  96.     { mapping = "EventsChatChannel", pattern "^EVENT_CHAT_CHANNEL_" },
  97.     { mapping = "EventsChatLog", pattern "^EVENT_CHAT_LOG_" },
  98.     { mapping = "EventsChatMessage", pattern "^EVENT_CHAT_MESSAGE_" },
  99.     { mapping = "EventsChatter", pattern "^EVENT_CHATTER_" },
  100.     { mapping = "EventsClose", pattern "^EVENT_CLOSE_" },
  101.     { mapping = "EventsCombat", pattern "^EVENT_COMBAT_" },
  102.     { mapping = "EventsConfirm", pattern "^EVENT_CONFIRM_" },
  103.     { mapping = "EventsConversation", pattern "^EVENT_CONVERSATION_" }
  104. }
  105.  
  106. for i=1, #ConstantMapperMappings, 1 do
  107.     local currentMap = ConstantMapperMappings[i]
  108.     lib:addMap(currentMap.mapping, currentMap.pattern, currentMap.exclude)
  109. end
  Reply With Quote