File-based systems

class Filesystem

class BNGBase::FS::IFileSystem

There is a global variable to use these functions: See FS

int fileSize(string filename)

Gets the Size of a file

Parameters:

  • string filename: The path of the file you want to know the size of
Returns:int size: The size of the file

Example:

print(FS:fileSize("test.lua"))
bool fileExists(string filename)

Checks if a File exists at filename

Parameters:

  • string filename: the path of the File, you want to check if its exist
Returns:boolean: true if it exist; false otherwise

Example

print(tostring(FS:fileExists("test.lua")))
string getFileRealPath(string virtualPath)

Gets the real path of your computer to the file.

Parameters:

  • string virtualPath: The path of the File, you want to check. (f.e. test.lua)
Returns:string: the Real path of the file: f.e: C:UsersUserDocumentsBeamNG.drivetest.lua

Example:

print(FS:getFileRealPath("test.lua")) -- prints f.e C:\Users\User\Documents\BeamNG.drive\test.lua
string getGamePath()

Gets the Path, where the Game is saved in.

Returns:The game path (f.e. C:Program Files (x86)SteamsteamappscommonBeamNG.drive)

Example:

print(FS:getGamePath()) -- prints f.e. C:\Program Files (x86)\Steam\steamapps\common\BeamNG.drive
bool directoryExists(string directory)

Checks if a Directory exist in Documents-Folder

Parameters:

  • string directory: directory
Returns:bool: true if the directory exist, false otherwise

Example:

print(FS:directoryExists("levels")) -- Prints true, because the directory exist
print(FS:directoryExists("crazydir")) -- Prints (normaly) false, because the directory doesn't exist
table findFiles(string inBasePath, string inFilePattern, int recursiveLevels, bool multiMatch, bool includeDirs)

List recursuvelly or not, all files and directories in the inBasePath path that match the pattern in inFilePattern

Parameters:

  • string inBasePath: Base folder to search in
  • string inFilePattern: the file pattern to search
  • int recursiveLevels: number of subfolder to look in
  • bool multiMatch: Match agains multiple patterns, seperated by tab caracter ('\t')
  • bool includeDirs: Directory included in search
Returns:table of string (filepath)

Example

local tab = FS:findFiles("/", "ball", 10, false, true)
for i, elem in ipairs(tab) do -- For all index in tab
    print(i..", "..elem) --prints the element from index i: f.e: /vehicles/ball
end
table findFilesByPattern(string inBasePath, string inFilePattern, int recursiveLevels, bool multiMatch, bool includeDirs)

Alias of findFiles()

table findFilesByRootPattern(string inBasePath, string inFilePattern, int recursiveLevels, bool multiMatch, bool includeDirs)

Alias of findFiles()

table directoryList(string inBasePath, bool listFiles, bool listFolders)

List all files and directories in the path inBasePath, without recursion

Parameters:

  • string inBasePath: Base folder to search in
  • bool listFiles: switch to include files in the search result
  • bool listFolders: switch to include directories in the search result
Returns:table of string (filepath)

Example

local tab = FS:directoryList("/", false, true)
for i, elem in ipairs(tab) do -- For all index in tab
    print(i..", "..elem) --prints the element from index i: f.e: /art ...
end

New in version 0.17.

string hashFile(string filename)

This Function hashes a File and gets the String of the hash

Parameters:

  • string filename: The path to the file
Returns:string

Example

local hashed = FS:hashFile("test.lua");
print(hashed)
int getLastError()

Gets the error-Code of the last error

Returns:(int) Error code

Example:

local error = FS:getLastError();
print(error)
table stat(string filename)

This function return information about a file or folder

Parameters:

string filename: filename

Returns:table
Index name Type Exemple
filesize number 5892421
accesstime number 1505125839
createtime number 1505125839
modtime number 1505131658
readonly bool true or false
filetype string "file" or "dir" or "link" or "other"
bool removeFile(string filepath)

Removes a file

Parameters:

string filename: The path of the file

Returns:boolean: true if it was deleted successfully, false otherwise

Example:

FS:removeFile("test.file"); --removes the file, that I created in f.e. C:\Users\User\Documents\BeamNG.drive
bool renameFile(string from, string to)

Renames a file

Parameters:

  • string from: filename
  • string to: filename
Returns:boolean, true if rename was successfull, false otherwise

Example:

