wobin@10
|
1
|
wobin@10
|
2 assert(LibStub, "LibDataBroker-1.1 requires LibStub")
|
wobin@10
|
3 assert(LibStub:GetLibrary("CallbackHandler-1.0", true), "LibDataBroker-1.1 requires CallbackHandler-1.0")
|
wobin@10
|
4
|
wobin@10
|
5 local lib, oldminor = LibStub:NewLibrary("LibDataBroker-1.1", 4)
|
wobin@10
|
6 if not lib then return end
|
wobin@10
|
7 oldminor = oldminor or 0
|
wobin@10
|
8
|
wobin@10
|
9
|
wobin@10
|
10 lib.callbacks = lib.callbacks or LibStub:GetLibrary("CallbackHandler-1.0"):New(lib)
|
wobin@10
|
11 lib.attributestorage, lib.namestorage, lib.proxystorage = lib.attributestorage or {}, lib.namestorage or {}, lib.proxystorage or {}
|
wobin@10
|
12 local attributestorage, namestorage, callbacks = lib.attributestorage, lib.namestorage, lib.callbacks
|
wobin@10
|
13
|
wobin@10
|
14 if oldminor < 2 then
|
wobin@10
|
15 lib.domt = {
|
wobin@10
|
16 __metatable = "access denied",
|
wobin@10
|
17 __index = function(self, key) return attributestorage[self] and attributestorage[self][key] end,
|
wobin@10
|
18 }
|
wobin@10
|
19 end
|
wobin@10
|
20
|
wobin@10
|
21 if oldminor < 3 then
|
wobin@10
|
22 lib.domt.__newindex = function(self, key, value)
|
wobin@10
|
23 if not attributestorage[self] then attributestorage[self] = {} end
|
wobin@10
|
24 if attributestorage[self][key] == value then return end
|
wobin@10
|
25 attributestorage[self][key] = value
|
wobin@10
|
26 local name = namestorage[self]
|
wobin@10
|
27 if not name then return end
|
wobin@10
|
28 callbacks:Fire("LibDataBroker_AttributeChanged", name, key, value, self)
|
wobin@10
|
29 callbacks:Fire("LibDataBroker_AttributeChanged_"..name, name, key, value, self)
|
wobin@10
|
30 callbacks:Fire("LibDataBroker_AttributeChanged_"..name.."_"..key, name, key, value, self)
|
wobin@10
|
31 callbacks:Fire("LibDataBroker_AttributeChanged__"..key, name, key, value, self)
|
wobin@10
|
32 end
|
wobin@10
|
33 end
|
wobin@10
|
34
|
wobin@10
|
35 if oldminor < 2 then
|
wobin@10
|
36 function lib:NewDataObject(name, dataobj)
|
wobin@10
|
37 if self.proxystorage[name] then return end
|
wobin@10
|
38
|
wobin@10
|
39 if dataobj then
|
wobin@10
|
40 assert(type(dataobj) == "table", "Invalid dataobj, must be nil or a table")
|
wobin@10
|
41 self.attributestorage[dataobj] = {}
|
wobin@10
|
42 for i,v in pairs(dataobj) do
|
wobin@10
|
43 self.attributestorage[dataobj][i] = v
|
wobin@10
|
44 dataobj[i] = nil
|
wobin@10
|
45 end
|
wobin@10
|
46 end
|
wobin@10
|
47 dataobj = setmetatable(dataobj or {}, self.domt)
|
wobin@10
|
48 self.proxystorage[name], self.namestorage[dataobj] = dataobj, name
|
wobin@10
|
49 self.callbacks:Fire("LibDataBroker_DataObjectCreated", name, dataobj)
|
wobin@10
|
50 return dataobj
|
wobin@10
|
51 end
|
wobin@10
|
52 end
|
wobin@10
|
53
|
wobin@10
|
54 if oldminor < 1 then
|
wobin@10
|
55 function lib:DataObjectIterator()
|
wobin@10
|
56 return pairs(self.proxystorage)
|
wobin@10
|
57 end
|
wobin@10
|
58
|
wobin@10
|
59 function lib:GetDataObjectByName(dataobjectname)
|
wobin@10
|
60 return self.proxystorage[dataobjectname]
|
wobin@10
|
61 end
|
wobin@10
|
62
|
wobin@10
|
63 function lib:GetNameByDataObject(dataobject)
|
wobin@10
|
64 return self.namestorage[dataobject]
|
wobin@10
|
65 end
|
wobin@10
|
66 end
|
wobin@10
|
67
|
wobin@10
|
68 if oldminor < 4 then
|
wobin@10
|
69 local next = pairs(attributestorage)
|
wobin@10
|
70 function lib:pairs(dataobject_or_name)
|
wobin@10
|
71 local t = type(dataobject_or_name)
|
wobin@10
|
72 assert(t == "string" or t == "table", "Usage: ldb:pairs('dataobjectname') or ldb:pairs(dataobject)")
|
wobin@10
|
73
|
wobin@10
|
74 local dataobj = self.proxystorage[dataobject_or_name] or dataobject_or_name
|
wobin@10
|
75 assert(attributestorage[dataobj], "Data object not found")
|
wobin@10
|
76
|
wobin@10
|
77 return next, attributestorage[dataobj], nil
|
wobin@10
|
78 end
|
wobin@10
|
79
|
wobin@10
|
80 local ipairs_iter = ipairs(attributestorage)
|
wobin@10
|
81 function lib:ipairs(dataobject_or_name)
|
wobin@10
|
82 local t = type(dataobject_or_name)
|
wobin@10
|
83 assert(t == "string" or t == "table", "Usage: ldb:ipairs('dataobjectname') or ldb:ipairs(dataobject)")
|
wobin@10
|
84
|
wobin@10
|
85 local dataobj = self.proxystorage[dataobject_or_name] or dataobject_or_name
|
wobin@10
|
86 assert(attributestorage[dataobj], "Data object not found")
|
wobin@10
|
87
|
wobin@10
|
88 return ipairs_iter, attributestorage[dataobj], 0
|
wobin@10
|
89 end
|
wobin@10
|
90 end
|