EigeenLoader/utils/Macro.lua

121 lines
2.5 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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