Compare commits

..

14 Commits
main ... keypad

Author SHA1 Message Date
9e633867da
:3 2024-08-26 00:16:13 +03:00
79c5654247
:3 2024-08-26 00:13:01 +03:00
6b864b43be
:3 2024-08-26 00:09:32 +03:00
5fa92ae0c4
:3 2024-08-26 00:08:40 +03:00
4ccc4b07da
:3 2024-08-26 00:07:19 +03:00
b0c5b756b1
:3 2024-08-26 00:05:54 +03:00
a3ca65a458
:3 2024-08-26 00:04:22 +03:00
d07a35f360
:3 2024-08-26 00:03:08 +03:00
b88ca058fa
:3 2024-08-25 23:58:57 +03:00
df5e544347
:3 2024-08-25 23:56:01 +03:00
7c78db3620
:3 2024-08-25 23:53:40 +03:00
7ed9f76f15
:3 2024-08-25 23:47:51 +03:00
2a8932a19f
:3 2024-08-25 23:36:03 +03:00
28ceac8643
:3 2024-08-25 22:00:12 +03:00
12 changed files with 469 additions and 1109 deletions

View File

@ -1,23 +0,0 @@
--- @class Exports
--- @field log Logger
--- @field updater Updater
--- @field notifier Notifier
--- @field json JsonParser
--- @class Logger
--- @field error fun(...)
--- @field warn fun(...)
--- @field info fun(...)
--- @field debug fun(...)
--- @class Updater
--- @field addEntry fun(self: Updater, path: string, branch: string, url: string)
--- @class JsonParser
--- @field decode fun(s: string): table
--- @field encode fun(s: table): string
--- @class Notifier
--- @field notify fun(priority: "1"|"2"|"3"|"4"|"5", body: string)

199
keypadOS.lua Normal file
View File

@ -0,0 +1,199 @@
local __BUNDLER_FILES = {}
local __DEFAULT_IMPORT = require
local require = function(path)
if __BUNDLER_FILES[path] then
return __BUNDLER_FILES[path]()
elseif __BUNDLER_FILES[path .. ".lua"] then
return __BUNDLER_FILES[path .. ".lua"]()
else
return __DEFAULT_IMPORT(path)
end
end
local __UPDATE_HASH = "5e1c10aba96c3f9ded6aecb030f2774577c005af"
rawset(__BUNDLER_FILES, "updater.lua", function ()
local utils = require("utils.lua")
local config = require("config.lua")
local mod = {}
function mod.GetBasalt()
if fs.exists("basalt.lua") then
utils.MonPrint("Basalt found!")
else
utils.MonPrint("Downloading basalt...")
local basalt_code = http.get(config.basalt_url).readAll()
utils.MonPrint("Installing basalt...")
local file = fs.open("basalt.lua", "w")
if not file then
utils.MonPrint("failed to get basalt")
sleep(60)
os.reboot()
end
file.write(utils.Cast(basalt_code))
file.close()
utils.MonPrint("Rebooting...")
os.reboot()
end
end
return mod
end)
rawset(__BUNDLER_FILES, "config.lua", function ()
local config = {
correctPin = "42069",
release_url = "https://git.mcorangehq.xyz/xomf/keypadOS/raw/branch/main/keypadOS.lua",
basalt_url = "https://raw.githubusercontent.com/Pyroxenium/Basalt/master/docs/versions/latest.lua",
ntfy_url = "https://ntfy.sh/keypadOS_alerts",
config_path = "keypadOS.config.lua"
}
local DEFAULT_CONFIG = "return {\n" ..
" group=\"default\",\n" ..
"}\n"
function config.ReadConfig()
if not fs.exists(config.config_path) then
local f = fs.open(config.config_path, "w")
f.write(DEFAULT_CONFIG)
f.close();
end
local cfg = require(config.config_path)
return cfg
end
return config
end)
rawset(__BUNDLER_FILES, "ui.lua", function ()
local utils = require("utils.lua")
local basalt = require("basalt")
local config = require("config.lua")
local updater = require("updater.lua")
local monitor = utils.Cast(peripheral.find("monitor"))
local drive = utils.Cast(peripheral.find("drive"))
local mod = {}
local function resetEverything(ui)
sleep(2)
ui.pin = ""
ui.pinLabel:setText("")
redstone.setOutput("front", false)
ui.enterButton:setBackground(colors.blue)
end
local function unlockDoor(ui)
if drive.isDiskPresent() then
if drive.getDiskLabel() == config.correctPin then
ui.pin = config.correctPin
drive:ejectDisk()
end
end
if ui.pin == config.correctPin then
blibs.notifier.notify("5", "Unlocked door :3 love u");
ui.enterButton:setBackground(colors.green)
ui.pinLabel:setText("Welcome")
redstone.setOutput("front", true)
if drive.isDiskPresent() then
if drive.getDiskLabel() == nil then
drive.setDiskLabel(config.correctPin)
ui.pinLabel:setText("Crd set")
drive:ejectDisk()
end
end
else
ui.pinLabel:setText("Wrong")
ui.enterButton:setBackground(colors.red)
end
ui.main:addThread():start(function()
resetEverything(ui)
end)
end
local function addToPin(ui, i)
if #ui.pin >= 5 then
return
end
ui.pin = ui.pin .. tostring(i)
ui.pinLabel:setText(ui.pin)
end
function mod.InitUi()
local ui = {
pin = "",
main = basalt.addMonitor(),
}
ui.main:setMonitor(monitor)
ui.pinLabel = ui.main:addLabel()
:setText("")
:setFontSize(1)
ui.enterButton = ui.main:addButton()
:setText(">>>>")
:setBackground(colors.blue)
:setPosition(6,2)
:setSize(1.5,3.2)
:onClick(function()
unlockDoor(ui)
end)
local btnX = 1
local btnY = 2
ui.main:addButton()
:setPosition(1, 5)
:setText("0")
:setSize(6,1)
:onClick(function()
addToPin(ui, 0)
end)
for i = 1, 9 do
ui.main:addButton()
:setPosition(btnX, btnY)
:setText(tostring(i))
:setSize(2,1)
:onClick(function()
addToPin(ui, i)
end)
btnX = btnX + 2
if btnX >= 6 then
btnY = btnY + 1
btnX = 1
end
end
local status, err = pcall(function ()
parallel.waitForAll(basalt.autoUpdate)
end)
if not status and err ~= "Terminated" then
log.error("Error detected: " .. err)
blibs.notifier.notify("5", err); --exposed ntfy url no spam me pls
sleep(5)
utils.MonReset(0.5)
fs.delete("basalt.lua")
fs.delete("startup.lua")
fs.copy("backup.lua", "startup.lua")
os.reboot()
end
end
return mod
end)
rawset(__BUNDLER_FILES, "utils.lua", function ()
local utils = {}
function utils.Cast(object)
return object
end
local MONITOR = utils.Cast(peripheral.find("monitor"))
local MONITOR_Y = 1
function utils.MonPrint(text)
MONITOR.setCursorPos(1,MONITOR_Y)
MONITOR.write(text)
MONITOR_Y = MONITOR_Y + 1
end
function utils.MonReset(scale)
MONITOR.clear()
MONITOR.setTextScale(scale)
end
return utils;
end)
rawset(__BUNDLER_FILES, "main.lua", function ()
local utils = require("utils.lua")
local updater = require("updater.lua")
local mod = {}
KEYPADOS_VERSION = "4.0"
function mod.main(blibs)
_G.log = blibs.log;
_G.blibs = blibs;
utils.MonReset(0.5)
updater.GetBasalt()
utils.MonPrint("keypadOS v" .. KEYPADOS_VERSION)
utils.MonReset(1)
require("ui.lua").InitUi()
end
return mod;
end)
return require("main.lua")

