From 4e697fa0a2c33ee73388b60e5b10635b2599a791 Mon Sep 17 00:00:00 2001 From: ck Date: Thu, 15 Feb 2024 22:26:36 -0600 Subject: [PATCH] Clean up code. --- src/Command/Command.lua | 2 + src/Item/Item.lua | 4 +- src/Library/Library.lua | 52 +++++ src/Module/Module.lua | 47 +++++ src/MythicPlusUtils/MythicPlusUtils.lua | 24 ++- src/NotificationList/NotificationList.lua | 2 + src/Spell/Spell.lua | 4 +- src/TimeToDie/TimeToDie.lua | 8 + src/Timer/Timer.lua | 18 +- src/Util/Util.lua | 62 ++++++ src/_bastion.lua | 246 +++------------------- 11 files changed, 243 insertions(+), 226 deletions(-) diff --git a/src/Command/Command.lua b/src/Command/Command.lua index 8b31b65..ca0abf1 100644 --- a/src/Command/Command.lua +++ b/src/Command/Command.lua @@ -81,4 +81,6 @@ function Command:PrintHelp() end end +Bastion.Globals.Command = Command:New("bastion") + Bastion.Command = Command diff --git a/src/Item/Item.lua b/src/Item/Item.lua index baad0ed..df7b18c 100644 --- a/src/Item/Item.lua +++ b/src/Item/Item.lua @@ -236,7 +236,7 @@ function Item:UseByID(unit) RunMacroText(string.format("/use [@%s] item:%d", target, self:GetID())) - Bastion:Debug("Using by id", self) + Bastion.Util:Debug("Using by id", self) end -- Use the Item @@ -268,7 +268,7 @@ function Item:Use(unit, condition) -- Check if the mouse was looking self.wasLooking = IsMouselooking() UseItemByName(self.traits.use.byId and self:GetID() or self:GetName(), target:GetOMToken()) - Bastion:Debug("Using", self) + Bastion.Util:Debug("Using", self) -- Set the last Use time self.lastUseAttempt = GetTime() diff --git a/src/Library/Library.lua b/src/Library/Library.lua index 0d6a506..dbcfef5 100644 --- a/src/Library/Library.lua +++ b/src/Library/Library.lua @@ -119,4 +119,56 @@ function Library:Import(library) return lib:Resolve() end +local LIBRARIES = {} + +---@param library Bastion.Library +function Bastion:RegisterLibrary(library) + LIBRARIES[library.name] = library +end + +---@param name string +function Bastion:GetLibrary(name) + if not LIBRARIES[name] then + error("Library " .. name .. " not found") + end + + return LIBRARIES[name] +end + +---@param library string +function Bastion:Import(library) + local lib = self:GetLibrary(library) + + if not lib then + error("Library " .. library .. " not found") + end + + return lib:Resolve() +end + +function Bastion:CheckLibraryDependencies() + for k, v in pairs(LIBRARIES) do + if v.dependencies then + for i = 1, #v.dependencies do + local dep = v.dependencies[i] + if LIBRARIES[dep] then + if LIBRARIES[dep].dependencies then + for j = 1, #LIBRARIES[dep].dependencies do + if LIBRARIES[dep].dependencies[j] == v.name then + Bastion.Util:Print("Circular dependency detected between " .. v.name .. " and " .. dep) + return false + end + end + end + else + Bastion.Util:Print("Library " .. v.name .. " depends on " .. dep .. " but it's not registered") + return false + end + end + end + end + + return true +end + Bastion.Library = Library diff --git a/src/Module/Module.lua b/src/Module/Module.lua index b081a18..c11b1a2 100644 --- a/src/Module/Module.lua +++ b/src/Module/Module.lua @@ -92,3 +92,50 @@ function Module:Tick() end Bastion.Module = Module + +---@type Bastion.Module[] +local MODULES = {} + +function Bastion:TickModules() + for i = 1, #MODULES do + MODULES[i]:Tick() + end +end + +-- Find a module by name +---@param name string +function Bastion:FindModule(name) + for i = 1, #MODULES do + if MODULES[i].name == name then + return MODULES[i] + end + end + return false +end + +---@param module Bastion.Module +function Bastion:Register(module) + local foundModule = Bastion:FindModule(module.name) + if not foundModule then + table.insert(MODULES, module) + Bastion.Util:Print("Registered", module) + else + Bastion.Util:Print("Module already registered", module) + return + end +end + +---@param args { [2]: string } +Bastion.Globals.Command:Register("module", "Toggle a module on/off", function(args) + local module = Bastion:FindModule(args[2]) + if module then + module:Toggle() + if module.enabled then + Bastion.Util:Print("Enabled", module.name) + else + Bastion.Util:Print("Disabled", module.name) + end + else + Bastion.Util:Print("Module not found") + end +end) diff --git a/src/MythicPlusUtils/MythicPlusUtils.lua b/src/MythicPlusUtils/MythicPlusUtils.lua index 67b2005..72ee869 100644 --- a/src/MythicPlusUtils/MythicPlusUtils.lua +++ b/src/MythicPlusUtils/MythicPlusUtils.lua @@ -1278,7 +1278,7 @@ end function MythicPlusUtils:CastingCriticalKick(unit, percent) local castingSpell = unit:GetCastingOrChannelingSpell() - if castingSpell then + if castingSpell and type(castingSpell) == "table" then local spellID = castingSpell:GetID() local kickEntry = self.kickList[spellID] if not kickEntry then @@ -1307,7 +1307,7 @@ end function MythicPlusUtils:CastingCriticalStun(unit, percent) local castingSpell = unit:GetCastingOrChannelingSpell() - if castingSpell then + if castingSpell and type(castingSpell) == "table" then local spellID = castingSpell:GetID() local kickEntry = self.kickList[spellID] if not kickEntry then @@ -1337,3 +1337,23 @@ function MythicPlusUtils:IsAOEBoss(unit) end Bastion.MythicPlusUtils = MythicPlusUtils:New() + +Bastion.Globals.Command:Register("mplus", "Toggle m+ module on/off", function(args) + local cmd = args[2] + if cmd == "debuffs" then + Bastion.MythicPlusUtils:ToggleDebuffLogging() + Bastion.Util:Print("Debuff logging", Bastion.MythicPlusUtils.debuffLogging and "enabled" or "disabled") + return + end + + if cmd == "casts" then + Bastion.MythicPlusUtils:ToggleCastLogging() + Bastion.Util:Print("Cast logging", Bastion.MythicPlusUtils.castLogging and "enabled" or "disabled") + return + end + + Bastion.Util:Print("[MythicPlusUtils] Unknown command") + Bastion.Util:Print("Available commands:") + Bastion.Util:Print("debuffs") + Bastion.Util:Print("casts") +end) diff --git a/src/NotificationList/NotificationList.lua b/src/NotificationList/NotificationList.lua index cb09dcb..a7c7988 100644 --- a/src/NotificationList/NotificationList.lua +++ b/src/NotificationList/NotificationList.lua @@ -90,4 +90,6 @@ function NotificationList:RemoveAllNotifications() end end +Bastion.Globals.Notifications = NotificationList:New() + Bastion.NotificationList = NotificationList diff --git a/src/Spell/Spell.lua b/src/Spell/Spell.lua index 91e74f2..510154b 100644 --- a/src/Spell/Spell.lua +++ b/src/Spell/Spell.lua @@ -269,7 +269,7 @@ function Spell:Cast(unit, condition) CastSpellByName(self:GetName(), u) SpellCancelQueuedSpell() - Bastion:Debug("Casting", self) + Bastion.Util:Debug("Casting", self) -- Set the last cast time self.lastCastAttempt = GetTime() @@ -300,7 +300,7 @@ function Spell:ForceCast(unit) CastSpellByName(self:GetName(), u) SpellCancelQueuedSpell() - Bastion:Debug("Force Casting", self) + Bastion.Util:Debug("Force Casting", self) -- Set the last cast time self.lastCastAttempt = GetTime() diff --git a/src/TimeToDie/TimeToDie.lua b/src/TimeToDie/TimeToDie.lua index 7439810..e4144b0 100644 --- a/src/TimeToDie/TimeToDie.lua +++ b/src/TimeToDie/TimeToDie.lua @@ -44,6 +44,7 @@ local TimeToDie = { PLAYER = -6, -- 25 DOES_NOT_EXIST = -7, }, + NextRefresh = 0 } function TimeToDie:IterableUnits() @@ -52,6 +53,13 @@ end function TimeToDie:Refresh() local currentTime = GetTime() + + if currentTime >= TimeToDie.NextRefresh then + TimeToDie.NextRefresh = currentTime + TimeToDie.Settings.Refresh + else + return + end + local historyCount = TimeToDie.Settings.HistoryCount local historyTime = TimeToDie.Settings.HistoryTime local ttdCache = TimeToDie.Cache diff --git a/src/Timer/Timer.lua b/src/Timer/Timer.lua index 336a670..616731b 100644 --- a/src/Timer/Timer.lua +++ b/src/Timer/Timer.lua @@ -6,19 +6,25 @@ Bastion = ... -- Create a new Timer class ---@class Bastion.Timer ---@field type string +---@field cb false | fun():boolean local Timer = { startTime = nil, resetAfterCombat = false, + cb = false } Timer.__index = Timer -- Constructor ---@param type string +---@param cb? fun():boolean ---@return Bastion.Timer -function Timer:New(type) +function Timer:New(type, cb) local self = setmetatable({}, Timer) self.startTime = nil self.type = type + if cb then + self.cb = cb + end return self end @@ -49,4 +55,14 @@ function Timer:Reset() self.startTime = nil end +function Timer:Check() + if self.cb then + if not self:IsRunning() and self.cb() then + self:Start() + elseif self:IsRunning() and not self.cb() then + self:Reset() + end + end +end + Bastion.Timer = Timer diff --git a/src/Util/Util.lua b/src/Util/Util.lua index 841f181..71c6eb2 100644 --- a/src/Util/Util.lua +++ b/src/Util/Util.lua @@ -25,4 +25,66 @@ local compareThisTable = { function Util:CompareThis(operator, a, b) end +local PrintEnabled = false + +function Util:Print(...) + if not PrintEnabled then + return + end + local args = { ... } + local str = "|cFFDF362D[Bastion]|r |cFFFFFFFF" + for i = 1, #args do + str = str .. tostring(args[i]) .. " " + end + print(str) +end + +function Util:TogglePrint() + PrintEnabled = not PrintEnabled + if PrintEnabled then + Util:Print("Enabled") + else + Util:Print("Disabled") + end +end + +function Util:GetPrintMode() + return PrintEnabled +end + +Bastion.Globals.Command:Register("toggle", "Toggle bastion on/off", function() + Util:TogglePrint() +end) + +local DebugMode = false + +function Util:ToggleDebug() + DebugMode = not DebugMode + if DebugMode then + Util:Print("Debug mode enabled") + else + Util:Print("Debug mode disabled") + end +end + +function Util:GetDebugMode() + return DebugMode +end + +function Util:Debug(...) + if not DebugMode then + return + end + local args = { ... } + local str = "|cFFDF6520[Bastion]|r |cFFFFFFFF" + for i = 1, #args do + str = str .. tostring(args[i]) .. " " + end + print(str) +end + +Bastion.Globals.Command:Register("debug", "Toggle debug mode on/off", function() + Util:ToggleDebug() +end) + Bastion.Util = Util diff --git a/src/_bastion.lua b/src/_bastion.lua index 49b6e24..72070ea 100644 --- a/src/_bastion.lua +++ b/src/_bastion.lua @@ -1,16 +1,21 @@ ---@type Tinkr local Tinkr = ... +---@class Bastion.Globals.SpellName : { [spellId]: string } + ---@class Bastion local Bastion = { - DebugMode = false, - Globals = {}, + Enabled = false, + Globals = { + ---@type Bastion.Globals.SpellName + SpellName = {} + }, + Tick = GetGameTick() } -local TinkrScriptsBase = "scripts" -local BastionBase = string.format("%s/%s", TinkrScriptsBase, "bastion") +local BastionBase = string.format("%s/%s", "scripts", "bastion") local BastionScriptsBase = string.format("%s/%s", BastionBase, "scripts") -local ThirdPartyModulesBase = string.format("%s/%s", TinkrScriptsBase, "BastionScripts") +local ThirdPartyModulesBase = string.format("%s/%s", "scripts", "BastionScripts") Bastion.__index = Bastion @@ -149,13 +154,13 @@ end local bastionFiles = { "~/src/ClassMagic/ClassMagic", "~/src/List/List", + "~/src/Command/Command", "~/src/Util/Util", "~/src/Library/Library", "~/src/Notification/Notification", "~/src/NotificationList/NotificationList", "~/src/Vector3/Vector3", "~/src/Sequencer/Sequencer", - "~/src/Command/Command", "~/src/Cache/Cache", "~/src/Cacheable/Cacheable", "~/src/Refreshable/Refreshable", @@ -184,23 +189,15 @@ for i = 1, #bastionFiles do Bastion:Require(bastionFiles[i]) end -Bastion.Globals.CombatTimer = Bastion.Timer:New("combat") -Bastion.Globals.Notifications = Bastion.NotificationList:New() +Bastion.Globals.CombatTimer = Bastion.Timer:New("combat", function() + return UnitAffectingCombat("player") +end) ---@param unitTarget UnitId Bastion.Globals.EventManager:RegisterWoWEvent("UNIT_HEALTH", function(unitTarget) --Bastion.UnitManager:Get(unitTarget):UpdateHealth() end) - ----@type table -local LIBRARIES = {} - ----@type Bastion.Module[] -local MODULES = {} - -Bastion.Enabled = false - ---@param unit UnitToken ---@param auras UnitAuraUpdateInfo Bastion.Globals.EventManager:RegisterWoWEvent("UNIT_AURA", function(unit, auras) @@ -229,9 +226,6 @@ end) local playerGuid = UnitGUID("player") local missed = {} ----@class Bastion.Globals.SpellName : { [spellId]: string } -Bastion.Globals.SpellName = {} - Bastion.Globals.EventManager:RegisterWoWEvent("COMBAT_LOG_EVENT_UNFILTERED", function() local args = { CombatLogGetCurrentEventInfo() } @@ -246,30 +240,32 @@ Bastion.Globals.EventManager:RegisterWoWEvent("COMBAT_LOG_EVENT_UNFILTERED", fun ---@type string local spellName = args[13] - if subEvent:find("SPELL") == 1 or subEvent:find("RANGE") == 1 then + if subEvent == "SPELL_CAST_SUCCESS" then if (not Bastion.Globals.SpellName[spellID] or Bastion.Globals.SpellName[spellID] ~= spellName) then Bastion.Globals.SpellName[spellID] = spellName end end + ---@type Bastion.Unit? local u = Bastion.UnitManager[sourceGUID] + ---@type Bastion.Unit local u2 = Bastion.UnitManager[destGUID] local t = GetTime() - if u then + if type(u) ~= "nil" then u:SetLastCombatTime(t) end - if u2 then + if type(u2) ~= "nil" then u2:SetLastCombatTime(t) if subEvent == "SPELL_MISSED" and sourceGUID == playerGuid and spellID == 408 then local missType = args[15] - if missType == "IMMUNE" then + if type(u) ~= "nil" and missType == "IMMUNE" then local castingSpell = u:GetCastingOrChannelingSpell() - if castingSpell then + if castingSpell and type(castingSpell) == "table" then if not missed[castingSpell:GetID()] then missed[castingSpell:GetID()] = true end @@ -279,97 +275,18 @@ Bastion.Globals.EventManager:RegisterWoWEvent("COMBAT_LOG_EVENT_UNFILTERED", fun end end) -local Timer = { - TTD = 0 -} - -Bastion.Now = GetTime() -Bastion.Tick = GetGameTick() - Bastion.Ticker = C_Timer.NewTicker(0.1, function() - Bastion.Now = GetTime() Bastion.Tick = GetGameTick() - - if not Bastion.Globals.CombatTimer:IsRunning() and UnitAffectingCombat("player") then - Bastion.Globals.CombatTimer:Start() - elseif Bastion.Globals.CombatTimer:IsRunning() and not UnitAffectingCombat("player") then - Bastion.Globals.CombatTimer:Reset() - end + Bastion.Globals.CombatTimer:Check() if Bastion.Enabled then Bastion.ObjectManager:Refresh() - if Bastion.Now > Timer.TTD then - Timer.TTD = Bastion.Now + Bastion.TimeToDie.Settings.Refresh - Bastion.TimeToDie:Refresh() - end - for i = 1, #MODULES do - MODULES[i]:Tick() - end + Bastion.TimeToDie:Refresh() + Bastion:TickModules() end end) ----@param module Bastion.Module -function Bastion:Register(module) - table.insert(MODULES, module) - Bastion:Print("Registered", module) -end - --- Find a module by name -function Bastion:FindModule(name) - for i = 1, #MODULES do - if MODULES[i].name == name then - return MODULES[i] - end - end -end - -Bastion.PrintEnabled = false - -function Bastion:Print(...) - if not Bastion.PrintEnabled then - return - end - local args = { ... } - local str = "|cFFDF362D[Bastion]|r |cFFFFFFFF" - for i = 1, #args do - str = str .. tostring(args[i]) .. " " - end - print(str) -end - -function Bastion:Debug(...) - if not Bastion.DebugMode then - return - end - local args = { ... } - local str = "|cFFDF6520[Bastion]|r |cFFFFFFFF" - for i = 1, #args do - str = str .. tostring(args[i]) .. " " - end - print(str) -end - -local Command = Bastion.Command:New("bastion") - -Command:Register("toggle", "Toggle bastion on/off", function() - Bastion.Enabled = not Bastion.Enabled - if Bastion.Enabled then - Bastion:Print("Enabled") - else - Bastion:Print("Disabled") - end -end) - -Command:Register("debug", "Toggle debug mode on/off", function() - Bastion.DebugMode = not Bastion.DebugMode - if Bastion.DebugMode then - Bastion:Print("Debug mode enabled") - else - Bastion:Print("Debug mode disabled") - end -end) - -Command:Register("dumpspells", "Dump spells to a file", function() +Bastion.Globals.Command:Register("dumpspells", "Dump spells to a file", function() local i = 1 local rand = math.random(100000, 999999) while true do @@ -392,121 +309,12 @@ Command:Register("dumpspells", "Dump spells to a file", function() end end) -Command:Register("module", "Toggle a module on/off", function(args) - local module = Bastion:FindModule(args[2]) - if module then - module:Toggle() - if module.enabled then - Bastion:Print("Enabled", module.name) - else - Bastion:Print("Disabled", module.name) - end - else - Bastion:Print("Module not found") - end -end) - -Command:Register("mplus", "Toggle m+ module on/off", function(args) - local cmd = args[2] - if cmd == "debuffs" then - Bastion.MythicPlusUtils:ToggleDebuffLogging() - Bastion:Print("Debuff logging", Bastion.MythicPlusUtils.debuffLogging and "enabled" or "disabled") - return - end - - if cmd == "casts" then - Bastion.MythicPlusUtils:ToggleCastLogging() - Bastion:Print("Cast logging", Bastion.MythicPlusUtils.castLogging and "enabled" or "disabled") - return - end - - Bastion:Print("[MythicPlusUtils] Unknown command") - Bastion:Print("Available commands:") - Bastion:Print("debuffs") - Bastion:Print("casts") -end) - -Command:Register("missed", "Dump the list of immune kidney shot spells", function() +Bastion.Globals.Command:Register("missed", "Dump the list of immune kidney shot spells", function() for k, v in pairs(missed) do - Bastion:Print(k) + Bastion.Util:Print(k) end end) ----@param library Bastion.Library -function Bastion:RegisterLibrary(library) - LIBRARIES[library.name] = library -end - -function Bastion:CheckLibraryDependencies() - for k, v in pairs(LIBRARIES) do - if v.dependencies then - for i = 1, #v.dependencies do - local dep = v.dependencies[i] - if LIBRARIES[dep] then - if LIBRARIES[dep].dependencies then - for j = 1, #LIBRARIES[dep].dependencies do - if LIBRARIES[dep].dependencies[j] == v.name then - Bastion:Print("Circular dependency detected between " .. v.name .. " and " .. dep) - return false - end - end - end - else - Bastion:Print("Library " .. v.name .. " depends on " .. dep .. " but it's not registered") - return false - end - end - end - end - - return true -end - ----@param library string -function Bastion:Import(library) - local lib = self:GetLibrary(library) - - if not lib then - error("Library " .. library .. " not found") - end - - return lib:Resolve() -end - ----@param name string -function Bastion:GetLibrary(name) - if not LIBRARIES[name] then - error("Library " .. name .. " not found") - end - - local library = LIBRARIES[name] - - -- if library.dependencies then - -- for i = 1, #library.dependencies do - -- local dep = library.dependencies[i] - -- if LIBRARIES[dep] then - -- if LIBRARIES[dep].dependencies then - -- for j = 1, #LIBRARIES[dep].dependencies do - -- if LIBRARIES[dep].dependencies[j] == library.name then - -- Bastion:Print("Circular dependency detected between " .. library.name .. " and " .. dep) - -- return false - -- end - -- end - -- end - -- else - -- Bastion:Print("Library " .. v.name .. " depends on " .. dep .. " but it's not registered") - -- return false - -- end - -- end - -- end - - return library -end - --- if not Bastion:CheckLibraryDependencies() then --- return --- end - Load("@Libraries/") Load("@Modules/") Load("@")