mirror of
https://github.com/ddnet/ddnet.git
synced 2024-11-17 21:48:19 +00:00
Merge pull request #9110 from Robyt3/Datafile-Reader-Assertions
Add assertions to `CDataFileReader` checking if file open/closed
This commit is contained in:
commit
fa285811fa
|
@ -102,6 +102,8 @@ struct CDatafile
|
||||||
|
|
||||||
bool CDataFileReader::Open(class IStorage *pStorage, const char *pFilename, int StorageType)
|
bool CDataFileReader::Open(class IStorage *pStorage, const char *pFilename, int StorageType)
|
||||||
{
|
{
|
||||||
|
dbg_assert(m_pDataFile == nullptr, "File already open");
|
||||||
|
|
||||||
log_trace("datafile", "loading. filename='%s'", pFilename);
|
log_trace("datafile", "loading. filename='%s'", pFilename);
|
||||||
|
|
||||||
IOHANDLE File = pStorage->OpenFile(pFilename, IOFLAG_READ, StorageType);
|
IOHANDLE File = pStorage->OpenFile(pFilename, IOFLAG_READ, StorageType);
|
||||||
|
@ -205,7 +207,6 @@ bool CDataFileReader::Open(class IStorage *pStorage, const char *pFilename, int
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Close();
|
|
||||||
m_pDataFile = pTmpDataFile;
|
m_pDataFile = pTmpDataFile;
|
||||||
|
|
||||||
#if defined(CONF_ARCH_ENDIAN_BIG)
|
#if defined(CONF_ARCH_ENDIAN_BIG)
|
||||||
|
@ -257,27 +258,22 @@ bool CDataFileReader::Close()
|
||||||
|
|
||||||
IOHANDLE CDataFileReader::File() const
|
IOHANDLE CDataFileReader::File() const
|
||||||
{
|
{
|
||||||
if(!m_pDataFile)
|
dbg_assert(m_pDataFile != nullptr, "File not open");
|
||||||
return 0;
|
|
||||||
return m_pDataFile->m_File;
|
return m_pDataFile->m_File;
|
||||||
}
|
}
|
||||||
|
|
||||||
int CDataFileReader::NumData() const
|
int CDataFileReader::NumData() const
|
||||||
{
|
{
|
||||||
if(!m_pDataFile)
|
dbg_assert(m_pDataFile != nullptr, "File not open");
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return m_pDataFile->m_Header.m_NumRawData;
|
return m_pDataFile->m_Header.m_NumRawData;
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns the size in the file
|
// returns the size in the file
|
||||||
int CDataFileReader::GetFileDataSize(int Index) const
|
int CDataFileReader::GetFileDataSize(int Index) const
|
||||||
{
|
{
|
||||||
if(!m_pDataFile)
|
dbg_assert(m_pDataFile != nullptr, "File not open");
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(Index == m_pDataFile->m_Header.m_NumRawData - 1)
|
if(Index == m_pDataFile->m_Header.m_NumRawData - 1)
|
||||||
return m_pDataFile->m_Header.m_DataSize - m_pDataFile->m_Info.m_pDataOffsets[Index];
|
return m_pDataFile->m_Header.m_DataSize - m_pDataFile->m_Info.m_pDataOffsets[Index];
|
||||||
|
@ -288,7 +284,9 @@ int CDataFileReader::GetFileDataSize(int Index) const
|
||||||
// returns the size of the resulting data
|
// returns the size of the resulting data
|
||||||
int CDataFileReader::GetDataSize(int Index) const
|
int CDataFileReader::GetDataSize(int Index) const
|
||||||
{
|
{
|
||||||
if(!m_pDataFile || Index < 0 || Index >= m_pDataFile->m_Header.m_NumRawData)
|
dbg_assert(m_pDataFile != nullptr, "File not open");
|
||||||
|
|
||||||
|
if(Index < 0 || Index >= m_pDataFile->m_Header.m_NumRawData)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -312,10 +310,7 @@ int CDataFileReader::GetDataSize(int Index) const
|
||||||
|
|
||||||
void *CDataFileReader::GetDataImpl(int Index, bool Swap)
|
void *CDataFileReader::GetDataImpl(int Index, bool Swap)
|
||||||
{
|
{
|
||||||
if(!m_pDataFile)
|
dbg_assert(m_pDataFile != nullptr, "File not open");
|
||||||
{
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(Index < 0 || Index >= m_pDataFile->m_Header.m_NumRawData)
|
if(Index < 0 || Index >= m_pDataFile->m_Header.m_NumRawData)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
@ -413,6 +408,8 @@ void *CDataFileReader::GetDataSwapped(int Index)
|
||||||
|
|
||||||
const char *CDataFileReader::GetDataString(int Index)
|
const char *CDataFileReader::GetDataString(int Index)
|
||||||
{
|
{
|
||||||
|
dbg_assert(m_pDataFile != nullptr, "File not open");
|
||||||
|
|
||||||
if(Index == -1)
|
if(Index == -1)
|
||||||
return "";
|
return "";
|
||||||
const int DataSize = GetDataSize(Index);
|
const int DataSize = GetDataSize(Index);
|
||||||
|
@ -426,6 +423,7 @@ const char *CDataFileReader::GetDataString(int Index)
|
||||||
|
|
||||||
void CDataFileReader::ReplaceData(int Index, char *pData, size_t Size)
|
void CDataFileReader::ReplaceData(int Index, char *pData, size_t Size)
|
||||||
{
|
{
|
||||||
|
dbg_assert(m_pDataFile != nullptr, "File not open");
|
||||||
dbg_assert(Index >= 0 && Index < m_pDataFile->m_Header.m_NumRawData, "Index invalid");
|
dbg_assert(Index >= 0 && Index < m_pDataFile->m_Header.m_NumRawData, "Index invalid");
|
||||||
|
|
||||||
free(m_pDataFile->m_ppDataPtrs[Index]);
|
free(m_pDataFile->m_ppDataPtrs[Index]);
|
||||||
|
@ -435,6 +433,8 @@ void CDataFileReader::ReplaceData(int Index, char *pData, size_t Size)
|
||||||
|
|
||||||
void CDataFileReader::UnloadData(int Index)
|
void CDataFileReader::UnloadData(int Index)
|
||||||
{
|
{
|
||||||
|
dbg_assert(m_pDataFile != nullptr, "File not open");
|
||||||
|
|
||||||
if(Index < 0 || Index >= m_pDataFile->m_Header.m_NumRawData)
|
if(Index < 0 || Index >= m_pDataFile->m_Header.m_NumRawData)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -445,8 +445,8 @@ void CDataFileReader::UnloadData(int Index)
|
||||||
|
|
||||||
int CDataFileReader::GetItemSize(int Index) const
|
int CDataFileReader::GetItemSize(int Index) const
|
||||||
{
|
{
|
||||||
if(!m_pDataFile)
|
dbg_assert(m_pDataFile != nullptr, "File not open");
|
||||||
return 0;
|
|
||||||
if(Index == m_pDataFile->m_Header.m_NumItems - 1)
|
if(Index == m_pDataFile->m_Header.m_NumItems - 1)
|
||||||
return m_pDataFile->m_Header.m_ItemSize - m_pDataFile->m_Info.m_pItemOffsets[Index] - sizeof(CDatafileItem);
|
return m_pDataFile->m_Header.m_ItemSize - m_pDataFile->m_Info.m_pItemOffsets[Index] - sizeof(CDatafileItem);
|
||||||
return m_pDataFile->m_Info.m_pItemOffsets[Index + 1] - m_pDataFile->m_Info.m_pItemOffsets[Index] - sizeof(CDatafileItem);
|
return m_pDataFile->m_Info.m_pItemOffsets[Index + 1] - m_pDataFile->m_Info.m_pItemOffsets[Index] - sizeof(CDatafileItem);
|
||||||
|
@ -501,16 +501,7 @@ int CDataFileReader::GetInternalItemType(int ExternalType)
|
||||||
|
|
||||||
void *CDataFileReader::GetItem(int Index, int *pType, int *pId, CUuid *pUuid)
|
void *CDataFileReader::GetItem(int Index, int *pType, int *pId, CUuid *pUuid)
|
||||||
{
|
{
|
||||||
if(!m_pDataFile)
|
dbg_assert(m_pDataFile != nullptr, "File not open");
|
||||||
{
|
|
||||||
if(pType)
|
|
||||||
*pType = 0;
|
|
||||||
if(pId)
|
|
||||||
*pId = 0;
|
|
||||||
if(pUuid)
|
|
||||||
*pUuid = UUID_ZEROED;
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
CDatafileItem *pItem = (CDatafileItem *)(m_pDataFile->m_Info.m_pItemStart + m_pDataFile->m_Info.m_pItemOffsets[Index]);
|
CDatafileItem *pItem = (CDatafileItem *)(m_pDataFile->m_Info.m_pItemStart + m_pDataFile->m_Info.m_pItemOffsets[Index]);
|
||||||
|
|
||||||
|
@ -529,12 +520,11 @@ void *CDataFileReader::GetItem(int Index, int *pType, int *pId, CUuid *pUuid)
|
||||||
|
|
||||||
void CDataFileReader::GetType(int Type, int *pStart, int *pNum)
|
void CDataFileReader::GetType(int Type, int *pStart, int *pNum)
|
||||||
{
|
{
|
||||||
|
dbg_assert(m_pDataFile != nullptr, "File not open");
|
||||||
|
|
||||||
*pStart = 0;
|
*pStart = 0;
|
||||||
*pNum = 0;
|
*pNum = 0;
|
||||||
|
|
||||||
if(!m_pDataFile)
|
|
||||||
return;
|
|
||||||
|
|
||||||
Type = GetInternalItemType(Type);
|
Type = GetInternalItemType(Type);
|
||||||
for(int i = 0; i < m_pDataFile->m_Header.m_NumItemTypes; i++)
|
for(int i = 0; i < m_pDataFile->m_Header.m_NumItemTypes; i++)
|
||||||
{
|
{
|
||||||
|
@ -549,10 +539,7 @@ void CDataFileReader::GetType(int Type, int *pStart, int *pNum)
|
||||||
|
|
||||||
int CDataFileReader::FindItemIndex(int Type, int Id)
|
int CDataFileReader::FindItemIndex(int Type, int Id)
|
||||||
{
|
{
|
||||||
if(!m_pDataFile)
|
dbg_assert(m_pDataFile != nullptr, "File not open");
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int Start, Num;
|
int Start, Num;
|
||||||
GetType(Type, &Start, &Num);
|
GetType(Type, &Start, &Num);
|
||||||
|
@ -580,36 +567,29 @@ void *CDataFileReader::FindItem(int Type, int Id)
|
||||||
|
|
||||||
int CDataFileReader::NumItems() const
|
int CDataFileReader::NumItems() const
|
||||||
{
|
{
|
||||||
if(!m_pDataFile)
|
dbg_assert(m_pDataFile != nullptr, "File not open");
|
||||||
return 0;
|
|
||||||
return m_pDataFile->m_Header.m_NumItems;
|
return m_pDataFile->m_Header.m_NumItems;
|
||||||
}
|
}
|
||||||
|
|
||||||
SHA256_DIGEST CDataFileReader::Sha256() const
|
SHA256_DIGEST CDataFileReader::Sha256() const
|
||||||
{
|
{
|
||||||
if(!m_pDataFile)
|
dbg_assert(m_pDataFile != nullptr, "File not open");
|
||||||
{
|
|
||||||
SHA256_DIGEST Result;
|
|
||||||
for(unsigned char &d : Result.data)
|
|
||||||
{
|
|
||||||
d = 0xff;
|
|
||||||
}
|
|
||||||
return Result;
|
|
||||||
}
|
|
||||||
return m_pDataFile->m_Sha256;
|
return m_pDataFile->m_Sha256;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned CDataFileReader::Crc() const
|
unsigned CDataFileReader::Crc() const
|
||||||
{
|
{
|
||||||
if(!m_pDataFile)
|
dbg_assert(m_pDataFile != nullptr, "File not open");
|
||||||
return 0xFFFFFFFF;
|
|
||||||
return m_pDataFile->m_Crc;
|
return m_pDataFile->m_Crc;
|
||||||
}
|
}
|
||||||
|
|
||||||
int CDataFileReader::MapSize() const
|
int CDataFileReader::MapSize() const
|
||||||
{
|
{
|
||||||
if(!m_pDataFile)
|
dbg_assert(m_pDataFile != nullptr, "File not open");
|
||||||
return 0;
|
|
||||||
return m_pDataFile->m_Header.m_Size + m_pDataFile->m_Header.SizeOffset();
|
return m_pDataFile->m_Header.m_Size + m_pDataFile->m_Header.SizeOffset();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -81,7 +81,6 @@ void CreateEmptyMap(IStorage *pStorage)
|
||||||
|
|
||||||
dbg_msg("dummy_map", "dummy map written to '%s'", pMapName);
|
dbg_msg("dummy_map", "dummy map written to '%s'", pMapName);
|
||||||
|
|
||||||
CDataFileReader Reader;
|
|
||||||
void *pData;
|
void *pData;
|
||||||
unsigned DataSize;
|
unsigned DataSize;
|
||||||
if(!pStorage->ReadFile(pMapName, IStorage::TYPE_ALL, &pData, &DataSize))
|
if(!pStorage->ReadFile(pMapName, IStorage::TYPE_ALL, &pData, &DataSize))
|
||||||
|
|
Loading…
Reference in a new issue