Difference between revisions of "Lua:createMemoryStream"

From Cheat Engine
Jump to navigation Jump to search
m (Syntax Highlighting.)
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
<h3>Example 1</h3>
+
[[Category:Lua]]
<pre>
+
{{CodeBox|'''function''' createMemoryStream() ''':''' MemoryStream}}
m = createMemoryStream()
 
m.Size = 8
 
m.Position = 0
 
  
print("addr: ", m.Memory)
+
Creates a new MemoryStream object.
  
writeIntegerLocal(m.Memory, 1)
+
A MemoryStream is a stream stored in memory. It inherits from Stream and Object, and can be used to load, store, modify, or save binary data.
m.Position = m.Position + 4
 
writeIntegerLocal(m.Memory+m.Position, -1)
 
  
m.Position = 0
+
The MemoryStream's memory address can be accessed through its read-only ''Memory'' property. This address may change when the stream size changes.
print( readIntegerLocal(m.Memory) ) -- 1
 
m.Position = m.Position + 4
 
print( readIntegerLocal(m.Memory+m.Position, true) ) -- -1
 
print( readIntegerLocal(m.Memory+m.Position) )      -- 4294967295
 
  
m.destroy()
+
===Function Parameters===
m = nil
+
This function has no parameters.
</pre>
 
  
<h3>Example 2</h3>
+
===Returns===
<pre>
+
MemoryStream — A new MemoryStream object.
m = createMemoryStream()
 
m.Size = 8
 
m.Position = 0
 
  
print("addr: ", m.Memory)
+
===Class Information===
 +
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
 +
!align="left"|Class
 +
!align="left"|Inherits From
 +
!style="width: 80%;background-color:white;" align="left"|Description
 +
|-
 +
|MemoryStream
 +
|Stream, Object
 +
|A stream object that stores its contents in memory.
 +
|}
  
m.writeDword(1)
+
===Properties===
m.writeDword(-1)
+
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
 +
!align="left"|Property
 +
!align="left"|Type
 +
!style="width: 80%;background-color:white;" align="left"|Description
 +
|-
 +
|Memory
 +
|Integer
 +
|The address in Cheat Engine's memory where this stream is currently loaded. This property is read-only and may change when the stream size changes.
 +
|}
  
m.Position = 0
+
===Methods===
print( m.readDword() ) -- 1
+
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
print( m.readDword() ) -- 4294967295
+
!align="left"|Method
 +
!align="left"|Return Type
 +
!style="width: 80%;background-color:white;" align="left"|Description
 +
|-
 +
|loadFromFile(filename)
 +
|void
 +
|Replaces the contents of the memory stream with the contents of a file on disk.
 +
|-
 +
|saveToFile(filename)
 +
|void
 +
|Writes the contents of the memory stream to the specified file.
 +
|-
 +
|loadFromFileNoError(filename)
 +
|Boolean, String
 +
|Replaces the contents of the memory stream with the contents of a file on disk. On success, returns true. On failure, returns false and a secondary return value containing the error message.
 +
|-
 +
|saveToFileNoError(filename)
 +
|Boolean, String
 +
|Writes the contents of the memory stream to the specified file. On success, returns true. On failure, returns false and a secondary return value containing the error message.
 +
|}
  
m.destroy()
+
===Examples===
m = nil
+
<syntaxhighlight lang="lua" line>
</pre>
+
local stream = createMemoryStream()
  
<h3>Example 3</h3>
+
stream.loadFromFile("C:\\\\Temp\\\\input.bin")
<pre>
+
print("Memory address: " .. string.format("%X", stream.Memory))
local m = createMemoryStream()
 
m.Size = 1024
 
  
local hwnd = findWindow("WTWindow", "Game")
+
stream.saveToFile("C:\\\\Temp\\\\output.bin")
local r = executeCodeLocalEx("GetWindowTextA", hwnd, m.Memory,  m.Size)
+
</syntaxhighlight>
  
if r ~= 0 then
+
<syntaxhighlight lang="lua" line>
m.Position = 0
+
local stream = createMemoryStream()
print( readStringLocal(m.Memory ,  m.Size) ) -- Game
+
 
 +
local success, errorMessage = stream.loadFromFileNoError("C:\\\\Temp\\\\input.bin")
 +
 
 +
if success then
 +
  print("File loaded into memory stream")
 +
else
 +
  print("Failed to load file: " .. errorMessage)
 
end
 
end
 +
</syntaxhighlight>
 +
 +
{{LuaSeeAlso}}
  
m.destroy()
+
{{Memory}}
m = nil
 
</pre>
 

Latest revision as of 16:34, 25 June 2026

<> Lua API Reference

function createMemoryStream() : MemoryStream

Creates a new MemoryStream object.

A MemoryStream is a stream stored in memory. It inherits from Stream and Object, and can be used to load, store, modify, or save binary data.

The MemoryStream's memory address can be accessed through its read-only Memory property. This address may change when the stream size changes.

Function Parameters[edit]

This function has no parameters.

Returns[edit]

MemoryStream — A new MemoryStream object.

Class Information[edit]

Class Inherits From Description
MemoryStream Stream, Object A stream object that stores its contents in memory.

Properties[edit]

Property Type Description
Memory Integer The address in Cheat Engine's memory where this stream is currently loaded. This property is read-only and may change when the stream size changes.

Methods[edit]

Method Return Type Description
loadFromFile(filename) void Replaces the contents of the memory stream with the contents of a file on disk.
saveToFile(filename) void Writes the contents of the memory stream to the specified file.
loadFromFileNoError(filename) Boolean, String Replaces the contents of the memory stream with the contents of a file on disk. On success, returns true. On failure, returns false and a secondary return value containing the error message.
saveToFileNoError(filename) Boolean, String Writes the contents of the memory stream to the specified file. On success, returns true. On failure, returns false and a secondary return value containing the error message.

Examples[edit]

1 local stream = createMemoryStream()
2 
3 stream.loadFromFile("C:\\\\Temp\\\\input.bin")
4 print("Memory address: " .. string.format("%X", stream.Memory))
5 
6 stream.saveToFile("C:\\\\Temp\\\\output.bin")
1 local stream = createMemoryStream()
2 
3 local success, errorMessage = stream.loadFromFileNoError("C:\\\\Temp\\\\input.bin")
4 
5 if success then
6   print("File loaded into memory stream")
7 else
8   print("Failed to load file: " .. errorMessage)
9 end

Main Pages

Core Lua documentation entry points

Lua
Script Engine

Sections / Views

Memory Mapping

Memory Copy / Compare

Memory Streams