470
rtmc.lua
View File

@ -1,470 +0,0 @@
local __BUNDLER_FILES = {}
local __VERSION = "1.0.0d"
local __DEFAULT_IMPORT = require
local require = function(path)
if __BUNDLER_FILES[path] then
return __BUNDLER_FILES[path]()
elseif __BUNDLER_FILES[path .. ".lua"] then
return __BUNDLER_FILES[path .. ".lua"]()
else
return __DEFAULT_IMPORT(path)
end
end
rawset(__BUNDLER_FILES, "main", function ()
local updater = require("updater");
local log = require("log");
local notifier = require("notifier")
local json = require("json")
local MODULE_NAME = "keypadOS";
local MODULE_URL = "https://git.mcorangehq.xyz/xomf/keypadOS/raw/branch/keypad/keypadOS.lua";
if not __VERSION then
__VERSION = "DEV";
end
local function updaterLoop(upd)
while true do
sleep(1)
upd:checkAndUpdateAll();
end
end
local function _start()
log.info("Starting bootloader (" .. __VERSION .. ")");
local upd = updater.new();
upd:addEntry(MODULE_NAME .. ".lua", "keypad", MODULE_URL);
local lib_exports = {
log = log,
updater = upd,
notifier = notifier,
json = json
};
parallel.waitForAny(
function()
updaterLoop(upd)
end,
function ()
require(MODULE_NAME).main(lib_exports);
end
);
end
local mod = {};
mod.main = _start;
return mod;
end)
rawset(__BUNDLER_FILES, "log", function ()
local log = {};
log.__index = log;
function log.error(...)
print("[ERROR] " .. ...);
end
function log.warn(...)
print("[WARN] " .. ...);
end
function log.info(...)
print("[INFO] " .. ...);
end
function log.debug(...)
print("[DEBUG] " .. ...);
end
return log;
end)
rawset(__BUNDLER_FILES, "json", function ()
local json = { _version = "0.1.2" }
local encode
local escape_char_map = {
[ "\\" ] = "\\",
[ "\"" ] = "\"",
[ "\b" ] = "b",
[ "\f" ] = "f",
[ "\n" ] = "n",
[ "\r" ] = "r",
[ "\t" ] = "t",
}
local escape_char_map_inv = { [ "/" ] = "/" }
for k, v in pairs(escape_char_map) do
escape_char_map_inv[v] = k
end
local function escape_char(c)
return "\\" .. (escape_char_map[c] or string.format("u%04x", c:byte()))
end
local function encode_nil(val)
return "null"
end
local function encode_table(val, stack)
local res = {}
stack = stack or {}
if stack[val] then error("circular reference") end
stack[val] = true
if rawget(val, 1) ~= nil or next(val) == nil then
local n = 0
for k in pairs(val) do
if type(k) ~= "number" then
error("invalid table: mixed or invalid key types")
end
n = n + 1
end
if n ~= #val then
error("invalid table: sparse array")
end
for i, v in ipairs(val) do
table.insert(res, encode(v, stack))
end
stack[val] = nil
return "[" .. table.concat(res, ",") .. "]"
else
for k, v in pairs(val) do
if type(k) ~= "string" then
error("invalid table: mixed or invalid key types")
end
table.insert(res, encode(k, stack) .. ":" .. encode(v, stack))
end
stack[val] = nil
return "{" .. table.concat(res, ",") .. "}"
end
end
local function encode_string(val)
return '"' .. val:gsub('[%z\1-\31\\"]', escape_char) .. '"'
end
local function encode_number(val)
if val ~= val or val <= -math.huge or val >= math.huge then
error("unexpected number value '" .. tostring(val) .. "'")
end
return string.format("%.14g", val)
end
local type_func_map = {
[ "nil" ] = encode_nil,
[ "table" ] = encode_table,
[ "string" ] = encode_string,
[ "number" ] = encode_number,
[ "boolean" ] = tostring,
}
encode = function(val, stack)
local t = type(val)
local f = type_func_map[t]
if f then
return f(val, stack)
end
error("unexpected type '" .. t .. "'")
end
function json.encode(val)
return ( encode(val) )
end
local parse
local function create_set(...)
local res = {}
for i = 1, select("#", ...) do
res[ select(i, ...) ] = true
end
return res
end
local space_chars = create_set(" ", "\t", "\r", "\n")
local delim_chars = create_set(" ", "\t", "\r", "\n", "]", "}", ",")
local escape_chars = create_set("\\", "/", '"', "b", "f", "n", "r", "t", "u")
local literals = create_set("true", "false", "null")
local literal_map = {
[ "true" ] = true,
[ "false" ] = false,
[ "null" ] = nil,
}
local function next_char(str, idx, set, negate)
for i = idx, #str do
if set[str:sub(i, i)] ~= negate then
return i
end
end
return #str + 1
end
local function decode_error(str, idx, msg)
local line_count = 1
local col_count = 1
for i = 1, idx - 1 do
col_count = col_count + 1
if str:sub(i, i) == "\n" then
line_count = line_count + 1
col_count = 1
end
end
error( string.format("%s at line %d col %d", msg, line_count, col_count) )
end
local function codepoint_to_utf8(n)
local f = math.floor
if n <= 0x7f then
return string.char(n)
elseif n <= 0x7ff then
return string.char(f(n / 64) + 192, n % 64 + 128)
elseif n <= 0xffff then
return string.char(f(n / 4096) + 224, f(n % 4096 / 64) + 128, n % 64 + 128)
elseif n <= 0x10ffff then
return string.char(f(n / 262144) + 240, f(n % 262144 / 4096) + 128,
f(n % 4096 / 64) + 128, n % 64 + 128)
end
error( string.format("invalid unicode codepoint '%x'", n) )
end
local function parse_unicode_escape(s)
local n1 = tonumber( s:sub(1, 4), 16 )
local n2 = tonumber( s:sub(7, 10), 16 )
if n2 then
return codepoint_to_utf8((n1 - 0xd800) * 0x400 + (n2 - 0xdc00) + 0x10000)
else
return codepoint_to_utf8(n1)
end
end
local function parse_string(str, i)
local res = ""
local j = i + 1
local k = j
while j <= #str do
local x = str:byte(j)
if x < 32 then
decode_error(str, j, "control character in string")
elseif x == 92 then -- `\`: Escape
res = res .. str:sub(k, j - 1)
j = j + 1
local c = str:sub(j, j)
if c == "u" then
local hex = str:match("^[dD][89aAbB]%x%x\\u%x%x%x%x", j + 1)
or str:match("^%x%x%x%x", j + 1)
or decode_error(str, j - 1, "invalid unicode escape in string")
res = res .. parse_unicode_escape(hex)
j = j + #hex
else
if not escape_chars[c] then
decode_error(str, j - 1, "invalid escape char '" .. c .. "' in string")
end
res = res .. escape_char_map_inv[c]
end
k = j + 1
elseif x == 34 then -- `"`: End of string
res = res .. str:sub(k, j - 1)
return res, j + 1
end
j = j + 1
end
decode_error(str, i, "expected closing quote for string")
end
local function parse_number(str, i)
local x = next_char(str, i, delim_chars)
local s = str:sub(i, x - 1)
local n = tonumber(s)
if not n then
decode_error(str, i, "invalid number '" .. s .. "'")
end
return n, x
end
local function parse_literal(str, i)
local x = next_char(str, i, delim_chars)
local word = str:sub(i, x - 1)
if not literals[word] then
decode_error(str, i, "invalid literal '" .. word .. "'")
end
return literal_map[word], x
end
local function parse_array(str, i)
local res = {}
local n = 1
i = i + 1
while 1 do
local x
i = next_char(str, i, space_chars, true)
if str:sub(i, i) == "]" then
i = i + 1
break
end
x, i = parse(str, i)
res[n] = x
n = n + 1
i = next_char(str, i, space_chars, true)
local chr = str:sub(i, i)
i = i + 1
if chr == "]" then break end
if chr ~= "," then decode_error(str, i, "expected ']' or ','") end
end
return res, i
end
local function parse_object(str, i)
local res = {}
i = i + 1
while 1 do
local key, val
i = next_char(str, i, space_chars, true)
if str:sub(i, i) == "}" then
i = i + 1
break
end
if str:sub(i, i) ~= '"' then
decode_error(str, i, "expected string for key")
end
key, i = parse(str, i)
i = next_char(str, i, space_chars, true)
if str:sub(i, i) ~= ":" then
decode_error(str, i, "expected ':' after key")
end
i = next_char(str, i + 1, space_chars, true)
val, i = parse(str, i)
res[key] = val
i = next_char(str, i, space_chars, true)
local chr = str:sub(i, i)
i = i + 1
if chr == "}" then break end
if chr ~= "," then decode_error(str, i, "expected '}' or ','") end
end
return res, i
end
local char_func_map = {
[ '"' ] = parse_string,
[ "0" ] = parse_number,
[ "1" ] = parse_number,
[ "2" ] = parse_number,
[ "3" ] = parse_number,
[ "4" ] = parse_number,
[ "5" ] = parse_number,
[ "6" ] = parse_number,
[ "7" ] = parse_number,
[ "8" ] = parse_number,
[ "9" ] = parse_number,
[ "-" ] = parse_number,
[ "t" ] = parse_literal,
[ "f" ] = parse_literal,
[ "n" ] = parse_literal,
[ "[" ] = parse_array,
[ "{" ] = parse_object,
}
parse = function(str, idx)
local chr = str:sub(idx, idx)
local f = char_func_map[chr]
if f then
return f(str, idx)
end
decode_error(str, idx, "unexpected character '" .. chr .. "'")
end
function json.decode(str)
if type(str) ~= "string" then
error("expected argument of type string, got " .. type(str))
end
local res, idx = parse(str, next_char(str, 1, space_chars, true))
idx = next_char(str, idx, space_chars, true)
if idx <= #str then
decode_error(str, idx, "trailing garbage")
end
return res
end
return json
end)
rawset(__BUNDLER_FILES, "updater", function ()
local log = require("log");
local json = require("json");
local notifier = require("notifier");
local _;
local updater = {};
updater.__index = updater;
function updater.new()
local c = setmetatable({}, updater);
c.last_check = os.time();
c.threshold = ((20*60)/24/60/60) * 5;
c.api_url = "https://git.mcorangehq.xyz/api/v1/repos/xomf/keypadOS";
c.curr_commit_hash = "";
c.updated_files = {};
c:addEntry("startup.lua", "main", "https://git.mcorangehq.xyz/xomf/keypadOS/raw/branch/main/rtmc.lua");
return c
end
function updater:addEntry(path, branch, url)
self.updated_files[path] = {
branch = branch,
url = url
};
self:update(path, url);
end
function updater:checkAndUpdateAll()
local updated = false;
for path, entry in pairs(self.updated_files) do
if self:check(entry.branch) then
self:update(path, entry.url);
updated = true;
end
end
if updated then
log.info("Rebooting!");
sleep(3);
os.reboot();
end
end
function updater:check(branch)
local curr = os.time();
if math.abs(curr - self.last_check) < self.threshold then
if self.curr_commit_hash ~= "" then
return false;
end
end
self.last_check = curr;
local req, rerr = http.get(self.api_url .. "/commits?sha="..branch);
if not req then
log.error("Updater:check: Could not send request: " .. rerr);
return false;
end
local body, berr = req.readAll();
if not body then
log.error("Updater:check: Could not get body of request: " .. berr);
return false;
end
local data = json.decode(body);
if self.curr_commit_hash == "" then
log.debug("No commit hash found, setting");
self.curr_commit_hash = data[1].sha;
elseif data[1].sha ~= self.curr_commit_hash then
log.debug("Commit hash doesnt match, probbably an update");
return true;
end
if data[1].hash == self.curr_commit_hash then
log.debug("Commit hash matches");
log.debug(data[1].sha .. " (old) => (new) " .. self.curr_commit_hash);
end
write(".");
return false;
end
function updater:update(path, url)
local req, rerr = http.get(url .. "?x=" .. tostring(math.random(0,69420)));
if not req then
log.error("Updater:update: Could not send request for update: " .. rerr);
return;
end
local body, berr = req.readAll();
if not body then
log.error("Updater:update: Could not get file for update: " .. berr);
return;
end
if fs.exists(path .. ".bak") then
fs.delete(path .. ".bak");
end
if fs.exists(path) then
fs.copy(path, path..".bak");
fs.delete(path);
end
local fd = fs.open(path, "w");
fd.write(body);
fd.close();
local notif = "Computer #" .. tostring( os.getComputerID() ) .. " updating";
notifier.notify("5", notif);
log.debug("New update written to disk (" .. path .. ")");
end
return updater;
end)
rawset(__BUNDLER_FILES, "notifier", function ()
local notifier = {};
notifier.__index = notifier;
function notifier.notify(priority, body)
local headers = {
["Priority"] = priority;
};
http.post("https://ntfy.sh/keypadOS_alerts", body, headers);
end
return notifier;
end)
if pcall(debug.getlocal, 4, 1) then
local exports = {};
for k, v in pairs(__BUNDLER_FILES) do
if k ~= "main" then
exports[k] = v();
end
end
return exports;
else
require("main").main()
end

