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

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Created page with "__NOTOC__ This function attempts to read an unsigned byte (native type) from a file and returns it. The amount of bytes read should be one. ==Syntax== <syntaxhighlight lang="lua"> unsigned_byt...")
 
mNo edit summary
 
(2 intermediate revisions by one other user not shown)
Line 4: Line 4:
==Syntax==
==Syntax==
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
unsigned_byte file.readUByte ()
unsigned_byte file:readUByte ()
</syntaxhighlight>
</syntaxhighlight>


Line 11: Line 11:


==Example==
==Example==
<section name="Server" class="server" show="true">
<section name="Client" class="client" show="true">
This snippet writes a Lua type into a file stream. The lua type can be anything that can be traversed over the network. It must be context-less data.
This snippet writes a Lua type into a file stream. The lua type can be anything that can be traversed over the network. It must be context-less data.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
Line 40: Line 40:
     -- Check what we are dealing with.
     -- Check what we are dealing with.
     if ( typeName == "boolean") then
     if ( typeName == "boolean") then
         theFile.writeBoolean( data );
         theFile:writeBoolean( data );
     elseif ( typeName == "number" ) then
     elseif ( typeName == "number" ) then
         theFile.writeDouble( data );
         theFile:writeDouble( data );
     elseif ( typeName == "string" ) then
     elseif ( typeName == "string" ) then
         theFile.writeUShort( #data );
         theFile:writeUShort( #data );
         theFile.write( data );
         theFile:write( data );
     elseif ( typeName == "table" ) then
     elseif ( typeName == "table" ) then
         -- First write iterated entries of the table.
         -- First write iterated entries of the table.
Line 67: Line 67:


             for m,n in pairs( data ) do
             for m,n in pairs( data ) do
                 if ( isValidNetworkType( type( n ) ) ) then
                 if ( isValidNetworkType( rawtype( n ) ) ) then
                     if not ( type( n ) == "number" ) or ( n <= 0 ) or ( n > numIteration ) then
                     if not ( rawtype( n ) == "number" ) or ( n <= 0 ) or ( n > numIteration ) then
                         entryCount = entryCount + 1;
                         entryCount = entryCount + 1;


Line 80: Line 80:


             -- Write the amount of data entries.
             -- Write the amount of data entries.
             theFile.writeUInt( entryCount );
             theFile:writeUInt( entryCount );


             -- Write all data entries.
             -- Write all data entries.

Latest revision as of 23:34, 16 January 2022

This function attempts to read an unsigned byte (native type) from a file and returns it. The amount of bytes read should be one.

Syntax

unsigned_byte file:readUByte ()

Returns

Returns the unsigned byte if it was successfully read from the file, false otherwise.

Example

Click to collapse [-]
Client

This snippet writes a Lua type into a file stream. The lua type can be anything that can be traversed over the network. It must be context-less data.

-- A table specifying descriptor information about Lua types.
local typeDescriptors = {
    ["boolean"] = { networkId = 0 },
    ["number"] = { networkId = 1 },
    ["string"] = { networkId = 2 },
    ["table"] = { networkId = 3 },
    ["nil"] = { networkId = 4 }
};

local function isValidNetworkType( typeName )
    return not ( typeDescriptors[typeName] == nil );
end

local function writeLuaData( theFile, data )
    -- Get the descriptor for the type.
    local typeName = rawtype(data);
    local info = typeDescriptors[typeName];

    -- Some types cannot be transfered over the network.
    if not ( info ) then return; end;

    -- Write the network identifier.
    theFile.writeUByte( info.networkId );

    -- Check what we are dealing with.
    if ( typeName == "boolean") then
        theFile:writeBoolean( data );
    elseif ( typeName == "number" ) then
        theFile:writeDouble( data );
    elseif ( typeName == "string" ) then
        theFile:writeUShort( #data );
        theFile:write( data );
    elseif ( typeName == "table" ) then
        -- First write iterated entries of the table.
        local numIteration = #data;
        do
            theFile.writeUInt( numIteration );

            local n = 1;

            while ( n <= numIteration ) do
                writeLuaData( data[n] );
                n = n + 1;
            end
        end

        -- Next write the data entries.
        do
            -- Construct a data entry table.
            local dataTable = {};
            local entryCount = 0;

            for m,n in pairs( data ) do
                if ( isValidNetworkType( rawtype( n ) ) ) then
                    if not ( rawtype( n ) == "number" ) or ( n <= 0 ) or ( n > numIteration ) then
                        entryCount = entryCount + 1;

                        dataTable[entryCount] = {
                            key = m,
                            value = n
                        };
                    end
                end
            end

            -- Write the amount of data entries.
            theFile:writeUInt( entryCount );

            -- Write all data entries.
            for m,n in ipairs( dataTable ) do
                writeLuaData( n.key );
                writeLuaData( n.value );
            end
        end
    elseif ( typeName == "nil" ) then
        -- Do nothing.
    end
end

-- task for the reader: write a function to read the Lua data again.

FileSystem File Functions