moved custom Lua methods from os table to new 'computer' table; some typos in config comments

This commit is contained in:
Florian Nücke 2013-12-06 15:44:16 +01:00
parent 039b3cce9b
commit c54f285345
18 changed files with 73 additions and 73 deletions

View File

@ -1,7 +1,6 @@
local deadline = 0 local deadline = 0
local realTime = os.realTime
local function checkDeadline() local function checkDeadline()
if realTime() > deadline then if computer.realTime() > deadline then
debug.sethook(coroutine.running(), checkDeadline, "", 1) debug.sethook(coroutine.running(), checkDeadline, "", 1)
error("too long without yielding", 0) error("too long without yielding", 0)
end end
@ -223,26 +222,28 @@ sandbox = {
rename = nil, -- in lib/os.lua rename = nil, -- in lib/os.lua
time = os.time, time = os.time,
tmpname = nil, -- in lib/os.lua tmpname = nil, -- in lib/os.lua
},
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
-- Start of non-standard stuff. -- Start of non-standard stuff.
isRobot = os.isRobot, computer = {
address = os.address, isRobot = computer.isRobot,
romAddress = os.romAddress, address = computer.address,
tmpAddress = os.tmpAddress, romAddress = computer.romAddress,
freeMemory = os.freeMemory, tmpAddress = computer.tmpAddress,
totalMemory = os.totalMemory, freeMemory = computer.freeMemory,
uptime = os.uptime, totalMemory = computer.totalMemory,
uptime = computer.uptime,
users = os.users, users = computer.users,
addUser = function(name) addUser = function(name)
checkArg(1, name, "string") checkArg(1, name, "string")
return os.addUser(name) return computer.addUser(name)
end, end,
removeUser = function(name) removeUser = function(name)
checkArg(1, name, "string") checkArg(1, name, "string")
return os.removeUser(name) return computer.removeUser(name)
end, end,
shutdown = function(reboot) shutdown = function(reboot)
@ -254,17 +255,17 @@ sandbox = {
for i = 1, args.n do for i = 1, args.n do
checkArg(i + 1, args[i], "nil", "boolean", "string", "number") checkArg(i + 1, args[i], "nil", "boolean", "string", "number")
end end
return os.pushSignal(name, ...) return computer.pushSignal(name, ...)
end, end,
pullSignal = function(timeout) pullSignal = function(timeout)
local deadline = os.uptime() + local deadline = computer.uptime() +
(type(timeout) == "number" and timeout or math.huge) (type(timeout) == "number" and timeout or math.huge)
repeat repeat
local signal = table.pack(coroutine.yield(deadline - os.uptime())) local signal = table.pack(coroutine.yield(deadline - computer.uptime()))
if signal.n > 0 then if signal.n > 0 then
return table.unpack(signal, 1, signal.n) return table.unpack(signal, 1, signal.n)
end end
until os.uptime() >= deadline until computer.uptime() >= deadline
end end
}, },
@ -330,7 +331,7 @@ local function main()
-- Minimalistic hard-coded pure async proxy for our ROM. -- Minimalistic hard-coded pure async proxy for our ROM.
local rom = {} local rom = {}
function rom.invoke(method, ...) function rom.invoke(method, ...)
return invoke(true, os.romAddress(), method, ...) return invoke(true, computer.romAddress(), method, ...)
end end
function rom.open(file) return rom.invoke("open", file) end function rom.open(file) return rom.invoke("open", file) end
function rom.read(handle) return rom.invoke("read", handle, math.huge) end function rom.read(handle) return rom.invoke("read", handle, math.huge) end
@ -387,7 +388,7 @@ local function main()
end end
local co, args = bootstrap(), {n=0} local co, args = bootstrap(), {n=0}
while true do while true do
deadline = os.realTime() + timeout -- timeout global is set by host deadline = computer.realTime() + timeout -- timeout global is set by host
debug.sethook(co, checkDeadline, "", 10000) debug.sethook(co, checkDeadline, "", 10000)
local result = table.pack(coroutine.resume(co, table.unpack(args, 1, args.n))) local result = table.pack(coroutine.resume(co, table.unpack(args, 1, args.n)))
if not result[1] then if not result[1] then

View File

@ -1 +1 @@
print(os.address()) print(computer.address())

View File

@ -1,2 +1,2 @@
print("Rebooting...") print("Rebooting...")
os.shutdown(true) computer.shutdown(true)

View File