28
src/config.lua Normal file
View File

@ -0,0 +1,28 @@
local config = {
correctPin = "42069",
release_url = "https://git.mcorangehq.xyz/xomf/keypadOS/raw/branch/main/keypadOS.lua",
basalt_url = "https://raw.githubusercontent.com/Pyroxenium/Basalt/master/docs/versions/latest.lua",
ntfy_url = "https://ntfy.sh/keypadOS_alerts",
config_path = "keypadOS.config.lua"
}
local DEFAULT_CONFIG = "return {\n" ..
" group=\"default\",\n" ..
"}\n"
--- @class Config
--- @field group string
--- Read current configs, create if doesnt exist :3
---@return Config
function config.ReadConfig()
if not fs.exists(config.config_path) then
local f = fs.open(config.config_path, "w")
f.write(DEFAULT_CONFIG)
f.close();
end
local cfg = require(config.config_path)
return cfg
end
return config

View File

@ -1,389 +0,0 @@
--
-- json.lua
--
-- Copyright (c) 2020 rxi
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy of
-- this software and associated documentation files (the "Software"), to deal in
-- the Software without restriction, including without limitation the rights to
-- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
-- of the Software, and to permit persons to whom the Software is furnished to do
-- so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in all
-- copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
--
--- @class JsonParser
local json = { _version = "0.1.2" }
-------------------------------------------------------------------------------
-- Encode
-------------------------------------------------------------------------------
local encode
local escape_char_map = {
[ "\\" ] = "\\",
[ "\"" ] = "\"",
[ "\b" ] = "b",
[ "\f" ] = "f",
[ "\n" ] = "n",
[ "\r" ] = "r",
[ "\t" ] = "t",
}
local escape_char_map_inv = { [ "/" ] = "/" }
for k, v in pairs(escape_char_map) do
escape_char_map_inv[v] = k
end
local function escape_char(c)
return "\\" .. (escape_char_map[c] or string.format("u%04x", c:byte()))
end
local function encode_nil(val)
return "null"
end
local function encode_table(val, stack)
local res = {}
stack = stack or {}
-- Circular reference?
if stack[val] then error("circular reference") end
stack[val] = true
if rawget(val, 1) ~= nil or next(val) == nil then
-- Treat as array -- check keys are valid and it is not sparse
local n = 0
for k in pairs(val) do
if type(k) ~= "number" then
error("invalid table: mixed or invalid key types")
end
n = n + 1
end
if n ~= #val then
error("invalid table: sparse array")
end
-- Encode
for i, v in ipairs(val) do
table.insert(res, encode(v, stack))
end
stack[val] = nil
return "[" .. table.concat(res, ",") .. "]"
else
-- Treat as an object
for k, v in pairs(val) do
if type(k) ~= "string" then
error("invalid table: mixed or invalid key types")
end
table.insert(res, encode(k, stack) .. ":" .. encode(v, stack))
end
stack[val] = nil
return "{" .. table.concat(res, ",") .. "}"
end
end
local function encode_string(val)
return '"' .. val:gsub('[%z\1-\31\\"]', escape_char) .. '"'
end
local function encode_number(val)
-- Check for NaN, -inf and inf
if val ~= val or val <= -math.huge or val >= math.huge then
error("unexpected number value '" .. tostring(val) .. "'")
end
return string.format("%.14g", val)
end
local type_func_map = {
[ "nil" ] = encode_nil,
[ "table" ] = encode_table,
[ "string" ] = encode_string,
[ "number" ] = encode_number,
[ "boolean" ] = tostring,
}
encode = function(val, stack)
local t = type(val)
local f = type_func_map[t]
if f then
return f(val, stack)
end
error("unexpected type '" .. t .. "'")
end
function json.encode(val)
return ( encode(val) )
end
-------------------------------------------------------------------------------
-- Decode
-------------------------------------------------------------------------------
local parse
local function create_set(...)
local res = {}
for i = 1, select("#", ...) do
res[ select(i, ...) ] = true
end
return res
end
local space_chars = create_set(" ", "\t", "\r", "\n")
local delim_chars = create_set(" ", "\t", "\r", "\n", "]", "}", ",")
local escape_chars = create_set("\\", "/", '"', "b", "f", "n", "r", "t", "u")
local literals = create_set("true", "false", "null")
local literal_map = {
[ "true" ] = true,
[ "false" ] = false,
[ "null" ] = nil,
}
local function next_char(str, idx, set, negate)
for i = idx, #str do
if set[str:sub(i, i)] ~= negate then
return i
end
end
return #str + 1
end
local function decode_error(str, idx, msg)
local line_count = 1
local col_count = 1
for i = 1, idx - 1 do
col_count = col_count + 1
if str:sub(i, i) == "\n" then
line_count = line_count + 1
col_count = 1
end
end
error( string.format("%s at line %d col %d", msg, line_count, col_count) )
end
local function codepoint_to_utf8(n)
-- http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=iws-appendixa
local f = math.floor
if n <= 0x7f then
return string.char(n)
elseif n <= 0x7ff then
return string.char(f(n / 64) + 192, n % 64 + 128)
elseif n <= 0xffff then
return string.char(f(n / 4096) + 224, f(n % 4096 / 64) + 128, n % 64 + 128)
elseif n <= 0x10ffff then
return string.char(f(n / 262144) + 240, f(n % 262144 / 4096) + 128,
f(n % 4096 / 64) + 128, n % 64 + 128)
end
error( string.format("invalid unicode codepoint '%x'", n) )
end
local function parse_unicode_escape(s)
local n1 = tonumber( s:sub(1, 4), 16 )
local n2 = tonumber( s:sub(7, 10), 16 )
-- Surrogate pair?
if n2 then
return codepoint_to_utf8((n1 - 0xd800) * 0x400 + (n2 - 0xdc00) + 0x10000)
else
return codepoint_to_utf8(n1)
end
end
local function parse_string(str, i)
local res = ""
local j = i + 1
local k = j
while j <= #str do
local x = str:byte(j)
if x < 32 then
decode_error(str, j, "control character in string")
elseif x == 92 then -- `\`: Escape
res = res .. str:sub(k, j - 1)
j = j + 1
local c = str:sub(j, j)
if c == "u" then
local hex = str:match("^[dD][89aAbB]%x%x\\u%x%x%x%x", j + 1)
or str:match("^%x%x%x%x", j + 1)
or decode_error(str, j - 1, "invalid unicode escape in string")
res = res .. parse_unicode_escape(hex)
j = j + #hex
else
if not escape_chars[c] then
decode_error(str, j - 1, "invalid escape char '" .. c .. "' in string")
end
res = res .. escape_char_map_inv[c]
end
k = j + 1
elseif x == 34 then -- `"`: End of string
res = res .. str:sub(k, j - 1)
return res, j + 1
end
j = j + 1
end
decode_error(str, i, "expected closing quote for string")
end
local function parse_number(str, i)
local x = next_char(str, i, delim_chars)
local s = str:sub(i, x - 1)
local n = tonumber(s)
if not n then
decode_error(str, i, "invalid number '" .. s .. "'")
end
return n, x
end
local function parse_literal(str, i)
local x = next_char(str, i, delim_chars)
local word = str:sub(i, x - 1)
if not literals[word] then
decode_error(str, i, "invalid literal '" .. word .. "'")
end
return literal_map[word], x
end
local function parse_array(str, i)
local res = {}
local n = 1
i = i + 1
while 1 do
local x
i = next_char(str, i, space_chars, true)
-- Empty / end of array?
if str:sub(i, i) == "]" then
i = i + 1
break
end
-- Read token
x, i = parse(str, i)
res[n] = x
n = n + 1
-- Next token
i = next_char(str, i, space_chars, true)
local chr = str:sub(i, i)
i = i + 1
if chr == "]" then break end
if chr ~= "," then decode_error(str, i, "expected ']' or ','") end
end
return res, i
end
local function parse_object(str, i)
local res = {}
i = i + 1
while 1 do
local key, val
i = next_char(str, i, space_chars, true)
-- Empty / end of object?
if str:sub(i, i) == "}" then
i = i + 1
break
end
-- Read key
if str:sub(i, i) ~= '"' then
decode_error(str, i, "expected string for key")
end
key, i = parse(str, i)
-- Read ':' delimiter
i = next_char(str, i, space_chars, true)
if str:sub(i, i) ~= ":" then
decode_error(str, i, "expected ':' after key")
end
i = next_char(str, i + 1, space_chars, true)
-- Read value
val, i = parse(str, i)
-- Set
res[key] = val
-- Next token
i = next_char(str, i, space_chars, true)
local chr = str:sub(i, i)
i = i + 1
if chr == "}" then break end
if chr ~= "," then decode_error(str, i, "expected '}' or ','") end
end
return res, i
end
local char_func_map = {
[ '"' ] = parse_string,
[ "0" ] = parse_number,
[ "1" ] = parse_number,
[ "2" ] = parse_number,
[ "3" ] = parse_number,
[ "4" ] = parse_number,
[ "5" ] = parse_number,
[ "6" ] = parse_number,
[ "7" ] = parse_number,
[ "8" ] = parse_number,
[ "9" ] = parse_number,
[ "-" ] = parse_number,
[ "t" ] = parse_literal,
[ "f" ] = parse_literal,
[ "n" ] = parse_literal,
[ "[" ] = parse_array,
[ "{" ] = parse_object,
}
parse = function(str, idx)
local chr = str:sub(idx, idx)
local f = char_func_map[chr]
if f then
return f(str, idx)
end
decode_error(str, idx, "unexpected character '" .. chr .. "'")
end
function json.decode(str)
if type(str) ~= "string" then
error("expected argument of type string, got " .. type(str))
end
local res, idx = parse(str, next_char(str, 1, space_chars, true))
idx = next_char(str, idx, space_chars, true)
if idx <= #str then
decode_error(str, idx, "trailing garbage")
end
return res
end
return json

