WoW:Parsing event messages: Difference between revisions

m
Move page script moved page Parsing event messages to WoW:Parsing event messages without leaving a redirect
m (Wikify)
m (Move page script moved page Parsing event messages to WoW:Parsing event messages without leaving a redirect)
 
(5 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{wikify}}
{{wow/uihowto}}
 
Event messages are things like combat log messages, loot messages, online/offline notifications, and so on.
Event messages are things like combat log messages, loot messages, online/offline notifications, and so on.


Line 9: Line 8:
In order to get this information, you need to parse the big long string.
In order to get this information, you need to parse the big long string.


== What's the quick-and-dirty way to parse an event message? ==
== Parsing messages the quick way ==
What's the quick-and-dirty way to parse an event message?


Well, you can look at how the event message looks, and you can manually write a string.find over that string to extract the information you need.  For example,
Well, you can look at how the event message looks, and you can manually write a string.find over that string to extract the information you need.  For example,
Line 22: Line 22:
  end
  end


== What's so dirty about the quick-and-dirty way? ==
You will find that as people in other locales start using your AddOn, that they have problems.  This is due to the fact that your code is expecting the system messages to be arriving in English, whereas they might actually be arriving in some other language.
Once you've started off on the quick-and-dirty way, you are likely to consider asking people to provide you translations, so you can modify your code accordingly for a quick-and-dirty fix.  However, you can tell what a mess your code would quickly turn into:
  function MyHonorTracker_OnEvent()
  function MyHonorTracker_OnEvent()
   if(event == "CHAT_MSG_COMBAT_HONOR_GAIN") then
   if(event == "CHAT_MSG_COMBAT_HONOR_GAIN") then
Line 35: Line 30:
     elseif(GetLocale() == "frFR") then
     elseif(GetLocale() == "frFR") then
       pattern = "french-words (.+) french-words (.+) french-words (.+)";
       pattern = "french-words (.+) french-words (.+) french-words (.+)";
       -- notice that the parameters are not necessarily always in the same order in all languages
    elseif(GetLocale() == "koKR") then
       pattern = "(.+) korean-for-dies, korean-words: (.+) %(more-korean: (.+)%)";
     else
     else
       pattern = "who knows";
       pattern = "who knows";
Line 52: Line 48:
  end
  end


== What's the clean-and-easy way? ==
== Parsing messages the clean way ==
What's the clean-and-easy way?


Well, first, you should extract GlobalStrings.lua out of your WoW install.  Then, you will want to look through that file for the variable that contains the template of the message you're looking for.  In our case, it is
Well, first, you should extract GlobalStrings.lua out of your WoW install.  Then, you will want to look through that file for the variable that contains the template of the message you're looking for.  In our case, it is
Line 84: Line 81:
MarsMessageParser will take care of reading the GlobalStrings.lua, creating the correct pattern for the current locale, re-ordering the parameters accordingly, and finally calling MyHonorTrackingFunction with three parameters: killed, rank, and honor.
MarsMessageParser will take care of reading the GlobalStrings.lua, creating the correct pattern for the current locale, re-ordering the parameters accordingly, and finally calling MyHonorTrackingFunction with three parameters: killed, rank, and honor.


== What's the Clean-And-Not-So-Easy way? ==
== Parsing messages the right way ==
What's the Clean-And-Not-So-Easy way?
 
If you don't want to download the MarsMessageParser and include it in your addon, the thing you're left to do is find a way to do it yourself. But rather than develop a ton of code, you just want to "hardwire" your addon for just the string you want to use, rather than the infinite amount of infinite variables in every string in GlobalStrings.lua.
If you don't want to download the MarsMessageParser and include it in your addon, the thing you're left to do is find a way to do it yourself. But rather than develop a ton of code, you just want to "hardwire" your addon for just the string you want to use, rather than the infinite amount of infinite variables in every string in GlobalStrings.lua.


Well, let's look at FACTION_STANDING_INCREASED. According to GlobalStrings.lua, this says "Reputation with %s increased by %d." Well, to make it work like the strings at the top, we'd need to replace %s and %d with (.+). But how do we do that? Here's how:
Well, let's look at FACTION_STANDING_INCREASED. According to GlobalStrings.lua, this says "Reputation with %s increased by %d." Well, to make it work like the strings at the top, we'd need to replace %s and %d with (.+). But how do we do that? Here's how:


  newpattern = string.gsub(string.gsub(FACTION_STANDING_DECREASED, "(%%s)", "(.+)"), "(%%d)", "(.+)")
  newpattern = string.gsub(string.gsub(FACTION_STANDING_INCREASED, "(%%s)", "(.+)"), "(%%d)", "(.+)")
  start, end, faction, amount = string.find(string, newpattern)
  start, end, faction, amount = string.find(string, newpattern)


This example will properly parse the string, and return all the information we need. How was this achieved? By using a % in front of the %s, it tells it to look for %s in stead of a space, which is what %s would normally be looking for. This way, even though we have to hardcode in the strings we want, we can also don't have to bother with translations.
This example will properly parse the string, and return all the information we need. How was this achieved? By using a % in front of the %s, it tells it to look for %s in stead of a space, which is what %s would normally be looking for. This way, even though we have to hardcode in the strings we want, we can also don't have to bother with translations.
[[Category:HOWTOs|Parse Event Messages]]
[[Category:HOWTOs|Parse Event Messages]]
Anonymous user