1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-17 02:40:06 +00:00

Merge pull request #378 from MineRobber9000/patch-3

Add ability to load raw data from a string [paintutils]
This commit is contained in:
Daniel Ratcliffe 2017-09-11 13:28:27 +01:00 committed by GitHub
commit 1fdfcdb5f2

View File

@ -9,25 +9,35 @@ for n=1,16 do
tColourLookup[ string.byte( "0123456789abcdef",n,n ) ] = 2^(n-1)
end
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 )
end
function parseImage( sRawData )
if type( sRawData ) ~= "string" then
error( "bad argument #1 (expected string, got " .. type( sRawData ) .. ")" )
end
local tImage = {}
for sLine in ( sRawData .. "\n" ):gmatch( "(.-)\n" ) do -- read each line like original file handling did
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
local tImage = {}
if fs.exists( sPath ) then
local file = io.open(sPath, "r" )
local sLine = file:read()
while sLine do
local tLine = {}
for x=1,sLine:len() do
tLine[x] = tColourLookup[ string.byte(sLine,x,x) ] or 0
end
table.insert( tImage, tLine )
sLine = file:read()
end
local file = io.open( sPath, "r" )
local sContent = file:readAll()
file:close()
return tImage
return parseImage( sContent ) -- delegate image parse to parseImage
end
return nil
end