Skip to content

Commit 9aa1ef8

Browse files
committedMay 28, 2025·
File functions
1 parent e28abb7 commit 9aa1ef8

36 files changed

+525
-107
lines changed
 

‎elements/File/file.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
name: 'file'
2+
description: |
3+
THIS FUNCTION NEEDS DOCUMENTATION
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
local newFile = fileCreate("test.txt") -- attempt to create a new file
2+
if newFile then -- check if the creation succeeded
3+
fileWrite(newFile, "This is a test file!") -- write a text line
4+
fileClose(newFile) -- close the file once you're done with it
5+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
addEventHandler("onClientResourceStart", resourceRoot, function(res)
2+
local filePath = ":"..getResourceName(res).."/test.txt"
3+
fileCreate(filePath) --create the file in this resource and name it 'test.txt'.
4+
if fileCopy(filePath,":"..getResourceName(res).."/test1.txt") then
5+
outputChatBox("File was successfully copied!", 0, 100, 0)
6+
else
7+
outputChatBox("File was not successfully copied, probably because it doesn't exist.", 100, 0, 0)
8+
end
9+
end)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
addEventHandler("onResourceStart", resourceRoot, function(res)
2+
local filePath = ":"..getResourceName(res).."/test.txt"
3+
fileCreate(filePath) --create the file in this resource and name it 'test.txt'.
4+
if fileCopy(filePath, ":"..getResourceName(res).."/test1.txt") then
5+
outputChatBox("File was successfully copied!", root, 0, 100, 0)
6+
else
7+
outputChatBox("File was not successfully copied, probably because it doesn't exist.", root, 100, 0, 0)
8+
end
9+
end)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
local newFile = fileCreate("test.txt") -- attempt to create a new file
2+
if (newFile) then -- check if the creation succeeded
3+
fileWrite(newFile, "This is a test file!") -- write a text line
4+
fileClose(newFile) -- close the file once you're done with it
5+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
local newFile = fileCreate("test.txt") -- attempt to create a new file
2+
if (newFile) then -- check if the creation succeeded
3+
fileWrite(newFile, "This is a test file!") -- write a text line
4+
fileClose(newFile) -- close the file once you're done with it
5+
fileDelete("test.txt") -- delete file
6+
end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function checkExistingFile(player,cmd,filename,resourcename)
2+
if not filename then -- if the player didn't include a filename
3+
outputChatBox("ERROR: Syntax '/checkfile filename resourcename(optional)'.",player) -- display error
4+
return false -- stop function
5+
end
6+
if not resourcename then -- if the player didn't specify the resource he wants to check, use current resource
7+
resourcename = getResourceName(resource) --every resource has a predefined global variable called resource that contains the resource pointer for that resource, in other words, the value that getThisResource() function returns.
8+
else
9+
if not getResourceFromName(resourcename) then -- if a resource with that name doesn't exist, output error and stop function
10+
outputChatBox("ERROR: Resource "..resourcename.." doesn't exist.",player) -- output error message
11+
return false -- stop the function here
12+
end
13+
end
14+
-- as it hasn't stopped anywhere, we have both correct resourcename and filename
15+
local exists = fileExists((":%s/%s"):format(resourcename,filename)) -- using shorter format of string.format, see StringLibraryTutorial in lua wiki for that
16+
if exists then
17+
outputChatBox(("The file %q in resource %q exists"):format(filename,resourcename))
18+
else
19+
outputChatBox(("The file %q in resource %q doesn't exist"):format(filename,resourcename))
20+
end
21+
end
22+
addCommandHandler("exists",checkExistingFile)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
local fileHandle = fileCreate("test.txt")
2+
if fileHandle then
3+
fileWrite(fileHandle, "Line 1")
4+
fileFlush(fileHandle)
5+
-- ... further writing operations
6+
fileClose(fileHandle)
7+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
local handle = fileOpen("code.lua", true)
2+
local buffer = fileGetContents(handle) -- code.lua must be listed in meta.xml (for example as <file> for this example)
3+
fileClose(handle)
4+
5+
if buffer then
6+
loadstring(buffer)() -- This is just an example. You should avoid using loadstring. If you are dealing with configuration use json functions instead for security reasons.
7+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
local newFile = fileCreate("test.txt") -- attempt to create a new file
2+
if (newFile) then -- check if the creation succeeded
3+
local path = fileGetPath(newFile)
4+
outputChatBox("New file created at: "..path, root, 0, 255, 0)
5+
fileClose(newFile) -- close the file once you're done with it
6+
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
local hFile = fileOpen("test.txt", true) -- attempt to open the file (read only)
2+
if hFile then -- check if it was successfully opened
3+
local buffer
4+
while not fileIsEOF(hFile) do -- as long as we're not at the end of the file...
5+
buffer = fileRead(hFile, 500) -- ... read the next 500 bytes...
6+
outputConsole(buffer.."Current Position: "..fileGetPos(hFile)) -- ... and output them to the console and outputs the current read position
7+
end
8+
fileClose(hFile) -- close the file once we're done with it
9+
else
10+
outputConsole("Unable to open test.txt")
11+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
local newFile = fileCreate("test.txt") -- attempt to create a new file
2+
if (newFile) then -- check if the creation succeeded
3+
fileWrite(newFile, "This is a test file!") -- write a text line
4+
local size = fileGetSize(newFile) -- get size
5+
if size then
6+
outputChatBox("Size of test.txt is: "..size, source) -- output size
7+
end
8+
fileClose(newFile) -- close the file once you're done with it
9+
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
local hFile = fileOpen("test.txt", true) -- attempt to open the file (read only)
2+
if hFile then -- check if it was successfully opened
3+
local buffer
4+
while not fileIsEOF(hFile) do -- as long as we're not at the end of the file...
5+
buffer = fileRead(hFile, 500) -- ... read the next 500 bytes...
6+
outputConsole(buffer) -- ... and output them to the console
7+
end
8+
fileClose(hFile) -- close the file once we're done with it
9+
else
10+
outputConsole("Unable to open test.txt")
11+
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
local hFile = fileOpen("test.txt", true) -- attempt to open the file (read only)
2+
if hFile then -- check if it was successfully opened
3+
local buffer
4+
while not fileIsEOF(hFile) do -- as long as we're not at the end of the file...
5+
buffer = fileRead(hFile, 500) -- ... read the next 500 bytes...
6+
outputConsole(buffer) -- ... and output them to the console
7+
end
8+
fileClose(hFile) -- close the file once we're done with it
9+
else
10+
outputConsole("Unable to open test.txt")
11+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
local hFile = fileOpen("test.txt") -- attempt to open the file (read and write mode)
2+
if hFile then -- check if it was successfully opened
3+
fileSetPos( hFile, fileGetSize( hFile ) ) -- move position to the end of the file
4+
fileWrite(hFile, "hello" ) -- append data
5+
fileFlush(hFile) -- Flush the appended data into the file.
6+
fileClose(hFile) -- close the file once we're done with it
7+
else
8+
outputConsole("Unable to open test.txt")
9+
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function readFile(path)
2+
local file = fileOpen(path) -- attempt to open the file
3+
if not file then
4+
return false -- stop function on failure
5+
end
6+
local count = fileGetSize(file) -- get file's total size
7+
local data = fileRead(file, count) -- read whole file
8+
fileClose(file) -- close the file once we're done with it
9+
outputConsole(data) -- output code in console
10+
end
11+
12+
addCommandHandler("readfile",function(cmd,fileName) -- add command to test this function
13+
readFile(fileName) -- execute the function
14+
end)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
if fileRename( "test1.txt", "test2.txt" ) then
2+
outputConsole("File `test1.txt` successfully renamed to `test2.txt`")
3+
else
4+
outputConsole("Unable to rename `test1.txt`")
5+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
if fileRename( "test1.txt", "myFolder/test1.txt" ) then
2+
outputConsole("File `test1.txt` successfuly moved to `myFolder` folder")
3+
else
4+
outputConsole("Unable to move `test1.txt`")
5+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
local hFile = fileOpen("test.dat") -- attempt to open the file
2+
if hFile then -- check if it succeeded
3+
fileSetPos(hFile, 8) -- set the read/write position
4+
local readByte = fileRead(hFile, 1) -- read one byte from this position
5+
outputConsole("Byte at position 8 = " .. string.byte(readByte)) -- output it
6+
fileClose(hFile) -- close the file
7+
else
8+
outputConsole("Unable to open test.dat")
9+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
local fileHandle = fileCreate("test.txt") -- attempt to create a new file
2+
if fileHandle then -- check if the creation succeeded
3+
fileWrite(fileHandle, "This is a test file!") -- write a text line
4+
fileClose(fileHandle) -- close the file once you're done with it
5+
end

