diff options
| author | Andrew Lee <alee14498@gmail.com> | 2019-07-15 13:12:04 -0400 |
|---|---|---|
| committer | Andrew Lee <alee14498@gmail.com> | 2019-07-15 13:12:04 -0400 |
| commit | bc294508b9d6566add6605bb7da09d3f6b74f9bb (patch) | |
| tree | 2dc2e09e6aa5e3edcc98e1a22217ac61c702c4a6 | |
| parent | a3e41759b761898f24b9e8b7c5e0679ca273df07 (diff) | |
| download | bits-UI-bc294508b9d6566add6605bb7da09d3f6b74f9bb.tar.gz bits-UI-bc294508b9d6566add6605bb7da09d3f6b74f9bb.tar.bz2 bits-UI-bc294508b9d6566add6605bb7da09d3f6b74f9bb.zip | |
Corrections and new API!
| -rw-r--r-- | README.md | 1 | ||||
| -rw-r--r-- | startup.lua | 11 | ||||
| -rw-r--r-- | system/apis/json.lua | 209 | ||||
| -rw-r--r-- | system/boot.lua | 2 | ||||
| -rw-r--r-- | system/post-setup.lua | 16 | ||||
| -rw-r--r-- | system/recovery/main.lua | 27 | ||||
| -rw-r--r-- | system/recovery/reset.lua (renamed from system/reset.lua) | 0 | ||||
| -rw-r--r-- | system/recovery/transfer.lua | 29 | ||||
| -rw-r--r-- | system/transfer.lua | 13 |
9 files changed, 287 insertions, 21 deletions
@@ -13,6 +13,7 @@ The programs/apis I used for bits-UI. - Mouse File Browser by Stiepen irc(Kilobyte), Cruor and BigSHinyToys - fLib by NDFJay - Sha256 by CompuTech +- JSON API by ElvishJerricco # Installation If you want to get this running be sure to have a ComputerCraft emulator such as <a href="http://www.computercraft.info/forums2/index.php?/topic/18789-ccemuredux-computercraft-emulator-redux/">CCEmuRedux</a> or <a href="https://emux.cc/">CCEmuX</a>. But if you want to run this in minecraft be sure to have CC-Tweaked. diff --git a/startup.lua b/startup.lua index 853bac9..eccb3b6 100644 --- a/startup.lua +++ b/startup.lua @@ -32,8 +32,10 @@ function bootloader() term.setCursorPos(1,5) print("2. Update bits-UI\n") term.setCursorPos(1,6) - print("3. Boot CraftOS\n") - term.setCursorPos(1,8) + print("3. Recovery Mode\n") + term.setCursorPos(1,7) + print("4. Boot CraftOS\n") + term.setCursorPos(1,9) term.write("> ") end @@ -77,6 +79,11 @@ function bootloaderInput() end elseif input == "3" then clear() + print("Running Recovery Mode...") + sleep(2) + shell.run("/system/recovery/main.lua") + elseif input == "4" then + clear() term.setTextColor(16) print(os.version()) term.setCursorPos(1,2) diff --git a/system/apis/json.lua b/system/apis/json.lua new file mode 100644 index 0000000..3bef482 --- /dev/null +++ b/system/apis/json.lua @@ -0,0 +1,209 @@ +------------------------------------------------------------------ utils +local controls = {["\n"]="\\n", ["\r"]="\\r", ["\t"]="\\t", ["\b"]="\\b", ["\f"]="\\f", ["\""]="\\\"", ["\\"]="\\\\"} + +local function isArray(t) + local max = 0 + for k,v in pairs(t) do + if type(k) ~= "number" then + return false + elseif k > max then + max = k + end + end + return max == #t +end + +local whites = {['\n']=true; ['\r']=true; ['\t']=true; [' ']=true; [',']=true; [':']=true} +function removeWhite(str) + while whites[str:sub(1, 1)] do + str = str:sub(2) + end + return str +end + +------------------------------------------------------------------ encoding + +local function encodeCommon(val, pretty, tabLevel, tTracking) + local str = "" + + -- Tabbing util + local function tab(s) + str = str .. ("\t"):rep(tabLevel) .. s + end + + local function arrEncoding(val, bracket, closeBracket, iterator, loopFunc) + str = str .. bracket + if pretty then + str = str .. "\n" + tabLevel = tabLevel + 1 + end + for k,v in iterator(val) do + tab("") + loopFunc(k,v) + str = str .. "," + if pretty then str = str .. "\n" end + end + if pretty then + tabLevel = tabLevel - 1 + end + if str:sub(-2) == ",\n" then + str = str:sub(1, -3) .. "\n" + elseif str:sub(-1) == "," then + str = str:sub(1, -2) + end + tab(closeBracket) + end + + -- Table encoding + if type(val) == "table" then + assert(not tTracking[val], "Cannot encode a table holding itself recursively") + tTracking[val] = true + if isArray(val) then + arrEncoding(val, "[", "]", ipairs, function(k,v) + str = str .. encodeCommon(v, pretty, tabLevel, tTracking) + end) + else + arrEncoding(val, "{", "}", pairs, function(k,v) + assert(type(k) == "string", "JSON object keys must be strings", 2) + str = str .. encodeCommon(k, pretty, tabLevel, tTracking) + str = str .. (pretty and ": " or ":") .. encodeCommon(v, pretty, tabLevel, tTracking) + end) + end + -- String encoding + elseif type(val) == "string" then + str = '"' .. val:gsub("[%c\"\\]", controls) .. '"' + -- Number encoding + elseif type(val) == "number" or type(val) == "boolean" then + str = tostring(val) + else + error("JSON only supports arrays, objects, numbers, booleans, and strings", 2) + end + return str +end + +function encode(val) + return encodeCommon(val, false, 0, {}) +end + +function encodePretty(val) + return encodeCommon(val, true, 0, {}) +end + +------------------------------------------------------------------ decoding + +local decodeControls = {} +for k,v in pairs(controls) do + decodeControls[v] = k +end + +function parseBoolean(str) + if str:sub(1, 4) == "true" then + return true, removeWhite(str:sub(5)) + else + return false, removeWhite(str:sub(6)) + end +end + +function parseNull(str) + return nil, removeWhite(str:sub(5)) +end + +local numChars = {['e']=true; ['E']=true; ['+']=true; ['-']=true; ['.']=true} +function parseNumber(str) + local i = 1 + while numChars[str:sub(i, i)] or tonumber(str:sub(i, i)) do + i = i + 1 + end + local val = tonumber(str:sub(1, i - 1)) + str = removeWhite(str:sub(i)) + return val, str +end + +function parseString(str) + str = str:sub(2) + local s = "" + while str:sub(1,1) ~= "\"" do + local next = str:sub(1,1) + str = str:sub(2) + assert(next ~= "\n", "Unclosed string") + + if next == "\\" then + local escape = str:sub(1,1) + str = str:sub(2) + + next = assert(decodeControls[next..escape], "Invalid escape character") + end + + s = s .. next + end + return s, removeWhite(str:sub(2)) +end + +function parseArray(str) + str = removeWhite(str:sub(2)) + + local val = {} + local i = 1 + while str:sub(1, 1) ~= "]" do + local v = nil + v, str = parseValue(str) + val[i] = v + i = i + 1 + str = removeWhite(str) + end + str = removeWhite(str:sub(2)) + return val, str +end + +function parseObject(str) + str = removeWhite(str:sub(2)) + + local val = {} + while str:sub(1, 1) ~= "}" do + local k, v = nil, nil + k, v, str = parseMember(str) + val[k] = v + str = removeWhite(str) + end + str = removeWhite(str:sub(2)) + return val, str +end + +function parseMember(str) + local k = nil + k, str = parseValue(str) + local val = nil + val, str = parseValue(str) + return k, val, str +end + +function parseValue(str) + local fchar = str:sub(1, 1) + if fchar == "{" then + return parseObject(str) + elseif fchar == "[" then + return parseArray(str) + elseif tonumber(fchar) ~= nil or numChars[fchar] then + return parseNumber(str) + elseif str:sub(1, 4) == "true" or str:sub(1, 5) == "false" then + return parseBoolean(str) + elseif fchar == "\"" then + return parseString(str) + elseif str:sub(1, 4) == "null" then + return parseNull(str) + end + return nil +end + +function decode(str) + str = removeWhite(str) + t = parseValue(str) + return t +end + +function decodeFromFile(path) + local file = assert(fs.open(path, "r")) + local decoded = decode(file.readAll()) + file.close() + return decoded +end
\ No newline at end of file diff --git a/system/boot.lua b/system/boot.lua index 08d5d64..5418427 100644 --- a/system/boot.lua +++ b/system/boot.lua @@ -80,7 +80,7 @@ end sleep(2) term.setTextColor(colors.green) -print("[DONE] Boot sequence is completed...") +print("[DONE] Boot sequence has been completed...") term.setTextColor(colors.white) sleep(2) shell.run(desktop)
\ No newline at end of file diff --git a/system/post-setup.lua b/system/post-setup.lua index 1e6bfe0..9ff14d6 100644 --- a/system/post-setup.lua +++ b/system/post-setup.lua @@ -38,12 +38,18 @@ print("Welcome to the bits-UI Post Setup!") sleep(2) print("Please enter your password.") print("(Don't set your real password in servers.)") + local passPath = "/etc/passwd.pwd" -local passwd = read(" ") -local insertPasswd = fs.open(passPath, "a") -insertPasswd.writeLine(passwd) -insertPasswd.close() -print("Thanks, I will save that.") +if fs.exists(passPath) then + print("[INFO] Password file exists! Skipping.") + sleep(2) +else + local passwd = read(" ") + local insertPasswd = fs.open(passPath, "a") + insertPasswd.writeLine(passwd) + insertPasswd.close() + print("Thanks, I will save that.") +end sleep(1) shell.run("/system/desktop.lua")
\ No newline at end of file diff --git a/system/recovery/main.lua b/system/recovery/main.lua new file mode 100644 index 0000000..39c1ec5 --- /dev/null +++ b/system/recovery/main.lua @@ -0,0 +1,27 @@ +--[[ + bits-UI Recovery Script: A script that's going to show you to either reset the computer or transfer files. + Copyright (C) 2019 Alee14 + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + +]]-- +term.clear() +term.setCursorPos(1,1) +term.setTextColor(colors.white) +print("Welcome to the bits-UI recovery mode!") +sleep(2) +print("Do you want to either reset or transfer?") +local input = read(); +if input == "reset" then + shell.run("/system/recovery/reset.lua") +elseif input == "transfer" then + shell.run("/system/recovery/transfer.lua") +end
\ No newline at end of file diff --git a/system/reset.lua b/system/recovery/reset.lua index 9b8efb4..9b8efb4 100644 --- a/system/reset.lua +++ b/system/recovery/reset.lua diff --git a/system/recovery/transfer.lua b/system/recovery/transfer.lua new file mode 100644 index 0000000..56be78c --- /dev/null +++ b/system/recovery/transfer.lua @@ -0,0 +1,29 @@ +--[[
+ bits-UI Transfer Script: A script that will transfer files from one system to the other.
+ Copyright (C) 2019 Alee14
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+]]--
+term.clear()
+term.setCursorPos(1,1)
+term.setTextColor(colors.white)
+print("Welcome to the bits-UI transfer!")
+sleep(2)
+if fs.exists("/disk") then
+ shell.run("copy", "/home", "/disk")
+ shell.run("copy", "/etc/passwd.pwd", "/disk")
+else
+ print("You need a floppy disk to copy over data.")
+ print("Transfer has been halted.")
+ sleep(2)
+ shell.run("/startup.lua")
+end
\ No newline at end of file diff --git a/system/transfer.lua b/system/transfer.lua deleted file mode 100644 index cc25dac..0000000 --- a/system/transfer.lua +++ /dev/null @@ -1,13 +0,0 @@ -term.clear()
-term.setCursorPos(1,1)
-term.setTextColor(colors.white)
-print("Welcome to the bits-UI transfer!")
-sleep(2)
-if fs.exists("/disk") then
- shell.run("copy", "/home", "/disk")
- shell.run("copy", "/etc/passwd.pwd", "/disk")
-else
- print("You need a floppy disk to copy over data.")
- print("Installation has been halted.")
- sleep(2)
-end
\ No newline at end of file |
