FileGetContents: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
m (add (file must be in meta.xml))
(Undo revision 79654 by Fernando187 (talk))
Tag: Undo
Line 3: Line 3:


{{Added feature/item|1.6.1|1.6.0|21938|
{{Added feature/item|1.6.1|1.6.0|21938|
Reads the entire contents of the file, optionally verifies the read contents by computing and comparing the checksum with the expected one (file must be in meta.xml), and returns the content as string. The file cursor position is not modified by calls to this function.
Reads the entire contents of the file, optionally verifies the read contents by computing and comparing the checksum with the expected one, and returns the content as string. The file cursor position is not modified by calls to this function.
}}
}}


Line 27: Line 27:
*'''filePath:''' Path to a file you want to read
*'''filePath:''' Path to a file you want to read
}}
}}
*'''verifyContents:''' Set to true, to compare the computed and the expected checksum of the file content (file must be in meta.xml).
*'''verifyContents:''' Set to true, to compare the computed and the expected checksum of the file content


===Returns===
===Returns===

Revision as of 09:47, 30 June 2024

BETA: NEW FEATURE (BUILD: 1.6.0 r21938)

Reads the entire contents of the file, optionally verifies the read contents by computing and comparing the checksum with the expected one, and returns the content as string. The file cursor position is not modified by calls to this function.

Please note that even if you enable SD #22 and #23 on your server, you are not protected from user attacks, which can happen after verification of the file, but before you read the contents of such verified file. This function enables you to safely read the contents of a meta.xml-listed file on both client and server.

Syntax

nil|string fileGetContents ( file theFile [ , bool verifyContents = true ] )
BETA: NEW FEATURE (BUILD: 1.6.0 r22560)
nil|string fileGetContents ( string filePath [ , bool verifyContents = true ] )


OOP Syntax Help! I don't understand this!

Method: file:getContents(...)


Required Arguments

  • theFile: A handle to the file you wish to get the contents from. Use fileOpen to obtain this handle.
BETA: NEW FEATURE (BUILD: 1.6.0 r22560)
  • filePath: Path to a file you want to read
  • verifyContents: Set to true, to compare the computed and the expected checksum of the file content

Returns

Returns the bytes that were read from the file, but only if verification was disabled or if the checksum comparison succeeded. On failure, this function returns nil.

Example

Click to collapse [-]
Shared

This example opens the code.lua file, checks its contents, and then runs it.

local handle = fileOpen("code.lua", true)
local buffer = fileGetContents(handle) -- code.lua must be listed in meta.xml (for example as <file> for this example)
fileClose(handle)

if buffer then
    loadstring(buffer)()
end
Click to collapse [-]
Shared

This example reads content from a requested model and sends it back to the client.

addEvent('onRequestModel', true)
addEventHandler('onRequestModel', resourceRoot, function(path)
    triggerClientEvent(client, 'onSendModel', client, fileGetContents(path))
end)

See Also