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 @@
-- 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
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()
: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)
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
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,
}

30
x.py
View File

@ -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");