Quantcast
--[[
##############################################################################
_____/\\\\\\\\\\\____/\\\________/\\\__/\\\________/\\\__/\\\\\\\\\\\_       #
 ___/\\\/////////\\\_\/\\\_______\/\\\_\/\\\_______\/\\\_\/////\\\///__      #
  __\//\\\______\///__\//\\\______/\\\__\/\\\_______\/\\\_____\/\\\_____     #
   ___\////\\\__________\//\\\____/\\\___\/\\\_______\/\\\_____\/\\\_____    #
	______\////\\\________\//\\\__/\\\____\/\\\_______\/\\\_____\/\\\_____   #
	 _________\////\\\______\//\\\/\\\_____\/\\\_______\/\\\_____\/\\\_____  #
	  __/\\\______\//\\\______\//\\\\\______\//\\\______/\\\______\/\\\_____ #
	   _\///\\\\\\\\\\\/________\//\\\________\///\\\\\\\\\/____/\\\\\\\\\\\_#
		___\///////////___________\///___________\/////////_____\///////////_#
##############################################################################
S U P E R - V I L L A I N - U I   By: Munglunch                              #
##############################################################################
##########################################################
LOCALIZED LUA FUNCTIONS
##########################################################
]]--
--[[ GLOBALS ]]--
local _G = _G;
local unpack    = _G.unpack;
local select    = _G.select;
local pairs     = _G.pairs;
local ipairs    = _G.ipairs;
local type      = _G.type;
local error     = _G.error;
local pcall     = _G.pcall;
local tostring  = _G.tostring;
local tonumber  = _G.tonumber;
local tinsert   = _G.tinsert;
local string    = _G.string;
local math      = _G.math;
local bit       = _G.bit;
local table     = _G.table;
--[[ STRING METHODS ]]--
local lower, upper = string.lower, string.upper;
local find, format, len, split = string.find, string.format, string.len, string.split;
local match, sub, join = string.match, string.sub, string.join;
local gmatch, gsub = string.gmatch, string.gsub;
--[[ MATH METHODS ]]--
local abs, ceil, floor, round = math.abs, math.ceil, math.floor, math.round;  -- Basic
local fmod, modf, sqrt = math.fmod, math.modf, math.sqrt;   -- Algebra
local atan2, cos, deg, rad, sin = math.atan2, math.cos, math.deg, math.rad, math.sin;  -- Trigonometry
local parsefloat, huge, random = math.parsefloat, math.huge, math.random;  -- Uncommon
--[[ BINARY METHODS ]]--
local band, bor = bit.band, bit.bor;
--[[ TABLE METHODS ]]--
local tremove, tcopy, twipe, tsort, tconcat, tdump = table.remove, table.copy, table.wipe, table.sort, table.concat, table.dump;
--[[
##########################################################
GET ADDON DATA
##########################################################
]]--
local SuperVillain, L = unpack(select(2, ...))
--[[
##########################################################
LOCALS
##########################################################
]]--
local toonClass = select(2,UnitClass("player"));
local playerName = UnitName("player");
local playerRealm = GetRealmName();

local rez = GetCVar("gxResolution");
local gxHeight = tonumber(match(rez,"%d+x(%d+)"));
local gxWidth = tonumber(match(rez,"(%d+)x%d+"));
local NewHook = hooksecurefunc;
local version = GetAddOnMetadata(..., "Version");
--[[
##########################################################
DISPEL MECHANICS
##########################################################
]]--
local droodSpell1, droodSpell2 = GetSpellInfo(110309), GetSpellInfo(4987);
local RefClassRoles;
local RefMagicSpec;
-- canBeTank, canBeHealer, canBeDPS = UnitGetAvailableRoles("unit")
do
	if(toonClass == "PRIEST") then
		RefClassRoles = {"C", "C", "C"}
		SuperVillain.Dispellable = {["Magic"] = true, ["Disease"] = true}
	elseif(toonClass == "WARLOCK") then
		RefClassRoles = {"C", "C", "C"}
	elseif(toonClass == "WARRIOR") then
		RefClassRoles = {"M", "M", "T"}
	elseif(toonClass == "HUNTER") then
		RefClassRoles = {"M", "M", "M"}
	elseif(toonClass == "ROGUE") then
		RefClassRoles = {"M", "M", "M"}
	elseif(toonClass == "MAGE") then
		RefClassRoles = {"C", "C", "C"}
		SuperVillain.Dispellable = {["Curse"] = true}
	elseif(toonClass == "DEATHKNIGHT") then
		RefClassRoles = {"T", "M", "M"}
	elseif(toonClass == "DRUID") then
		RefMagicSpec = 4
		RefClassRoles = {"C", "M", "T", "C"}
		SuperVillain.Dispellable = {["Curse"] = true, ["Poison"] = true}
	elseif(toonClass == "SHAMAN") then
		RefMagicSpec = 3
		RefClassRoles = {"C", "M", "C"}
		SuperVillain.Dispellable = {["Curse"] = true}
	elseif(toonClass == "MONK") then
		RefMagicSpec = 2
		RefClassRoles = {"T", "C", "M"}
		SuperVillain.Dispellable = {["Disease"] = true, ["Poison"] = true}
	elseif(toonClass == "PALADIN") then
		RefMagicSpec = 1
		RefClassRoles = {"C", "T", "M"}
		SuperVillain.Dispellable = {["Poison"] = true, ["Disease"] = true}
	end
