3 Commits
1.1.2 ... main

Author SHA1 Message Date
nnwang
cf0fdfa4d0 删除不必要的拓展名 2025-12-12 20:45:59 +08:00
nnwang
9a68952fac 将json解析改为外部库 2025-12-12 20:42:40 +08:00
nnwang
b661afed4c 修复2进制文件dfpwm被视为文本传输的问题 2025-12-12 20:11:48 +08:00

View File

@@ -4,6 +4,15 @@ local httpServer = args[1] or "http://192.168.2.200:8080"
local roomId = args[2] local roomId = args[2]
local pollInterval = 1 local pollInterval = 1
local computerID = tostring(os.computerID() or "unknown") local computerID = tostring(os.computerID() or "unknown")
-- ========== 加载 JSON ==========
local JsonUrl = "https://git.liulikeji.cn/GitHub/json.lua/raw/branch/master/json.lua"
local JsonResp = http.get(JsonUrl)
if not JsonResp then
error("无法下载 Json 框架")
end
local json = load(JsonResp.readAll())()
JsonResp.close()
-- ========== 加载 Basalt ========== -- ========== 加载 Basalt ==========
local basaltUrl = "https://git.liulikeji.cn/GitHub/Basalt/releases/download/v1.7/basalt.lua" local basaltUrl = "https://git.liulikeji.cn/GitHub/Basalt/releases/download/v1.7/basalt.lua"
@@ -21,97 +30,65 @@ local function log(msg)
--basalt.debug("[FileClient] " .. tostring(msg)) --basalt.debug("[FileClient] " .. tostring(msg))
end end
function table_to_json(t, indent)
indent = indent or 0 local function isBinaryFile(path)
local spaces = string.rep(" ", indent) local extension = string.lower(string.match(path, "%.([^%.%s]+)$") or "")
local result = {} local binaryExtensions = {
["dfpwm"] = true,
if type(t) ~= "table" then }
if type(t) == "string" then
-- 正确转义所有特殊字符 if binaryExtensions[extension] then
local escaped = t:gsub("[\\\"\b\f\n\r\t]", function(c) return true
local replacements = {
['\\'] = '\\\\',
['"'] = '\\"',
['\b'] = '\\b',
['\f'] = '\\f',
['\n'] = '\\n',
['\r'] = '\\r',
['\t'] = '\\t'
}
return replacements[c]
end)
return '"' .. escaped .. '"'
elseif type(t) == "number" or type(t) == "boolean" then
return tostring(t)
else
return '"' .. tostring(t) .. '"'
end
end end
-- 检查是否是数组 -- 对于没有扩展名的文件,检查内容
local is_array = true local absPath = path
local max_index = 0 if not fs.exists(absPath) then
local count = 0 return false
for k, v in pairs(t) do
count = count + 1
if type(k) ~= "number" or k <= 0 or math.floor(k) ~= k then
is_array = false
end
if type(k) == "number" and k > max_index then
max_index = k
end
end end
-- 空表当作对象处理 local ok, handle = pcall(fs.open, absPath, "rb")
if count == 0 then if not ok or not handle then
is_array = false return false
end end
if is_array then local data = handle.read(math.min(1024, fs.getSize(absPath)))
-- 处理数组 handle.close()
table.insert(result, "[")
local items = {} if not data then
for i = 1, max_index do return false
if t[i] ~= nil then end
table.insert(items, table_to_json(t[i], indent + 2))
else -- 检查是否存在控制字符(除常见的空白字符外)
table.insert(items, "null") 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 end
table.insert(result, table.concat(items, ", "))
table.insert(result, "]")
else
-- 处理对象
table.insert(result, "{")
local items = {}
for k, v in pairs(t) do
local key = '"' .. tostring(k) .. '"'
local value = table_to_json(v, indent + 2)
if indent > 0 then
table.insert(items, spaces .. " " .. key .. ": " .. value)
else
table.insert(items, key .. ":" .. value)
end
end
if indent > 0 then
table.insert(result, table.concat(items, ",\n"))
table.insert(result, "\n" .. spaces .. "}")
else
table.insert(result, table.concat(items, ","))
table.insert(result, "}")
end
end end
return table.concat(result, indent > 0 and "\n" .. spaces or "") return false
end end
local function cleanPath(path) local function cleanPath(path)
return (path:gsub("^computer/", ""):gsub("^computer\\", "")) return (path:gsub("^computer/", ""):gsub("^computer\\", ""))
end end
local function httpPost(path, data) local function httpPost(path, data)
local jsonData = table_to_json(data) local jsonData = json.encode(data)
local url = httpServer .. path local url = httpServer .. path
-- 使用长轮询 -- 使用长轮询
@@ -132,7 +109,7 @@ local function httpPost(path, data)
local responseBody = response.readAll() local responseBody = response.readAll()
response.close() response.close()
local ok, result = pcall(textutils.unserialiseJSON, responseBody) local ok, result = pcall(json.decode, responseBody)
if ok then if ok then
return result return result
else else
@@ -141,16 +118,6 @@ local function httpPost(path, data)
end 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 function getFiles(currentPath, result, prefix)
local computerPrefix = "computer_" .. computerID local computerPrefix = "computer_" .. computerID
local fullPrefix = currentPath == "" and prefix:sub(1, -2) or prefix .. currentPath local fullPrefix = currentPath == "" and prefix:sub(1, -2) or prefix .. currentPath
@@ -165,15 +132,16 @@ local function getFiles(currentPath, result, prefix)
end end
else else
local content = "[binary]" local content = "[binary]"
local ok, handle = pcall(fs.open, absPath, "rb") if not isBinaryFile(absPath) then
if ok and handle then local ok, handle = pcall(fs.open, absPath, "r")
local data = handle.readAll() if ok and handle then
handle.close() local data = handle.readAll()
if data and isLikelyText(data) then handle.close()
content = data content = data or ""
end end
end end
result[fullPrefix] = { isFile = true, content = content }
result[fullPrefix] = { isFile = true, content = content, isBinary = isBinaryFile(absPath) }
end end
end end
@@ -399,4 +367,4 @@ local function main()
end end
-- 启动主逻辑和Basalt事件循环 -- 启动主逻辑和Basalt事件循环
parallel.waitForAll(basalt.autoUpdate, main) parallel.waitForAll(basalt.autoUpdate, main)