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 


Using require/cheat table files

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
jgoemat
Master Cheater
Reputation: 22

Joined: 25 Sep 2011
Posts: 252

PostPosted: Wed Jun 06, 2018 4:44 pm    Post subject: Using require/cheat table files Reply with quote

Doing a lot of lua coding can be tedious with the one table script and CE's editor. Is there a better way available for using packages that I don't know about?

For now I use this code and call 'cerequire()'. It defaults to a regular `require()` so I can use separate files in my table directory for packages. When I want to deploy I just have to add those files to CE and it will fall back to looking for them there... I also have the function 'clearLoadedPackages()', So my table script looks like this:

Code:
function cerequire(packageName)
  local success, result = pcall(require, packageName)
  if success then return result end

  -- not found, scan table files for '{packageName}.lua'
  local file = findTableFile(packageName..".lua")

  -- if no table file, throw error we got from require()
  if file == nil then error(result) end

  -- get file as string
  local data = file.getData()
  local ss = createStringStream()
  ss.copyFrom(data, data.Size)
  local s = ss.DataString
  ss.Destroy()

  -- process using loadstring
  local pkg = loadstring(s)()

  -- add to packages.loaded and require again
  package.loaded[packageName] = pkg
  return require(packageName)
end

function clearLoadedPackages(...)
  for i,packageName in ipairs({...}) do package.loaded[packageName] = nil end
end

clearLoadedPackages('Main')
cerequire('Main')


And my 'Main.lua' file looks like this:

Code:

-- for development so changes to the files show up
clearLoadedPackages('Address', 'Value')

Address = cerequire('Address')
Value = cerequire('Value')

-- ... lots of other stuff ... --




FYI I use ZeroBraneStudio for editing LUA, I just wish it had syntax highlighting for CE functions and objects Smile
Back to top
View user's profile Send private message
TheyCallMeTim13
Wiki Contributor
Reputation: 50

Joined: 24 Feb 2017
Posts: 976
Location: Pluto

PostPosted: Sat Jul 21, 2018 5:41 am    Post subject: Reply with quote

As far as I know there really isn't an easier way.

This is the function I use, it looks in the current directory for a folder ("luaFiles") and loads that file if found, if not found it looks for it in the table files.

Code:
--------
-------- CE Table Require
--------
local TableLuaFilesDirectory = 'luaFiles'
function CETrequire(moduleStr)
   if moduleStr ~= nil then
      local localTableLuaFilePath = moduleStr
      if TableLuaFilesDirectory ~= nil or TableLuaFilesDirectory ~= '' then
         local sep = package.config:sub(1,1)
         localTableLuaFilePath = TableLuaFilesDirectory .. sep .. moduleStr
      end
      local f, err = io.open(localTableLuaFilePath .. '.lua')
      if f and not err then
         f:close()
         return require(localTableLuaFilePath)
      else
         local tableFile = findTableFile(moduleStr .. '.lua')
         if tableFile == nil then
            return nil
         end
         local stream = tableFile.getData()
         local fileStr = nil
         local bytes = stream.read(stream.Size)
         for i = 1, #bytes do
            if fileStr == nil then
               fileStr = ''
            end
            fileStr = fileStr .. string.char(bytes[i])
         end
         if fileStr then
            return assert(loadstring(fileStr))()
         end
      end
   end
   return nil
end
-- CETrequire('I2CETableCEA')
-- CETrequire('SomeLuaModuleStoredAsTableFile')
-- SomeLuaModuleStoredAsTableFile.printTest() -- Just an example.


P.S.: Most code editors let you create language definitions for them, I did that for Sublime Text and created a CE Lua language and a CE CEA language. Just see if you can find the default Lua one and modify a copy to create a new one.

_________________
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 Lua Scripting 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