From ca66a9e20c3cc772ef5f3d1d68e342bb72a20a69 Mon Sep 17 00:00:00 2001 From: gamax92 Date: Fri, 19 Jun 2015 12:17:55 -0600 Subject: [PATCH] Various things Add persistence code to the eeprom, so it will actually work when booting OCEmu convert a print -> cprint Implement the HTTP side of the internet card Add clipboard support to the keyboard Edit the readme to mention luasocket and luasec --- README.md | 8 ++++-- src/component/eeprom.lua | 39 +++++++++++++++++++++++++++- src/component/internet.lua | 46 +++++++++++++++++++++++++++++---- src/component/keyboard_sdl2.lua | 6 +++++ 4 files changed, 91 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index f35de4c..f3ea317 100644 --- a/README.md +++ b/README.md @@ -4,12 +4,16 @@ OCEmu - OpenComputers Emulator Installation ------------ -Needs luafilesystem, utf8, and luaffi +Needs luafilesystem, utf8, and luaffi. + +luasocket is required for the Internet Component. + +luasec is optional but is required for HTTPS. ``` luarocks-5.2 install luafilesystem luarocks-5.2 install utf8 -git clone https://github.com/colesbury/luaffi.git +git clone https://github.com/gamax92/luaffi.git cd luaffi make sudo cp ffi.so /appropriate/path/for/lua/libraries/ diff --git a/src/component/eeprom.lua b/src/component/eeprom.lua index c9e00a2..80cc4ab 100644 --- a/src/component/eeprom.lua +++ b/src/component/eeprom.lua @@ -2,10 +2,43 @@ local address, slot, filename = ... local crc32 = require("support.crc32") +local directory = elsa.filesystem.getSaveDirectory() .. "/" .. address +if not elsa.filesystem.exists(directory) then + elsa.filesystem.createDirectory(directory) +end + local code = elsa.filesystem.read(filename) local data = "" local label = "EEPROM" local readonly = false +if elsa.filesystem.exists(directory .. "/data.lua") then + local fn, err = elsa.filesystem.load(directory .. "/data.lua","t",{}) + if not fn then + cprint("Failed to unpersist eeprom @" .. address .. ": " .. err) + else + local ncode,ndata,nlabel,nread = fn() + if type(ncode) ~= "string" or type(ndata) ~= "string" or type(nlabel) ~= "string" or type(nread) ~= "boolean" then + cprint("Failed to unpersist eeprom @" .. address .. ": Invalid persist data") + cprint("code) " .. type(ncode)) + cprint("data) " .. type(ndata)) + cprint("labl) " .. type(nlabel)) + cprint("read) " .. type(nread)) + else + code,data,label,readonly = ncode,ndata,nlabel,nread + end + end +end + +local function persist() + local file, err = io.open(directory .. "/data.lua", "wb") + if not file then + cprint("Failed to persist eeprom @" .. address .. ": " .. err) + return false + end + file:write(string.format("return %q,%q,%q,%s",code,data,label,tostring(readonly)):gsub("\\\n","\\n") .. "") + file:close() + return true +end -- eeprom component local obj = {} @@ -22,6 +55,7 @@ function obj.setData(newdata) -- Overwrite the currently stored byte array. error("not enough space",3) end data = newdata + persist() end function obj.getDataSize() -- Get the storage capacity of this EEPROM. cprint("eeprom.getDataSize") @@ -43,6 +77,7 @@ function obj.setLabel(newlabel) -- Set the label of the EEPROM. compCheckArg(1,newlabel,"string","nil") if newlabel == nil then newlabel = "EEPROM" end label = newlabel:sub(1,16) + persist() return label end function obj.getChecksum() -- Get the checksum of the data on this EEPROM. @@ -64,14 +99,16 @@ function obj.set(newcode) -- Overwrite the currently stored byte array. error("not enough space",3) end code = newcode + persist() end function obj.makeReadonly(checksum) -- Make this EEPROM readonly if it isn't already. This process cannot be reversed! - print("eeprom.makeReadonly", checksum) + cprint("eeprom.makeReadonly", checksum) compCheckArg(1,checksum,"string") if checksum ~= obj.getChecksum() then return nil, "incorrect checksum" end readonly = true + persist() return true end diff --git a/src/component/internet.lua b/src/component/internet.lua index e999fac..e4dd317 100644 --- a/src/component/internet.lua +++ b/src/component/internet.lua @@ -6,6 +6,11 @@ if not okay then return end local url = require("socket.url") +local okay, http = pcall(require, "ssl.https") +if not okay then + cprint("Cannot use HTTPS: " .. http) + http = require("socket.http") +end component.connect("filesystem",gen_uuid(),nil,"lua/component/internet",true) @@ -94,17 +99,48 @@ function obj.connect(address, port) -- Opens a new TCP connection. Returns the h return fakesocket end function obj.request(url, postData) -- Starts an HTTP request. If this returns true, further results will be pushed using `http_response` signals. - --STUB cprint("internet.request",url, postData) compCheckArg(1,url,"string") if not config.get("internet.enableHttp",true) then return nil, "http requests are unavailable" end - local post - if type(postData) == "string" then - post = postData + -- TODO: Check for too many connections + if type(postData) ~= "string" then + postData = nil end - return nil + -- TODO: This works ... but is slow. + local page, _, headers, status = http.request(url, postData) + local protocol, code, message = status:match("(.-) (.-) (.*)") + code = tonumber(code) + local fakesocket = { + read = function(n) + cprint("(socket) read",n) + -- OC doesn't actually return n bytes when requested. + if page == nil then + return nil, "connection lost" + elseif page == "" then + return nil + else + -- Return up to 8192 bytes + local data = page:sub(1,8192) + page = page:sub(8193) + return data + end + end, + response = function() + cprint("(socket) response") + return code, message, headers + end, + close = function() + cprint("(request) close") + page = nil + end, + finishConnect = function() + cprint("(socket) finishConnect") + return true + end + } + return fakesocket end local cec = {} diff --git a/src/component/keyboard_sdl2.lua b/src/component/keyboard_sdl2.lua index 27d240e..8068f51 100644 --- a/src/component/keyboard_sdl2.lua +++ b/src/component/keyboard_sdl2.lua @@ -2,6 +2,7 @@ local address = ... local ffi = require("ffi") local lua_utf8 = require("utf8") +local SDL = elsa.SDL -- Conversion table for SDL2 keys to LWJGL key codes local keys = require("support.sdl_to_lwjgl") @@ -21,6 +22,11 @@ function elsa.keydown(event) local key = keyevent.keysym.scancode cprint("keydown",keys[key]) table.insert(kbdcodes,{type="key_down",addr=address,code=keys[key]}) + if keys[key] == 210 then + if SDL.hasClipboardText() > 0 then + table.insert(machine.signals,{"clipboard",address,ffi.string(SDL.getClipboardText())}) + end + end end function elsa.keyup(event)