vanilla-wow-addons – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1  
2 local vmajor, vminor = "1", tonumber(string.sub("$Revision: 666 $", 12, -3))
3 local stubvarname = "TekLibStub"
4 local libvarname = "PTTradeskillsEmbed"
5  
6  
7 -- Check to see if an update is needed
8 -- if not then just return out now before we do anything
9 local libobj = getglobal(libvarname)
10 if libobj and not libobj:NeedsUpgraded(vmajor, vminor) then return end
11  
12 local stubobj = getglobal(stubvarname)
13 if not stubobj then
14 stubobj = {}
15 setglobal(stubvarname, stubobj)
16  
17  
18 -- Instance replacement method, replace contents of old with that of new
19 function stubobj:ReplaceInstance(old, new)
20 for k,v in pairs(old) do old[k]=nil end
21 for k,v in pairs(new) do old[k]=v end
22 end
23  
24  
25 -- Get a new copy of the stub
26 function stubobj:NewStub(name)
27 local newStub = {}
28 self:ReplaceInstance(newStub, self)
29 newStub.libName = name
30 newStub.lastVersion = ''
31 newStub.versions = {}
32 return newStub
33 end
34  
35  
36 -- Get instance version
37 function stubobj:NeedsUpgraded(vmajor, vminor)
38 local versionData = self.versions[vmajor]
39 if not versionData or versionData.minor < vminor then return true end
40 end
41  
42  
43 -- Get instance version
44 function stubobj:GetInstance(version)
45 if not version then version = self.lastVersion end
46 local versionData = self.versions[version]
47 if not versionData then print(string.format("<%s> Cannot find library version: %s", self.libName, version or "")) return end
48 return versionData.instance
49 end
50  
51  
52 -- Register new instance
53 function stubobj:Register(newInstance)
54 local version,minor = newInstance:GetLibraryVersion()
55 self.lastVersion = version
56 local versionData = self.versions[version]
57 if not versionData then
58 -- This one is new!
59 versionData = {
60 instance = newInstance,
61 minor = minor,
62 old = {},
63 }
64 self.versions[version] = versionData
65 newInstance:LibActivate(self)
66 return newInstance
67 end
68 -- This is an update
69 local oldInstance = versionData.instance
70 local oldList = versionData.old
71 versionData.instance = newInstance
72 versionData.minor = minor
73 local skipCopy = newInstance:LibActivate(self, oldInstance, oldList)
74 table.insert(oldList, oldInstance)
75 if not skipCopy then
76 for i, old in ipairs(oldList) do self:ReplaceInstance(old, newInstance) end
77 end
78 return newInstance
79 end
80 end
81  
82  
83 if not libobj then
84 libobj = stubobj:NewStub(libvarname)
85 setglobal(libvarname, libobj)
86 end
87  
88 local lib = {}
89  
90  
91 -- Return the library's current version
92 function lib:GetLibraryVersion()
93 return vmajor, vminor
94 end
95  
96  
97 -- Activate a new instance of this library
98 function lib:LibActivate(stub, oldLib, oldList)
99 local maj, min = self:GetLibraryVersion()
100  
101 if oldLib then
102 local omaj, omin = oldLib:GetLibraryVersion()
103 self.compost = oldLib.compost or CompostLib and CompostLib:GetInstance("compost-1")
104 self.quals, self.tradedata, self.eventframe = oldLib.quals, oldLib.tradedata, oldLib.eventframe
105 else
106 self.compost = CompostLib and CompostLib:GetInstance("compost-1")
107 self.quals = {trivial = 0, easy = 1, medium = 2, optimal = 3, difficult = 4}
108 self.tradedata = {}
109  
110 self.eventframe = CreateFrame("Frame")
111 self.eventframe.master = self
112 self.eventframe:SetScript("OnEvent", function() this.master[event](this.master) end)
113 self.eventframe:RegisterEvent("CRAFT_SHOW")
114 self.eventframe:RegisterEvent("TRADE_SKILL_SHOW")
115 end
116 -- nil return makes stub do object copy
117 end
118  
119  
120 ------------------------------
121 -- Lookup methods --
122 ------------------------------
123  
124 -- Returns a table of recepies this item is used in, or nil if no known use
125 -- Return table elements are as follows:
126 -- [recepieid] = {recepiename, skilllevel, numberneeded}
127 -- recepieid == an itemid for the product (or an enchantid if the recepie is an enchant)
128 -- recepiename == name of the item made
129 -- skilllevel == relative difficulty of the recepie
130 -- numberneeded == number of the ingred item called for in the recepie
131 function lib:GetRecepieUse(itemid)
132 if type(itemid) == "string" then
133 _, _, id = string.find(itemid, "item:(%d+):%d+:%d+:%d+")
134 if id then itemid = tonumber(id)
135 else return end
136 end
137 if type(itemid) ~= "number" then return end
138  
139 local retval
140 for trade,data in self.tradedata do
141 for recid,recdata in data do
142 for id,num in recdata.ing do
143 if id == itemid then
144 if not retval then retval = self.compost and self.compost:Acquire() or {} end
145 local name = recdata.name or GetItemInfo(recid)
146 retval[recid] = self.compost and self.compost:Acquire(name, recdata.skill, num) or {name, recdata.skill, num}
147 end
148 end
149 end
150 end
151  
152 return retval
153 end
154  
155  
156 -- returns true if the tradeskill passed uses the itemid, nil otherwise
157 function lib:TradeUsesItem(trade, itemid)
158 if not self.tradedata[trade] then return end
159  
160 for recid,recdata in self.tradedata[trade] do
161 for id,num in recdata.ing do
162 if id == itemid then return true end
163 end
164 end
165 end
166  
167  
168 -- Returns a list of tradeskills the player know which can use the itemid, or nil if none
169 function lib:TradesUseItem(itemid)
170 local retval
171 for trade,data in self.tradedata do
172 if self:TradeUsesItem(trade, itemid) then
173 if not retval then retval = self.compost and self.compost:Acquire() or {} end
174 table.insert(retval, trade)
175 end
176 end
177  
178 return retval
179 end
180  
181  
182 ----------------------------------
183 -- Tradeskill parsing --
184 ----------------------------------
185  
186 function lib:TRADE_SKILL_SHOW()
187 local trade = GetTradeSkillLine()
188 if not self.tradedata[trade] then self.tradedata[trade] = {} end
189  
190 for i=1,GetNumTradeSkills() do
191 local _, t = GetTradeSkillInfo(i)
192 if (t ~= "header") then
193 local link = GetTradeSkillItemLink(i)
194 if link then
195 local _, _, id = string.find(link, "item:(%d+):%d+:%d+:%d+")
196 self.tradedata[trade][tonumber(id)] = self.tradedata[trade][tonumber(id)] or {ing = self:ParseTradeskillItem(i), skill = self.quals[t]}
197 end
198 end
199 end
200 end
201  
202  
203 function lib:ParseTradeskillItem(tidx)
204 local retval = {}
205  
206 for i=1,GetTradeSkillNumReagents(tidx) do
207 local _, _, num = GetTradeSkillReagentInfo(tidx, i)
208 local link = GetTradeSkillReagentItemLink(tidx, i)
209 if link then
210 local _, _, id = string.find(link, "item:(%d+):%d+:%d+:%d+")
211 retval[tonumber(id)] = num
212 end
213 end
214  
215 return retval
216 end
217  
218  
219 -----------------------------
220 -- Craft parsing --
221 -----------------------------
222  
223 -- Craft == enchanting, tradeskill == everything else
224 function lib:CRAFT_SHOW()
225 local trade = GetCraftName()
226 if not self.tradedata[trade] then self.tradedata[trade] = {} end
227  
228 for i=1,GetNumCrafts() do
229 local name, _, t = GetCraftInfo(i)
230 if (t ~= "header") then
231 local link = GetCraftItemLink(i)
232 if link then
233 local _, _, id = string.find(link, "enchant:(%d+)")
234 self.tradedata[trade][tonumber(id)] = self.tradedata[trade][tonumber(id)] or {ing = self:ParseCraftItem(i), skill = self.quals[t], name = name}
235 end
236 end
237 end
238 end
239  
240  
241 function lib:ParseCraftItem(idx)
242 local retval = {}
243  
244 for i=1,GetCraftNumReagents(idx) do
245 local _, _, num = GetCraftReagentInfo(idx, i)
246 local link = GetCraftReagentItemLink(idx, i)
247 if link then
248 local _, _, id = string.find(link, "item:(%d+):%d+:%d+:%d+")
249 retval[tonumber(id)] = num
250 end
251 end
252  
253 return retval
254 end
255  
256  
257 --------------------------------
258 -- Load this bitch! --
259 --------------------------------
260 libobj:Register(lib)