end

local function GetTalentInfo(arg)
	if type(arg) == "number" then
		return arg == GetActiveSpecGroup();
	else
		return false;
	end
end

function SuperVillain:DefinePlayerRole()
	local spec = GetSpecialization()
	local role;
	if spec then
		if(self.CurrentSpec == spec) then return end
		role = RefClassRoles[spec]
		if role == "T" and UnitLevel("player") == MAX_PLAYER_LEVEL then
			local bonus, pvp = GetCombatRatingBonus(COMBAT_RATING_RESILIENCE_PLAYER_DAMAGE_TAKEN), false;
			if bonus > GetDodgeChance() and bonus > GetParryChance() then
				role = "M"
			end
		end
		self.CurrentSpec = spec
	else
		local intellect = select(2, UnitStat("player", 4))
		local agility = select(2, UnitStat("player", 2))
		local baseAP, posAP, negAP = UnitAttackPower("player")
		local totalAP = baseAP  +  posAP  +  negAP;
		if totalAP > intellect or agility > intellect then
			role = "M"
		else
			role = "C"
		end
	end
	if self.ClassRole ~= role and self.RoleChangedCallback then
		self.ClassRole = role;
		self.RoleChangedCallback()
	end
	if RefMagicSpec then
		if(GetTalentInfo(RefMagicSpec)) then
			self.Dispellable["Magic"] = true
		elseif(self.Dispellable["Magic"]) then
			self.Dispellable["Magic"] = nil
		end
	end
	self.RoleIsSet = true
end
--[[
##########################################################
SYSTEM FUNCTIONS
##########################################################
]]--
function SuperVillain:StaticPopup_Show(arg)
	if arg == "ADDON_ACTION_FORBIDDEN" then
		StaticPopup_Hide(arg)
	end
end

function SuperVillain:ResetAllUI(confirmed)
	if InCombatLockdown()then
		SendAddonMessage(ERR_NOT_IN_COMBAT)
		return
	end
	if(not confirmed) then
		self:StaticPopup_Show('RESET_UI_CHECK')
		return
	end
	self:ResetInstallation()
end

function SuperVillain:ResetUI(confirmed)
	if InCombatLockdown()then
		SendAddonMessage(ERR_NOT_IN_COMBAT)
		return
	end
	if(not confirmed) then
		self:StaticPopup_Show('RESETMOVERS_CHECK')
		return
	end
	self:ResetMovables()
end

function SuperVillain:ToggleConfig()
	if InCombatLockdown() then
		SendAddonMessage(ERR_NOT_IN_COMBAT)
		self.UIParent:RegisterEvent('PLAYER_REGEN_ENABLED')
		return
	end
	if not IsAddOnLoaded("SVUI_ConfigOMatic") then
		local _,_,_,_,_,state = GetAddOnInfo("SVUI_ConfigOMatic")
		if state ~= "MISSING" and state ~= "DISABLED" then
			LoadAddOn("SVUI_ConfigOMatic")
			local config_version = GetAddOnMetadata("SVUI_ConfigOMatic","Version")
			if(tonumber(config_version) < 4) then
				self:StaticPopup_Show("CLIENT_UPDATE_REQUEST")
			end
		else
			self:AddonMessage("|cffff0000Error -- Addon 'SVUI_ConfigOMatic' not found or is disabled.|r")
			return
		end
	end
	local aceConfig = LibStub("AceConfigDialog-3.0")
	local switch = not aceConfig.OpenFrames["SVUI"] and "Open" or "Close"
	aceConfig[switch](aceConfig, "SVUI")
	GameTooltip:Hide()
end

--/script SVUI[1]:TaintHandler("SVUI", "Script", "Function")
function SuperVillain:TaintHandler(taint, sourceName, sourceFunc)
	if GetCVarBool('scriptErrors') ~= 1 then return end
	local errorString = ("Error Captured: %s->%s->{%s}"):format(taint, sourceName or "Unknown", sourceFunc or "Unknown")
	SuperVillain:AddonMessage(errorString)
	SuperVillain:StaticPopup_Show("TAINT_RL")
