1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-24 06:03:28 +00:00

Use Lua style error messages in the rom files

This makes it mostly consistent with the Java APIs, and makes debugging
significantly easier.
This commit is contained in:
SquidDev 2017-06-12 10:54:44 +01:00
parent bffc3c18cc
commit fac625173a
6 changed files with 99 additions and 99 deletions

View File

@ -45,6 +45,6 @@ function rgb8( r, g, b )
bit32.lshift( bit32.band(g * 255, 0xFF), 8 ) +
bit32.band(b * 255, 0xFF)
else
error( "Expected 1 or 3 numbers" )
error( "Expected 1 or 3 numbers", 2 )
end
end

View File

@ -11,7 +11,7 @@ end
function loadImage( sPath )
if type( sPath ) ~= "string" then
error( "Expected path", 2 )
error( "bad argument #1 (expected string, got " .. type( sPath ) .. ")", 2 )
end
local tImage = {}
@ -33,9 +33,9 @@ function loadImage( sPath )
end
function drawPixel( xPos, yPos, nColour )
if type( xPos ) ~= "number" or type( yPos ) ~= "number" or (nColour ~= nil and type( nColour ) ~= "number") then
error( "Expected x, y, colour", 2 )
end
if type( xPos ) ~= "number" then error( "bad argument #1 (expected number, got " .. type( xPos ) .. ")", 2 ) end
if type( yPos ) ~= "number" then error( "bad argument #2 (expected number, got " .. type( yPos ) .. ")", 2 ) end
if nColour ~= nil and type( nColour ) ~= "number" then error( "bad argument #3 (expected number, got " .. type( nColour ) .. ")", 2 ) end
if nColour then
term.setBackgroundColor( nColour )
end
@ -43,11 +43,11 @@ function drawPixel( xPos, yPos, nColour )
end
function drawLine( startX, startY, endX, endY, nColour )
if type( startX ) ~= "number" or type( startX ) ~= "number" or
type( endX ) ~= "number" or type( endY ) ~= "number" or
(nColour ~= nil and type( nColour ) ~= "number") then
error( "Expected startX, startY, endX, endY, colour", 2 )
end
if type( startX ) ~= "number" then error( "bad argument #1 (expected number, got " .. type( startX ) .. ")", 2 ) end
if type( startY ) ~= "number" then error( "bad argument #2 (expected number, got " .. type( startY ) .. ")", 2 ) end
if type( endX ) ~= "number" then error( "bad argument #3 (expected number, got " .. type( endX ) .. ")", 2 ) end
if type( endY ) ~= "number" then error( "bad argument #4 (expected number, got " .. type( endY ) .. ")", 2 ) end
if nColour ~= nil and type( nColour ) ~= "string" then error( "bad argument #5 (expected number, got " .. type( nColour ) .. ")", 2 ) end
startX = math.floor(startX)
startY = math.floor(startY)
@ -103,11 +103,11 @@ function drawLine( startX, startY, endX, endY, nColour )
end
function drawBox( startX, startY, endX, endY, nColour )
if type( startX ) ~= "number" or type( startX ) ~= "number" or
type( endX ) ~= "number" or type( endY ) ~= "number" or
(nColour ~= nil and type( nColour ) ~= "number") then
error( "Expected startX, startY, endX, endY, colour", 2 )
end
if type( startX ) ~= "number" then error( "bad argument #1 (expected number, got " .. type( startX ) .. ")", 2 ) end
if type( startY ) ~= "number" then error( "bad argument #2 (expected number, got " .. type( startY ) .. ")", 2 ) end
if type( endX ) ~= "number" then error( "bad argument #3 (expected number, got " .. type( endX ) .. ")", 2 ) end
if type( endY ) ~= "number" then error( "bad argument #4 (expected number, got " .. type( endY ) .. ")", 2 ) end
if nColour ~= nil and type( nColour ) ~= "string" then error( "bad argument #5 (expected number, got " .. type( nColour ) .. ")", 2 ) end
startX = math.floor(startX)
startY = math.floor(startY)
@ -147,11 +147,11 @@ function drawBox( startX, startY, endX, endY, nColour )
end
function drawFilledBox( startX, startY, endX, endY, nColour )
if type( startX ) ~= "number" or type( startX ) ~= "number" or
type( endX ) ~= "number" or type( endY ) ~= "number" or
(nColour ~= nil and type( nColour ) ~= "number") then
error( "Expected startX, startY, endX, endY, colour", 2 )
end
if type( startX ) ~= "number" then error( "bad argument #1 (expected number, got " .. type( startX ) .. ")", 2 ) end
if type( startY ) ~= "number" then error( "bad argument #2 (expected number, got " .. type( startY ) .. ")", 2 ) end
if type( endX ) ~= "number" then error( "bad argument #3 (expected number, got " .. type( endX ) .. ")", 2 ) end
if type( endY ) ~= "number" then error( "bad argument #4 (expected number, got " .. type( endY ) .. ")", 2 ) end
if nColour ~= nil and type( nColour ) ~= "string" then error( "bad argument #5 (expected number, got " .. type( nColour ) .. ")", 2 ) end
startX = math.floor(startX)
startY = math.floor(startY)
@ -185,9 +185,9 @@ function drawFilledBox( startX, startY, endX, endY, nColour )
end
function drawImage( tImage, xPos, yPos )
if type( tImage ) ~= "table" or type( xPos ) ~= "number" or type( yPos ) ~= "number" then
error( "Expected image, x, y", 2 )
end
if type( tImage ) ~= "table" then error( "bad argument #1 (expected number, got " .. type( tImage ) .. ")", 2 ) end
if type( xPos ) ~= "number" then error( "bad argument #2 (expected number, got " .. type( xPos ) .. ")", 2 ) end
if type( yPos ) ~= "number" then error( "bad argument #3 (expected number, got " .. type( yPos ) .. ")", 2 ) end
for y=1,#tImage do
local tLine = tImage[y]
for x=1,#tLine do

