LibElvUIPlugin-1.0.lua 8.81 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 68
function lib:RegisterPlugin(name, callback, isLib)
	if not ElvUI then return end -- lol?
69
	local E = ElvUI[1]
70

Elv's avatar
Elv committed
71 72
    local plugin = {}
	plugin.name = name
Simpy's avatar
Simpy committed
73
	plugin.version = name == MAJOR and MINOR or GetAddOnMetadata(name, 'Version')
Elv's avatar
Elv committed
74 75 76
	if isLib then plugin.isLib = true; plugin.version = 1 end
	plugin.callback = callback
	lib.plugins[name] = plugin
Simpy's avatar
Simpy committed
77
	local loaded = IsAddOnLoaded('ElvUI_Config')
Elv's avatar
Elv committed
78

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

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

Elv's avatar
Elv committed
95 96 97 98 99 100
		callback()
	end

	return plugin
end

101
local function SendVersionCheckMessage()
102 103 104 105 106 107 108
	lib:SendPluginVersionCheck(lib:GenerateVersionCheckMessage())
end

function lib:DelayedSendVersionCheck(delay)
	local E = ElvUI[1]

	if not E.SendPluginVersionCheck then
109
		ElvUI[1].SendPluginVersionCheck = SendVersionCheckMessage
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
	end

	if not lib.SendMessageTimer then
		lib.SendMessageTimer = E:ScheduleTimer('SendPluginVersionCheck', delay or 10)
	end
end

function lib:ConfigLoaded(_, _, addon)
	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
137
function lib:GetPluginOptions()
138
	ElvUI[1].Options.args.plugins = {
Elv's avatar
Elv committed
139
        order = -10,
Simpy's avatar
Simpy committed
140
        type = 'group',
Elv's avatar
Elv committed
141 142 143 144 145
        name = HDR_CONFIG,
        guiInline = false,
        args = {
            pluginheader = {
                order = 1,
Simpy's avatar
Simpy committed
146
                type = 'header',
Elv's avatar
Elv committed
147 148 149 150
                name = format(HDR_INFORMATION, MINOR),
            },
            plugins = {
                order = 2,
Simpy's avatar
Simpy committed
151
                type = 'description',
Elv's avatar
Elv committed
152 153 154 155 156 157
                name = lib:GeneratePluginList(),
            },
        }
    }
end

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

164
		if not E.pluginRecievedOutOfDateMessage then
165
			local name, version, plugin, Pname
Simpy's avatar
Simpy committed
166 167 168
			for _, p in pairs({strsplit(';',message)}) do
				if not strmatch(p, '^%s-$') then
					name, version = strmatch(p, '([%w_]+)=([%d%p]+)')
Simpy's avatar
Simpy committed
169
					if lib.plugins[name] then
170
						plugin = lib.plugins[name]
171 172
						if (version ~= nil and plugin.version ~= nil and plugin.version ~= 'BETA') and (tonumber(version) ~= nil and tonumber(plugin.version) ~= nil) and (tonumber(version) > tonumber(plugin.version)) then
							plugin.old, plugin.newversion = true, tonumber(version)
Simpy's avatar
Simpy committed
173
							Pname = GetAddOnMetadata(plugin.name, 'Title')
174
							E:Print(format(MSG_OUTDATED,Pname,plugin.version,plugin.newversion))
175
							ElvUI[1].pluginRecievedOutOfDateMessage = true
Simpy's avatar
Simpy committed
176
						end
Elv's avatar
Elv committed
177 178 179 180
					end
				end
			end
		end
Simpy's avatar
Simpy committed
181
	elseif event == 'GROUP_ROSTER_UPDATE' then
Simpy's avatar
Simpy committed
182 183 184
		local num = GetNumGroupMembers()
		if num ~= lib.groupSize then
			if num > 1 and num > lib.groupSize then
185
				lib:DelayedSendVersionCheck()
Simpy's avatar
Simpy committed
186 187 188
			end
			lib.groupSize = num
		end
189 190
	elseif event == 'PLAYER_ENTERING_WORLD' then
		lib:DelayedSendVersionCheck(15)
Simpy's avatar
Simpy committed
191
	end
Elv's avatar
Elv committed
192 193 194
end

function lib:GeneratePluginList()
Simpy's avatar
Simpy committed
195
	local E, list = ElvUI[1], ''
196
	local author, Pname, color
Elv's avatar
Elv committed
197 198
	for _, plugin in pairs(lib.plugins) do
		if plugin.name ~= MAJOR then
Simpy's avatar
Simpy committed
199 200
			author = GetAddOnMetadata(plugin.name, 'Author')
			Pname = GetAddOnMetadata(plugin.name, 'Title') or plugin.name
201
			color = plugin.old and E:RGBToHex(1,0,0) or E:RGBToHex(0,1,0)
Simpy's avatar
Simpy committed
202 203 204 205 206
			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
207 208 209 210 211
		end
	end
	return list
end

Simpy's avatar
Simpy committed
212
function lib:ClearSendMessageTimer()
213 214 215
	lib.SendMessageTimer = nil
end

Elv's avatar
Elv committed
216
function lib:SendPluginVersionCheck(message)
Simpy's avatar
Simpy committed
217
	if (not message) or strmatch(message, '^%s-$') then
218 219 220 221
		lib.ClearSendMessageTimer()
		return
	end

222
	local ChatType, Channel
Simpy's avatar
Simpy committed
223
	if IsInRaid() then
Simpy's avatar
Simpy committed
224
		ChatType = (not IsInRaid(LE_PARTY_CATEGORY_HOME) and IsInRaid(LE_PARTY_CATEGORY_INSTANCE)) and 'INSTANCE_CHAT' or 'RAID'
Simpy's avatar
Simpy committed
225
	elseif IsInGroup() then
Simpy's avatar
Simpy committed
226
		ChatType = (not IsInGroup(LE_PARTY_CATEGORY_HOME) and IsInGroup(LE_PARTY_CATEGORY_INSTANCE)) and 'INSTANCE_CHAT' or 'PARTY'
Simpy's avatar
Simpy committed
227 228 229
	else
		local ElvUIGVC = GetChannelName('ElvUIGVC')
		if ElvUIGVC and ElvUIGVC > 0 then
Simpy's avatar
Simpy committed
230
			ChatType, Channel = 'CHANNEL', ElvUIGVC
Simpy's avatar
Simpy committed
231
		elseif IsInGuild() then
Simpy's avatar
Simpy committed
232
			ChatType = 'GUILD'
Simpy's avatar
Simpy committed
233
		end
234
	end
235

236
	if not ChatType then
Simpy's avatar
Simpy committed
237
		lib.ClearSendMessageTimer()
238 239
		return
	end
240

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

259 260 261 262 263 264
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
265
lib:RegisterPlugin(MAJOR, lib.GetPluginOptions)