‎functions/File/fileClose.yaml

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
1-
# Scraped from: https://wiki.multitheftauto.com/wiki/fileClose
21
shared: &shared
32
name: fileClose
4-
description: THIS FUNCTION NEEDS DOCUMENTATION
5-
6-
server:
7-
<<: *shared
8-
client:
9-
<<: *shared
3+
notes:
4+
- type: 'important'
5+
content: |
6+
It is important to remember to close a file after you've finished all your operations on it, especially if you've been writing to the file. If you don't close a file and your resource crashes, all changes to the file may be lost.
7+
oop:
8+
entity: file
9+
method: close
10+
description: Closes a file handle obtained by [[fileCreate]] or [[fileOpen]].
11+
parameters:
12+
- name: 'theFile'
13+
type: 'file'
14+
description: The file handle to close.
15+
returns:
16+
description: Returns true if successful, false otherwise.
17+
values:
18+
- type: bool
19+
name: result
20+
examples:
21+
- path: 'examples/fileClose-1.lua'
22+
description: This example creates a text file and writes a string to it.

‎functions/File/fileCopy.yaml

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,38 @@
1-
# Scraped from: https://wiki.multitheftauto.com/wiki/fileCopy
21
shared: &shared
32
name: fileCopy
4-
description: THIS FUNCTION NEEDS DOCUMENTATION
3+
notes:
4+
- type: 'tip'
5+
content: |
6+
If you do not want to share the content of the created file with other servers, prepend the file path with @ (See [[filepath]] for more information).
7+
oop:
8+
entity: file
9+
method: copy
10+
description: This function copies a file.
11+
parameters:
12+
- name: 'filePath'
13+
type: 'string'
14+
description: The path of the file you want to copy.
15+
- name: 'copyToFilePath'
16+
type: 'string'
17+
description: Where to copy the specified file to.
18+
- name: 'overwrite'
19+
type: 'bool'
20+
description: If set to true it will overwrite a file that already exists at `copyToFilePath`.
21+
default: 'false'
22+
returns:
23+
description: Return true if the file was copied, else false if the `filePath` doesn't exist.
24+
values:
25+
- type: bool
26+
name: result
27+
28+
client:
29+
<<: *shared
30+
examples:
31+
- path: 'examples/fileCopy-1.lua'
32+
description: This example copies a file called `test.txt` and called it `test1.txt`.
533

