This commit is contained in:
Gvidas Juknevičius 2024-08-18 03:34:13 +03:00
parent 6f61e9549b
commit c32838af39
Signed by: MCorange
GPG Key ID: 12B1346D720B7FBB
12 changed files with 485 additions and 315 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/cctweaked

11
.luarc.json Normal file
View File

@ -0,0 +1,11 @@
{
"runtime.version": "LuaJIT",
"runtime.path": [
"src/?.lua",
],
"diagnostics.globals": ["vim"],
"workspace.checkThirdParty": false,
"workspace.library": [
"cctweaked"
]
}

View File

@ -1,182 +1,234 @@
local __BUNDLER_FILES = {}
-- FILE: src/boot.lua local __DEFAULT_IMPORT = require
local require = function(path)
-- keycardOS "bootloader", has no access to basalt return __BUNDLER_FILES[path]() or __DEFAULT_IMPORT(path)
-- 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 end
function updateChecker() __BUNDLER_FILES["updater.lua"] = function ()
while true do local utils = require("utils.lua")
checkForUpdate() local LAST_USED = os.time()
sleep(1) local function checkForUpdate()
end local current_time = os.time()
end local difference = math.abs(current_time - LAST_USED)
print("Checking for update... difference: " .. tostring(difference))
local function getBasalt() --its been considerable time since the keypad was interacted with
if fs.exists("basalt.lua") then --therefore it's time to force an update down the users throat (microsoft moment)
mPrint("Basalt found!") if difference > .5 then
else local update_code_request = http.get("https://git.mcorangehq.xyz/xomf/keypadOS/raw/branch/main/keypadOS.lua")
mPrint("Download basalt...") if update_code_request then
basalt_code = http.get("https://raw.githubusercontent.com/Pyroxenium/Basalt/master/docs/versions/latest.lua").readAll() local update_code_text = update_code_request.readAll()
mPrint("Install basalt...") if update_code_text then
local file = fs.open("basalt.lua", "w") if string.find(update_code_text, "^local __BUNDLER_FILES = {}") then
file.write(basalt_code) --make backup
file.close() fs.copy("startup.lua","backup.lua")
mPrint("Rebooting...") --install update
os.reboot() local file = fs.open("startup.lua", "w")
end file.write(update_code_text)
end file.close()
os.reboot()
KEYPADOS_VERSION = "3.0" end
MONITOR = peripheral.find("monitor") end
end
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()
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") end -- FILE END: updater.lua --
if pin == correctPin then
enterButton:setBackground(colors.green)
pinLabel:setText("Welcome")
redstone.setOutput("front", true)
__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.isDiskPresent() then
if drive.getDiskLabel() == nil then if drive.getDiskLabel() == config.correctPin then
drive.setDiskLabel(correctPin) ui.pin = config.correctPin
pinLabel:setText("Crd set")
drive:ejectDisk() drive:ejectDisk()
end end
end end
basalt.debug("test")
else if ui.pin == config.correctPin then
pinLabel:setText("Wrong") ui.enterButton:setBackground(colors.green)
enterButton:setBackground(colors.red) 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 end
local thread = main:addThread() --- @param ui Ui
:start(resetEverything) local function resetEverything(ui)
sleep(2)
end ui.pin = ""
ui.pinLabel:setText("")
function resetEverything() redstone.setOutput("front", false)
sleep(2) ui.enterButton:setBackground(colors.blue)
pin = ""
pinLabel:setText("")
redstone.setOutput("front", false)
enterButton:setBackground(colors.blue)
end
function addToPin(i)
if #pin >= 5 then
return
end end
--- @param ui Ui
pin = pin .. tostring(i) local function addToPin(ui, i)
pinLabel:setText(pin) if #ui.pin >= 5 then
end return
-- FILE: src/utils.lua end
ui.pin = ui.pin .. tostring(i)
MONITOR_Y = 1 ui.pinLabel:setText(ui.pin)
function mPrint(text) end
MONITOR.setCursorPos(1,y) function InitUi()
MONITOR.write(text) local ui = {
MONITOR_Y = MONITOR_Y + 1 resetEverything,
end unlockDoor,
addToPin,
function mon_reset(scale) main = basalt.addMonitor(monitor),
MONITOR.clear() }
MONITOR.setTextScale(scale) ui.main:setMonitor(monitor)
end -- local pinLabel =
ui.pinLabel = ui.main:addLabel()
-- FILE: src/main.lua :setText("")
:setFontSize(1)
pinLabel = main:addLabel() -- local enterButton =
:setText("") ui.enterButton = ui.main:addButton()
:setFontSize(1) :setText(">>>>")
:setBackground(colors.blue)
enterButton = main:addButton() :setPosition(6,3)
:setText(">>>>") :setSize(1.5,3.2)
:setBackground(colors.blue) :onClick(function()
:setPosition(6,3) unlockDoor(ui)
: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) end)
btnX = btnX + 2 local btnX = 1
local btnY = 3
if btnX >= 6 then for i = 1, 9 do
btnY = btnY + 1 ui.main:addButton()
btnX = 1 :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,
}
end end -- FILE END: ui.lua --
parallel.waitForAll(basalt.autoUpdate, updateChecker) __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()

View File

@ -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()

View File

@ -1 +1,3 @@
correctPin = "42169" return {
correctPin = "42169"
}

View File

@ -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 = ""

View File

@ -1,30 +1,23 @@
pinLabel = main:addLabel() -- keycardOS "bootloader", has no access to basalt
:setText("") -- intended for checking for updates, and automatically updating basalt if it is missing
:setFontSize(1) 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 KEYPADOS_VERSION = "3.0"
btnY = btnY + 1
btnX = 1
end
function Main()
utils.MonPrint(0.5)
updater.getBasalt()
utils.MonPrint("keypadOS v" .. KEYPADOS_VERSION)
utils.MonPrint(1)
ui.InitUi()
end end
parallel.waitForAll(basalt.autoUpdate, updateChecker)
return {
Main
}

126
src/ui.lua Normal file
View File

@ -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,
}

View File

@ -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

64
src/updater.lua Normal file
View File

@ -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
}

View File

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

30
x.py
View File

@ -2,26 +2,38 @@
OUTPUT="keypadOS.lua"; OUTPUT="keypadOS.lua";
FILES= [ FILES= [
"src/boot.lua", "updater.lua",
"src/init.lua", "config.lua",
"src/config.lua", "ui.lua",
"src/unlock_door.lua", "utils.lua",
"src/utils.lua", "main.lua",
"src/main.lua",
] ]
def read_file(p: str) -> str: def read_file(p: str) -> str:
buf = ""; buf = "";
with open(p, "r", encoding="utf-8") as f: with open("src/"+p, "r", encoding="utf-8") as f:
buf = f"\n-- FILE: {p}\n\n" + f.read(); 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; return buf;
def main(): 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: for file in FILES:
print(f"=== FILE: {file}"); print(f"=== FILE: {file}");
buf += read_file(file); buf += read_file(file);
buf += "\nrequire(\"main.lua\").Main()\n";
with open(OUTPUT, "w", encoding="utf-8") as f: with open(OUTPUT, "w", encoding="utf-8") as f:
f.write(buf); f.write(buf);
print("DONE"); print("DONE");