MTA:Eir/FileSystem/file/readInt

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

This function attempts to read an integer (native type) from a file and return it. The amount of bytes read should be four.

Syntax

int file:readInt ()

Returns

Returns an integer if it was successfully read from the file, false otherwise.

Example

Click to collapse [-]
Client

This snippet demonstrates an encrypted Lua source code format.

-- Have some pieces of source code as samples.
local sourceSamples = {
[[print("Hello World!");]],
[[print("Ballas on the streets.");]],
[[return 1+1==0;]]
};

local function packSourceCode( theFile, sourceString )
    -- Encrypt the source code.
    local encryptedCode = teaEncode( sourceString );

    -- Write the file header.
    theFile:writeInt( #encryptedCode );

    -- Write file contents.
    theFile:write( encryptedCode );
end

local function unpackSourceCode( theFile )
    -- Grab the source code from the file.
    local encryptedLen = theFile:readInt();

    -- Has the length been read correctly?
    if not ( encryptedLen ) then
        return false;
    end

    local encryptedSource = theFile:read( encryptedLen );

    -- Make sure it has not been fragmented.
    if not ( #encryptedSource == encryptedLen ) then
        return false;
    end

    -- Decrypt the source code.
    local sourceCode = teaDecode( encryptedSource );

    -- Return it.
    return sourceCode;
end

-- Attempt some encryption.
local theFile = fileCreate( "encryptedSource.lue" );

for m,n in ipairs( sourceSamples ) do
    packSourceCode( theFile, n );
end

-- Get the original source code again.
theFile:seek( 0, "set" );

local unencryptedSources = {};

while ( true ) do
    local code = unpackSourceCode( theFile );

    if not ( code ) then break; end

    table.insert( unencryptedSources, code );
end

FileSystem File Functions