From 2189f65c9fc1a719175bfcd7aff3f373f7ef6d85 Mon Sep 17 00:00:00 2001 From: Denis Hamann Date: Sat, 4 Sep 2021 02:38:54 +0200 Subject: [PATCH] first commit --- .gitignore | 3 + ArenaStats.lua | 427 ++++ ArenaStats.toc | 14 + GUI.lua | 235 ++ HybridScrollFrame.xml | 133 ++ Libs/AceAddon-3.0/AceAddon-3.0.lua | 653 ++++++ Libs/AceAddon-3.0/AceAddon-3.0.xml | 4 + Libs/AceConfig-3.0/AceConfig-3.0.lua | 58 + Libs/AceConfig-3.0/AceConfig-3.0.xml | 8 + .../AceConfigCmd-3.0/AceConfigCmd-3.0.lua | 794 +++++++ .../AceConfigCmd-3.0/AceConfigCmd-3.0.xml | 4 + .../AceConfigDialog-3.0.lua | 2019 +++++++++++++++++ .../AceConfigDialog-3.0.xml | 4 + .../AceConfigRegistry-3.0.lua | 371 +++ .../AceConfigRegistry-3.0.xml | 4 + Libs/AceConsole-3.0/AceConsole-3.0.lua | 250 ++ Libs/AceConsole-3.0/AceConsole-3.0.xml | 4 + Libs/AceDB-3.0/AceDB-3.0.lua | 744 ++++++ Libs/AceDB-3.0/AceDB-3.0.xml | 4 + Libs/AceDBOptions-3.0/AceDBOptions-3.0.lua | 460 ++++ Libs/AceDBOptions-3.0/AceDBOptions-3.0.xml | 4 + Libs/AceEvent-3.0/AceEvent-3.0.lua | 126 + Libs/AceEvent-3.0/AceEvent-3.0.xml | 4 + Libs/AceGUI-3.0/AceGUI-3.0.lua | 1026 +++++++++ Libs/AceGUI-3.0/AceGUI-3.0.xml | 28 + .../AceGUIContainer-BlizOptionsGroup.lua | 138 ++ .../widgets/AceGUIContainer-DropDownGroup.lua | 157 ++ .../widgets/AceGUIContainer-Frame.lua | 318 +++ .../widgets/AceGUIContainer-InlineGroup.lua | 103 + .../widgets/AceGUIContainer-ScrollFrame.lua | 215 ++ .../widgets/AceGUIContainer-SimpleGroup.lua | 69 + .../widgets/AceGUIContainer-TabGroup.lua | 349 +++ .../widgets/AceGUIContainer-TreeGroup.lua | 715 ++++++ .../widgets/AceGUIContainer-Window.lua | 336 +++ .../widgets/AceGUIWidget-Button.lua | 103 + .../widgets/AceGUIWidget-CheckBox.lua | 296 +++ .../widgets/AceGUIWidget-ColorPicker.lua | 190 ++ .../widgets/AceGUIWidget-DropDown-Items.lua | 471 ++++ .../widgets/AceGUIWidget-DropDown.lua | 737 ++++++ .../widgets/AceGUIWidget-EditBox.lua | 263 +++ .../widgets/AceGUIWidget-Heading.lua | 78 + Libs/AceGUI-3.0/widgets/AceGUIWidget-Icon.lua | 140 ++ .../widgets/AceGUIWidget-InteractiveLabel.lua | 94 + .../widgets/AceGUIWidget-Keybinding.lua | 249 ++ .../AceGUI-3.0/widgets/AceGUIWidget-Label.lua | 179 ++ .../widgets/AceGUIWidget-MultiLineEditBox.lua | 366 +++ .../widgets/AceGUIWidget-Slider.lua | 284 +++ Libs/AceLocale-3.0/AceLocale-3.0.lua | 137 ++ Libs/AceLocale-3.0/AceLocale-3.0.xml | 4 + Libs/AceSerializer-3.0/AceSerializer-3.0.lua | 287 +++ Libs/AceSerializer-3.0/AceSerializer-3.0.xml | 4 + .../CallbackHandler-1.0.lua | 212 ++ .../CallbackHandler-1.0.xml | 4 + Libs/LibDBIcon-1.0/LibDBIcon-1.0.lua | 476 ++++ Libs/LibDBIcon-1.0/lib.xml | 7 + Libs/LibDataBroker-1.1/LibDataBroker-1.1.lua | 90 + Libs/LibDataBroker-1.1/README.textile | 13 + Libs/LibStub/LibStub.lua | 51 + Libs/LibStub/LibStub.toc | 9 + Libs/LibStub/tests/test.lua | 41 + Libs/LibStub/tests/test2.lua | 27 + Libs/LibStub/tests/test3.lua | 14 + Libs/LibStub/tests/test4.lua | 41 + Locales/deDE.lua | 69 + Locales/enUS.lua | 36 + Locales/esES.lua | 70 + Locales/frFR.lua | 69 + Locales/itIT.lua | 69 + Locales/koKR.lua | 69 + Locales/ptBR.lua | 70 + Locales/ruRU.lua | 69 + Locales/zhCN.lua | 69 + Locales/zhTW.lua | 69 + README.md | 80 +- embeds.xml | 17 + locales.xml | 14 + options.lua | 85 + 77 files changed, 15501 insertions(+), 2 deletions(-) create mode 100644 ArenaStats.lua create mode 100644 ArenaStats.toc create mode 100644 GUI.lua create mode 100644 HybridScrollFrame.xml create mode 100644 Libs/AceAddon-3.0/AceAddon-3.0.lua create mode 100644 Libs/AceAddon-3.0/AceAddon-3.0.xml create mode 100644 Libs/AceConfig-3.0/AceConfig-3.0.lua create mode 100644 Libs/AceConfig-3.0/AceConfig-3.0.xml create mode 100644 Libs/AceConfig-3.0/AceConfigCmd-3.0/AceConfigCmd-3.0.lua create mode 100644 Libs/AceConfig-3.0/AceConfigCmd-3.0/AceConfigCmd-3.0.xml create mode 100644 Libs/AceConfig-3.0/AceConfigDialog-3.0/AceConfigDialog-3.0.lua create mode 100644 Libs/AceConfig-3.0/AceConfigDialog-3.0/AceConfigDialog-3.0.xml create mode 100644 Libs/AceConfig-3.0/AceConfigRegistry-3.0/AceConfigRegistry-3.0.lua create mode 100644 Libs/AceConfig-3.0/AceConfigRegistry-3.0/AceConfigRegistry-3.0.xml create mode 100644 Libs/AceConsole-3.0/AceConsole-3.0.lua create mode 100644 Libs/AceConsole-3.0/AceConsole-3.0.xml create mode 100644 Libs/AceDB-3.0/AceDB-3.0.lua create mode 100644 Libs/AceDB-3.0/AceDB-3.0.xml create mode 100644 Libs/AceDBOptions-3.0/AceDBOptions-3.0.lua create mode 100644 Libs/AceDBOptions-3.0/AceDBOptions-3.0.xml create mode 100644 Libs/AceEvent-3.0/AceEvent-3.0.lua create mode 100644 Libs/AceEvent-3.0/AceEvent-3.0.xml create mode 100644 Libs/AceGUI-3.0/AceGUI-3.0.lua create mode 100644 Libs/AceGUI-3.0/AceGUI-3.0.xml create mode 100644 Libs/AceGUI-3.0/widgets/AceGUIContainer-BlizOptionsGroup.lua create mode 100644 Libs/AceGUI-3.0/widgets/AceGUIContainer-DropDownGroup.lua create mode 100644 Libs/AceGUI-3.0/widgets/AceGUIContainer-Frame.lua create mode 100644 Libs/AceGUI-3.0/widgets/AceGUIContainer-InlineGroup.lua create mode 100644 Libs/AceGUI-3.0/widgets/AceGUIContainer-ScrollFrame.lua create mode 100644 Libs/AceGUI-3.0/widgets/AceGUIContainer-SimpleGroup.lua create mode 100644 Libs/AceGUI-3.0/widgets/AceGUIContainer-TabGroup.lua create mode 100644 Libs/AceGUI-3.0/widgets/AceGUIContainer-TreeGroup.lua create mode 100644 Libs/AceGUI-3.0/widgets/AceGUIContainer-Window.lua create mode 100644 Libs/AceGUI-3.0/widgets/AceGUIWidget-Button.lua create mode 100644 Libs/AceGUI-3.0/widgets/AceGUIWidget-CheckBox.lua create mode 100644 Libs/AceGUI-3.0/widgets/AceGUIWidget-ColorPicker.lua create mode 100644 Libs/AceGUI-3.0/widgets/AceGUIWidget-DropDown-Items.lua create mode 100644 Libs/AceGUI-3.0/widgets/AceGUIWidget-DropDown.lua create mode 100644 Libs/AceGUI-3.0/widgets/AceGUIWidget-EditBox.lua create mode 100644 Libs/AceGUI-3.0/widgets/AceGUIWidget-Heading.lua create mode 100644 Libs/AceGUI-3.0/widgets/AceGUIWidget-Icon.lua create mode 100644 Libs/AceGUI-3.0/widgets/AceGUIWidget-InteractiveLabel.lua create mode 100644 Libs/AceGUI-3.0/widgets/AceGUIWidget-Keybinding.lua create mode 100644 Libs/AceGUI-3.0/widgets/AceGUIWidget-Label.lua create mode 100644 Libs/AceGUI-3.0/widgets/AceGUIWidget-MultiLineEditBox.lua create mode 100644 Libs/AceGUI-3.0/widgets/AceGUIWidget-Slider.lua create mode 100644 Libs/AceLocale-3.0/AceLocale-3.0.lua create mode 100644 Libs/AceLocale-3.0/AceLocale-3.0.xml create mode 100644 Libs/AceSerializer-3.0/AceSerializer-3.0.lua create mode 100644 Libs/AceSerializer-3.0/AceSerializer-3.0.xml create mode 100644 Libs/CallbackHandler-1.0/CallbackHandler-1.0.lua create mode 100644 Libs/CallbackHandler-1.0/CallbackHandler-1.0.xml create mode 100644 Libs/LibDBIcon-1.0/LibDBIcon-1.0.lua create mode 100644 Libs/LibDBIcon-1.0/lib.xml create mode 100644 Libs/LibDataBroker-1.1/LibDataBroker-1.1.lua create mode 100644 Libs/LibDataBroker-1.1/README.textile create mode 100644 Libs/LibStub/LibStub.lua create mode 100644 Libs/LibStub/LibStub.toc create mode 100644 Libs/LibStub/tests/test.lua create mode 100644 Libs/LibStub/tests/test2.lua create mode 100644 Libs/LibStub/tests/test3.lua create mode 100644 Libs/LibStub/tests/test4.lua create mode 100644 Locales/deDE.lua create mode 100644 Locales/enUS.lua create mode 100644 Locales/esES.lua create mode 100644 Locales/frFR.lua create mode 100644 Locales/itIT.lua create mode 100644 Locales/koKR.lua create mode 100644 Locales/ptBR.lua create mode 100644 Locales/ruRU.lua create mode 100644 Locales/zhCN.lua create mode 100644 Locales/zhTW.lua create mode 100644 embeds.xml create mode 100644 locales.xml create mode 100644 options.lua diff --git a/.gitignore b/.gitignore index 6fd0a37..808bdf6 100644 --- a/.gitignore +++ b/.gitignore @@ -39,3 +39,6 @@ luac.out *.x86_64 *.hex +# Auto-formatter config +.luaconfig + diff --git a/ArenaStats.lua b/ArenaStats.lua new file mode 100644 index 0000000..1e8d43b --- /dev/null +++ b/ArenaStats.lua @@ -0,0 +1,427 @@ +local addonName = "ArenaStats" +local addonTitle = select(2, _G.GetAddOnInfo(addonName)) +local ArenaStats = _G.LibStub("AceAddon-3.0"):NewAddon(addonName, "AceConsole-3.0", "AceEvent-3.0") +local L = _G.LibStub("AceLocale-3.0"):GetLocale(addonName, true) +local libDBIcon = _G.LibStub("LibDBIcon-1.0") +local AceSerializer = _G.LibStub("AceSerializer-3.0") +local IsActiveBattlefieldArena = IsActiveBattlefieldArena +local GetBattlefieldStatus, GetBattlefieldTeamInfo, GetNumBattlefieldScores, GetBattlefieldScore, GetBattlefieldWinner = GetBattlefieldStatus, + GetBattlefieldTeamInfo, + GetNumBattlefieldScores, + GetBattlefieldScore, + GetBattlefieldWinner +local UnitName, UnitRace, UnitClass, UnitGUID, UnitFactionGroup, UnitIsPlayer = UnitName, UnitRace, UnitClass, UnitGUID, UnitFactionGroup, UnitIsPlayer +local IsInGroup = IsInGroup + +function ArenaStats:OnInitialize() + self.db = _G.LibStub("AceDB-3.0"):New(addonName, {profile = {minimapButton = {hide = false}, maxHistory = 0}, char = {history = {}}}) + self:Print("Tracking ready, have a nice session!") + + self:RegisterEvent("UPDATE_BATTLEFIELD_STATUS") + self:RegisterEvent("UPDATE_BATTLEFIELD_SCORE") + self:RegisterEvent("ARENA_OPPONENT_UPDATE") + + self:DrawMinimapIcon() + self:RegisterOptionsTable() + + self.arenaEnded = false + self.current = {status = "none", stats = {}, units = {}} + self:Reset() +end + +function ArenaStats:UPDATE_BATTLEFIELD_STATUS(_, index) + local status, mapName, instanceID, levelRangeMin, levelRangeMax, teamSize, isRankedArena, suspendedQueue, bool, queueType = GetBattlefieldStatus(index) + if (status == "active" and teamSize > 0 and IsActiveBattlefieldArena()) then + self.arenaEnded = false + self.current["status"] = status + self.current["stats"]["isRanked"] = isRankedArena + self.current["stats"]["zoneId"] = self:ZoneId(mapName) + if (self.current["stats"]["startTime"] == nil or self.current["stats"]["startTime"] == '') then self.current["stats"]["startTime"] = _G.time() end + end +end + +function ArenaStats:SetLastArenaRankingData() + + local playerTeam = '' + local greenTeam = {} + local goldTeam = {} + local myName = UnitName("player") + + for i = 1, GetNumBattlefieldScores() do + local playerName, killingBlows, honorKills, deaths, honorGained, faction, rank, race, class, filename, damageDone, healingDone = GetBattlefieldScore(i) + local teamIndex = select(6, GetBattlefieldScore(i)) + if teamIndex == 0 then + table.insert(greenTeam, playerName) + else + table.insert(goldTeam, playerName) + end + end + + if self:Has_value(greenTeam, myName) then + playerTeam = 'GREEN' + elseif self:Has_value(goldTeam, myName) then + playerTeam = 'GOLD' + end + + for i = 0, 1 do + local teamName, oldTeamRating, newTeamRating, teamMMR = GetBattlefieldTeamInfo(i); + if teamMMR > 0 then + if ((i == 0 and playerTeam == 'GREEN') or (i == 1 and playerTeam == 'GOLD')) then + self.current["stats"]["teamName"] = teamName + self.current["stats"]["oldTeamRating"] = oldTeamRating + self.current["stats"]["newTeamRating"] = newTeamRating + self.current["stats"]["diffRating"] = newTeamRating - oldTeamRating + self.current["stats"]["mmr"] = teamMMR + else + self.current["stats"]["enemyTeamName"] = teamName + self.current["stats"]["enemyOldTeamRating"] = oldTeamRating + self.current["stats"]["enemyNewTeamRating"] = newTeamRating + self.current["stats"]["enemyDiffRating"] = newTeamRating - oldTeamRating + self.current["stats"]["enemyMmr"] = teamMMR + end + end + end + + self.current["stats"]["enemyClass"] = {} + self.current["stats"]["enemyName"] = {} + self.current["stats"]["enemyRace"] = {} + local idx = 0 + for k, v in pairs(self.current["units"]) do + self.current["stats"]["enemyClass"][idx] = v["class"]:upper() + self.current["stats"]["enemyName"][idx] = v["name"] + self.current["stats"]["enemyRace"][idx] = v["race"]:upper() + if self.current["stats"]["enemyFaction"] == nil then + if v["faction"] == 'Alliance' then + self.current["stats"]["enemyFaction"] = 1 + else + self.current["stats"]["enemyFaction"] = 0 + end + end + idx = idx + 1 + end +end + +function ArenaStats:UPDATE_BATTLEFIELD_SCORE() + local battlefieldWinner = GetBattlefieldWinner() + if battlefieldWinner == nil or self.arenaEnded then return end + + if self.current.status ~= 'none' then + self.current["stats"]["endTime"] = _G.time() + self.battlegroundEnded = true + self:SetLastArenaRankingData() + self:RecordArena() + end +end + +function ArenaStats:ARENA_OPPONENT_UPDATE(_, unit, updateReason) + if updateReason == "seen" then + ArenaStats:SpotEnemy(unit) + ArenaStats:SetPartyData() + end +end + +function ArenaStats:SpotEnemy(unit) + if UnitIsPlayer(unit) then + local guid = UnitGUID(unit) + local freshName, realm = UnitName(unit) + if freshName == "Unknown" then freshName = "" end + if (self.current["units"][guid] and not self:StringIsempty(self.current["units"][guid]["name"])) then + freshName = self.current["units"][guid]["name"] + end + + self.current["units"][guid] = { + class = select(2, UnitClass(unit)), + name = freshName, + race = select(2, UnitRace(unit)), + faction = select(2, UnitFactionGroup(unit)) + } + end +end + +function ArenaStats:SetPartyData() + if self.current["stats"]["teamClass"] == nil then self.current["stats"]["teamClass"] = {} end + if self.current["stats"]["teamCharName"] == nil then self.current["stats"]["teamCharName"] = {} end + if self.current["stats"]["teamRace"] == nil then self.current["stats"]["teamRace"] = {} end + + self.current["stats"]["teamClass"][0] = select(2, UnitClass('player')) + self.current["stats"]["teamCharName"][0] = UnitName("player") + self.current["stats"]["teamRace"][0] = select(2, UnitRace("player")) + + if IsInGroup() then + for i = 1, 4 do + if UnitClass('party' .. i) then + local _, englishClass = UnitClass('party' .. i) + local partyPlayerName = UnitName('party' .. i) + self.current["stats"]["teamClass"][i] = englishClass:upper() + self.current["stats"]["teamCharName"][i] = partyPlayerName + self.current["stats"]["teamRace"][i] = select(2, UnitRace('party' .. i)) + end + end + end +end + +function ArenaStats:Reset() + self.arenaEnded = false + self.current["status"] = "none" + self.current["stats"] = {} + self.current["units"] = {} +end + +function ArenaStats:RecordArena() + self:AddEntryToHistory(self.current["stats"]) + self:Print("Arena recorded") + self:Reset() +end + +function ArenaStats:AddEntryToHistory(stats) + table.insert(self.db.char.history, stats) + if self.db.profile.maxHistory > 0 then while (#self.db.char.history > self.db.profile.maxHistory) do table.remove(self.db.char.history, 1) end end +end + +function ArenaStats:DrawMinimapIcon() + libDBIcon:Register(addonName, _G.LibStub("LibDataBroker-1.1"):NewDataObject(addonName, { + type = "data source", + text = addonName, + icon = "interface/icons/achievement_arena_2v2_7", + OnClick = function(self, button) + if button == "RightButton" then + _G.InterfaceOptionsFrame_OpenToCategory(addonName) + _G.InterfaceOptionsFrame_OpenToCategory(addonName) + else + ArenaStats:Toggle() + end + end, + OnTooltipShow = function(tooltip) + tooltip:AddLine(string.format("%s |cff777777v%s|r", addonTitle, "0.7.6")) + tooltip:AddLine(string.format("|cFFCFCFCF%s|r %s", L["Left Click"], L["to open the main window"])) + tooltip:AddLine(string.format("|cFFCFCFCF%s|r %s", L["Right Click"], L["to open options"])) + tooltip:AddLine(string.format("|cFFCFCFCF%s|r %s", L["Drag"], L["to move this button"])) + end + }), self.db.profile.minimapButton) +end + +function ArenaStats:ToggleMinimapButton() + self.db.profile.minimapButton.hide = not self.db.profile.minimapButton.hide + if self.db.profile.minimapButton.hide then + libDBIcon:Hide(addonName) + else + libDBIcon:Show(addonName) + end +end + +function ArenaStats:BuildTable() + local tbl = {} + + for _, row in ipairs(self.db.char.history) do + table.insert(tbl, { + + -- Common stats + + ["startTime"] = row["startTime"], + ["endTime"] = row["endTime"], + ["zoneId"] = row["zoneId"], + ["isRanked"] = row["isRanked"], + ["duration"] = (row["endTime"] - row["startTime"]), + + -- Player's team + + ["teamName"] = row["teamName"], + + ["teamPlayerClass1"] = row["teamClass"] and row["teamClass"][0] or nil, + ["teamPlayerClass2"] = row["teamClass"] and row["teamClass"][1] or nil, + ["teamPlayerClass3"] = row["teamClass"] and row["teamClass"][2] or nil, + ["teamPlayerClass4"] = row["teamClass"] and row["teamClass"][3] or nil, + ["teamPlayerClass5"] = row["teamClass"] and row["teamClass"][4] or nil, + ["teamPlayerName1"] = row["teamCharName"] and row["teamCharName"][0] or nil, + ["teamPlayerName2"] = row["teamCharName"] and row["teamCharName"][1] or nil, + ["teamPlayerName3"] = row["teamCharName"] and row["teamCharName"][2] or nil, + ["teamPlayerName4"] = row["teamCharName"] and row["teamCharName"][3] or nil, + ["teamPlayerName5"] = row["teamCharName"] and row["teamCharName"][4] or nil, + ["teamPlayerRace1"] = row["teamRace"] and row["teamRace"][0] or nil, + ["teamPlayerRace2"] = row["teamRace"] and row["teamRace"][1] or nil, + ["teamPlayerRace3"] = row["teamRace"] and row["teamRace"][2] or nil, + ["teamPlayerRace4"] = row["teamRace"] and row["teamRace"][3] or nil, + ["teamPlayerRace5"] = row["teamRace"] and row["teamRace"][4] or nil, + + ["oldTeamRating"] = row["oldTeamRating"], + ["newTeamRating"] = row["newTeamRating"], + ["diffRating"] = row["diffRating"], + ["mmr"] = row["mmr"], + + -- Enemy team + + ["enemyTeamName"] = row["enemyTeamName"], + + ["enemyPlayerClass1"] = row["enemyClass"] and row["enemyClass"][0] or nil, + ["enemyPlayerClass2"] = row["enemyClass"] and row["enemyClass"][1] or nil, + ["enemyPlayerClass3"] = row["enemyClass"] and row["enemyClass"][2] or nil, + ["enemyPlayerClass4"] = row["enemyClass"] and row["enemyClass"][3] or nil, + ["enemyPlayerClass5"] = row["enemyClass"] and row["enemyClass"][4] or nil, + ["enemyPlayerName1"] = row["enemyName"] and row["enemyName"][0] or nil, + ["enemyPlayerName2"] = row["enemyName"] and row["enemyName"][1] or nil, + ["enemyPlayerName3"] = row["enemyName"] and row["enemyName"][2] or nil, + ["enemyPlayerName4"] = row["enemyName"] and row["enemyName"][3] or nil, + ["enemyPlayerName5"] = row["enemyName"] and row["enemyName"][4] or nil, + ["enemyPlayerRace1"] = row["enemyRace"] and row["enemyRace"][0] or nil, + ["enemyPlayerRace2"] = row["enemyRace"] and row["enemyRace"][1] or nil, + ["enemyPlayerRace3"] = row["enemyRace"] and row["enemyRace"][2] or nil, + ["enemyPlayerRace4"] = row["enemyRace"] and row["enemyRace"][3] or nil, + ["enemyPlayerRace5"] = row["enemyRace"] and row["enemyRace"][4] or nil, + ["enemyFaction"] = row["enemyFaction"], + + ["enemyOldTeamRating"] = row["enemyOldTeamRating"], + ["enemyNewTeamRating"] = row["enemyNewTeamRating"], + ["enemyDiffRating"] = row["enemyDiffRating"], + ["enemyMmr"] = row["enemyMmr"] + + }) + end + return tbl +end + +function ArenaStats:ZoneId(zoneName, x) + if zoneName == L["Nagrand Arena"] then + return 3698 + elseif zoneName == L["Blade's Edge Arena"] then + return 3702 + elseif zoneName == L["Ruins of Lordaeron"] then + return 3968 + end + return nil +end + +function ArenaStats:ZoneNameShort(zoneId) + if zoneId == 3698 then + return L["NA"] + elseif zoneId == 3702 then + return L["BEA"] + elseif zoneId == 3968 then + return L["RoL"] + end + return nil +end + +function ArenaStats:ZoneName(zoneId) + if zoneId == 3698 then + return L["Nagrand Arena"] + elseif zoneId == 3702 then + return L["Blade's Edge Arena"] + elseif zoneId == 3968 then + return L["Ruins of Lordaeron"] + end + return nil +end + +function ArenaStats:ResetDatabase() + self.db:ResetDB() + self:Print(L["Database reset"]) +end + +function ArenaStats:ExportCSV() + + local csv = "isRanked,startTime,endTime,zoneId,duration,teamName," .. + "teamPlayerName1,teamPlayerName2,teamPlayerName3,teamPlayerName4,teamPlayerName5," .. + "teamPlayerClass1,teamPlayerClass2,teamPlayerClass3,teamPlayerClass4,teamPlayerClass5," .. + "teamPlayerRace1,teamPlayerRace2,teamPlayerRace3,teamPlayerRace4,teamPlayerRace5," .. + "oldTeamRating,newTeamRating,diffRating,mmr," .. + "enemyOldTeamRating,enemyNewTeamRating,enemyDiffRating,enemyMmr,enemyTeamName," .. + "enemyPlayerName1,enemyPlayerName2,enemyPlayerName3,enemyPlayerName4,enemyPlayerName5," .. + "enemyPlayerClass1,enemyPlayerClass2,enemyPlayerClass3,enemyPlayerClass4,enemyPlayerClass5," .. + "enemyPlayerRace1,enemyPlayerRace2,enemyPlayerRace3,enemyPlayerRace4,enemyPlayerRace5,enemyFaction".. + "\n" + + for _, row in ipairs(self.db.char.history) do + csv = csv .. (self:YesOrNo(row["isRanked"])) .. "," .. + (row["startTime"] ~= nil and row["startTime"] or "") .. "," .. + (row["endTime"] ~= nil and row["endTime"] or "") .. "," .. + (row["zoneId"] ~= nil and row["zoneId"] or "") .. "," .. + (row["startTime"] ~= nil and row["endTime"] ~= nil and row["endTime"] - row["startTime"] or "") .. "," .. + + (row["teamName"] ~= nil and row["teamName"] or "") .. "," .. + + (row["teamCharName"] and row["teamCharName"][0] ~= nil and row["teamCharName"][0] or "") .. "," .. + (row["teamCharName"] and row["teamCharName"][1] ~= nil and row["teamCharName"][1] or "") .. "," .. + (row["teamCharName"] and row["teamCharName"][2] ~= nil and row["teamCharName"][2] or "") .. "," .. + (row["teamCharName"] and row["teamCharName"][3] ~= nil and row["teamCharName"][3] or "") .. "," .. + (row["teamCharName"] and row["teamCharName"][4] ~= nil and row["teamCharName"][4] or "") .. "," .. + (row["teamClass"] and row["teamClass"][0] ~= nil and row["teamClass"][0] or "") .. "," .. + (row["teamClass"] and row["teamClass"][1] ~= nil and row["teamClass"][1] or "") .. "," .. + (row["teamClass"] and row["teamClass"][2] ~= nil and row["teamClass"][2] or "") .. "," .. + (row["teamClass"] and row["teamClass"][3] ~= nil and row["teamClass"][3] or "") .. "," .. + (row["teamClass"] and row["teamClass"][4] ~= nil and row["teamClass"][4] or "") .. "," .. + (row["teamRace"] and row["teamRace"][0] ~= nil and row["teamRace"][0] or "") .. "," .. + (row["teamRace"] and row["teamRace"][1] ~= nil and row["teamRace"][1] or "") .. "," .. + (row["teamRace"] and row["teamRace"][2] ~= nil and row["teamRace"][2] or "") .. "," .. + (row["teamRace"] and row["teamRace"][3] ~= nil and row["teamRace"][3] or "") .. "," .. + (row["teamRace"] and row["teamRace"][4] ~= nil and row["teamRace"][4] or "") .. "," .. + + (self:ComputeSafeNumber(row["oldTeamRating"])) .. "," .. + (self:ComputeSafeNumber(row["newTeamRating"])) .. "," .. + (self:ComputeSafeNumber(row["diffRating"])) .. "," .. + (self:ComputeSafeNumber(row["mmr"])) .. "," .. + + (self:ComputeSafeNumber(row["enemyOldTeamRating"])) .. "," .. + (self:ComputeSafeNumber(row["enemyNewTeamRating"])) .. "," .. + (self:ComputeSafeNumber(row["enemyDiffRating"])) .. "," .. + (self:ComputeSafeNumber(row["enemyMmr"])) .. "," .. + + (row["enemyTeamName"] ~= nil and row["enemyTeamName"] or "") .. "," .. + + (row["enemyName"][0] ~= nil and row["enemyName"][0] or "") .. "," .. + (row["enemyName"][1] ~= nil and row["enemyName"][1] or "") .. "," .. + (row["enemyName"][2] ~= nil and row["enemyName"][2] or "") .. "," .. + (row["enemyName"][3] ~= nil and row["enemyName"][3] or "") .. "," .. + (row["enemyName"][4] ~= nil and row["enemyName"][4] or "") .. "," .. + (row["enemyClass"][0] ~= nil and row["enemyClass"][0] or "") .. "," .. + (row["enemyClass"][1] ~= nil and row["enemyClass"][1] or "") .. "," .. + (row["enemyClass"][2] ~= nil and row["enemyClass"][2] or "") .. "," .. + (row["enemyClass"][3] ~= nil and row["enemyClass"][3] or "") .. "," .. + (row["enemyClass"][4] ~= nil and row["enemyClass"][4] or "") .. "," .. + (row["enemyRace"][0] ~= nil and row["enemyRace"][0] or "") .. "," .. + (row["enemyRace"][1] ~= nil and row["enemyRace"][1] or "") .. "," .. + (row["enemyRace"][2] ~= nil and row["enemyRace"][2] or "") .. "," .. + (row["enemyRace"][3] ~= nil and row["enemyRace"][3] or "") .. "," .. + (row["enemyRace"][4] ~= nil and row["enemyRace"][4] or "") .. "," .. + + (self:ComputeFaction(row["enemyFaction"])) .. "," .. "\n" + end + + ArenaStats:ExportFrame().eb:SetText(csv) + ArenaStats:ExportFrame():Show() + ArenaStats:ExportFrame().eb:SetFocus() + ArenaStats:ExportFrame().eb:HighlightText(0, ArenaStats:ExportFrame().eb.editBox:GetNumLetters()) +end + +function ArenaStats:Has_value(tab, val) + for index, value in ipairs(tab) do if value == val then return true end end + return false +end + +function ArenaStats:ComputeFaction(factionId) + if factionId == 1 then + return "ALLIANCE" + elseif factionId == 0 then + return "HORDE" + end + return "" +end + +function ArenaStats:YesOrNo(bool) + if bool then + return "YES" + elseif bool == 0 then + return "NO" + end + return "" +end + +function ArenaStats:ComputeSafeNumber(number) + if number == nil then + return "" + elseif number == 0 then + return "0" + end + return number +end + +function ArenaStats:StringIsempty(s) return s == nil or s == '' end diff --git a/ArenaStats.toc b/ArenaStats.toc new file mode 100644 index 0000000..c497097 --- /dev/null +++ b/ArenaStats.toc @@ -0,0 +1,14 @@ +## Interface: 20502 +## Title: ArenaStats - TBC +## Notes: Arena history and statistics with csv export +## Author: Kallias-Sulfuron +## Version: 1 +## SavedVariables: ArenaStats + +embeds.xml +locales.xml + +ArenaStats.lua +GUI.lua +HybridScrollFrame.xml +options.lua diff --git a/GUI.lua b/GUI.lua new file mode 100644 index 0000000..970bb49 --- /dev/null +++ b/GUI.lua @@ -0,0 +1,235 @@ +local addonName = "ArenaStats" +local addonTitle = select(2, _G.GetAddOnInfo(addonName)) +local ArenaStats = _G.LibStub("AceAddon-3.0"):GetAddon(addonName) +local L = _G.LibStub("AceLocale-3.0"):GetLocale(addonName, true) +local AceGUI = _G.LibStub("AceGUI-3.0") +local f, scrollFrame, rows, stats +local exportFrame, exportEditBox + +function ArenaStats:CreateGUI() + f = AceGUI:Create("Frame") + f:Hide() + f:SetWidth(699) + f:EnableResize(false) + + f:SetTitle(addonTitle) + local frameName = addonName .."_MainFrame" + _G[frameName] = f + table.insert(_G.UISpecialFrames, frameName) + f:SetStatusText("Status Bar") + f:SetLayout("Flow") + + + local exportButton = AceGUI:Create("Button") + exportButton:SetWidth(100) + exportButton:SetText(string.format(" %s ", L["Export"])) + f:AddChild(exportButton) + exportButton:SetCallback("OnClick", function() ArenaStats:ExportCSV() end) + + -- TABLE HEADER + local tableHeader = AceGUI:Create("SimpleGroup") + tableHeader:SetFullWidth(true) + tableHeader:SetLayout("Flow") + f:AddChild(tableHeader) + + local margin = AceGUI:Create("Label") + margin:SetWidth(4) + tableHeader:AddChild(margin) + + ArenaStats:CreateScoreButton(tableHeader, 145, "Date") + ArenaStats:CreateScoreButton(tableHeader, 40, "Map") + ArenaStats:CreateScoreButton(tableHeader, 94, "Duration") + ArenaStats:CreateScoreButton(tableHeader, 100, "Team") + ArenaStats:CreateScoreButton(tableHeader, 40, "Rating") + ArenaStats:CreateScoreButton(tableHeader, 100, "Enemy Team") + ArenaStats:CreateScoreButton(tableHeader, 80, "Enemy Faction") + + + -- TABLE + local scrollContainer = AceGUI:Create("SimpleGroup") + scrollContainer:SetFullWidth(true) + scrollContainer:SetFullHeight(true) + scrollContainer:SetLayout("Fill") + f:AddChild(scrollContainer) + + scrollFrame = _G.CreateFrame("ScrollFrame", nil, scrollContainer.frame, "ArenaStatsHybridScrollFrame") + _G.HybridScrollFrame_CreateButtons(scrollFrame, "ArenaStatsHybridScrollListItemTemplate") + scrollFrame.update = function() ArenaStats:UpdateTableView() end + + + -- Export frame + + exportFrame = AceGUI:Create("Frame") + exportFrame:SetWidth(550) + exportFrame.sizer_se:Hide() + exportFrame:SetStatusText("") + exportFrame:SetLayout("Flow") + exportFrame:SetTitle(L["Export"]) + exportFrame:Hide() + exportEditBox = AceGUI:Create("MultiLineEditBox") + exportEditBox:SetLabel('ExportString') + exportEditBox:SetNumLines(29) + exportEditBox:SetText("") + exportEditBox:SetWidth(500) + exportEditBox.button:Hide() + exportEditBox.frame:SetClipsChildren(true) + exportFrame:AddChild(exportEditBox) + exportFrame.eb = exportEditBox +end + +function ArenaStats:UpdateTableView() + self:RefreshLayout() +end + +function ArenaStats:CreateScoreButton(tableHeader, width, localeStr) + btn = AceGUI:Create("Label") + btn:SetWidth(width) + btn:SetText(string.format(" %s ", L[localeStr])) + btn:SetJustifyH("LEFT") + tableHeader:AddChild(btn) + margin = AceGUI:Create("Label") + margin:SetWidth(4) + tableHeader:AddChild(margin) +end + +function ArenaStats:RefreshLayout() + local buttons = _G.HybridScrollFrame_GetButtons(scrollFrame) + local offset = _G.HybridScrollFrame_GetOffset(scrollFrame) + + f:SetStatusText(string.format(L["Recorded %i arenas"], #rows)) + + for buttonIndex = 1, #buttons do + local button = buttons[buttonIndex] + local itemIndex = buttonIndex + offset + local row = rows[itemIndex] + + if (itemIndex <= #rows) then + button:SetID(itemIndex) + button.Date:SetText(_G.date(L["%F %T"], row["endTime"])) + button.Map:SetText(self:ZoneNameShort(row["zoneId"])) + button.Duration:SetText(self:HumanDuration(row["duration"])) + + button.IconTeamPlayerClass1:SetTexture(self:ClassIconId(row["teamPlayerClass1"])) + button.IconTeamPlayerClass2:SetTexture(self:ClassIconId(row["teamPlayerClass2"])) + button.IconTeamPlayerClass3:SetTexture(self:ClassIconId(row["teamPlayerClass3"])) + button.IconTeamPlayerClass4:SetTexture(self:ClassIconId(row["teamPlayerClass4"])) + button.IconTeamPlayerClass5:SetTexture(self:ClassIconId(row["teamPlayerClass5"])) + button.Rating:SetText(row["diffRating"]) + button.Rating:SetTextColor(self:ColorForRating(row["diffRating"])) + button.IconEnemyPlayer1:SetTexture(self:ClassIconId(row["enemyPlayerClass1"])) + button.IconEnemyPlayer2:SetTexture(self:ClassIconId(row["enemyPlayerClass2"])) + button.IconEnemyPlayer3:SetTexture(self:ClassIconId(row["enemyPlayerClass3"])) + button.IconEnemyPlayer4:SetTexture(self:ClassIconId(row["enemyPlayerClass4"])) + button.IconEnemyPlayer5:SetTexture(self:ClassIconId(row["enemyPlayerClass5"])) + button.IconEnemyPlayer5:SetTexture(self:ClassIconId(row["enemyPlayerClass5"])) + button.EnemyFaction:SetTexture(self:FactionIconId(row["enemyFaction"])) + + + button:SetWidth(scrollFrame.scrollChild:GetWidth()) + button:Show() + else + button:Hide() + end + end + + local buttonHeight = scrollFrame.buttonHeight + local totalHeight = #rows * buttonHeight + local shownHeight = #buttons * buttonHeight + + _G.HybridScrollFrame_Update(scrollFrame, totalHeight, shownHeight) +end + +function ArenaStats:Show() + if not f then + self:CreateGUI() + end + + rows = ArenaStats:BuildTable() + + f:Show() + self:RefreshLayout() +end + +function ArenaStats:Hide() + f:Hide() +end + +function ArenaStats:Toggle() + if f and f:IsShown() then + self:Hide() + else + self:Show() + end +end + +function ArenaStats:HumanDuration(seconds) + if seconds < 60 then + return string.format(L["%is"], seconds) + end + local minutes = math.floor(seconds / 60) + if minutes < 60 then + return string.format(L["%im %is"], minutes, (seconds - minutes * 60)) + end + local hours = math.floor(minutes / 60) + return string.format(L["%ih %im"], hours, (minutes - hours * 60)) +end + +function ArenaStats:ClassIconId(className) + + if not className then + return 0 + end + + if className == "MAGE" then + return 626001 + elseif className == "PRIEST" then + return 626004 + elseif className == "DRUID" then + return 625999 + elseif className == "SHAMAN" then + return 626006 + elseif className == "PALADIN" then + return 626003 + elseif className == "WARLOCK" then + return 626007 + elseif className == "WARRIOR" then + return 626008 + elseif className == "HUNTER" then + return 626000 + elseif className == "ROGUE" then + return 626005 + end +end + +function ArenaStats:FactionIconId(factionId) + + if not factionId then + return 0 + end + + if factionId == 0 then + return 132485 + else + return 132486 + end +end + +function ArenaStats:ColorForRating(rating) + + if not rating or rating == 0 then + return 255, 255, 255, 1 + end + + if rating < 0 then + return 255, 0, 0, 1 + else + return 0, 255, 0, 1 + end +end + +function ArenaStats:ExportFrame() + return exportFrame +end +function ArenaStats:ExportEditBox() + return exportEditBox +end \ No newline at end of file diff --git a/HybridScrollFrame.xml b/HybridScrollFrame.xml new file mode 100644 index 0000000..c594447 --- /dev/null +++ b/HybridScrollFrame.xml @@ -0,0 +1,133 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Libs/AceAddon-3.0/AceAddon-3.0.lua b/Libs/AceAddon-3.0/AceAddon-3.0.lua new file mode 100644 index 0000000..1b63bd1 --- /dev/null +++ b/Libs/AceAddon-3.0/AceAddon-3.0.lua @@ -0,0 +1,653 @@ +--- **AceAddon-3.0** provides a template for creating addon objects. +-- It'll provide you with a set of callback functions that allow you to simplify the loading +-- process of your addon.\\ +-- Callbacks provided are:\\ +-- * **OnInitialize**, which is called directly after the addon is fully loaded. +-- * **OnEnable** which gets called during the PLAYER_LOGIN event, when most of the data provided by the game is already present. +-- * **OnDisable**, which is only called when your addon is manually being disabled. +-- @usage +-- -- A small (but complete) addon, that doesn't do anything, +-- -- but shows usage of the callbacks. +-- local MyAddon = LibStub("AceAddon-3.0"):NewAddon("MyAddon") +-- +-- function MyAddon:OnInitialize() +-- -- do init tasks here, like loading the Saved Variables, +-- -- or setting up slash commands. +-- end +-- +-- function MyAddon:OnEnable() +-- -- Do more initialization here, that really enables the use of your addon. +-- -- Register Events, Hook functions, Create Frames, Get information from +-- -- the game that wasn't available in OnInitialize +-- end +-- +-- function MyAddon:OnDisable() +-- -- Unhook, Unregister Events, Hide frames that you created. +-- -- You would probably only use an OnDisable if you want to +-- -- build a "standby" mode, or be able to toggle modules on/off. +-- end +-- @class file +-- @name AceAddon-3.0.lua +-- @release $Id: AceAddon-3.0.lua 1238 2020-08-28 16:18:42Z nevcairiel $ + +local MAJOR, MINOR = "AceAddon-3.0", 13 +local AceAddon, oldminor = LibStub:NewLibrary(MAJOR, MINOR) + +if not AceAddon then return end -- No Upgrade needed. + +AceAddon.frame = AceAddon.frame or CreateFrame("Frame", "AceAddon30Frame") -- Our very own frame +AceAddon.addons = AceAddon.addons or {} -- addons in general +AceAddon.statuses = AceAddon.statuses or {} -- statuses of addon. +AceAddon.initializequeue = AceAddon.initializequeue or {} -- addons that are new and not initialized +AceAddon.enablequeue = AceAddon.enablequeue or {} -- addons that are initialized and waiting to be enabled +AceAddon.embeds = AceAddon.embeds or setmetatable({}, {__index = function(tbl, key) tbl[key] = {} return tbl[key] end }) -- contains a list of libraries embedded in an addon + +-- Lua APIs +local tinsert, tconcat, tremove = table.insert, table.concat, table.remove +local fmt, tostring = string.format, tostring +local select, pairs, next, type, unpack = select, pairs, next, type, unpack +local loadstring, assert, error = loadstring, assert, error +local setmetatable, getmetatable, rawset, rawget = setmetatable, getmetatable, rawset, rawget + +-- Global vars/functions that we don't upvalue since they might get hooked, or upgraded +-- List them here for Mikk's FindGlobals script +-- GLOBALS: LibStub, IsLoggedIn, geterrorhandler + +--[[ + xpcall safecall implementation +]] +local xpcall = xpcall + +local function errorhandler(err) + return geterrorhandler()(err) +end + +local function safecall(func, ...) + -- we check to see if the func is passed is actually a function here and don't error when it isn't + -- this safecall is used for optional functions like OnInitialize OnEnable etc. When they are not + -- present execution should continue without hinderance + if type(func) == "function" then + return xpcall(func, errorhandler, ...) + end +end + +-- local functions that will be implemented further down +local Enable, Disable, EnableModule, DisableModule, Embed, NewModule, GetModule, GetName, SetDefaultModuleState, SetDefaultModuleLibraries, SetEnabledState, SetDefaultModulePrototype + +-- used in the addon metatable +local function addontostring( self ) return self.name end + +-- Check if the addon is queued for initialization +local function queuedForInitialization(addon) + for i = 1, #AceAddon.initializequeue do + if AceAddon.initializequeue[i] == addon then + return true + end + end + return false +end + +--- Create a new AceAddon-3.0 addon. +-- Any libraries you specified will be embeded, and the addon will be scheduled for +-- its OnInitialize and OnEnable callbacks. +-- The final addon object, with all libraries embeded, will be returned. +-- @paramsig [object ,]name[, lib, ...] +-- @param object Table to use as a base for the addon (optional) +-- @param name Name of the addon object to create +-- @param lib List of libraries to embed into the addon +-- @usage +-- -- Create a simple addon object +-- MyAddon = LibStub("AceAddon-3.0"):NewAddon("MyAddon", "AceEvent-3.0") +-- +-- -- Create a Addon object based on the table of a frame +-- local MyFrame = CreateFrame("Frame") +-- MyAddon = LibStub("AceAddon-3.0"):NewAddon(MyFrame, "MyAddon", "AceEvent-3.0") +function AceAddon:NewAddon(objectorname, ...) + local object,name + local i=1 + if type(objectorname)=="table" then + object=objectorname + name=... + i=2 + else + name=objectorname + end + if type(name)~="string" then + error(("Usage: NewAddon([object,] name, [lib, lib, lib, ...]): 'name' - string expected got '%s'."):format(type(name)), 2) + end + if self.addons[name] then + error(("Usage: NewAddon([object,] name, [lib, lib, lib, ...]): 'name' - Addon '%s' already exists."):format(name), 2) + end + + object = object or {} + object.name = name + + local addonmeta = {} + local oldmeta = getmetatable(object) + if oldmeta then + for k, v in pairs(oldmeta) do addonmeta[k] = v end + end + addonmeta.__tostring = addontostring + + setmetatable( object, addonmeta ) + self.addons[name] = object + object.modules = {} + object.orderedModules = {} + object.defaultModuleLibraries = {} + Embed( object ) -- embed NewModule, GetModule methods + self:EmbedLibraries(object, select(i,...)) + + -- add to queue of addons to be initialized upon ADDON_LOADED + tinsert(self.initializequeue, object) + return object +end + + +--- Get the addon object by its name from the internal AceAddon registry. +-- Throws an error if the addon object cannot be found (except if silent is set). +-- @param name unique name of the addon object +-- @param silent if true, the addon is optional, silently return nil if its not found +-- @usage +-- -- Get the Addon +-- MyAddon = LibStub("AceAddon-3.0"):GetAddon("MyAddon") +function AceAddon:GetAddon(name, silent) + if not silent and not self.addons[name] then + error(("Usage: GetAddon(name): 'name' - Cannot find an AceAddon '%s'."):format(tostring(name)), 2) + end + return self.addons[name] +end + +-- - Embed a list of libraries into the specified addon. +-- This function will try to embed all of the listed libraries into the addon +-- and error if a single one fails. +-- +-- **Note:** This function is for internal use by :NewAddon/:NewModule +-- @paramsig addon, [lib, ...] +-- @param addon addon object to embed the libs in +-- @param lib List of libraries to embed into the addon +function AceAddon:EmbedLibraries(addon, ...) + for i=1,select("#", ... ) do + local libname = select(i, ...) + self:EmbedLibrary(addon, libname, false, 4) + end +end + +-- - Embed a library into the addon object. +-- This function will check if the specified library is registered with LibStub +-- and if it has a :Embed function to call. It'll error if any of those conditions +-- fails. +-- +-- **Note:** This function is for internal use by :EmbedLibraries +-- @paramsig addon, libname[, silent[, offset]] +-- @param addon addon object to embed the library in +-- @param libname name of the library to embed +-- @param silent marks an embed to fail silently if the library doesn't exist (optional) +-- @param offset will push the error messages back to said offset, defaults to 2 (optional) +function AceAddon:EmbedLibrary(addon, libname, silent, offset) + local lib = LibStub:GetLibrary(libname, true) + if not lib and not silent then + error(("Usage: EmbedLibrary(addon, libname, silent, offset): 'libname' - Cannot find a library instance of %q."):format(tostring(libname)), offset or 2) + elseif lib and type(lib.Embed) == "function" then + lib:Embed(addon) + tinsert(self.embeds[addon], libname) + return true + elseif lib then + error(("Usage: EmbedLibrary(addon, libname, silent, offset): 'libname' - Library '%s' is not Embed capable"):format(libname), offset or 2) + end +end + +--- Return the specified module from an addon object. +-- Throws an error if the addon object cannot be found (except if silent is set) +-- @name //addon//:GetModule +-- @paramsig name[, silent] +-- @param name unique name of the module +-- @param silent if true, the module is optional, silently return nil if its not found (optional) +-- @usage +-- -- Get the Addon +-- MyAddon = LibStub("AceAddon-3.0"):GetAddon("MyAddon") +-- -- Get the Module +-- MyModule = MyAddon:GetModule("MyModule") +function GetModule(self, name, silent) + if not self.modules[name] and not silent then + error(("Usage: GetModule(name, silent): 'name' - Cannot find module '%s'."):format(tostring(name)), 2) + end + return self.modules[name] +end + +local function IsModuleTrue(self) return true end + +--- Create a new module for the addon. +-- The new module can have its own embeded libraries and/or use a module prototype to be mixed into the module.\\ +-- A module has the same functionality as a real addon, it can have modules of its own, and has the same API as +-- an addon object. +-- @name //addon//:NewModule +-- @paramsig name[, prototype|lib[, lib, ...]] +-- @param name unique name of the module +-- @param prototype object to derive this module from, methods and values from this table will be mixed into the module (optional) +-- @param lib List of libraries to embed into the addon +-- @usage +-- -- Create a module with some embeded libraries +-- MyModule = MyAddon:NewModule("MyModule", "AceEvent-3.0", "AceHook-3.0") +-- +-- -- Create a module with a prototype +-- local prototype = { OnEnable = function(self) print("OnEnable called!") end } +-- MyModule = MyAddon:NewModule("MyModule", prototype, "AceEvent-3.0", "AceHook-3.0") +function NewModule(self, name, prototype, ...) + if type(name) ~= "string" then error(("Usage: NewModule(name, [prototype, [lib, lib, lib, ...]): 'name' - string expected got '%s'."):format(type(name)), 2) end + if type(prototype) ~= "string" and type(prototype) ~= "table" and type(prototype) ~= "nil" then error(("Usage: NewModule(name, [prototype, [lib, lib, lib, ...]): 'prototype' - table (prototype), string (lib) or nil expected got '%s'."):format(type(prototype)), 2) end + + if self.modules[name] then error(("Usage: NewModule(name, [prototype, [lib, lib, lib, ...]): 'name' - Module '%s' already exists."):format(name), 2) end + + -- modules are basically addons. We treat them as such. They will be added to the initializequeue properly as well. + -- NewModule can only be called after the parent addon is present thus the modules will be initialized after their parent is. + local module = AceAddon:NewAddon(fmt("%s_%s", self.name or tostring(self), name)) + + module.IsModule = IsModuleTrue + module:SetEnabledState(self.defaultModuleState) + module.moduleName = name + + if type(prototype) == "string" then + AceAddon:EmbedLibraries(module, prototype, ...) + else + AceAddon:EmbedLibraries(module, ...) + end + AceAddon:EmbedLibraries(module, unpack(self.defaultModuleLibraries)) + + if not prototype or type(prototype) == "string" then + prototype = self.defaultModulePrototype or nil + end + + if type(prototype) == "table" then + local mt = getmetatable(module) + mt.__index = prototype + setmetatable(module, mt) -- More of a Base class type feel. + end + + safecall(self.OnModuleCreated, self, module) -- Was in Ace2 and I think it could be a cool thing to have handy. + self.modules[name] = module + tinsert(self.orderedModules, module) + + return module +end + +--- Returns the real name of the addon or module, without any prefix. +-- @name //addon//:GetName +-- @paramsig +-- @usage +-- print(MyAddon:GetName()) +-- -- prints "MyAddon" +function GetName(self) + return self.moduleName or self.name +end + +--- Enables the Addon, if possible, return true or false depending on success. +-- This internally calls AceAddon:EnableAddon(), thus dispatching a OnEnable callback +-- and enabling all modules of the addon (unless explicitly disabled).\\ +-- :Enable() also sets the internal `enableState` variable to true +-- @name //addon//:Enable +-- @paramsig +-- @usage +-- -- Enable MyModule +-- MyAddon = LibStub("AceAddon-3.0"):GetAddon("MyAddon") +-- MyModule = MyAddon:GetModule("MyModule") +-- MyModule:Enable() +function Enable(self) + self:SetEnabledState(true) + + -- nevcairiel 2013-04-27: don't enable an addon/module if its queued for init still + -- it'll be enabled after the init process + if not queuedForInitialization(self) then + return AceAddon:EnableAddon(self) + end +end + +--- Disables the Addon, if possible, return true or false depending on success. +-- This internally calls AceAddon:DisableAddon(), thus dispatching a OnDisable callback +-- and disabling all modules of the addon.\\ +-- :Disable() also sets the internal `enableState` variable to false +-- @name //addon//:Disable +-- @paramsig +-- @usage +-- -- Disable MyAddon +-- MyAddon = LibStub("AceAddon-3.0"):GetAddon("MyAddon") +-- MyAddon:Disable() +function Disable(self) + self:SetEnabledState(false) + return AceAddon:DisableAddon(self) +end + +--- Enables the Module, if possible, return true or false depending on success. +-- Short-hand function that retrieves the module via `:GetModule` and calls `:Enable` on the module object. +-- @name //addon//:EnableModule +-- @paramsig name +-- @usage +-- -- Enable MyModule using :GetModule +-- MyAddon = LibStub("AceAddon-3.0"):GetAddon("MyAddon") +-- MyModule = MyAddon:GetModule("MyModule") +-- MyModule:Enable() +-- +-- -- Enable MyModule using the short-hand +-- MyAddon = LibStub("AceAddon-3.0"):GetAddon("MyAddon") +-- MyAddon:EnableModule("MyModule") +function EnableModule(self, name) + local module = self:GetModule( name ) + return module:Enable() +end + +--- Disables the Module, if possible, return true or false depending on success. +-- Short-hand function that retrieves the module via `:GetModule` and calls `:Disable` on the module object. +-- @name //addon//:DisableModule +-- @paramsig name +-- @usage +-- -- Disable MyModule using :GetModule +-- MyAddon = LibStub("AceAddon-3.0"):GetAddon("MyAddon") +-- MyModule = MyAddon:GetModule("MyModule") +-- MyModule:Disable() +-- +-- -- Disable MyModule using the short-hand +-- MyAddon = LibStub("AceAddon-3.0"):GetAddon("MyAddon") +-- MyAddon:DisableModule("MyModule") +function DisableModule(self, name) + local module = self:GetModule( name ) + return module:Disable() +end + +--- Set the default libraries to be mixed into all modules created by this object. +-- Note that you can only change the default module libraries before any module is created. +-- @name //addon//:SetDefaultModuleLibraries +-- @paramsig lib[, lib, ...] +-- @param lib List of libraries to embed into the addon +-- @usage +-- -- Create the addon object +-- MyAddon = LibStub("AceAddon-3.0"):NewAddon("MyAddon") +-- -- Configure default libraries for modules (all modules need AceEvent-3.0) +-- MyAddon:SetDefaultModuleLibraries("AceEvent-3.0") +-- -- Create a module +-- MyModule = MyAddon:NewModule("MyModule") +function SetDefaultModuleLibraries(self, ...) + if next(self.modules) then + error("Usage: SetDefaultModuleLibraries(...): cannot change the module defaults after a module has been registered.", 2) + end + self.defaultModuleLibraries = {...} +end + +--- Set the default state in which new modules are being created. +-- Note that you can only change the default state before any module is created. +-- @name //addon//:SetDefaultModuleState +-- @paramsig state +-- @param state Default state for new modules, true for enabled, false for disabled +-- @usage +-- -- Create the addon object +-- MyAddon = LibStub("AceAddon-3.0"):NewAddon("MyAddon") +-- -- Set the default state to "disabled" +-- MyAddon:SetDefaultModuleState(false) +-- -- Create a module and explicilty enable it +-- MyModule = MyAddon:NewModule("MyModule") +-- MyModule:Enable() +function SetDefaultModuleState(self, state) + if next(self.modules) then + error("Usage: SetDefaultModuleState(state): cannot change the module defaults after a module has been registered.", 2) + end + self.defaultModuleState = state +end + +--- Set the default prototype to use for new modules on creation. +-- Note that you can only change the default prototype before any module is created. +-- @name //addon//:SetDefaultModulePrototype +-- @paramsig prototype +-- @param prototype Default prototype for the new modules (table) +-- @usage +-- -- Define a prototype +-- local prototype = { OnEnable = function(self) print("OnEnable called!") end } +-- -- Set the default prototype +-- MyAddon:SetDefaultModulePrototype(prototype) +-- -- Create a module and explicitly Enable it +-- MyModule = MyAddon:NewModule("MyModule") +-- MyModule:Enable() +-- -- should print "OnEnable called!" now +-- @see NewModule +function SetDefaultModulePrototype(self, prototype) + if next(self.modules) then + error("Usage: SetDefaultModulePrototype(prototype): cannot change the module defaults after a module has been registered.", 2) + end + if type(prototype) ~= "table" then + error(("Usage: SetDefaultModulePrototype(prototype): 'prototype' - table expected got '%s'."):format(type(prototype)), 2) + end + self.defaultModulePrototype = prototype +end + +--- Set the state of an addon or module +-- This should only be called before any enabling actually happend, e.g. in/before OnInitialize. +-- @name //addon//:SetEnabledState +-- @paramsig state +-- @param state the state of an addon or module (enabled=true, disabled=false) +function SetEnabledState(self, state) + self.enabledState = state +end + + +--- Return an iterator of all modules associated to the addon. +-- @name //addon//:IterateModules +-- @paramsig +-- @usage +-- -- Enable all modules +-- for name, module in MyAddon:IterateModules() do +-- module:Enable() +-- end +local function IterateModules(self) return pairs(self.modules) end + +-- Returns an iterator of all embeds in the addon +-- @name //addon//:IterateEmbeds +-- @paramsig +local function IterateEmbeds(self) return pairs(AceAddon.embeds[self]) end + +--- Query the enabledState of an addon. +-- @name //addon//:IsEnabled +-- @paramsig +-- @usage +-- if MyAddon:IsEnabled() then +-- MyAddon:Disable() +-- end +local function IsEnabled(self) return self.enabledState end +local mixins = { + NewModule = NewModule, + GetModule = GetModule, + Enable = Enable, + Disable = Disable, + EnableModule = EnableModule, + DisableModule = DisableModule, + IsEnabled = IsEnabled, + SetDefaultModuleLibraries = SetDefaultModuleLibraries, + SetDefaultModuleState = SetDefaultModuleState, + SetDefaultModulePrototype = SetDefaultModulePrototype, + SetEnabledState = SetEnabledState, + IterateModules = IterateModules, + IterateEmbeds = IterateEmbeds, + GetName = GetName, +} +local function IsModule(self) return false end +local pmixins = { + defaultModuleState = true, + enabledState = true, + IsModule = IsModule, +} +-- Embed( target ) +-- target (object) - target object to embed aceaddon in +-- +-- this is a local function specifically since it's meant to be only called internally +function Embed(target, skipPMixins) + for k, v in pairs(mixins) do + target[k] = v + end + if not skipPMixins then + for k, v in pairs(pmixins) do + target[k] = target[k] or v + end + end +end + + +-- - Initialize the addon after creation. +-- This function is only used internally during the ADDON_LOADED event +-- It will call the **OnInitialize** function on the addon object (if present), +-- and the **OnEmbedInitialize** function on all embeded libraries. +-- +-- **Note:** Do not call this function manually, unless you're absolutely sure that you know what you are doing. +-- @param addon addon object to intialize +function AceAddon:InitializeAddon(addon) + safecall(addon.OnInitialize, addon) + + local embeds = self.embeds[addon] + for i = 1, #embeds do + local lib = LibStub:GetLibrary(embeds[i], true) + if lib then safecall(lib.OnEmbedInitialize, lib, addon) end + end + + -- we don't call InitializeAddon on modules specifically, this is handled + -- from the event handler and only done _once_ +end + +-- - Enable the addon after creation. +-- Note: This function is only used internally during the PLAYER_LOGIN event, or during ADDON_LOADED, +-- if IsLoggedIn() already returns true at that point, e.g. for LoD Addons. +-- It will call the **OnEnable** function on the addon object (if present), +-- and the **OnEmbedEnable** function on all embeded libraries.\\ +-- This function does not toggle the enable state of the addon itself, and will return early if the addon is disabled. +-- +-- **Note:** Do not call this function manually, unless you're absolutely sure that you know what you are doing. +-- Use :Enable on the addon itself instead. +-- @param addon addon object to enable +function AceAddon:EnableAddon(addon) + if type(addon) == "string" then addon = AceAddon:GetAddon(addon) end + if self.statuses[addon.name] or not addon.enabledState then return false end + + -- set the statuses first, before calling the OnEnable. this allows for Disabling of the addon in OnEnable. + self.statuses[addon.name] = true + + safecall(addon.OnEnable, addon) + + -- make sure we're still enabled before continueing + if self.statuses[addon.name] then + local embeds = self.embeds[addon] + for i = 1, #embeds do + local lib = LibStub:GetLibrary(embeds[i], true) + if lib then safecall(lib.OnEmbedEnable, lib, addon) end + end + + -- enable possible modules. + local modules = addon.orderedModules + for i = 1, #modules do + self:EnableAddon(modules[i]) + end + end + return self.statuses[addon.name] -- return true if we're disabled +end + +-- - Disable the addon +-- Note: This function is only used internally. +-- It will call the **OnDisable** function on the addon object (if present), +-- and the **OnEmbedDisable** function on all embeded libraries.\\ +-- This function does not toggle the enable state of the addon itself, and will return early if the addon is still enabled. +-- +-- **Note:** Do not call this function manually, unless you're absolutely sure that you know what you are doing. +-- Use :Disable on the addon itself instead. +-- @param addon addon object to enable +function AceAddon:DisableAddon(addon) + if type(addon) == "string" then addon = AceAddon:GetAddon(addon) end + if not self.statuses[addon.name] then return false end + + -- set statuses first before calling OnDisable, this allows for aborting the disable in OnDisable. + self.statuses[addon.name] = false + + safecall( addon.OnDisable, addon ) + + -- make sure we're still disabling... + if not self.statuses[addon.name] then + local embeds = self.embeds[addon] + for i = 1, #embeds do + local lib = LibStub:GetLibrary(embeds[i], true) + if lib then safecall(lib.OnEmbedDisable, lib, addon) end + end + -- disable possible modules. + local modules = addon.orderedModules + for i = 1, #modules do + self:DisableAddon(modules[i]) + end + end + + return not self.statuses[addon.name] -- return true if we're disabled +end + +--- Get an iterator over all registered addons. +-- @usage +-- -- Print a list of all installed AceAddon's +-- for name, addon in AceAddon:IterateAddons() do +-- print("Addon: " .. name) +-- end +function AceAddon:IterateAddons() return pairs(self.addons) end + +--- Get an iterator over the internal status registry. +-- @usage +-- -- Print a list of all enabled addons +-- for name, status in AceAddon:IterateAddonStatus() do +-- if status then +-- print("EnabledAddon: " .. name) +-- end +-- end +function AceAddon:IterateAddonStatus() return pairs(self.statuses) end + +-- Following Iterators are deprecated, and their addon specific versions should be used +-- e.g. addon:IterateEmbeds() instead of :IterateEmbedsOnAddon(addon) +function AceAddon:IterateEmbedsOnAddon(addon) return pairs(self.embeds[addon]) end +function AceAddon:IterateModulesOfAddon(addon) return pairs(addon.modules) end + +-- Blizzard AddOns which can load very early in the loading process and mess with Ace3 addon loading +local BlizzardEarlyLoadAddons = { + Blizzard_DebugTools = true, + Blizzard_TimeManager = true, + Blizzard_BattlefieldMap = true, + Blizzard_MapCanvas = true, + Blizzard_SharedMapDataProviders = true, + Blizzard_CombatLog = true, +} + +-- Event Handling +local function onEvent(this, event, arg1) + -- 2020-08-28 nevcairiel - ignore the load event of Blizzard addons which occur early in the loading process + if (event == "ADDON_LOADED" and (arg1 == nil or not BlizzardEarlyLoadAddons[arg1])) or event == "PLAYER_LOGIN" then + -- if a addon loads another addon, recursion could happen here, so we need to validate the table on every iteration + while(#AceAddon.initializequeue > 0) do + local addon = tremove(AceAddon.initializequeue, 1) + -- this might be an issue with recursion - TODO: validate + if event == "ADDON_LOADED" then addon.baseName = arg1 end + AceAddon:InitializeAddon(addon) + tinsert(AceAddon.enablequeue, addon) + end + + if IsLoggedIn() then + while(#AceAddon.enablequeue > 0) do + local addon = tremove(AceAddon.enablequeue, 1) + AceAddon:EnableAddon(addon) + end + end + end +end + +AceAddon.frame:RegisterEvent("ADDON_LOADED") +AceAddon.frame:RegisterEvent("PLAYER_LOGIN") +AceAddon.frame:SetScript("OnEvent", onEvent) + +-- upgrade embeded +for name, addon in pairs(AceAddon.addons) do + Embed(addon, true) +end + +-- 2010-10-27 nevcairiel - add new "orderedModules" table +if oldminor and oldminor < 10 then + for name, addon in pairs(AceAddon.addons) do + addon.orderedModules = {} + for module_name, module in pairs(addon.modules) do + tinsert(addon.orderedModules, module) + end + end +end diff --git a/Libs/AceAddon-3.0/AceAddon-3.0.xml b/Libs/AceAddon-3.0/AceAddon-3.0.xml new file mode 100644 index 0000000..e6ad639 --- /dev/null +++ b/Libs/AceAddon-3.0/AceAddon-3.0.xml @@ -0,0 +1,4 @@ + +