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