LuaAuxLib庫
LuaAuxLib是按鍵精靈所有內置命令所在的庫文件,有多種方式來獲取LuaAuxLib庫下的函數命令,例如反編譯按鍵精靈手機端庫文件等。這里咱們來介紹一種淺顯易懂的方式來獲取,直接for循環遍歷獲取函數名。
ScanLuaAuxLib
我們寫一個自定義插件函數ScanLuaAuxLib來實現遍歷循環LuaAuxLib內的鍵值對內容,因為LuaAuxLib庫的本質也是一個table數據,在這個table中的鍵名就是我們需要的命令名。遍歷代碼如下:
調試運行獲取所有命令名
我們來調試運行下這個插件命令,看看輸出的內容都是什么。
在調試信息中,所有命令名稱都被輸出顯示,后面類型大部分都是function,表示命令函數的意思。從這些函數名稱中,可以推斷出對應按鍵精靈的命令,例如UI_AddButton就是對應UI.AddButton()命令,SetAutoLockTime對應的是Device.SetAutoLockTime()等,絕大多數都是有跡可循的。至于參數跟返回值,大部分也都是與按鍵精靈命令保持一致,具體還請大家自行測試。這里提下比較特殊的幾個含返回參數的命令,一般返回參數是作為多個返回值實現的,例如找圖命令LuaAuxLib.FindPicture(),它有8個參數,與按鍵精靈前8個保持一致,但是返回坐標參數被放到了返回值中,它有3個返回值,分別是返回X坐標,返回Y坐標,返回圖片序號。由于篇幅有限,這里就不對每一個命令一一進行講解了,還請大家自行測試摸索。
附:完整代碼
local _zimao = {} --這是內部私有table函數
local zimao = {} --這是對外公開table函數
QMPlugin = zimao --通過這行代碼, 實現將zimao表中所有函數對外公開_zimao.version = "20220306" --插件版本號, 方便自己記憶local function try(block) -- 保護執行函數
local tablejoin = function (...)
local result = {}
for _, t in ipairs({...}) do
if type(t) == "table" then
for k, v in pairs(t) do
if type(k) == "number" then table.insert(result, v)
else result[k] = v end
end
else
table.insert(result, t)
end
end
return result
end-- get the try function
local try = block[1]
assert(try)-- get catch and finally functions
local funcs = tablejoin(block[2] or {}, block[3] or {})-- try to call it
local result_error = {}
local results = {pcall(try)}
if not results[1] then
-- run the catch function
if funcs and funcs.catch then
result_error = {funcs.catch(results[2])}
end
end-- run the finally function
if funcs and funcs.finally then
local result_fin = {funcs.finally(table.unpack(results))}
if #result_fin > 0 then
return table.unpack(result_fin)
end
end-- ok?
if results[1] and #results > 1 then
return table.unpack(results, 2, #results)
else
if #result_error > 0 then
return table.unpack(result_error)
else
return nil
end
end
end
local function catch(block) --異常捕獲函數
return {catch = block[1]}
end
local function finally(block) --最終必定執行函數
return {finally = block[1]}
end-- 常用的內部函數, 不加入私有table中, 直接定義使用
local function traceprint(...) --調用按鍵精靈的調試輸出命令
if QMPlugin then -- 在Lua調試環境下, QMPlugin變量的值是我們插件公開table函數值, 而在按鍵精靈調試運行環境下, 該變量值被替換為nil
print(...)
else
-- 獲取可變長參數的第一個參數值
local line = select(1, ...)
-- 如果第一個參數是字符串, 并且符合格式 _數字 , 則判定為行號意思
if type(line) == "string" and line:match("^%_%d+$") then
-- 第一個參數按照格式 _數字: 傳入TracePrint中可實現打印行號功能
LuaAuxLib.TracePrint(line .. ":", table.concat({...}, " ", 2, select("#", ...)))
elseif line == ":" and #{...} > 1 then
-- 第一個參數是冒號 : 時, 表示直接打印輸出數據
LuaAuxLib.TracePrint(":", table.concat({...}, " ", 2, select("#", ...)))
else
-- 其他的情況下則加上前綴后, 進行正常輸出
LuaAuxLib.TracePrint(":","紫貓學院測試插件:", ...)
end
end
end-- 實現獲取變量信息的插件函數, 需對外公開, 所以使用zimao前綴
function zimao.VarInfo(...)
-- 防止無法獲取nil參數
local paramCount = select("#", ...)
local varType, printStr, t = "", "", {}
for i = 1, paramCount do
local v = select(i, ...)
try {
function()
varType = type(v)
if varType == "table" then
printStr = "【" .. varType .." " .. tostring(#v) .. "】" .. LuaAuxLib.Encode_GetJsonLib():encode(v)
elseif varType == "number" or varType == "string" then
printStr = "【" .. varType .." " .. tostring(#tostring(v)) .. "】" .. tostring(v)
elseif varType == "boolean" or varType == "null" then
printStr = "【" .. varType .."】" .. tostring(v)
else
printStr = "【" .. varType .."】 未知數據,無法查看!"
end
table.insert( t, #t + 1, printStr )
end,
catch {
function (errors)
-- 下面這個traceprint是我們上面定義過的內部輸出命令,注意大小寫
traceprint("發生運行時錯誤!錯誤代碼:VarInfo(),錯誤信息:", errors)
end
}
}
end
printStr = table.concat( t, ", " )
return printStr
end-- 實現打印輸出變量詳細信息數據, 需對外公開
function zimao.TracePrint(...)
-- 通過VarInfo函數獲取參數的詳細數據信息
local info = zimao.VarInfo(...)
try {
function()
-- 在保護模式下打印輸出這個數據內容
traceprint(info)
end,
catch {
function (errors)
traceprint("發生運行時錯誤!錯誤代碼:TracePrint(),錯誤信息:", errors)
end
}
}
endfunction zimao.ScanLuaAuxLib()
-- 以保護模式運行, 避免插件錯誤造成腳本終止
try {
function()
-- 遍歷LuaAuxLib內的內容
for k, v in pairs(LuaAuxLib) do
-- 使用自定義函數traceprint在按鍵精靈中打印函數名
traceprint("名稱: " .. k, "類型: " .. type(v))
end
end,
catch {
function (errors)
traceprint("發生運行時錯誤!錯誤代碼:ScanLuaAuxLib(),錯誤信息:", errors)
end
}
}
end
?