MTA:Eir/FileSystem/translator/open: Difference between revisions
Jump to navigation
Jump to search
(Created page with "__NOTOC__ This function opens a link to a file instance on a given MTA:Eir FileSystem translator. Using a file link, you can write and/or receive data from filesystems. ==Syntax...") |
mNo edit summary |
||
(4 intermediate revisions by 2 users not shown) | |||
Line 4: | Line 4: | ||
==Syntax== | ==Syntax== | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
file translator | file, string translator:open ( string filePath, string fileMode ) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 12: | Line 12: | ||
==Returns== | ==Returns== | ||
This function returns the '''FileSystem file''' class that can be used to retrieve or store data persistently. Returns '''false''' if the file failed to open | This function returns the '''FileSystem file''' class that can be used to retrieve or store data persistently. Returns '''false''' if the file failed to open and the '''reason of failure as string'''. | ||
===Failure reasons=== | |||
* unknown error | |||
* path out of scope | |||
* invalid parameters | |||
* resources exhausted | |||
* access denied | |||
* not found | |||
* already exists | |||
==Example== | ==Example== | ||
<section name="Server" class="server" show="true"> | |||
This snippet lists information about the registered MTA server modules. This information can be retrieved through a command. | |||
<syntaxhighlight lang="lua"> | |||
-- The table that will contain all module information. | |||
local moduleInfo = {}; | |||
-- Attempt to get a handle to the FileSystem module namespace. | |||
local fsys = createFilesystemInterface(); | |||
-- Could fail if the server restrictions are set tight. | |||
if not ( fsys ) then | |||
outputDebugString( "could not get a handle to the FileSystem module namespace" ); | |||
return false; | |||
end | |||
local function moduleFileIterator( filePath ) | |||
-- Create an entry for this module. | |||
local moduleName = fsys.root:relPath( filePath ); | |||
local moduleStats = fsys.root:stat( filePath ); | |||
local entry = { | |||
name = moduleName, | |||
stats = moduleStats | |||
}; | |||
-- Add the entry into the registry. | |||
table.insert( moduleInfo, entry ); | |||
end | |||
-- Loop through all server modules. | |||
fsys.root:chdir( "mods/deathmatch/modules/" ); | |||
fsys.root:scanDirEx( "", "*", nil, moduleFileIterator, false ); | |||
-- Function to get a module into by name. | |||
local function getModuleByName( name ) | |||
for m,n in ipairs( moduleInfo ) do | |||
if ( n.name == name ) then | |||
return n; | |||
end | |||
end | |||
return false; | |||
end | |||
-- Command to request server module information. | |||
addCommandHandler( "modules", | |||
function(player, moduleName) | |||
-- Output module information to the player. | |||
local module = getModuleByName( moduleName ); | |||
if not ( module ) then | |||
outputChatBox( "could not find module named " .. tostring( moduleName ), player ); | |||
return false; | |||
end | |||
-- Output it. | |||
outputChatBox( "module-name: " .. module.name ); | |||
outputChatBox( "module-size: " .. module.stats.size ); | |||
-- todo: add more info about the module. | |||
end | |||
); | |||
</syntaxhighlight> | |||
</section> | |||
<section name="Client" class="client" show="true"> | <section name="Client" class="client" show="true"> | ||
This snippet attempts to open a file and output its contents inside of a CEGUI memo. | This snippet attempts to open a file and output its contents inside of a CEGUI memo. | ||
Line 30: | Line 103: | ||
if ( fileHandle ) then | if ( fileHandle ) then | ||
fileContents = fileHandle | fileContents = fileHandle:read( fileHandle.size() ); | ||
-- Clean up our file handle. | -- Clean up our file handle. | ||
fileHandle | fileHandle:destroy(); | ||
end | end | ||
Latest revision as of 23:22, 16 January 2022
This function opens a link to a file instance on a given MTA:Eir FileSystem translator. Using a file link, you can write and/or receive data from filesystems.
Syntax
file, string translator:open ( string filePath, string fileMode )
Arguments
- filePath: the path to the file that should be opened
- fileMode: an ANSI file mode descriptor (can be 'w', 'r' or 'a', with 'b' and/or '+' appended)
Returns
This function returns the FileSystem file class that can be used to retrieve or store data persistently. Returns false if the file failed to open and the reason of failure as string.
Failure reasons
- unknown error
- path out of scope
- invalid parameters
- resources exhausted
- access denied
- not found
- already exists
Example
Click to collapse [-]
ServerThis snippet lists information about the registered MTA server modules. This information can be retrieved through a command.
-- The table that will contain all module information. local moduleInfo = {}; -- Attempt to get a handle to the FileSystem module namespace. local fsys = createFilesystemInterface(); -- Could fail if the server restrictions are set tight. if not ( fsys ) then outputDebugString( "could not get a handle to the FileSystem module namespace" ); return false; end local function moduleFileIterator( filePath ) -- Create an entry for this module. local moduleName = fsys.root:relPath( filePath ); local moduleStats = fsys.root:stat( filePath ); local entry = { name = moduleName, stats = moduleStats }; -- Add the entry into the registry. table.insert( moduleInfo, entry ); end -- Loop through all server modules. fsys.root:chdir( "mods/deathmatch/modules/" ); fsys.root:scanDirEx( "", "*", nil, moduleFileIterator, false ); -- Function to get a module into by name. local function getModuleByName( name ) for m,n in ipairs( moduleInfo ) do if ( n.name == name ) then return n; end end return false; end -- Command to request server module information. addCommandHandler( "modules", function(player, moduleName) -- Output module information to the player. local module = getModuleByName( moduleName ); if not ( module ) then outputChatBox( "could not find module named " .. tostring( moduleName ), player ); return false; end -- Output it. outputChatBox( "module-name: " .. module.name ); outputChatBox( "module-size: " .. module.stats.size ); -- todo: add more info about the module. end );
Click to collapse [-]
ClientThis snippet attempts to open a file and output its contents inside of a CEGUI memo.
-- Get the screen size so we can scale the memo properly local screenWidth, screenHeight = guiGetScreenSize(); -- Make the memo cover nearly the entire screen. local myMemo = guiCreateMemo( 20, 20, screenWidth - 40, screenHeight - 40, "", false ); -- Read the contents of some file. local fileContents = ""; local fileHandle = fileOpen( "someFile.txt", "rb" ); if ( fileHandle ) then fileContents = fileHandle:read( fileHandle.size() ); -- Clean up our file handle. fileHandle:destroy(); end -- Update the memo. guiSetText( myMemo, fileContents );
FileSystem Translator Functions
- open
- exists
- createDir
- chdir
- delete
- copy
- rename
- size
- stat
- relPath
- relPathRoot
- absPath
- absPathRoot
- scanDir
- scanDirEx
- getDirs
- getFiles
- setOutbreakEnabled
- getOutbreakEnabled
- setPathProcessingMode
- getPathProcessingMode