In this video I go over using Lua modules as table files.
Code: Select all
-- Loaded in table lua script
-- Table File Package Loader
function tableFilePackageLoader(name, modPath)
local tableFile = findTableFile(modPath)
if tableFile then
local stream = tableFile.getData()
if stream then
local fileStr = readStringLocal(stream.memory, stream.size)
if fileStr then
local status, ret = pcall(load, fileStr, modPath)
if not status then error(ret, 2) end
return ret(name, modPath)
end
end
end
error(string.format('Error loading table module: %s : %s', name, modPath), 2)
end
function tableFilePackageSearcher(name)
local err = ''
local mod_paths = { name..'.lua', name..'.init.lua' }
local tableFile, modPath
for i, path in ipairs(mod_paths) do
tableFile = findTableFile(path)
if tableFile then
modPath = path
break
else
err = err..string.format("\n\tno table file '%s'", path)
end
end
if tableFile and modPath then
return tableFilePackageLoader, modPath
end
return err
end
if not TABLE_LUA_PACKAGE_SEARCHER_LOADED then
if RELEASE_MODE then
table.insert(package.searchers, 2, tableFilePackageSearcher)
else
table.insert(package.searchers, tableFilePackageSearcher)
end
TABLE_LUA_PACKAGE_SEARCHER_LOADED = true
end
---- Bypass require for better formated outputs
if not REQUIRE_BYPASS_LOADED then
local _require = require
function require(name)
if REREQUIRE or RerequireList and RerequireList[name] then
package.loaded[name] = false
end
local status, ret = pcall(_require, name)
if status then
return ret
else
local err = ret:gsub('\n', '\r\n'):gsub('.dllC:\\', ".dll'\r\n\tno file 'C:\\")
error(err, 2)
end
end
REQUIRE_BYPASS_LOADED = true
end