LibElvUIPlugin-1.0.lua 8.87 KB
Newer Older
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 24 25 26 27

--Global variables that we don't cache, list them here for the mikk's Find Globals script
-- GLOBALS: ElvUI

lib.plugins = {}
lib.index = 0
lib.prefix = "ElvUIPluginVC"

-- MULTI Language Support (Default Language: English)
28
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
29 30 31 32 33 34 35 36
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"

if GetLocale() == "deDE" then -- German Translation
37
	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
38 39 40 41 42 43 44 45 46
	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

if GetLocale() == "ruRU" then -- Russian Translations
47
	MSG_OUTDATED = "Ваша версия %s %s устарела (последняя версия %s). Вы можете скачать последнюю версию на http://www.tukui.org"
Elv's avatar
Elv committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
	HDR_CONFIG = "Плагины"
	HDR_INFORMATION = "LibElvUIPlugin-1.0.%d - загруженные плагины (зеленый означает, что у вас последняя версия, красный - устаревшая)"
	INFO_BY = "от"
	INFO_VERSION = "Версия:"
	INFO_NEW = "Последняя:"
	LIBRARY = "Библиотека"
end

--
-- 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
--

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
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

local function SendPluginVersionCheck()
	lib:SendPluginVersionCheck(lib:GenerateVersionCheckMessage())
end

function lib:RegisterPlugin(name, callback, isLib)
	if not ElvUI then return end -- lol?
	lib.E = ElvUI[1]

Elv's avatar
Elv committed
87 88 89 90 91 92 93 94 95
    local plugin = {}
	plugin.name = name
	plugin.version = name == MAJOR and MINOR or GetAddOnMetadata(name, "Version")
	if isLib then plugin.isLib = true; plugin.version = 1 end
	plugin.callback = callback
	lib.plugins[name] = plugin
	local loaded = IsAddOnLoaded("ElvUI_Config")

	if not lib.vcframe then
96
		C_ChatInfo_RegisterAddonMessagePrefix(lib.prefix)
Elv's avatar
Elv committed
97 98
		local f = CreateFrame('Frame')
		f:RegisterEvent("CHAT_MSG_ADDON")
Simpy's avatar
Simpy committed
99
		f:RegisterEvent("GROUP_ROSTER_UPDATE")
Elv's avatar
Elv committed
100 101 102 103
		f:SetScript('OnEvent', lib.VersionCheck)
		lib.vcframe = f
	end

104 105 106 107 108
	if not lib.delayedCheck then
		lib.E:Delay(10, SendPluginVersionCheck)
		lib.delayedCheck = true
	end

Elv's avatar
Elv committed
109 110 111 112
	if not loaded then
		if not lib.ConfigFrame then
			local configFrame = CreateFrame("Frame")
			configFrame:RegisterEvent("ADDON_LOADED")
113
			configFrame:SetScript("OnEvent", function(_, _, addon)
Elv's avatar
Elv committed
114
				if addon == "ElvUI_Config" then
115 116 117
					for _, PlugIn in pairs(lib.plugins) do
						if PlugIn.callback then
							PlugIn.callback()
Elv's avatar
Elv committed
118 119 120 121 122 123 124 125 126
						end
					end
				end
			end)
			lib.ConfigFrame = configFrame
		end
	elseif loaded then
		-- Need to update plugins list
		if name ~= MAJOR then
127
			lib.E.Options.args.plugins.args.plugins.name = lib:GeneratePluginList()
Elv's avatar
Elv committed
128 129 130 131 132 133 134 135
		end
		callback()
	end

	return plugin
end

function lib:GetPluginOptions()
136
	lib.E.Options.args.plugins = {
Elv's avatar
Elv committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
        order = -10,
        type = "group",
        name = HDR_CONFIG,
        guiInline = false,
        args = {
            pluginheader = {
                order = 1,
                type = "header",
                name = format(HDR_INFORMATION, MINOR),
            },
            plugins = {
                order = 2,
                type = "description",
                name = lib:GeneratePluginList(),
            },
        }
    }
end

156
function lib:VersionCheck(event, prefix, message, _, sender)
Simpy's avatar
Simpy committed
157
	if (event == "CHAT_MSG_ADDON" and prefix == lib.prefix) and (sender and message and not strmatch(message, "^%s-$")) then
158 159 160 161 162 163
		if not lib.myName then lib.myName = lib.E.myname..'-'..gsub(lib.E.myrealm,'[%s%-]','') end
		if sender == lib.myName then
			if lib.delayedCheck then
				lib.delayedCheck = nil
			end
			return
Elv's avatar
Elv committed
164
		end
165
		if not lib.E["pluginRecievedOutOfDateMessage"] then
166
			local name, version, plugin, Pname
Elv's avatar
Elv committed
167
			for _, p in pairs({strsplit(";",message)}) do
