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