vanilla-wow-addons – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | --[[ |
2 | path: /ChatParse/ |
||
3 | filename: BGChatParse.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 = BGChatParse_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 = BGChatParse_MatchPattern(message, spec); |
||
20 | if ( match ) then |
||
21 | BGDebugMessage("test", "Wahoo!!!"..match.creepName, "info"); |
||
22 | end |
||
23 | ]] |
||
24 | |||
25 | |||
26 | local BGChatParse_Events = { }; |
||
27 | |||
28 | function BGChatParse_OnLoad() |
||
29 | end |
||
30 | |||
31 | function BGChatParse_OnEvent(event) |
||
32 | if ( not BGChatParse_Events[event] ) then |
||
33 | --this:UnregisterEvent(event); |
||
34 | BGDebugMessage("CP", "OnEvent("..event..") called but nothing registered...", "warning"); |
||
35 | else |
||
36 | local index, info, match, processed, string; |
||
37 | processed = { }; |
||
38 | for index, info in BGChatParse_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 = BGChatParse_MatchPattern(string, info.patternSpec); |
||
46 | if ( match ) then |
||
47 | if ( arg2 ) then |
||
48 | match.player = arg2; |
||
49 | end |
||
50 | result = info.func(match); |
||
51 | if ( not result and info.AddOn ) then |
||
52 | processed[info.AddOn] = true; |
||
53 | end |
||
54 | end |
||
55 | end |
||
56 | end |
||
57 | end |
||
58 | end |
||
59 | |||
60 | function BGChatParse_RegisterEvent(info) |
||
61 | if ( type(info) ~= "table" ) then |
||
62 | BGDebugMessage("CP", "Input to BGChatParse_RegisterEvent must be a table!", "error"); |
||
63 | return nil; |
||
64 | elseif ( not info.event or type(info.event) ~= "string" ) then |
||
65 | BGDebugMessage("CP", "Input to BGChatParse_RegisterEvent must contain an event to watch!", "error"); |
||
66 | return nil; |
||
67 | elseif ( not info.func or type(info.func) ~= "function" ) then |
||
68 | BGDebugMessage("CP", "Input to BGChatParse_RegisterEvent must contain a function to call back to!", "error"); |
||
69 | return nil; |
||
70 | elseif ( not info.template or type(info.template) ~= "string" ) then |
||
71 | BGDebugMessage("CP", "Input to BGChatParse_RegisterEvent must contain a template to match against the Chat message!", "error"); |
||
72 | return nil; |
||
73 | |||
74 | elseif ( not BGChatParse_Events[info.event] ) then |
||
75 | BGChatParseFrame:RegisterEvent(info.event); |
||
76 | BGChatParse_Events[info.event] = { }; |
||
77 | table.setn(BGChatParse_Events[info.event], 0); |
||
78 | end |
||
79 | |||
80 | if ( not info.AddOn ) then |
||
81 | BGDebugMessage("CP", "Input to BGChatParse_RegisterEvent did not contain AddOn name; this is needed to BGChatParse_UnregisterEvent and to stop looking for additional matches.", "warning"); |
||
82 | end |
||
83 | BGDebugMessage("CP", "new spec for event: "..info.event, "helper"); |
||
84 | |||
85 | local spec = BGChatParse_FormatToPattern(info.template, info.fields, info.english); |
||
86 | if ( spec ) then |
||
87 | table.insert(BGChatParse_Events[info.event], { func = info.func, patternSpec = spec, AddOn = info.AddOn, input = info.input }); |
||
88 | end |
||
89 | end |
||
90 | |||
91 | function BGChatParse_UnregisterEvent(info) |
||
92 | if ( type(info) ~= "table" ) then |
||
93 | BGDebugMessage("CP", "Input to BGChatParse_UnregisterEvent must be a table!", "error"); |
||
94 | return nil; |
||
95 | elseif ( not info.AddOn or type(info.AddOn) ~= "string" ) then |
||
96 | BGDebugMessage("CP", "Input to BGChatParse_UnregisterEvent must contain an AddOn to stop watching!", "error"); |
||
97 | return nil; |
||
98 | end |
||
99 | local event, specList, index, spec; |
||
100 | |||
101 | for event, specList in BGChatParse_Events do |
||
102 | if ( not info.event or event == info.event ) then |
||
103 | for index, spec in specList do |
||
104 | if ( info.AddOn == spec.AddOn ) then |
||
105 | table.remove(BGChatParse_Events[event], index); |
||
106 | end |
||
107 | end |
||
108 | if ( table.getn(BGChatParse_Events[event]) == 0 ) then |
||
109 | BGChatParse_Events[event] = nil; |
||
110 | BGChatParseFrame:UnregisterEvent(event); |
||
111 | end |
||
112 | end |
||
113 | end |
||
114 | end |
||
115 | |||
116 | -- ("%s hits you for %d damage.", "creep", "damage"); |
||
117 | -- this function will convert a format string into a pattern that will extract |
||
118 | -- named parameters from the string |
||
119 | |||
120 | function BGChatParse_FormatToPattern(template, fields, english) |
||
121 | BGDebugMessage("CP", "template: "..template, "helper"); |
||
122 | local ret = { pattern = template }; |
||
123 | ret.pattern = string.gsub(ret.pattern, "%(", "%%("); |
||
124 | ret.pattern = string.gsub(ret.pattern, "%)", "%%)"); |
||
125 | ret.pattern = string.gsub(ret.pattern, "%.", "%%."); |
||
126 | ret.pattern = string.gsub(ret.pattern, "%+", "%%+"); |
||
127 | ret.pattern = string.gsub(ret.pattern, "%[", "%%["); |
||
128 | ret.pattern = string.gsub(ret.pattern, "%]", "%%]"); |
||
129 | |||
130 | local index, field, count, matchCount, fieldCount; |
||
131 | matchCount = 0; |
||
132 | fieldCount = 0; |
||
133 | local replaceFunc = function(i, m) |
||
134 | if ( i == "" ) then |
||
135 | matchCount = matchCount + 1; |
||
136 | i = matchCount; |
||
137 | else |
||
138 | i = 0 + i; |
||
139 | end |
||
140 | BGDebugMessage("CP", "Match: "..i..", "..m, "helper"); |
||
141 | if ( not fields[i] ) then |
||
142 | if ( m == "d" ) then |
||
143 | return "[0-9]+"; |
||
144 | elseif ( m == "s" ) then |
||
145 | return ".+"; |
||
146 | end |
||
147 | else |
||
148 | fieldCount = fieldCount + 1; |
||
149 | ret[fieldCount] = fields[i]; |
||
150 | fields[i] = nil; |
||
151 | if ( m == "d" ) then |
||
152 | return "([0-9]+)"; |
||
153 | elseif ( m == "s" ) then |
||
154 | return "(.+)"; |
||
155 | end |
||
156 | end |
||
157 | end; |
||
158 | ret.pattern, count = string.gsub(ret.pattern, "%%(%d*)$?([ds])", replaceFunc); |
||
159 | if ( next(fields) ) then |
||
160 | if ( english ) then |
||
161 | BGDebugMessage("CP", "More names passed than substitution items!\n "..template.."\n "..english, "error"); |
||
162 | else |
||
163 | BGDebugMessage("CP", "More names passed than substitution items!", "error"); |
||
164 | end |
||
165 | end |
||
166 | BGDebugMessage("CP", "pattern: "..ret.pattern, "helper"); |
||
167 | return ret; |
||
168 | end |
||
169 | |||
170 | |||
171 | function BGChatParse_MatchPattern(message, spec) |
||
172 | --BGDebugMessage("CP", "match pattern on "..message.." with "..spec.pattern, "error"); |
||
173 | local match = { string.gfind(message, spec.pattern)() }; |
||
174 | if ( table.getn(match) ~= 0 ) then |
||
175 | local ret = { }; |
||
176 | local index; |
||
177 | for index=1, table.getn(match), 1 do |
||
178 | -- BGDebugMessage("CP","match:"..match[index],"debug"); |
||
179 | if ( spec[index] ) then |
||
180 | ret[spec[index]] = match[index]; |
||
181 | end |
||
182 | end |
||
183 | ret["player"] = nil; |
||
184 | return ret; |
||
185 | end |
||
186 | return nil; |
||
187 | end |