View File

@ -1,19 +0,0 @@
--- @class Logger
local log = {};
log.__index = log;
function log.error(...)
print("[ERROR] " .. ...);
end
function log.warn(...)
print("[WARN] " .. ...);
end
function log.info(...)
print("[INFO] " .. ...);
end
function log.debug(...)
print("[DEBUG] " .. ...);
end
return log;

View File

@ -1,50 +1,44 @@
local updater = require("updater");
local log = require("log");
local notifier = require("notifier")
local json = require("json")
local utils = require("utils.lua")
local updater = require("updater.lua")
local MODULE_NAME = "keypadOS";
local MODULE_URL = "https://git.mcorangehq.xyz/xomf/keypadOS/raw/branch/keypad/keypadOS.lua";
local mod = {}
if not __VERSION then
__VERSION = "DEV";
KEYPADOS_VERSION = "4.0"
--- @class BootloaderLibs
--- @field log Logger
--- @field updater Updater
--- @field notifier Notifier
--- @field json JsonParser
--- @class Logger
--- @field error fun(...)
--- @field warn fun(...)
--- @field info fun(...)
--- @field debug fun(...)
--- @class Updater
--- @field addEntry fun(path: string, branch: string, url: string)
--- @class JsonParser
--- @field decode fun(s: string): table
--- @field encode fun(s: table): string
--- @class Notifier
--- @field notify fun(priority: "1"|"2"|"3"|"4"|"5", body: string)
---@param blibs BootloaderLibs
function mod.main(blibs)
_G.log = blibs.log;
_G.blibs = blibs;
utils.MonReset(0.5)
updater.GetBasalt()
utils.MonPrint("keypadOS v" .. KEYPADOS_VERSION)
utils.MonReset(1)
require("ui.lua").InitUi()
end
--- @param upd Updater
local function updaterLoop(upd)
while true do
sleep(1)
upd:checkAndUpdateAll();
end
end
local function _start()
log.info("Starting bootloader (" .. __VERSION .. ")");
local upd = updater.new();
upd:addEntry(MODULE_NAME .. ".lua", "keypad", MODULE_URL);
--- @type Exports
local lib_exports = {
log = log,
updater = upd,
notifier = notifier,
json = json
};
parallel.waitForAny(
function()
updaterLoop(upd)
end,
function ()
require(MODULE_NAME).main(lib_exports);
end
);
end
local mod = {};
mod.main = _start;
return mod;

