mirror of
https://github.com/love2d/love.git
synced 2026-08-20 12:40:20 +02:00
Cleaned up love.filesystem.load and love.filesystem.getLastModified code
This commit is contained in:
@@ -471,56 +471,14 @@ int Filesystem::lines_i(lua_State *L)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Filesystem::load(lua_State *L)
|
int64 Filesystem::getLastModified(const char *filename) const
|
||||||
{
|
{
|
||||||
// Need only one arg.
|
|
||||||
luax_assert_argc(L, 1, 1);
|
|
||||||
|
|
||||||
// Must be string.
|
|
||||||
if (!lua_isstring(L, -1))
|
|
||||||
return luaL_error(L, "The argument must be a string.");
|
|
||||||
|
|
||||||
std::string filename = lua_tostring(L, -1);
|
|
||||||
|
|
||||||
// The file must exist.
|
|
||||||
if (!exists(filename.c_str()))
|
|
||||||
return luaL_error(L, "File %s does not exist.", filename.c_str());
|
|
||||||
|
|
||||||
// Create the file.
|
|
||||||
File *file = newFile(filename.c_str());
|
|
||||||
|
|
||||||
// Get the data from the file.
|
|
||||||
Data *data = file->read();
|
|
||||||
|
|
||||||
int status = luaL_loadbuffer(L, (const char *)data->getData(), data->getSize(), ("@" + filename).c_str());
|
|
||||||
|
|
||||||
data->release();
|
|
||||||
file->release();
|
|
||||||
|
|
||||||
// Load the chunk, but don't run it.
|
|
||||||
switch (status)
|
|
||||||
{
|
|
||||||
case LUA_ERRMEM:
|
|
||||||
return luaL_error(L, "Memory allocation error: %s\n", lua_tostring(L, -1));
|
|
||||||
case LUA_ERRSYNTAX:
|
|
||||||
return luaL_error(L, "Syntax error: %s\n", lua_tostring(L, -1));
|
|
||||||
default: // success
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int Filesystem::getLastModified(lua_State *L)
|
|
||||||
{
|
|
||||||
const char *filename = luaL_checkstring(L, 1);
|
|
||||||
PHYSFS_sint64 time = PHYSFS_getLastModTime(filename);
|
PHYSFS_sint64 time = PHYSFS_getLastModTime(filename);
|
||||||
|
|
||||||
if (time == -1)
|
if (time == -1)
|
||||||
{
|
throw love::Exception("Could not determine file modification date.");
|
||||||
lua_pushnil(L);
|
|
||||||
lua_pushstring(L, "Could not determine file modification date.");
|
return time;
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
lua_pushnumber(L, static_cast<lua_Number>(time));
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int64 Filesystem::getSize(const char *filename) const
|
int64 Filesystem::getSize(const char *filename) const
|
||||||
|
|||||||
@@ -236,7 +236,7 @@ public:
|
|||||||
* @param filename The name of the file to read from.
|
* @param filename The name of the file to read from.
|
||||||
* @param size The size in bytes of the data to read.
|
* @param size The size in bytes of the data to read.
|
||||||
**/
|
**/
|
||||||
Data *read(const char *filename, int64 size) const;
|
Data *read(const char *filename, int64 size = File::ALL) const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write data to a file.
|
* Write data to a file.
|
||||||
@@ -279,14 +279,11 @@ public:
|
|||||||
int enumerate(lua_State *L);
|
int enumerate(lua_State *L);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads a file without running it. The loaded
|
* Gets the last modification time of a file, in seconds
|
||||||
* chunk is returned as a function.
|
* since the Unix epoch.
|
||||||
* @param filename The filename of the file to load.
|
* @param filename The name of the file.
|
||||||
* @return A function.
|
|
||||||
**/
|
**/
|
||||||
int load(lua_State *L);
|
int64 getLastModified(const char *filename) const;
|
||||||
|
|
||||||
int getLastModified(lua_State *L);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the size of a file in bytes.
|
* Gets the size of a file in bytes.
|
||||||
|
|||||||
@@ -307,19 +307,53 @@ int w_lines(lua_State *L)
|
|||||||
|
|
||||||
int w_load(lua_State *L)
|
int w_load(lua_State *L)
|
||||||
{
|
{
|
||||||
|
std::string filename = std::string(luaL_checkstring(L, 1));
|
||||||
|
|
||||||
|
Data *data = 0;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return instance->load(L);
|
data = instance->read(filename.c_str());
|
||||||
}
|
}
|
||||||
catch(love::Exception &e)
|
catch (love::Exception &e)
|
||||||
{
|
{
|
||||||
return luaL_error(L, e.what());
|
return luaL_error(L, "%s", e.what());
|
||||||
|
}
|
||||||
|
|
||||||
|
int status = luaL_loadbuffer(L, (const char *)data->getData(), data->getSize(), ("@" + filename).c_str());
|
||||||
|
|
||||||
|
data->release();
|
||||||
|
|
||||||
|
// Load the chunk, but don't run it.
|
||||||
|
switch (status)
|
||||||
|
{
|
||||||
|
case LUA_ERRMEM:
|
||||||
|
return luaL_error(L, "Memory allocation error: %s\n", lua_tostring(L, -1));
|
||||||
|
case LUA_ERRSYNTAX:
|
||||||
|
return luaL_error(L, "Syntax error: %s\n", lua_tostring(L, -1));
|
||||||
|
default: // success
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int w_getLastModified(lua_State *L)
|
int w_getLastModified(lua_State *L)
|
||||||
{
|
{
|
||||||
return instance->getLastModified(L);
|
const char *filename = luaL_checkstring(L, 1);
|
||||||
|
|
||||||
|
int64 time = 0;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
time = instance->getLastModified(filename);
|
||||||
|
}
|
||||||
|
catch (love::Exception &e)
|
||||||
|
{
|
||||||
|
lua_pushnil(L);
|
||||||
|
lua_pushstring(L, e.what());
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
lua_pushnumber(L, static_cast<lua_Number>(time));
|
||||||
|
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int w_getSize(lua_State *L)
|
int w_getSize(lua_State *L)
|
||||||
@@ -370,7 +404,7 @@ int loader(lua_State *L)
|
|||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
lua_pushstring(L, tmp.c_str());
|
lua_pushstring(L, tmp.c_str());
|
||||||
// Ok, load it.
|
// Ok, load it.
|
||||||
return instance->load(L);
|
return w_load(L);
|
||||||
}
|
}
|
||||||
|
|
||||||
tmp = filename;
|
tmp = filename;
|
||||||
@@ -391,7 +425,7 @@ int loader(lua_State *L)
|
|||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
lua_pushstring(L, tmp.c_str());
|
lua_pushstring(L, tmp.c_str());
|
||||||
// Ok, load it.
|
// Ok, load it.
|
||||||
return instance->load(L);
|
return w_load(L);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user