diff --git a/sys/apis/fs/netfs.lua b/sys/apis/fs/netfs.lua index 28f64c3..01b5218 100644 --- a/sys/apis/fs/netfs.lua +++ b/sys/apis/fs/netfs.lua @@ -29,7 +29,7 @@ local function remoteCommand(node, msg) error('netfs: Connection failed', 2) end -local methods = { 'delete', 'exists', 'getFreeSpace', 'getSize', 'makeDir' } +local methods = { 'delete', 'exists', 'getFreeSpace', 'makeDir' } local function resolveDir(dir, node) dir = dir:gsub(node.mountPoint, '', 1) @@ -101,6 +101,16 @@ function netfs.isReadOnly(node, dir) }) end +function netfs.getSize(node, dir) + if dir == node.mountPoint and node.directory == '' then + return 0 + end + return remoteCommand(node, { + fn = 'getSize', + args = { resolveDir(dir, node) }, + }) +end + function netfs.find(node, spec) spec = resolveDir(spec, node) local list = remoteCommand(node, { diff --git a/sys/apis/meProvider.lua b/sys/apis/meProvider.lua index 826df0c..2b82950 100644 --- a/sys/apis/meProvider.lua +++ b/sys/apis/meProvider.lua @@ -1,16 +1,40 @@ local class = require('class') local Logger = require('logger') +local Peripheral = require('peripheral') local MEProvider = class() function MEProvider:init(args) - self.items = {} - self.name = 'ME' + local defaults = { + items = { }, + name = 'ME', + } + Util.merge(self, defaults) + Util.merge(self, args) + + if self.side then + local mep = peripheral.wrap('bottom') + if mep then + Util.merge(self, mep) + end + else + local mep = Peripheral.getByMethod('getAvailableItems') + if mep then + Util.merge(self, mep) + end + end + + if self.side then + local sides = { + top = 'down', + bottom = 'up', + } + self.oside = sides[self.side] + end end function MEProvider:isValid() - local mep = peripheral.wrap('bottom') - return mep and mep.getAvailableItems and mep.getAvailableItems() + return self.getAvailableItems and self.getAvailableItems() end -- Strip off color prefix @@ -32,16 +56,18 @@ local function safeString(text) end function MEProvider:refresh() - local mep = peripheral.wrap('bottom') - if mep then - self.items = mep.getAvailableItems('all') - for _,v in pairs(self.items) do - Util.merge(v, v.item) - v.name = safeString(v.display_name) - end + self.items = self.getAvailableItems('all') + for _,v in pairs(self.items) do + Util.merge(v, v.item) + v.name = safeString(v.display_name) end return self.items end + +function MEProvider:listItems() + self:refresh() + return self.items +end function MEProvider:getItemInfo(id, dmg) @@ -60,21 +86,14 @@ function MEProvider:craft(id, dmg, qty) if item and item.is_craftable then - local mep = peripheral.wrap('bottom') - if mep then - Logger.log('meProvideer', 'requested crafting for: ' .. id .. ':' .. dmg .. ' qty: ' .. qty) - mep.requestCrafting({ id = id, dmg = dmg }, qty) - return true - end + Logger.log('MEProvider', 'requested crafting for: ' .. id .. ':' .. dmg .. ' qty: ' .. qty) + self.requestCrafting({ id = id, dmg = dmg }, qty) + return true end - - return false end function MEProvider:craftItems(items) - local mep = peripheral.wrap('bottom') - - local cpus = mep.getCraftingCPUs() or { } + local cpus = self.getCraftingCPUs() or { } local count = 0 for _,cpu in pairs(cpus) do @@ -94,42 +113,29 @@ function MEProvider:craftItems(items) end function MEProvider:provide(item, qty, slot) - local mep = peripheral.wrap('bottom') - if mep then - return pcall(function() - mep.exportItem({ - id = item.id, - dmg = item.dmg - }, - 'up', - qty, - slot) - end) - - --if item.qty then - -- item.qty = item.qty - extractedQty - --end - end + return pcall(function() + self.exportItem({ + id = item.id, + dmg = item.dmg + }, self.oside, qty, slot) + end) end function MEProvider:insert(slot, qty) - local mep = peripheral.wrap('bottom') - if mep then - local s, m = pcall(function() mep.pullItem('up', slot, qty) end) + local s, m = pcall(function() self.pullItem(self.oside, slot, qty) end) + if not s and m then + print('MEProvider:pullItem') + print(m) + Logger.log('MEProvider', 'Insert failed, trying again') + sleep(1) + s, m = pcall(function() self.pullItem('up', slot, qty) end) if not s and m then - print('meProvider:pullItem') + print('MEProvider:pullItem') print(m) - Logger.log('meProvider', 'Insert failed, trying again') - sleep(1) - s, m = pcall(function() mep.pullItem('up', slot, qty) end) - if not s and m then - print('meProvider:pullItem') - print(m) - Logger.log('meProvider', 'Insert failed again') - read() - else - Logger.log('meProvider', 'Insert successful') - end + Logger.log('MEProvider', 'Insert failed again') + read() + else + Logger.log('MEProvider', 'Insert successful') end end end diff --git a/sys/apps/Appstore.lua b/sys/apps/Appstore.lua index f344242..c2f2ff0 100644 --- a/sys/apps/Appstore.lua +++ b/sys/apps/Appstore.lua @@ -1,8 +1,6 @@ require = requireInjector(getfenv(1)) local Util = require('util') -local class = require('class') local UI = require('ui') -local Event = require('event') local sandboxEnv = Util.shallowCopy(getfenv(1)) setmetatable(sandboxEnv, { __index = _G }) @@ -10,12 +8,14 @@ setmetatable(sandboxEnv, { __index = _G }) multishell.setTitle(multishell.getCurrent(), 'App Store') UI:configure('Appstore', ...) +local APP_DIR = 'usr/apps' + local sources = { { text = "STD Default", event = 'source', url = "http://pastebin.com/raw/zVws7eLq" }, --stock - +--[[ { text = "Discover", event = 'source', generateName = true, @@ -24,9 +24,10 @@ local sources = { { text = "Opus", event = 'source', url = "http://pastebin.com/raw/ajQ91Rmn" }, +]] } -shell.setDir('/apps') +shell.setDir(APP_DIR) function downloadApp(app) local h @@ -49,8 +50,8 @@ function runApp(app, checkExists, ...) local path, fn local args = { ... } - if checkExists and fs.exists(fs.combine('/apps', app.name)) then - path = fs.combine('/apps', app.name) + if checkExists and fs.exists(fs.combine(APP_DIR, app.name)) then + path = fs.combine(APP_DIR, app.name) else local program = downloadApp(app) @@ -89,7 +90,7 @@ local installApp = function(app) return false, "Failed to download" end - local fullPath = fs.combine('/apps', app.name) + local fullPath = fs.combine(APP_DIR, app.name) Util.writeFile(fullPath, program) return true, 'Installed as ' .. fullPath end @@ -103,6 +104,7 @@ local viewApp = function(app) Util.writeFile('/.source', program) shell.openForegroundTab('edit /.source') + fs.delete('/.source') return true end @@ -190,7 +192,7 @@ function appPage:enable(source, app) UI.Page.enable(self) self.container.viewport:setScrollPosition(0) - if fs.exists(fs.combine('/apps', app.name)) then + if fs.exists(fs.combine(APP_DIR, app.name)) then self.menuBar.removeButton:enable('Remove') else self.menuBar.removeButton:disable('Remove') @@ -215,13 +217,13 @@ function appPage:eventHandler(event) if self.app.runOnly then s,m = runApp(self.app, false, 'uninstall') else - fs.delete(fs.combine('/apps', self.app.name)) + fs.delete(fs.combine(APP_DIR, self.app.name)) self.notification:success("Uninstalled " .. self.app.name, 3) self:focusFirst(self) self.menuBar.removeButton:disable('Remove') self.menuBar:draw() - os.unregisterApp(fs.combine('/apps', self.app.name)) + os.unregisterApp(self.app.creator .. '.' .. self.app.name) end elseif event.type == 'install' then @@ -246,11 +248,11 @@ function appPage:eventHandler(event) end os.registerApp({ - run = fs.combine('/apps', self.app.name), + run = fs.combine(APP_DIR, self.app.name), title = self.app.title, category = category, icon = self.app.icon, - }) + }, self.app.creator .. '.' .. self.app.name) end else self.notification:error(m, 3) @@ -348,7 +350,7 @@ function categoryPage.grid:sortCompare(a, b) end function categoryPage.grid:getRowTextColor(row, selected) - if fs.exists(fs.combine('/apps', row.name)) then + if fs.exists(fs.combine(APP_DIR, row.name)) then return colors.orange end return UI.Grid:getRowTextColor(row, selected) @@ -370,7 +372,7 @@ function categoryPage:eventHandler(event) self:draw() elseif event.type == 'quit' then - Event.exitPullEvents() + UI:exitPullEvents() else return UI.Page.eventHandler(self, event) @@ -382,5 +384,5 @@ print("Retrieving catalog list") categoryPage:setSource(sources[1]) UI:setPage(categoryPage) -Event.pullEvents() +UI:pullEvents() UI.term:reset() diff --git a/sys/apps/Overview.lua b/sys/apps/Overview.lua index 4bc754a..00fb107 100644 --- a/sys/apps/Overview.lua +++ b/sys/apps/Overview.lua @@ -158,8 +158,8 @@ function page.container:setCategory(categoryName) filtered = { } for _,v in ipairs(config.Recent) do - local app = Util.find(applications, 'run', v) - if app and fs.exists(app.run) then + local app = Util.find(applications, 'key', v) + if app then -- and fs.exists(app.run) then table.insert(filtered, app) end end @@ -273,12 +273,12 @@ function page:eventHandler(event) elseif event.type == 'button' then for k,v in ipairs(config.Recent) do - if v == event.button.app.run then + if v == event.button.app.key then table.remove(config.Recent, k) break end end - table.insert(config.Recent, 1, event.button.app.run) + table.insert(config.Recent, 1, event.button.app.key) if #config.Recent > maxRecent then table.remove(config.Recent, maxRecent + 1) end diff --git a/sys/apps/Script.lua b/sys/apps/Script.lua index 519c8fe..7b6d989 100644 --- a/sys/apps/Script.lua +++ b/sys/apps/Script.lua @@ -5,7 +5,7 @@ local Socket = require('socket') local Config = require('config') local GROUPS_PATH = 'usr/groups' -local SCRIPTS_PATH = 'usr/scripts' +local SCRIPTS_PATH = 'sys/etc/scripts' multishell.setTitle(multishell.getCurrent(), 'Script') UI:configure('script', ...) diff --git a/sys/apps/shell b/sys/apps/shell index 9cdd65f..c76368c 100644 --- a/sys/apps/shell +++ b/sys/apps/shell @@ -79,14 +79,11 @@ end function shell.resolveProgram( _sCommand ) - local sPath = PATH or '' - if ALIASES[ _sCommand ] ~= nil then _sCommand = ALIASES[ _sCommand ] end local path = shell.resolve(_sCommand) - if fs.exists(path) and not fs.isDir(path) then return path end @@ -104,9 +101,9 @@ function shell.resolveProgram( _sCommand ) return nil end --- Otherwise, look on the path variable - for sPath in string.gmatch(sPath, "[^:]+") do - sPath = fs.combine( shell.resolve(sPath), _sCommand ) + -- Otherwise, look on the path variable + for sPath in string.gmatch(PATH or '', "[^:]+") do + sPath = fs.combine(sPath, _sCommand ) if fs.exists( sPath ) and not fs.isDir( sPath ) then return sPath end diff --git a/sys/extensions/os.lua b/sys/extensions/os.lua index a8e8929..4b82552 100644 --- a/sys/extensions/os.lua +++ b/sys/extensions/os.lua @@ -1,5 +1,8 @@ require = requireInjector(getfenv(1)) local Config = require('config') +local SHA1 = require('sha1') + +local REGISTRY_DIR = 'usr/.registry' local config = { enable = false, @@ -13,7 +16,6 @@ function lockScreen() require = requireInjector(getfenv(1)) local UI = require('ui') local Event = require('event') - local SHA1 = require('sha1') local center = math.floor(UI.term.width / 2) @@ -153,45 +155,20 @@ end -- move completely into overview -- just post event from appstore -function os.registerApp(entry) - local apps = { } - Config.load('apps', apps) - - local run = fs.combine(entry.run, '') - - for k,app in pairs(apps) do - if app.run == run then - table.remove(apps, k) - break - end - end - - table.insert(apps, { - run = run, - title = entry.title, - args = entry.args, - category = entry.category, - icon = entry.icon, - }) - - Config.update('apps', apps) +function os.registerApp(app, key) + app.key = SHA1.sha1(key) +debug(fs.combine(REGISTRY_DIR, app.key)) +debug(app) + Util.writeTable(fs.combine(REGISTRY_DIR, app.key), app) os.queueEvent('os_register_app') end -function os.unregisterApp(run) +function os.unregisterApp(key) - local apps = { } - Config.load('apps', apps) - - local run = fs.combine(run, '') - - for k,app in pairs(apps) do - if app.run == run then - table.remove(apps, k) - Config.update('apps', apps) - os.queueEvent('os_register_app') - break - end + local filename = fs.combine(REGISTRY_DIR, SHA1.sha1(key)) + if fs.exists(filename) then + fs.delete(filename) + os.queueEvent('os_register_app') end end