This commit is contained in:
Gvidas Juknevičius 2024-08-25 22:35:33 +03:00
parent 5df9f33c2e
commit 4161160ab6
Signed by: MCorange
GPG Key ID: 12B1346D720B7FBB
4 changed files with 56 additions and 11 deletions

View File

@ -1,4 +1,5 @@
local __BUNDLER_FILES = {} local __BUNDLER_FILES = {}
local __VERSION = "1.0.0d"
local __DEFAULT_IMPORT = require local __DEFAULT_IMPORT = require
local require = function(path) local require = function(path)
if __BUNDLER_FILES[path] then if __BUNDLER_FILES[path] then
@ -9,10 +10,14 @@ local require = function(path)
return __DEFAULT_IMPORT(path) return __DEFAULT_IMPORT(path)
end end
end end
rawset(__BUNDLER_FILES, "main.lua", function () rawset(__BUNDLER_FILES, "main", function ()
local updater = require("updater"); local updater = require("updater");
local log = require("log");
local MODULE_NAME = "keypadOS"; local MODULE_NAME = "keypadOS";
local MODULE_URL = "https://git.mcorangehq.xyz/xomf/keypadOS/raw/branch/keypad/keypadOS.lua"; local MODULE_URL = "https://git.mcorangehq.xyz/xomf/keypadOS/raw/branch/keypad/keypadOS.lua";
if not __VERSION then
__VERSION = "DEV";
end
local function updaterLoop(upd) local function updaterLoop(upd)
while true do while true do
sleep(1) sleep(1)
@ -20,6 +25,7 @@ rawset(__BUNDLER_FILES, "main.lua", function ()
end end
end end
local function _start() local function _start()
log.info("Starting bootloader (" .. __VERSION .. ")");
local upd = updater.new(); local upd = updater.new();
upd:addEntry(MODULE_NAME .. ".lua", "keypad", MODULE_URL); upd:addEntry(MODULE_NAME .. ".lua", "keypad", MODULE_URL);
parallel.waitForAny(function() updaterLoop(upd) end, require(MODULE_NAME)) parallel.waitForAny(function() updaterLoop(upd) end, require(MODULE_NAME))
@ -28,7 +34,7 @@ rawset(__BUNDLER_FILES, "main.lua", function ()
mod.main = _start; mod.main = _start;
return mod; return mod;
end) end)
rawset(__BUNDLER_FILES, "log.lua", function () rawset(__BUNDLER_FILES, "log", function ()
local log = {}; local log = {};
log.__index = log; log.__index = log;
function log.error(...) function log.error(...)
@ -45,7 +51,7 @@ rawset(__BUNDLER_FILES, "log.lua", function ()
end end
return log; return log;
end) end)
rawset(__BUNDLER_FILES, "json.lua", function () rawset(__BUNDLER_FILES, "json", function ()
local json = { _version = "0.1.2" } local json = { _version = "0.1.2" }
local encode local encode
local escape_char_map = { local escape_char_map = {
@ -327,7 +333,7 @@ rawset(__BUNDLER_FILES, "json.lua", function ()
end end
return json return json
end) end)
rawset(__BUNDLER_FILES, "updater.lua", function () rawset(__BUNDLER_FILES, "updater", function ()
local log = require("log"); local log = require("log");
local json = require("json"); local json = require("json");
local _; local _;
@ -357,7 +363,7 @@ rawset(__BUNDLER_FILES, "updater.lua", function ()
for path, entry in pairs(self.updated_files) do for path, entry in pairs(self.updated_files) do
if self:check(entry.branch) then if self:check(entry.branch) then
self:update(path, entry.url); self:update(path, entry.url);
log.info("Updated!"); log.info("Rebooting!");
sleep(3); sleep(3);
os.reboot(); os.reboot();
end end
@ -366,6 +372,7 @@ rawset(__BUNDLER_FILES, "updater.lua", function ()
function updater:check(branch) function updater:check(branch)
local curr = os.time(); local curr = os.time();
if not ((math.abs(curr - self.last_check) >= self.threshold) and (self.curr_commit_hash ~= "")) then if not ((math.abs(curr - self.last_check) >= self.threshold) and (self.curr_commit_hash ~= "")) then
log.debug("Not time for an update yet");
return false; return false;
end end
self.last_check = curr; self.last_check = curr;
@ -381,8 +388,10 @@ rawset(__BUNDLER_FILES, "updater.lua", function ()
end end
local data = json.decode(body); local data = json.decode(body);
if self.curr_commit_hash == "" then if self.curr_commit_hash == "" then
log.debug("No commit hash found, setting");
self.curr_commit_hash = data[1].hash; self.curr_commit_hash = data[1].hash;
elseif data[1].hash == self.curr_commit_hash then elseif data[1].hash == self.curr_commit_hash then
log.debug("Commit hash doesnt match, probbably an update");
return true; return true;
end end
return false; return false;
@ -407,9 +416,20 @@ rawset(__BUNDLER_FILES, "updater.lua", function ()
local fd = fs.open(path, "w"); local fd = fs.open(path, "w");
fd.write(body); fd.write(body);
fd.close(); fd.close();
log.debug("New update written to disk (" .. path .. ")");
end end
function updater:notify() function updater:notify()
end end
return updater; return updater;
end) end)
require("main.lua").main() if pcall(debug.getlocal, 4, 1) then
local exports = {};
for k, v in pairs(__BUNDLER_FILES) do
if k ~= "main" then
exports[k] = v();
end
end
return exports;
else
require("main").main()
end

View File

@ -1,8 +1,13 @@
local updater = require("updater"); local updater = require("updater");
local log = require("log");
local MODULE_NAME = "keypadOS"; local MODULE_NAME = "keypadOS";
local MODULE_URL = "https://git.mcorangehq.xyz/xomf/keypadOS/raw/branch/keypad/keypadOS.lua"; local MODULE_URL = "https://git.mcorangehq.xyz/xomf/keypadOS/raw/branch/keypad/keypadOS.lua";
if not __VERSION then
__VERSION = "DEV";
end
--- @param upd Updater --- @param upd Updater
local function updaterLoop(upd) local function updaterLoop(upd)
while true do while true do
@ -12,6 +17,7 @@ local function updaterLoop(upd)
end end
local function _start() local function _start()
log.info("Starting bootloader (" .. __VERSION .. ")");
local upd = updater.new(); local upd = updater.new();
upd:addEntry(MODULE_NAME .. ".lua", "keypad", MODULE_URL); upd:addEntry(MODULE_NAME .. ".lua", "keypad", MODULE_URL);
parallel.waitForAny(function() updaterLoop(upd) end, require(MODULE_NAME)) parallel.waitForAny(function() updaterLoop(upd) end, require(MODULE_NAME))

View File

@ -41,7 +41,7 @@ function updater:checkAndUpdateAll()
for path, entry in pairs(self.updated_files) do for path, entry in pairs(self.updated_files) do
if self:check(entry.branch) then if self:check(entry.branch) then
self:update(path, entry.url); self:update(path, entry.url);
log.info("Updated!"); log.info("Rebooting!");
sleep(3); sleep(3);
os.reboot(); os.reboot();
end end
@ -51,6 +51,7 @@ end
function updater:check(branch) function updater:check(branch)
local curr = os.time(); local curr = os.time();
if not ((math.abs(curr - self.last_check) >= self.threshold) and (self.curr_commit_hash ~= "")) then if not ((math.abs(curr - self.last_check) >= self.threshold) and (self.curr_commit_hash ~= "")) then
log.debug("Not time for an update yet");
return false; return false;
end end
self.last_check = curr; self.last_check = curr;
@ -69,8 +70,10 @@ function updater:check(branch)
local data = json.decode(body); local data = json.decode(body);
if self.curr_commit_hash == "" then if self.curr_commit_hash == "" then
log.debug("No commit hash found, setting");
self.curr_commit_hash = data[1].hash; self.curr_commit_hash = data[1].hash;
elseif data[1].hash == self.curr_commit_hash then elseif data[1].hash == self.curr_commit_hash then
log.debug("Commit hash doesnt match, probbably an update");
return true; return true;
end end
@ -100,6 +103,7 @@ function updater:update(path, url)
local fd = fs.open(path, "w"); local fd = fs.open(path, "w");
fd.write(body); fd.write(body);
fd.close(); fd.close();
log.debug("New update written to disk (" .. path .. ")");
end end
function updater:notify() function updater:notify()

23
x.py
View File

@ -1,5 +1,4 @@
#!/usr/bin/python #!/usr/bin/python
import hashlib
OUTPUT="rtmc.lua"; OUTPUT="rtmc.lua";
FILES= [ FILES= [
"main.lua", "main.lua",
@ -8,12 +7,13 @@ FILES= [
"updater.lua" "updater.lua"
] ]
VERSION="1.0.0d"
MINIMISE=True MINIMISE=True
def read_file(p: str) -> str: def read_file(p: str) -> str:
buf = ""; buf = "";
with open("src/"+p, "r", encoding="utf-8") as f: with open("src/"+p, "r", encoding="utf-8") as f:
buf += f"rawset(__BUNDLER_FILES, \"{p}\", function ()\n"; buf += f"rawset(__BUNDLER_FILES, \"{p.replace(".lua", "")}\", function ()\n";
for line in f.readlines(): for line in f.readlines():
buf += " " + line; buf += " " + line;
buf += "\nend)"; buf += "\nend)";
@ -39,7 +39,8 @@ def minimise(buf: str) -> str:
# hasher.update(bytes(buf, "utf-8")); # hasher.update(bytes(buf, "utf-8"));
# return hasher.hexdigest(); # return hasher.hexdigest();
REQ_HEADER="""local __BUNDLER_FILES = {} REQ_HEADER=f"""local __BUNDLER_FILES = {{}}
local __VERSION = "{VERSION}"
local __DEFAULT_IMPORT = require local __DEFAULT_IMPORT = require
local require = function(path) local require = function(path)
if __BUNDLER_FILES[path] then if __BUNDLER_FILES[path] then
@ -52,6 +53,20 @@ local require = function(path)
end end
""" """
REQ_FOOTER="""
if pcall(debug.getlocal, 4, 1) then
local exports = {};
for k, v in pairs(__BUNDLER_FILES) do
if k ~= "main" then
exports[k] = v();
end
end
return exports;
else
require("main").main()
end
"""
def main(): def main():
buf = "" buf = ""
@ -61,7 +76,7 @@ def main():
print(f"=== FILE: {file}"); print(f"=== FILE: {file}");
buf += read_file(file); buf += read_file(file);
buf += "require(\"main.lua\").main()\n"; buf += REQ_FOOTER;
if MINIMISE: if MINIMISE:
buf = minimise(buf); buf = minimise(buf);