MTA:Eir/FileSystem/createTranslator

From Multi Theft Auto: Wiki

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