wobin@10: wobin@10: assert(LibStub, "LibDataBroker-1.1 requires LibStub") wobin@10: assert(LibStub:GetLibrary("CallbackHandler-1.0", true), "LibDataBroker-1.1 requires CallbackHandler-1.0") wobin@10: wobin@10: local lib, oldminor = LibStub:NewLibrary("LibDataBroker-1.1", 4) wobin@10: if not lib then return end wobin@10: oldminor = oldminor or 0 wobin@10: wobin@10: wobin@10: lib.callbacks = lib.callbacks or LibStub:GetLibrary("CallbackHandler-1.0"):New(lib) wobin@10: lib.attributestorage, lib.namestorage, lib.proxystorage = lib.attributestorage or {}, lib.namestorage or {}, lib.proxystorage or {} wobin@10: local attributestorage, namestorage, callbacks = lib.attributestorage, lib.namestorage, lib.callbacks wobin@10: wobin@10: if oldminor < 2 then wobin@10: lib.domt = { wobin@10: __metatable = "access denied", wobin@10: __index = function(self, key) return attributestorage[self] and attributestorage[self][key] end, wobin@10: } wobin@10: end wobin@10: wobin@10: if oldminor < 3 then wobin@10: lib.domt.__newindex = function(self, key, value) wobin@10: if not attributestorage[self] then attributestorage[self] = {} end wobin@10: if attributestorage[self][key] == value then return end wobin@10: attributestorage[self][key] = value wobin@10: local name = namestorage[self] wobin@10: if not name then return end wobin@10: callbacks:Fire("LibDataBroker_AttributeChanged", name, key, value, self) wobin@10: callbacks:Fire("LibDataBroker_AttributeChanged_"..name, name, key, value, self) wobin@10: callbacks:Fire("LibDataBroker_AttributeChanged_"..name.."_"..key, name, key, value, self) wobin@10: callbacks:Fire("LibDataBroker_AttributeChanged__"..key, name, key, value, self) wobin@10: end wobin@10: end wobin@10: wobin@10: if oldminor < 2 then wobin@10: function lib:NewDataObject(name, dataobj) wobin@10: if self.proxystorage[name] then return end wobin@10: wobin@10: if dataobj then wobin@10: assert(type(dataobj) == "table", "Invalid dataobj, must be nil or a table") wobin@10: self.attributestorage[dataobj] = {} wobin@10: for i,v in pairs(dataobj) do wobin@10: self.attributestorage[dataobj][i] = v wobin@10: dataobj[i] = nil wobin@10: end wobin@10: end wobin@10: dataobj = setmetatable(dataobj or {}, self.domt) wobin@10: self.proxystorage[name], self.namestorage[dataobj] = dataobj, name wobin@10: self.callbacks:Fire("LibDataBroker_DataObjectCreated", name, dataobj) wobin@10: return dataobj wobin@10: end wobin@10: end wobin@10: wobin@10: if oldminor < 1 then wobin@10: function lib:DataObjectIterator() wobin@10: return pairs(self.proxystorage) wobin@10: end wobin@10: wobin@10: function lib:GetDataObjectByName(dataobjectname) wobin@10: return self.proxystorage[dataobjectname] wobin@10: end wobin@10: wobin@10: function lib:GetNameByDataObject(dataobject) wobin@10: return self.namestorage[dataobject] wobin@10: end wobin@10: end wobin@10: wobin@10: if oldminor < 4 then wobin@10: local next = pairs(attributestorage) wobin@10: function lib:pairs(dataobject_or_name) wobin@10: local t = type(dataobject_or_name) wobin@10: assert(t == "string" or t == "table", "Usage: ldb:pairs('dataobjectname') or ldb:pairs(dataobject)") wobin@10: wobin@10: local dataobj = self.proxystorage[dataobject_or_name] or dataobject_or_name wobin@10: assert(attributestorage[dataobj], "Data object not found") wobin@10: wobin@10: return next, attributestorage[dataobj], nil wobin@10: end wobin@10: wobin@10: local ipairs_iter = ipairs(attributestorage) wobin@10: function lib:ipairs(dataobject_or_name) wobin@10: local t = type(dataobject_or_name) wobin@10: assert(t == "string" or t == "table", "Usage: ldb:ipairs('dataobjectname') or ldb:ipairs(dataobject)") wobin@10: wobin@10: local dataobj = self.proxystorage[dataobject_or_name] or dataobject_or_name wobin@10: assert(attributestorage[dataobj], "Data object not found") wobin@10: wobin@10: return ipairs_iter, attributestorage[dataobj], 0 wobin@10: end wobin@10: end