end
--[[
##########################################################
ANIMATION CLASS
##########################################################
]]--
local FlickerAlpha = {0.2,0.15,0.1,0.15,0.2,0.15,0.1,0.15}
local Animate = {};
--[[ HANDLERS ]]--

local Anim_OnShow = function(self)
	if not self.anim:IsPlaying() then
		self.anim:Play()
	end
end

local Anim_OnHide = function(self)
	self.anim:Finish()
end

local Anim_OnPlay = function(self)
	local parent = self.parent
	parent:SetAlpha(1)
	if self.hideOnFinished and not parent:IsShown() then
		parent:Show()
	end
end

local Anim_OnStop = function(self)
	local parent = self.parent
	if self.fadeOnFinished then
		parent:SetAlpha(0)
	else
		parent:SetAlpha(1)
	end
	if self.hideOnFinished and parent:IsShown() then
		parent:Hide()
	end
	if self.savedFrameLevel then
		parent:SetScale(1)
		parent:SetFrameLevel(self.savedFrameLevel)
	end
end

local Anim_OnFinished = function(self)
	local parent = self.parent
	local looped = self:GetLooping()
	self:Stop()
	if(looped and looped == "REPEAT" and parent:IsShown()) then
		self:Play()
	end
end

local Sprite_OnUpdate = function(self)
	local order = self:GetOrder()
	local parent = self.parent
	local left, right;
	if(self.isFadeFrame) then
		parent:SetAlpha(0)
		return
	end
	left = (order - 1) * 0.25;
	right = left + 0.25;
	parent:SetTexCoord(left,right,0,1)
	if parent.overlay then
		parent.overlay:SetTexCoord(left,right,0,1)
		parent.overlay:SetVertexColor(1,1,1,FlickerAlpha[order])
	end
end

local SmallSprite_OnUpdate = function(self)
	local order = self:GetOrder()
	local parent = self.parent
	local left, right;
	if(self.isFadeFrame) then
		parent:SetAlpha(0)
		return
	end
	left = (order - 1) * 0.125;
	right = left + 0.125;
	parent:SetTexCoord(left,right,0,1)
	if parent.overlay then
		parent.overlay:SetTexCoord(left,right,0,1)
		parent.overlay:SetVertexColor(1,1,1,FlickerAlpha[order])
	end
end

local PulseIn_OnUpdate = function(self)
	local parent = self.parent
	local step = self:GetProgress()
	if(parent.savedFrameLevel) then
		parent:SetFrameLevel(128)
	end
	parent:SetScale(1 + (1.05 * step))
end

local PulseOut_OnUpdate = function(self)
	local parent = self.parent
	local step = self:GetProgress()
	if(parent.savedFrameLevel) then
		parent:SetFrameLevel(128)
	end
	parent:SetScale(1 + (1.05 * (1 - step)))
end

local Slide_OnUpdate = function(self)
	local parent = self.parent
	local step = self:GetProgress()
	parent:SetScale(1 + (1.05 * step))
end

local Slide_OnPlay = function(self)
	local parent = self.parent
	parent:SetScale(0.01)
	parent:SetAlpha(1)
end

local Slide_FadeStart = function(self)
	local parent = self.parent
	UIFrameFadeOut(parent, 0.3, 1, 0)
end

local Slide_FadeStop = function(self)
	self.parent:SetAlpha(0)
end

--[[ HELPER FUNCTION ]]--

local function SetNewAnimation(frame, animType, subType)
	local anim = frame:CreateAnimation(animType, subType)
	anim.parent = frame.parent
	return anim
end

--[[ ANIMATION CLASS METHODS ]]--

