Difference between revisions of "Lua:Class:TableFile"

From Cheat Engine
Jump to navigation Jump to search
(Major overhaul of the post.)
 
(9 intermediate revisions by one other user not shown)
Line 1: Line 1:
TableFile '''class''': ('''Inheritance''': ''[[Object]]'')
+
[[Category:Lua]]
 +
{{Class|'''class''' TableFile ''':''' Object}}
  
Class for Cheat Engine Table Files (CE Form->Menu->Table)
+
The TableFile class represents a file embedded in the current cheat table.
  
Table files can be added from the Cheat Engine form.
+
Table files can be created with [[Lua:createTableFile|createTableFile]] or retrieved with [[Lua:findTableFile|findTableFile]]. Their contents can be accessed through a [[Lua:Class:MemoryStream|MemoryStream]], saved to disk, or used directly by functions that accept table files.
* CE Form->Menu->Table->Add file
 
  
== Creation ==
+
===Inheritance===
; findTableFile(''fileName'')
+
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
: Returns the TableFile class object for the saved file
+
!align="left"|Class
 +
!align="left"|Inherits From
 +
!style="width: 80%;background-color:white;" align="left"|Description
 +
|-
 +
|TableFile
 +
|[[Lua:Class:Object|Object]]
 +
|Represents a file stored inside the cheat table.
 +
|}
  
; createTableFile(''fileName'', ''filePath'' OPTIONAL)
+
===Related Functions===
: Returns a newly added table file.
+
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
: If no file path is specified, it will create a blank file. Otherwise, it will read the contents from disk.
+
!align="left"|Function
 +
!align="left"|Return Type
 +
!style="width: 80%;background-color:white;" align="left"|Description
 +
|-
 +
|[[Lua:findTableFile|findTableFile]](filename)
 +
|TableFile
 +
|Returns the TableFile object for a file stored in the cheat table.
 +
|-
 +
|[[Lua:createTableFile|createTableFile]](filename, filepath OPTIONAL)
 +
|TableFile
 +
|Adds a new file to the cheat table. If filepath is not specified, it creates a blank file. If filepath is specified, it reads the contents from disk.
 +
|}
  
== Properties ==
+
===Properties===
; Name : string
+
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
: The table file's name.
+
!align="left"|Property
 +
!align="left"|Type
 +
!style="width: 80%;background-color:white;" align="left"|Description
 +
|-
 +
|Name
 +
|String
 +
|The name of the file stored in the cheat table.
 +
|-
 +
|Stream
 +
|[[Lua:Class:MemoryStream|MemoryStream]]
 +
|The stream containing the file data.
 +
|}
  
; Stream : [[MemoryStream]]
+
===Methods===
: The table file's underlying [[MemoryStream]] object.
+
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
 +
!align="left"|Method
 +
!align="left"|Return Type
 +
!style="width: 80%;background-color:white;" align="left"|Description
 +
|-
 +
|delete()
 +
|void
 +
|Deletes this file from the cheat table.
 +
|-
 +
|saveToFile(filename)
 +
|void
 +
|Saves this table file to disk.
 +
|-
 +
|getData()
 +
|[[Lua:Class:MemoryStream|MemoryStream]]
 +
|Returns a MemoryStream object containing the file data.
 +
|}
  
== Methods ==
+
===Examples===
; saveToFile(''filePath'')
 
: Saves the TableFile to the given file path.
 
  
; getData() : [[MemoryStream]]
+
====Find an embedded table file====
: Returns the table file's underlying [[MemoryStream]] object.
+
<syntaxhighlight lang="lua" line>
 +
local file = findTableFile("protected.wav")
  
== Examples ==
+
if file ~= nil then
local fileStr = nil
+
  print("Found table file: " .. file.Name)
local tableFile = findTableFile('PlayerBaseHook.CEA')
+
end
if tableFile ~= nil then
+
</syntaxhighlight>
  local stringStream = createStringStream()
 
  stringStream.Position = 0 -- if not set before using 'copyFrom' the 'StringStream' object will be inconsistent.
 
  stringStream.copyFrom(tableFile.Stream, tableFile.Stream.Size)
 
  fileStr = stringStream.DataString
 
  stringStream.destroy()
 