634
server:
735
<<: *shared
8-
client:
9-
<<: *shared
36+
examples:
37+
- path: 'examples/fileCopy-2.lua'
38+
description: This example copies a file called `test.txt` and called it `test1.txt`.

‎functions/File/fileCreate.yaml

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,28 @@
1-
# Scraped from: https://wiki.multitheftauto.com/wiki/fileCreate
21
shared: &shared
32
name: fileCreate
4-
description: THIS FUNCTION NEEDS DOCUMENTATION
5-
6-
server:
7-
<<: *shared
8-
client:
9-
<<: *shared
3+
notes:
4+
- type: 'important'
5+
content: |
6+
To prevent memory leaks, ensure each successful call to [[fileCreate]] has a matching call to [[fileClose]].
7+
- type: 'tip'
8+
content: |
9+
If you do not want to share the content of the created file with other servers, prepend the file path with @ (See [[filepath]] for more information).
10+
- type: 'important'
11+
content: |
12+
It is important to remember to close a file after you've finished all your operations on it, especially if you've been writing to the file. If you don't close a file and your resource crashes, all changes to the file may be lost.
13+
oop:
14+
entity: file
Code has comments. Press enter to view.
15+
method: new
16+
description: Creates a new file in a directory of a resource. If there already exists a file with the specified name, it is overwritten with an empty file.
17+
parameters:
18+
- name: 'filePath'
19+
type: 'string'
20+
description: The path of the file you want to copy.
21+
returns:
22+
description: If successful, returns a [[file]] handle which can be used with other file functions ([[fileWrite]], [[fileClose]] etc.). Returns false if an error occured.
23+
values:
24+
- type: bool
25+
name: result
26+
examples:
27+
- path: 'examples/fileCreate-1.lua'
28+
description: This example creates a text file in the current resource and writes a string to it.

