84 lines
1.9 KiB
Lua
84 lines
1.9 KiB
Lua
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 |