end
 
  
== See also ==
+
====Play an embedded sound====
* [[Lua]]
+
<syntaxhighlight lang="lua" line>
* [[Help_File:Script engine|Script engine]]
+
local file = findTableFile("protected.wav")
  
=== Related Functions ===
+
if file ~= nil then
* [[findTableFile]]
+
  playSound(file)
* [[createMemoryStream]]
+
end
* [[createFileStream]]
+
</syntaxhighlight>
* [[createStringStream]]
 
  
=== Related Classes ===
+
====Create a blank table file====
* [[MemoryStream]]
+
<syntaxhighlight lang="lua" line>
* [[FileStream]]
+
local file = createTableFile("runtime.dat")
* [[StringStream]]
+
 
* [[Stream]]
+
print("Created table file: " .. file.Name)
 +
</syntaxhighlight>
 +
 
 +
====Create a table file from disk====
 +
<syntaxhighlight lang="lua" line>
 +
local file = createTableFile("config.json", [[C:\\Temp\\config.json]])
 +
 
 +
print("Added table file: " .. file.Name)
 +
</syntaxhighlight>
 +
 
 +
====Save a table file to disk====
 +
<syntaxhighlight lang="lua" line>
 +
local file = findTableFile("config.json")
 +
 
 +
if file ~= nil then
 +
  file.saveToFile([[C:\\Temp\\config_copy.json]])
 +
end
 +
</syntaxhighlight>
 +
 
 +
====Read table file data as a MemoryStream====
 +
<syntaxhighlight lang="lua" line>
 +
local file = findTableFile("data.bin")
 +
 
 +
if file ~= nil then
 +
  local stream = file.getData()
 +
 
 +
  print("Stream size: " .. tostring(stream.Size))
 +
end
 +
</syntaxhighlight>
 +
 
 +
====Access the Stream property====
 +
<syntaxhighlight lang="lua" line>
 +
local file = findTableFile("data.bin")
 +
 
 +
if file ~= nil then
 +
  local stream = file.Stream
 +
 
 +
  stream.Position = 0
 +
  print("Stream size: " .. tostring(stream.Size))
 +
end
 +
</syntaxhighlight>
 +
 
 +
<syntaxhighlight lang="lua" line>
 +
local fileStr = nil
 +
local tableFile = findTableFile('PlayerBaseHook.CEA')
 +
if tableFile ~= nil then
 +
  local stringStream = createStringStream()
 +
  stringStream.Position = 0 -- if not set before using 'copyFrom' the 'StringStream' object will be inconsistent.
 +
  stringStream.copyFrom(tableFile.Stream, tableFile.Stream.Size)
 +
  fileStr = stringStream.DataString
 +
  stringStream.destroy()
 +
end
 +
</syntaxhighlight>
 +
 
 +
====Copy table file data into another MemoryStream====
 +
<syntaxhighlight lang="lua" line>
 +
local file = findTableFile("data.bin")
 +
 
 +
if file ~= nil then
 +
  local source = file.getData()
 +
  local copy = createMemoryStream()
 +
 
 +
  source.Position = 0
 +
  copy.copyFrom(source, source.Size)
 +
 
 +
  print("Copied bytes: " .. tostring(copy.Size))
 +
 
 +
  copy.destroy()
 +
end
 +
</syntaxhighlight>
 +
 
 +
====Delete a table file====
 +
<syntaxhighlight lang="lua" line>
 +
local file = findTableFile("old_data.bin")
 +
 
 +
if file ~= nil then
 +
  file.delete()
 +
end
 +
</syntaxhighlight>
 +
 
 +
====Replace an embedded file====
 +
<syntaxhighlight lang="lua" line>
 +
local oldFile = findTableFile("config.json")
 +
 
 +
if oldFile ~= nil then
 +
  oldFile.delete()
 +