function Animate:SetTemplate(frame, animType, hideOnFinished, speed, special, scriptToParent)
	if not animType then return end

	frame.anim = frame:CreateAnimationGroup(animType)
	frame.anim.parent = frame;
	frame.anim.hideOnFinished = hideOnFinished
	if animType ~= 'Flash' then
		frame.anim:SetScript("OnPlay", Anim_OnPlay)
		frame.anim:SetScript("OnFinished", Anim_OnFinished)
		frame.anim:SetScript("OnStop", Anim_OnStop)
	end

	if scriptToParent then
		local frameParent = frame:GetParent();
		if(frameParent.SetScript) then
			frameParent.anim = frame.anim;
			frameParent:SetScript("OnShow", Anim_OnShow)
			frameParent:SetScript("OnHide", Anim_OnHide)
		end
	elseif(frame.SetScript) then
		frame:SetScript("OnShow", Anim_OnShow)
		frame:SetScript("OnHide", Anim_OnHide)
	end

	if animType == 'Flash'then
		frame.anim.fadeOnFinished = true
		if not speed then speed = 0.33 end

		frame.anim[1] = SetNewAnimation(frame.anim, "ALPHA", "FadeIn")
		frame.anim[1]:SetChange(1)
		frame.anim[1]:SetOrder(2)
		frame.anim[1]:SetDuration(speed)

		frame.anim[2] = SetNewAnimation(frame.anim, "ALPHA","FadeOut")
		frame.anim[2]:SetChange(-1)
		frame.anim[2]:SetOrder(1)
		frame.anim[2]:SetDuration(speed)

		if special then
			frame.anim:SetLooping("REPEAT")
		end
	elseif animType == 'Orbit' then
		frame.anim[1] = SetNewAnimation(frame.anim, "Rotation")
		if special then
			frame.anim[1]:SetDegrees(-360)
		else
			frame.anim[1]:SetDegrees(360)
		end
		frame.anim[1]:SetDuration(speed)
		frame.anim:SetLooping("REPEAT")
		frame.anim:Play()
	elseif animType == 'Sprite' then
		frame.anim[1] = SetNewAnimation(frame.anim, "Translation")
		frame.anim[1]:SetOrder(1)
		frame.anim[1]:SetDuration(speed)
		frame.anim[1]:SetScript("OnUpdate", Sprite_OnUpdate)

		frame.anim[2] = SetNewAnimation(frame.anim, "Translation")
		frame.anim[2]:SetOrder(2)
		frame.anim[2]:SetDuration(speed)
		frame.anim[2]:SetScript("OnUpdate", Sprite_OnUpdate)

		frame.anim[3] = SetNewAnimation(frame.anim, "Translation")
		frame.anim[3]:SetOrder(3)
		frame.anim[3]:SetDuration(speed)
		frame.anim[3]:SetScript("OnUpdate", Sprite_OnUpdate)

		frame.anim[4] = SetNewAnimation(frame.anim, "Translation")
		frame.anim[4]:SetOrder(4)
		frame.anim[4]:SetDuration(speed)
		frame.anim[4]:SetScript("OnUpdate", Sprite_OnUpdate)

		if special then
			frame.anim[5] = SetNewAnimation(frame.anim, "Translation")
			frame.anim[5]:SetOrder(5)
			frame.anim[5]:SetDuration(special)
			frame.anim[5].isFadeFrame = true;
			frame.anim[5]:SetScript("OnUpdate", Sprite_OnUpdate)
		end

		frame.anim:SetLooping("REPEAT")
	elseif animType == 'SmallSprite' then
		frame.anim[1] = SetNewAnimation(frame.anim, "Translation")
		frame.anim[1]:SetOrder(1)
		frame.anim[1]:SetDuration(speed)
		frame.anim[1]:SetScript("OnUpdate", SmallSprite_OnUpdate)

		frame.anim[2] = SetNewAnimation(frame.anim, "Translation")
		frame.anim[2]:SetOrder(2)
		frame.anim[2]:SetDuration(speed)
		frame.anim[2]:SetScript("OnUpdate", SmallSprite_OnUpdate)

		frame.anim[3] = SetNewAnimation(frame.anim, "Translation")
		frame.anim[3]:SetOrder(3)
		frame.anim[3]:SetDuration(speed)
		frame.anim[3]:SetScript("OnUpdate", SmallSprite_OnUpdate)

		frame.anim[4] = SetNewAnimation(frame.anim, "Translation")
		frame.anim[4]:SetOrder(4)
		frame.anim[4]:SetDuration(speed)
		frame.anim[4]:SetScript("OnUpdate", SmallSprite_OnUpdate)

		frame.anim[5] = SetNewAnimation(frame.anim, "Translation")
		frame.anim[5]:SetOrder(5)
		frame.anim[5]:SetDuration(speed)
		frame.anim[5]:SetScript("OnUpdate", SmallSprite_OnUpdate)

		frame.anim[6] = SetNewAnimation(frame.anim, "Translation")
		frame.anim[6]:SetOrder(6)
		frame.anim[6]:SetDuration(speed)
		frame.anim[6]:SetScript("OnUpdate", SmallSprite_OnUpdate)

		frame.anim[7] = SetNewAnimation(frame.anim, "Translation")
		frame.anim[7]:SetOrder(7)
		frame.anim[7]:SetDuration(speed)
		frame.anim[7]:SetScript("OnUpdate", SmallSprite_OnUpdate)

		frame.anim[8] = SetNewAnimation(frame.anim, "Translation")
		frame.anim[8]:SetOrder(8)
		frame.anim[8]:SetDuration(speed)
		frame.anim[8]:SetScript("OnUpdate", SmallSprite_OnUpdate)

		if special then
			frame.anim[9] = SetNewAnimation(frame.anim, "Translation")
			frame.anim[9]:SetOrder(9)
			frame.anim[9]:SetDuration(special)
			frame.anim[9].isFadeFrame = true;
			frame.anim[9]:SetScript("OnUpdate", Sprite_OnUpdate)
		end

		frame.anim:SetLooping("REPEAT")
	elseif animType == 'Pulse' then
		frame.anim.savedFrameLevel = frame:GetFrameLevel()

		frame.anim[1] = SetNewAnimation(frame.anim)
		frame.anim[1]:SetDuration(0.2)
		frame.anim[1]:SetEndDelay(0.1)
		frame.anim[1]:SetOrder(1)
		frame.anim[1]:SetScript("OnUpdate", PulseIn_OnUpdate)

		frame.anim[2] = SetNewAnimation(frame.anim)
		frame.anim[2]:SetDuration(0.6)
		frame.anim[2]:SetOrder(2)
		frame.anim[2]:SetScript("OnUpdate", PulseOut_OnUpdate)
	end