FS:renameFile("test.file", "supertest.file")
bool mount(string srcPath, string mountPath = "", int priority = 0)

Mounts a mod, if its not enabled.

Parameters:

  • string srcPath: srcPath
  • string mountPath: mountPath
  • int priority: filename
Returns:boolean
bool mountList(table mountList)

List of mods to mount, Used in modmanager

mountList is using this table format

Index Type Default
srcPath String None
mountPath String / (root)

Parameters:

  • table mountList: in mountList : List of files to mount
Returns:boolean
bool unmount(string zipPath)
Parameters:zipPath – zipPath
Returns:boolean
bool isMounted(string zipPath)
Parameters:zipPath – zipPath
Returns:boolean
string expandFilename(string path)

Return a filepath that get expanded, It’s a Torque3D Console function to transform relative path

Parameters:path – path
Returns:string

Class ZipArchive

class ZipArchive
Warning:It’s not recomended to read/write an archive mounted by modmanager
ZipArchive()

Example:

local zip = ZipArchive()
bool openArchiveName(string filename, string mode)

Parameters:

  • char filename: path to Zip-zip file
  • char mode: mode : { r = read only, w = write only, rw = read & write}
Returns:false if failed to open

Example:

local zip = ZipArchive()
local bool = zip:openArchiveName("test.zip", "r")
print(tostring(bool))
table getFileList()

get a table with each file. the path is relative to the zip

Returns:table with all files

Example

local zip = ZipArchive()
local bool = zip:openArchiveName("test.zip", "r")
print(tostring(bool))
local filelist = zip:getFileList()
for i, elem in pairs(filelist) do
    print(i..", "..elem) --prints files like: screenshots/screen1.png
end
string readFileEntryByIdx(int idx)

read the content of a file that is in the zip

Parameters:

Returns:content of the file

Example:

local zip = ZipArchive()
local bool = zip:openArchiveName("test.zip", "r")
local filelist = zip:getFileList()
for i, elem in pairs(filelist) do
   local text = zip:readFileEntryByIdx(i)
   print(text) -- prints the complete into console.
end
string getFileEntryHashByIdx(int idx)

Parameters:

Returns:hash of the file using the index of the getFileList() table

Example:

local zip = ZipArchive()
local bool = zip:openArchiveName("test.zip", "r")
local filelist = zip:getFileList()
for i, elem in pairs(filelist) do
    local text = zip:getFileEntryHashByIdx(i)
    print(text)
end
bool addFile(char filename, char pathInZip)
bool extractFile(char pathInZip, char filePath)

Parameters:

  • char pathInZip: The path to the file in the zip
  • char filePath: the path
Returns:true if sucess

Example

local zip = ZipArchive()
local bool = zip:openArchiveName("test.zip", "r")
local test = zip:extractFile('testing.lua', 'testing.lua')
print(test)
bool deleteFile(string filename)
Returns:true if sucess
close()

Closes the Zip-File.

Example:

local zip = ZipArchive()
local bool = zip:openArchiveName("test.zip", "rw")
zip:close()
char __tostring()

Turns the Zip in a string

Returns:a LUA string in this format %filename.zip%  [mode: %r|w%]

Example:

local zip = ZipArchive()
local bool = zip:openArchiveName("test.zip", "w")
print(zip:__tostring())

class BeamNGNodeStreamPhysFS

class BeamNGNodeStreamPhysFS
int play(char filename)

function to play a replay file

Parameter

char filename: Path to the file to be played back.

Example

local test = be.nodeStream
test:play("test.file")
int record(char filename)

function to record everything that happens in the level

Parameter

char filename: Path to the file to be played back.

Example

local test = be.nodeStream
test:record("test.file")
stop()

stops playing the current file

Example:

local test = be.nodeStream
test:play("test.file")
test:stop(); --Stops directly the replay
int seek(float percent)

Parameters:

  • float percent: The percentage that you want to display in the replay

Example

local test = be.nodeStream
test:play("test.file")
test:seek(30) -- Switch to 30 percent of the replay
setPaused(bool paused)

set paused state of replay

Example:

local test = be.nodeStream
test:record("test.file")
test:setPaused(true) -- pause the game
setSpeed(float speed)

set the speed of replay

  • float speed: The speed, the replay is played

Example:

local test = be.nodeStream
test:record("test.file")
test:setPaused(true) -- pause the game
requestState()

