LibElvUIPlugin-1.0.lua 8.09 KB
Newer Older
1
local MAJOR, MINOR = "LibElvUIPlugin-1.0", 17
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 10
--WoW API / Variables
local CreateFrame = CreateFrame
11
local IsInGroup, IsInRaid = IsInGroup, IsInRaid
Elv's avatar
Elv committed
12 13 14 15
local GetAddOnMetadata = GetAddOnMetadata
local IsAddOnLoaded = IsAddOnLoaded
local RegisterAddonMessagePrefix = RegisterAddonMessagePrefix
local SendAddonMessage = SendAddonMessage
16
local GetNumGroupMembers = GetNumGroupMembers
Elv's avatar
Elv committed
17 18 19 20 21 22 23 24 25 26 27
local LE_PARTY_CATEGORY_HOME = LE_PARTY_CATEGORY_HOME
local LE_PARTY_CATEGORY_INSTANCE = LE_PARTY_CATEGORY_INSTANCE

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

function lib:RegisterPlugin(name,callback, isLib)
    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
		RegisterAddonMessagePrefix(lib.prefix)
		local f = CreateFrame('Frame')
		f:RegisterEvent("GROUP_ROSTER_UPDATE")
		f:RegisterEvent("CHAT_MSG_ADDON")
		f:SetScript('OnEvent', lib.VersionCheck)
		lib.vcframe = f
	end

	if not loaded then
		if not lib.ConfigFrame then
			local configFrame = CreateFrame("Frame")
			configFrame:RegisterEvent("ADDON_LOADED")
91
			configFrame:SetScript("OnEvent", function(self, event, addon)
Elv's avatar
Elv committed
92
				if addon == "ElvUI_Config" then
93 94 95
					for _, PlugIn in pairs(lib.plugins) do
						if PlugIn.callback then
							PlugIn.callback()
Elv's avatar
Elv committed
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
						end
					end
				end
			end)
			lib.ConfigFrame = configFrame
		end
	elseif loaded then
		-- Need to update plugins list
		if name ~= MAJOR then
			ElvUI[1].Options.args.plugins.args.plugins.name = lib:GeneratePluginList()
		end
		callback()
	end

	return plugin
end

function lib:GetPluginOptions()
	ElvUI[1].Options.args.plugins = {
        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

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(self)
	lib:SendPluginVersionCheck(lib:GenerateVersionCheckMessage())
Simpy's avatar
Simpy committed
146

Elv's avatar
Elv committed
147 148 149 150 151 152 153 154
	if self["ElvUIPluginSendMSGTimer"] then
		self:CancelTimer(self["ElvUIPluginSendMSGTimer"])
		self["ElvUIPluginSendMSGTimer"] = nil
	end
end

function lib:VersionCheck(event, prefix, message, channel, sender)
	local E = ElvUI[1]
155
	if (event == "CHAT_MSG_ADDON") and sender and message and (not strmatch(message, "^%s-$")) and (prefix == lib.prefix) then
156
		if not lib.myName then lib.myName = E.myname..'-'..gsub(E.myrealm,'[%s%-]','') end
157
		if sender == lib.myName then return end
Elv's avatar
Elv committed
158
		if not E["pluginRecievedOutOfDateMessage"] then
159
			local name, version, plugin, Pname
Elv's avatar
Elv committed
160
			for _, p in pairs({strsplit(";",message)}) do
161 162
				if not strmatch(p, "^%s-$") then
					name, version = strmatch(p, "([%w_]+)=([%d%p]+)")
Simpy's avatar
Simpy committed
163
					if lib.plugins[name] then
164
						plugin = lib.plugins[name]
Simpy's avatar
Simpy committed
165 166 167
						if plugin.version ~= 'BETA' and version ~= nil and tonumber(version) ~= nil and plugin.version ~= nil and tonumber(plugin.version) ~= nil and tonumber(version) > tonumber(plugin.version) then
							plugin.old = true
							plugin.newversion = tonumber(version)
168
							Pname = GetAddOnMetadata(plugin.name, "Title")
169
							E:Print(format(MSG_OUTDATED,Pname,plugin.version,plugin.newversion))
Simpy's avatar
Simpy committed
170 171
							E["pluginRecievedOutOfDateMessage"] = true
						end
Elv's avatar
Elv committed
172 173 174 175 176
					end
				end
			end
		end
	else
177 178 179
		if not E.SendPluginVersionCheck then
			E.SendPluginVersionCheck = SendPluginVersionCheck
		end
180 181

		local numMembers = GetNumGroupMembers()
182
		if numMembers == 0 and lib.numMembers then
183
			lib.numMembers = nil -- clear this after we leave the group
184
		elseif numMembers > 1 and lib.numMembers ~= numMembers then
185
			E["ElvUIPluginSendMSGTimer"] = E:ScheduleTimer("SendPluginVersionCheck", 12)
186 187
			lib.numMembers = numMembers
		end
Simpy's avatar
Simpy committed
188
	end
Elv's avatar
Elv committed
189 190 191
end

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

function lib:SendPluginVersionCheck(message)
214 215 216 217 218
	if (not message) or strmatch(message, "^%s-$") then return end

	local ChatType = ((not IsInRaid(LE_PARTY_CATEGORY_HOME) and IsInRaid(LE_PARTY_CATEGORY_INSTANCE)) or (not IsInGroup(LE_PARTY_CATEGORY_HOME) and IsInGroup(LE_PARTY_CATEGORY_INSTANCE))) and "INSTANCE_CHAT" or (IsInRaid() and "RAID") or (IsInGroup() and "PARTY") or nil
	if not ChatType then return end

219
	local delay, maxChar, msgLength = 0, 250, strlen(message)
220 221 222 223 224 225
	if msgLength > maxChar then
		local splitMessage
		for _=1, ceil(msgLength/maxChar) do
			splitMessage = strmatch(strsub(message, 1, maxChar), '.+;')
			if splitMessage then -- incase the string is over 250 but doesnt contain `;`
				message = gsub(message, "^"..gsub(splitMessage, '([%(%)%.%%%+%-%*%?%[%^%$])','%%%1'), "")
226 227
				ElvUI[1]:Delay(delay, SendAddonMessage(lib.prefix, splitMessage, ChatType))
				delay = delay + 1
Elv's avatar
Elv committed
228 229
			end
		end
230
	else
231
		SendAddonMessage(lib.prefix, message, ChatType)
Elv's avatar
Elv committed
232 233 234 235
	end
end

lib:RegisterPlugin(MAJOR, lib.GetPluginOptions)