Hey all! My newest project, I was trying to sneakily hide a function, and I came up with this! It saves a function into an image and can decode an image into a function!
function setup( )
getImage("https://dl.dropboxusercontent.com/s/3s8jnuew7a5rxjj/HelloWorld.png?dl=1&token_hash=AAEi-RKiHv52zHbfWYgA2oBA8kxn6w_g2ctMtrkXh5gqlw") -- comment this line and uncomment the following two lines to see how to use the functions
--[[
spr = encode(function () print("Testing Testing 1 2 3") end)
decode(spr)()
]]
end
function draw( )
background(255)
noSmooth()
if spr then sprite(spr, WIDTH / 2, HEIGHT / 2, WIDTH - 100, HEIGHT - 100) end
end
function functionToTable( f )
local dump = string.dump( f )
local chars = {}
for c in dump:gmatch( "." ) do
table.insert( chars, string.byte( c ) )
end
return chars
end
-- Referenced from code by dave1707
function encode( f )
local t = functionToTable(f)
local l = #t
local s = math.ceil(l / 3) + 1 -- + 1 for size information
local w = math.ceil(math.sqrt(s))
local h = math.ceil(s / w)
local img = image(w, h)
setContext(img)
background(0)
setContext()
local e = l % 256
local e1 = (math.floor(l / 256)) % 256
local e2 = (math.floor(l / 256 / 256)) % 256
img:set(1, 1, e2, e1, e, 255)
for i = 2, s do
local x, y = (i - 1) % w + 1, math.ceil(i / w)
local r, g, b
r = t[(i - 2) * 3 + 1] or 0
g = t[(i - 2) * 3 + 2] or 0
b = t[(i - 2) * 3 + 3] or 0
img:set(x, y, r, g, b, 255)
end
return img
end
function decode( img )
local w, h = img.width, img.height
local e2, e1, e = img:get(1, 1)
local l = e2 * 256 * 256 + e1 * 256 + e
local s = math.ceil(l / 3) + 1
local t = {}
for i = 2, s do
local x, y = (i - 1) % w + 1, math.ceil(i / w)
local r, g, b = img:get(x, y)
table.insert(t, r)
table.insert(t, g)
table.insert(t, b)
end
for i = 1, #t - l do
table.remove(t)
end
local str = ""
for i, b in ipairs(t) do
str = str .. string.char(b)
end
return loadstring(str)
end
function getImage(link)
http.request(link, gotImage)
print("Downloading function image...")
end
function gotImage(img)
print"Downloaded function image... Calling function"
spr = img
decode(spr)()
end