end

--[[ ROTATE AND WOBBLE (kinda like twerking i guess...) ]]--

function Animate:Orbit(frame, speed, reversed, hideOnFinished)
	if not frame then return end
	if not speed then speed = 1 end
	self:SetTemplate(frame, 'Orbit', hideOnFinished, speed, reversed)
end

function Animate:Pulse(frame, hideOnFinished)
	if not frame then return end
	self:SetTemplate(frame, 'Pulse', hideOnFinished)
end

--[[ ANIMATED SPRITES ]]--

function Animate:Sprite(frame, speed, fadeTime, scriptToParent)
	if not frame then return end
	speed = speed or 0.08;
	self:SetTemplate(frame, 'Sprite', false, speed, fadeTime, scriptToParent)
end

function Animate:SmallSprite(frame, speed, fadeTime, scriptToParent)
	if not frame then return end
	speed = speed or 0.08;
	self:SetTemplate(frame, 'SmallSprite', false, speed, fadeTime, scriptToParent)
end

function Animate:StopSprite(frame)
	if not frame then return end
	frame.anim:Finish()
end

--[[ FLASHING ]]--

function Animate:Flash(frame, speed, looped)
	if not frame.anim then
		Animate:SetTemplate(frame, 'Flash', false, speed, looped)
	end
	if not frame.anim:IsPlaying() then
		frame.anim:Play()
	end
end

function Animate:StopFlash(frame)
	if not frame.anim then return end
	frame.anim:Finish()
	frame.anim:Stop()
end

--[[ SLIDING ]]--

function Animate:Slide(frame, xDirection, yDirection, bounce)
	if(not frame or (frame and frame.anim)) then return end

	frame.anim = frame:CreateAnimationGroup("Slide")
	frame.anim.hideOnFinished = true;
	frame.anim.parent = frame;
	frame.anim:SetScript("OnPlay", Anim_OnPlay)
	frame.anim:SetScript("OnFinished", Anim_OnFinished)
	frame.anim:SetScript("OnStop", Anim_OnStop)

	frame.anim[1] = SetNewAnimation(frame.anim, "Translation")
	frame.anim[1]:SetDuration(0)
	frame.anim[1]:SetOrder(1)

	frame.anim[2] = SetNewAnimation(frame.anim, "Translation")
	frame.anim[2]:SetDuration(0.3)
	frame.anim[2]:SetOrder(2)
	frame.anim[2]:SetSmoothing("OUT")

	if bounce then
		frame.anim[3] = SetNewAnimation(frame.anim, "Translation")
		frame.anim[3]:SetDuration(0.5)
		frame.anim[3]:SetOrder(3)

		frame.anim[4] = SetNewAnimation(frame.anim, "Translation")
		frame.anim[4]:SetDuration(0.3)
		frame.anim[4]:SetOrder(4)
		frame.anim[4]:SetSmoothing("IN")
		frame.anim[4]:SetOffset(xDirection, yDirection)
	end
end

