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 type      = _G.type;
local tonumber  = _G.tonumber;
local tinsert   = _G.tinsert;
local string    = _G.string;
local table     = _G.table;
--[[ STRING METHODS ]]--
local format, match = string.format, string.match;
--[[ TABLE METHODS ]]--
local tcopy = table.copy;
--[[
##########################################################
GET ADDON DATA
##########################################################
]]--
local SV, L, Registry = unpack(select(2, ...))
--[[
##########################################################
LOCALS
##########################################################
]]--
local NewHook = hooksecurefunc;
--[[
##########################################################
SYSTEM UPDATES
##########################################################
]]--
local playerClass = select(2,UnitClass("player"));

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 SV: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 SV:RefreshEverything(bypass)
	self:RefreshAllSystemMedia();

	SV.UIParent:Hide();

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

	SV.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 SV:Load()
	self.Timers:ClearAllTimers()

	local rez = GetCVar("gxResolution");
	local gxHeight = tonumber(match(rez,"%d+x(%d+)"));
	local gxWidth = tonumber(match(rez,"(%d+)x%d+"));

	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

	if playerClass == "DRUID" and not SVUI_Profile.STORED[4] then
		SVUI_Profile.STORED[4] = {}
	    SVUI_Profile.STORED[4].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)
    Registry:FindAddons()

	self:ScreenCalibration();
	self:RefreshSystemFonts();
	self:LoadSystemAlerts();

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

function SV:Launch()
	self:SetDatabaseObjects();
	self:ScreenCalibration("PLAYER_LOGIN");
	Registry:LoadRegisteredAddons();
	self:UpdateDatabase(true);
	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");

	Registry:Update("SVMap");
	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.___version)
		self:AddonMessage(logMsg);
	end

	self.AddonLaunched = true
end
--[[
##########################################################
EVENT HANDLERS
##########################################################
]]--
local PlayerClass = select(2,UnitClass("player"));
local droodSpell1, droodSpell2 = GetSpellInfo(110309), GetSpellInfo(4987);

local SVUISystem_OnEvent = function(self, event, arg, ...)
	if(event == "ADDON_LOADED" and arg == "SVUI") then
		if(not SV.AddonLoaded) then
			SV:Load()
			self:UnregisterEvent("ADDON_LOADED")
		end
	end
	if(event == "PLAYER_LOGIN") then
		if(not SV.AddonLaunched and IsLoggedIn()) then
			SV: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
		SV:DefinePlayerRole()
	elseif(event == "UI_SCALE_CHANGED") then
		SV:ScreenCalibration("UI_SCALE_CHANGED")
	elseif(event == "PLAYER_ENTERING_WORLD") then
		if(not SV.RoleIsSet) then
			SV:DefinePlayerRole()
		end
		if(not SV.MediaInitialized) then
			SV:RefreshAllSystemMedia()
		end
		local _,instanceType = IsInInstance()
		if(instanceType == "pvp") then
			SV.BGTimer = SV.Timers:ExecuteLoop(RequestBattlefieldScoreData, 5)
		elseif(SV.BGTimer) then
			SV.Timers:RemoveLoop(SV.BGTimer)
			SV.BGTimer = nil
		end
	elseif(event == "SPELLS_CHANGED") then
		if (PlayerClass ~= "DRUID") then
			self:UnregisterEvent("SPELLS_CHANGED")
			return
		end
		if GetSpellInfo(droodSpell1) == droodSpell2 then
			SV.Dispellable["Disease"] = true
		elseif(SV.Dispellable["Disease"]) then
			SV.Dispellable["Disease"] = nil
		end
	elseif(event == "PET_BATTLE_CLOSE") then
		SV:PushDisplayAudit()
	elseif(event == "PET_BATTLE_OPENING_START") then
		SV:FlushDisplayAudit()
	elseif(event == "ADDON_ACTION_BLOCKED" or event == "ADDON_ACTION_FORBIDDEN") then
		SV: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 SV.MentaloFrames then
			for frame,_ in pairs(SV.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
			SV:AddonMessage(ERR_NOT_IN_COMBAT)
		end
	elseif(event == "PLAYER_REGEN_ENABLED") then
		SV:ToggleConfig()
		self:UnregisterEvent('PLAYER_REGEN_ENABLED')
	end
end

SV.UIParent:RegisterEvent("ADDON_LOADED")
SV.UIParent:RegisterEvent("PLAYER_LOGIN")
SV.UIParent:SetScript("OnEvent", SVUISystem_OnEvent)