Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Lua Table File Package Loader

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Extensions
View previous topic :: View next topic  
Author Message
TheyCallMeTim13
Wiki Contributor
Reputation: 50

Joined: 24 Feb 2017
Posts: 976
Location: Pluto

PostPosted: Mon Dec 20, 2021 9:57 pm    Post subject: Lua Table File Package Loader This post has 1 review(s) Reply with quote

Lua Table File Package Loader

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 [c]RELEASE_MODE[/c] 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.
Can't post archives so go here to download

Code:

---- 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:

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)


And if you run "require('NO_MODULE')" (and the module doesn't exist) and RELEASE_MODE is false. Then you'll get an output like this.
Code:
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


But if RELEASE_MODE is true then it will look like this.
Code:
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

_________________
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Extensions All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites