mirror of
https://github.com/1dot13/source.git
synced 2026-07-22 13:40:22 +02:00
- Virtual File System (VFS) by birdflu. This is needed for Multiplayer and is also used for Single Player. Very neat system :-) - Multiplayer Version 1.1 + some additional features and bugfixes * INFO: If you compile a new EXE and want to test, be sure to also use the latest SVN GameDir files in your JA2 install directory * git-svn-id: https://ja2svn.mooo.com/source/ja2/trunk/GameSource/ja2_v1.13/Build@2961 3b4a5df2-a311-0410-b5c6-a8a6f20db521
122 lines
2.8 KiB
C++
122 lines
2.8 KiB
C++
#include "vfs_file_raii.h"
|
|
#include "vfs.h"
|
|
|
|
#include <sstream>
|
|
/********************************************************************************************/
|
|
/********************************************************************************************/
|
|
|
|
vfs::COpenReadFile::COpenReadFile(vfs::Path const& sPath, vfs::CVirtualFile::ESearchFile eSF)
|
|
{
|
|
vfs::IBaseFile *pFile = GetVFS()->GetFile(sPath,eSF);
|
|
THROWIFFALSE(pFile, (L"file \"" + sPath().c_wcs() + L"\" does not exist").c_str());
|
|
m_pFile = vfs::tReadableFile::Cast(pFile);
|
|
THROWIFFALSE(m_pFile, L"not readable");
|
|
THROWIFFALSE(m_pFile->OpenRead(), L"open read failed");
|
|
}
|
|
|
|
vfs::COpenReadFile::COpenReadFile(vfs::tReadableFile *pFile)
|
|
{
|
|
try
|
|
{
|
|
m_pFile = pFile;
|
|
THROWIFFALSE(m_pFile, L"no file");
|
|
THROWIFFALSE(m_pFile->OpenRead(), L"not open");
|
|
}
|
|
catch(CBasicException &ex)
|
|
{
|
|
RETHROWEXCEPTION(L"",&ex);
|
|
}
|
|
}
|
|
vfs::COpenReadFile::~COpenReadFile()
|
|
{
|
|
if(m_pFile)
|
|
{
|
|
m_pFile->Close();
|
|
m_pFile = NULL;
|
|
}
|
|
}
|
|
|
|
vfs::tReadableFile& vfs::COpenReadFile::file()
|
|
{
|
|
return *m_pFile;
|
|
}
|
|
|
|
void vfs::COpenReadFile::release()
|
|
{
|
|
m_pFile = NULL;
|
|
}
|
|
|
|
/**************************************************************************/
|
|
|
|
vfs::COpenWriteFile::COpenWriteFile(vfs::Path const& sPath,
|
|
bool bCreate,
|
|
bool bTruncate,
|
|
vfs::CVirtualFile::ESearchFile eSF)
|
|
{
|
|
vfs::IBaseFile *pFile = GetVFS()->GetFile(sPath,eSF);
|
|
if(!pFile && bCreate)
|
|
{
|
|
if(GetVFS()->CreateNewFile(sPath))
|
|
{
|
|
pFile = GetVFS()->GetFile(sPath,eSF);
|
|
}
|
|
else
|
|
{
|
|
std::wstringstream wss;
|
|
wss << L"Could not create VFS file \"" << sPath().c_wcs() << L"\"";
|
|
THROWEXCEPTION(wss.str().c_str());
|
|
}
|
|
}
|
|
if(!pFile)
|
|
{
|
|
std::wstringstream wss;
|
|
wss << L"File \"" << sPath().c_wcs() << L"\" not found";
|
|
THROWEXCEPTION(wss.str().c_str());
|
|
}
|
|
m_pFile = vfs::tWriteableFile::Cast(pFile);
|
|
if(!m_pFile)
|
|
{
|
|
std::wstringstream wss;
|
|
wss << L"File \"" << sPath().c_wcs() << L"\" exists, but is not writeable";
|
|
THROWEXCEPTION(wss.str().c_str());
|
|
}
|
|
if(!m_pFile->OpenWrite(bCreate,bTruncate))
|
|
{
|
|
std::wstringstream wss;
|
|
wss << L"File \"" << sPath().c_wcs() << L"\" could not be opened for writing";
|
|
THROWEXCEPTION(wss.str().c_str());
|
|
}
|
|
}
|
|
vfs::COpenWriteFile::COpenWriteFile(vfs::tWriteableFile *pFile)
|
|
{
|
|
try
|
|
{
|
|
m_pFile = pFile;
|
|
THROWIFFALSE(m_pFile, L"no file");
|
|
THROWIFFALSE(m_pFile->OpenWrite(true,false), L"not open");
|
|
}
|
|
catch(CBasicException& ex)
|
|
{
|
|
RETHROWEXCEPTION(L"",&ex);
|
|
};
|
|
}
|
|
vfs::COpenWriteFile::~COpenWriteFile()
|
|
{
|
|
if(m_pFile)
|
|
{
|
|
m_pFile->Close();
|
|
m_pFile = NULL;
|
|
}
|
|
}
|
|
|
|
vfs::tWriteableFile& vfs::COpenWriteFile::file()
|
|
{
|
|
return *m_pFile;
|
|
}
|
|
|
|
void vfs::COpenWriteFile::release()
|
|
{
|
|
m_pFile = NULL;
|
|
}
|
|
|