1.0.4 支持Macro定义简单宏功能进行循环控制

This commit is contained in:
Eigeen 2023-12-23 00:36:02 +08:00
parent bb9397e934
commit 7a7f087ae0
Signed by: eigeen
GPG Key ID: B730E75AFABD2ED8
3 changed files with 125 additions and 3 deletions

View File

@ -1,5 +1,5 @@
-- 猫猫虫的 LuaEngine 插件管理框架 -- 猫猫虫的 LuaEngine 插件管理框架
-- @version: 1.0.3 -- @version: 1.0.4
-- --
-- 自动加载以下目录的lua模块 -- 自动加载以下目录的lua模块
-- ./eigeen_modules -- ./eigeen_modules
@ -23,6 +23,7 @@
-- --
local Speed = require("utils/Speed") local Speed = require("utils/Speed")
local Keypad = require("utils/Keypad") local Keypad = require("utils/Keypad")
local Macro = require("utils/Macro")
-- consts -- consts
local MODULE_ROOT = "./Lua/eigeen_modules" local MODULE_ROOT = "./Lua/eigeen_modules"
@ -201,7 +202,8 @@ function on_time()
worldData = engine.World:new(), worldData = engine.World:new(),
questData = engine.Quest:new(), questData = engine.Quest:new(),
Speed = Speed:new(playerData), Speed = Speed:new(playerData),
Keypad = Keypad Keypad = Keypad,
Macro = Macro
} }
-- local MetaCtx = { -- local MetaCtx = {
-- Speed = Speed:new(Ctx.playerData), -- Speed = Speed:new(Ctx.playerData),

View File

@ -15,7 +15,7 @@ function Keypad.UseChronoscope(keypadID, chrono, func, xbox)
if chrono < 0 then if chrono < 0 then
return return
end end
if xbox ~= true and xbox ~= false then if type(xbox) ~= "boolean" then
xbox = nil xbox = nil
end end

120
utils/Macro.lua Normal file
View File

@ -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