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

0 commit comments

Comments
 (0)