commit b08e75faa6a04d781ddfbae53fbca5fde947b7a0 Author: Robin Date: Tue Jan 19 00:36:03 2021 +0100 Init diff --git a/LibGUID.lua b/LibGUID.lua new file mode 100644 index 0000000..b4ffe70 --- /dev/null +++ b/LibGUID.lua @@ -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 \ No newline at end of file diff --git a/LibGUID.toc b/LibGUID.toc new file mode 100644 index 0000000..78c753e --- /dev/null +++ b/LibGUID.toc @@ -0,0 +1,4 @@ +## Interface: 90002 +## Name: LibGUID +## Description: Handle GUID Info +LibGUID.lua \ No newline at end of file