class GBitmap

class GBitmap
GBitmap()

Creates a new Bitmap-Object

Example: Creates a new Bitmap and fills with red color and saves to the Documentsfolder.

local bmap = GBitmap(150, 150)
bmap:init(150, 150)
bmap:fillColor(ColorI(255, 0, 0, 255))
bmap:saveFile("image.jpg")
GBitmap(GBitmap)

Parameters

  • GBitmap map: The bitmap to be copied

Example:

local bmap = GBitmap()
bmap:init(150, 150)
bmap:fillColor(ColorI(255, 0, 0, 255))
bmap:saveFile("image.jpg")
local bmap2 = GBitmap(bmap)
bmap2:saveFile("image2.jpg")
init(U32 w, U32 h)

allocate the bitmap object

Parameters

  • int w: The width of the Bitmap
  • int h: The height of the Bitmap

Example: see GBitmap()

bool getColor(int x, int y, ColorI rColor)

gets the color of the pixel pointed by x and y

Parameters

int x: The x-Position of the Bitmap int y: The y-Position of the Bitmap ColorI: A color Object, The value is overwritten with the queried color.

Returns:(bool) true if its equal, false otherwise

Example:

local bmap = GBitmap(150, 150)
bmap:init(150, 150)
bmap:fillColor(ColorI(255, 0, 0, 255))
local color_new = ColorI(0,0,0,0)
local color = bmap:getColor(100, 100, color_new)
print(color_new) -- prints 255, 0, 0, 255)
bool setColor(int x, int y, ColorI rColor)

get the color of the pixel pointed by x and y

Parameters

  • int x: The x-Position of the Bitmap
  • int y: The y-Position of the Bitmap
  • ColorI: The new Color of this pixel
Returns:(bool) true if it was successfull, false otherwise

Example

local bmap = GBitmap(150, 150)
bmap:init(150, 150)
bmap:fillColor(ColorI(255, 0, 0, 255))
bmap:setColor(100, 100, ColorI(255, 255, 255, 255))
local color_new = ColorI(0,0,0,0)
local color = bmap:getColor(100, 100, color_new)
print(color_new) -- prints 255, 255, 255, 255
fillColor(ColorI rColor)

Fills the first mip level of the bitmap with the specified color.

Parameters

  • ColorI rColor: The Color to fill the Bitmap

Example:

local bmap = GBitmap(150, 150)
bmap:init(150, 150)
bmap:fillColor(ColorI(255, 0, 0, 255))
bmap:saveFile("image.jpg")
int getWidth(int in_mipLevel = 0)

get the width of the selected mipmap

Parameters

  • int in_mipLevel: The Miplevel you want to get the width of. Standard = 0

Example:

local bmap = GBitmap(150, 150)
bmap:init(150, 150)
bmap:fillColor(ColorI(255, 0, 0, 255))
print(bmap:getWidth()) -- prints 150
int getHeight(int in_mipLevel = 0)

get the height of the selected mipmap

Parameters

  • int in_mipLevel: The Miplevel you want to get the width of. Standard = 0

Example:

local bmap = GBitmap(150, 150)
bmap:init(150, 150)
bmap:fillColor(ColorI(255, 0, 0, 255))
print(bmap:getHeight()) -- prints 150
int getBytesPerPixel()

Gets the Bytes per Pixel

Returns:(int) The Bytes per Pixel

Example

local bmap = GBitmap(150, 150)
bmap:init(150, 150)
bmap:fillColor(ColorI(255, 0, 0, 255))
print(bmap:getBytesPerPixel()) -- prints 3
bool loadFile(string filePath)

Writes the new Image in the Bitmap

Parameters

  • string filePath: The path you want to get the Bitmap from
Returns:true on sucess

Example:

local bmap = GBitmap()
bmap:loadFile("image.jpg") --image.jpg must be present in the folder: "Documents/BeamNG.drive"
bmap:saveFile("image2.jpg") -- Copies the first Bitmap and Writes to a new file.
bool saveFile(string filePath)

Saves the Bitmap to a File in Documents-Directory (Documents/BeamNG.drive)

Parameters

  • string filePath: The path you want to write the Bitmap in
Returns:true on sucess

Example

local bmap = GBitmap(150, 150)
bmap:init(150, 150)
bmap:fillColor(ColorI(255, 0, 0, 255))
bmap:saveFile("image.jpg")