function Animate:RandomSlide(frame, raised)
	if not frame then return end
	if raised then
		frame:SetFrameLevel(30)
	else
		frame:SetFrameLevel(20)
	end
	frame:SetPoint("CENTER", SuperVillain.UIParent, "CENTER", 0, -150)

	frame.anim = frame:CreateAnimationGroup("RandomSlide")
	frame.anim.parent = frame;
	frame.anim[1] = SetNewAnimation(frame.anim, "Translation")
	frame.anim[1]:SetOrder(1)
	frame.anim[1]:SetDuration(0.1)
	frame.anim[1]:SetScript("OnUpdate", Slide_OnUpdate)
	frame.anim[1]:SetScript("OnPlay", Slide_OnPlay)

	frame.anim[2] = SetNewAnimation(frame.anim, "Translation")
	frame.anim[2]:SetOrder(2)
	frame.anim[2]:SetDuration(1)

	frame.anim[3] = SetNewAnimation(frame.anim, "Translation")
	frame.anim[3]:SetOrder(3)
	frame.anim[3]:SetDuration(0.3)
	frame.anim[3]:SetSmoothing("OUT")
	frame.anim[3]:SetScript("OnPlay", Slide_FadeStart)
	frame.anim[3]:SetScript("OnStop", Slide_FadeStop)

	frame.anim:SetScript("OnFinished", Slide_FadeStop)
end

function Animate:SlideIn(frame)
	if not frame.anim then return end
	frame:Show()
	frame.anim:Play()
end

function Animate:SlideOut(frame)
	if not frame.anim then return end
	frame.anim:Finish()
	frame.anim:Stop()
end

SuperVillain.Animate = Animate;
--[[
##########################################################
SYSTEM UPDATES
##########################################################
]]--
local function DeleteOldSavedVars()
	--[[ BEGIN DEPRECATED ]]--
    if SVUI_DATA then SVUI_DATA = nil end
    if SVUI_SAFE_DATA then SVUI_SAFE_DATA = nil end
    if SVUI_TRACKER then SVUI_TRACKER = nil end
    if SVUI_ENEMIES then SVUI_ENEMIES = nil end
    if SVUI_JOURNAL then SVUI_JOURNAL = nil end
    if SVUI_CHARACTER_LOG then SVUI_CHARACTER_LOG = nil end
    if SVUI_MOVED_FRAMES then SVUI_MOVED_FRAMES = nil end
    if SVUI_SystemData then SVUI_SystemData = nil end
    if SVUI_ProfileData then SVUI_ProfileData = nil end
    if SVUI_Filters then SVUI_Filters = nil end
    if SVUI_AuraFilters then SVUI_AuraFilters = nil end
    if SVUI_AuraWatch then SVUI_AuraWatch = nil end
    if SVUI_Cache["Mentalo"]["Blizzard"] then SVUI_Cache["Mentalo"]["Blizzard"] = nil end
    if SVUI_Cache["Mentalo"]["UI"] then SVUI_Cache["Mentalo"]["UI"] = nil end
    if(SVUI_Profile.SAFEDATA.install_complete) then SVUI_Profile.SAFEDATA.install_complete = nil end
    --[[ END DEPRECATED ]]--
end

function SuperVillain:VersionCheck()
	local minimumVersion = 4.06;
	local installedVersion = SVUI_Profile.SAFEDATA.install_version
	if(installedVersion) then
		if(type(installedVersion) == "string") then
			installedVersion = tonumber(SVUI_Profile.SAFEDATA.install_version)
		end
		if(type(installedVersion) == "number" and installedVersion < minimumVersion) then
			DeleteOldSavedVars()
			self:Install(true)
		end
	else
		DeleteOldSavedVars()
		self:Install(true)
	end
end

function SuperVillain:RefreshEverything(bypass)
	self:RefreshAllSystemMedia();

	SuperVillain.UIParent:Hide();

	self:SetSVMovablesPositions();
	self.Registry:Update('SVUnit');
	self.Registry:UpdateAll();

	SuperVillain.UIParent:Show();

	if not bypass then
		self:VersionCheck()
	end
end
--[[
##########################################################
SVUI LOAD PROCESS
##########################################################
]]--
local function PrepareStorage()
	if(not SVUI_Profile) then return end
	SVUI_Profile.STORED = {}
	local old = SVUI_Profile.SAFEDATA
	local media = old.mediastyle or ""
	local bars = old.barstyle or ""
	local units = old.unitstyle or ""
	local groups = old.groupstyle or ""
	local auras = old.aurastyle or ""
	local spec = GetSpecialization() or 1
	SVUI_Profile.STORED[1] = tcopy(SVUI_Profile, true)
	SVUI_Profile.STORED[1].LAYOUT = {
		mediastyle = media,
		barstyle = bars,
		unitstyle = units,
		groupstyle = groups,
		aurastyle = auras
	}
	SVUI_Profile.SAFEDATA.mediastyle = nil
	SVUI_Profile.SAFEDATA.barstyle = nil
	SVUI_Profile.SAFEDATA.unitstyle = nil
	SVUI_Profile.SAFEDATA.groupstyle = nil
	SVUI_Profile.SAFEDATA.aurastyle = nil
	SVUI_Profile.SAFEDATA.dualSpecEnabled = false