168 169
				if not strmatch(p, "^%s-$") then
					name, version = strmatch(p, "([%w_]+)=([%d%p]+)")
Simpy's avatar
Simpy committed
170
					if lib.plugins[name] then
171
						plugin = lib.plugins[name]
172 173
						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)
174
							Pname = GetAddOnMetadata(plugin.name, "Title")
175 176
							lib.E:Print(format(MSG_OUTDATED,Pname,plugin.version,plugin.newversion))
							lib.E["pluginRecievedOutOfDateMessage"] = true
Simpy's avatar
Simpy committed
177
						end
Elv's avatar
Elv committed
178 179 180 181
					end
				end
			end
		end
Simpy's avatar
Simpy committed
182 183 184 185 186 187 188 189 190 191
	elseif event == "GROUP_ROSTER_UPDATE" then
		local num = GetNumGroupMembers()
		if num ~= lib.groupSize then
			if num > 1 and num > lib.groupSize then
				if not lib.SendMessageTimer then
					lib.SendMessageTimer = lib.E:ScheduleTimer("SendPluginVersionCheck", 10)
				end
			end
			lib.groupSize = num
		end
Elv's avatar
Elv committed
192
	else
193 194
		if not lib.E.SendPluginVersionCheck then
			lib.E.SendPluginVersionCheck = SendPluginVersionCheck
195
		end
196

197 198 199
		if not lib.SendMessageTimer then
			lib.SendMessageTimer = lib.E:ScheduleTimer("SendPluginVersionCheck", 10)
		end
Simpy's avatar
Simpy committed
200
	end
Elv's avatar
Elv committed
201 202 203
end

function lib:GeneratePluginList()
204
	local list = ""
205
	local author, Pname, color
Elv's avatar
Elv committed
206 207
	for _, plugin in pairs(lib.plugins) do
		if plugin.name ~= MAJOR then
208 209
			author = GetAddOnMetadata(plugin.name, "Author")
			Pname = GetAddOnMetadata(plugin.name, "Title") or plugin.name
210
			color = plugin.old and lib.E:RGBToHex(1,0,0) or lib.E:RGBToHex(0,1,0)
Simpy's avatar
Simpy committed
211
			list = list .. Pname
Elv's avatar
Elv committed
212 213 214 215 216 217 218 219 220 221 222 223 224
			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"
		end
	end
	return list
end

Simpy's avatar
Simpy committed
225
function lib:ClearSendMessageTimer()
226 227 228
	lib.SendMessageTimer = nil
end

Elv's avatar
Elv committed
229
function lib:SendPluginVersionCheck(message)
230
	if (not message) or strmatch(message, "^%s-$") then return end
231
	local ChatType, Channel
232

Simpy's avatar
Simpy committed
233 234 235 236 237 238 239 240 241 242 243
	if IsInRaid() then
		ChatType = (not IsInRaid(LE_PARTY_CATEGORY_HOME) and IsInRaid(LE_PARTY_CATEGORY_INSTANCE)) and "INSTANCE_CHAT" or "RAID"
	elseif IsInGroup() then
		ChatType = (not IsInGroup(LE_PARTY_CATEGORY_HOME) and IsInGroup(LE_PARTY_CATEGORY_INSTANCE)) and "INSTANCE_CHAT" or "PARTY"
	else
		local ElvUIGVC = GetChannelName('ElvUIGVC')
		if ElvUIGVC and ElvUIGVC > 0 then
			ChatType, Channel = "CHANNEL", ElvUIGVC
		elseif IsInGuild() then
			ChatType = "GUILD"
		end
244
	end
245

246
	if not ChatType then
Simpy's avatar
Simpy committed
247
		lib.ClearSendMessageTimer()
248 249
		return
	end
250

251
	local delay, maxChar, msgLength = 0, 250, strlen(message)
252 253
	if msgLength > maxChar then
		local splitMessage
254
		for _ = 1, ceil(msgLength/maxChar) do
255 256 257
			splitMessage = strmatch(strsub(message, 1, maxChar), '.+;')
			if splitMessage then -- incase the string is over 250 but doesnt contain `;`
				message = gsub(message, "^"..gsub(splitMessage, '([%(%)%.%%%+%-%*%?%[%^%$])','%%%1'), "")
258
				lib.E:Delay(delay, C_ChatInfo_SendAddonMessage, lib.prefix, splitMessage, ChatType, Channel)
259
				delay = delay + 1
Simpy's avatar
Simpy committed
260
				lib.E:Delay(delay, lib.ClearSendMessageTimer) -- keep this after `delay = delay + 1`
Elv's avatar
Elv committed
261 262
			end
		end
263
	else
264
		C_ChatInfo_SendAddonMessage(lib.prefix, message, ChatType, Channel)
Simpy's avatar
Simpy committed
265
		lib.ClearSendMessageTimer()
Elv's avatar
Elv committed
266 267 268
	end
end

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