Skip to main content

Content

Description:

  The Content record is a singleton object that manages file searching, loading and other operations related to resources.

Usage:

local Content = require("Content")
local text = Content:load("filename.txt")

searchPaths

Type: Field.

Description:

  An array of directories to search for resource files.

Signature:

searchPaths: {string}

assetPath

Type: Readonly Field.

Description:

  The path to the directory containing read-only resources.

Signature:

const assetPath: string

writablePath

Type: Readonly Field.

Description:

  The path to the directory where files can be written.

Signature:

const writablePath: string

load

Type: Function.

Description:

  Loads the content of the file with the specified filename.

Signature:

load: function(self: Content, filename: string): string

Parameters:

ParameterTypeDescription
filenamestringThe name of the file to load.

Returns:

Return TypeDescription
stringThe content of the loaded file.

loadExcel

Type: Function.

Description:

  Loads the content of an Excel file with the specified filename and optional sheet names

Signature:

loadExcel: function(self: Content, filename: string, sheetNames?: {string}):
{
--[[sheetName]] string:
--[[rows]] {
--[[colums]] {string | number}
}
} | nil

Parameters:

ParameterTypeDescription
filenamestringThe name of the Excel file to load
sheetNames{string}[optional] An array of strings representing the names of the sheets to load. If not provided, all sheets will be loaded.

Returns:

Return TypeDescription
tableA table containing the data in the Excel file. The keys are the sheet names and the values are tables containing the rows and columns of the sheet.

save

Type: Function.

Description:

  Saves the specified content to a file with the specified filename.

Signature:

save: function(self: Content, filename: string, content: string): boolean

Parameters:

ParameterTypeDescription
filenamestringThe name of the file to save.
contentstringThe content to save to the file.

Returns:

Return TypeDescription
booleantrue if the content saves to file successfully, false otherwise.

exist

Type: Function.

Description:

  Checks if a file with the specified filename exists.

Signature:

exist: function(self: Content, filename: string): boolean

Parameters:

ParameterTypeDescription
filenamestringThe name of the file to check.

Returns:

Return TypeDescription
booleantrue if the file exists, false otherwise.

mkdir

Type: Function.

Description:

  Creates a new directory with the specified path.

Signature:

mkdir: function(self: Content, path: string): boolean

Parameters:

ParameterTypeDescription
pathstringThe path of the directory to create.

Returns:

Return TypeDescription
booleantrue if the directory was created, false otherwise.

isdir

Type: Function.

Description:

  Checks if the specified path is a directory.

Signature:

isdir: function(self: Content, path: string): boolean

Parameters:

ParameterTypeDescription
pathstringThe path to check.

Returns:

Return TypeDescription
booleantrue if the path is a directory, false otherwise.

remove

Type: Function.

Description:

  Removes the file or directory with the specified path.

Signature:

remove: function(self: Content, path: string): boolean

Parameters:

ParameterTypeDescription
pathstringThe path of the file or directory to remove.

Returns:

Return TypeDescription
booleantrue if the file or directory was removed, false otherwise.

copy

Type: Function.

Description:

  Copies the file or directory in the specified path to target path.

Signature:

copy: function(self: Content, srcPath: string, dstPath: string): boolean

Parameters:

ParameterTypeDescription
srcPathstringThe path of the file or directory to copy.
dstPathstringThe path to copy files to.

Returns:

Return TypeDescription
booleantrue if the file or directory was copied to target path, false otherwise.

move

Type: Function.

Description:

  Moves the file or directory in the specified path to target path.

Signature:

move: function(self: Content, srcPath: string, dstPath: string): boolean

Parameters:

ParameterTypeDescription
srcPathstringThe path of the file or directory to move.
dstPathstringThe path to move files to.

Returns:

Return TypeDescription
booleantrue if the file or directory was moved to target path, false otherwise.

isAbsolutePath

Type: Function.

Description:

  Checks if the specified path is an absolute path.

Signature:

isAbsolutePath: function(self: Content, path: string): boolean

Parameters:

ParameterTypeDescription
pathstringThe path to check.

Returns:

Return TypeDescription
booleantrue if the path is an absolute path, false otherwise.

getFullPath

Type: Function.

Description:

  Gets the full path of a file with the specified filename.

Signature:

getFullPath: function(self: Content, filename: string): string

Parameters:

ParameterTypeDescription
filenamestringThe name of the file to get the full path of.

Returns:

Return TypeDescription
stringThe full path of the file.

insertSearchPath

Type: Function.

Description:

  Inserts a search path at the specified index.

Signature:

insertSearchPath: function(self: Content, index: integer, path: string)

Parameters:

ParameterTypeDescription
indexintegerThe index at which to insert the search path.
pathstringThe search path to insert.

