Skip to content

Instantly share code, notes, and snippets.

@pfirsich
Created February 22, 2015 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pfirsich/549fc8cb38949feef665 to your computer and use it in GitHub Desktop.
Save pfirsich/549fc8cb38949feef665 to your computer and use it in GitHub Desktop.
Package loader for tweakables without "name"-argument, discarded for SudoHack
do
local compiler = function(filename, codeString)
local counter = 0
filename = filename:gsub("\\", "/")
codeString = codeString:gsub("func%(",
function()
counter = counter + 1
return 'func("' .. filename .. "_" .. tostring(counter) .. '"'
end)
print(codeString)
return codeString
end
local loader = function(modulename)
local modulepath = modulename:gsub("%.", "/")
errmsg = ""
for path in string.gmatch(package.mondpath, "(.-);") do
local filename = string.gsub(path, "%?", modulepath)
local file = io.open(filename, "rb")
if file then -- if this is used for .lua-files (not .mond) the function should throw an error if file is nil
return assert(loadstring(compiler(filename, assert(file:read("*a"))), filename))
end
errmsg = errmsg .. "\n\t Couldn't find module named " .. modulename
end
return errmsg == "" and nil or errmsg
end
-- substitute package.path once and reuse for every require
if not package.mondpath then
package.mondpath = package.path
--package.mondpath = ""
--for path in string.gmatch(package.path, "(.-)%.lua;") do
-- package.mondpath = package.mondpath .. path .. ".mond;"
--end
end
-- only add loader if not already added
for i = 1, #package.loaders do
if package.loaders[i] == loader then
return false
end
end
-- add just before the default loader
table.insert(package.loaders, 2, loader)
return true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment