gg.multiChoice多选菜单记忆配置与表缓存
title: gg.multiChoice多选菜单记忆配置与表缓存
date: 2024-10-19 21:29:58
id: 2
tags: lua
笔者没有缩进的习惯,所以看上去很乱,虽然最近玩Python之后不得不注意了……
multiChoice多选菜单记忆配置
—— 配置函数 ——
function SavePreferences() SaveConfig = {} local file=io.open(SaveConfigPath,'r') if not file then return end local SaveConfig = load('return '..file:read('*a'))() if not SaveConfig or SaveConfig['switch'] == nil then return end _G['SaveConfig'] = SaveConfig file:close() end
function UpdateSaveConfig(tag, value) SaveConfig[tag] = value io.open(SaveConfigPath, 'w'):write(tostring(SaveConfig)):close() end
function multiChoice(items, sign, message) local boolean = nil if SaveConfig and SaveConfig['switch'] then boolean = SaveConfig[sign] end local a = gg.multiChoice(items,boolean,message) if a and SaveConfig and SaveConfig['switch'] and sign then local NewSaveConfig = {} for i, v in pairs(a) do if string.find(items[i], "返回") or string.find(items[i], "退出") then return {[i] = v} end if v then NewSaveConfig[i] = v end end UpdateSaveConfig(sign, NewSaveConfig) end return a end
—— 使用方法 ——
– SaveConfigPath = Path
– 设置记忆配置文件路径
– 要包含文件名
– SavePreferences()
– 引入记忆配置文件表
– UpdateSaveConfig(tag, value)
– 更新记忆配置表与文件
– tag 标签名,value 内容
– 一般除记忆配置开关外不会主动用到
– multiChoice(items, sign, message)
– 多选菜单
– items 可选项目表,sign 多选菜单标识,message 标题
– items 中包含 返回 或 退出 的选项被勾选时,仅返回该项,避免将该项选择状态保存到配置,仅想执行该项时也较为方便
—— 使用示例 ——
SaveConfigPath = '/sdcard/Android/SaveConfi'
SavePreferences()
if SaveConfig['switch'] then SavePreferencesGUI = '⭕' else SavePreferencesGUI = '❌' end
-- SavePreferencesGUI 记忆配置开关标识
local menu = multiChoice({'功能一','功能二','记忆配置开关'..SavePreferencesGUI,'假设返回','退出脚本'}, 'menu', '这是一个标题')
-- 此处 sign 必须为字符串,且不可与其他菜单相同
if not menu then return end
function switch() -- 记忆配置开关
if SaveConfig['switch'] then
UpdateSaveConfig('switch', false) -- 现状态为开写入关
print('记忆配置:关') else
UpdateSaveConfig('switch', true) -- 现状态为关写入开
print('记忆配置:开') end
end
-- 也可以不用选项开关,在 SavePreferences() 后 UpdateSaveConfig('switch', true) 直接开启
local functions = {function() print('功能一') end,function() print('功能二') end,switch,function() return end,os.exit} -- 函数表
for i, v in pairs(menu) do
if v then functions[i]() end
end
表缓存
function list(sign, url, match)
local path = '/sdcard/Android/.Cache'
local file = io.open(path, 'r')
if file then _G['Cache'] = load('return '..file:read("*a"))() else _G['Cache'] = {} end
if not Cache or not Cache[sign] then
response = gg.makeRequest(url).content
if response == nil then return end
Cache[sign] = {}
for id, name in response:gmatch(match) do
Cache[sign][tonumber(id)] = name
end
io.open(path, "w"):write(tostring(Cache)):close()
end
end
list('itemlist','https://pvp.qq.com/web201605/js/item.json','"item_id"%s*:%s*(%d+)%s*,%s*"[^"]*"%s*:%s*"([^"]+)"')
评论