@ -2,7 +2,7 @@ local args, options = shell.parse(...)
local history = {} local history = {}
if options.v then if options.v then
print("OpenOS v1.0 (" .. math.floor(os.totalMemory() / 1024) .. "k RAM)") print("OpenOS v1.0 (" .. math.floor(computer.totalMemory() / 1024) .. "k RAM)")
end end
while true do while true do
@ -12,7 +12,7 @@ while true do
end end
term.clear() term.clear()
if options.v then if options.v then
print("OpenOS v1.0 (" .. math.floor(os.totalMemory() / 1024) .. "k RAM)") print("OpenOS v1.0 (" .. math.floor(computer.totalMemory() / 1024) .. "k RAM)")
end end
end end
while term.isAvailable() do while term.isAvailable() do

View File

@ -1,2 +1,2 @@
term.clear() term.clear()
os.shutdown() computer.shutdown()

View File

@ -1,4 +1,4 @@
local seconds = math.floor(os.uptime()) local seconds = math.floor(computer.uptime())
local minutes, hours = 0, 0 local minutes, hours = 0, 0
if seconds >= 60 then if seconds >= 60 then
minutes = math.floor(seconds / 60) minutes = math.floor(seconds / 60)

View File

@ -4,7 +4,7 @@ if #args < 1 then
return return
end end
local result, reason = os.addUser(args[1]) local result, reason = computer.addUser(args[1])
if not result then if not result then
print(reason) print(reason)
end end

View File

@ -4,6 +4,6 @@ if #args < 1 then
return return
end end
if not os.removeUser(args[1]) then if not computer.removeUser(args[1]) then
print("no such user") print("no such user")
end end

View File

@ -1,8 +1,8 @@
fs.mount(os.romAddress(), "/") fs.mount(computer.romAddress(), "/")
if os.tmpAddress() then fs.mount(os.tmpAddress(), "/tmp") end if computer.tmpAddress() then fs.mount(computer.tmpAddress(), "/tmp") end
for c, t in component.list() do for c, t in component.list() do
os.pushSignal("component_added", c, t) computer.pushSignal("component_added", c, t)
end end
os.sleep(0.5) -- Allow signal processing by libraries. os.sleep(0.5) -- Allow signal processing by libraries.

View File

@ -55,10 +55,10 @@ function component.setPrimary(componentType, address)
end end
primaries[componentType] = address and component.proxy(address) or nil primaries[componentType] = address and component.proxy(address) or nil
if wasAvailable then if wasAvailable then
os.pushSignal("component_unavailable", componentType) computer.pushSignal("component_unavailable", componentType)
end end
if component.isAvailable(componentType) then if component.isAvailable(componentType) then
os.pushSignal("component_available", componentType) computer.pushSignal("component_available", componentType)
end end
end end

View File

@ -40,13 +40,13 @@ local function tick()
local function elapsed() local function elapsed()
local list = {} local list = {}
for id, timer in pairs(timers) do for id, timer in pairs(timers) do
if timer.after <= os.uptime() then if timer.after <= computer.uptime() then
table.insert(list, timer.callback) table.insert(list, timer.callback)
timer.times = timer.times - 1 timer.times = timer.times - 1
if timer.times <= 0 then if timer.times <= 0 then
timers[id] = nil timers[id] = nil
else else
timer.after = os.uptime() + timer.interval timer.after = computer.uptime() + timer.interval
end end
end end
end end
@ -131,7 +131,7 @@ function event.pull(...)
end end
local deadline = seconds and local deadline = seconds and
(os.uptime() + seconds) or (computer.uptime() + seconds) or
(hasFilter and math.huge or 0) (hasFilter and math.huge or 0)
repeat repeat
@ -139,23 +139,23 @@ function event.pull(...)
for _, timer in pairs(timers) do for _, timer in pairs(timers) do
closest = math.min(closest, timer.after) closest = math.min(closest, timer.after)
end end
local signal = table.pack(os.pullSignal(closest - os.uptime())) local signal = table.pack(computer.pullSignal(closest - computer.uptime()))
if signal.n > 0 then if signal.n > 0 then
dispatch(table.unpack(signal, 1, signal.n)) dispatch(table.unpack(signal, 1, signal.n))
end end
tick() tick()
if event.shouldInterrupt() then if event.shouldInterrupt() then
lastInterrupt = os.uptime() lastInterrupt = computer.uptime()
error("interrupted", 0) error("interrupted", 0)
end end
if not (seconds or hasFilter) or matches(signal, name, filter) then if not (seconds or hasFilter) or matches(signal, name, filter) then
return table.unpack(signal, 1, signal.n) return table.unpack(signal, 1, signal.n)
end end
until os.uptime() >= deadline until computer.uptime() >= deadline
end end
function event.shouldInterrupt() function event.shouldInterrupt()
return os.uptime() - lastInterrupt > 1 and return computer.uptime() - lastInterrupt > 1 and
keyboard.isControlDown() and keyboard.isControlDown() and
keyboard.isAltDown() and keyboard.isAltDown() and
keyboard.isKeyDown(keyboard.keys.c) keyboard.isKeyDown(keyboard.keys.c)
@ -171,7 +171,7 @@ function event.timer(interval, callback, times)
until not timers[id] until not timers[id]
timers[id] = { timers[id] = {
interval = interval, interval = interval,
after = os.uptime() + interval, after = computer.uptime() + interval,
callback = callback, callback = callback,
times = times or 1 times = times or 1
} }

