Skip to content

Instantly share code, notes, and snippets.

@Meorawr
Created February 20, 2021 16:25
Show Gist options
  • Save Meorawr/f139b8f3a032c1e9c18af88a514402b9 to your computer and use it in GitHub Desktop.
Save Meorawr/f139b8f3a032c1e9c18af88a514402b9 to your computer and use it in GitHub Desktop.
Battle Pet GUID Detector
local BattlePetTrackerMixin = {};
function BattlePetTrackerMixin:OnLoad()
self:RegisterUnitEvent("UNIT_SPELLCAST_SUCCEEDED", "player", "");
self:RegisterEvent("COMPANION_UPDATE");
self:SetScript("OnEvent", function(_, ...) self:OnEvent(...) end);
self.currentServerId = nil;
self.currentInstanceId = nil;
self.currentZoneUid = nil;
self.lastCastTime = nil;
self.companionJournalGuid = nil;
self.companionUnitGuid = nil;
end
function BattlePetTrackerMixin:OnEvent(event, arg1, ...)
if event == "UNIT_SPELLCAST_SUCCEEDED" then
self:OnPlayerSpellCastSucceeded(...);
elseif event == "COMPANION_UPDATE" and arg1 == "CRITTER" then
self:OnCompanionUpdate();
end
end
function BattlePetTrackerMixin:OnPlayerSpellCastSucceeded(castGuid, spellId)
local _, _, serverId, instanceId, zoneUid, _, castUid = string.split("-", castGuid);
serverId = tonumber(serverId);
instanceId = tonumber(instanceId);
zoneUid = tonumber(zoneUid);
if serverId == 0 then
-- Local spell cast that fizzled due to casting requirements.
return;
end
local castOffset = bit.band(tonumber(string.sub(castUid, -6), 16), 0x7fffff);
local castEpoch = GetServerTime() - (GetServerTime() % 2^23);
local castTime = castEpoch + castOffset;
self.currentServerId = serverId;
self.currentInstanceId = instanceId;
self.currentZoneUid = zoneUid;
self.lastCastTime = castTime;
DevTools_Dump({{
spellId = spellId,
currentServerId = self.currentServerId,
currentInstanceId = self.currentInstanceId,
currentZoneUid = self.currentZoneUid,
lastCastTime = date("%Y-%m-%d %H:%M:%S", self.lastCastTime),
}});
end
function BattlePetTrackerMixin:OnCompanionUpdate()
local companionJournalGuid = C_PetJournal.GetSummonedPetGUID();
if self.companionJournalGuid == companionJournalGuid then
return;
end
self.companionJournalGuid = companionJournalGuid;
self.companionUnitGuid = nil;
if not self.companionJournalGuid or not self.lastCastTime then
return;
end
local serverId = self.currentServerId;
local instanceId = self.currentInstanceId;
local zoneUid = self.currentZoneUid;
local npcId = select(11, C_PetJournal.GetPetInfoByPetID(self.companionJournalGuid));
local castTime = self.lastCastTime % 2^23;
for spawnIndex = 0, 16 do -- Arbitrary limit.
local spawnUidLow = bit.band(castTime, 0xfffff);
local spawnUidHigh = bit.bor(bit.lshift(spawnIndex, 3), bit.rshift(castTime, 20));
local guid = string.format("Creature-0-%d-%d-%d-%d-%05X%05X", serverId, instanceId, zoneUid, npcId, spawnUidHigh, spawnUidLow);
GameTooltip:SetOwner(WorldFrame, "ANCHOR_NONE");
GameTooltip:SetHyperlink("unit:" .. guid);
GameTooltip:Show();
if GameTooltip:GetUnit() then
self.companionUnitGuid = guid;
break;
end
end
GameTooltip:Hide();
DevTools_Dump({{
battlePetGuid = self.companionJournalGuid,
unitGuid = self.companionUnitGuid or UnitGUID("none"),
castTime = date("%Y-%m-%d %H:%M:%S", self.lastCastTime),
serverTime = date("%Y-%m-%d %H:%M:%S", GetServerTime()),
}});
end
local frame = Mixin(CreateFrame("Frame"), BattlePetTrackerMixin);
frame:OnLoad();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment