added automatic module loading system, use ignore.txt to exclude specific modules
This commit is contained in:
50
modpol_core/storage/store-modules.lua
Normal file
50
modpol_core/storage/store-modules.lua
Normal file
@ -0,0 +1,50 @@
|
||||
local lfs = require "lfs"
|
||||
|
||||
-- loads file names to ignore into a table
|
||||
function fetch_ignores(module_path)
|
||||
local ignore_list = {}
|
||||
-- checks if ignore.txt exists
|
||||
local f_test = io.open(module_path .. "/ignore.txt", "r")
|
||||
if not f_test then return {} end
|
||||
|
||||
-- puts each line of ignore.txt into the table
|
||||
local f = io.lines(module_path .. "/ignore.txt")
|
||||
for line in f do
|
||||
table.insert(ignore_list, line)
|
||||
end
|
||||
return ignore_list
|
||||
end
|
||||
|
||||
-- checks if a string is in a list
|
||||
function check_list(ignore_list, name)
|
||||
for i, v in ipairs(ignore_list) do
|
||||
if v == name then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
modpol.load_modules = function(module_path)
|
||||
local loaded = 0
|
||||
local ignored = 0
|
||||
local ignores = fetch_ignores(module_path)
|
||||
for file in lfs.dir(module_path) do
|
||||
if file == "." or file == ".." then
|
||||
-- ignoring current and parent directory
|
||||
else
|
||||
-- only looks for .lua files
|
||||
if string.sub(file, -4, -1) == ".lua" then
|
||||
|
||||
-- doesn't load files in the ignore.txt
|
||||
if check_list(ignores, file) then
|
||||
ignored = ignored + 1
|
||||
else
|
||||
dofile(module_path .. "/" .. file)
|
||||
loaded = loaded + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
print(loaded .. " modules loaded (" .. ignored .. " ignored)")
|
||||
end
|
Reference in New Issue
Block a user