-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCore.lua
131 lines (109 loc) · 3.19 KB
/
Core.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
local ADDON_NAME, SanluliUtils = ...
_G[ADDON_NAME] = SanluliUtils
local ADDON_MESSAGE_AFFIX = "|cffffffffSanluliUtils: |r"
local ADDON_LISTENER_SUFFIX = "Listener"
SanluliUtils.Locale = {} -- 本地化表
SanluliUtils.Modules = {} -- 模块表
local Listener = CreateFrame('Frame', ADDON_NAME .. ADDON_LISTENER_SUFFIX)
local EventListeners = {}
Listener:SetScript("OnEvent", function(frame, event, ...)
if EventListeners[event] then
for callback, func in pairs(EventListeners[event]) do
if func == 0 then
callback[event](callback, ...)
else
callback[func](callback, event, ...)
end
end
end
end)
-- 注册事件
function SanluliUtils:RegisterEvent(event, callback, func)
if func == nil then func = 0 end
if EventListeners[event] == nil then
Listener:RegisterEvent(event)
EventListeners[event] = { [callback]=func }
else
EventListeners[event][callback] = func
end
end
-- 取消注册事件
function SanluliUtils:UnregisterEvent(event, callback)
local listeners = EventListeners[event]
if listeners then
local count = 0
for index,_ in pairs(listeners) do
if index == callback then
listeners[index] = nil
else
count = count + 1
end
end
if count == 0 then
EventListeners[event] = nil
Listener:UnregisterEvent(event)
end
end
end
-- 获取配置项
function SanluliUtils:GetConfig(module, key)
if self.Database then
return self.Database[module..'.'..key]
end
end
-- 设置配置项
function SanluliUtils:SetConfig(module, key, value)
if self.Database then
self.Database[module..'.'..key] = value
end
end
-- 插件消息输出
function SanluliUtils:Print(text, r, g, b, ...)
r, g, b = r or 1, g or 1, b or 0
DEFAULT_CHAT_FRAME:AddMessage("|cffffffff" .. self.Locale["addon.name"] .. ": |r" .. tostring(text), r, g, b, ...)
end
-- 模块类
local ModulePrototype = {
RegisterEvent = function (self, event, func)
SanluliUtils:RegisterEvent(event, self, func)
end,
UnregisterEvent = function (self, event)
SanluliUtils:UnregisterEvent(event, self)
end,
GetConfig = function(self, key)
return SanluliUtils:GetConfig(self.name, key)
end,
SetConfig = function(self, key, value)
return SanluliUtils:SetConfig(self.name, key, value)
end
}
-- 新建模块
function SanluliUtils:NewModule(name)
local module = {}
self.Modules[name] = module
setmetatable(module, {__index = ModulePrototype})
module.name = name
return module
end
setmetatable(SanluliUtils, {__index = SanluliUtils.Modules})
function SanluliUtils:ForAllModules(event, ...)
for _, module in pairs(SanluliUtils.Modules) do
if type(module) == 'table' and module[event] then
module[event](module, ...)
end
end
end
SanluliUtils:RegisterEvent("ADDON_LOADED", SanluliUtils)
function SanluliUtils:ADDON_LOADED(addOnName, containsBindings)
if addOnName == ADDON_NAME then
SanluliUtilsDB = (type(SanluliUtilsDB) == "table" and SanluliUtilsDB) or {}
self.Database = SanluliUtilsDB
self:ForAllModules('BeforeStartup')
self:ForAllModules('Startup')
end
end
SanluliUtils:RegisterEvent("PLAYER_ENTERING_WORLD", SanluliUtils)
function SanluliUtils:PLAYER_ENTERING_WORLD()
self:ForAllModules('AfterStartup')
self:UnregisterEvent("PLAYER_ENTERING_WORLD", SanluliUtils)
end