1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-28 08:03:21 +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.lshift( bit32.band(g * 255, 0xFF), 8 ) +
bit32.band(b * 255, 0xFF) bit32.band(b * 255, 0xFF)
else else
error( "Expected 1 or 3 numbers" ) error( "Expected 1 or 3 numbers", 2 )
end end
end end

View File

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

View File

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

View File

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

View File

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

View File

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