made modload more failsafe
This commit is contained in:
@@ -3,7 +3,17 @@ local fs = require("filesystem")
|
|||||||
|
|
||||||
local modulePath = "/halyde/kernel/modules"
|
local modulePath = "/halyde/kernel/modules"
|
||||||
|
|
||||||
local moduleList = assert(fs.list(modulePath))
|
if not (fs.exists(modulePath) or fs.isDirectory(modulePath)) then
|
||||||
|
return log.kernel.warn(
|
||||||
|
string.format("Module directory (%s) does not exist and/or has been detected as a file - skipping", modulePath)
|
||||||
|
)
|
||||||
|
end
|
||||||
|
local moduleList, err = fs.list(modulePath)
|
||||||
|
if not moduleList then
|
||||||
|
return log.kernel.warn(
|
||||||
|
string.format("Could not get list of modules (from %s): %s", modulePath, tostring(err or "unknown error"))
|
||||||
|
)
|
||||||
|
end
|
||||||
local modules = {}
|
local modules = {}
|
||||||
local moduleTypes = {}
|
local moduleTypes = {}
|
||||||
|
|
||||||
@@ -43,13 +53,38 @@ end
|
|||||||
|
|
||||||
for _, modName in pairs(moduleList) do -- Get all the module types
|
for _, modName in pairs(moduleList) do -- Get all the module types
|
||||||
log.kernel.info(string.format("[modload: %s] Getting data from module", modName))
|
log.kernel.info(string.format("[modload: %s] Getting data from module", modName))
|
||||||
local moduleData = require(fs.concat(modulePath, modName)) -- TODO: Make this not actually throw an error, rather put something in the log and move on
|
local moduleData
|
||||||
|
local status, err = pcall(function()
|
||||||
|
moduleData = require(fs.concat(modulePath, modName)) -- TODO: Make this not actually throw an error, rather put something in the log and move on
|
||||||
|
end)
|
||||||
|
if not status then
|
||||||
|
log.kernel.error(
|
||||||
|
string.format(
|
||||||
|
"[modload: %s] Module returned error while getting data: %s",
|
||||||
|
modName,
|
||||||
|
tostring(err or "unknown error")
|
||||||
|
)
|
||||||
|
)
|
||||||
|
goto continue
|
||||||
|
end
|
||||||
if type(moduleData) ~= "table" then
|
if type(moduleData) ~= "table" then
|
||||||
log.kernel.error(
|
log.kernel.error(
|
||||||
string.format("[modload: %s] Module returned invalid type (%s) - skipping", modName, type(moduleData))
|
string.format("[modload: %s] Module returned invalid type (%s) - skipping", modName, type(moduleData))
|
||||||
)
|
)
|
||||||
goto continue
|
goto continue
|
||||||
end
|
end
|
||||||
|
if type(moduleData.check) ~= "function" then
|
||||||
|
log.kernel.error(string.format('[modload: %s] Module doesn\'t contain a "check" function', modName))
|
||||||
|
goto continue
|
||||||
|
end
|
||||||
|
if type(moduleData.init) ~= "function" then
|
||||||
|
log.kernel.error(string.format('[modload: %s] Module doesn\'t contain an "init" function', modName))
|
||||||
|
goto continue
|
||||||
|
end
|
||||||
|
if type(moduleData.exit) ~= "function" then
|
||||||
|
log.kernel.error(string.format('[modload: %s] Module doesn\'t contain an "exit" function', modName))
|
||||||
|
goto continue
|
||||||
|
end
|
||||||
modules[modName] = moduleData
|
modules[modName] = moduleData
|
||||||
if moduleData.type then
|
if moduleData.type then
|
||||||
--print(moduleData.type)
|
--print(moduleData.type)
|
||||||
|
|||||||
Reference in New Issue
Block a user