‎functions/File/fileDelete.yaml

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
1-
# Scraped from: https://wiki.multitheftauto.com/wiki/fileDelete
21
shared: &shared
32
name: fileDelete
4-
description: THIS FUNCTION NEEDS DOCUMENTATION
5-
6-
server:
7-
<<: *shared
8-
client:
9-
<<: *shared
3+
oop:
4+
entity: file
5+
method: delete
6+
description: Deletes the specified file.
7+
parameters:
8+
- name: 'filePath'
9+
type: 'string'
10+
description: |
11+
The [filepath](/filepath) of the file to delete in the following format: `:resourceName/path`. `resourceName` is the name of the resource the file is in, and 'path' is the path from the root directory of the resource to the file.
12+
For example, if you want to delete a file name "myFile.txt" in the resource 'fileres', it can be deleted from another resource this way: `fileDelete(":fileres/myFile.txt")`.
13+
If the file is in the current resource, only the file path is necessary, e.g. `fileDelete("myFile.txt")`.
14+
returns:
15+
description: Returns true if successful, false otherwise (for example if there exists no file with the given name, or it does exist but is in use).
16+
values:
17+
- type: bool
18+
name: result
19+
examples:
20+
- path: 'examples/fileDelete-1.lua'
21+
description: This example will show us how to create a file "text.txt" spell it "This is a test file!", Close the file and delete it.

‎functions/File/fileExists.yaml

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
1-
# Scraped from: https://wiki.multitheftauto.com/wiki/fileExists
21
shared: &shared
32
name: fileExists
4-
description: THIS FUNCTION NEEDS DOCUMENTATION
5-
6-
server:
7-
<<: *shared
8-
client:
9-
<<: *shared
3+
oop:
Code has comments. Press enter to view.
4+
entity: file
5+
method: exists
6+
description: This functions checks whether a specified file exists inside a resource.
7+
parameters:
8+
- name: 'filePath'
9+
type: 'string'
10+
description: |
11+
The [filepath](/filepath) of the file, whose existence is going to be checked, in the following format: `:resourceName/path`. `resourceName` is the name of the resource the file is checked to be in, and 'path' is the path from the root directory of the resource to the file.
12+
For example, if you want to check whether a file named 'myfile.txt' exists in the resource 'mapcreator', it can be done from another resource this way: `fileExists(":mapcreator/myfile.txt")`.
13+
If the file, whose existence is going to be checked, is in the current resource, only the file path is necessary, e.g. `fileExists("myfile.txt")`. Note that you must use forward slashes '/' for the folders, backslashes '\' will return false.
14+
returns:
15+
description: Returns true if the file exists, false otherwise.
16+
values:
17+
- type: bool
18+
name: result
19+
examples:
20+
- path: 'examples/fileExists-1.lua'
21+
description: This example checks if a file exists in a resource directory.

‎functions/File/fileFlush.yaml

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
1-
# Scraped from: https://wiki.multitheftauto.com/wiki/fileFlush
21
shared: &shared
32
name: fileFlush
4-
description: THIS FUNCTION NEEDS DOCUMENTATION
5-
6-
server:
7-
<<: *shared
8-
client:
9-
<<: *shared
3+
notes:
4+
- type: 'info'
5+
content: '[[fileClose]] automatically flushes the file.'
6+
oop:
7+
entity: file
8+
method: flush
9+
description: |
10+
Forces pending disk writes to be executed. [[fileWrite]] doesn't directly write to the hard disk but places the data in a temporary buffer; only when there is enough data in the buffer it is actually written to disk. Call this function if you need the data written right now without closing the file. This is useful for log files that might want to be read while the resource is still executing. `fileFlush` can be called after each log entry is written. Without this, the file may appear empty or outdated to the user.
11+
parameters:
12+
- name: 'theFile'
13+
type: 'file'
14+
description: The file handle of the file you wish to flush.
15+
returns:
16+
description: Returns true if succeeded, false in case of failure (e.g. the file handle is invalid).
17+
values:
18+
- type: bool
19+
name: result
20+
examples:
21+
- path: 'examples/fileFlush-1.lua'

