Files
nova-corp/kernel.lua
2026-05-05 17:57:45 -04:00

125 lines
3.8 KiB
Lua

local BASE_URL = "https://git.astrocore.space/root/nova-corp/raw/branch/main/"
-- Download <url> to <destFile>, replacing any existing file.
local function download(url, destFile)
if fs.exists(destFile) then
shell.execute("rm", destFile)
end
shell.execute("wget", url, destFile)
sleep(1)
end
local function addDriver(fileName)
local fullFile = fileName .. ".lua"
download(BASE_URL .. fullFile, fullFile)
return require(fileName)
end
local function addFolderDriver(folder, fileName)
local fullFile = fileName .. ".lua"
download(BASE_URL .. folder .. "/" .. fullFile, fullFile)
return require(fileName)
end
local function addProgram(fileName)
local fullFile = fileName .. ".lua"
download(BASE_URL .. "programs/" .. fullFile, fullFile)
return require(fileName)
end
local function addSound(fileName)
local fullFile = fileName .. ".dfpwm"
download(BASE_URL .. "sounds/" .. fullFile, fullFile)
end
local function addServerHandler(fileName)
local fullFile = fileName .. ".lua"
download(BASE_URL .. "server/" .. fullFile, fullFile)
return require(fileName)
end
-- installInit downloads programs/init.lua and writes it as startup.lua
-- so that on next boot the user is prompted for which program to install.
local function installInit()
download(BASE_URL .. "programs/init.lua", "startup.lua")
print("init installed as startup.lua. Reboot to choose a program.")
end
-- addStartup("roulette") downloads programs/roulette.lua AND writes a
-- startup.lua that re-pulls the kernel + program on every boot and runs it.
local function addStartup(programName)
-- Pull the program once now so it's runnable immediately.
addProgram(programName)
local startupSrc = ([[-- Auto-generated by kernel.addStartup("%s")
local BASE_URL = "%s"
local function fetch(path, dest)
if fs.exists(dest) then shell.execute("rm", dest) end
shell.execute("wget", BASE_URL .. path, dest)
sleep(1)
end
-- Refresh kernel and program on every boot so updates flow through.
fetch("kernel.lua", "kernel.lua")
local kernel = require("kernel")
local program = kernel.addProgram("%s")
if program and program.start then program.start() end
if program and program.main then
program.main()
else
print("Program '%s' has no main() function.")
end
]]):format(programName, BASE_URL, programName, programName)
if fs.exists("startup.lua") then shell.execute("rm", "startup.lua") end
local f = fs.open("startup.lua", "w")
f.write(startupSrc)
f.close()
print("Startup installed for '" .. programName .. "'. Reboot to launch.")
end
addDriver("task-manager")
addDriver("monitor-driver")
addDriver("disk-driver")
addDriver("speaker-driver")
addSound("notification")
-- Always (re)install init as startup.lua when the kernel is freshly fetched.
-- The init program will overwrite startup.lua with the chosen program's
-- launcher the first time the user picks one.
--
-- Gate: only run when executed directly from the shell (e.g. right after
-- `wget kernel.lua` then `kernel`). When required from a startup script
-- (`require("kernel")`) we skip this so we don't clobber the program's
-- startup.lua on every boot.
if not _G.__KERNEL_LOADED then
_G.__KERNEL_LOADED = true
if shell and shell.getRunningProgram and shell.getRunningProgram() == "kernel.lua" then
installInit()
end
end
local kernel = {
addDriver = addDriver,
addFolderDriver = addFolderDriver,
addProgram = addProgram,
addSound = addSound,
addServerHandler = addServerHandler,
addStartup = addStartup,
installInit = installInit,
}
-- Expose helpers as globals so they can be called directly from the shell:
-- > addStartup("roulette")
_G.addStartup = addStartup
_G.addProgram = addProgram
_G.addDriver = addDriver
_G.kernel = kernel
return kernel