addSearchPath

Type: Function.

Description:

  Adds a new search path to the end of the list.

Signature:

addSearchPath: function(self: Content, path: string)

Parameters:

ParameterTypeDescription
pathstringThe search path to add.

removeSearchPath

Type: Function.

Description:

  Removes the specified search path from the list.

Signature:

removeSearchPath: function(self: Content, path: string)

Parameters:

ParameterTypeDescription
pathstringThe search path to remove.

loadAsync

Type: Function.

Description:

  Asynchronously loads the content of the file with the specified filename.

Signature:

loadAsync: function(self: Content, filename: string): string

Parameters:

ParameterTypeDescription
filenamestringThe name of the file to load.

Returns:

Return TypeDescription
stringThe content of the loaded file.

loadExcelAsync

Type: Function.

Description:

  Asynchronously loads the content of an Excel file with the specified filename and optional sheet names.

Signature:

loadExcelAsync: function(self: Content, filename: string, sheetNames?: {string}):
{
--[[sheetName]] string:
--[[rows]] {
--[[colums]] {string | number}
}
} | nil

Parameters:

ParameterTypeDescription
filenamestringThe name of the Excel file to load.
sheetNames{string}[optional] An array of strings representing the names of the sheets to load. If not provided, all sheets will be loaded.

Returns:

Return TypeDescription
tableA table containing the data in the Excel file. The keys are the sheet names and the values are tables containing the rows and columns of the sheet.

saveAsync

Type: Function.

Description:

  Asynchronously saves the specified content to a file with the specified filename.

Signature:

saveAsync: function(self: Content, filename: string, content: string): boolean

Parameters:

ParameterTypeDescription
filenamestringThe name of the file to save.
contentstringThe content to save to the file.

Returns:

Return TypeDescription
booleantrue if the content was saved successfully, false otherwise.

copyAsync

Type: Function.

Description:

  Asynchronously copies a file or a folder from the source path to the destination path.

Signature:

copyAsync: function(self: Content, src: string, dst: string): boolean

Parameters:

ParameterTypeDescription
srcstringThe path of the file or folder to copy.
dststringThe destination path of the copied files.

Returns:

Return TypeDescription
booleantrue if the file or folder was copied successfully, false otherwise.

zipAsync

Type: Function.

Description:

  Asynchronously compresses the specified folder to a ZIP archive with the specified filename.

Signature:

zipAsync: function(self: Content, folderPath: string, zipFile: string, filter?: function(string): boolean): boolean

Parameters:

ParameterTypeDescription
folderPathstringThe path of the folder to compress, should be under the asset writable path.
zipFilestringThe name of the ZIP archive to create.
filterfunction[optional] A function to filter the files to include in the archive. The function takes a filename as input and returns a boolean indicating whether to include the file. If not provided, all files will be included.

Returns:

Return TypeDescription
booleantrue if the folder was compressed successfully, false otherwise.

unzipAsync

Type: Function.

Description:

  Asynchronously decompresses a ZIP archive to the specified folder.

Signature:

unzipAsync: function(self: Content, folderPath: string, zipFile: string, filter?: function(string): boolean): boolean

Parameters:

ParameterTypeDescription
zipFilestringThe name of the ZIP archive to decompress, should be a file under the asset writable path.
folderPathstringThe path of the folder to decompress to, should be under the asset writable path.
filterfunction[optional] A function to filter the files to include in the archive. The function takes a filename as input and returns a boolean indicating whether to include the file. If not provided, all files will be included.

Returns:

Return TypeDescription
booleantrue if the folder was decompressed successfully, false otherwise.

getDirs

Type: Function.

Description:

  Gets the names of all subdirectories in the specified directory.

Signature:

getDirs: function(self: Content, path: string): {string}

Parameters:

ParameterTypeDescription
pathstringThe path of the directory to search.

Returns:

Return TypeDescription
tableAn array of the names of all subdirectories in the specified directory.

getFiles

Type: Function.

Description:

  Gets the names of all files in the specified directory.

Signature:

getFiles: function(self: Content, path: string): {string}

Parameters:

ParameterTypeDescription
pathstringThe path of the directory to search.

Returns:

Return TypeDescription
tableAn array of the names of all files in the specified directory.

getAllFiles

Type: Function.

Description:

  Gets the names of all files in the specified directory and its subdirectories.

Signature:

getAllFiles: function(self: Content, path: string): {string}

Parameters:

ParameterTypeDescription
pathstringThe path of the directory to search.

Returns:

Return TypeDescription
tableAn array of the names of all files in the specified directory and its subdirectories.

clearPathCache

Type: Function.

Description:

  Clears the search path cache of the map of relative paths to full paths.

Signature:

clearPathCache: function(self: Content)