This commit is contained in:
Robin 2021-01-19 00:36:03 +01:00
commit b08e75faa6
2 changed files with 88 additions and 0 deletions

84
LibGUID.lua Normal file
View File

@ -0,0 +1,84 @@
local lib = LibStub:NewLibrary("LibGUID", 1)
if not lib then return end
local guid_methods = guid_methods or {}
local mixins = {'CreateGUIDObject'}
lib.mixinTargets = lib.mixinTargets or {}
lib.guidCache = lib.guidCache or {}
local guidCache = lib.guidCache
local guidTypes = {
["Player"] = true,
["Creature"] = true,
["Pet"] = true,
["GameObject"] = true,
["Vehicle"] = true,
["Vignette"] = true
}
local function isGUID(guid)
if(guid) then
local typ = strsplit("-", guid)
if(typ and guidTypes[typ]) then
return typ
end
end
end
lib.isGUID = isGUID
function guid_methods:Touch()
self['lastModified'] = GetTime()
end
function guid_methods:Refresh()
if(self['type'] == 'Player') then
local localizedClass, englishClass, localizedRace, englishRace, sex, name, realm = GetPlayerInfoByGUID(self[1])
if(name and name !== self['name']) then
self['localizedClass'] = localizedClass
self['englishClass'] = englishClass
self['localizedRace'] = localizedRace
self['englishRace'] = englishRace
self['sex'] = sex
self['name'] = name
self['realm'] = realm
end
end
self:Touch()
end
function lib:CreateGUIDObject(obj)
if(obj) then
if(type(obj) == 'string') then
obj = {obj}
end
local guid = obj[1]
if(not guidCache[guid]) then
local typ = isGUID(guid)
if(typ) then
obj['type'] = typ
for k, v in pairs(guid_methods) do
obj[k] = v
end
obj:Refresh()
guidCache[guid] = obj
end
else
return guidCache[guid]
end
end
end
function lib:PruneGUIDCache(maxAge)
local maxAge = maxAge or 3600
local t = GetTime()
for guid, obj in pairs(guidCache) do
if(obj['lastModified'] < t - maxAge) then
guidCache[guid] = nil
end
end
end
for guid, obj in pairs(guidCache)do
obj:Refresh()
end
function lib:Embed(target)
for _,name in pairs(mixins) do
target[name] = lib[name]
end
lib.mixinTargets[target] = true
end
for target, _ in pairs(lib.mixinTargets) do
lib:Embed(target)
end

4
LibGUID.toc Normal file
View File

@ -0,0 +1,4 @@
## Interface: 90002
## Name: LibGUID
## Description: Handle GUID Info
LibGUID.lua