|
| 1 | +-- Bios entries for CCEmu |
| 2 | +-- A good majority of this is copied from ComputerCraft's bios.lua |
| 3 | + |
| 4 | +local term = require("term") |
| 5 | +local text = require("text") |
| 6 | +local fs = require("filesystem") |
| 7 | +local component = require("component") |
| 8 | +local computer = require("computer") |
| 9 | +local unicode = require("unicode") |
| 10 | + |
| 11 | +local env = ccemu.env |
| 12 | +local config = ccemu.config |
| 13 | + |
| 14 | +local function tablecopy(orig) |
| 15 | + local orig_type = type(orig) |
| 16 | + local copy |
| 17 | + if orig_type == 'table' then |
| 18 | + copy = {} |
| 19 | + for orig_key, orig_value in pairs(orig) do |
| 20 | + copy[orig_key] = orig_value |
| 21 | + end |
| 22 | + else |
| 23 | + copy = orig |
| 24 | + end |
| 25 | + return copy |
| 26 | +end |
| 27 | + |
| 28 | +function env.os.version() |
| 29 | + return "CCEmu 1.1" |
| 30 | +end |
| 31 | +function env.os.pullEventRaw(filter) |
| 32 | + return coroutine.yield(filter) |
| 33 | +end |
| 34 | +function env.os.pullEvent(filter) |
| 35 | + local e = table.pack(env.os.pullEventRaw(filter)) |
| 36 | + if e[1] == "terminate" then |
| 37 | + error("interrupted", 0) |
| 38 | + end |
| 39 | + return table.unpack(e) |
| 40 | +end |
| 41 | +env.sleep = os.sleep |
| 42 | +env.write = function(data) |
| 43 | + local count = 0 |
| 44 | + local otw = text.wrap |
| 45 | + function text.wrap(...) |
| 46 | + local a, b, c = otw(...) |
| 47 | + if c then count = count + 1 end |
| 48 | + return a, b, c |
| 49 | + end |
| 50 | + local x = term.getCursor() |
| 51 | + local w = component.gpu.getResolution() |
| 52 | + term.write(data, unicode.len(data) + x - 1 > w) |
| 53 | + text.wrap = otw |
| 54 | + return count |
| 55 | +end |
| 56 | +env.print = function(...) |
| 57 | + local args = {...} |
| 58 | + for i = 1, #args do |
| 59 | + args[i] = tostring(args[i]) |
| 60 | + end |
| 61 | + return env.write(table.concat(args, "\t") .. "\n") |
| 62 | +end |
| 63 | +env.printError = function(...) io.stderr:write(table.concat({...}, "\t") .. "\n") end |
| 64 | +env.read = function(pwchar, hist) |
| 65 | + local line = term.read(tablecopy(hist), nil, nil, pwchar) |
| 66 | + if line == nil then |
| 67 | + return "" |
| 68 | + end |
| 69 | + return line:gsub("\n", "") |
| 70 | +end |
| 71 | +env.loadfile = function(file, env) |
| 72 | + return loadfile(file, "t", env) |
| 73 | +end |
| 74 | +env.dofile = dofile |
| 75 | +env.os.run = function(newenv, name, ...) |
| 76 | + local args = {...} |
| 77 | + setmetatable(newenv, {__index=env}) |
| 78 | + local fn, err = loadfile(name, nil, newenv) |
| 79 | + if fn then |
| 80 | + local ok, err = pcall(function() fn(table.unpack(args)) end) |
| 81 | + if not ok then |
| 82 | + if err and err ~= "" then |
| 83 | + env.printError(err) |
| 84 | + end |
| 85 | + return false |
| 86 | + end |
| 87 | + return true |
| 88 | + end |
| 89 | + if err and err ~= "" then |
| 90 | + env.printError(err) |
| 91 | + end |
| 92 | + return false |
| 93 | +end |
| 94 | + |
| 95 | +local tAPIsLoading = {} |
| 96 | +env.os.loadAPI = function(path) |
| 97 | + local sName = fs.name(path) |
| 98 | + if tAPIsLoading[sName] == true then |
| 99 | + env.printError("API " .. sName .. " is already being loaded") |
| 100 | + return false |
| 101 | + end |
| 102 | + tAPIsLoading[sName] = true |
| 103 | + |
| 104 | + local env2 |
| 105 | + env2 = { |
| 106 | + getfenv = function() return env2 end |
| 107 | + } |
| 108 | + setmetatable(env2, {__index = env}) |
| 109 | + local fn, err = loadfile(path, nil, env2) |
| 110 | + if fn then |
| 111 | + fn() |
| 112 | + else |
| 113 | + env.printError(err) |
| 114 | + tAPIsLoading[sName] = nil |
| 115 | + return false |
| 116 | + end |
| 117 | + |
| 118 | + local tmpcopy = {} |
| 119 | + for k, v in pairs(env2) do |
| 120 | + tmpcopy[k] = v |
| 121 | + end |
| 122 | + |
| 123 | + env[sName] = tmpcopy |
| 124 | + tAPIsLoading[sName] = nil |
| 125 | + return true |
| 126 | +end |
| 127 | +env.os.unloadAPI = function(name) |
| 128 | + if _name ~= "_G" and type(env[name]) == "table" then |
| 129 | + env[name] = nil |
| 130 | + end |
| 131 | +end |
| 132 | +env.os.sleep = os.sleep |
| 133 | +if env.http ~= nil then |
| 134 | + -- TODO: http.get |
| 135 | + -- TODO: http.post |
| 136 | +end |
| 137 | + |
| 138 | +-- Install the lua part of the FS api |
| 139 | +local empty = {} |
| 140 | +env.fs.complete = function(path, location, includeFiles, includeDirs) |
| 141 | + includeFiles = (includeFiles ~= false) |
| 142 | + includeDirs = (includeDirs ~= false) |
| 143 | + local dir = location |
| 144 | + local start = 1 |
| 145 | + local slash = string.find(path, "[/\\]", start) |
| 146 | + if slash == 1 then |
| 147 | + dir = "" |
| 148 | + start = 2 |
| 149 | + end |
| 150 | + local name |
| 151 | + while not name do |
| 152 | + local slash = string.find(path, "[/\\]", start) |
| 153 | + if slash then |
| 154 | + local part = string.sub(path, start, slash - 1) |
| 155 | + dir = env.fs.combine(dir, part) |
| 156 | + start = slash + 1 |
| 157 | + else |
| 158 | + name = string.sub(path, start) |
| 159 | + end |
| 160 | + end |
| 161 | + |
| 162 | + if env.fs.isDir(dir) then |
| 163 | + local results = {} |
| 164 | + if includeDirs and path == "" then |
| 165 | + table.insert(results, ".") |
| 166 | + end |
| 167 | + if dir ~= "" then |
| 168 | + if path == "" then |
| 169 | + table.insert(results, (includeDirs and "..") or "../") |
| 170 | + elseif path == "." then |
| 171 | + table.insert(results, (includeDirs and ".") or "./") |
| 172 | + end |
| 173 | + end |
| 174 | + local tFiles = env.fs.list(dir) |
| 175 | + for n=1,#tFiles do |
| 176 | + local sFile = tFiles[n] |
| 177 | + if #sFile >= #name and string.sub(sFile, 1, #name) == name then |
| 178 | + local bIdir = env.fs.isDir(env.fs.combine(dir, sFile)) |
| 179 | + local result = string.sub(sFile, #name + 1) |
| 180 | + if bIdir then |
| 181 | + table.insert(results, result .. "/") |
| 182 | + if includeDirs and #result > 0 then |
| 183 | + table.insert(results, result) |
| 184 | + end |
| 185 | + else |
| 186 | + if includeFiles and #result > 0 then |
| 187 | + table.insert(results, result) |
| 188 | + end |
| 189 | + end |
| 190 | + end |
| 191 | + end |
| 192 | + return results |
| 193 | + end |
| 194 | + return empty |
| 195 | +end |
0 commit comments