Mercurial > wow > cyborg-mmo7
comparison CyborgMMO7.lua @ 40:67ad1101ee10
Added an asynchronous loading mechanism for item and battle pet data. The wow objects loading is delayed until all the necessary data is available.
author | madcatzinc@35b17cf1-18cd-47ff-9ca3-31d6b526ef09 |
---|---|
date | Thu, 25 Apr 2013 23:02:39 +0000 |
parents | 0734b90e605a |
children | 841191e42919 |
comparison
equal
deleted
inserted
replaced
39:ce4ddefb68c2 | 40:67ad1101ee10 |
---|---|
28 -- CyborgMMO_DPrint("LoadStrings("..self:GetName()..") = "..CyborgMMO_StringTable[self:GetName()]) | 28 -- CyborgMMO_DPrint("LoadStrings("..self:GetName()..") = "..CyborgMMO_StringTable[self:GetName()]) |
29 self:SetText(CyborgMMO_StringTable[self:GetName()]) | 29 self:SetText(CyborgMMO_StringTable[self:GetName()]) |
30 end | 30 end |
31 | 31 |
32 local VarsLoaded = false | 32 local VarsLoaded = false |
33 local AsyncDataLoaded = false | |
33 local EnteredWorld = false | 34 local EnteredWorld = false |
34 local BindingsLoaded = false | 35 local BindingsLoaded = false |
35 local SettingsLoaded = false | 36 local SettingsLoaded = false |
36 local SaveName = GetRealmName().."_"..UnitName("player") | 37 local SaveName = GetRealmName().."_"..UnitName("player") |
37 local Settings = nil | 38 local Settings = nil |
115 if id then | 116 if id then |
116 return tonumber(id) | 117 return tonumber(id) |
117 end | 118 end |
118 end | 119 end |
119 end | 120 end |
121 | |
122 local KnownOldObjectTypes = { | |
123 item = true, | |
124 macro = true, | |
125 spell = true, | |
126 petaction = true, | |
127 merchant = true, | |
128 companion = true, | |
129 equipmentset = true, | |
130 callback = true, | |
131 } | |
120 | 132 |
121 local function ConvertOldRatData(oldData) | 133 local function ConvertOldRatData(oldData) |
122 local newData = {} | 134 local newData = {} |
123 for mode,modeData in ipairs(oldData) do | 135 for mode,modeData in ipairs(oldData) do |
124 newData[mode] = {} | 136 newData[mode] = {} |
166 CyborgMMO_DPrint("converting callback:", buttonData.Detail) | 178 CyborgMMO_DPrint("converting callback:", buttonData.Detail) |
167 newData[mode][button] = { | 179 newData[mode][button] = { |
168 type = type, | 180 type = type, |
169 detail = buttonData.Detail, | 181 detail = buttonData.Detail, |
170 } | 182 } |
171 elseif type then | 183 elseif not KnownOldObjectTypes[type] then |
172 -- maybe it's an item type | 184 -- maybe it's an item type |
173 local id = buttonData.Detail | 185 local id = buttonData.Detail |
174 local class = select(6, GetItemInfo(id)) -- :NOTE: this may fail if the item is not yet in the cache | 186 local class = select(6, GetItemInfo(id)) -- :NOTE: this may fail if the item is not yet in the cache |
175 if class == type then | 187 if class == type then |
176 CyborgMMO_DPrint("converting item:", id, type, class) | 188 CyborgMMO_DPrint("converting item:", id, type, class) |
185 end | 197 end |
186 end | 198 end |
187 return newData | 199 return newData |
188 end | 200 end |
189 | 201 |
202 ------------------------------------------------------------------------------ | |
203 | |
204 local PreloadFrame | |
205 local step_timeout = 1 | |
206 local total_timeout = 15 | |
207 | |
208 local function PreloadFrameUpdate(self, dt) | |
209 self.step_timeout = self.step_timeout - dt | |
210 self.total_timeout = self.total_timeout - dt | |
211 if self.step_timeout < 0 then | |
212 local items,pets = 0,0 | |
213 -- check items | |
214 for itemID in pairs(self.itemIDs) do | |
215 if GetItemInfo(itemID) then | |
216 self.itemIDs[itemID] = nil | |
217 else | |
218 items = items + 1 | |
219 end | |
220 end | |
221 -- check pets | |
222 for petID in pairs(self.petIDs) do | |
223 if C_PetJournal.GetPetInfoByPetID(petID) then | |
224 self.petIDs[petID] = nil | |
225 else | |
226 pets = pets + 1 | |
227 end | |
228 end | |
229 CyborgMMO_DPrint("PreloadFrameUpdate step", self.total_timeout, "items:", items, "pets:", pets) | |
230 if self.total_timeout < 0 or next(self.itemIDs)==nil and next(self.petIDs)==nil then | |
231 -- when done destroy the frame and throw an event for further loading | |
232 self:Hide() | |
233 self:SetParent(nil) | |
234 PreloadFrame = nil | |
235 CyborgMMO_Event(nil, "CYBORGMMO_ASYNC_DATA_LOADED") | |
236 else | |
237 self.step_timeout = step_timeout | |
238 end | |
239 end | |
240 end | |
241 | |
242 function PreLoad(data) | |
243 -- create ID sets to sync | |
244 local itemIDs = {} | |
245 local petIDs = {} | |
246 | |
247 -- gather all needed IDs (and trigger sync while doing so) | |
248 if data.Rat then | |
249 for mode=1,RAT7.MODES do | |
250 for button=1,RAT7.BUTTONS do | |
251 local data = data.Rat[mode][button] | |
252 if data then | |
253 if data.type=='item' then | |
254 local itemID = data.detail | |
255 if not GetItemInfo(itemID) then | |
256 itemIDs[itemID] = true | |
257 end | |
258 elseif data.type=='battlepet' then | |
259 local petID = data.detail | |
260 if not C_PetJournal.GetPetInfoByPetID(petID) then | |
261 petIDs[petID] = true | |
262 end | |
263 end | |
264 end | |
265 end | |
266 end | |
267 end | |
268 -- gather IDs from old unconverted data (in case we need to convert it) | |
269 if data[SaveName] and data[SaveName].Rat then | |
270 for mode=1,RAT7.MODES do | |
271 for button=1,RAT7.BUTTONS do | |
272 local data = data[SaveName].Rat[mode][button] | |
273 if data then | |
274 -- items actually had their class overwrite the Type field | |
275 if not KnownOldObjectTypes[data.Type] and type(data.Detail)=='number' then | |
276 local itemID = data.Detail | |
277 if not GetItemInfo(itemID) then | |
278 itemIDs[itemID] = true | |
279 end | |
280 end | |
281 end | |
282 end | |
283 end | |
284 end | |
285 | |
286 -- create frame for regular updates | |
287 PreloadFrame = CreateFrame("Frame") | |
288 PreloadFrame.itemIDs = itemIDs | |
289 PreloadFrame.petIDs = petIDs | |
290 PreloadFrame.total_timeout = total_timeout | |
291 PreloadFrame.step_timeout = step_timeout | |
292 PreloadFrame:SetScript("OnUpdate", PreloadFrameUpdate) | |
293 PreloadFrame:Show() | |
294 end | |
295 | |
296 ------------------------------------------------------------------------------ | |
297 | |
190 function CyborgMMO_Event(self, event, ...) | 298 function CyborgMMO_Event(self, event, ...) |
191 if event == "VARIABLES_LOADED" then | 299 if event == "VARIABLES_LOADED" then |
192 VarsLoaded = true | 300 VarsLoaded = true |
193 -- create root table if necessary | 301 -- create root table if necessary |
194 if not CyborgMMO7SaveData then | 302 if not CyborgMMO7SaveData then |
195 CyborgMMO7SaveData = {} | 303 CyborgMMO7SaveData = {} |
196 end | 304 end |
305 PreLoad(CyborgMMO7SaveData) | |
306 elseif event == "CYBORGMMO_ASYNC_DATA_LOADED" then | |
307 AsyncDataLoaded = true | |
197 -- convert old profile | 308 -- convert old profile |
198 if CyborgMMO7SaveData[SaveName] and not CyborgMMO7SaveData.Settings then | 309 if CyborgMMO7SaveData[SaveName] and not CyborgMMO7SaveData.Settings then |
199 local oldData = CyborgMMO7SaveData[SaveName] | 310 local oldData = CyborgMMO7SaveData[SaveName] |
200 CyborgMMO7SaveData = {} | 311 CyborgMMO7SaveData = {} |
201 CyborgMMO7SaveData.Settings = oldData.Settings | 312 CyborgMMO7SaveData.Settings = oldData.Settings |
209 else | 320 else |
210 CyborgMMO_DPrint("Event is "..tostring(event)) | 321 CyborgMMO_DPrint("Event is "..tostring(event)) |
211 end | 322 end |
212 | 323 |
213 -- Fire Loading if and only if the player is in the world and vars are loaded | 324 -- Fire Loading if and only if the player is in the world and vars are loaded |
214 if not BindingsLoaded and VarsLoaded and EnteredWorld then | 325 if not BindingsLoaded and VarsLoaded and AsyncDataLoaded and EnteredWorld then |
215 local data = CyborgMMO_GetSaveData() | 326 local data = CyborgMMO_GetSaveData() |
216 | 327 |
217 CyborgMMO_RatPageModel:LoadData() | 328 CyborgMMO_RatPageModel:LoadData() |
218 BindingsLoaded = true | 329 BindingsLoaded = true |
219 | 330 |
239 CyborgMMO_SetMiniMapButton(Settings.MiniMapButton) | 350 CyborgMMO_SetMiniMapButton(Settings.MiniMapButton) |
240 CyborgMMO_SetCyborgHeadButton(Settings.CyborgButton) | 351 CyborgMMO_SetCyborgHeadButton(Settings.CyborgButton) |
241 | 352 |
242 local xmin,ymin = Minimap:GetLeft(),Minimap:GetBottom() | 353 local xmin,ymin = Minimap:GetLeft(),Minimap:GetBottom() |
243 CyborgMMO_MiniMapButtonReposition(math.deg(math.atan2(ymin, xmin))) | 354 CyborgMMO_MiniMapButtonReposition(math.deg(math.atan2(ymin, xmin))) |
244 -- Close the main window for now | |
245 CyborgMMO_Close() | |
246 end | 355 end |
247 end | 356 end |
248 | 357 |
249 function CyborgMMO_SetDefaultSettings() | 358 function CyborgMMO_SetDefaultSettings() |
250 CyborgMMO_OpenButtonPageOpenMainForm:ClearAllPoints() | 359 CyborgMMO_OpenButtonPageOpenMainForm:ClearAllPoints() |