Skip to content

Instantly share code, notes, and snippets.

@vsky279
Last active November 13, 2020 22:02
Show Gist options
  • Save vsky279/9489b1a68653d3568db4de32da9e685b to your computer and use it in GitHub Desktop.
Save vsky279/9489b1a68653d3568db4de32da9e685b to your computer and use it in GitHub Desktop.
local FILE_READ_CHUNK = 1024
local _file = file
local file_exists, file_open = _file.exists, _file.open
local node_LFS_resource = node.LFS.resource -- luacheck: ignore
local file_lfs = {}
local function file_lfs_create (filename)
local content = node_LFS_resource(filename)
local pos = 1
local read = function (_, n_or_char)
local p1, p2
if type(n_or_char) == "number" then
p1, p2 = pos, pos + n_or_char
elseif type(n_or_char) == "string" then
p1 = pos
local _
_, p2 = content:find(n_or_char, p1)
if not p2 then p2 = p1 + FILE_READ_CHUNK end
else
error("invalid parameter")
end
if p2 - p1 > FILE_READ_CHUNK then p2 = pos + FILE_READ_CHUNK end
if p1>#content then return end
pos = p2+1
return content:sub(p1, p2)
end
local seek = function (_, whence, offset)
if whence == "set" then
pos = offset
elseif whence == "cur" then
pos = pos + offset
elseif whence == "end" then
pos = #content + offset
elseif whence then -- not nil not one of above
return -- on error return nil
end
return pos
end
local obj = {}
obj.read = read
obj.readline = function(self) return read(self, "\n") end
obj.seek = seek
setmetatable(obj, {
__index = function (_, k)
return function (...) return end
end
})
return obj
end
file_lfs.exists = function (filename)
return (node_LFS_resource(filename) ~= nil) or file_exists(filename)
end
file_lfs.open = function (filename, mode)
mode = mode or "r"
if file_exists(filename) then
return file_open(filename, mode)
elseif node_LFS_resource(filename) and mode:find("r") then
return file_lfs_create (filename)
else
return file_open(filename, mode)
end
end
setmetatable(file_lfs, {
__index = function (_, k)
return function (...)
local t = ...
if type(t) == "table" then
return t[k](...)
else
return _file[k](...)
end
end
end
})
print ("[file_lfs] LFS file routines loaded")
return file_lfs
#!/usr/bin/lua
local OUT = "resource.lua"
local function readfile(file)
local f = io.open(file, "rb")
if f then
local lines = f:read("*all")
f:close()
return lines
end
end
-- tests the functions above
print(string.format("make_resource script - %d parameter(s)", #arg))
print("parameters: [-o outputfile] file1 [file2] ...")
local larg = {}
local outpar = false
for i, a in pairs(arg) do
if i>0 then
if outpar then
OUT = a
outpar = false
else
if a == "-o" then
outpar = true
else
table.insert(larg, a)
end
end
end
end
print(string.format("output set to: %s", OUT))
local res = io.open(OUT, "w")
res:write("-- luacheck: max line length no\narg = ...\n")
res:close(file)
for _, a in pairs(larg) do
inp = string.match(a, ".*[/](.*)")
local content = readfile(a)
print(string.format("# processing %s", inp))
if content then
res = io.open(OUT, "a")
res:write(('if arg == "%s" then return %q end\n\n'):format(inp, content))
res:close(file)
end
end
-- luacheck: max line length no
arg = ...
if arg == "favicon.ico" then return "\000\000\000\000\000\000\000\000h\000\000\000\000\000(\000\000\000\000\000\000 \000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000ŔŔŔ˝˝˝ĂĂĂĆĆĆ˝˝˝ĂĂĂ\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000şşş­­­ťťťOOO\000\000\000\000\000\000hhhĄĄĄ±±±ÎÎÎ\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000µµµĄĄĄ\000\000\000\000\000\000\000\000\000uuu^^^\000\000\000\000\000\000@@@´´´»»»\000\000\000\000\000\000\000\000\000±±±˘˘˘\000\000\000\000\000\000QQQ˙˙˙˙˙˙˙˙˙ĎĎĎ\000\000\000\000\000\000¶¶¶ĐĐĐ\000\000\000ÝÝÝ´´´\000\000\000\000\000\000\000\000\000ůůůÔÔÔŰŰŰÇÇÇ\000\000\000\000\000\000\000\000\000LLLŻŻŻ\000\000\000···………\000\000\000\000\000\000\000\000\000\000\000\000\r\r\r\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000­­­ÍÍÍ´´´\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000```´´´®®®\000\000\000\000\000\000\000\000\000\000\000\000†††˝˝˝¦¦¦\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000ÂÂÂ333iiiŁŁŁ\000\000\000\000\000\000\000\000\000\000\000\000YYYµµµ¦¦¦\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000ĂĂĂ333iiiááávvv\000\000\000\000\000\000\000\000\000ZZZµµµ´´´\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000```ÍÍÍĽĽĽuuuÍÍÍÂÂÂ\000\000\000\000\000\000†††ĽĽĽ···………\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000¦¦¦zzz\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000­­­ÍÍÍÝÝÝ´´´\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000¦¦¦www\000\000\000\000\000\000\000\000\000\000\000\000LLLŻŻŻ\000\000\000\000\000\000˛˛˛˘˘˘\000\000\000\000\000\000\000\000\000\000\000\000§§§vvv\000\000\000\000\000\000\000\000\000···ĐĐĐ\000\000\000\000\000\000\000\000\000µµµĄĄĄ\000\000\000\000\000\000\000\000\000”””MMM\000\000\000\000\000\000@@@µµµşşş\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000şşş­­­śśśOOO???\000\000\000hhhĄĄĄ±±±ÎÎÎ\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000ŔŔŔ˝˝˝ĂĂĂĆĆĆ˝˝˝ĂĂĂ\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000ř\000\000ŕ\000\000Ŕ\000\000€\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000€\000\000Ŕ\000\000ŕ\000\000ř\000\000" end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment