From c32838af3910bc67f5e310c272e1db44c7b9e8d2 Mon Sep 17 00:00:00 2001 From: MCorange Date: Sun, 18 Aug 2024 03:34:13 +0300 Subject: [PATCH] nyah :3 --- .gitignore | 1 + .luarc.json | 11 ++ keypadOS.lua | 388 +++++++++++++++++++++++++------------------- src/boot.lua | 50 ------ src/config.lua | 4 +- src/init.lua | 8 - src/main.lua | 45 +++-- src/ui.lua | 126 ++++++++++++++ src/unlock_door.lua | 49 ------ src/updater.lua | 64 ++++++++ src/utils.lua | 24 ++- x.py | 30 +++- 12 files changed, 485 insertions(+), 315 deletions(-) create mode 100644 .gitignore create mode 100644 .luarc.json delete mode 100644 src/boot.lua delete mode 100644 src/init.lua create mode 100644 src/ui.lua delete mode 100644 src/unlock_door.lua create mode 100644 src/updater.lua diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d7dd68c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/cctweaked diff --git a/.luarc.json b/.luarc.json new file mode 100644 index 0000000..f4a002e --- /dev/null +++ b/.luarc.json @@ -0,0 +1,11 @@ +{ + "runtime.version": "LuaJIT", + "runtime.path": [ + "src/?.lua", + ], + "diagnostics.globals": ["vim"], + "workspace.checkThirdParty": false, + "workspace.library": [ + "cctweaked" + ] +} diff --git a/keypadOS.lua b/keypadOS.lua index dc14c8d..5383d3a 100644 --- a/keypadOS.lua +++ b/keypadOS.lua @@ -1,182 +1,234 @@ - --- FILE: src/boot.lua - --- keycardOS "bootloader", has no access to basalt --- intended for checking for updates, and automatically updating basalt if it is missing - -LAST_USED = os.time() - -local function checkForUpdate() - current_time = os.time() - difference = math.abs(current_time - LAST_USED) - print("Checking for update... difference: " .. tostring(difference)) - --its been considerable time since the keypad was interacted with - --therefore it's time to force an update down the users throat (microsoft moment) - -- if difference > 4 then - if difference > .5 then - --logic for updating - end - +local __BUNDLER_FILES = {} +local __DEFAULT_IMPORT = require +local require = function(path) + return __BUNDLER_FILES[path]() or __DEFAULT_IMPORT(path) end -function updateChecker() - while true do - checkForUpdate() - sleep(1) - end -end - -local function getBasalt() - if fs.exists("basalt.lua") then - mPrint("Basalt found!") - else - mPrint("Download basalt...") - basalt_code = http.get("https://raw.githubusercontent.com/Pyroxenium/Basalt/master/docs/versions/latest.lua").readAll() - mPrint("Install basalt...") - local file = fs.open("basalt.lua", "w") - file.write(basalt_code) - file.close() - mPrint("Rebooting...") - os.reboot() - end -end - -KEYPADOS_VERSION = "3.0" -MONITOR = peripheral.find("monitor") - -local function boot() - mon_reset(0.5); - mPrint("keypadOS v" .. KEYPADOS_VERSION) - mon_reset(1); -end - -boot() - -local files = {} -local globalRequire = require -- Store default require reference -local require = function(path) -- Will return saved file or attempt default lua require - return files[path] or globalRequire(path) -end - -files["boot.lua"] = function(...) - local function boot() - mon_reset(0.5); - mPrint("keypadOS v" .. KEYPADOS_VERSION) - mon_reset(1); - end - - return { - boot - } -end - --- FILE: src/init.lua - -local basalt = require("basalt") -local monitor = peripheral.find("monitor") -local drive = peripheral.find("drive") -local main = basalt.addMonitor() -main:setMonitor(monitor) -btnX = 1 -btnY = 3 -pin = "" --- FILE: src/config.lua - -correctPin = "42169" --- FILE: src/unlock_door.lua - -function unlockDoor() - - if drive.isDiskPresent() then - if drive.getDiskLabel() == correctPin then - pin = correctPin - drive:ejectDisk() +__BUNDLER_FILES["updater.lua"] = function () + local utils = require("utils.lua") + local LAST_USED = os.time() + local function checkForUpdate() + local current_time = os.time() + local difference = math.abs(current_time - LAST_USED) + print("Checking for update... difference: " .. tostring(difference)) + --its been considerable time since the keypad was interacted with + --therefore it's time to force an update down the users throat (microsoft moment) + if difference > .5 then + local update_code_request = http.get("https://git.mcorangehq.xyz/xomf/keypadOS/raw/branch/main/keypadOS.lua") + if update_code_request then + local update_code_text = update_code_request.readAll() + if update_code_text then + if string.find(update_code_text, "^local __BUNDLER_FILES = {}") then + --make backup + fs.copy("startup.lua","backup.lua") + --install update + local file = fs.open("startup.lua", "w") + file.write(update_code_text) + file.close() + os.reboot() + end + end + end end end + function UpdateChecker() + while true do + checkForUpdate() + sleep(1) + end + end + function GetBasalt() + if fs.exists("basalt.lua") then + utils.MonPrint("Basalt found!") + else + utils.MonPrint("Downloading basalt...") + local basalt_code = http.get("https://raw.githubusercontent.com/Pyroxenium/Basalt/master/docs/versions/latest.lua").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(Cast(basalt_code)) + file.close() + utils.MonPrint("Rebooting...") + os.reboot() + end + end + return { + UpdateChecker, + GetBasalt + } - basalt.debug("test") - if pin == correctPin then - enterButton:setBackground(colors.green) - pinLabel:setText("Welcome") - redstone.setOutput("front", true) - +end -- FILE END: updater.lua -- + +__BUNDLER_FILES["config.lua"] = function () + return { + correctPin = "42169" + } + +end -- FILE END: config.lua -- + +__BUNDLER_FILES["ui.lua"] = function () + local basalt = require("basalt") + local config = require("config.lua") + local updater = require("updater.lua") + --- @type Monitor + local monitor = Cast(peripheral.find("monitor")) + --- @type drive + local drive = Cast(peripheral.find("drive")) + --- @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 unlockDoor(ui) if drive.isDiskPresent() then - if drive.getDiskLabel() == nil then - drive.setDiskLabel(correctPin) - pinLabel:setText("Crd set") + if drive.getDiskLabel() == config.correctPin then + ui.pin = config.correctPin drive:ejectDisk() end end - - else - pinLabel:setText("Wrong") - enterButton:setBackground(colors.red) + basalt.debug("test") + if ui.pin == config.correctPin then + 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(ui.resetEverything) end - local thread = main:addThread() - :start(resetEverything) - -end - -function resetEverything() - sleep(2) - pin = "" - pinLabel:setText("") - redstone.setOutput("front", false) - enterButton:setBackground(colors.blue) -end - -function addToPin(i) - - if #pin >= 5 then - return + --- @param ui Ui + local function resetEverything(ui) + sleep(2) + ui.pin = "" + ui.pinLabel:setText("") + redstone.setOutput("front", false) + ui.enterButton:setBackground(colors.blue) end - - pin = pin .. tostring(i) - pinLabel:setText(pin) -end --- FILE: src/utils.lua - -MONITOR_Y = 1 -function mPrint(text) - MONITOR.setCursorPos(1,y) - MONITOR.write(text) - MONITOR_Y = MONITOR_Y + 1 -end - -function mon_reset(scale) - MONITOR.clear() - MONITOR.setTextScale(scale) -end - --- FILE: src/main.lua - -pinLabel = main:addLabel() - :setText("") - :setFontSize(1) - -enterButton = main:addButton() - :setText(">>>>") - :setBackground(colors.blue) - :setPosition(6,3) - :setSize(1.5,3.2) - :onClick(unlockDoor) - -for i = 1, 9 do - local button = main:addButton() - :setPosition(btnX,btnY) - :setText(tostring(i)) - :setSize(2,1) - :onClick( - function() - addToPin(i) + --- @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 InitUi() + local ui = { + resetEverything, + unlockDoor, + addToPin, + main = basalt.addMonitor(monitor), + } + ui.main:setMonitor(monitor) + -- local pinLabel = + ui.pinLabel = ui.main:addLabel() + :setText("") + :setFontSize(1) + -- local enterButton = + ui.enterButton = ui.main:addButton() + :setText(">>>>") + :setBackground(colors.blue) + :setPosition(6,3) + :setSize(1.5,3.2) + :onClick(function() + unlockDoor(ui) end) - btnX = btnX + 2 - - if btnX >= 6 then - btnY = btnY + 1 - btnX = 1 + local btnX = 1 + local btnY = 3 + 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, _ = pcall(function () + parallel.waitForAll(basalt.autoUpdate, updater.updateChecker) + end) + if not status then + monitor.clear() + fs.delete("basalt.lua") + fs.delete("startup.lua") + fs.copy("backup.lua", "startup.lua") + os.reboot() + end end - -end + return { + InitUi, + } -parallel.waitForAll(basalt.autoUpdate, updateChecker) \ No newline at end of file +end -- FILE END: ui.lua -- + +__BUNDLER_FILES["utils.lua"] = function () + --- @type Monitor + local MONITOR = Cast(peripheral.find("monitor")) + local MONITOR_Y = 1 + function MonPrint(text) + MONITOR.setCursorPos(1,MONITOR_Y) + MONITOR.write(text) + MONITOR_Y = MONITOR_Y + 1 + end + function MonReset(scale) + MONITOR.clear() + MONITOR.setTextScale(scale) + end + -- Type coersion for lsp + ---@generic T + ---@param object any + ---@return T + function Cast(object) + return object + end + return { + MonPrint, + MonReset, + Cast, + } + +end -- FILE END: utils.lua -- + +__BUNDLER_FILES["main.lua"] = function () + -- keycardOS "bootloader", has no access to basalt + -- intended for checking for updates, and automatically updating basalt if it is missing + local utils = require("utils.lua") + local updater = require("updater.lua") + local ui = require("ui.lua") + KEYPADOS_VERSION = "3.0" + function Main() + utils.MonPrint(0.5) + updater.getBasalt() + utils.MonPrint("keypadOS v" .. KEYPADOS_VERSION) + utils.MonPrint(1) + ui.InitUi() + end + return { + Main + } + +end -- FILE END: main.lua -- + +require("main.lua").Main() diff --git a/src/boot.lua b/src/boot.lua deleted file mode 100644 index da7a9ca..0000000 --- a/src/boot.lua +++ /dev/null @@ -1,50 +0,0 @@ --- keycardOS "bootloader", has no access to basalt --- intended for checking for updates, and automatically updating basalt if it is missing - -LAST_USED = os.time() - -local function checkForUpdate() - current_time = os.time() - difference = math.abs(current_time - LAST_USED) - print("Checking for update... difference: " .. tostring(difference)) - --its been considerable time since the keypad was interacted with - --therefore it's time to force an update down the users throat (microsoft moment) - -- if difference > 4 then - if difference > .5 then - --logic for updating - end - -end - -function updateChecker() - while true do - checkForUpdate() - sleep(1) - end -end - -local function getBasalt() - if fs.exists("basalt.lua") then - mPrint("Basalt found!") - else - mPrint("Download basalt...") - basalt_code = http.get("https://raw.githubusercontent.com/Pyroxenium/Basalt/master/docs/versions/latest.lua").readAll() - mPrint("Install basalt...") - local file = fs.open("basalt.lua", "w") - file.write(basalt_code) - file.close() - mPrint("Rebooting...") - os.reboot() - end -end - -KEYPADOS_VERSION = "3.0" -MONITOR = peripheral.find("monitor") - -local function boot() - mon_reset(0.5); - mPrint("keypadOS v" .. KEYPADOS_VERSION) - mon_reset(1); -end - -boot() diff --git a/src/config.lua b/src/config.lua index 78fd0cc..40cdf55 100644 --- a/src/config.lua +++ b/src/config.lua @@ -1 +1,3 @@ -correctPin = "42169" \ No newline at end of file +return { + correctPin = "42169" +} diff --git a/src/init.lua b/src/init.lua deleted file mode 100644 index 78023a2..0000000 --- a/src/init.lua +++ /dev/null @@ -1,8 +0,0 @@ -local basalt = require("basalt") -local monitor = peripheral.find("monitor") -local drive = peripheral.find("drive") -local main = basalt.addMonitor() -main:setMonitor(monitor) -btnX = 1 -btnY = 3 -pin = "" \ No newline at end of file diff --git a/src/main.lua b/src/main.lua index 42ca3f5..116e8cd 100644 --- a/src/main.lua +++ b/src/main.lua @@ -1,30 +1,23 @@ -pinLabel = main:addLabel() - :setText("") - :setFontSize(1) +-- keycardOS "bootloader", has no access to basalt +-- intended for checking for updates, and automatically updating basalt if it is missing +local utils = require("utils.lua") +local updater = require("updater.lua") +local ui = require("ui.lua") -enterButton = main:addButton() - :setText(">>>>") - :setBackground(colors.blue) - :setPosition(6,3) - :setSize(1.5,3.2) - :onClick(unlockDoor) -for i = 1, 9 do - local button = main:addButton() - :setPosition(btnX,btnY) - :setText(tostring(i)) - :setSize(2,1) - :onClick( - function() - addToPin(i) - end) - btnX = btnX + 2 - - if btnX >= 6 then - btnY = btnY + 1 - btnX = 1 - end - + +KEYPADOS_VERSION = "3.0" + +function Main() + utils.MonPrint(0.5) + updater.getBasalt() + utils.MonPrint("keypadOS v" .. KEYPADOS_VERSION) + utils.MonPrint(1) + ui.InitUi() end -parallel.waitForAll(basalt.autoUpdate, updateChecker) \ No newline at end of file + +return { + Main +} + diff --git a/src/ui.lua b/src/ui.lua new file mode 100644 index 0000000..99cfcfb --- /dev/null +++ b/src/ui.lua @@ -0,0 +1,126 @@ +local basalt = require("basalt") +local config = require("config.lua") +local updater = require("updater.lua") +--- @type Monitor +local monitor = Cast(peripheral.find("monitor")) +--- @type drive +local drive = Cast(peripheral.find("drive")) + +--- @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 unlockDoor(ui) + if drive.isDiskPresent() then + if drive.getDiskLabel() == config.correctPin then + ui.pin = config.correctPin + drive:ejectDisk() + end + end + + basalt.debug("test") + if ui.pin == config.correctPin then + 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(ui.resetEverything) +end + +--- @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 addToPin(ui, i) + + if #ui.pin >= 5 then + return + end + + ui.pin = ui.pin .. tostring(i) + ui.pinLabel:setText(ui.pin) +end + +function InitUi() + local ui = { + resetEverything, + unlockDoor, + addToPin, + main = basalt.addMonitor(monitor), + } + + ui.main:setMonitor(monitor) + -- local pinLabel = + ui.pinLabel = ui.main:addLabel() + :setText("") + :setFontSize(1) + + -- local enterButton = + ui.enterButton = ui.main:addButton() + :setText(">>>>") + :setBackground(colors.blue) + :setPosition(6,3) + :setSize(1.5,3.2) + :onClick(function() + unlockDoor(ui) + end) + + local btnX = 1 + local btnY = 3 + + 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, _ = pcall(function () + parallel.waitForAll(basalt.autoUpdate, updater.updateChecker) + end) + + if not status then + monitor.clear() + fs.delete("basalt.lua") + fs.delete("startup.lua") + fs.copy("backup.lua", "startup.lua") + os.reboot() + end +end + +return { + InitUi, +} diff --git a/src/unlock_door.lua b/src/unlock_door.lua deleted file mode 100644 index acb6839..0000000 --- a/src/unlock_door.lua +++ /dev/null @@ -1,49 +0,0 @@ -function unlockDoor() - - if drive.isDiskPresent() then - if drive.getDiskLabel() == correctPin then - pin = correctPin - drive:ejectDisk() - end - end - - basalt.debug("test") - if pin == correctPin then - enterButton:setBackground(colors.green) - pinLabel:setText("Welcome") - redstone.setOutput("front", true) - - if drive.isDiskPresent() then - if drive.getDiskLabel() == nil then - drive.setDiskLabel(correctPin) - pinLabel:setText("Crd set") - drive:ejectDisk() - end - end - - else - pinLabel:setText("Wrong") - enterButton:setBackground(colors.red) - end - local thread = main:addThread() - :start(resetEverything) - -end - -function resetEverything() - sleep(2) - pin = "" - pinLabel:setText("") - redstone.setOutput("front", false) - enterButton:setBackground(colors.blue) -end - -function addToPin(i) - - if #pin >= 5 then - return - end - - pin = pin .. tostring(i) - pinLabel:setText(pin) -end \ No newline at end of file diff --git a/src/updater.lua b/src/updater.lua new file mode 100644 index 0000000..27a4ecd --- /dev/null +++ b/src/updater.lua @@ -0,0 +1,64 @@ +local utils = require("utils.lua") + +local LAST_USED = os.time() + +local function checkForUpdate() + local current_time = os.time() + local difference = math.abs(current_time - LAST_USED) + print("Checking for update... difference: " .. tostring(difference)) + --its been considerable time since the keypad was interacted with + --therefore it's time to force an update down the users throat (microsoft moment) + if difference > .5 then + local update_code_request = http.get("https://git.mcorangehq.xyz/xomf/keypadOS/raw/branch/main/keypadOS.lua") + if update_code_request then + local update_code_text = update_code_request.readAll() + if update_code_text then + if string.find(update_code_text, "^local __BUNDLER_FILES = {}") then + --make backup + fs.copy("startup.lua","backup.lua") + + --install update + local file = fs.open("startup.lua", "w") + file.write(update_code_text) + file.close() + os.reboot() + end + end + end + end +end + +function UpdateChecker() + while true do + checkForUpdate() + sleep(1) + end +end + +function GetBasalt() + if fs.exists("basalt.lua") then + utils.MonPrint("Basalt found!") + else + utils.MonPrint("Downloading basalt...") + local basalt_code = http.get("https://raw.githubusercontent.com/Pyroxenium/Basalt/master/docs/versions/latest.lua").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(Cast(basalt_code)) + file.close() + + utils.MonPrint("Rebooting...") + os.reboot() + end +end + +return { + UpdateChecker, + GetBasalt +} diff --git a/src/utils.lua b/src/utils.lua index f622f1e..6f26832 100644 --- a/src/utils.lua +++ b/src/utils.lua @@ -1,11 +1,27 @@ -MONITOR_Y = 1 -function mPrint(text) - MONITOR.setCursorPos(1,y) +--- @type Monitor +local MONITOR = Cast(peripheral.find("monitor")) +local MONITOR_Y = 1 +function MonPrint(text) + MONITOR.setCursorPos(1,MONITOR_Y) MONITOR.write(text) MONITOR_Y = MONITOR_Y + 1 end -function mon_reset(scale) +function MonReset(scale) MONITOR.clear() MONITOR.setTextScale(scale) end + +-- Type coersion for lsp +---@generic T +---@param object any +---@return T +function Cast(object) + return object +end + +return { + MonPrint, + MonReset, + Cast, +} diff --git a/x.py b/x.py index b2baa9e..860a740 100755 --- a/x.py +++ b/x.py @@ -2,26 +2,38 @@ OUTPUT="keypadOS.lua"; FILES= [ - "src/boot.lua", - "src/init.lua", - "src/config.lua", - "src/unlock_door.lua", - "src/utils.lua", - "src/main.lua", + "updater.lua", + "config.lua", + "ui.lua", + "utils.lua", + "main.lua", ] def read_file(p: str) -> str: buf = ""; - with open(p, "r", encoding="utf-8") as f: - buf = f"\n-- FILE: {p}\n\n" + f.read(); + with open("src/"+p, "r", encoding="utf-8") as f: + buf += f"\n__BUNDLER_FILES[\"{p}\"] = function ()\n"; + for line in f.readlines(): + if str.strip(line) != "": + buf += " " + line; + buf += f"\nend -- FILE END: {p} --\n"; return buf; def main(): - buf = ""; + buf = "" + buf += "local __BUNDLER_FILES = {}\n"; + buf += "local __DEFAULT_IMPORT = require\n"; + buf += "local require = function(path)\n"; + buf += " return __BUNDLER_FILES[path]() or __DEFAULT_IMPORT(path)\n"; + buf += "end\n"; + for file in FILES: print(f"=== FILE: {file}"); buf += read_file(file); + + buf += "\nrequire(\"main.lua\").Main()\n"; + with open(OUTPUT, "w", encoding="utf-8") as f: f.write(buf); print("DONE");