‎functions/File/fileGetContents.yaml

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
1-
# Scraped from: https://wiki.multitheftauto.com/wiki/fileGetContents
21
shared: &shared
32
name: fileGetContents
4-
description: THIS FUNCTION NEEDS DOCUMENTATION
5-
6-
server:
7-
<<: *shared
8-
client:
9-
<<: *shared
3+
notes:
4+
- type: 'important'
5+
content: |
6+
Please note that even if you enable SD #22 and #23 on your server, you are not protected from user attacks, which can happen after verification of the file, but before you read the contents of such verified file. This function enables you to safely read the contents of a meta.xml-listed file on both client and server.
7+
oop:
8+
entity: file
9+
method: getContents
10+
description: |
11+
Reads the entire contents of the file, optionally verifies the read contents by computing and comparing the checksum with the expected one, and returns the content as string. The file cursor position is not modified by calls to this function. File must be added in the [[meta.xml]].
12+
parameters:
13+
- name: 'theFile'
14+
type: 'file'
15+
description: A handle to the file you wish to get the contents from. Use [[fileOpen]] to obtain this handle.
16+
- name: 'verifyContents'
17+
type: 'bool'
18+
description: Set to true, to compare the computed and the expected checksum of the file content.
19+
default: 'true'
20+
returns:
21+
description: Returns the bytes that were read from the file, but only if verification was disabled or if the checksum comparison succeeded. On failure, this function returns nil.
22+
values:
23+
- type: nil|string
24+
name: result
25+
examples:
26+
- path: 'examples/fileGetContents-1.lua'
27+
description: This example opens the code.lua file, checks its contents, and then runs it.

‎functions/File/fileGetPath.yaml

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1-
# Scraped from: https://wiki.multitheftauto.com/wiki/fileGetPath
21
shared: &shared
32
name: fileGetPath
4-
description: THIS FUNCTION NEEDS DOCUMENTATION
5-
6-
server:
7-
<<: *shared
8-
client:
9-
<<: *shared
3+
oop:
4+
entity: file
5+
method: getPath
6+
variable: path
7+
description: This function retrieves the path of the given file.
8+
parameters:
9+
- name: 'theFile'
10+
type: 'file'
11+
description: The file you want to get the path.
12+
returns:
13+
description: Returns a string representing the file path, false if invalid file was provided.
14+
values:
15+
- type: string|false
16+
name: result
17+
examples:
18+
- path: 'examples/fileGetPath-1.lua'

‎functions/File/fileGetPos.yaml

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1-
# Scraped from: https://wiki.multitheftauto.com/wiki/fileGetPos
21
shared: &shared
32
name: fileGetPos
4-
description: THIS FUNCTION NEEDS DOCUMENTATION
5-
6-
server:
7-
<<: *shared
8-
client:
9-
<<: *shared
3+
pair: fileSetPos
4+
oop:
5+
entity: file
6+
method: getPos
7+
variable: pos
8+
description: Returns the current read/write position in the given file.
9+
parameters:
10+
- name: 'theFile'
11+
type: 'file'
12+
description: The file handle you wish to get the position of.
13+
returns:
14+
description: Returns the file position if successful, or false if an error occured (e.g. an invalid handle was passed).
15+
values:
16+
- type: int|false
17+
name: result
18+
examples:
19+
- path: 'examples/fileGetPos-1.lua'
20+
description: This example opens the file test.txt and outputs its contents and current read position to the console.

‎functions/File/fileGetSize.yaml

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1-
# Scraped from: https://wiki.multitheftauto.com/wiki/fileGetSize
21
shared: &shared
32
name: fileGetSize
4-
description: THIS FUNCTION NEEDS DOCUMENTATION
5-
6-
server:
7-
<<: *shared
8-
client:
9-
<<: *shared
3+
oop:
4+
entity: file
5+
method: getSize
6+
variable: size
7+
description: Returns the total size in **bytes** of the given file.
8+
parameters:
9+
- name: 'theFile'
10+
type: 'file'
11+
description: The file handle you wish to get the size of.
12+
returns:
13+
description: Returns the file size if successful, or false if an error occured (e.g. an invalid file handle was passed).
14+
values:
15+
- type: int|false
16+
name: result
17+
examples:
18+
- path: 'examples/fileGetSize-1.lua'

