MTA:Eir/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 (...") |
mNo edit summary |
||
Line 4: | Line 4: | ||
==Syntax== | ==Syntax== | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
int file | int file:readInt () | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 26: | Line 26: | ||
-- Write the file header. | -- Write the file header. | ||
theFile | theFile:writeInt( #encryptedCode ); | ||
-- Write file contents. | -- Write file contents. | ||
theFile | theFile:write( encryptedCode ); | ||
end | end | ||
local function unpackSourceCode( theFile ) | local function unpackSourceCode( theFile ) | ||
-- Grab the source code from the file. | -- Grab the source code from the file. | ||
local encryptedLen = theFile | local encryptedLen = theFile:readInt(); | ||
-- Has the length been read correctly? | -- Has the length been read correctly? | ||
Line 41: | Line 41: | ||
end | end | ||
local encryptedSource = theFile | local encryptedSource = theFile:read( encryptedLen ); | ||
-- Make sure it has not been fragmented. | -- Make sure it has not been fragmented. | ||
Line 63: | Line 63: | ||
-- Get the original source code again. | -- Get the original source code again. | ||
theFile | theFile:seek( 0, "set" ); | ||
local unencryptedSources = {}; | local unencryptedSources = {}; |
Latest revision as of 23:36, 16 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