fix: 修复事件拉取函数的安全性问题并改进错误处理

修复多个文件中事件拉取函数的安全性问题,添加更健壮的fallback机制
在imageview.lua中改进错误信息显示,确保image变量转换为字符串
在pkg.lua中调整JSON序列化参数以生成更紧凑的输出
This commit is contained in:
2025-09-03 15:56:53 +08:00
parent 0ffb590516
commit 9b94ae16e2
4 changed files with 22 additions and 11 deletions

View File

@@ -592,13 +592,18 @@ end
function GUIManager:handleEvents() function GUIManager:handleEvents()
while self.running do while self.running do
-- Safely get the pullEvent function with fallback to rc.pullEvent -- Safely get the pullEvent function with multiple fallbacks
local pullEventFunc = os.pullEvent local pullEventFunc = os.pullEvent
if type(pullEventFunc) ~= "function" then if type(pullEventFunc) ~= "function" then
-- Try using rc.pullEvent as fallback -- Try using rc.pullEvent as fallback if rc is available
pullEventFunc = rc and rc.pullEvent if rc and type(rc.pullEvent) == "function" then
if type(pullEventFunc) ~= "function" then pullEventFunc = rc.pullEvent
error("Neither os.pullEvent nor rc.pullEvent is available") else
-- In CC Tweaked, os.pullEventRaw is always available
pullEventFunc = os.pullEventRaw
if type(pullEventFunc) ~= "function" then
error("No valid event pulling function found. Ensure you're running in a CC Tweaked environment.")
end
end end
end end

View File

@@ -60,8 +60,8 @@ local function main(args)
end) end)
if not success then if not success then
print("Error loading image: " .. image) print("Error loading image: " .. tostring(image))
print("Make sure the URL points to a valid image file compatible with CC Tweaked.") print("Make sure the URL points to a valid image file compatible with CC Tweaked.")
return return
end end
@@ -79,9 +79,15 @@ local function main(args)
print("\nImage displayed. Press ESC to exit.") print("\nImage displayed. Press ESC to exit.")
-- 监听ESC键 -- 安全地监听ESC键
while true do while true do
local _, key = os.pullEvent("key") -- 使用安全的事件拉取函数
local pullEventFunc = os.pullEvent or os.pullEventRaw
if not pullEventFunc then
error("No valid event pulling function found")
end
local event, key = table.unpack({pullEventFunc("key")})
if key == 27 then -- ESC键 if key == 27 then -- ESC键
term.clear() term.clear()
term.setCursorPos(1, 1) term.setCursorPos(1, 1)

View File

@@ -60,7 +60,7 @@ local function create_package(pkg_name)
} }
local json_file = io.open(fs.combine(version_dir, "package.json"), "w") local json_file = io.open(fs.combine(version_dir, "package.json"), "w")
if json_file then if json_file then
json_file:write(textutils.serializeJSON(package_json, true)) json_file:write(textutils.serializeJSON(package_json, false))
json_file:close() json_file:close()
print("Created package.json") print("Created package.json")
else else

View File

@@ -1,5 +1,5 @@
-- LeonOS installer -- LeonOS installer
local INSTALLER_VERSION = "0.3.8 Beta 4 Alpha 1" local INSTALLER_VERSION = "0.3.8 Beta 4 Alpha 2"
local DEFAULT_ROM_DIR = "/leonos" local DEFAULT_ROM_DIR = "/leonos"
print("Start loading LeonOS installer ("..INSTALLER_VERSION..")...") print("Start loading LeonOS installer ("..INSTALLER_VERSION..")...")