MTA:Eir/FileSystem/file/read: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Created page with "__NOTOC__ This function attempts to read the specified amount of bytes from the file. The actual amount of bytes read equals to the length of the Lua string. ==Syntax== <syntaxhighlight lang="lua">[l...")
 
mNo edit summary
 
Line 4: Line 4:
==Syntax==
==Syntax==
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
string file.read ( int readCount )
string file:read ( int readCount )
</syntaxhighlight>
</syntaxhighlight>


Line 19: Line 19:
local function isCollisionFile( file )
local function isCollisionFile( file )
     -- Check whether the file is big enough to be a collision file.
     -- Check whether the file is big enough to be a collision file.
     if ( ( file.size() - file.tell() ) <= 8 ) then
     if ( ( file:size() - file:tell() ) <= 8 ) then
         return false;
         return false;
     end
     end


     -- Read the header checksum from the file.
     -- Read the header checksum from the file.
     local checksum = file.read( 4 );
     local checksum = file:read( 4 );


     -- Check whether the checksum is correct.
     -- Check whether the checksum is correct.
Line 37: Line 37:
     result = isCollisionFile( myColFile );
     result = isCollisionFile( myColFile );


     myColFile.destroy();
     myColFile:destroy();
end
end



Latest revision as of 23:32, 16 January 2022

This function attempts to read the specified amount of bytes from the file. The actual amount of bytes read equals to the length of the Lua string.

Syntax

string file:read ( int readCount )

Arguments

  • readCount: the amount of bytes to read from the file

Returns

This function returns a string that contains the bytes that have been read from the file.

Example

Click to collapse [-]
Client

This snippet returns whether the file that is passed to it looks like a collision file.

local function isCollisionFile( file )
    -- Check whether the file is big enough to be a collision file.
    if ( ( file:size() - file:tell() ) <= 8 ) then
        return false;
    end

    -- Read the header checksum from the file.
    local checksum = file:read( 4 );

    -- Check whether the checksum is correct.
    return ( checksum == "COLL" ) or ( checksum == "COL2" ) or ( checksum == "COL3" ) or ( checksum == "COL4" );
end

-- Verify a collision file.
local myColFile = fileOpen( "collisions/fence.col" );
local result = false;

if ( myColFile ) then
    result = isCollisionFile( myColFile );

    myColFile:destroy();
end

outputChatBox( "proper collision of fence model: " .. result );

FileSystem Translator Functions

FileSystem File Functions