mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-03-04 18:48:12 +00:00
Add ability to load raw data from a string
This commit is contained in:
parent
94d701b1f7
commit
260bb2e4e1
@ -9,25 +9,48 @@ 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
|
||||||
|
|
||||||
function loadImage( sPath )
|
function loadImage( sPath, bLoadRawData )
|
||||||
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
|
||||||
|
if bLoadRawData ~= nil and type(bLoadRawData) ~= "boolean" then
|
||||||
|
error( "bad argument #2 (expected nil or boolean, got " .. type( sPath ) .. ")" )
|
||||||
|
end
|
||||||
|
|
||||||
local tImage = {}
|
local function parseLine( tImageArg, sLine )
|
||||||
if fs.exists( sPath ) then
|
local tLine = {}
|
||||||
local file = io.open(sPath, "r" )
|
for x=1,sLine:len() do
|
||||||
|
tLine[x] = tColourLookup[ string.byte(sLine,x,x) ] or 0
|
||||||
|
end
|
||||||
|
table.insert( tImageArg, tLine )
|
||||||
|
return tImageArg
|
||||||
|
end
|
||||||
|
|
||||||
|
if fs.exists( sPath ) and not bLoadRawData then
|
||||||
|
local tImage = {}
|
||||||
|
local file = io.open( sPath, "r" )
|
||||||
local sLine = file:read()
|
local sLine = file:read()
|
||||||
while sLine do
|
while sLine do
|
||||||
local tLine = {}
|
tImage = parseLine( tImage, sLine )
|
||||||
for x=1,sLine:len() do
|
|
||||||
tLine[x] = tColourLookup[ string.byte(sLine,x,x) ] or 0
|
|
||||||
end
|
|
||||||
table.insert( tImage, tLine )
|
|
||||||
sLine = file:read()
|
sLine = file:read()
|
||||||
end
|
end
|
||||||
file:close()
|
file:close()
|
||||||
return tImage
|
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
|
||||||
|
for part, pos in string.gfind( str, pat ) do table.insert( result, part ); lastpos = pos; end
|
||||||
|
table.insert( result, string.sub( str, lastpos ) )
|
||||||
|
return result
|
||||||
|
end
|
||||||
|
local tImage = {}
|
||||||
|
for _, sLine in pairs( split( sPath, "\n" ) ) do -- read each line like original file handling did
|
||||||
|
tImage = parseLine( tImage, sLine )
|
||||||
|
end
|
||||||
|
return tImage
|
||||||
|
end
|
||||||
end
|
end
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user