Added runtime drag-and-drop file and folder support via new love.filedropped and love.directorydropped event callback functions. filedropped has a single File object argument, and directorydropped has a single string argument containing the full path of the directory, which can be used with love.filesystem.mount.

--HG--
branch : minor
This commit is contained in:
Alex Szpakowski
2014-10-18 20:30:36 -03:00
parent af0371361f
commit 8d0654a6a7
25 changed files with 1151 additions and 428 deletions
+31 -98
View File
@@ -39,8 +39,8 @@ namespace physfs
File::File(const std::string &filename)
: filename(filename)
, file(0)
, mode(CLOSED)
, file(nullptr)
, mode(MODE_CLOSED)
, bufferMode(BUFFER_NONE)
, bufferSize(0)
{
@@ -48,25 +48,25 @@ File::File(const std::string &filename)
File::~File()
{
if (mode != CLOSED)
if (mode != MODE_CLOSED)
close();
}
bool File::open(Mode mode)
{
if (mode == CLOSED)
if (mode == MODE_CLOSED)
return true;
// File must exist if read mode.
if ((mode == READ) && !PHYSFS_exists(filename.c_str()))
if ((mode == MODE_READ) && !PHYSFS_exists(filename.c_str()))
throw love::Exception("Could not open file %s. Does not exist.", filename.c_str());
// Check whether the write directory is set.
if ((mode == APPEND || mode == WRITE) && (PHYSFS_getWriteDir() == 0) && !hack_setupWriteDirectory())
if ((mode == MODE_APPEND || mode == MODE_WRITE) && (PHYSFS_getWriteDir() == 0) && !hack_setupWriteDirectory())
throw love::Exception("Could not set write directory.");
// File already open?
if (file != 0)
if (file != nullptr)
return false;
PHYSFS_getLastError(); // Clear the error buffer.
@@ -74,13 +74,13 @@ bool File::open(Mode mode)
switch (mode)
{
case READ:
case MODE_READ:
handle = PHYSFS_openRead(filename.c_str());
break;
case APPEND:
case MODE_APPEND:
handle = PHYSFS_openAppend(filename.c_str());
break;
case WRITE:
case MODE_WRITE:
handle = PHYSFS_openWrite(filename.c_str());
break;
default:
@@ -99,94 +99,50 @@ bool File::open(Mode mode)
this->mode = mode;
if (file != 0 && !setBuffer(bufferMode, bufferSize))
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 != 0);
return (file != nullptr);
}
bool File::close()
{
if (!PHYSFS_close(file))
if (file == nullptr || !PHYSFS_close(file))
return false;
mode = CLOSED;
file = 0;
mode = MODE_CLOSED;
file = nullptr;
return true;
}
bool File::isOpen() const
{
return mode != CLOSED && file != 0;
return mode != MODE_CLOSED && file != nullptr;
}
int64 File::getSize()
{
// If the file is closed, open it to
// check the size.
if (file == 0)
if (file == nullptr)
{
open(READ);
int64 size = (int64)PHYSFS_fileLength(file);
open(MODE_READ);
int64 size = (int64) PHYSFS_fileLength(file);
close();
return size;
}
return (int64)PHYSFS_fileLength(file);
}
FileData *File::read(int64 size)
{
bool isOpen = (file != 0);
if (!isOpen && !open(READ))
throw love::Exception("Could not read file %s.", filename.c_str());
int64 max = getSize();
int64 cur = tell();
size = (size == ALL) ? max : size;
if (size < 0)
throw love::Exception("Invalid read size.");
// Clamping because the file offset may be in a weird position.
if (cur < 0)
cur = 0;
else if (cur > max)
cur = max;
if (cur + size > max)
size = max - cur;
FileData *fileData = new FileData(size, getFilename());
int64 bytesRead = read(fileData->getData(), size);
if (bytesRead < 0 || (bytesRead == 0 && bytesRead != size))
{
delete fileData;
throw love::Exception("Could not read from file.");
}
if (bytesRead < size)
{
FileData *tmpFileData = new FileData(bytesRead, getFilename());
memcpy(tmpFileData->getData(), fileData->getData(), (size_t) bytesRead);
delete fileData;
fileData = tmpFileData;
}
if (!isOpen)
close();
return fileData;
return (int64) PHYSFS_fileLength(file);
}
int64 File::read(void *dst, int64 size)
{
if (!file || mode != READ)
if (!file || mode != MODE_READ)
throw love::Exception("File is not opened for reading.");
int64 max = (int64)PHYSFS_fileLength(file);
@@ -205,7 +161,7 @@ int64 File::read(void *dst, int64 size)
bool File::write(const void *data, int64 size)
{
if (!file || (mode != WRITE && mode != APPEND))
if (!file || (mode != MODE_WRITE && mode != MODE_APPEND))
throw love::Exception("File is not opened for writing.");
// Another clamp, for the time being.
@@ -215,7 +171,7 @@ bool File::write(const void *data, int64 size)
throw love::Exception("Invalid write size.");
// Try to write.
int64 written = static_cast<int64>(PHYSFS_write(file, data, 1, (PHYSFS_uint32) size));
int64 written = (int64) PHYSFS_write(file, data, 1, (PHYSFS_uint32) size);
// Check that correct amount of data was written.
if (written != size)
@@ -231,14 +187,9 @@ bool File::write(const void *data, int64 size)
return true;
}
bool File::write(const Data *data, int64 size)
{
return write(data->getData(), (size == ALL) ? data->getSize() : size);
}
bool File::flush()
{
if (!file || (mode != WRITE && mode != APPEND))
if (!file || (mode != MODE_WRITE && mode != MODE_APPEND))
throw love::Exception("File is not opened for writing.");
return PHYSFS_flush(file) != 0;
@@ -263,14 +214,12 @@ inline bool test_eof(File *, PHYSFS_File *file)
bool File::eof()
{
if (file == 0 || test_eof(this, file))
return true;
return false;
return file == nullptr || test_eof(this, file);
}
int64 File::tell()
{
if (file == 0)
if (file == nullptr)
return -1;
return (int64) PHYSFS_tell(file);
@@ -278,23 +227,17 @@ int64 File::tell()
bool File::seek(uint64 pos)
{
if (file == 0)
return false;
if (!PHYSFS_seek(file, (PHYSFS_uint64) pos))
return false;
return true;
return file != nullptr && PHYSFS_seek(file, (PHYSFS_uint64) pos) != 0;
}
bool File::setBuffer(BufferMode bufmode, int64 size)
{
// No negativity allowed!
if (size < 0)
return false;
// If the file isn't open, we'll make sure the buffer values are set in
// File::open.
if (file == 0 || mode == CLOSED)
if (!isOpen())
{
bufferMode = bufmode;
bufferSize = size;
@@ -331,21 +274,11 @@ File::BufferMode File::getBuffer(int64 &size) const
return bufferMode;
}
std::string File::getFilename() const
const std::string &File::getFilename() const
{
return filename;
}
std::string File::getExtension() const
{
std::string::size_type idx = filename.rfind('.');
if (idx != std::string::npos)
return filename.substr(idx+1);
else
return std::string();
}
filesystem::File::Mode File::getMode() const
{
return mode;
+4 -5
View File
@@ -54,14 +54,14 @@ public:
virtual ~File();
// Implements love::filesystem::File.
using love::filesystem::File::read;
using love::filesystem::File::write;
bool open(Mode mode);
bool close();
bool isOpen() const;
int64 getSize();
FileData *read(int64 size = ALL);
int64 read(void *dst, int64 size);
virtual int64 read(void *dst, int64 size);
bool write(const void *data, int64 size);
bool write(const Data *data, int64 size = ALL);
bool flush();
bool eof();
int64 tell();
@@ -69,8 +69,7 @@ public:
bool setBuffer(BufferMode bufmode, int64 size);
BufferMode getBuffer(int64 &size) const;
Mode getMode() const;
std::string getFilename() const;
std::string getExtension() const;
const std::string &getFilename() const;
private:
+44 -118
View File
@@ -18,8 +18,6 @@
* 3. This notice may not be removed or altered from any source distribution.
**/
#include "common/config.h"
#include <iostream>
#include <sstream>
@@ -27,6 +25,25 @@
#include "common/b64.h"
#include "Filesystem.h"
#include "File.h"
// PhysFS
#ifdef LOVE_MACOSX_USE_FRAMEWORKS
#include <physfs/physfs.h>
#else
#include <physfs.h>
#endif
// For great CWD. (Current Working Directory)
// Using this instead of boost::filesystem which totally
// cramped our style.
#ifdef LOVE_WINDOWS
# include <windows.h>
# include <direct.h>
#else
# include <sys/param.h>
# include <unistd.h>
#endif
namespace
{
@@ -73,8 +90,7 @@ namespace physfs
{
Filesystem::Filesystem()
: initialized(false)
, fused(false)
: fused(false)
, fusedSet(false)
{
requirePath = {"?.lua", "?/init.lua"};
@@ -82,7 +98,7 @@ Filesystem::Filesystem()
Filesystem::~Filesystem()
{
if (initialized)
if (PHYSFS_isInit())
PHYSFS_deinit();
}
@@ -95,7 +111,6 @@ void Filesystem::init(const char *arg0)
{
if (!PHYSFS_init(arg0))
throw Exception(PHYSFS_getLastError());
initialized = true;
}
void Filesystem::setFused(bool fused)
@@ -115,7 +130,7 @@ bool Filesystem::isFused() const
bool Filesystem::setIdentity(const char *ident, bool appendToPath)
{
if (!initialized)
if (!PHYSFS_isInit())
return false;
std::string old_save_path = save_path_full;
@@ -164,7 +179,7 @@ const char *Filesystem::getIdentity() const
bool Filesystem::setSource(const char *source)
{
if (!initialized)
if (!PHYSFS_isInit())
return false;
// Check whether directory is already set.
@@ -188,7 +203,7 @@ const char *Filesystem::getSource() const
bool Filesystem::setupWriteDirectory()
{
if (!initialized)
if (!PHYSFS_isInit())
return false;
// These must all be set.
@@ -244,13 +259,20 @@ bool Filesystem::setupWriteDirectory()
bool Filesystem::mount(const char *archive, const char *mountpoint, bool appendToPath)
{
if (!initialized || !archive)
if (!PHYSFS_isInit() || !archive)
return false;
std::string realPath;
std::string sourceBase = getSourceBaseDirectory();
if (isFused() && sourceBase.compare(archive) == 0)
// Check whether the given archive path is in the list of allowed full paths.
auto it = std::find(allowedMountPaths.begin(), allowedMountPaths.end(), archive);
if (it != allowedMountPaths.end())
{
realPath = *it;
}
else if (isFused() && sourceBase.compare(archive) == 0)
{
// Special case: if the game is fused and the archive is the source's
// base directory, mount it even though it's outside of the save dir.
@@ -285,7 +307,7 @@ bool Filesystem::mount(const char *archive, const char *mountpoint, bool appendT
bool Filesystem::unmount(const char *archive)
{
if (!initialized || !archive)
if (!PHYSFS_isInit() || !archive)
return false;
std::string realPath;
@@ -319,7 +341,7 @@ bool Filesystem::unmount(const char *archive)
return PHYSFS_removeFromSearchPath(realPath.c_str());
}
File *Filesystem::newFile(const char *filename) const
love::filesystem::File *Filesystem::newFile(const char *filename) const
{
return new File(filename);
}
@@ -478,7 +500,7 @@ FileData *Filesystem::read(const char *filename, int64 size) const
{
File file(filename);
file.open(File::READ);
file.open(File::MODE_READ);
// close() is called in the File destructor.
return file.read(size);
@@ -488,7 +510,7 @@ void Filesystem::write(const char *filename, const void *data, int64 size) const
{
File file(filename);
file.open(File::WRITE);
file.open(File::MODE_WRITE);
// close() is called in the File destructor.
if (!file.write(data, size))
@@ -499,7 +521,7 @@ void Filesystem::append(const char *filename, const void *data, int64 size) cons
{
File file(filename);
file.open(File::APPEND);
file.open(File::MODE_APPEND);
// close() is called in the File destructor.
if (!file.write(data, size))
@@ -538,108 +560,6 @@ int Filesystem::getDirectoryItems(lua_State *L)
return 1;
}
int Filesystem::lines_i(lua_State *L)
{
const int bufsize = 1024;
char buf[bufsize];
int linesize = 0;
bool newline = false;
File *file = luax_checktype<File>(L, lua_upvalueindex(1), "File", FILESYSTEM_FILE_T);
// Only accept read mode at this point.
if (file->getMode() != File::READ)
return luaL_error(L, "File needs to stay in read mode.");
int64 pos = file->tell();
int64 userpos = -1;
if (lua_isnoneornil(L, lua_upvalueindex(2)) == 0)
{
// User may have changed the file position.
userpos = pos;
pos = (int64) lua_tonumber(L, lua_upvalueindex(2));
if (userpos != pos)
file->seek(pos);
}
while (!newline && !file->eof())
{
// This 64-bit to 32-bit integer cast should be safe as it never exceeds bufsize.
int read = (int) file->read(buf, bufsize);
if (read < 0)
return luaL_error(L, "Could not read from file.");
linesize += read;
for (int i = 0; i < read; i++)
{
if (buf[i] == '\n')
{
linesize -= read - i;
newline = true;
break;
}
}
}
if (newline || (file->eof() && linesize > 0))
{
if (linesize < bufsize)
{
// We have the line in the buffer on the stack. No 'new' and 'read' needed.
lua_pushlstring(L, buf, linesize > 0 && buf[linesize - 1] == '\r' ? linesize - 1 : linesize);
if (userpos < 0)
file->seek(pos + linesize + 1);
}
else
{
char *str = 0;
try
{
str = new char[linesize + 1];
}
catch(std::bad_alloc &)
{
// Can't lua_error (longjmp) in exception handlers.
}
if (!str)
return luaL_error(L, "Out of memory.");
file->seek(pos);
// Read the \n anyway and save us a call to seek.
if (file->read(str, linesize + 1) == -1)
{
delete [] str;
return luaL_error(L, "Could not read from file.");
}
lua_pushlstring(L, str, str[linesize - 1] == '\r' ? linesize - 1 : linesize);
delete [] str;
}
if (userpos >= 0)
{
// Save new position in upvalue.
lua_pushnumber(L, (lua_Number)(pos + linesize + 1));
lua_replace(L, lua_upvalueindex(2));
file->seek(userpos);
}
return 1;
}
// EOF reached.
if (userpos >= 0 && luax_toboolean(L, lua_upvalueindex(3)))
file->seek(userpos);
else
file->close();
return 0;
}
int64 Filesystem::getLastModified(const char *filename) const
{
PHYSFS_sint64 time = PHYSFS_getLastModTime(filename);
@@ -677,6 +597,12 @@ std::vector<std::string> &Filesystem::getRequirePath()
return requirePath;
}
void Filesystem::allowMountingForPath(const std::string &path)
{
if (std::find(allowedMountPaths.begin(), allowedMountPaths.end(), path) == allowedMountPaths.end())
allowedMountPaths.push_back(path);
}
} // physfs
} // filesystem
} // love
+6 -174
View File
@@ -24,47 +24,9 @@
// STD
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
// LOVE
#include "common/Module.h"
#include "common/config.h"
#include "common/int.h"
#include "common/runtime.h"
#include "filesystem/FileData.h"
#include "File.h"
// For great CWD. (Current Working Directory)
// Using this instead of boost::filesystem which totally
// cramped our style.
#ifdef LOVE_WINDOWS
# include <windows.h>
# include <direct.h>
#else
# include <sys/param.h>
# include <unistd.h>
#endif
// In Windows, we would like to use "LOVE" as the
// application folder, but in Linux, we like .love.
#define LOVE_APPDATA_PREFIX ""
#ifdef LOVE_WINDOWS
# define LOVE_APPDATA_FOLDER "LOVE"
# define LOVE_PATH_SEPARATOR "/"
# define LOVE_MAX_PATH _MAX_PATH
#else
# ifdef LOVE_MACOSX
# define LOVE_APPDATA_FOLDER "LOVE"
# elif defined(LOVE_LINUX)
# define LOVE_APPDATA_FOLDER "love"
# else
# define LOVE_APPDATA_PREFIX "."
# define LOVE_APPDATA_FOLDER "love"
# endif
# define LOVE_PATH_SEPARATOR "/"
# define LOVE_MAX_PATH MAXPATHLEN
#endif
#include "filesystem/Filesystem.h"
namespace love
{
@@ -73,7 +35,7 @@ namespace filesystem
namespace physfs
{
class Filesystem : public Module
class Filesystem : public love::filesystem::Filesystem
{
public:
@@ -81,7 +43,6 @@ public:
virtual ~Filesystem();
// Implements Module.
virtual ModuleType getModuleType() const { return M_FILESYSTEM; }
const char *getName() const;
void init(const char *arg0);
@@ -89,183 +50,55 @@ public:
void setFused(bool fused);
bool isFused() const;
/**
* This sets up the save directory. If the
* it is already set up, nothing happens.
* @return True on success, false otherwise.
**/
bool setupWriteDirectory();
/**
* Sets the name of the save folder.
* @param ident The name of the game. Will be used to
* to create the folder in the LOVE data folder.
**/
bool setIdentity(const char *ident, bool appendToPath = false);
const char *getIdentity() const;
/**
* Sets the path to the game source.
* This can only be set once.
* @param source Path to a directory or a .love-file.
**/
bool setSource(const char *source);
/**
* Gets the path to the game source.
* Returns a 0-length string if the source has not been set.
**/
const char *getSource() const;
bool mount(const char *archive, const char *mountpoint, bool appendToPath = false);
bool unmount(const char *archive);
/**
* Creates a new file.
**/
File *newFile(const char *filename) const;
/**
* Creates a new FileData object. Data will be copied.
* @param data Pointer to the data.
* @param size The size of the data.
* @param filename The full filename used to file type identification.
**/
FileData *newFileData(void *data, unsigned int size, const char *filename) const;
/**
* Creates a new FileData object from base64 data.
* @param b64 The base64 data.
**/
FileData *newFileData(const char *b64, const char *filename) const;
/**
* Gets the current working directory.
**/
const char *getWorkingDirectory();
/**
* Gets the user home directory.
**/
std::string getUserDirectory();
/**
* Gets the APPDATA directory. On Windows, this is the folder
* in the %APPDATA% enviroment variable. On Linux, this is the
* user home folder.
**/
std::string getAppdataDirectory();
/**
* Gets the full path of the save folder.
**/
const char *getSaveDirectory();
/**
* Gets the full path to the directory containing the game source.
* For example if the game source is C:\Games\mygame.love, this will return
* C:\Games.
**/
std::string getSourceBaseDirectory() const;
/**
* Gets the real directory path containing the file.
**/
std::string getRealDirectory(const char *filename) const;
/**
* Checks if a path is a directory.
* @param dir The directory name to check.
**/
bool isDirectory(const char *dir) const;
/**
* Checks if a filename exists.
* @param file The filename to check.
**/
bool isFile(const char *file) const;
/**
* Creates a directory. Write dir must be set.
* @param dir The directory to create.
**/
bool createDirectory(const char *dir);
/**
* Removes a file (or directory).
* @param file The file or directory to remove.
**/
bool remove(const char *file);
/**
* Reads data from a file.
* @param filename The name of the file to read from.
* @param size The size in bytes of the data to read.
**/
FileData *read(const char *filename, int64 size = File::ALL) const;
/**
* Write data to a file.
* @param filename The name of the file to write to.
* @param data The data to write.
* @param size The size in bytes of the data to write.
**/
void write(const char *filename, const void *data, int64 size) const;
/**
* Append data to a file, creating it if it doesn't exist.
* @param filename The name of the file to write to.
* @param data The data to append.
* @param size The size in bytes of the data to append.
**/
void append(const char *filename, const void *data, int64 size) const;
/**
* This "native" method returns a table of all
* files in a given directory.
**/
int getDirectoryItems(lua_State *L);
/**
* Gets the last modification time of a file, in seconds
* since the Unix epoch.
* @param filename The name of the file.
**/
int64 getLastModified(const char *filename) const;
/**
* Gets the size of a file in bytes.
* @param filename The name of the file.
**/
int64 getSize(const char *filename) const;
/**
* Enable or disable symbolic link support in love.filesystem.
**/
void setSymlinksEnabled(bool enable);
/**
* Gets whether symbolic link support is enabled.
**/
bool areSymlinksEnabled() const;
/**
* Gets whether a filepath is actually a symlink.
* Always returns false if symlinks are not enabled.
**/
bool isSymlink(const char *filename) const;
/**
* Text file line-reading iterator function used and
* pushed on the Lua stack by love.filesystem.lines
* and File:lines.
**/
static int lines_i(lua_State *L);
// Require path accessors
// Not const because it's R/W
std::vector<std::string> &getRequirePath();
void allowMountingForPath(const std::string &path);
private:
// Contains the current working directory (UTF8).
@@ -286,9 +119,6 @@ private:
// The full path to the source of the game.
std::string game_source;
// Workaround for machines without PhysFS 2.0
bool initialized;
// Allow saving outside of the LOVE_APPDATA_FOLDER
// for release 'builds'
bool fused;
@@ -297,6 +127,8 @@ private:
// Search path for require
std::vector<std::string> requirePath;
std::vector<std::string> allowedMountPaths;
}; // Filesystem
} // physfs