vanilla-wow-addons – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | --------------------------------------------------------------------------- |
2 | -- MarsMessageParser.lua |
||
3 | --------------------------------------------------------------------------- |
||
4 | -- |
||
5 | -- This file Copyright (C) 2005 MarsMod. All rights reserved. |
||
6 | -- |
||
7 | -- You are permitted to include this file in your AddOns provided that you |
||
8 | -- do not modify it. Suggestions and bug reports should be sent to |
||
9 | -- MarsMod @ gmail.com. |
||
10 | -- |
||
11 | -- You are also permitted to modify this file, but only if you first |
||
12 | -- replace all occurances of "MarsMessageParser" with "YourMod". |
||
13 | -- |
||
14 | --------------------------------------------------------------------------- |
||
15 | -- |
||
16 | -- How to use MarsMessageParser: |
||
17 | -- |
||
18 | -- 1. Decide on what your "clientId" will be. Lets use "MyAddOn" for this |
||
19 | -- example. |
||
20 | -- |
||
21 | -- 2. Write a function that you want called when certain messages arrive: |
||
22 | -- function MyAddOn_DeclareWinner(player, itemText) |
||
23 | -- DEFAULT_CHAT_FRAME:AddMessage(itemText.." goes to "..player.."."); |
||
24 | -- end |
||
25 | -- function MyAddOn_DeclareSelfWinner(itemText) |
||
26 | -- MyAddOn_DeclareWinner(UnitName("player"), itemText); |
||
27 | -- end |
||
28 | -- |
||
29 | -- 3. Register your functions in the order you want them checked: |
||
30 | -- MarsMessageParser_RegisterFunction("MyAddOn", LOOT_ROLL_YOU_WON, |
||
31 | -- MyAddOn_DeclareSelfWinner, true); |
||
32 | -- MarsMessageParser_RegisterFunction("MyAddOn", LOOT_ROLL_WON, |
||
33 | -- MyAddOn_DeclareWinner, true); |
||
34 | -- |
||
35 | -- 4. Somewhere in your event handling code, add something like this: |
||
36 | -- if(event == "CHAT_MSG_LOOT") then |
||
37 | -- MarsMessageParser_ParseMessage("MyAddOn", arg1); |
||
38 | -- end |
||
39 | -- |
||
40 | -- The message parser will try to check arg1 against all your registered |
||
41 | -- functions in the order you registered them and will call the first |
||
42 | -- function that matches. |
||
43 | -- |
||
44 | --------------------------------------------------------------------------- |
||
45 | -- |
||
46 | -- API Specification |
||
47 | -- |
||
48 | -- MarsMessageParser_RegisterFunction(clientId, formatString, func, endsWithLink) |
||
49 | -- clientId - a string unique to your AddOn to avoid AddOn conflict |
||
50 | -- formatString - the localized format from GlobalVariables.lua for the |
||
51 | -- message you want to check for |
||
52 | -- func - the function that you want called when that message arrives. |
||
53 | -- the parameters in formatString will be passed into your function |
||
54 | -- in the local-independent order |
||
55 | -- endsWithLink - if true, tells the message parser to treat the last |
||
56 | -- seven parameters in the local-independent order as the seven |
||
57 | -- parameters for a textual item link. these parameters will then be |
||
58 | -- wrapped up into a single string parameter for passing to your |
||
59 | -- function |
||
60 | -- |
||
61 | -- MarsMessageParser_ParseMessage(clientId, message) |
||
62 | -- clientId - a string unique to your AddOn to avoid AddOn conflict |
||
63 | -- message - the localized message received from the server |
||
64 | -- |
||
65 | --------------------------------------------------------------------------- |
||
66 | |||
67 | local parseRegistrations; |
||
68 | local ParseFunction; |
||
69 | local parseBuffer; |
||
70 | local parseOrder; |
||
71 | local parseCount; |
||
72 | |||
73 | if((not MarsMessageParser_Version) or (MarsMessageParser_Version < 10)) then |
||
74 | |||
75 | MarsMessageParser_Version = 10; |
||
76 | |||
77 | parseRegistrations = {}; |
||
78 | |||
79 | ParseFunction = function (character) |
||
80 | local result; |
||
81 | if(not parseBuffer) then |
||
82 | if(character == "%") then |
||
83 | parseBuffer = character; |
||
84 | result = ""; |
||
85 | else |
||
86 | if(string.find(character, "[%^%$%(%)%%%.%[%]%*%+%-%?]")) then |
||
87 | result = "%"..character; |
||
88 | else |
||
89 | result = character; |
||
90 | end |
||
91 | end |
||
92 | else |
||
93 | parseBuffer = parseBuffer..character; |
||
94 | if(string.find(character, "[%%cdEefgGiouXxqs]")) then |
||
95 | local order; |
||
96 | _, _, order = string.find(parseBuffer, "(%d+)%$"); |
||
97 | parseCount = parseCount + 1; |
||
98 | if(order) then |
||
99 | parseOrder[parseCount] = tonumber(order); |
||
100 | else |
||
101 | parseOrder[parseCount] = parseCount; |
||
102 | end |
||
103 | result = "(.+)"; |
||
104 | parseBuffer = nil; |
||
105 | elseif(string.find(character, "[%$%.%d]")) then |
||
106 | result = ""; |
||
107 | else |
||
108 | result = parseBuffer; |
||
109 | parseBuffer = nil; |
||
110 | end |
||
111 | end |
||
112 | return result; |
||
113 | end |
||
114 | |||
115 | function MarsMessageParser_RegisterFunction(clientId, formatString, func, endsWithLink) |
||
116 | if(not parseRegistrations[clientId]) then |
||
117 | parseRegistrations[clientId] = {}; |
||
118 | end |
||
119 | parseBuffer = nil; |
||
120 | parseOrder = {}; |
||
121 | parseCount = 0; |
||
122 | local parseString = string.gsub(formatString, ".", ParseFunction); |
||
123 | local registerId = table.getn(parseRegistrations[clientId]) + 1; |
||
124 | parseRegistrations[clientId][registerId] = { parseString, parseOrder, func, endsWithLink }; |
||
125 | end |
||
126 | |||
127 | function MarsMessageParser_ParseMessage(clientId, message) |
||
128 | local registerId = 1; |
||
129 | local findResult; |
||
130 | if(not parseRegistrations[clientId]) then |
||
131 | return; |
||
132 | end |
||
133 | repeat |
||
134 | findResult = {string.find(message, parseRegistrations[clientId][registerId][1])}; |
||
135 | if(findResult[1]) then |
||
136 | local i, n; |
||
137 | local callResult = {}; |
||
138 | n = table.getn(findResult); |
||
139 | for i=3,n do |
||
140 | callResult[parseRegistrations[clientId][registerId][2][i-2]] = findResult[i]; |
||
141 | end |
||
142 | n = n - 2; |
||
143 | if(parseRegistrations[clientId][registerId][4]) then |
||
144 | callResult[n-6] = string.format("%s|Hitem:%s:%s:%s:%s|h[%s]|h%s", callResult[n-6], callResult[n-5], callResult[n-4], callResult[n-3], callResult[n-2], callResult[n-1], callResult[n]); |
||
145 | for i=n,n-5,-1 do |
||
146 | callResult[i] = nil; |
||
147 | end |
||
148 | end |
||
149 | parseRegistrations[clientId][registerId][3](unpack(callResult)); |
||
150 | return; |
||
151 | end |
||
152 | registerId = registerId + 1; |
||
153 | until(not parseRegistrations[clientId][registerId]); |
||
154 | end |
||
155 | |||
156 | end |