-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathaddon.lua
189 lines (150 loc) · 5.98 KB
/
addon.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
local detailsFramework = _G ["DetailsFramework"]
if (not detailsFramework or not DetailsFrameworkCanLoad) then
return
end
local _
local CONST_DEFAULT_PROFILE_NAME = "default"
---@class df_addon : table
---@field __name string the addon toc name
---@field __savedGlobalVarsName string the name of the global saved variables
---@field __savedVarsDefaultTemplate table the default template for the saved variables
---@field __frame frame a frame to use for events
---@field OnLoaded fun(addon:df_addon, profileTable:table) runs when the addon is loaded at event "ADDON_LOADED"
---@field OnInit fun(addon:df_addon, profileTable:table) runs when the addon is initialized at event "PLAYER_LOGIN"
---@field OnProfileChanged fun(addon:df_addon, profileTable:table) runs when the profile is changed
--runs when the addon received addon_loaded
local addonLoaded = function(addonFrame, event, addonName)
if (addonName ~= addonFrame.__name) then
return
end
local addonObject = addonFrame.__addonObject
if (not addonObject.__savedGlobalVarsName) then
if (addonObject.OnLoad) then
detailsFramework:Dispatch(addonObject.OnLoad, addonObject)
end
return
end
local playerGUID = UnitGUID("player") --the guid points to a profile name
---@type table
local tSavedVariables = detailsFramework.SavedVars.GetSavedVariables(addonObject)
--check if the player has a profileId saved
local profileId = tSavedVariables.profile_ids[playerGUID]
if (not profileId) then
--if it doesn't, set it to use the default profile
profileId = CONST_DEFAULT_PROFILE_NAME
tSavedVariables.profile_ids[playerGUID] = profileId
end
local bCreateIfNotFound = true
local profileTable = detailsFramework.SavedVars.GetProfile(addonObject, bCreateIfNotFound)
addonObject.profile = profileTable
if (addonObject.OnLoad) then
detailsFramework:Dispatch(addonObject.OnLoad, addonObject, addonObject.profile, true)
end
end
--runs when the addon received PLAYER_LOGIN
local addonInit = function(addonFrame)
local addonObject = addonFrame.__addonObject
if (addonObject.OnInit) then
detailsFramework:Dispatch(addonObject.OnInit, addonObject, addonObject.profile)
end
end
--when the player logout or reloadUI
local addonUnload = function(addonFrame)
local addonObject = addonFrame.__addonObject
local bOkay, errortext = pcall(detailsFramework.SavedVars.SaveProfile, addonObject)
if (not bOkay) then
if (addonFrame.logoutLogs) then
table.insert(addonFrame.logoutLogs, 1, date("%a %b %d %H:%M:%S %Y") .. "|LOGOUT error:" .. errortext)
table.remove(addonFrame.logoutLogs, 3)
end
end
end
local addonEvents = {
["ADDON_LOADED"] = addonLoaded,
["PLAYER_LOGIN"] = addonInit,
["PLAYER_LOGOUT"] = addonUnload,
}
local addonOnEvent = function(addonFrame, event, ...)
local func = addonEvents[event]
if (func) then
func(addonFrame, event, ...)
else
--might be a registered event from the user
if (addonFrame[event]) then
detailsFramework:CoreDispatch(addonFrame.__name, addonFrame[event], addonFrame, event, ...)
end
end
end
detailsFramework.AddonMixin = {
}
--log erros during the save data
local setLogoutLogTable = function(addonObject, logTable)
addonObject.__frame.logoutLogs = logTable
end
---create an addon object
---@param addonName addonname
---@param globalSavedVariablesName string
---@param savedVarsTemplate table
---@return frame
function detailsFramework:CreateNewAddOn(addonName, globalSavedVariablesName, savedVarsTemplate)
local newAddonObject = {}
---@type frame
local addonFrame = CreateFrame("frame")
newAddonObject.__name = addonName
newAddonObject.__savedGlobalVarsName = globalSavedVariablesName
newAddonObject.__savedVarsDefaultTemplate = savedVarsTemplate or {}
newAddonObject.__frame = addonFrame
addonFrame.__name = addonName
addonFrame.__savedGlobalVarsName = globalSavedVariablesName
addonFrame.__savedVarsDefaultTemplate = newAddonObject.__savedVarsDefaultTemplate
addonFrame.__addonObject = newAddonObject
addonFrame:RegisterEvent("ADDON_LOADED")
addonFrame:RegisterEvent("PLAYER_LOGIN")
addonFrame:RegisterEvent("PLAYER_LOGOUT")
addonFrame:SetScript("OnEvent", addonOnEvent)
newAddonObject.SetLogoutLogTable = setLogoutLogTable
return newAddonObject
end
--old create addon using ace3
function detailsFramework:CreateAddOn(name, global_saved, global_table, options_table, broker)
local addon = LibStub("AceAddon-3.0"):NewAddon (name, "AceConsole-3.0", "AceEvent-3.0", "AceTimer-3.0", "DetailsFramework-1.0", "AceComm-3.0")
_G [name] = addon
addon.__name = name
function addon:OnInitialize()
if (global_saved) then
if (broker and broker.Minimap and not global_table.Minimap) then
detailsFramework:Msg(name, "broker.Minimap is true but no global.Minimap declared.")
end
self.db = LibStub("AceDB-3.0"):New (global_saved, global_table or {}, true)
end
if (options_table) then
LibStub("AceConfig-3.0"):RegisterOptionsTable (name, options_table)
addon.OptionsFrame1 = LibStub("AceConfigDialog-3.0"):AddToBlizOptions (name, name)
LibStub("AceConfig-3.0"):RegisterOptionsTable (name .. "-Profiles", LibStub("AceDBOptions-3.0"):GetOptionsTable (self.db))
addon.OptionsFrame2 = LibStub("AceConfigDialog-3.0"):AddToBlizOptions (name .. "-Profiles", "Profiles", name)
end
if (broker) then
local broker_click_function = broker.OnClick
if (not broker_click_function and options_table) then
broker_click_function = function()
InterfaceOptionsFrame_OpenToCategory (name)
InterfaceOptionsFrame_OpenToCategory (name)
end
end
local databroker = LibStub("LibDataBroker-1.1"):NewDataObject (name, {
type = broker.type or "launcher",
icon = broker.icon or [[Interface\PvPRankBadges\PvPRank15]],
text = broker.text or "",
OnTooltipShow = broker.OnTooltipShow,
OnClick = broker_click_function
})
if (databroker and broker.Minimap and global_table.Minimap) then
LibStub("LibDBIcon-1.0"):Register (name, databroker, addon.db.profile.Minimap)
end
end
if (addon.OnInit) then
xpcall(addon.OnInit, geterrorhandler(), addon)
end
end
return addon
end