From bfe8bddf37cc8c1d6f01fd337a5e0f65645f3f25 Mon Sep 17 00:00:00 2001 From: CadenCoaster Date: Thu, 23 Oct 2025 21:26:17 -0700 Subject: [PATCH] small changes \j --- installer.lua | 17 ++++ libs/compat.lua | 4 +- libs/window.lua | 8 +- modules/netmount.lua | 203 +++++++++++++++++++++++++++++++++++++++++++ startup.lua | 55 +++++++++++- 5 files changed, 279 insertions(+), 8 deletions(-) create mode 100644 modules/netmount.lua diff --git a/installer.lua b/installer.lua index 994279a..11c1810 100644 --- a/installer.lua +++ b/installer.lua @@ -13,6 +13,19 @@ local function download_to(url,path) print(" OK") end +write("Do you want to setup netmount?\n(Y/n)") +local netmount = read():sub(1,1):lower() == "n" + +if netmount then + write("URL: ") + settings.set("netmount.url", read()) + write("Username: ") + settings.set("netmount.username", read()) + write("Password: ") + settings.set("netmount.password", read("\7")) + settings.save() +end + download_to("https://github.com/cadenthecreator/ccde/raw/refs/heads/main/startup.lua","startup.lua") download_to("https://github.com/cadenthecreator/ccde/raw/refs/heads/main/modules/interactions.lua","modules/interactions.lua") download_to("https://github.com/cadenthecreator/ccde/raw/refs/heads/main/modules/keybinds.lua","modules/keybinds.lua") @@ -30,3 +43,7 @@ download_to("https://github.com/cadenthecreator/ccde/raw/refs/heads/main/.apps/a download_to("https://github.com/cadenthecreator/ccde/raw/refs/heads/main/.apps/shell.app",".apps/shell.app") download_to("https://github.com/cadenthecreator/ccde/raw/refs/heads/main/.apps/worm.app",".apps/worm.app") download_to("https://github.com/cadenthecreator/ccde/raw/refs/heads/main/.wallpaper.nfp",".wallpaper.nfp") + +if netmount then + download_to("https://github.com/cadenthecreator/ccde/raw/refs/heads/main/modules/netmount.lua","modules/netmount.lua") +end \ No newline at end of file diff --git a/libs/compat.lua b/libs/compat.lua index 1c0b63c..5835c6b 100644 --- a/libs/compat.lua +++ b/libs/compat.lua @@ -541,6 +541,7 @@ function lib.setupENV(win) env.settings = settings env._ENV = env env._G = env + env.http = http term.redirect(win) return env end @@ -617,6 +618,7 @@ local function runInRuntime(func, win, close_handled) end local data = table.pack(os.pullEvent()) data[1] = data[1]:gsub(escape_lua_pattern "_" .. winid, "") + event_data = { n = 0 } if data[1] == filter or filter == nil or data[1] == "terminated" then if data[1] == "char" then if data[#data] == winid then @@ -661,8 +663,6 @@ local function runInRuntime(func, win, close_handled) else event_data = data end - else - event_data = { n = 0 } end end end diff --git a/libs/window.lua b/libs/window.lua index 7132912..d4057c4 100644 --- a/libs/window.lua +++ b/libs/window.lua @@ -52,7 +52,7 @@ end local function clamp(v, lo, hi) return (v < lo) and lo or ((v > hi) and hi or v) end -function lib.create(name, w, h, x, y) +function lib.create(name, w, h, x, y, do_not_add) local sx,sy = term.getSize() if not x then x = sx/2-w/2 end if not y then y = sy/2-h/2 end @@ -337,8 +337,10 @@ function lib.create(name, w, h, x, y) -- initialize t.clear() - add(_G.windows, t) - lib.reorder() + if not do_not_add then + add(_G.windows, t) + lib.reorder() + end return t end diff --git a/modules/netmount.lua b/modules/netmount.lua new file mode 100644 index 0000000..63de4b3 --- /dev/null +++ b/modules/netmount.lua @@ -0,0 +1,203 @@ +local _ +local ofs = assert(_G.fs, "-eh?") +-- [[ Argument Parsing ]] -- +local args = table.pack(...) +local handle +while true do + for i = 1, #args do + args[i]:gsub("(.*)=(.*)", function(k, v) + args[k] = v + end) + end + if #args < 3 then + local keys = { + "url", + "username", + "password", + "path", + "run" + } + for i = 1, #keys do + local key = keys[i] + if not args[key] then + args[key] = settings.get("netmount." .. key) + end + end + args.path = args.path or "net" + end + if (args.username and args.url and args.password) then + handle = http.get(args.url:gsub("/$", "").."/api.lua") + if handle then break end + sleep(3) + end + sleep() +end + +handle.close() +local state = assert(nm.createState(args.url, args.username, args.password)) + +local netroot = ofs.combine(args.path) +assert(not ofs.exists(netroot), "Directory "..netroot.." already exists") + +local function toNetRoot(path) + path = ofs.combine(path) + local nreplaced + path, nreplaced = path:gsub("^" .. netroot .. "/", "") + if path == netroot then + return true, "" + elseif path == netroot or nreplaced == 1 then + return true, path + else + return false, path + end +end + +-- [[ Websocket Request/Response Function & Netmount fs Initialization ]] -- + +local function wrapfs() + local nfs = nm.createFs(state, args.path) + + local api = {} + + -- [[ Functions that can be directly ripped from old fs API ]] -- + local copyold = { + "combine", + "getName", + "getDir", + } + + for _, fn in ipairs(copyold) do + api[fn] = ofs[fn] + end + + -- [[ Network Dependent Overrides ]] -- + + local singleOverrides = { + "makeDir", "delete", "list", + "attributes", "exists", "isDir", + "isReadOnly", "getDrive", "getSize", + "getFreeSpace", "getCapacity" + } + + for _, name in ipairs(singleOverrides) do + api[name] = function(path) + local net + net, path = toNetRoot(path) + if net then + return nfs[name](path) + else + local out = ofs[name](path) + if #fs.combine(path) == 0 then + if name == "list" then + ---@cast out string[] + out[#out + 1] = args.path + table.sort(out, function(a, b) + return #a < #b + end) + end + end + return out + end + end + end + + local doubleOverrides = { + "move", + "copy" + } + + --- Bidirectionally handle relocating files + ---@param path string + ---@param dest string + local function relocate(name, path, dest) + if api.exists(dest) then + error("/" .. api.combine(dest) .. ": File exists") + end + local pnet, dnet + pnet, path = toNetRoot(path) + dnet, dest = toNetRoot(dest) + if pnet and dnet then + nfs[name](path, dest) + elseif not (pnet or dnet) then + ofs[name](path, dest) + else + local pfs, dfs, perr, derr + local estr = "Failed to open %s file %s" + if pnet and not dnet then -- from server to client + pfs, dfs = nfs, ofs + perr, derr = "remote", "local" + else -- from client to server + pfs, dfs = ofs, nfs + perr, derr = "local", "remote" + end + if pfs.isDir(path) then + local list = pfs.list(path) + for _, p in ipairs(list) do + relocate(api.combine(path, p), api.combine(dest, p)) + end + else + local pfile, dfile = assert(pfs.open(path, "rb"), estr:format(perr, path)), assert(dfs.open(dest, "wb"), estr:format(derr, dest)) + dfile.write(pfile.readAll()) + pfile.close() + dfile.close() + if name == "move" then + pfs.delete(path) + end + end + end + end + + for _, name in ipairs(doubleOverrides) do + api[name] = function(path, dest) + relocate(name, path, dest) + end + end + + -- [[ Network Dependent File Handles ]] -- + + api.open = function(path, mode) + local net + net, path = toNetRoot(path) + if net then + return nfs.open(path, mode) + else + return ofs.open(path, mode) + end + end + + do + local romfs, i = "", 1 + for line in io.lines("rom/apis/fs.lua") do + -- Rip out definition weirdness + if not (i > 9 and i < 14) then + romfs = romfs .. line .. "\n" + end + i = i + 1 + end + local env = {} + for k, f in pairs(_ENV) do + if f == _ENV then + f = env + else + env[k] = f + end + end + env.fs = api + setmetatable(env, {__index = _G}) + assert(pcall(assert(load(romfs, "romfsapi", nil, env)))) -- find, complete, and isDriveRoot + end + + return api +end + +-- [[ Main Program / Connection handlers ]] -- +_G.fs = wrapfs() + +local pok, err = pcall(parallel.waitForAny, nm.getSyncHandler(state), nm.getConnectionHandler(state)) +if not pok then + printError(err) +end +state.close() +print("Press any key to continue") +os.pullEvent("key") +_G.fs = ofs \ No newline at end of file diff --git a/startup.lua b/startup.lua index c74d98e..b87907c 100644 --- a/startup.lua +++ b/startup.lua @@ -8,8 +8,28 @@ local wrap = require("cc.strings").wrap _G.threads = {} _G.windows = {} _G.keybinds = {} -local term = term.native() +local nterm = term.native() +local sx,sy = term.getSize() +local term = window.create("",sx,sy,1,1,true) local event = { n = 0 } + +local function drawPixelInternal(xPos, yPos) + term.setCursorPos(xPos, yPos) + term.write(" ") +end + +local function drawImage(image, xPos, yPos) + for y = 1, #image do + local tLine = image[y] + for x = 1, #tLine do + if tLine[x] > 0 then + term.setBackgroundColor(tLine[x]) + drawPixelInternal(x + xPos - 1, y + yPos - 1) + end + end + end +end + local function threads() for id, thr in pairs(_G.threads) do if thr then @@ -75,7 +95,7 @@ local function windows() end end term.setCursorPos(win.x + win.cursorX - 1, win.y + win.cursorY - 1) - term.setCursorBlink(win.cursorBlink) + nterm.setCursorBlink(win.cursorBlink) if win.closing then _G.windows[id] = nil end @@ -90,7 +110,7 @@ local function desktop() if image then nft.draw(image,1,1) elseif paint_image then - paintutils.drawImage(paint_image,1,2) + drawImage(paint_image,1,2) end end @@ -118,11 +138,40 @@ for _, i in ipairs(fs.list("/modules")) do end end +local function screen() + local cx,cy = term.getCursorPos() + for cy = 1, term.h do + nterm.setCursorPos(term.x, term.y + cy - 1) + local line, fg, bg = "", "", "" + for cx = 1, term.w do + if term.buffer[cx] then + local cell = term.buffer[cx][cy] + if cell then + line = line .. cell.char + fg = fg .. ("0123456789abcdef"):sub(math.log(cell.tc, 2) + 1, math.log(cell.tc, 2) + 1) + bg = bg .. ("0123456789abcdef"):sub(math.log(cell.bc, 2) + 1, math.log(cell.bc, 2) + 1) + else + line = line .. " " + fg = fg .. "0" + bg = bg .. "f" + end + else + line = line .. " " + fg = fg .. "0" + bg = bg .. "f" + end + end + nterm.blit(line, fg, bg) + end + nterm.setCursorPos(cx,cy) +end + local function render() while true do desktop() windows() bars() + screen() sleep(1 / 20) end end