end
 +
 
 +
local newFile = createTableFile("config.json", [[C:\\Temp\\config.json]])
 +
 
 +
print("Replaced table file: " .. newFile.Name)
 +
</syntaxhighlight>
 +
 
 +
{{LuaSeeAlso}}

Latest revision as of 18:55, 25 June 2026

{} Class

class TableFile : Object

The TableFile class represents a file embedded in the current cheat table.

Table files can be created with createTableFile or retrieved with findTableFile. Their contents can be accessed through a MemoryStream, saved to disk, or used directly by functions that accept table files.

Inheritance[edit]

Class Inherits From Description
TableFile Object Represents a file stored inside the cheat table.

Related Functions[edit]

Function Return Type Description
findTableFile(filename) TableFile Returns the TableFile object for a file stored in the cheat table.
createTableFile(filename, filepath OPTIONAL) TableFile Adds a new file to the cheat table. If filepath is not specified, it creates a blank file. If filepath is specified, it reads the contents from disk.

Properties[edit]

Property Type Description
Name String The name of the file stored in the cheat table.
Stream MemoryStream The stream containing the file data.

Methods[edit]

Method Return Type Description
delete() void Deletes this file from the cheat table.
saveToFile(filename) void Saves this table file to disk.
getData() MemoryStream Returns a MemoryStream object containing the file data.

Examples[edit]

Find an embedded table file[edit]

1 local file = findTableFile("protected.wav")
2 
3 if file ~= nil then
4   print("Found table file: " .. file.Name)
5 end

Play an embedded sound[edit]

1 local file = findTableFile("protected.wav")
2 
3 if file ~= nil then
4   playSound(file)
5 end

Create a blank table file[edit]

1 local file = createTableFile("runtime.dat")
2 
3 print("Created table file: " .. file.Name)

Create a table file from disk[edit]

1 local file = createTableFile("config.json", [[C:\\Temp\\config.json]])
2 
3 print("Added table file: " .. file.Name)

Save a table file to disk[edit]

1 local file = findTableFile("config.json")
2 
3 if file ~= nil then
4   file.saveToFile([[C:\\Temp\\config_copy.json]])
5 end

Read table file data as a MemoryStream[edit]

1 local file = findTableFile("data.bin")
2 
3 if file ~= nil then
4   local stream = file.getData()
5 
6   print("Stream size: " .. tostring(stream.Size))
7 end

Access the Stream property[edit]

1 local file = findTableFile("data.bin")
2 
3 if file ~= nil then
4   local stream = file.Stream
5 
6   stream.Position = 0
7   print("Stream size: " .. tostring(stream.Size))
8 end
1 local fileStr = nil
2 local tableFile = findTableFile('PlayerBaseHook.CEA')
3 if tableFile ~= nil then
4   local stringStream = createStringStream()
5   stringStream.Position = 0 -- if not set before using 'copyFrom' the 'StringStream' object will be inconsistent.
6   stringStream.copyFrom(tableFile.Stream, tableFile.Stream.Size)
7   fileStr = stringStream.DataString
8   stringStream.destroy()
9 end

Copy table file data into another MemoryStream[edit]

 1 local file = findTableFile("data.bin")
 2 
 3 if file ~= nil then
 4   local source = file.getData()
 5   local copy = createMemoryStream()
 6 
 7   source.Position = 0
 8   copy.copyFrom(source, source.Size)
 9 
10   print("Copied bytes: " .. tostring(copy.Size))
11 
12   copy.destroy()
13 end

Delete a table file[edit]

1 local file = findTableFile("old_data.bin")
2 
3 if file ~= nil then
4   file.delete()
5 end

Replace an embedded file[edit]

1 local oldFile = findTableFile("config.json")
2 
3 if oldFile ~= nil then
4   oldFile.delete()
5 end
6 
7 local newFile = createTableFile("config.json", [[C:\\Temp\\config.json]])
8 
9 print("Replaced table file: " .. newFile.Name)

Main Pages

Core Lua documentation entry points

Lua
Script Engine