From 4161160ab62f16e989e158bd54e8ee411da6dd12 Mon Sep 17 00:00:00 2001 From: MCorange Date: Sun, 25 Aug 2024 22:35:33 +0300 Subject: [PATCH] :3 --- rtmc.lua | 32 ++++++++++++++++++++++++++------ src/main.lua | 6 ++++++ src/updater.lua | 6 +++++- x.py | 23 +++++++++++++++++++---- 4 files changed, 56 insertions(+), 11 deletions(-) diff --git a/rtmc.lua b/rtmc.lua index a881aff..d211138 100644 --- a/rtmc.lua +++ b/rtmc.lua @@ -1,4 +1,5 @@ local __BUNDLER_FILES = {} +local __VERSION = "1.0.0d" local __DEFAULT_IMPORT = require local require = function(path) if __BUNDLER_FILES[path] then @@ -9,10 +10,14 @@ local require = function(path) return __DEFAULT_IMPORT(path) end end -rawset(__BUNDLER_FILES, "main.lua", function () +rawset(__BUNDLER_FILES, "main", function () local updater = require("updater"); + local log = require("log"); local MODULE_NAME = "keypadOS"; 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) while true do sleep(1) @@ -20,6 +25,7 @@ rawset(__BUNDLER_FILES, "main.lua", function () end end local function _start() + log.info("Starting bootloader (" .. __VERSION .. ")"); local upd = updater.new(); upd:addEntry(MODULE_NAME .. ".lua", "keypad", MODULE_URL); parallel.waitForAny(function() updaterLoop(upd) end, require(MODULE_NAME)) @@ -28,7 +34,7 @@ rawset(__BUNDLER_FILES, "main.lua", function () mod.main = _start; return mod; end) -rawset(__BUNDLER_FILES, "log.lua", function () +rawset(__BUNDLER_FILES, "log", function () local log = {}; log.__index = log; function log.error(...) @@ -45,7 +51,7 @@ rawset(__BUNDLER_FILES, "log.lua", function () end return log; end) -rawset(__BUNDLER_FILES, "json.lua", function () +rawset(__BUNDLER_FILES, "json", function () local json = { _version = "0.1.2" } local encode local escape_char_map = { @@ -327,7 +333,7 @@ rawset(__BUNDLER_FILES, "json.lua", function () end return json end) -rawset(__BUNDLER_FILES, "updater.lua", function () +rawset(__BUNDLER_FILES, "updater", function () local log = require("log"); local json = require("json"); local _; @@ -357,7 +363,7 @@ rawset(__BUNDLER_FILES, "updater.lua", function () for path, entry in pairs(self.updated_files) do if self:check(entry.branch) then self:update(path, entry.url); - log.info("Updated!"); + log.info("Rebooting!"); sleep(3); os.reboot(); end @@ -366,6 +372,7 @@ rawset(__BUNDLER_FILES, "updater.lua", function () function updater:check(branch) local curr = os.time(); 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; end self.last_check = curr; @@ -381,8 +388,10 @@ rawset(__BUNDLER_FILES, "updater.lua", function () end local data = json.decode(body); if self.curr_commit_hash == "" then + log.debug("No commit hash found, setting"); self.curr_commit_hash = data[1].hash; elseif data[1].hash == self.curr_commit_hash then + log.debug("Commit hash doesnt match, probbably an update"); return true; end return false; @@ -407,9 +416,20 @@ rawset(__BUNDLER_FILES, "updater.lua", function () local fd = fs.open(path, "w"); fd.write(body); fd.close(); + log.debug("New update written to disk (" .. path .. ")"); end function updater:notify() end return updater; 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 diff --git a/src/main.lua b/src/main.lua index ba7d3a1..bd3d680 100644 --- a/src/main.lua +++ b/src/main.lua @@ -1,8 +1,13 @@ local updater = require("updater"); +local log = require("log"); local MODULE_NAME = "keypadOS"; local MODULE_URL = "https://git.mcorangehq.xyz/xomf/keypadOS/raw/branch/keypad/keypadOS.lua"; +if not __VERSION then + __VERSION = "DEV"; +end + --- @param upd Updater local function updaterLoop(upd) while true do @@ -12,6 +17,7 @@ local function updaterLoop(upd) end local function _start() + log.info("Starting bootloader (" .. __VERSION .. ")"); local upd = updater.new(); upd:addEntry(MODULE_NAME .. ".lua", "keypad", MODULE_URL); parallel.waitForAny(function() updaterLoop(upd) end, require(MODULE_NAME)) diff --git a/src/updater.lua b/src/updater.lua index da0bb8c..2bf2599 100644 --- a/src/updater.lua +++ b/src/updater.lua @@ -41,7 +41,7 @@ function updater:checkAndUpdateAll() for path, entry in pairs(self.updated_files) do if self:check(entry.branch) then self:update(path, entry.url); - log.info("Updated!"); + log.info("Rebooting!"); sleep(3); os.reboot(); end @@ -51,6 +51,7 @@ end function updater:check(branch) local curr = os.time(); 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; end self.last_check = curr; @@ -69,8 +70,10 @@ function updater:check(branch) local data = json.decode(body); if self.curr_commit_hash == "" then + log.debug("No commit hash found, setting"); self.curr_commit_hash = data[1].hash; elseif data[1].hash == self.curr_commit_hash then + log.debug("Commit hash doesnt match, probbably an update"); return true; end @@ -100,6 +103,7 @@ function updater:update(path, url) local fd = fs.open(path, "w"); fd.write(body); fd.close(); + log.debug("New update written to disk (" .. path .. ")"); end function updater:notify() diff --git a/x.py b/x.py index 91cf8b0..92c4e20 100755 --- a/x.py +++ b/x.py @@ -1,5 +1,4 @@ #!/usr/bin/python -import hashlib OUTPUT="rtmc.lua"; FILES= [ "main.lua", @@ -8,12 +7,13 @@ FILES= [ "updater.lua" ] +VERSION="1.0.0d" MINIMISE=True def read_file(p: str) -> str: buf = ""; 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(): buf += " " + line; buf += "\nend)"; @@ -39,7 +39,8 @@ def minimise(buf: str) -> str: # hasher.update(bytes(buf, "utf-8")); # return hasher.hexdigest(); -REQ_HEADER="""local __BUNDLER_FILES = {} +REQ_HEADER=f"""local __BUNDLER_FILES = {{}} +local __VERSION = "{VERSION}" local __DEFAULT_IMPORT = require local require = function(path) if __BUNDLER_FILES[path] then @@ -52,6 +53,20 @@ local require = function(path) 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(): buf = "" @@ -61,7 +76,7 @@ def main(): print(f"=== FILE: {file}"); buf += read_file(file); - buf += "require(\"main.lua\").main()\n"; + buf += REQ_FOOTER; if MINIMISE: buf = minimise(buf);