vanilla-wow-addons – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 --[[
2 path: /ChatParse/
3 filename: PFChatParse.lua
4 author: "Daniel Risse" <dan@risse.com>
5 created: Thu, 03 Feb 2004 00:38:00 -0600
6 updated:
7  
8 Chat Parse: Used to scrap information from Chat Messages
9  
10  
11  
12 COMBATLOG_XPGAIN_EXHAUSTION1_GROUP = "%s dies, you gain %d experience. (%s exp %s bonus, +%d group bonus)";
13  
14 local spec = PFChatParse_FormatToPattern(COMBATLOG_XPGAIN_EXHAUSTION1_GROUP, "creepName", "xp", CHAT_PARSE_VALUE1, CHAT_PARSE_NAME1, "group");
15  
16 local message = format(COMBATLOG_XPGAIN_EXHAUSTION1_GROUP, "Creep X", 100, "+20", "rested", 10);
17 -- Creep X dies, you gain 100 experience. (+20 exp rested bonus, +10 group bonus)
18  
19 local match = PFChatParse_MatchPattern(message, spec);
20 if ( match ) then
21 PFDebugMessage("test", "Wahoo!!!"..match.creepName, "info");
22 end
23 ]]
24  
25  
26 local PFChatParse_Events = { };
27  
28 function PFChatParse_OnLoad()
29 end
30  
31 function PFChatParse_OnEvent(event)
32 if ( not PFChatParse_Events[event] ) then
33 --this:UnregisterEvent(event);
34 PFDebugMessage("CP", "OnEvent("..event..") called but nothing registered...", "warning");
35 else
36 local index, info, match, processed, string;
37 processed = { };
38 for index, info in PFChatParse_Events[event] do
39 if ( not info.AddOn or not processed[info.AddOn] ) then
40 if ( info.input ) then
41 string = info.input()
42 else
43 string = arg1;
44 end
45 match = PFChatParse_MatchPattern(string, info.patternSpec);
46 if ( match ) then
47 result = info.func(match);
48 if ( not result and info.AddOn ) then
49 processed[info.AddOn] = true;
50 end
51 end
52 end
53 end
54 end
55 end
56  
57 function PFChatParse_RegisterEvent(info)
58 if ( type(info) ~= "table" ) then
59 PFDebugMessage("CP", "Input to PFChatParse_RegisterEvent must be a table!", "error");
60 return nil;
61 elseif ( not info.event or type(info.event) ~= "string" ) then
62 PFDebugMessage("CP", "Input to PFChatParse_RegisterEvent must contain an event to watch!", "error");
63 return nil;
64 elseif ( not info.func or type(info.func) ~= "function" ) then
65 PFDebugMessage("CP", "Input to PFChatParse_RegisterEvent must contain a function to call back to!", "error");
66 return nil;
67 elseif ( not info.template or type(info.template) ~= "string" ) then
68 PFDebugMessage("CP", "Input to PFChatParse_RegisterEvent must contain a template to match against the Chat message!", "error");
69 return nil;
70  
71 elseif ( not PFChatParse_Events[info.event] ) then
72 PFChatParseFrame:RegisterEvent(info.event);
73 PFChatParse_Events[info.event] = { };
74 table.setn(PFChatParse_Events[info.event], 0);
75 end
76  
77 if ( not info.AddOn ) then
78 PFDebugMessage("CP", "Input to PFChatParse_RegisterEvent did not contain AddOn name; this is needed to PFChatParse_UnregisterEvent and to stop looking for additional matches.", "warning");
79 end
80 PFDebugMessage("CP", "new spec for event: "..info.event, "helper");
81  
82 local spec = PFChatParse_FormatToPattern(info.template, info.fields, info.english);
83 if ( spec ) then
84 table.insert(PFChatParse_Events[info.event], { func = info.func, patternSpec = spec, AddOn = info.AddOn, input = info.input });
85 end
86 end
87  
88 function PFChatParse_UnregisterEvent(info)
89 if ( type(info) ~= "table" ) then
90 PFDebugMessage("CP", "Input to PFChatParse_UnregisterEvent must be a table!", "error");
91 return nil;
92 elseif ( not info.AddOn or type(info.AddOn) ~= "string" ) then
93 PFDebugMessage("CP", "Input to PFChatParse_UnregisterEvent must contain an AddOn to stop watching!", "error");
94 return nil;
95 end
96 local event, specList, index, spec;
97  
98 for event, specList in PFChatParse_Events do
99 if ( not info.event or event == info.event ) then
100 for index, spec in specList do
101 if ( info.AddOn == spec.AddOn ) then
102 table.remove(PFChatParse_Events[event], index);
103 end
104 end
105 if ( table.getn(PFChatParse_Events[event]) == 0 ) then
106 PFChatParse_Events[event] = nil;
107 PFChatParseFrame:UnregisterEvent(event);
108 end
109 end
110 end
111 end
112  
113 -- ("%s hits you for %d damage.", "creep", "damage");
114 -- this function will convert a format string into a pattern that will extract
115 -- named parameters from the string
116  
117 function PFChatParse_FormatToPattern(template, fields, english)
118 PFDebugMessage("CP", "template: "..template, "helper");
119 local ret = { pattern = template };
120 ret.pattern = string.gsub(ret.pattern, "%(", "%%(");
121 ret.pattern = string.gsub(ret.pattern, "%)", "%%)");
122 ret.pattern = string.gsub(ret.pattern, "%.", "%%.");
123 ret.pattern = string.gsub(ret.pattern, "%+", "%%+");
124 ret.pattern = string.gsub(ret.pattern, "%[", "%%[");
125 ret.pattern = string.gsub(ret.pattern, "%]", "%%]");
126  
127 local index, field, count, matchCount, fieldCount;
128 matchCount = 0;
129 fieldCount = 0;
130 local replaceFunc = function(i, m)
131 if ( i == "" ) then
132 matchCount = matchCount + 1;
133 i = matchCount;
134 else
135 i = 0 + i;
136 end
137 PFDebugMessage("CP", "Match: "..i..", "..m, "helper");
138 if ( not fields[i] ) then
139 if ( m == "d" ) then
140 return "[0-9]+";
141 elseif ( m == "s" ) then
142 return ".+";
143 end
144 else
145 fieldCount = fieldCount + 1;
146 ret[fieldCount] = fields[i];
147 fields[i] = nil;
148 if ( m == "d" ) then
149 return "([0-9]+)";
150 elseif ( m == "s" ) then
151 return "(.+)";
152 end
153 end
154 end;
155 ret.pattern, count = string.gsub(ret.pattern, "%%(%d*)$?([ds])", replaceFunc);
156 if ( next(fields) ) then
157 if ( english ) then
158 PFDebugMessage("CP", "More names passed than substitution items!\n "..template.."\n "..english, "error");
159 else
160 PFDebugMessage("CP", "More names passed than substitution items!", "error");
161 end
162 end
163 PFDebugMessage("CP", "pattern: "..ret.pattern, "helper");
164 return ret;
165 end
166  
167  
168 function PFChatParse_MatchPattern(message, spec)
169 local match = { string.gfind(message, spec.pattern)() };
170 if ( table.getn(match) ~= 0 ) then
171 local ret = { };
172 local index;
173 for index=1, table.getn(match), 1 do
174 if ( spec[index] ) then
175 ret[spec[index]] = match[index];
176 end
177 end
178 return ret;
179 end
180 return nil;
181 end