Modules/FileSystem/file/readInt: Difference between revisions
Jump to navigation
Jump to search
(Created page with "__NOTOC__ This function attempts to read an integer (native type) from a file and return it. The amount of bytes read should be four. ==Syntax== <syntaxhighlight lang="lua"> int file:readInt () </syntaxhighlight> ==Returns== Returns an integer if it was successfully read from the file, '''false''' otherwise. ==Example== <section name="Client" class="client" show="true"> This snippet demonstrates an encrypted Lua source code format. <syntaxhighlight lang="lua"> -- Have...") |
No edit summary |
||
Line 1: | Line 1: | ||
<pageclass class="#3cc882" subcaption="File function"></pageclass> | |||
__NOTOC__ | __NOTOC__ | ||
This function attempts to read an integer (native type) from a file and return it. The amount of bytes read should be four. | This function attempts to read an integer (native type) from a file and return it. The amount of bytes read should be four. |
Latest revision as of 03:01, 23 January 2022
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 [-]
ClientThis 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