View File

@ -5,7 +5,7 @@ function file.new(mode, stream, nogc)
mode = mode or "r", mode = mode or "r",
stream = stream, stream = stream,
buffer = "", buffer = "",
bufferSize = math.max(128, math.min(8 * 1024, os.freeMemory() / 8)), bufferSize = math.max(128, math.min(8 * 1024, computer.freeMemory() / 8)),
bufferMode = "full" bufferMode = "full"
} }
local metatable = { local metatable = {

View File

@ -31,10 +31,10 @@ os.rename = fs.rename
function os.sleep(timeout) function os.sleep(timeout)
checkArg(1, timeout, "number", "nil") checkArg(1, timeout, "number", "nil")
local deadline = os.uptime() + (timeout or 0) local deadline = computer.uptime() + (timeout or 0)
repeat repeat
event.pull(deadline - os.uptime()) event.pull(deadline - computer.uptime())
until os.uptime() >= deadline until computer.uptime() >= deadline
end end
function os.tmpname() function os.tmpname()

View File

@ -1,4 +1,4 @@
if not os.isRobot() then if not computer.isRobot() then
return return
end end

View File

@ -33,7 +33,7 @@ function term.clearLine()
cursorX = 1 cursorX = 1
end end
function term.getCursor(col, row) function term.getCursor()
return cursorX, cursorY return cursorX, cursorY
end end
@ -43,8 +43,8 @@ function term.setCursor(col, row)
if cursorBlink and cursorBlink.state then if cursorBlink and cursorBlink.state then
toggleBlink() toggleBlink()
end end
cursorX = col cursorX = math.floor(col)
cursorY = row cursorY = math.floor(row)
end end
function term.getCursorBlink() function term.getCursorBlink()
@ -371,7 +371,7 @@ local function onComponentAvailable(_, componentType)
screenAvailable = true screenAvailable = true
end end
if not wasAvailable and term.isAvailable() then if not wasAvailable and term.isAvailable() then
os.pushSignal("term_available") computer.pushSignal("term_available")
end end
end end
@ -383,7 +383,7 @@ local function onComponentUnavailable(_, componentType)
screenAvailable = false screenAvailable = false
end end
if wasAvailable and not term.isAvailable() then if wasAvailable and not term.isAvailable() then
os.pushSignal("term_unavailable") computer.pushSignal("term_unavailable")
end end
end end

View File

@ -761,6 +761,20 @@ class Computer(val owner: tileentity.Computer) extends ManagedComponent with Con
}) })
lua.setField(-2, "time") lua.setField(-2, "time")
// Pop the os table.
lua.pop(1)
// Computer API, stuff that kinda belongs to os, but we don't want to
// clutter it.
lua.newTable()
// Allow getting the real world time via os.realTime() for timeouts.
lua.pushScalaFunction(lua => {
lua.pushNumber(System.currentTimeMillis() / 1000.0)
1
})
lua.setField(-2, "realTime")
// The time the computer has been running, as opposed to the CPU time. // The time the computer has been running, as opposed to the CPU time.
lua.pushScalaFunction(lua => { lua.pushScalaFunction(lua => {
// World time is in ticks, and each second has 20 ticks. Since we // World time is in ticks, and each second has 20 ticks. Since we
@ -876,8 +890,8 @@ class Computer(val owner: tileentity.Computer) extends ManagedComponent with Con
}) })
lua.setField(-2, "removeUser") lua.setField(-2, "removeUser")
// Pop the os table. // Set the computer table.
lua.pop(1) lua.setGlobal("computer")
// Until we get to ingame screens we log to Java's stdout. // Until we get to ingame screens we log to Java's stdout.
lua.pushScalaFunction(lua => { lua.pushScalaFunction(lua => {

View File

@ -142,20 +142,6 @@ object LuaStateFactory {
state.pushNil() state.pushNil()
state.setGlobal("loadfile") state.setGlobal("loadfile")
// Push a couple of functions that override original Lua API functions or
// that add new functionality to it.
state.getGlobal("os")
// Allow getting the real world time via os.realTime() for timeouts.
state.pushScalaFunction(lua => {
lua.pushNumber(System.currentTimeMillis() / 1000.0)
1
})
state.setField(-2, "realTime")
// Pop the os table.
state.pop(1)
state.getGlobal("math") state.getGlobal("math")
// We give each Lua state it's own randomizer, since otherwise they'd // We give each Lua state it's own randomizer, since otherwise they'd

View File

@ -154,7 +154,7 @@ opencomputers {
# an utter pain in the part you sit on, because it makes robots meant to # an utter pain in the part you sit on, because it makes robots meant to
# dig holes utterly useless: the poor things couldn't break cobwebs in # dig holes utterly useless: the poor things couldn't break cobwebs in
# mining shafts with their golden pick axes. So, if this setting is true, # mining shafts with their golden pick axes. So, if this setting is true,
# we check for cobweb and allow robots to break 'em anyway, no matter # we check for cobwebs and allow robots to break 'em anyway, no matter
# their current tool. After all, the hardness value of cobweb can only # their current tool. After all, the hardness value of cobweb can only
# rationally explained by Steve's fear of spiders, anyway. # rationally explained by Steve's fear of spiders, anyway.
notAfraidOfSpiders: true notAfraidOfSpiders: true
@ -163,8 +163,7 @@ opencomputers {
# is the distance to the center of block the robot swings the tool in to # is the distance to the center of block the robot swings the tool in to
# the side the tool is swung towards. I.e. for the collision check, which # the side the tool is swung towards. I.e. for the collision check, which
# is performed via ray tracing, this determines the end point of the ray # is performed via ray tracing, this determines the end point of the ray
# like so: # like so: `block_center + unit_vector_towards_side * swingRange`
# `block_center + unit_vector_towards_side * swingRange`
# This defaults to a value just below 0.5 to ensure the robots will not # This defaults to a value just below 0.5 to ensure the robots will not
# hit anything that's actually outside said block. # hit anything that's actually outside said block.
swingRange: 0.49 swingRange: 0.49
@ -190,7 +189,7 @@ opencomputers {
# replaced with the name of the player that owns the robot, so for the # replaced with the name of the player that owns the robot, so for the
# first robot placed this will be the name of the player that placed it. # first robot placed this will be the name of the player that placed it.
# This is transitive, i.e. when a robot in turn places a robot, that # This is transitive, i.e. when a robot in turn places a robot, that
# robot's owner, too will be the owner of the placing robot. # robot's owner, too, will be the owner of the placing robot.
# The substring $random$ will be replaced with a random number in the # The substring $random$ will be replaced with a random number in the
# interval [1, 0xFFFFFF], which may be useful if you need to differentiate # interval [1, 0xFFFFFF], which may be useful if you need to differentiate
# individual robots. # individual robots.
@ -215,7 +214,7 @@ opencomputers {
actionXp: 0.05 actionXp: 0.05
# This determines how much "exhaustion" contributes to a robots # This determines how much "exhaustion" contributes to a robots
# experience. This is addiditve to the "action" xp, so digging a block # experience. This is additive to the "action" xp, so digging a block
# will per default give 0.05 + 0.025 [exhaustion] * 1.0 = 0.075 XP. # will per default give 0.05 + 0.025 [exhaustion] * 1.0 = 0.075 XP.
exhaustionXpRate: 1.0 exhaustionXpRate: 1.0
@ -264,8 +263,8 @@ opencomputers {
# The time in seconds to pause execution after a robot successfully # The time in seconds to pause execution after a robot successfully
# used an equipped tool (or it's 'hands' if nothing is equipped). # used an equipped tool (or it's 'hands' if nothing is equipped).
# Successful in this case means that it either used the equipped item, # Successful in this case means that it either used the equipped item,
# for example bone meal, or that it activated a block, for example by # for example a splash potion, or that it activated a block, for
# pushing a button. # example by pushing a button.
# Note that if an item is used for a specific amount of time, like # Note that if an item is used for a specific amount of time, like
# when shooting a bow, the maximum of this and the duration of the # when shooting a bow, the maximum of this and the duration of the
# item use is taken. # item use is taken.