View File

@ -1,16 +0,0 @@
--- @class Notifier
local notifier = {};
notifier.__index = notifier;
--- @param priority "1"|"2"|"3"|"4"|"5"
--- @param body string
function notifier.notify(priority, body)
local headers = {
["Priority"] = priority;
};
http.post("https://ntfy.sh/keypadOS_alerts", body, headers);
end
return notifier;

136
src/ui.lua Normal file
View File

@ -0,0 +1,136 @@
local utils = require("utils.lua")
local basalt = require("basalt")
local config = require("config.lua")
local updater = require("updater.lua")
--- @type Monitor
local monitor = utils.Cast(peripheral.find("monitor"))
--- @type drive
local drive = utils.Cast(peripheral.find("drive"))
local mod = {}
--- @class Ui
--- @field pin string
--- @field main any
--- @field pinLabel any
--- @field enterButton any
--- @field resetEverything function
--- @field unlockDoor function
--- @field addToPin function
--- @param ui Ui
local function resetEverything(ui)
sleep(2)
ui.pin = ""
ui.pinLabel:setText("")
redstone.setOutput("front", false)
ui.enterButton:setBackground(colors.blue)
end
--- @param ui Ui
local function unlockDoor(ui)
if drive.isDiskPresent() then
if drive.getDiskLabel() == config.correctPin then
ui.pin = config.correctPin
drive:ejectDisk()
end
end
if ui.pin == config.correctPin then
blibs.notifier.notify("5", "Unlocked door :3 love u");
ui.enterButton:setBackground(colors.green)
ui.pinLabel:setText("Welcome")
redstone.setOutput("front", true)
if drive.isDiskPresent() then
if drive.getDiskLabel() == nil then
drive.setDiskLabel(config.correctPin)
ui.pinLabel:setText("Crd set")
drive:ejectDisk()
end
end
else
ui.pinLabel:setText("Wrong")
ui.enterButton:setBackground(colors.red)
end
ui.main:addThread():start(function()
resetEverything(ui)
end)
end
--- @param ui Ui
local function addToPin(ui, i)
if #ui.pin >= 5 then
return
end
ui.pin = ui.pin .. tostring(i)
ui.pinLabel:setText(ui.pin)
end
function mod.InitUi()
local ui = {
pin = "",
main = basalt.addMonitor(),
}
ui.main:setMonitor(monitor)
ui.pinLabel = ui.main:addLabel()
:setText("")
:setFontSize(1)
ui.enterButton = ui.main:addButton()
:setText(">>>>")
:setBackground(colors.blue)
:setPosition(6,2)
:setSize(1.5,3.2)
:onClick(function()
unlockDoor(ui)
end)
local btnX = 1
local btnY = 2
ui.main:addButton()
:setPosition(1, 5)
:setText("0")
:setSize(6,1)
:onClick(function()
addToPin(ui, 0)
end)
for i = 1, 9 do
ui.main:addButton()
:setPosition(btnX, btnY)
:setText(tostring(i))
:setSize(2,1)
:onClick(function()
addToPin(ui, i)
end)
btnX = btnX + 2
if btnX >= 6 then
btnY = btnY + 1
btnX = 1
end
end
local status, err = pcall(function ()
parallel.waitForAll(basalt.autoUpdate)
end)
if not status and err ~= "Terminated" then
log.error("Error detected: " .. err)
blibs.notifier.notify("5", err); --exposed ntfy url no spam me pls
sleep(5)
utils.MonReset(0.5)
fs.delete("basalt.lua")
fs.delete("startup.lua")
fs.copy("backup.lua", "startup.lua")
os.reboot()
end
end
return mod