View File

@ -1,12 +1,17 @@
local function create( first, ... )
if first ~= nil then
if type( first ) ~= "function" then
error( "Expected function, got "..type( first ), 3 )
end
return coroutine.create(first), create( ... )
local function create( ... )
local tFns = table.pack(...)
local tCos = {}
for i = 1, tFns.n, 1 do
local fn = tFns[i]
if type( fn ) ~= "function" then
error( "bad argument #" .. i .. " (expected function, got " .. type( fn ) .. ")", 3 )
end
tCos[i] = coroutine.create(fn)
end
return nil
return tCos
end
local function runUntilLimit( _routines, _limit )
@ -51,11 +56,11 @@ local function runUntilLimit( _routines, _limit )
end
function waitForAny( ... )
local routines = { create( ... ) }
local routines = create( ... )
return runUntilLimit( routines, #routines - 1 )
end
function waitForAll( ... )
local routines = { create( ... ) }
local routines = create( ... )
runUntilLimit( routines, 0 )
end

View File

@ -18,7 +18,7 @@ end
function isPresent( _sSide )
if type( _sSide ) ~= "string" then
error( "Expected string", 2 )
error( "bad argument #1 (expected string, got " .. type( _sSide ) .. ")", 2 )
end
if native.isPresent( _sSide ) then
return true
@ -35,7 +35,7 @@ end
function getType( _sSide )
if type( _sSide ) ~= "string" then
error( "Expected string", 2 )
error( "bad argument #1 (expected string, got " .. type( _sSide ) .. ")", 2 )
end
if native.isPresent( _sSide ) then
return native.getType( _sSide )
@ -52,7 +52,7 @@ end
function getMethods( _sSide )
if type( _sSide ) ~= "string" then
error( "Expected string", 2 )
error( "bad argument #1 (expected string, got " .. type( _sSide ) .. ")", 2 )
end
if native.isPresent( _sSide ) then
return native.getMethods( _sSide )
@ -68,8 +68,11 @@ function getMethods( _sSide )
end
function call( _sSide, _sMethod, ... )
if type( _sSide ) ~= "string" or type( _sMethod ) ~= "string" then
error( "Expected string, string", 2 )
if type( _sSide ) ~= "string" then
error( "bad argument #1 (expected string, got " .. type( _sSide ) .. ")", 2 )
end
if type( _sSide ) ~= "string" then
error( "bad argument #2 (expected string, got " .. type( _sMethod ) .. ")", 2 )
end
if native.isPresent( _sSide ) then
return native.call( _sSide, _sMethod, ... )
@ -85,8 +88,8 @@ function call( _sSide, _sMethod, ... )
end
function wrap( _sSide )
if type( _sSide ) ~= "string" then
error( "Expected string", 2 )
if type( _sSide ) ~= "string" then
error( "bad argument #1 (expected string, got " .. type( _sSide ) .. ")", 2 )
end
if peripheral.isPresent( _sSide ) then
local tMethods = peripheral.getMethods( _sSide )
@ -102,8 +105,11 @@ function wrap( _sSide )
end
function find( sType, fnFilter )
if type( sType ) ~= "string" or (fnFilter ~= nil and type( fnFilter ) ~= "function") then
error( "Expected string, [function]", 2 )
if type( _sSide ) ~= "string" then
error( "bad argument #1 (expected string, got " .. type( _sSide ) .. ")", 2 )
end
if fnFilter ~= nil and type( fnFilter ) ~= "string" then
error( "bad argument #2 (expected function, got " .. type( fnFilter ) .. ")", 2 )
end
local tResults = {}
for n,sName in ipairs( peripheral.getNames() ) do

View File

@ -2,11 +2,13 @@
local tSettings = {}
function set( sName, value )
if type(sName) ~= "string" or
(type(value) ~= "string" and type(value) ~= "number" and type(value) ~= "boolean" and type(value) ~= "table") then
error( "Expected string, value", 2 )
if type( sName ) ~= "string" then error( "bad argument #1 (expected string, got " .. type( sName ) .. ")", 2 ) end
local sValueTy = type(value)
if sValueTy ~= "number" and sValueTy ~= "string" and sValueTy ~= "boolean" and sValueTy ~= "table" then
error( "bad argument #2 (expected value, got " .. sValueTy .. ")", 2 )
end
if type(value) == "table" then
if sValueTy == "table" then
-- Ensure value is serializeable
value = textutils.unserialize( textutils.serialize(value) )
end
@ -28,7 +30,7 @@ end
function get( sName, default )
if type(sName) ~= "string" then
error( "Expected string", 2 )
error( "bad argument #1 (expected string, got " .. type( sName ) .. ")", 2 )
end
local result = tSettings[ sName ]
if result ~= nil then
@ -40,7 +42,7 @@ end
function unset( sName )
if type(sName) ~= "string" then
error( "Expected string", 2 )
error( "bad argument #1 (expected string, got " .. type( sName ) .. ")", 2 )
end
tSettings[ sName ] = nil
end
@ -59,7 +61,7 @@ end
function load( sPath )
if type(sPath) ~= "string" then
error( "Expected string", 2 )
error( "bad argument #1 (expected string, got " .. type( sPath ) .. ")", 2 )
end
local file = fs.open( sPath, "r" )
if not file then
@ -86,7 +88,7 @@ end
function save( sPath )
if type(sPath) ~= "string" then
error( "Expected string", 2 )
error( "bad argument #1 (expected string, got " .. type( sPath ) .. ")", 2 )
end
local file = fs.open( sPath, "w" )
if not file then

View File

@ -18,20 +18,18 @@ local tHex = {
[ colors.black ] = "f",
}
local type = type
local string_rep = string.rep
local string_sub = string.sub
local table_unpack = table.unpack
function create( parent, nX, nY, nWidth, nHeight, bStartVisible )
if type( parent ) ~= "table" or
type( nX ) ~= "number" or
type( nY ) ~= "number" or
type( nWidth ) ~= "number" or
type( nHeight ) ~= "number" or
(bStartVisible ~= nil and type( bStartVisible ) ~= "boolean") then
error( "Expected object, number, number, number, number, [boolean]", 2 )
end
if type( parent ) ~= "table" then error( "bad argument #1 (expected table, got " .. type( parent ) .. ")", 2 ) end
if type( nX ) ~= "number" then error( "bad argument #2 (expected number, got " .. type( nX ) .. ")", 2 ) end
if type( nY ) ~= "number" then error( "bad argument #3 (expected number, got " .. type( nY ) .. ")", 2 ) end
if type( nWidth ) ~= "number" then error( "bad argument #4 (expected number, got " .. type( nWidth ) .. ")", 2 ) end
if type( nHeight ) ~= "number" then error( "bad argument #5 (expected number, got " .. type( nHeight ) .. ")", 2 ) end
if bStartVisible ~= nil and type( bStartVisible ) ~= "boolean" then error( "bad argument #6 (expected boolean, got " .. type( bStartVisible ) .. ")", 2 ) end
if parent == term then
error( "term is not a recommended window parent, try term.current() instead", 2 )
@ -193,9 +191,9 @@ function create( parent, nX, nY, nWidth, nHeight, bStartVisible )
end
function window.blit( sText, sTextColor, sBackgroundColor )
if type(sText) ~= "string" or type(sTextColor) ~= "string" or type(sBackgroundColor) ~= "string" then
error( "Expected string, string, string", 2 )
end
if type( sText ) ~= "string" then error( "bad argument #1 (expected string, got " .. type( sText ) .. ")", 2 ) end
if type( sTextColor ) ~= "string" then error( "bad argument #2 (expected string, got " .. type( sTextColor ) .. ")", 2 ) end
if type( sBackgroundColor ) ~= "string" then error( "bad argument #3 (expected string, got " .. type( sBackgroundColor ) .. ")", 2 ) end
if #sTextColor ~= #sText or #sBackgroundColor ~= #sText then
error( "Arguments must be the same length", 2 )
end
@ -243,9 +241,8 @@ function create( parent, nX, nY, nWidth, nHeight, bStartVisible )
end
function window.setCursorPos( x, y )
if type( x ) ~= "number" or type( y ) ~= "number" then
error( "Expected number, number", 2 )
end
if type( x ) ~= "number" then error( "bad argument #1 (expected number, got " .. type( x ) .. ")", 2 ) end
if type( y ) ~= "number" then error( "bad argument #2 (expected number, got " .. type( y ) .. ")", 2 ) end
nCursorX = math.floor( x )
nCursorY = math.floor( y )
if bVisible then
@ -254,9 +251,7 @@ function create( parent, nX, nY, nWidth, nHeight, bStartVisible )
end
function window.setCursorBlink( blink )
if type( blink ) ~= "boolean" then
error( "Expected boolean", 2 )
end
if type( blink ) ~= "boolean" then error( "bad argument #1 (expected boolean, got " .. type( blink ) .. ")", 2 ) end
bCursorBlink = blink
if bVisible then
updateCursorBlink()
@ -276,10 +271,10 @@ function create( parent, nX, nY, nWidth, nHeight, bStartVisible )
end
local function setTextColor( color )
if type(color) ~= "number" then
error( "Expected number", 3 )
if type( color ) ~= "number" then
error( "bad argument #1 (expected number, got " .. type( color ) .. ")", 2 )
elseif tHex[color] == nil then
error( "Invalid color", 3 )
error( "Invalid color", 2 )
end
nTextColor = color
if bVisible then
@ -287,26 +282,25 @@ function create( parent, nX, nY, nWidth, nHeight, bStartVisible )
end
end
function window.setTextColor( color )
setTextColor( color )
end
function window.setTextColour( color )
setTextColor( color )
end
window.setTextColor = setTextColor
window.setTextColour = setTextColor
function window.setPaletteColour( colour, r, g, b )
if type( colour ) ~= "number" then error( "bad argument #1 (expected number, got " .. type( colour ) .. ")", 2 ) end
local tCol
if type(colour) == "number" and type(r) == "number" and g == nil and b == nil then
if type(r) == "number" and g == nil and b == nil then
tCol = { colours.rgb8( r ) }
tPalette[ colour ] = tCol
elseif type(colour) == "number" and type(r) == "number" and type(g) == "number" and type(b) == "number" then
else
if type( r ) ~= "number" then error( "bad argument #2 (expected number, got " .. type( r ) .. ")", 2 ) end
if type( g ) ~= "number" then error( "bad argument #3 (expected number, got " .. type( g ) .. ")", 2 ) end
if type( b ) ~= "number" then error( "bad argument #4 (expected number, got " .. type( b ) .. ")", 2 ) end
tCol = tPalette[ colour ]
tCol[1] = r
tCol[2] = g
tCol[3] = b
else
error( "Expected number, number, number, number", 2 )
end
if bVisible then
@ -324,30 +318,23 @@ function create( parent, nX, nY, nWidth, nHeight, bStartVisible )
window.getPaletteColor = window.getPaletteColour
local function setBackgroundColor( color )
if type(color) ~= "number" then
error( "Expected number", 3 )
if type( color ) ~= "number" then
error( "bad argument #1 (expected number, got " .. type( color ) .. ")", 2 )
elseif tHex[color] == nil then
error( "Invalid color", 3 )
end
nBackgroundColor = color
end
function window.setBackgroundColor( color )
setBackgroundColor( color )
end
function window.setBackgroundColour( color )
setBackgroundColor( color )
end
window.setBackgroundColor = setBackgroundColor
window.setBackgroundColour = setBackgroundColor
function window.getSize()
return nWidth, nHeight
end
function window.scroll( n )
if type( n ) ~= "number" then
error( "Expected number", 2 )
end
if type( n ) ~= "number" then error( "bad argument #1 (expected number, got " .. type( n ) .. ")", 2 ) end
if n ~= 0 then
local tNewLines = {}
local sEmptyText = sEmptySpaceLine
@ -392,9 +379,7 @@ function create( parent, nX, nY, nWidth, nHeight, bStartVisible )
-- Other functions
function window.setVisible( bVis )
if type( bVis) ~= "boolean" then
error( "Expected boolean", 2 )
end
if type( bVis ) ~= "boolean" then error( "bad argument #1 (expected boolean, got " .. type( bVis ) .. ")", 2 ) end
if bVisible ~= bVis then
bVisible = bVis
if bVisible then
@ -426,9 +411,11 @@ function create( parent, nX, nY, nWidth, nHeight, bStartVisible )
end
function window.reposition( nNewX, nNewY, nNewWidth, nNewHeight )
if type( nNewX ) ~= "number" or type( nNewY ) ~= "number" or type( nNewWidth ) ~= "number" or type( nNewWidth ) ~= "number" then
error( "Expected number, number, number, number", 2 )
end
if type( nNewX ) ~= "number" then error( "bad argument #1 (expected number, got " .. type( nNewX ) .. ")", 2 ) end
if type( nNewY ) ~= "number" then error( "bad argument #2 (expected number, got " .. type( nNewY ) .. ")", 2 ) end
if type( nNewWidth ) ~= "number" then error( "bad argument #3 (expected number, got " .. type( nNewWidth ) .. ")", 2 ) end
if type( nNewHeight ) ~= "number" then error( "bad argument #4 (expected number, got " .. type( nNewHeight ) .. ")", 2 ) end
nX = nNewX
nY = nNewY
if nNewWidth and nNewHeight then