end

function SuperVillain:Load()
	self:ClearAllTimers()

	if not SVUI_Global then SVUI_Global = {} end
    if not SVUI_Global["profiles"] then SVUI_Global["profiles"] = {} end
    if SVUI_Global["gold"] then SVUI_Global["gold"] = nil end
    if SVUI_Global["profileKeys"] then SVUI_Global["profileKeys"] = nil end

    if not SVUI_Profile then SVUI_Profile = {} end
    if not SVUI_Profile.SAFEDATA then SVUI_Profile.SAFEDATA = {dualSpecEnabled = false} end
    if not SVUI_Profile.STORED then PrepareStorage() end
    if not SVUI_Profile.STORED[2] then
    	SVUI_Profile.STORED[2] = {}
	    SVUI_Profile.STORED[2].LAYOUT = SVUI_Profile.STORED[1].LAYOUT
	end
	if not SVUI_Profile.STORED[3] then
		SVUI_Profile.STORED[3] = {}
	    SVUI_Profile.STORED[3].LAYOUT = SVUI_Profile.STORED[1].LAYOUT
	end
    for k,v in pairs(SVUI_Profile) do
    	if(k ~= "STORED" and k ~= "SAFEDATA") then
    		SVUI_Profile[k] = nil
    	end
    end

    if not SVUI_Cache then SVUI_Cache = {} end
    if not SVUI_Cache["Dock"] then SVUI_Cache["Dock"] = {} end
    if not SVUI_Cache["Mentalo"] then SVUI_Cache["Mentalo"] = {} end
    if not SVUI_Cache["Bindings"] then SVUI_Cache["Bindings"] = {} end
    if(not SVUI_Cache["screenheight"] or (SVUI_Cache["screenheight"] and type(SVUI_Cache["screenheight"]) ~= "number")) then
    	SVUI_Cache["screenheight"] = gxHeight
    end
    if(not SVUI_Cache["screenwidth"] or (SVUI_Cache["screenwidth"] and type(SVUI_Cache["screenwidth"]) ~= "number")) then
    	SVUI_Cache["screenwidth"] = gxWidth
    end

    self:SetDatabaseObjects(true)

	self:UIScale();
	self:RefreshSystemFonts();
	self:LoadSystemAlerts();

	self.UIParent:RegisterEvent('PLAYER_REGEN_DISABLED');
	self.AddonLoaded = true
end

function SuperVillain:Launch()
	self:SetDatabaseObjects()
	self:UIScale("PLAYER_LOGIN");
	self.Registry:LoadPackages();
	self:DefinePlayerRole();
	self:LoadMovables();
	self:SetSVMovablesPositions();

	self:VersionCheck()

	self:RefreshAllSystemMedia();

	NewHook("StaticPopup_Show", self.StaticPopup_Show)

	self.UIParent:RegisterEvent("ACTIVE_TALENT_GROUP_CHANGED");
	self.UIParent:RegisterEvent("PLAYER_TALENT_UPDATE");
	self.UIParent:RegisterEvent("CHARACTER_POINTS_CHANGED");
	self.UIParent:RegisterEvent("UNIT_INVENTORY_CHANGED");
	self.UIParent:RegisterEvent("UPDATE_BONUS_ACTIONBAR");
	self.UIParent:RegisterEvent("UI_SCALE_CHANGED");
	self.UIParent:RegisterEvent("PLAYER_ENTERING_WORLD");
	self.UIParent:RegisterEvent("PET_BATTLE_CLOSE");
	self.UIParent:RegisterEvent("PET_BATTLE_OPENING_START");
	self.UIParent:RegisterEvent("ADDON_ACTION_BLOCKED");
	self.UIParent:RegisterEvent("ADDON_ACTION_FORBIDDEN");
	self.UIParent:RegisterEvent("SPELLS_CHANGED");

	self.Registry:Update("SVMap");
	self.Registry:Update("SVUnit", true);
	collectgarbage("collect")

	_G["SVUI_Mentalo"]:SetFixedPanelTemplate("Component")
	_G["SVUI_Mentalo"]:SetPanelColor("yellow")
	_G["SVUI_MentaloPrecision"]:SetPanelTemplate("Transparent")

	if self.db.system.loginmessage then
		local logMsg = (L["LOGIN_MSG"]):format("|cffFFFF1A", "|cffAA78FF", self.___ver)
		self:AddonMessage(logMsg);
	end

	self.AddonLaunched = true
