From b661afed4c6649fa4a61439af1edfcdca1591245 Mon Sep 17 00:00:00 2001 From: nnwang <10577303+nnwang@user.noreply.gitee.com> Date: Fri, 12 Dec 2025 20:11:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D2=E8=BF=9B=E5=88=B6=E6=96=87?= =?UTF-8?q?=E4=BB=B6dfpwm=E8=A2=AB=E8=A7=86=E4=B8=BA=E6=96=87=E6=9C=AC?= =?UTF-8?q?=E4=BC=A0=E8=BE=93=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Client/main.lua | 105 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 87 insertions(+), 18 deletions(-) diff --git a/Client/main.lua b/Client/main.lua index 3812c0a..9ea2b31 100644 --- a/Client/main.lua +++ b/Client/main.lua @@ -21,6 +21,84 @@ local function log(msg) --basalt.debug("[FileClient] " .. tostring(msg)) end +-- 改进的二进制检测函数,增加对DFPWM等特定格式的支持 +local function isBinaryFile(path) + local extension = string.lower(string.match(path, "%.([^%.%s]+)$") or "") + local binaryExtensions = { + ["wav"] = true, + ["mp3"] = true, + ["ogg"] = true, + ["flac"] = true, + ["dfpwm"] = true, + ["png"] = true, + ["jpg"] = true, + ["jpeg"] = true, + ["gif"] = true, + ["bmp"] = true, + ["ico"] = true, + ["exe"] = true, + ["dll"] = true, + ["so"] = true, + ["bin"] = true, + ["dat"] = true, + ["zip"] = true, + ["rar"] = true, + ["tar"] = true, + ["gz"] = true, + ["pdf"] = true, + ["doc"] = true, + ["docx"] = true, + ["xls"] = true, + ["xlsx"] = true, + ["ppt"] = true, + ["pptx"] = true + } + + if binaryExtensions[extension] then + return true + end + + -- 对于没有扩展名的文件,检查内容 + local absPath = path + if not fs.exists(absPath) then + return false + end + + local ok, handle = pcall(fs.open, absPath, "rb") + if not ok or not handle then + return false + end + + local data = handle.read(math.min(1024, fs.getSize(absPath))) + handle.close() + + if not data then + return false + end + + -- 检查是否存在控制字符(除常见的空白字符外) + for i = 1, #data do + local b = data:byte(i) + -- 控制字符范围是 0-8, 11-12, 14-31, 127 + if (b >= 0 and b <= 8) or (b == 11) or (b == 12) or (b >= 14 and b <= 31) or (b == 127) then + -- 如果控制字符过多(超过5%),则认为是二进制文件 + local controlCount = 0 + for j = 1, #data do + local byte = data:byte(j) + if (byte >= 0 and byte <= 8) or (byte == 11) or (byte == 12) or (byte >= 14 and byte <= 31) or (byte == 127) then + controlCount = controlCount + 1 + end + end + + if controlCount / #data > 0.05 then + return true + end + end + end + + return false +end + function table_to_json(t, indent) indent = indent or 0 local spaces = string.rep(" ", indent) @@ -141,16 +219,6 @@ local function httpPost(path, data) end -- ========== 文件系统操作 ========== -local function isLikelyText(data) - for i = 1, math.min(#data, 1024) do - local b = data:byte(i) - if b < 32 and b ~= 9 and b ~= 10 and b ~= 13 then - return false - end - end - return true -end - local function getFiles(currentPath, result, prefix) local computerPrefix = "computer_" .. computerID local fullPrefix = currentPath == "" and prefix:sub(1, -2) or prefix .. currentPath @@ -165,15 +233,16 @@ local function getFiles(currentPath, result, prefix) end else local content = "[binary]" - local ok, handle = pcall(fs.open, absPath, "rb") - if ok and handle then - local data = handle.readAll() - handle.close() - if data and isLikelyText(data) then - content = data + if not isBinaryFile(absPath) then + local ok, handle = pcall(fs.open, absPath, "r") + if ok and handle then + local data = handle.readAll() + handle.close() + content = data or "" end end - result[fullPrefix] = { isFile = true, content = content } + + result[fullPrefix] = { isFile = true, content = content, isBinary = isBinaryFile(absPath) } end end @@ -399,4 +468,4 @@ local function main() end -- 启动主逻辑和Basalt事件循环 -parallel.waitForAll(basalt.autoUpdate, main) +parallel.waitForAll(basalt.autoUpdate, main) \ No newline at end of file