Created
June 15, 2018 08:38
-
-
Save neoascetic/889a59dd4bfc6d2d22f121481ba5b95f to your computer and use it in GitHub Desktop.
Simple module watcher (for Love2D, but not necessarily - just replace the mtime function)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local m = {} | |
m.mods = {} | |
function m.mtime(filepath) | |
local info = love.filesystem.getInfo(filepath) | |
return info and info.modtime | |
end | |
function m.modToPath(modname) | |
return modname:gsub('%.', '/') .. '.lua' | |
end | |
function m.proxy(modname) | |
return function(_, k) return package.loaded[modname][k] end | |
end | |
function m.require(modname) | |
m.mods[modname] = m.mtime(m.modToPath(modname)) | |
require(modname) | |
local m = {__index=m.proxy(modname)} | |
return setmetatable(m, m) | |
end | |
function m.update() | |
for mod, ts in pairs(m.mods) do | |
local fp = m.modToPath(mod) | |
local nts = m.mtime(fp) | |
if nts ~= ts then | |
m.mods[mod] = nts | |
print('Reloading ' .. mod) | |
local ok, err = pcall(function() package.loaded[mod] = dofile(fp) end) | |
if not ok then print('Error: ' .. err) end | |
end | |
end | |
end | |
return m |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment