LibElvUIPlugin-1.0.lua 8.67 KB
Newer Older
Simpy's avatar
Simpy committed
1
local MAJOR, MINOR = 'LibElvUIPlugin-1.0', 22
Elv's avatar
Elv committed
2 3 4 5 6
local lib, oldminor = LibStub:NewLibrary(MAJOR, MINOR)
if not lib then return end

--Cache global variables
--Lua functions
7 8
local pairs, tonumber, strmatch, strsub = pairs, tonumber, strmatch, strsub
local format, strsplit, strlen, gsub, ceil = format, strsplit, strlen, gsub, ceil
Elv's avatar
Elv committed
9
--WoW API / Variables
Simpy's avatar
Simpy committed
10
local GetNumGroupMembers = GetNumGroupMembers
11
local GetLocale, IsInGuild = GetLocale, IsInGuild
12 13
local CreateFrame, IsAddOnLoaded = CreateFrame, IsAddOnLoaded
local GetAddOnMetadata, GetChannelName = GetAddOnMetadata, GetChannelName
Simpy's avatar
Simpy committed
14 15 16
local IsInRaid, IsInGroup = IsInRaid, IsInGroup
local LE_PARTY_CATEGORY_HOME = LE_PARTY_CATEGORY_HOME
local LE_PARTY_CATEGORY_INSTANCE = LE_PARTY_CATEGORY_INSTANCE
17
local C_ChatInfo_RegisterAddonMessagePrefix = C_ChatInfo.RegisterAddonMessagePrefix
Simpy's avatar
Simpy committed
18
local C_ChatInfo_SendAddonMessage = C_ChatInfo.SendAddonMessage
Elv's avatar
Elv committed
19 20 21 22 23
--Global variables that we don't cache, list them here for the mikk's Find Globals script
-- GLOBALS: ElvUI

lib.plugins = {}
lib.index = 0
24
lib.groupSize = 0
Simpy's avatar
Simpy committed
25
lib.prefix = 'ElvUIPluginVC'
26
------------------------------
27
local MSG_OUTDATED = "Your version of %s %s is out of date (latest is version %s). You can download the latest version from http://www.tukui.org"
Elv's avatar
Elv committed
28 29 30 31 32 33 34
local HDR_CONFIG = "Plugins"
local HDR_INFORMATION = "LibElvUIPlugin-1.0.%d - Plugins Loaded  (Green means you have current version, Red means out of date)"
local INFO_BY = "by"
local INFO_VERSION = "Version:"
local INFO_NEW = "Newest:"
local LIBRARY = "Library"

35
if GetLocale() == "deDE" then
36
	MSG_OUTDATED = "Deine Version von %s %s ist veraltet (akutelle Version ist %s). Du kannst die aktuelle Version von http://www.tukui.org herunterrladen."
Elv's avatar
Elv committed
37 38 39 40 41 42 43 44
	HDR_CONFIG = "Plugins"
	HDR_INFORMATION = "LibElvUIPlugin-1.0.%d - Plugins geladen (Grün bedeutet du hast die aktuelle Version, Rot bedeutet es ist veraltet)"
	INFO_BY = "von"
	INFO_VERSION = "Version:"
	INFO_NEW = "Neuste:"
	LIBRARY = "Bibliothek"
end

45
if GetLocale() == "ruRU" then
46
	MSG_OUTDATED = "Ваша версия %s %s устарела (последняя версия %s). Вы можете скачать последнюю версию на http://www.tukui.org"
Elv's avatar
Elv committed
47 48 49 50 51 52 53
	HDR_CONFIG = "Плагины"
	HDR_INFORMATION = "LibElvUIPlugin-1.0.%d - загруженные плагины (зеленый означает, что у вас последняя версия, красный - устаревшая)"
	INFO_BY = "от"
	INFO_VERSION = "Версия:"
	INFO_NEW = "Последняя:"
	LIBRARY = "Библиотека"
end
54
------------------------------
Simpy's avatar
Simpy committed
55 56 57 58 59 60 61 62 63 64 65
--
-- Plugin table format:
--   { name (string) - The name of the plugin,
--     version (string) - The version of the plugin,
--     optionCallback (string) - The callback to call when ElvUI_Config is loaded
--   }
--
--
-- RegisterPlugin(name,callback)
--   Registers a module with the given name and option callback, pulls version info from metadata
--
66
------------------------------
67
function lib:RegisterPlugin(name, callback, isLib)
Simpy's avatar
Simpy committed
68 69 70 71 72 73 74 75 76 77 78
    local plugin = {
		name = name,
		callback = callback,
		version = name == MAJOR and MINOR or GetAddOnMetadata(name, 'Version')
	}

	if isLib then
		plugin.isLib = true
		plugin.version = 1
	end

Elv's avatar
Elv committed
79 80
	lib.plugins[name] = plugin

81
	if not lib.registeredPrefix then
