From e2da98fbf4e00a565895f7b4c2b4373518efbf31 Mon Sep 17 00:00:00 2001 From: Rahix Date: Sat, 30 Jan 2021 10:32:46 +0100 Subject: [PATCH] Fallback to os.clock() if computer.uptime() is not available To be compatible with systems that don't have computer.uptime(), use os.clock() as a fallback. The time is only used for timeout calculations. --- bc.lua | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/bc.lua b/bc.lua index fbf9d594829a..fa03de9dd7e1 100644 --- a/bc.lua +++ b/bc.lua @@ -1,5 +1,4 @@ local uuid = require("uuid") -local computer = require("computer") local bit32 = require("bit32") local Version = {0, 1} @@ -31,6 +30,18 @@ local Query = { Below = setmetatable({ty=0x424c4f, invalid=true}, query_param), } +-- Helpers ---------------------------------------------------------------- {{{ +local clock -- A monotonically increasing timer for timeout calculation +do + local status, computer = pcall(require, "computer") + if status and computer.uptime ~= nil then + clock = computer.uptime + else + clock = os.clock + end +end +-- }}} + -- Network ---------------------------------------------------------------- {{{ local Network = { default_port = bit32.bor(0xBC00, bit32.lshift(Version[1], 4), Version[2]), @@ -190,11 +201,11 @@ function BaseControl:finalize(waits, timeout) end end - local tstart = computer.uptime() + local tstart = clock() local timeout_remaining = timeout while num_remaining > 0 do if timeout ~= nil then - timeout_remaining = timeout - (computer.uptime() - tstart) + timeout_remaining = timeout - (clock() - tstart) if timeout_remaining <= 0 then error("timeout") end