end
--[[
##########################################################
EVENT HANDLERS
##########################################################
]]--
local SVUISystem_OnEvent = function(self, event, arg, ...)
	if(event == "ADDON_LOADED" and arg == "SVUI") then
		if(not SuperVillain.AddonLoaded) then
			SuperVillain:Load()
			self:UnregisterEvent("ADDON_LOADED")
		end
	end
	if(event == "PLAYER_LOGIN") then
		if(not SuperVillain.AddonLaunched and IsLoggedIn()) then
			SuperVillain:Launch()
			self:UnregisterEvent("PLAYER_LOGIN")
		end
	end
	if(event == "ACTIVE_TALENT_GROUP_CHANGED" or event == "PLAYER_TALENT_UPDATE" or event == "CHARACTER_POINTS_CHANGED" or event == "UNIT_INVENTORY_CHANGED" or event == "UPDATE_BONUS_ACTIONBAR") then
		SuperVillain:DefinePlayerRole()
	elseif(event == "UI_SCALE_CHANGED") then
		SuperVillain:UIScale("UI_SCALE_CHANGED")
	elseif(event == "PLAYER_ENTERING_WORLD") then
		if(not SuperVillain.RoleIsSet) then
			SuperVillain:DefinePlayerRole()
		end
		if(not SuperVillain.MediaInitialized) then
			SuperVillain:RefreshAllSystemMedia()
		end
		local a,b = IsInInstance()
		if(b == "pvp") then
			SuperVillain.BGTimer = SuperVillain:ExecuteLoop(RequestBattlefieldScoreData, 5)
		elseif(SuperVillain.BGTimer) then
			SuperVillain:RemoveLoop(SuperVillain.BGTimer)
			SuperVillain.BGTimer = nil
		end
	elseif(event == "SPELLS_CHANGED") then
		if (toonClass ~= "DRUID") then
			self:UnregisterEvent("SPELLS_CHANGED")
			return
		end
		if GetSpellInfo(droodSpell1) == droodSpell2 then
			SuperVillain.Dispellable["Disease"] = true
		elseif(SuperVillain.Dispellable["Disease"]) then
			SuperVillain.Dispellable["Disease"] = nil
		end
	elseif(event == "PET_BATTLE_CLOSE") then
		SuperVillain:PushDisplayAudit()
	elseif(event == "PET_BATTLE_OPENING_START") then
		SuperVillain:FlushDisplayAudit()
	elseif(event == "ADDON_ACTION_BLOCKED" or event == "ADDON_ACTION_FORBIDDEN") then
		SuperVillain:TaintHandler(arg, ...)
	elseif(event == "PLAYER_REGEN_DISABLED") then
		local forceClosed = false;
		if IsAddOnLoaded("SVUI_ConfigOMatic") then
			local aceConfig=LibStub("AceConfigDialog-3.0")
			if aceConfig.OpenFrames["SVUI"] then
				self:RegisterEvent('PLAYER_REGEN_ENABLED')
				aceConfig:Close("SVUI")
				forceClosed = true
			end
		end
		if SuperVillain.MentaloFrames then
			for frame,_ in pairs(SuperVillain.MentaloFrames) do
				if _G[frame] and _G[frame]:IsShown() then
					forceClosed = true;
					_G[frame]:Hide()
				end
			end
		end
		if(HenchmenFrameModel and HenchmenFrame and HenchmenFrame:IsShown()) then
			HenchmenFrame:Hide()
			HenchmenFrameBG:Hide()
			forceClosed = true;
		end
		if forceClosed == true then
			SendAddonMessage(ERR_NOT_IN_COMBAT)
		end
	elseif(event == "PLAYER_REGEN_ENABLED") then
		SuperVillain:ToggleConfig()
		self:UnregisterEvent('PLAYER_REGEN_ENABLED')
	end
end

SuperVillain.UIParent:RegisterEvent("ADDON_LOADED")
SuperVillain.UIParent:RegisterEvent("PLAYER_LOGIN")
SuperVillain.UIParent:SetScript("OnEvent", SVUISystem_OnEvent)
--[[
##########################################################
THE CLEANING LADY
##########################################################
]]--
-- local LemonPledge = 0;
-- local Consuela = CreateFrame("Frame", nil)
-- Consuela:RegisterAllEvents()
-- Consuela:SetScript("OnEvent", function(self, event)
-- 	LemonPledge = LemonPledge + 1
-- 	if (InCombatLockdown() and LemonPledge > 25000) or (not InCombatLockdown() and LemonPledge > 10000) then
-- 		collectgarbage("collect")
-- 		LemonPledge = 0;
-- 	end
-- end)