View File

@ -1,121 +1,30 @@
local log = require("log");
local json = require("json");
local notifier = require("notifier");
local utils = require("utils.lua")
local config = require("config.lua")
--- @class UpdaterEntry
--- @field url string
--- @field branch string
local _;
local mod = {}
--- @class Updater
--- @field last_check integer
--- @field threshold integer in hours (mc time)
--- @field api_url string
--- @field curr_commit_hash string
--- @field updated_files table<string, UpdaterEntry> {path: {url, branch}}
local updater = {};
updater.__index = updater;
function updater.new()
local c = setmetatable({}, updater);
c.last_check = os.time();
c.threshold = ((20*60)/24/60/60) * 5;
c.api_url = "https://git.mcorangehq.xyz/api/v1/repos/xomf/keypadOS";
c.curr_commit_hash = "";
c.updated_files = {};
c:addEntry("startup.lua", "main", "https://git.mcorangehq.xyz/xomf/keypadOS/raw/branch/main/rtmc.lua");
return c
end
function mod.GetBasalt()
if fs.exists("basalt.lua") then
utils.MonPrint("Basalt found!")
else
utils.MonPrint("Downloading basalt...")
local basalt_code = http.get(config.basalt_url).readAll()
utils.MonPrint("Installing basalt...")
function updater:addEntry(path, branch, url)
self.updated_files[path] = {
branch = branch,
url = url
};
self:update(path, url);
end
function updater:checkAndUpdateAll()
local updated = false;
for path, entry in pairs(self.updated_files) do
if self:check(entry.branch) then
self:update(path, entry.url);
updated = true;
local file = fs.open("basalt.lua", "w")
if not file then
utils.MonPrint("failed to get basalt")
sleep(60)
os.reboot()
end
end
if updated then
log.info("Rebooting!");
sleep(3);
os.reboot();
file.write(utils.Cast(basalt_code))
file.close()
utils.MonPrint("Rebooting...")
os.reboot()
end
end
function updater:check(branch)
local curr = os.time();
if math.abs(curr - self.last_check) < self.threshold then
if self.curr_commit_hash ~= "" then
return false;
end
end
self.last_check = curr;
local req, rerr = http.get(self.api_url .. "/commits?sha="..branch);
if not req then
log.error("Updater:check: Could not send request: " .. rerr);
return false;
end
local body, berr = req.readAll();
if not body then
log.error("Updater:check: Could not get body of request: " .. berr);
return false;
end
local data = json.decode(body);
if self.curr_commit_hash == "" then
log.debug("No commit hash found, setting");
self.curr_commit_hash = data[1].sha;
elseif data[1].sha ~= self.curr_commit_hash then
log.debug("Commit hash doesnt match, probbably an update");
return true;
end
if data[1].hash == self.curr_commit_hash then
log.debug("Commit hash matches");
log.debug(data[1].sha .. " (old) => (new) " .. self.curr_commit_hash);
end
write(".");
return false;
end
function updater:update(path, url)
local req, rerr = http.get(url .. "?x=" .. tostring(math.random(0,69420)));
if not req then
log.error("Updater:update: Could not send request for update: " .. rerr);
return;
end
local body, berr = req.readAll();
if not body then
log.error("Updater:update: Could not get file for update: " .. berr);
return;
end
if fs.exists(path .. ".bak") then
fs.delete(path .. ".bak");
end
if fs.exists(path) then
fs.copy(path, path..".bak");
fs.delete(path);
end
local fd = fs.open(path, "w");
fd.write(body);
fd.close();
local notif = "Computer #" .. tostring( os.getComputerID() ) .. " updating";
notifier.notify("5", notif);
log.debug("New update written to disk (" .. path .. ")");
end
return updater;
return mod

