From 9b94ae16e2df6acd5a365a69d3d001f3cb87e33b Mon Sep 17 00:00:00 2001 From: Leonmmcoset Date: Wed, 3 Sep 2025 15:56:53 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E6=8B=89=E5=8F=96=E5=87=BD=E6=95=B0=E7=9A=84=E5=AE=89=E5=85=A8?= =?UTF-8?q?=E6=80=A7=E9=97=AE=E9=A2=98=E5=B9=B6=E6=94=B9=E8=BF=9B=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复多个文件中事件拉取函数的安全性问题,添加更健壮的fallback机制 在imageview.lua中改进错误信息显示,确保image变量转换为字符串 在pkg.lua中调整JSON序列化参数以生成更紧凑的输出 --- data/computercraft/lua/rom/apis/lgui.lua | 15 ++++++++++----- data/computercraft/lua/rom/programs/imageview.lua | 14 ++++++++++---- data/computercraft/lua/rom/programs/pkg.lua | 2 +- installer.lua | 2 +- 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/data/computercraft/lua/rom/apis/lgui.lua b/data/computercraft/lua/rom/apis/lgui.lua index c547805..75e3197 100644 --- a/data/computercraft/lua/rom/apis/lgui.lua +++ b/data/computercraft/lua/rom/apis/lgui.lua @@ -592,13 +592,18 @@ end function GUIManager:handleEvents() 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 if type(pullEventFunc) ~= "function" then - -- Try using rc.pullEvent as fallback - pullEventFunc = rc and rc.pullEvent - if type(pullEventFunc) ~= "function" then - error("Neither os.pullEvent nor rc.pullEvent is available") + -- Try using rc.pullEvent as fallback if rc is available + if rc and type(rc.pullEvent) == "function" then + pullEventFunc = rc.pullEvent + 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 diff --git a/data/computercraft/lua/rom/programs/imageview.lua b/data/computercraft/lua/rom/programs/imageview.lua index 2cd2d0f..933aea1 100644 --- a/data/computercraft/lua/rom/programs/imageview.lua +++ b/data/computercraft/lua/rom/programs/imageview.lua @@ -60,8 +60,8 @@ local function main(args) end) if not success then - print("Error loading image: " .. image) - print("Make sure the URL points to a valid image file compatible with CC Tweaked.") + print("Error loading image: " .. tostring(image)) + print("Make sure the URL points to a valid image file compatible with CC Tweaked.") return end @@ -79,9 +79,15 @@ local function main(args) print("\nImage displayed. Press ESC to exit.") - -- 监听ESC键 + -- 安全地监听ESC键 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键 term.clear() term.setCursorPos(1, 1) diff --git a/data/computercraft/lua/rom/programs/pkg.lua b/data/computercraft/lua/rom/programs/pkg.lua index af3e011..3b75cab 100644 --- a/data/computercraft/lua/rom/programs/pkg.lua +++ b/data/computercraft/lua/rom/programs/pkg.lua @@ -60,7 +60,7 @@ local function create_package(pkg_name) } local json_file = io.open(fs.combine(version_dir, "package.json"), "w") if json_file then - json_file:write(textutils.serializeJSON(package_json, true)) + json_file:write(textutils.serializeJSON(package_json, false)) json_file:close() print("Created package.json") else diff --git a/installer.lua b/installer.lua index af3fd29..31abbfc 100644 --- a/installer.lua +++ b/installer.lua @@ -1,5 +1,5 @@ -- 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" print("Start loading LeonOS installer ("..INSTALLER_VERSION..")...")