MTA:Eir/FileSystem/createTranslator: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Created page with "__NOTOC__ This function creates a FileSystem translator. A FileSystem translator represents a directory on a real or virtual filesystem. Through translators you get access to the...")
 
(added server example)
 
(2 intermediate revisions by the same user not shown)
Line 14: Line 14:


==Example==
==Example==
<section name="Client" class="client" show="true">
<section name="Server" class="server" show="true">
This snippet links all directories inside of your resource instance folder using translators and lists them in a dictionary under their name.
<syntaxhighlight lang="lua">
-- Dictionary that will contain all directory links of our resource.
local dirs = {};
 
-- Get a handle to the FileSystem module namespace.
local fsys = createFilesystemInterface();
 
-- Make sure we could obtain the module namespace.
if not ( fsys ) then
    outputDebugString( "could not obtain FileSystem module namespace" );
    return false;
end
 
-- Create a translator to the resource root.
local resRoot = fsys.createTranslator( "mods/deathmatch/resources/" .. getResourceName(resource) .. "/" );
 
local function dirIterator( dirPath )
    -- get the simple name of this directory.
    -- the simple name is the path relative to resRoot without the '/'
    local simpleName = resRoot.relPathRoot( dirPath );
    simpleName = string.sub( simpleName, 1, #simpleName - 1 );
 
    -- link this directory and set it into our dirs dictionary.
    -- we should always pass the absolute directory to this function.
    local translator = fileCreateTranslator( dirPath );
 
    dirs[simpleName] = translator;
end
 
local function fileIterator( filePath )
    -- do nothing.
    return;
end
 
resRoot.scanDirEx( "/", "*", dirIterator, fileIterator, false );
</syntaxhighlight>
</section>
<section name="Client" class="client" show="false">
This snippet links all directories inside of your resource instance folder using translators and lists them in a dictionary under their name.
This snippet links all directories inside of your resource instance folder using translators and lists them in a dictionary under their name.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
Line 44: Line 83:
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>
{{:MTA:Eir/FileSystem/functions}}
{{:MTA:Eir/FileSystem/namespace/functions}}
 
{{:MTA:Eir/FileSystem/translator/functions}}

Latest revision as of 20:06, 2 February 2014

This function creates a FileSystem translator. A FileSystem translator represents a directory on a real or virtual filesystem. Through translators you get access to the files that reside in their directory trees. The translator returned by this function usually represents an OS filesystem directory.

Syntax

translator fsnamespace.createTranslator( string rootPath )

Arguments

  • rootPath: the absolute path to the directory that you want access to.

Returns

This function returns the FileSystem translator that grants access to files in the requested directory.

Example

Click to collapse [-]
Server

This snippet links all directories inside of your resource instance folder using translators and lists them in a dictionary under their name.

-- Dictionary that will contain all directory links of our resource.
local dirs = {};

-- Get a handle to the FileSystem module namespace.
local fsys = createFilesystemInterface();

-- Make sure we could obtain the module namespace.
if not ( fsys ) then
    outputDebugString( "could not obtain FileSystem module namespace" );
    return false;
end

-- Create a translator to the resource root.
local resRoot = fsys.createTranslator( "mods/deathmatch/resources/" .. getResourceName(resource) .. "/" );

local function dirIterator( dirPath )
    -- get the simple name of this directory.
    -- the simple name is the path relative to resRoot without the '/'
    local simpleName = resRoot.relPathRoot( dirPath );
    simpleName = string.sub( simpleName, 1, #simpleName - 1 );

    -- link this directory and set it into our dirs dictionary.
    -- we should always pass the absolute directory to this function.
    local translator = fileCreateTranslator( dirPath );

    dirs[simpleName] = translator;
end

local function fileIterator( filePath )
    -- do nothing.
    return;
end

resRoot.scanDirEx( "/", "*", dirIterator, fileIterator, false );
Click to expand [+]
Client

FileSystem Namespace Functions

FileSystem Translator Functions