‎functions/File/fileIsEOF.yaml

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
1-
# Scraped from: https://wiki.multitheftauto.com/wiki/fileIsEOF
21
shared: &shared
32
name: fileIsEOF
4-
description: THIS FUNCTION NEEDS DOCUMENTATION
5-
6-
server:
7-
<<: *shared
8-
client:
9-
<<: *shared
3+
notes:
4+
- type: 'warning'
5+
content: Due to underlying C API restrictions this function may return false until an attempt to read further than the end of the file is made.
6+
- type: 'info'
7+
content: |
8+
When you open a file, its file position is set to the beginning of the file. Each call to [[fileRead]] or [[fileWrite]] moves the position ahead by the amount of bytes that were read/written. This way, by using [[fileIsEOF]] you can check if you've passed through the whole file.
9+
oop:
10+
entity: file
11+
method: isEOF
12+
variable: eof
13+
description: Checks if the file position is at the end of the file.
14+
parameters:
15+
- name: 'theFile'
16+
type: 'file'
17+
description: A handle to the file you wish to check.
18+
returns:
19+
description: Returns true if the file position of the specified file is at the end of the file, false otherwise.
20+
values:
21+
- type: bool
22+
name: result
23+
examples:
24+
- path: 'examples/fileIsEOF-1.lua'
25+
description: This example opens the file test.txt and outputs its contents to the console.

