From 7a7f087ae08ad8faa4a052f039cd387161d1caa2 Mon Sep 17 00:00:00 2001 From: Eigeen Date: Sat, 23 Dec 2023 00:36:02 +0800 Subject: [PATCH] =?UTF-8?q?1.0.4=20=E6=94=AF=E6=8C=81Macro=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E7=AE=80=E5=8D=95=E5=AE=8F=E5=8A=9F=E8=83=BD=E8=BF=9B?= =?UTF-8?q?=E8=A1=8C=E5=BE=AA=E7=8E=AF=E6=8E=A7=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- EigeenLoader.lua | 6 ++- utils/Keypad.lua | 2 +- utils/Macro.lua | 120 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+), 3 deletions(-) create mode 100644 utils/Macro.lua diff --git a/EigeenLoader.lua b/EigeenLoader.lua index 4a4d6f8..ab4f9a0 100644 --- a/EigeenLoader.lua +++ b/EigeenLoader.lua @@ -1,5 +1,5 @@ -- 猫猫虫的 LuaEngine 插件管理框架 --- @version: 1.0.3 +-- @version: 1.0.4 -- -- 自动加载以下目录的lua模块: -- ./eigeen_modules @@ -23,6 +23,7 @@ -- local Speed = require("utils/Speed") local Keypad = require("utils/Keypad") +local Macro = require("utils/Macro") -- consts local MODULE_ROOT = "./Lua/eigeen_modules" @@ -201,7 +202,8 @@ function on_time() worldData = engine.World:new(), questData = engine.Quest:new(), Speed = Speed:new(playerData), - Keypad = Keypad + Keypad = Keypad, + Macro = Macro } -- local MetaCtx = { -- Speed = Speed:new(Ctx.playerData), diff --git a/utils/Keypad.lua b/utils/Keypad.lua index c78a767..f039a0d 100644 --- a/utils/Keypad.lua +++ b/utils/Keypad.lua @@ -15,7 +15,7 @@ function Keypad.UseChronoscope(keypadID, chrono, func, xbox) if chrono < 0 then return end - if xbox ~= true and xbox ~= false then + if type(xbox) ~= "boolean" then xbox = nil end diff --git a/utils/Macro.lua b/utils/Macro.lua new file mode 100644 index 0000000..39d19fe --- /dev/null +++ b/utils/Macro.lua @@ -0,0 +1,120 @@ +local Macro = {} +Macro.__index = Macro + +-- Macro 宏类 +-- 提供宏定义和执行的功能 +-- +-- 宏命令支持以下类别: +-- {} 空表,占位符 +-- { placeholder = 3 } 占位符,placeholder 表示占位符个数(这样不用重复塞多个空表了) +-- 其他类型都视为参数 +-- +-- 注意:表中含有 nil 可能会造成不可预料的问题 + +function Macro:new(macroSeries) + if not macroSeries or type(macroSeries) ~= "table" then + return nil + end + + local obj = { + rawSeries = macroSeries, + macroLen = 0, + compiledSeries = nil, + compiledLen = 0, + pointer = 0 + } + setmetatable(obj, Macro) + + obj:compile() + return obj +end + +function Macro:compile() + self.compiledSeries = {} + for _, v in ipairs(self.rawSeries) do + if type(v) ~= "table" then + table.insert(self.compiledSeries, v) + goto continue + end + if v == nil or next(v) == nil then + -- 是nil或空表 + table.insert(self.compiledSeries, {}) + elseif v.placeholder then + for i = 1, v.placeholder do + table.insert(self.compiledSeries, {}) + end + else + -- 非特殊指令 + table.insert(self.compiledSeries, v) + end + + ::continue:: + end + self.compiledLen = #self.compiledSeries +end + +function Macro:getNext(loop) + if self.pointer >= self.compiledLen then + if not loop then + return nil + end + self:reset() + end + + self.pointer = self.pointer + 1 + local cmd = self.compiledSeries[self.pointer] + if cmd == nil then -- 理论不应该是nil,此处保险 + return nil + end + if type(cmd) ~= "table" then + return cmd + end + if next(cmd) == nil then + return nil + end + return cmd +end + +function Macro:reset() + self.pointer = 0 +end + +function Macro:runNext(func, forceRun) + if not func or type(func) ~= "function" then + return + end + if type(forceRun) ~= "boolean" then + forceRun = false + end + + local cmd = self:getNext(false) + if not cmd then + if forceRun then + func(nil) + end + return + end + + func(cmd) +end + +function Macro:runNextLoop(func, forceRun) + if not func or type(func) ~= "function" then + return + end + if type(forceRun) ~= "boolean" then + forceRun = false + end + + local cmd = self:getNext(true) + if not cmd then + if forceRun then + func(nil) + end + return + end + + func(cmd) +end + +return Macro