helium/loader.lua
2020-02-08 21:11:03 +02:00

88 lines
2.1 KiB
Lua

local path = string.sub(..., 1, string.len(...) - string.len(".loader"))
local helium = require(path..'.dummy')
local elements = {}
local debugLoader = {}
--Return level: 1--string; 2--chunk; 3--return value; default: element factory
local function loader(path)
local fileContents, err = love.filesystem.read(path)
local lastLoaded, status, func, succ, ret
if fileContents then
lastLoaded = love.filesystem.getInfo(path).modtime
status, func = pcall(loadstring,fileContents)
if not status then
print('Error compiling ',path,':',tostring(err),', will continue watching!')
else
succ, ret = pcall(func,path)
end
if not succ then
print('Error calling ',path,':',tostring(ret))
end
else
print('Error loading ',path,':',tostring(err),', will continue watching!')
end
return fileContents, err, ret, lastLoaded
end
debugLoader.loader = function(path,returnLevel)
local level = returnLevel or 6
if elements[path] then
return elements[path][level]
end
local setfuncs = {}
local fileContents, func, ret, lastLoaded = loader(path)
local reloader = function(setFunc)
setfuncs[#setfuncs+1] = setFunc
end
local factory = function()
return helium.element(ret, reloader)
end
elements[path] = { fileContents, func, ret, path, lastLoaded, factory, setfuncs = setfuncs }
return elements[path][level]
end
local counter = 0
function debugLoader.update(dt)
counter = counter+dt
if counter>2 then
for ind, elem in pairs(elements) do
--Get the current last save time
local t = love.filesystem.getInfo(elem[4])
local ll = t['modtime']
if ll ~= elem[5] then
--If last save time differs then start reload sequence
local _, _, ret, lastLoaded = loader(elem[4])
local setfuncs = {}
local reloader = function(setFunc)
setfuncs[#setfuncs+1] = setFunc
end
local factory = function()
return helium.element(ret, reloader)
end
elem[5] = lastLoaded
elem[6] = factory
for i, func in ipairs(elem.setfuncs) do
func(ret)
end
end
end
counter = 0
end
end
HeliumLoader = debugLoader.loader
return debugLoader