Modules/FileSystem/file/readUByte: Difference between revisions
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_byte file:readUByte () </syntaxhighlight> ==Returns== Returns the unsigned byte if it was successfully read from the file, '''false''' otherwise. ==Example== <section name="Client" class="client" show="true"> This snippet writes a Lua type into a file stream. The lua type can be any...") |
No edit summary |
||
(One intermediate revision by one other user not shown) | |||
Line 1: | Line 1: | ||
<pageclass class="#3cc882" subcaption="File function"></pageclass> | |||
__NOTOC__ | __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. | This function attempts to read an unsigned byte (native type) from a file and returns it. The amount of bytes read should be one. | ||
Line 36: | Line 37: | ||
-- Write the network identifier. | -- Write the network identifier. | ||
theFile | theFile:writeUByte( info.networkId ); | ||
-- Check what we are dealing with. | -- Check what we are dealing with. | ||
Line 50: | Line 51: | ||
local numIteration = #data; | local numIteration = #data; | ||
do | do | ||
theFile | theFile:writeUInt( numIteration ); | ||
local n = 1; | local n = 1; |
Latest revision as of 03:00, 23 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 [-]
ClientThis 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.