1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-11-14 13:54:52 +00:00

Move raw data parsing to paintutils.drawImage

This commit is contained in:
MineRobber___T 2017-07-15 18:27:52 -04:00 committed by GitHub
parent 260bb2e4e1
commit 71d024a76c

View File

@ -9,35 +9,19 @@ for n=1,16 do
tColourLookup[ string.byte( "0123456789abcdef",n,n ) ] = 2^(n-1)
end
function loadImage( sPath, bLoadRawData )
if type( sPath ) ~= "string" then
error( "bad argument #1 (expected string, got " .. type( sPath ) .. ")", 2 )
end
if bLoadRawData ~= nil and type(bLoadRawData) ~= "boolean" then
error( "bad argument #2 (expected nil or boolean, got " .. type( sPath ) .. ")" )
end
local function parseLine( tImageArg, sLine )
local function parseLine( tImageArg, sLine )
local tLine = {}
for x=1,sLine:len() do
tLine[x] = tColourLookup[ string.byte(sLine,x,x) ] or 0
end
table.insert( tImageArg, tLine )
return tImageArg
end
end
if fs.exists( sPath ) and not bLoadRawData then
local tImage = {}
local file = io.open( sPath, "r" )
local sLine = file:read()
while sLine do
tImage = parseLine( tImage, sLine )
sLine = file:read()
function parseImage( sRawData )
if type( sRawData ) ~= "string" then
error ( "bad argument #1 (expected string, got " .. type( sRawData ) .. ")" )
end
file:close()
return tImage
else
if bLoadRawData then
local function split( str, delim ) -- modified split function from https://codea.io/talk/discussion/2118/split-a-string-by-return-newline
if string.find( str, delim ) == nil then return { str } end
local result, pat, lastpos = {}, "(.-)" .. delim .. "()", nil
@ -50,7 +34,24 @@ function loadImage( sPath, bLoadRawData )
tImage = parseLine( tImage, sLine )
end
return tImage
end
function loadImage( sPath )
if type( sPath ) ~= "string" then
error( "bad argument #1 (expected string, got " .. type( sPath ) .. ")", 2 )
end
if fs.exists( sPath ) and not bLoadRawData then
local tImage = {}
local file = io.open( sPath, "r" )
local sLine = file:read()
local sContent = ""
while sLine do
sContent = sContent .. sLine .. "\n"
sLine = file:read()
end
file:close()
return parseImage( sContent ) -- delegate parsing of images to parseImage function as suggested by @SquidDev in discussion of PR #378
end
return nil
end