Changed RageFile lua functions to emit lua errors instead of asserting if the file cannot be safely read or written to.

This commit is contained in:
Kyzentun Keeslala
2016-01-10 16:38:34 -07:00
parent 14caac26ca
commit 7b61974df6
2 changed files with 32 additions and 0 deletions
+31
View File
@@ -320,6 +320,29 @@ int32_t FileReading::read_32_le( RageFileBasic &f, RString &sError )
class LunaRageFile: public Luna<RageFile> class LunaRageFile: public Luna<RageFile>
{ {
public: public:
static void safely_opened(T* p, lua_State* L)
{
if(!p->IsOpen())
{
luaL_error(L, "File '%s' is not open.", p->GetPath().c_str());
}
}
static void can_safely_read(T* p, lua_State* L)
{
safely_opened(p, L);
if(!(p->GetMode()&RageFile::READ))
{
luaL_error(L, "File '%s' is not open for reading.", p->GetPath().c_str());
}
}
static void can_safely_write(T* p, lua_State* L)
{
safely_opened(p, L);
if(!(p->GetMode()&RageFile::WRITE))
{
luaL_error(L, "File '%s' is not open for writing.", p->GetPath().c_str());
}
}
static int destroy( T* p, lua_State *L ) static int destroy( T* p, lua_State *L )
{ {
SAFE_DELETE(p); SAFE_DELETE(p);
@@ -340,6 +363,7 @@ public:
static int Write( T* p, lua_State *L ) static int Write( T* p, lua_State *L )
{ {
can_safely_write(p, L);
lua_pushinteger( L, p->Write( SArg(1) ) ); lua_pushinteger( L, p->Write( SArg(1) ) );
return 1; return 1;
} }
@@ -352,6 +376,7 @@ public:
static int Read( T* p, lua_State *L ) static int Read( T* p, lua_State *L )
{ {
can_safely_read(p, L);
RString string; RString string;
p->Read(string); p->Read(string);
lua_pushstring( L, string ); lua_pushstring( L, string );
@@ -360,6 +385,7 @@ public:
static int ReadBytes( T* p, lua_State *L ) static int ReadBytes( T* p, lua_State *L )
{ {
can_safely_read(p, L);
RString string; RString string;
p->Read( string, IArg(1) ); p->Read( string, IArg(1) );
lua_pushstring( L, string ); lua_pushstring( L, string );
@@ -368,18 +394,21 @@ public:
static int Seek( T* p, lua_State *L ) static int Seek( T* p, lua_State *L )
{ {
can_safely_read(p, L);
lua_pushinteger( L, p->Seek( IArg(1) ) ); lua_pushinteger( L, p->Seek( IArg(1) ) );
return 1; return 1;
} }
static int Tell( T* p, lua_State *L ) static int Tell( T* p, lua_State *L )
{ {
can_safely_read(p, L);
lua_pushinteger( L, p->Tell() ); lua_pushinteger( L, p->Tell() );
return 1; return 1;
} }
static int GetLine( T* p, lua_State *L ) static int GetLine( T* p, lua_State *L )
{ {
can_safely_read(p, L);
RString string; RString string;
p->GetLine(string); p->GetLine(string);
lua_pushstring( L, string ); lua_pushstring( L, string );
@@ -388,6 +417,7 @@ public:
static int PutLine( T* p, lua_State *L ) static int PutLine( T* p, lua_State *L )
{ {
can_safely_write(p, L);
lua_pushinteger( L, p->PutLine( SArg(1) ) ); lua_pushinteger( L, p->PutLine( SArg(1) ) );
return 1; return 1;
} }
@@ -408,6 +438,7 @@ public:
static int AtEOF( T* p, lua_State *L ) static int AtEOF( T* p, lua_State *L )
{ {
can_safely_read(p, L);
lua_pushboolean( L, p->AtEOF() ); lua_pushboolean( L, p->AtEOF() );
return 1; return 1;
} }
+1
View File
@@ -46,6 +46,7 @@ public:
bool Open( const RString& path, int mode = READ ); bool Open( const RString& path, int mode = READ );
void Close(); void Close();
bool IsOpen() const { return m_File != NULL; } bool IsOpen() const { return m_File != NULL; }
int GetMode() const { return m_Mode; }
bool AtEOF() const; bool AtEOF() const;
RString GetError() const; RString GetError() const;