Compare commits
2 Commits
b661afed4c
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cf0fdfa4d0 | ||
|
|
9a68952fac |
125
Client/main.lua
125
Client/main.lua
@@ -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,37 +30,11 @@ local function log(msg)
|
|||||||
--basalt.debug("[FileClient] " .. tostring(msg))
|
--basalt.debug("[FileClient] " .. tostring(msg))
|
||||||
end
|
end
|
||||||
|
|
||||||
-- 改进的二进制检测函数,增加对DFPWM等特定格式的支持
|
|
||||||
local function isBinaryFile(path)
|
local function isBinaryFile(path)
|
||||||
local extension = string.lower(string.match(path, "%.([^%.%s]+)$") or "")
|
local extension = string.lower(string.match(path, "%.([^%.%s]+)$") or "")
|
||||||
local binaryExtensions = {
|
local binaryExtensions = {
|
||||||
["wav"] = true,
|
|
||||||
["mp3"] = true,
|
|
||||||
["ogg"] = true,
|
|
||||||
["flac"] = true,
|
|
||||||
["dfpwm"] = 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
|
if binaryExtensions[extension] then
|
||||||
@@ -99,97 +82,13 @@ local function isBinaryFile(path)
|
|||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
function table_to_json(t, indent)
|
|
||||||
indent = indent or 0
|
|
||||||
local spaces = string.rep(" ", indent)
|
|
||||||
local result = {}
|
|
||||||
|
|
||||||
if type(t) ~= "table" then
|
|
||||||
if type(t) == "string" then
|
|
||||||
-- 正确转义所有特殊字符
|
|
||||||
local escaped = t:gsub("[\\\"\b\f\n\r\t]", function(c)
|
|
||||||
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
|
|
||||||
|
|
||||||
-- 检查是否是数组
|
|
||||||
local is_array = true
|
|
||||||
local max_index = 0
|
|
||||||
local count = 0
|
|
||||||
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
|
|
||||||
|
|
||||||
-- 空表当作对象处理
|
|
||||||
if count == 0 then
|
|
||||||
is_array = false
|
|
||||||
end
|
|
||||||
|
|
||||||
if is_array then
|
|
||||||
-- 处理数组
|
|
||||||
table.insert(result, "[")
|
|
||||||
local items = {}
|
|
||||||
for i = 1, max_index do
|
|
||||||
if t[i] ~= nil then
|
|
||||||
table.insert(items, table_to_json(t[i], indent + 2))
|
|
||||||
else
|
|
||||||
table.insert(items, "null")
|
|
||||||
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
|
|
||||||
|
|
||||||
return table.concat(result, indent > 0 and "\n" .. spaces or "")
|
|
||||||
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
|
||||||
|
|
||||||
-- 使用长轮询
|
-- 使用长轮询
|
||||||
@@ -210,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
|
||||||
|
|||||||
Reference in New Issue
Block a user