mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2024-06-26 07:03:22 +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:
commit
1fdfcdb5f2
|
@ -9,25 +9,35 @@ for n=1,16 do
|
||||||
tColourLookup[ string.byte( "0123456789abcdef",n,n ) ] = 2^(n-1)
|
tColourLookup[ string.byte( "0123456789abcdef",n,n ) ] = 2^(n-1)
|
||||||
end
|
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 )
|
function loadImage( sPath )
|
||||||
if type( sPath ) ~= "string" then
|
if type( sPath ) ~= "string" then
|
||||||
error( "bad argument #1 (expected string, got " .. type( sPath ) .. ")", 2 )
|
error( "bad argument #1 (expected string, got " .. type( sPath ) .. ")", 2 )
|
||||||
end
|
end
|
||||||
|
|
||||||
local tImage = {}
|
|
||||||
if fs.exists( sPath ) then
|
if fs.exists( sPath ) then
|
||||||
local file = io.open(sPath, "r" )
|
local file = io.open( sPath, "r" )
|
||||||
local sLine = file:read()
|
local sContent = file:readAll()
|
||||||
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
|
|
||||||
file:close()
|
file:close()
|
||||||
return tImage
|
return parseImage( sContent ) -- delegate image parse to parseImage
|
||||||
end
|
end
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue
Block a user