From f198235de757941a1bad71d29221fc254e83c918 Mon Sep 17 00:00:00 2001 From: Damian Monogue <3660+demonnic@users.noreply.github.com> Date: Thu, 22 Sep 2022 12:01:03 -0400 Subject: [PATCH] Fix looping hooks and add aliasmgr (#38) * Make sure to kill the timer for timer gauges before executing the hook, so it doesn't fire multiple times * Add aliasmgr --- mfile | 2 +- src/resources/aliasmgr.lua | 164 +++++++++++++++++++++++++++++++++++ src/resources/mdkversion.txt | 2 +- src/resources/timergauge.lua | 6 +- 4 files changed, 171 insertions(+), 3 deletions(-) create mode 100644 src/resources/aliasmgr.lua diff --git a/mfile b/mfile index b3751de..4932c6f 100644 --- a/mfile +++ b/mfile @@ -1,6 +1,6 @@ { "package": "MDK", - "version": "2.7.0", + "version": "2.8.0", "author": "Demonnic", "title": "Collection of useful objects/classes", "icon": "computer.png", diff --git a/src/resources/aliasmgr.lua b/src/resources/aliasmgr.lua new file mode 100644 index 0000000..7bf788a --- /dev/null +++ b/src/resources/aliasmgr.lua @@ -0,0 +1,164 @@ +--- Alias Manager +-- @classmod aliasmgr +-- @author Damian Monogue +-- @copyright 2022 Damian Monogue +-- @license MIT, see LICENSE.lua +local aliasmgr = {} +aliasmgr.__index = aliasmgr + +--- Creates a new alias manager +function aliasmgr:new() + local mgr = { + aliases = {} + } + setmetatable(mgr, self) + return mgr +end + +local function argError(funcName, argument, expected, actual) + local msg = string.format("%s: %s as %s expected, got %s", funcName, argument, expected, actual) + printError(msg, true, true) +end + +--- Registers an alias with the alias manager +-- @param name the name for the alias +-- @param regex the regular expression the alias matches against +-- @param func The code to run when the alias matches. Can wrap code in [[ ]] or pass an actual function +function aliasmgr:register(name, regex, func) + local funcName = "aliasmgr:register(name, regex, func)" + if func == nil then + printError(f"{funcName} takes 3 arguments and you have provided less than that", true, true) + end + local nameType = type(name) + if nameType ~= "string" then + argError(funcName, "name", "string", nameType) + end + local regexType = type(regex) + if regexType ~= "string" then + argError(funcName, "regex", "string", regexType) + end + local funcType = type(func) + if funcType ~= "string" and funcType ~= "function" then + argError(funcName, "func", "string or function", funcType) + end + local object = { + regex = regex, + func = func + } + self:kill(name) + local ok, err = pcall(tempAlias, regex, func) + if not ok then + return nil, err + end + object.handlerID = err + self.aliases[name] = object + return true +end + +--- Registers an alias with the alias manager. Alias for register +-- @param name the name for the alias +-- @param regex the regular expression the alias matches against +-- @param func The code to run when the alias matches. Can wrap code in [[ ]] or pass an actual function +-- @see register +function aliasmgr:add(name, regex, func) + self:register(name, regex, func) +end + +--- Disables an alias, but does not delete it so it can be enabled later without being redefined +-- @param name the name of the alias to disable +-- @return true if the alias exists and gets disabled, false if it does not exist or is already disabled +function aliasmgr:disable(name) + local funcName = "aliasmgr:disable(name)" + local nameType = type(name) + if nameType ~= "string" then + argError(funcName, "name", "string", nameType) + end + local object = self.aliases[name] + if not object or object.handlerID == -1 then + return false + end + killAlias(object.handlerID) + object.handlerID = -1 + return true +end + +--- Disables all aliases registered with the manager +function aliasmgr:disableAll() + local aliases = self.aliases + for name, object in pairs(aliases) do + self:disable(name) + end +end + +--- Enables an alias by name +-- @param name the name of the alias to enable +-- @return true if the alias exists and was enabled, false if it does not exist. +function aliasmgr:enable(name) + local funcName = "aliasmgr:enable(name)" + local nameType = type(name) + if nameType ~= "string" then + argError(funcName, "name", "string", nameType) + end + local object = self.aliases[name] + if not object then + return false + end + self:register(name, object.regex, object.func) +end + +--- Enables all aliases registered with the manager +function aliasmgr:enableAll() + local aliases = self.aliases + for name,_ in pairs(aliases) do + self:enable(name) + end + return true +end + +--- Kill an alias, deleting it from the manager +-- @param name the name of the alias to kill +-- @return true if the alias exists and gets deleted, false if the alias does not exist +function aliasmgr:kill(name) + local funcName = "aliasmgr:kill(name)" + local nameType = type(name) + if nameType ~= "string" then + argError(funcName, "name", "string", nameType) + end + local object = self.aliases[name] + if not object then + return false + end + self:disable(name) + self.aliases[name] = nil + return true +end + +--- Kills all aliases registered with the manager, clearing it out +function aliasmgr:killAll() + local aliases = self.aliases + for name, _ in pairs(aliases) do + self:kill(name) + end +end + +--- Kills an alias, deleting it from the manager +-- @param name the name of the alias to delete +-- @return true if the alias exists and gets deleted, false if the alias does not exist +-- @see kill +function aliasmgr:delete(name) + return self:kill(name) +end + +--- Kills all aliases, deleting them from the manager +-- @see killAll +function aliasmgr:deleteAll() + return self:killAll() +end + +--- Returns the list of aliases and the information being tracked for them +-- @return the table of alias information, with names as keys and a table of information as the values. +function aliasmgr:getAliases() + return self.aliases +end + +return aliasmgr \ No newline at end of file diff --git a/src/resources/mdkversion.txt b/src/resources/mdkversion.txt index 6a6a3d8..834f262 100644 --- a/src/resources/mdkversion.txt +++ b/src/resources/mdkversion.txt @@ -1 +1 @@ -2.6.1 +2.8.0 diff --git a/src/resources/timergauge.lua b/src/resources/timergauge.lua index d7a3b61..0e52823 100644 --- a/src/resources/timergauge.lua +++ b/src/resources/timergauge.lua @@ -276,9 +276,13 @@ function TimerGauge:update(skipHook) local text = string.format("%s%s%s", prefix, time, suffix) self:setValue(current, self.time, text) if current == 0 then + if self.timer then + killTimer(self.timer) + self.timer = nil + end if not skipHook then self:executeHook() - end + end end end