multitheftauto / mtasa-blue

Multi Theft Auto is a game engine that incorporates an extendable network play element into a proprietary commercial single-player game.
https://multitheftauto.com
GNU General Public License v3.0
1.3k stars 412 forks source link

Read a file directly from path - `fileRead` and `fileGetContents` update #3490

Closed TracerDS closed 4 days ago

TracerDS commented 1 week ago

Currently, to read a file you have to have a boilerplate code for opening and closing a file:

function ReadFile(path)
    local file = fileOpen(path)
    if not file then return end
    local content = fileRead(file, fileGetSize(file))
    fileClose(file)
    return content
end

This PR will allow you to read file from path without having to define this boilerplate:

local content = fileRead('MyFile.txt')

local content = fileGetContents('MyFile.txt')
Fernando-A-Rocha commented 1 week ago

Genius

tederis commented 1 week ago

Are you aware of function fileGetContents? You can just fileGetContents(fileOpen("file.txt")).

TracerDS commented 1 week ago

Are you aware of function fileGetContents? You can just fileGetContents(fileOpen("file.txt")).

Does fileGetContents close the file automatically for you? It also requires a file handle. It was either that or modify fileGetContents

MegadreamsBE commented 1 week ago

Are you aware of function fileGetContents? You can just fileGetContents(fileOpen("file.txt")).

Does fileGetContents close the file automatically for you? It also requires a file handle. It was either that or modify fileGetContents

It doesn't no. So I'm fine with this change for quality of life. Perhaps fileGetContents could have something similar

tederis commented 1 week ago

Are you aware of function fileGetContents? You can just fileGetContents(fileOpen("file.txt")).

Does fileGetContents close the file automatically for you? It also requires a file handle. It was either that or modify fileGetContents

Oh, you're right, it doesn't. Okay then.

Fernando-A-Rocha commented 1 week ago

@TracerDS Make fileGetContents accept a string as argument, so it does the opening and closing of the file automatically. It can keep working with an open file handle as argument for backwards compatibility and certain use cases. This way, there is no need for a separate new function.

TracerDS commented 1 week ago

@TracerDS Make fileGetContents accept a string as argument, so it does the opening and closing of the file automatically. It can keep working with an open file handle as argument for backwards compatibility and certain use cases.

so, the same thing as fileRead but for fileGetContents?

Fernando-A-Rocha commented 1 week ago

@TracerDS Make fileGetContents accept a string as argument, so it does the opening and closing of the file automatically. It can keep working with an open file handle as argument for backwards compatibility and certain use cases.

so, the same thing as fileRead but for fileGetContents?

I don't see any cons to that

Fernando-A-Rocha commented 1 week ago

So uhmmm, arent fileRead and fileGetContents the same now? Is fileRead necessary?

MegadreamsBE commented 1 week ago

So uhmmm, arent fileRead and fileGetContents the same now? Is fileRead necessary?

One major difference at least is that fileGetContents requires files to be in meta.xml, and has the ability to validate them. But overall they are getting pretty close to each other

tederis commented 1 week ago

So uhmmm, arent fileRead and fileGetContents the same now? Is fileRead necessary?

One major difference at least is that fileGetContents requires files to be in meta.xml, and has the ability to validate them. But overall they are getting pretty close to each other

Only if the second argument is set to true.

Fernando-A-Rocha commented 1 week ago

I like fileRead but it's exactly the same as doing fileGetContents(path, false)

Can we have fileRead still?

TracerDS commented 1 week ago

I like fileRead but it's exactly the same as doing fileGetContents(path, false)

Can we have fileRead still?

There is no reason to remove it

Fernando-A-Rocha commented 1 week ago

I like fileRead but it's exactly the same as doing fileGetContents(path, false) Can we have fileRead still?

There is no reason to remove it

Sorry, I had a brain fart. Forgot fileRead already existed. This is nice QoL feature.

PR LGTM then :shipit: