From 1927784aa82939a15656ecb4e310cb9cb1a81d60 Mon Sep 17 00:00:00 2001 From: igor Date: Fri, 24 Nov 2017 12:24:25 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9C=D0=B4=D0=B0-=D0=BC=D0=B4=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Applications.cfg | 8 +- Applications/AppMarket/Main.lua | 6 +- Installer/Installer.lua | 2 +- lib/MineOSCore.lua | 28 ++++++ lib/MineOSInterface.lua | 2 +- lib/web.lua | 164 +++++++++++++++----------------- 6 files changed, 114 insertions(+), 96 deletions(-) diff --git a/Applications.cfg b/Applications.cfg index 16a09042..1fc30166 100644 --- a/Applications.cfg +++ b/Applications.cfg @@ -227,7 +227,7 @@ url="https://raw.githubusercontent.com/IgorTimofeev/OpenComputers/master/lib/MineOSCore.lua", type="Library", preloadFile=true, - version=1.99, + version=2.00, }, { path="/lib/MineOSNetwork.lua", @@ -239,7 +239,7 @@ path="/lib/MineOSInterface.lua", url="https://raw.githubusercontent.com/IgorTimofeev/OpenComputers/master/lib/MineOSInterface.lua", type="Library", - version=1.25, + version=1.26, }, { path="/lib/MineOSPaths.lua", @@ -260,7 +260,7 @@ url="https://raw.githubusercontent.com/IgorTimofeev/OpenComputers/master/lib/web.lua", type="Library", preloadFile=true, - version=1.07, + version=1.08, }, { path="/lib/event.lua", @@ -1001,7 +1001,7 @@ type="Application", icon="https://raw.githubusercontent.com/IgorTimofeev/OpenComputers/master/Applications/AppMarket/Icon.pic", forceDownload=true, - version=1.75, + version=1.76, resources={ { path="/Update.pic", diff --git a/Applications/AppMarket/Main.lua b/Applications/AppMarket/Main.lua index 157474fb..0c461e04 100755 --- a/Applications/AppMarket/Main.lua +++ b/Applications/AppMarket/Main.lua @@ -31,7 +31,7 @@ local function newApp(x, y, width, applicationListElement, hideDownloadButton) app.icon = app:addChild(GUI.image(1, 1, MineOSInterface.iconsCache.script)) if applicationListElement.icon then - web.downloadFile(applicationListElement.icon, temproraryIconPath) + web.download(applicationListElement.icon, temproraryIconPath) app.icon.image = image.load(temproraryIconPath) end @@ -44,7 +44,7 @@ local function newApp(x, y, width, applicationListElement, hideDownloadButton) mainContainer:draw() buffer.draw() - web.downloadMineOSApplication(applicationListElement, MineOSCore.properties.language) + MineOSCore.downloadApplication(applicationListElement, MineOSCore.properties.language) app.downloadButton.text = localization.downloaded computer.pushSignal("MineOSCore", "updateFileList") @@ -129,7 +129,7 @@ local function displayApps(fromPage, typeFilter, nameFilter, updateCheck) mainContainer:draw() buffer.draw() - web.downloadMineOSApplication(finalApplicationList[i], MineOSCore.properties.language) + MineOSCore.downloadApplication(finalApplicationList[i], MineOSCore.properties.language) end mainContainer:draw() diff --git a/Installer/Installer.lua b/Installer/Installer.lua index 3dca8f3a..60748a03 100644 --- a/Installer/Installer.lua +++ b/Installer/Installer.lua @@ -270,7 +270,7 @@ stages[4] = function() mainContainer:draw() buffer.draw() - web.downloadMineOSApplication(thingsToDownload[i], MineOSCore.properties.language) + MineOSCore.downloadApplication(thingsToDownload[i], MineOSCore.properties.language) end stageContainer:deleteChildren(2) diff --git a/lib/MineOSCore.lua b/lib/MineOSCore.lua index 9f5cc277..2ff3b1d2 100755 --- a/lib/MineOSCore.lua +++ b/lib/MineOSCore.lua @@ -1,5 +1,6 @@ require("advancedLua") +local web = require("web") local component = require("component") local buffer = require("doubleBuffering") local filesystem = require("filesystem") @@ -256,6 +257,33 @@ end ----------------------------------------------------------------------------------------------------------------------------------- +function MineOSCore.downloadApplication(application, language, createShortcut) + if application.type == "Application" then + fs.remove(application.path .. ".app") + + web.download(application.url, application.path .. ".app/Main.lua") + web.download(application.icon, application.path .. ".app/Resources/Icon.pic") + + if application.resources then + for i = 1, #application.resources do + web.download(application.resources[i].url, application.path .. ".app/Resources/" .. application.resources[i].path) + end + end + + if application.about then + web.download(application.about .. language .. ".txt", application.path .. ".app/Resources/About/" .. language .. ".txt") + end + + if application.createShortcut or createShortcut then + MineOSCore.createShortcut(MineOSPaths.desktop .. fs.name(application.path) .. ".lnk", application.path .. ".app/") + end + else + web.download(application.url, application.path) + end +end + +----------------------------------------------------------------------------------------------------------------------------------- + MineOSCore.loadPropeties() ----------------------------------------------------------------------------------------------------------------------------------- diff --git a/lib/MineOSInterface.lua b/lib/MineOSInterface.lua index 6bee8ea0..dbcce663 100755 --- a/lib/MineOSInterface.lua +++ b/lib/MineOSInterface.lua @@ -1031,7 +1031,7 @@ function MineOSInterface.newFileFromURL(parentWindow, iconField, x, y, path) buffer.draw() else if container.inputFieldURL.text then - local success, reason = require("web").downloadFile(container.inputFieldURL.text, path .. container.inputField.text) + local success, reason = require("web").download(container.inputFieldURL.text, path .. container.inputField.text) if not success then GUI.error(reason) end diff --git a/lib/web.lua b/lib/web.lua index 62ff26e0..87da77d2 100755 --- a/lib/web.lua +++ b/lib/web.lua @@ -1,11 +1,11 @@ -local fs = require("filesystem") local component = require("component") +local fs = require("filesystem") local web = {} ---------------------------------------------------------------------------------------------------- -local function serializeTableToURL(existentData, table) +local function serializeTable(table, currentData) local result = "" for key, value in pairs(table) do @@ -13,43 +13,59 @@ local function serializeTableToURL(existentData, table) if keyType == "number" then key = key - 1 - -- elseif keyType == "string" then - -- key = "\"" .. key .. "\"" end if valueType == "table" then - result = result .. serializeTableToURL(existentData .. "[" .. key .. "]", value) + result = result .. serializeTable(value, currentData .. "[" .. key .. "]") else - result = result .. existentData .. "[" .. key .. "]=" .. value .. "&" + result = result .. currentData .. "[" .. key .. "]=" .. value .. "&" end end return result end -local function rawRequest(url, postData, headers, chunkHandler) - local stringPostData - if postData then - if type(postData) == "table" then - stringPostData = "" - - for key, value in pairs(postData) do - if type(value) == "table" then - stringPostData = stringPostData .. serializeTableToURL(key, value) - else - stringPostData = stringPostData .. key .. "=" .. value .. "&" - end +function web.serialize(data) + if type(data) == "table" then + local serializedData = "" + + for key, value in pairs(data) do + if type(value) == "table" then + serializedData = serializedData .. serializeTable(value, key) + else + serializedData = serializedData .. key .. "=" .. value .. "&" end - elseif type(postData) == "string" then - stringPostData = postData end + + return serializedData + else + return tostring(data) + end +end + +function web.encode(data) + if data then + data = string.gsub(data, "([^%w ])", function(char) + return string.format("%%%02X", string.byte(char)) + end) + data = string.gsub(data, " ", "+") end - local pcallSuccess, requestHandle, requestReason = pcall(component.internet.request, url, stringPostData, headers) + return data +end + +---------------------------------------------------------------------------------------------------- + +function web.rawRequest(url, postData, headers, chunkHandler, chunkSize) + if postData then + postData = web.serialize(postData) + end + + local pcallSuccess, requestHandle, requestReason = pcall(component.internet.request, url, postData, headers) if pcallSuccess then if requestHandle then while true do - local chunk, reason = requestHandle.read(math.huge) + local chunk, reason = requestHandle.read(chunkSize or math.huge) if chunk then chunkHandler(chunk) else @@ -62,18 +78,16 @@ local function rawRequest(url, postData, headers, chunkHandler) end end else - return false, "Invalid URL-addess" + return false, "Invalid URL-address" end else - return false, "Usage: web.request(string url)" + return false, "Invalid arguments to component.internet.request" end end ----------------------------------------------------------------------------------------------------- - function web.request(url, postData, headers) local data = "" - local success, reason = rawRequest(url, postData, headers, function(chunk) + local success, reason = web.rawRequest(url, postData, headers, function(chunk) data = data .. chunk end) @@ -84,91 +98,67 @@ function web.request(url, postData, headers) end end -function web.downloadFile(url, path) +function web.download(url, path) fs.makeDirectory(fs.path(path) or "") - local file, reason = io.open(path, "w") - if file then - local success, reason = rawRequest(url, nil, nil, function(chunk) - file:write(chunk) + + local handle, reason = io.open(path, "w") + if handle then + local success, reason = web.rawRequest(url, nil, nil, function(chunk) + handle:write(chunk) end) - file:close() + handle:close() if success then return true else - return false, "Could not connect to to URL-address \"" .. tostring(url) .. "\", the reason is \"" .. tostring(reason) .. "\"" + return false, reason end else return false, "Failed to open file for writing: " .. tostring(reason) end end -function web.runScript(url) +function web.run(url, ...) local result, reason = web.request(url) - if success then - local loadSucces, loadReason = load(result) - if loadSucces then - local xpcallSuccess, xpcallSuccessReason = xpcall(loadSucces, debug.traceback) - if xpcallSuccess then - return true + if result then + result, reason = load(result) + if result then + result = { pcall(result, ...) } + if result[1] then + return table.unpack(result, 2) else - return false, "Failed to run script: " .. tostring(xpcallSuccessReason) + return false, "Failed to run script: " .. tostring(result[2]) end else return false, "Failed to run script: " .. tostring(loadReason) end else - return false, "Could not connect to to URL-address \"" .. tostring(url) .. "\", the reason is \"" .. tostring(reason) .. "\"" - end -end - -function web.downloadMineOSApplication(application, language) - if application.type == "Application" then - fs.remove(application.path .. ".app") - - web.downloadFile(application.url, application.path .. ".app/Main.lua") - web.downloadFile(application.icon, application.path .. ".app/Resources/Icon.pic") - - if application.resources then - for i = 1, #application.resources do - web.downloadFile(application.resources[i].url, application.path .. ".app/Resources/" .. application.resources[i].path) - end - end - - if application.about then - web.downloadFile(application.about .. language .. ".txt", application.path .. ".app/Resources/About/" .. language .. ".txt") - end - - if application.createShortcut then - local path = "/MineOS/Desktop/" .. fs.name(application.path) .. ".lnk" - fs.makeDirectory(fs.path(path)) - - local file, reason = io.open(path, "w") - if file then - file:write(application.path .. ".app/") - file:close() - else - print(reason) - end - end - else - web.downloadFile(application.url, application.path) + return false, reason end end ---------------------------------------------------------------------------------------------------- --- print(web.request("http://94.242.34.251:8888/MineOS/AppMarket/test.php", { --- abc = "siski", --- pizda = "test", --- def = { --- {name = "Test1.png", data = "F0"}, --- {name = "Test2.png", data = "FF"}, --- {hello = "world", meow = "meow-meow"} --- }, --- })) +-- print( +-- web.serialize({ +-- string = "Hello world", +-- number = 123, +-- array = { +-- arrayString = "Meow", +-- arrayInArray = { +-- arrayInArrayNumber = 456 +-- } +-- } +-- }) +-- ) --- web.downloadFile("https://github.com/IgorTimofeev/OpenComputers/raw/master/Wallpapers/CloudyEvening.pic", "Clouds.pic") +-- print( +-- web.run( +-- "https://raw.githubusercontent.com/IgorTimofeev/OpenComputers/master/Screensavers/Matrix.lua" +-- ) +-- ) + +-- print(result) ----------------------------------------------------------------------------------------------------