The two tables File and Config help a module read its own configuration file instead of hard-coding values in the code.
Path rules
- A relative path is counted from the
Data\LuaAPI\folder. For example"Includes\\MyModule\\Config.ini"is the fileData\LuaAPI\Includes\MyModule\Config.ini. - A path starting with
..\\is counted from the GameServer folder. For example"..\\Data\\Event\\Abc.xml". - A path containing the character
:(with a drive name) is used verbatim. - In a Lua string, the
\character must be written twice (\\) or use/instead. - File names with non-ASCII characters (for example Chinese) are supported when the script is saved as UTF-8.
File.ReadText, File.ReadIni, File.ReadXml do not raise a Lua error when the file cannot be read: they return nil together with an error message string. Check the result before using it.File
File.Exists
Checks whether a file exists (folders do not count).
File.Exists(path) -> boolean
| Parameter | Type | Description |
|---|---|---|
path | string | File path (see the path rules) |
Returns: true if it is an existing file; false if it does not exist, is a folder, or the path is empty.
if File.Exists("Includes\\MyModule\\Config.ini") then ... end
File.ReadText
Reads a text file into a table of lines. It strips the trailing line-break characters of each line and the UTF-8 BOM on the first line.
File.ReadText(path) -> table | nil, message
| Parameter | Type | Description |
|---|---|---|
path | string | File path |
Returns: a table of strings, each element being one line (index from 1); or nil and the string cannot open file: ... if the file cannot be opened.
local lines, err = File.ReadText("Includes\\MyModule\\Quiz.txt")
if not lines then Log.AddC(1, err) return end
for i, line in ipairs(lines) do Log.Add(line) end
File.ReadIni
Reads an INI file into a nested table { [Section] = { Key = "Value" } }. A line starting with ; or # is a comment. A value enclosed in a pair of "..." has its quotes removed. A key that comes before any [Section] is stored in the section with the empty name [""]. Every value is a string; use tonumber to convert to a number.
File.ReadIni(path) -> table | nil, message
| Parameter | Type | Description |
|---|---|---|
path | string | INI file path |
Returns: a table, or nil and cannot open file: ... if the file cannot be opened.
local ini = File.ReadIni("Includes\\MyModule\\Config.ini")
if ini then
local rate = tonumber(ini["General"]["Rate"]) or 1
end
File.ReadXml
Reads an XML file into a tree of tables. Each tag is a table with the fields: name (the tag name), attr (a table of attributes { Name = "value" }), kids (an array of child tags, tags only, from index 1) and text (the text content). The function returns the root tag (the outermost tag).
File.ReadXml(path) -> table | nil, message
| Parameter | Type | Description |
|---|---|---|
path | string | XML file path |
Returns: the root tag table; or nil and an error message (the file cannot be opened, the XML has a syntax error with its position, or the XML is nested more than 64 levels deep).
local root = File.ReadXml("Includes\\MyModule\\Items.xml")
if root then
for _, node in ipairs(root.kids) do
Log.Add(node.name .. " Id=" .. tostring(node.attr.Id))
end
end
Config (Windows-style INI files)
The Config.* functions read / write a single key of an INI file directly by section and key. Paths follow the same rules as above.
Config.ReadNumber
Reads a numeric value in an INI file.
Config.ReadNumber(section, key, path, default) -> number
| Parameter | Type | Description |
|---|---|---|
section | string | Section name (without the square brackets) |
key | string | Key name |
path | string | INI file path |
default | number | Value used when it cannot be read (default 0) |
Returns: the numeric value; default if the file does not exist, the key does not exist or the value is empty. Short names: ConfigReadNumber(section, key, path) and ReadConfigNumber (the default is always 0).
local rate = Config.ReadNumber("General", "Rate", "Includes\\MyModule\\Config.ini", 1)
Config.ReadString
Reads a string value in an INI file (at most about 4000 characters).
Config.ReadString(section, key, path, default) -> string
| Parameter | Type | Description |
|---|---|---|
section | string | Section name |
key | string | Key name |
path | string | INI file path |
default | string | Value used when it cannot be read (default empty string) |
Returns: the string that was read, or default. Short name: ConfigReadString(section, key, path) (the default is an empty string).
local title = Config.ReadString("General", "Title", "Includes\\MyModule\\Config.ini", "Hello")
Config.SaveString
Writes a value into an INI file. Only files with the .ini extension located in the server's Data folder can be written.
Config.SaveString(section, key, value, path) -> boolean
| Parameter | Type | Description |
|---|---|---|
section | string | Section name (must not be empty) |
key | string | Key name (must not be empty) |
value | string | Value to write (a number is converted to a string) |
path | string | INI file path; must have the .ini extension and be inside the Data folder (a path containing .. after normalization is rejected) |
Returns: true if the write succeeded; false if the section / key is missing, the file is outside Data, the file does not have the .ini extension or the write failed. Short name: ConfigSaveString.
Config.SaveString("State", "LastReset", tostring(os.time()), "Includes\\MyModule\\State.ini")