MTA:Eir/FileSystem/file/readUByte

From Multi Theft Auto: Wiki
Jump to navigation Jump to search

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