82
		C_ChatInfo_RegisterAddonMessagePrefix(lib.prefix)
83 84
		lib.VCFrame:RegisterEvent('CHAT_MSG_ADDON')
		lib.VCFrame:RegisterEvent('GROUP_ROSTER_UPDATE')
85
		lib.VCFrame:RegisterEvent('PLAYER_ENTERING_WORLD')
86
		lib.registeredPrefix = true
Elv's avatar
Elv committed
87 88
	end

Simpy's avatar
Simpy committed
89
	local loaded = IsAddOnLoaded('ElvUI_Config')
Elv's avatar
Elv committed
90
	if not loaded then
91
		lib.CFFrame:RegisterEvent('ADDON_LOADED')
Elv's avatar
Elv committed
92 93 94
	elseif loaded then
		-- Need to update plugins list
		if name ~= MAJOR then
95
			ElvUI[1].Options.args.plugins.args.plugins.name = lib:GeneratePluginList()
Elv's avatar
Elv committed
96
		end
Simpy's avatar
Simpy committed
97

Elv's avatar
Elv committed
98 99 100 101 102 103
		callback()
	end

	return plugin
end

104
local function SendVersionCheckMessage()
105 106 107
	lib:SendPluginVersionCheck(lib:GenerateVersionCheckMessage())
end

Simpy's avatar
Simpy committed
108
function lib:DelayedSendVersionCheck(delay)
109 110 111
	local E = ElvUI[1]

	if not E.SendPluginVersionCheck then
112
		ElvUI[1].SendPluginVersionCheck = SendVersionCheckMessage
113 114
	end

115
	if not lib.SendMessageWaiting then
Simpy's avatar
Simpy committed
116
		lib.SendMessageWaiting = E:Delay(delay or 10, ElvUI[1].SendPluginVersionCheck)
117 118 119
	end
end

120
function lib:ConfigLoaded(_, addon)
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
	if addon == 'ElvUI_Config' then
		for _, plugin in pairs(lib.plugins) do
			if plugin.callback then
				plugin.callback()
			end
		end
	end
end

function lib:GenerateVersionCheckMessage()
	local list = ''
	for _, plugin in pairs(lib.plugins) do
		if plugin.name ~= MAJOR then
			list = list..plugin.name..'='..plugin.version..';'
		end
	end
	return list
end

Elv's avatar
Elv committed
140
function lib:GetPluginOptions()
141
	ElvUI[1].Options.args.plugins = {
Elv's avatar
Elv committed
142
        order = -10,
Simpy's avatar
Simpy committed
143
        type = 'group',
Elv's avatar
Elv committed
144 145 146 147 148
        name = HDR_CONFIG,
        guiInline = false,
        args = {
            pluginheader = {
                order = 1,
Simpy's avatar
Simpy committed
149
                type = 'header',
Elv's avatar
Elv committed
150 151 152 153
                name = format(HDR_INFORMATION, MINOR),
            },
            plugins = {
                order = 2,
Simpy's avatar
Simpy committed
154
                type = 'description',
Elv's avatar
Elv committed
155 156 157 158 159 160
                name = lib:GeneratePluginList(),
            },
        }
    }
end

161
function lib:VersionCheck(event, prefix, message, _, sender)
162
	local E = ElvUI[1]
Simpy's avatar
Simpy committed
163
	if (event == 'CHAT_MSG_ADDON' and prefix == lib.prefix) and (sender and message and not strmatch(message, '^%s-$')) then
164
		if not lib.myName then lib.myName = E.myname..'-'..gsub(E.myrealm,'[%s%-]','') end
165 166
		if sender == lib.myName then return end

167
		if not E.pluginRecievedOutOfDateMessage then
Simpy's avatar
Simpy committed
168
			local name, version, plugin, Pname, Pver, ver
Simpy's avatar
Simpy committed
169 170 171
			for _, p in pairs({strsplit(';',message)}) do
				if not strmatch(p, '^%s-$') then
					name, version = strmatch(p, '([%w_]+)=([%d%p]+)')
172 173 174 175 176
					plugin = name and lib.plugins[name]

					if version and plugin and plugin.version and (plugin.version ~= 'BETA') then
						Pver, ver = tonumber(plugin.version), tonumber(version)
						if (ver and Pver) and (ver > Pver) then
Simpy's avatar
Simpy committed
177
							plugin.old, plugin.newversion = true, ver
178
							Pname = GetAddOnMetadata(plugin.name, 'Title')
179
							E:Print(format(MSG_OUTDATED,Pname,plugin.version,plugin.newversion))
180
							ElvUI[1].pluginRecievedOutOfDateMessage = true
Simpy's avatar
Simpy committed
181
						end
Elv's avatar
Elv committed
182 183 184 185
					end
				end
			end
		end
Simpy's avatar
Simpy committed
186
	elseif event == 'GROUP_ROSTER_UPDATE' then