‎functions/File/fileOpen.yaml

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,29 @@
1-
# Scraped from: https://wiki.multitheftauto.com/wiki/fileOpen
21
shared: &shared
32
name: fileOpen
4-
description: THIS FUNCTION NEEDS DOCUMENTATION
5-
6-
server:
7-
<<: *shared
8-
client:
9-
<<: *shared
3+
notes:
4+
- type: 'important'
5+
content: To prevent memory leaks, ensure each successful call to [[fileOpen]] has a matching call to [[fileClose]].
6+
oop:
7+
entity: file
8+
method: open
9+
description: Opens an existing file for reading and writing.
10+
parameters:
11+
- name: 'filePath'
12+
type: 'string'
13+
description: |
14+
The [filepath](/filepath) of the file in the following format: `:resourceName/path`. `resourceName` is the name of the resource the file is in, and 'path' is the path from the root directory of the resource to the file.
15+
For example, if there is a file named `coolObjects.txt` in the resource `objectSearch`, it can be opened from another resource this way: `fileOpen(":objectSearch/coolObjects.txt")`.
16+
If the file is in the current resource, only the file path is necessary, e.g. `fileOpen("coolObjects.txt")`.
17+
- name: 'readOnly'
18+
type: 'bool'
19+
description: By default, the file is opened with reading and writing access. You can specify true for this parameter if you only need reading access.
20+
returns:
21+
description: If successful, returns a file handle for the file. Otherwise returns false (f.e. if the file doesn't exist).
22+
values:
23+
- type: file|false
24+
name: result
25+
examples:
26+
- path: 'examples/fileOpen-1.lua'
27+
description: This example opens the file test.txt that is in the root of the current resource, and outputs its contents to the console.
28+
- path: 'examples/fileOpen-2.lua'
29+
description: This example show how to append data to an existing file

‎functions/File/fileRead.yaml

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
1-
# Scraped from: https://wiki.multitheftauto.com/wiki/fileRead
21
shared: &shared
32
name: fileRead
4-
description: THIS FUNCTION NEEDS DOCUMENTATION
3+
notes:
4+
- type: 'info'
5+
content: '[[fileOpen]] sets the read/write position to the beginning of the file. [[fileGetSize]] gets the total size in bytes of given file.'
6+
oop:
7+
entity: file
8+
method: read
9+
description: Reads the specified number of bytes from the given file starting at its current read/write position, and returns them as a string.
10+
parameters:
11+
- name: 'theFile'
12+
type: 'file'
13+
description: A handle to the file you wish to read from. Use [fileOpen](/fileOpen) to obtain this handle.
14+
- name: 'count'
15+
type: 'int'
16+
description: The number of bytes you wish to read.
17+
returns:
18+
description: Returns the bytes that were read in a string. Note that this string might not contain as many bytes as you specified if an error occured, i.e. end of file.
19+
values:
20+
- type: string|false
21+
name: result
522

6-
server:
7-
<<: *shared
823
client:
9-
<<: *shared
24+
<<: *shared
25+
examples:
26+
- path: 'examples/fileRead-1.lua'
27+
description: This example opens the file test.txt and outputs its contents to the console.

‎functions/File/fileRename.yaml

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,28 @@
1-
# Scraped from: https://wiki.multitheftauto.com/wiki/fileRename
21
shared: &shared
32
name: fileRename
4-
description: THIS FUNCTION NEEDS DOCUMENTATION
5-
6-
server:
7-
<<: *shared
8-
client:
9-
<<: *shared
3+
notes:
4+
- type: 'info'
5+
content: |
6+
Also with this function you can move specified file to a new location, new folder or even to another resource's folder. But for this action executing resource must have `ModifyOtherObjects` [[ACL]] right set to true.
7+
oop:
8+
entity: file
9+
method: rename
10+
description: Renames the specified file.
11+
parameters:
12+
- name: 'filePath'
13+
type: 'string'
14+
description: |
15+
The filepath of the source file in the following format: `:resourceName/path`. `resourceName` is the name of the resource the file is in, and 'path' is the path from the root directory of the resource to the file. If the file is in the current resource, only the file path is necessary.
16+
- name: 'newFilePath'
17+
type: 'string'
18+
description: Destination [filepath](/filepath) for the specified source file in the same format.
19+
returns:
20+
description: If successful, returns true. Otherwise returns false.
21+
values:
22+
- type: bool
23+
name: result
24+
examples:
25+
- path: 'examples/fileRename-1.lua'
26+
description: This example renames the file test1.txt that is in the root of the current resource to test2.txt.
27+
- path: 'examples/fileRename-2.lua'
28+
description: This example moves the file test1.txt that is in the root of the current resource to myFolder folder. If this folder is not exists, it will be created before moving the file test1.txt.

‎functions/File/fileSetPos.yaml

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
1-
# Scraped from: https://wiki.multitheftauto.com/wiki/fileSetPos
21
shared: &shared
32
name: fileSetPos
4-
description: THIS FUNCTION NEEDS DOCUMENTATION
5-
6-
server:
7-
<<: *shared
8-
client:
9-
<<: *shared
3+
oop:
4+
entity: file
5+
method: setPos
6+
variable: pos
7+
description: Sets the current read/write position in the file.
8+
parameters:
9+
- name: 'theFile'
10+
type: 'file'
11+
description: The file handle of which you want to change the read/write position.
12+
- name: 'offset'
13+
type: 'int'
14+
description: The new position. This is the number of bytes from the beginning of the file. If this value is larger than the file size, it is limited to `52,428,800` bytes (50 MB).
15+
returns:
16+
description: Returns where the **offset** was actually set at. I.e. if offset was past the end of the file, it will be set at the end of the file, and this position will be returned. Returns false in case of failure (e.g. the specified file handle is invalid).
17+
values:
18+
- type: int|false
19+
name: result
20+
examples:
21+
- path: 'examples/fileSetPos-1.lua'
22+
description: This example opens a binary file and prints the value of the byte at position 8 to the console.

‎functions/File/fileWrite.yaml

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,29 @@
1-
# Scraped from: https://wiki.multitheftauto.com/wiki/fileWrite
21
shared: &shared
32
name: fileWrite
4-
description: THIS FUNCTION NEEDS DOCUMENTATION
5-
6-
server:
7-
<<: *shared
8-
client:
9-
<<: *shared
3+
notes:
4+
- type: 'important'
5+
content: |
6+
It is important to remember to close a file after you've finished all your operations on it, especially if you've been writing to the file. If you don't close a file and your resource crashes, all changes to the file may be lost.
7+
oop:
8+
entity: file
9+
method: write
10+
description: Writes one or more strings to a given file, starting at the current read/write position. Advances the position over the number of bytes that were written.
11+
parameters:
12+
- name: 'theFile'
13+
type: 'file'
14+
description: A handle to the file you wish to write to. The file must have been opened with write access, i.e. the file handle must be a result of [fileCreate](/fileCreate) or [fileOpen](/fileOpen) with the readonly parameter set to false.
15+
- name: 'string1'
16+
type: 'string'
17+
description: 'The string to write.'
18+
- name: 'string2'
19+
type: 'string'
20+
description: 'You can provide any number of additional strings to write after string1. These will be written in the order in which they are specified.'
21+
default: "nil"
22+
returns:
23+
description: Returns the number of bytes successfully written to the file, returns false if invalid arguments were specified.
24+
values:
25+
- type: int|false
26+
name: result
27+
examples:
28+
- path: 'examples/fileWrite-1.lua'
29+
description: This example creates a text file and writes a string to it.

0 commit comments

Comments
 (0)
Please sign in to comment.