mirror of
https://github.com/love2d/love.git
synced 2026-08-12 08:30:56 +02:00
Replace NativeFile to use SDL IO functions.
Also remove Android-specific code from it as SDL is now able to handle them.
This commit is contained in:
+3
-5
@@ -482,11 +482,9 @@ add_library(love_filesystem_physfs STATIC
|
|||||||
src/modules/filesystem/physfs/PhysfsIo.h
|
src/modules/filesystem/physfs/PhysfsIo.h
|
||||||
src/modules/filesystem/physfs/PhysfsIo.cpp
|
src/modules/filesystem/physfs/PhysfsIo.cpp
|
||||||
)
|
)
|
||||||
if(ANDROID)
|
target_link_libraries(love_filesystem_physfs PUBLIC
|
||||||
target_link_libraries(love_filesystem_physfs PUBLIC
|
|
||||||
lovedep::SDL
|
lovedep::SDL
|
||||||
)
|
)
|
||||||
endif()
|
|
||||||
|
|
||||||
add_library(love_filesystem INTERFACE)
|
add_library(love_filesystem INTERFACE)
|
||||||
target_link_libraries(love_filesystem INTERFACE
|
target_link_libraries(love_filesystem INTERFACE
|
||||||
@@ -1962,7 +1960,7 @@ love_group_projects(NAME "liblove" NESTED TARGETS ${LIBLOVE_DEPENDENCIES})
|
|||||||
love_group_projects(NAME "liblove/libraries" NESTED TARGETS ${LIBLOVE_LIBRARIES})
|
love_group_projects(NAME "liblove/libraries" NESTED TARGETS ${LIBLOVE_LIBRARIES})
|
||||||
love_group_projects(NAME "liblove" TARGETS liblove ${LOVE_EXTRA_DEPENDECIES})
|
love_group_projects(NAME "liblove" TARGETS liblove ${LOVE_EXTRA_DEPENDECIES})
|
||||||
|
|
||||||
love_group_projects(NAME "lovedep" TARGETS lovedep::SDL3 lovedep::Freetype lovedep::Harfbuzz lovedep::OpenAL lovedep::Modplug lovedep::Theora lovedep::Vorbis lovedep::Ogg lovedep::Zlib lovedep::Lua)
|
love_group_projects(NAME "lovedep" TARGETS lovedep::SDL lovedep::Freetype lovedep::Harfbuzz lovedep::OpenAL lovedep::Modplug lovedep::Theora lovedep::Vorbis lovedep::Ogg lovedep::Zlib lovedep::Lua)
|
||||||
love_group_projects(NAME "lovedep" TARGETS lua51 alcommon al-excommon harfbuzz-subset zlib)
|
love_group_projects(NAME "lovedep" TARGETS lua51 alcommon al-excommon harfbuzz-subset zlib)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -42,22 +42,22 @@ namespace filesystem
|
|||||||
{
|
{
|
||||||
|
|
||||||
NativeFile::NativeFile(const std::string &filename, Mode mode)
|
NativeFile::NativeFile(const std::string &filename, Mode mode)
|
||||||
: filename(filename)
|
: filename(filename)
|
||||||
, file(nullptr)
|
, file(nullptr)
|
||||||
, mode(MODE_CLOSED)
|
, mode(MODE_CLOSED)
|
||||||
, bufferMode(BUFFER_NONE)
|
, bufferMode(BUFFER_NONE)
|
||||||
, bufferSize(0)
|
, bufferSize(0)
|
||||||
{
|
{
|
||||||
if (!open(mode))
|
if (!open(mode))
|
||||||
throw love::Exception("Could not open file at path %s", filename.c_str());
|
throw love::Exception("Could not open file at path %s", filename.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
NativeFile::NativeFile(const NativeFile &other)
|
NativeFile::NativeFile(const NativeFile &other)
|
||||||
: filename(other.filename)
|
: filename(other.filename)
|
||||||
, file(nullptr)
|
, file(nullptr)
|
||||||
, mode(MODE_CLOSED)
|
, mode(MODE_CLOSED)
|
||||||
, bufferMode(other.bufferMode)
|
, bufferMode(other.bufferMode)
|
||||||
, bufferSize(other.bufferSize)
|
, bufferSize(other.bufferSize)
|
||||||
{
|
{
|
||||||
if (!open(other.mode))
|
if (!open(other.mode))
|
||||||
throw love::Exception("Could not open file at path %s", filename.c_str());
|
throw love::Exception("Could not open file at path %s", filename.c_str());
|
||||||
@@ -86,49 +86,23 @@ bool NativeFile::open(Mode newmode)
|
|||||||
if (file != nullptr)
|
if (file != nullptr)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
#if defined(LOVE_ANDROID)
|
file = SDL_IOFromFile(filename.c_str(), getModeString(newmode));
|
||||||
// Try to handle content:// URI
|
if (file == nullptr)
|
||||||
int fd = love::android::getFDFromContentProtocol(filename.c_str());
|
|
||||||
if (fd != -1)
|
|
||||||
{
|
{
|
||||||
if (newmode != MODE_READ)
|
std::string err = SDL_GetError();
|
||||||
{
|
|
||||||
::close(fd);
|
|
||||||
throw love::Exception("%s is read-only.", filename.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
file = fdopen(fd, "rb");
|
if (err != "")
|
||||||
|
throw love::Exception("Could not open file %s: %s", filename.c_str(), err.c_str());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
file = fopen(filename.c_str(), getModeString(newmode));
|
|
||||||
#elif defined(LOVE_WINDOWS)
|
|
||||||
// make sure non-ASCII filenames work.
|
|
||||||
std::wstring modestr = to_widestr(getModeString(newmode));
|
|
||||||
std::wstring wfilename = to_widestr(filename);
|
|
||||||
|
|
||||||
file = _wfopen(wfilename.c_str(), modestr.c_str());
|
|
||||||
#else
|
|
||||||
file = fopen(filename.c_str(), getModeString(newmode));
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (newmode == MODE_READ && file == nullptr)
|
|
||||||
throw love::Exception("Could not open file %s. Does not exist.", filename.c_str());
|
|
||||||
|
|
||||||
mode = newmode;
|
mode = newmode;
|
||||||
|
|
||||||
if (file != nullptr && !setBuffer(bufferMode, bufferSize))
|
|
||||||
{
|
|
||||||
// Revert to buffer defaults if we don't successfully set the buffer.
|
|
||||||
bufferMode = BUFFER_NONE;
|
|
||||||
bufferSize = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return file != nullptr;
|
return file != nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool NativeFile::close()
|
bool NativeFile::close()
|
||||||
{
|
{
|
||||||
if (file == nullptr || fclose(file) != 0)
|
if (file == nullptr || !SDL_CloseIO(file))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
mode = MODE_CLOSED;
|
mode = MODE_CLOSED;
|
||||||
@@ -144,44 +118,8 @@ bool NativeFile::isOpen() const
|
|||||||
|
|
||||||
int64 NativeFile::getSize()
|
int64 NativeFile::getSize()
|
||||||
{
|
{
|
||||||
int fd = file ? fileno(file) : -1;
|
int64 size = SDL_GetIOSize(file);
|
||||||
|
return std::max(size, -1LL);
|
||||||
#ifdef LOVE_WINDOWS
|
|
||||||
|
|
||||||
struct _stat64 buf;
|
|
||||||
|
|
||||||
if (fd != -1)
|
|
||||||
{
|
|
||||||
if (_fstat64(fd, &buf) != 0)
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// make sure non-ASCII filenames work.
|
|
||||||
std::wstring wfilename = to_widestr(filename);
|
|
||||||
|
|
||||||
if (_wstat64(wfilename.c_str(), &buf) != 0)
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (int64) buf.st_size;
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
// Assume POSIX support...
|
|
||||||
struct stat buf;
|
|
||||||
|
|
||||||
if (fd != -1)
|
|
||||||
{
|
|
||||||
if (fstat(fd, &buf) != 0)
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
else if (stat(filename.c_str(), &buf) != 0)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
return (int64) buf.st_size;
|
|
||||||
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int64 NativeFile::read(void *dst, int64 size)
|
int64 NativeFile::read(void *dst, int64 size)
|
||||||
@@ -192,7 +130,7 @@ int64 NativeFile::read(void *dst, int64 size)
|
|||||||
if (size < 0)
|
if (size < 0)
|
||||||
throw love::Exception("Invalid read size.");
|
throw love::Exception("Invalid read size.");
|
||||||
|
|
||||||
size_t read = fread(dst, 1, (size_t) size, file);
|
size_t read = SDL_ReadIO(file, dst, (size_t) size);
|
||||||
|
|
||||||
return (int64) read;
|
return (int64) read;
|
||||||
}
|
}
|
||||||
@@ -205,7 +143,7 @@ bool NativeFile::write(const void *data, int64 size)
|
|||||||
if (size < 0)
|
if (size < 0)
|
||||||
throw love::Exception("Invalid write size.");
|
throw love::Exception("Invalid write size.");
|
||||||
|
|
||||||
int64 written = (int64) fwrite(data, 1, (size_t) size, file);
|
int64 written = SDL_WriteIO(file, data, (size_t) size);
|
||||||
|
|
||||||
return written == size;
|
return written == size;
|
||||||
}
|
}
|
||||||
@@ -215,7 +153,7 @@ bool NativeFile::flush()
|
|||||||
if (!file || (mode != MODE_WRITE && mode != MODE_APPEND))
|
if (!file || (mode != MODE_WRITE && mode != MODE_APPEND))
|
||||||
throw love::Exception("File is not opened for writing.");
|
throw love::Exception("File is not opened for writing.");
|
||||||
|
|
||||||
return fflush(file) == 0;
|
return SDL_FlushIO(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool NativeFile::isEOF()
|
bool NativeFile::isEOF()
|
||||||
@@ -228,11 +166,7 @@ int64 NativeFile::tell()
|
|||||||
if (file == nullptr)
|
if (file == nullptr)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
#ifdef LOVE_WINDOWS
|
return SDL_TellIO(file);
|
||||||
return (int64) _ftelli64(file);
|
|
||||||
#else
|
|
||||||
return (int64) ftello(file);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool NativeFile::seek(int64 pos, SeekOrigin origin)
|
bool NativeFile::seek(int64 pos, SeekOrigin origin)
|
||||||
@@ -240,18 +174,13 @@ bool NativeFile::seek(int64 pos, SeekOrigin origin)
|
|||||||
if (file == nullptr)
|
if (file == nullptr)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
int forigin = SEEK_SET;
|
SDL_IOWhence whence = SDL_IO_SEEK_SET;
|
||||||
if (origin == SEEKORIGIN_CURRENT)
|
if (origin == SEEKORIGIN_CURRENT)
|
||||||
forigin = SEEK_CUR;
|
whence = SDL_IO_SEEK_CUR;
|
||||||
else if (origin == SEEKORIGIN_END)
|
else if (origin == SEEKORIGIN_END)
|
||||||
forigin = SEEK_END;
|
whence = SDL_IO_SEEK_END;
|
||||||
|
|
||||||
// TODO
|
return SDL_SeekIO(file, pos, whence) >= 0;
|
||||||
#ifdef LOVE_WINDOWS
|
|
||||||
return _fseeki64(file, pos, forigin) == 0;
|
|
||||||
#else
|
|
||||||
return fseeko(file, (off_t) pos, forigin) == 0;
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool NativeFile::setBuffer(BufferMode bufmode, int64 size)
|
bool NativeFile::setBuffer(BufferMode bufmode, int64 size)
|
||||||
@@ -262,32 +191,7 @@ bool NativeFile::setBuffer(BufferMode bufmode, int64 size)
|
|||||||
if (bufmode == BUFFER_NONE)
|
if (bufmode == BUFFER_NONE)
|
||||||
size = 0;
|
size = 0;
|
||||||
|
|
||||||
// If the file isn't open, we'll make sure the buffer values are set in
|
// FIXME: SDL doesn't have option to set buffering.
|
||||||
// NativeFile::open.
|
|
||||||
if (!isOpen())
|
|
||||||
{
|
|
||||||
bufferMode = bufmode;
|
|
||||||
bufferSize = size;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
int vbufmode;
|
|
||||||
switch (bufmode)
|
|
||||||
{
|
|
||||||
case File::BUFFER_NONE:
|
|
||||||
default:
|
|
||||||
vbufmode = _IONBF;
|
|
||||||
break;
|
|
||||||
case File::BUFFER_LINE:
|
|
||||||
vbufmode = _IOLBF;
|
|
||||||
break;
|
|
||||||
case File::BUFFER_FULL:
|
|
||||||
vbufmode = _IOFBF;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (setvbuf(file, nullptr, vbufmode, (size_t) size) != 0)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
bufferMode = bufmode;
|
bufferMode = bufmode;
|
||||||
bufferSize = size;
|
bufferSize = size;
|
||||||
@@ -323,6 +227,9 @@ const char *NativeFile::getModeString(Mode mode)
|
|||||||
case File::MODE_WRITE:
|
case File::MODE_WRITE:
|
||||||
return "wb";
|
return "wb";
|
||||||
case File::MODE_APPEND:
|
case File::MODE_APPEND:
|
||||||
|
// Note: PhysFS "append" allows the user to
|
||||||
|
// seek the write pointer, but it's not possible
|
||||||
|
// to do so with standard fopen-style modes.
|
||||||
return "ab";
|
return "ab";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,6 +25,9 @@
|
|||||||
// C
|
// C
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
|
||||||
|
// SDL
|
||||||
|
#include <SDL3/SDL_iostream.h>
|
||||||
|
|
||||||
namespace love
|
namespace love
|
||||||
{
|
{
|
||||||
namespace filesystem
|
namespace filesystem
|
||||||
@@ -69,7 +72,7 @@ private:
|
|||||||
|
|
||||||
std::string filename;
|
std::string filename;
|
||||||
|
|
||||||
FILE *file;
|
SDL_IOStream *file;
|
||||||
|
|
||||||
Mode mode;
|
Mode mode;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user