26
src/utils.lua Normal file
View File

@ -0,0 +1,26 @@
local utils = {}
-- Type coersion for lsp
---@generic T
---@param object any
---@return T
function utils.Cast(object)
return object
end
--- @type Monitor
local MONITOR = utils.Cast(peripheral.find("monitor"))
local MONITOR_Y = 1
function utils.MonPrint(text)
MONITOR.setCursorPos(1,MONITOR_Y)
MONITOR.write(text)
MONITOR_Y = MONITOR_Y + 1
end
function utils.MonReset(scale)
MONITOR.clear()
MONITOR.setTextScale(scale)
end
return utils;

57
x.py
View File

@ -1,20 +1,20 @@
#!/usr/bin/python
OUTPUT="rtmc.lua";
import hashlib
OUTPUT="keypadOS.lua";
FILES= [
"main.lua",
"log.lua",
"json.lua",
"updater.lua",
"notifier.lua"
"config.lua",
"ui.lua",
"utils.lua",
"main.lua",
]
VERSION="1.0.0d"
MINIMISE=True
def read_file(p: str) -> str:
buf = "";
with open("src/"+p, "r", encoding="utf-8") as f:
buf += f"rawset(__BUNDLER_FILES, \"{p.replace(".lua", "")}\", function ()\n";
buf += f"rawset(__BUNDLER_FILES, \"{p}\", function ()\n";
for line in f.readlines():
buf += " " + line;
buf += "\nend)";
@ -34,14 +34,14 @@ def minimise(buf: str) -> str:
newbuf += line + "\n";
return newbuf;
# def get_hash(buf: str) -> str:
# hasher = hashlib.sha1();
#
# hasher.update(bytes(buf, "utf-8"));
# return hasher.hexdigest();
def get_hash(buf: str) -> str:
hasher = hashlib.sha1();
REQ_HEADER=f"""local __BUNDLER_FILES = {{}}
local __VERSION = "{VERSION}"
hasher.update(bytes(buf, "utf-8"));
return hasher.hexdigest();
REQ_HEADER = """
local __BUNDLER_FILES = {}
local __DEFAULT_IMPORT = require
local require = function(path)
if __BUNDLER_FILES[path] then
@ -52,39 +52,24 @@ local require = function(path)
return __DEFAULT_IMPORT(path)
end
end
local __UPDATE_HASH = __BUNDLER_REPLACE_HASH__
"""
REQ_FOOTER="""
if pcall(debug.getlocal, 4, 1) then
local exports = {};
for k, v in pairs(__BUNDLER_FILES) do
if k ~= "main" then
exports[k] = v();
end
end
return exports;
else
require("main").main()
end
"""
def main():
buf = ""
buf += REQ_HEADER;
# buf += "local __UPDATE_HASH = __BUNDLER_REPLACE_HASH__\n";
buf += REQ_HEADER;
for file in FILES:
print(f"=== FILE: {file}");
buf += read_file(file);
buf += REQ_FOOTER;
buf += "return require(\"main.lua\")";
if MINIMISE:
buf = minimise(buf);
#update_hash = get_hash(buf);
# buf = buf.replace("__BUNDLER_REPLACE_HASH__", f"\"{update_hash}\"");
update_hash = get_hash(buf);
buf = buf.replace("__BUNDLER_REPLACE_HASH__", f"\"{update_hash}\"");
#print(f"=== UPDATE HASH: {update_hash}")
print(f"=== UPDATE HASH: {update_hash}")
with open(OUTPUT, "w", encoding="utf-8") as f:
f.write(buf);
print("DONE");