Skip to content

Commit 9aa1ef8

Browse files
committed
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
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:
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.

0 commit comments

Comments
 (0)