Sets up a Lua package loader to check for table files when require is called. It checks for "[module].lua" and "[module].init.lua" table files. If
RELEASE_MODE
is true then it will add the file searcher function to the front of the Lua searchers list after the preload searcher, thus table files will be loaded first. Else it places the searcher function at the end of the list, thus local files will be loaded first.Uses a simple format, local file "mymodule\Shape.lua" (required as "mymodule.Shape") needs to be saved as a table file named "mymodule.Shape.lua". An archive containing a table with some helper functions is included in this post; for removing all, packing, and repacking table files. And the helpers will store lua files with the right name formats. Archive includes the files and folders of the packed files to help show the module setups.
Code: Select all
---- 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
With the above code ran in the table lua script you can then require files like normal.
Code: Select all
require('mypackage.Square')
local squ = Square({Name = 'someSquare', Width = 120, Height = 800})
print('Name:', squ.Name)
print('Area:', squ:getArea())
print('Width:', squ.Width)
print('Height:', squ.Height)
require('NO_MODULE')
(and the module doesn't exist) and RELEASE_MODE is false. Then you'll get an output like this.
Code: Select all
Error:[string "require('NO_MODULE')
..."]:1: module 'NO_MODULE' not found:
no field package.preload['NO_MODULE']
no file '[Cheat Engine Folder]\lua\NO_MODULE.lua'
...
no file '.\clibs64\NO_MODULE.dll'
no table file 'NO_MODULE.lua'
no table file 'NO_MODULE.init.lua'
Script Error
Code: Select all
Error:[string "require('NO_MODULE')
..."]:1: module 'NO_MODULE' not found:
no field package.preload['NO_MODULE']
no table file 'NO_MODULE.lua'
no table file 'NO_MODULE.init.lua'
no file '[Cheat Engine Folder]\lua\NO_MODULE.lua'
...
no file '.\clibs64\NO_MODULE.dll'
Script Error