Simpy's avatar
Simpy committed
187 188 189
		local num = GetNumGroupMembers()
		if num ~= lib.groupSize then
			if num > 1 and num > lib.groupSize then
190
				lib:DelayedSendVersionCheck()
Simpy's avatar
Simpy committed
191 192 193
			end
			lib.groupSize = num
		end
194 195
	elseif event == 'PLAYER_ENTERING_WORLD' then
		lib:DelayedSendVersionCheck()
Simpy's avatar
Simpy committed
196
	end
Elv's avatar
Elv committed
197 198 199
end

function lib:GeneratePluginList()
Simpy's avatar
Simpy committed
200
	local E, list = ElvUI[1], ''
201
	local author, Pname, color
Elv's avatar
Elv committed
202 203
	for _, plugin in pairs(lib.plugins) do
		if plugin.name ~= MAJOR then
Simpy's avatar
Simpy committed
204 205
			author = GetAddOnMetadata(plugin.name, 'Author')
			Pname = GetAddOnMetadata(plugin.name, 'Title') or plugin.name
206
			color = plugin.old and E:RGBToHex(1,0,0) or E:RGBToHex(0,1,0)
Simpy's avatar
Simpy committed
207 208 209 210 211
			list = list..Pname
			if author then list = list..' '..INFO_BY..' '..author end
			list = list..color..(plugin.isLib and ' '..LIBRARY or ' - '..INFO_VERSION..' '..plugin.version)
			if plugin.old then list = list..INFO_NEW..plugin.newversion..')' end
			list = list..'|r\n'
Elv's avatar
Elv committed
212 213 214 215 216
		end
	end
	return list
end

217 218
function lib:ClearSendMessageWait()
	lib.SendMessageWaiting = nil
219 220
end

Elv's avatar
Elv committed
221
function lib:SendPluginVersionCheck(message)
Simpy's avatar
Simpy committed
222
	if (not message) or strmatch(message, '^%s-$') then
223
		lib.ClearSendMessageWait()
224 225 226
		return
	end

Azilroka's avatar
Azilroka committed
227
	local ChatType, Channel
Simpy's avatar
Simpy committed
228
	if IsInRaid() then
Simpy's avatar
Simpy committed
229
		ChatType = (not IsInRaid(LE_PARTY_CATEGORY_HOME) and IsInRaid(LE_PARTY_CATEGORY_INSTANCE)) and 'INSTANCE_CHAT' or 'RAID'
Simpy's avatar
Simpy committed
230
	elseif IsInGroup() then
Simpy's avatar
Simpy committed
231
		ChatType = (not IsInGroup(LE_PARTY_CATEGORY_HOME) and IsInGroup(LE_PARTY_CATEGORY_INSTANCE)) and 'INSTANCE_CHAT' or 'PARTY'
Azilroka's avatar
Azilroka committed
232
	else
Azilroka's avatar
Azilroka committed
233 234 235
		local ElvUIGVC = GetChannelName('ElvUIGVC')
		if ElvUIGVC and ElvUIGVC > 0 then
			ChatType, Channel = 'CHANNEL', ElvUIGVC
Azilroka's avatar
Azilroka committed
236 237 238
		elseif IsInGuild() then
			ChatType = 'GUILD'
		end
239
	end
240

241
	if not ChatType then
242
		lib.ClearSendMessageWait()
243 244
		return
	end
245

246
	local delay, maxChar, msgLength = 0, 250, strlen(message)
247
	if msgLength > maxChar then
248
		local E, splitMessage = ElvUI[1]
249
		for _ = 1, ceil(msgLength/maxChar) do
250 251
			splitMessage = strmatch(strsub(message, 1, maxChar), '.+;')
			if splitMessage then -- incase the string is over 250 but doesnt contain `;`
Simpy's avatar
Simpy committed
252
				message = gsub(message, '^'..gsub(splitMessage, '([%(%)%.%%%+%-%*%?%[%^%$])','%%%1'), '')
Azilroka's avatar
Azilroka committed
253
				E:Delay(delay, C_ChatInfo_SendAddonMessage, lib.prefix, splitMessage, ChatType, Channel)
254
				delay = delay + 1
Elv's avatar
Elv committed
255 256
			end
		end
257

258
		E:Delay(delay, lib.ClearSendMessageWait)
259
	else
Azilroka's avatar
Azilroka committed
260
		C_ChatInfo_SendAddonMessage(lib.prefix, message, ChatType, Channel)
261
		lib.ClearSendMessageWait()
Elv's avatar
Elv committed
262 263 264
	end
end

265 266 267 268 269 270
lib.VCFrame = CreateFrame('Frame')
lib.VCFrame:SetScript('OnEvent', lib.VersionCheck)

lib.CFFrame = CreateFrame('Frame')
lib.CFFrame:SetScript('OnEvent', lib.ConfigLoaded)

Simpy's avatar
Simpy committed
271
lib:RegisterPlugin(MAJOR, lib.GetPluginOptions)