2010-11-20 10:37:14 +00:00
|
|
|
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
|
|
|
|
/* If you are missing that file, acquire a complete release at teeworlds.com. */
|
2018-10-05 09:20:30 +00:00
|
|
|
|
|
|
|
#include "datafile.h"
|
|
|
|
|
2010-10-28 21:32:01 +00:00
|
|
|
#include <base/math.h>
|
2018-06-05 19:22:40 +00:00
|
|
|
#include <base/hash_ctxt.h>
|
2010-05-29 07:25:38 +00:00
|
|
|
#include <base/system.h>
|
|
|
|
#include <engine/storage.h>
|
2018-10-05 09:20:30 +00:00
|
|
|
|
|
|
|
#include "uuid_manager.h"
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
#include <zlib.h>
|
|
|
|
|
|
|
|
static const int DEBUG=0;
|
|
|
|
|
2018-10-05 09:20:30 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
OFFSET_UUID_TYPE=0x8000,
|
|
|
|
ITEMTYPE_EX=0xffff,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CItemEx
|
|
|
|
{
|
|
|
|
int m_aUuid[sizeof(CUuid) / 4];
|
|
|
|
|
|
|
|
static CItemEx FromUuid(CUuid Uuid)
|
|
|
|
{
|
|
|
|
CItemEx Result;
|
|
|
|
for(int i = 0; i < (int)sizeof(CUuid) / 4; i++)
|
|
|
|
{
|
|
|
|
Result.m_aUuid[i] =
|
|
|
|
(Uuid.m_aData[i * 4 + 0] << 24) |
|
|
|
|
(Uuid.m_aData[i * 4 + 1] << 16) |
|
|
|
|
(Uuid.m_aData[i * 4 + 2] << 8) |
|
|
|
|
(Uuid.m_aData[i * 4 + 3]);
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
CUuid ToUuid() const
|
|
|
|
{
|
|
|
|
CUuid Result;
|
|
|
|
for(int i = 0; i < (int)sizeof(CUuid) / 4; i++)
|
|
|
|
{
|
|
|
|
Result.m_aData[i * 4 + 0] = m_aUuid[i] >> 24;
|
|
|
|
Result.m_aData[i * 4 + 1] = m_aUuid[i] >> 16;
|
|
|
|
Result.m_aData[i * 4 + 2] = m_aUuid[i] >> 8;
|
|
|
|
Result.m_aData[i * 4 + 3] = m_aUuid[i];
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static int GetTypeFromIndex(int Index)
|
|
|
|
{
|
|
|
|
return ITEMTYPE_EX - Index - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
struct CDatafileItemType
|
|
|
|
{
|
|
|
|
int m_Type;
|
|
|
|
int m_Start;
|
|
|
|
int m_Num;
|
|
|
|
} ;
|
|
|
|
|
|
|
|
struct CDatafileItem
|
|
|
|
{
|
2011-02-12 10:40:36 +00:00
|
|
|
int m_TypeAndID;
|
2010-05-29 07:25:38 +00:00
|
|
|
int m_Size;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CDatafileHeader
|
|
|
|
{
|
2011-02-12 10:40:36 +00:00
|
|
|
char m_aID[4];
|
2010-05-29 07:25:38 +00:00
|
|
|
int m_Version;
|
|
|
|
int m_Size;
|
|
|
|
int m_Swaplen;
|
|
|
|
int m_NumItemTypes;
|
|
|
|
int m_NumItems;
|
|
|
|
int m_NumRawData;
|
|
|
|
int m_ItemSize;
|
|
|
|
int m_DataSize;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CDatafileData
|
|
|
|
{
|
|
|
|
int m_NumItemTypes;
|
|
|
|
int m_NumItems;
|
|
|
|
int m_NumRawData;
|
|
|
|
int m_ItemSize;
|
|
|
|
int m_DataSize;
|
|
|
|
char m_aStart[4];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CDatafileInfo
|
|
|
|
{
|
|
|
|
CDatafileItemType *m_pItemTypes;
|
|
|
|
int *m_pItemOffsets;
|
|
|
|
int *m_pDataOffsets;
|
|
|
|
int *m_pDataSizes;
|
|
|
|
|
|
|
|
char *m_pItemStart;
|
|
|
|
char *m_pDataStart;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CDatafile
|
|
|
|
{
|
|
|
|
IOHANDLE m_File;
|
2018-06-05 19:22:40 +00:00
|
|
|
SHA256_DIGEST m_Sha256;
|
2010-05-29 07:25:38 +00:00
|
|
|
unsigned m_Crc;
|
|
|
|
CDatafileInfo m_Info;
|
|
|
|
CDatafileHeader m_Header;
|
|
|
|
int m_DataStartOffset;
|
|
|
|
char **m_ppDataPtrs;
|
|
|
|
char *m_pData;
|
|
|
|
};
|
|
|
|
|
2010-10-06 21:07:35 +00:00
|
|
|
bool CDataFileReader::Open(class IStorage *pStorage, const char *pFilename, int StorageType)
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
|
|
|
dbg_msg("datafile", "loading. filename='%s'", pFilename);
|
|
|
|
|
2010-10-06 21:07:35 +00:00
|
|
|
IOHANDLE File = pStorage->OpenFile(pFilename, IOFLAG_READ, StorageType);
|
2010-05-29 07:25:38 +00:00
|
|
|
if(!File)
|
|
|
|
{
|
|
|
|
dbg_msg("datafile", "could not open '%s'", pFilename);
|
|
|
|
return false;
|
2011-04-13 18:37:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// take the CRC of the file and store it
|
|
|
|
unsigned Crc = 0;
|
2018-06-05 19:22:40 +00:00
|
|
|
SHA256_DIGEST Sha256;
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
BUFFER_SIZE = 64*1024
|
|
|
|
};
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2018-06-05 19:22:40 +00:00
|
|
|
SHA256_CTX Sha256Ctxt;
|
|
|
|
sha256_init(&Sha256Ctxt);
|
2010-05-29 07:25:38 +00:00
|
|
|
unsigned char aBuffer[BUFFER_SIZE];
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
unsigned Bytes = io_read(File, aBuffer, BUFFER_SIZE);
|
|
|
|
if(Bytes <= 0)
|
|
|
|
break;
|
|
|
|
Crc = crc32(Crc, aBuffer, Bytes); // ignore_convention
|
2018-06-05 19:22:40 +00:00
|
|
|
sha256_update(&Sha256Ctxt, aBuffer, Bytes);
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
2018-06-05 19:22:40 +00:00
|
|
|
Sha256 = sha256_finish(&Sha256Ctxt);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
io_seek(File, 0, IOSEEK_START);
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// TODO: change this header
|
|
|
|
CDatafileHeader Header;
|
2016-01-18 16:20:47 +00:00
|
|
|
if (sizeof(Header) != io_read(File, &Header, sizeof(Header)))
|
|
|
|
{
|
2016-01-17 08:48:21 +00:00
|
|
|
dbg_msg("datafile", "couldn't load header");
|
|
|
|
return 0;
|
|
|
|
}
|
2011-02-12 10:40:36 +00:00
|
|
|
if(Header.m_aID[0] != 'A' || Header.m_aID[1] != 'T' || Header.m_aID[2] != 'A' || Header.m_aID[3] != 'D')
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
2011-02-12 10:40:36 +00:00
|
|
|
if(Header.m_aID[0] != 'D' || Header.m_aID[1] != 'A' || Header.m_aID[2] != 'T' || Header.m_aID[3] != 'A')
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
2011-02-12 10:40:36 +00:00
|
|
|
dbg_msg("datafile", "wrong signature. %x %x %x %x", Header.m_aID[0], Header.m_aID[1], Header.m_aID[2], Header.m_aID[3]);
|
2010-05-29 07:25:38 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(CONF_ARCH_ENDIAN_BIG)
|
2011-04-13 18:37:12 +00:00
|
|
|
swap_endian(&Header, sizeof(int), sizeof(Header)/sizeof(int));
|
2010-05-29 07:25:38 +00:00
|
|
|
#endif
|
|
|
|
if(Header.m_Version != 3 && Header.m_Version != 4)
|
|
|
|
{
|
|
|
|
dbg_msg("datafile", "wrong version. version=%x", Header.m_Version);
|
|
|
|
return 0;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// read in the rest except the data
|
|
|
|
unsigned Size = 0;
|
|
|
|
Size += Header.m_NumItemTypes*sizeof(CDatafileItemType);
|
|
|
|
Size += (Header.m_NumItems+Header.m_NumRawData)*sizeof(int);
|
|
|
|
if(Header.m_Version == 4)
|
2018-02-04 15:00:47 +00:00
|
|
|
Size += Header.m_NumRawData*sizeof(int); // v4 has uncompressed data sizes as well
|
2010-05-29 07:25:38 +00:00
|
|
|
Size += Header.m_ItemSize;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
unsigned AllocSize = Size;
|
|
|
|
AllocSize += sizeof(CDatafile); // add space for info structure
|
|
|
|
AllocSize += Header.m_NumRawData*sizeof(void*); // add space for data pointers
|
|
|
|
|
2018-04-09 09:56:39 +00:00
|
|
|
CDatafile *pTmpDataFile = (CDatafile *)malloc(AllocSize);
|
2010-09-28 10:46:15 +00:00
|
|
|
pTmpDataFile->m_Header = Header;
|
|
|
|
pTmpDataFile->m_DataStartOffset = sizeof(CDatafileHeader) + Size;
|
2018-04-09 09:56:39 +00:00
|
|
|
pTmpDataFile->m_ppDataPtrs = (char **)(pTmpDataFile+1);
|
2010-09-28 10:46:15 +00:00
|
|
|
pTmpDataFile->m_pData = (char *)(pTmpDataFile+1)+Header.m_NumRawData*sizeof(char *);
|
|
|
|
pTmpDataFile->m_File = File;
|
2018-06-05 19:22:40 +00:00
|
|
|
pTmpDataFile->m_Sha256 = Sha256;
|
2010-09-28 10:46:15 +00:00
|
|
|
pTmpDataFile->m_Crc = Crc;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// clear the data pointers
|
2010-09-28 10:46:15 +00:00
|
|
|
mem_zero(pTmpDataFile->m_ppDataPtrs, Header.m_NumRawData*sizeof(void*));
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// read types, offsets, sizes and item data
|
2010-09-28 10:46:15 +00:00
|
|
|
unsigned ReadSize = io_read(File, pTmpDataFile->m_pData, Size);
|
2010-05-29 07:25:38 +00:00
|
|
|
if(ReadSize != Size)
|
|
|
|
{
|
2010-09-28 10:46:15 +00:00
|
|
|
io_close(pTmpDataFile->m_File);
|
2018-04-09 09:56:39 +00:00
|
|
|
free(pTmpDataFile);
|
2010-09-28 10:46:15 +00:00
|
|
|
pTmpDataFile = 0;
|
2010-05-29 07:25:38 +00:00
|
|
|
dbg_msg("datafile", "couldn't load the whole thing, wanted=%d got=%d", Size, ReadSize);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-09-28 10:46:15 +00:00
|
|
|
Close();
|
|
|
|
m_pDataFile = pTmpDataFile;
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
#if defined(CONF_ARCH_ENDIAN_BIG)
|
2019-04-26 19:36:49 +00:00
|
|
|
swap_endian(m_pDataFile->m_pData, sizeof(int), minimum(static_cast<unsigned>(Header.m_Swaplen), Size) / sizeof(int));
|
2010-05-29 07:25:38 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
//if(DEBUG)
|
|
|
|
{
|
|
|
|
dbg_msg("datafile", "allocsize=%d", AllocSize);
|
|
|
|
dbg_msg("datafile", "readsize=%d", ReadSize);
|
|
|
|
dbg_msg("datafile", "swaplen=%d", Header.m_Swaplen);
|
|
|
|
dbg_msg("datafile", "item_size=%d", m_pDataFile->m_Header.m_ItemSize);
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
m_pDataFile->m_Info.m_pItemTypes = (CDatafileItemType *)m_pDataFile->m_pData;
|
|
|
|
m_pDataFile->m_Info.m_pItemOffsets = (int *)&m_pDataFile->m_Info.m_pItemTypes[m_pDataFile->m_Header.m_NumItemTypes];
|
2019-04-11 10:21:42 +00:00
|
|
|
m_pDataFile->m_Info.m_pDataOffsets = &m_pDataFile->m_Info.m_pItemOffsets[m_pDataFile->m_Header.m_NumItems];
|
|
|
|
m_pDataFile->m_Info.m_pDataSizes = &m_pDataFile->m_Info.m_pDataOffsets[m_pDataFile->m_Header.m_NumRawData];
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(Header.m_Version == 4)
|
|
|
|
m_pDataFile->m_Info.m_pItemStart = (char *)&m_pDataFile->m_Info.m_pDataSizes[m_pDataFile->m_Header.m_NumRawData];
|
|
|
|
else
|
|
|
|
m_pDataFile->m_Info.m_pItemStart = (char *)&m_pDataFile->m_Info.m_pDataOffsets[m_pDataFile->m_Header.m_NumRawData];
|
|
|
|
m_pDataFile->m_Info.m_pDataStart = m_pDataFile->m_Info.m_pItemStart + m_pDataFile->m_Header.m_ItemSize;
|
|
|
|
|
|
|
|
dbg_msg("datafile", "loading done. datafile='%s'", pFilename);
|
|
|
|
|
|
|
|
if(DEBUG)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
for(int i = 0; i < m_pDataFile->data.num_raw_data; i++)
|
|
|
|
{
|
|
|
|
void *p = datafile_get_data(df, i);
|
|
|
|
dbg_msg("datafile", "%d %d", (int)((char*)p - (char*)(&m_pDataFile->data)), size);
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
for(int i = 0; i < datafile_num_items(df); i++)
|
|
|
|
{
|
|
|
|
int type, id;
|
|
|
|
void *data = datafile_get_item(df, i, &type, &id);
|
|
|
|
dbg_msg("map", "\t%d: type=%x id=%x p=%p offset=%d", i, type, id, data, m_pDataFile->info.item_offsets[i]);
|
|
|
|
int *idata = (int*)data;
|
|
|
|
for(int k = 0; k < 3; k++)
|
|
|
|
dbg_msg("datafile", "\t\t%d=%d (%x)", k, idata[k], idata[k]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int i = 0; i < m_pDataFile->data.num_m_aItemTypes; i++)
|
|
|
|
{
|
|
|
|
dbg_msg("map", "\t%d: type=%x start=%d num=%d", i,
|
|
|
|
m_pDataFile->info.m_aItemTypes[i].type,
|
|
|
|
m_pDataFile->info.m_aItemTypes[i].start,
|
|
|
|
m_pDataFile->info.m_aItemTypes[i].num);
|
|
|
|
for(int k = 0; k < m_pDataFile->info.m_aItemTypes[i].num; k++)
|
|
|
|
{
|
|
|
|
int type, id;
|
|
|
|
datafile_get_item(df, m_pDataFile->info.m_aItemTypes[i].start+k, &type, &id);
|
|
|
|
if(type != m_pDataFile->info.m_aItemTypes[i].type)
|
|
|
|
dbg_msg("map", "\tERROR");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CDataFileReader::NumData()
|
|
|
|
{
|
|
|
|
if(!m_pDataFile) { return 0; }
|
|
|
|
return m_pDataFile->m_Header.m_NumRawData;
|
|
|
|
}
|
|
|
|
|
2017-08-30 06:36:17 +00:00
|
|
|
// returns the size in the file
|
|
|
|
int CDataFileReader::GetFileDataSize(int Index)
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
|
|
|
if(!m_pDataFile) { return 0; }
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(Index == m_pDataFile->m_Header.m_NumRawData-1)
|
|
|
|
return m_pDataFile->m_Header.m_DataSize-m_pDataFile->m_Info.m_pDataOffsets[Index];
|
2011-04-13 18:37:12 +00:00
|
|
|
return m_pDataFile->m_Info.m_pDataOffsets[Index+1]-m_pDataFile->m_Info.m_pDataOffsets[Index];
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
|
|
|
|
2017-08-30 06:36:17 +00:00
|
|
|
// returns the size of the resulting data
|
|
|
|
int CDataFileReader::GetDataSize(int Index)
|
2014-01-10 23:46:32 +00:00
|
|
|
{
|
|
|
|
if(!m_pDataFile) { return 0; }
|
|
|
|
|
|
|
|
if(m_pDataFile->m_Header.m_Version == 4)
|
|
|
|
return m_pDataFile->m_Info.m_pDataSizes[Index];
|
|
|
|
else
|
2017-08-30 06:36:17 +00:00
|
|
|
return GetFileDataSize(Index);
|
2014-01-10 23:46:32 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void *CDataFileReader::GetDataImpl(int Index, int Swap)
|
|
|
|
{
|
|
|
|
if(!m_pDataFile) { return 0; }
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2019-03-25 15:28:54 +00:00
|
|
|
if(Index < 0 || Index >= m_pDataFile->m_Header.m_NumRawData)
|
|
|
|
return 0;
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// load it if needed
|
|
|
|
if(!m_pDataFile->m_ppDataPtrs[Index])
|
|
|
|
{
|
|
|
|
// fetch the data size
|
2017-08-30 06:36:17 +00:00
|
|
|
int DataSize = GetFileDataSize(Index);
|
2011-05-04 23:43:27 +00:00
|
|
|
#if defined(CONF_ARCH_ENDIAN_BIG)
|
2010-05-29 07:25:38 +00:00
|
|
|
int SwapSize = DataSize;
|
2011-05-04 23:43:27 +00:00
|
|
|
#endif
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(m_pDataFile->m_Header.m_Version == 4)
|
|
|
|
{
|
|
|
|
// v4 has compressed data
|
2018-04-09 09:56:39 +00:00
|
|
|
void *pTemp = malloc(DataSize);
|
2010-05-29 07:25:38 +00:00
|
|
|
unsigned long UncompressedSize = m_pDataFile->m_Info.m_pDataSizes[Index];
|
|
|
|
unsigned long s;
|
|
|
|
|
2017-07-26 01:58:00 +00:00
|
|
|
dbg_msg("datafile", "loading data index=%d size=%d uncompressed=%lu", Index, DataSize, UncompressedSize);
|
2018-04-09 09:56:39 +00:00
|
|
|
m_pDataFile->m_ppDataPtrs[Index] = (char *)malloc(UncompressedSize);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// read the compressed data
|
|
|
|
io_seek(m_pDataFile->m_File, m_pDataFile->m_DataStartOffset+m_pDataFile->m_Info.m_pDataOffsets[Index], IOSEEK_START);
|
|
|
|
io_read(m_pDataFile->m_File, pTemp, DataSize);
|
|
|
|
|
|
|
|
// decompress the data, TODO: check for errors
|
|
|
|
s = UncompressedSize;
|
|
|
|
uncompress((Bytef*)m_pDataFile->m_ppDataPtrs[Index], &s, (Bytef*)pTemp, DataSize); // ignore_convention
|
2011-05-04 23:43:27 +00:00
|
|
|
#if defined(CONF_ARCH_ENDIAN_BIG)
|
2010-05-29 07:25:38 +00:00
|
|
|
SwapSize = s;
|
2011-05-04 23:43:27 +00:00
|
|
|
#endif
|
2010-05-29 07:25:38 +00:00
|
|
|
|
|
|
|
// clean up the temporary buffers
|
2018-04-09 09:56:39 +00:00
|
|
|
free(pTemp);
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// load the data
|
|
|
|
dbg_msg("datafile", "loading data index=%d size=%d", Index, DataSize);
|
2018-04-09 09:56:39 +00:00
|
|
|
m_pDataFile->m_ppDataPtrs[Index] = (char *)malloc(DataSize);
|
2010-05-29 07:25:38 +00:00
|
|
|
io_seek(m_pDataFile->m_File, m_pDataFile->m_DataStartOffset+m_pDataFile->m_Info.m_pDataOffsets[Index], IOSEEK_START);
|
|
|
|
io_read(m_pDataFile->m_File, m_pDataFile->m_ppDataPtrs[Index], DataSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(CONF_ARCH_ENDIAN_BIG)
|
|
|
|
if(Swap && SwapSize)
|
|
|
|
swap_endian(m_pDataFile->m_ppDataPtrs[Index], sizeof(int), SwapSize/sizeof(int));
|
|
|
|
#endif
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
return m_pDataFile->m_ppDataPtrs[Index];
|
|
|
|
}
|
|
|
|
|
|
|
|
void *CDataFileReader::GetData(int Index)
|
|
|
|
{
|
|
|
|
return GetDataImpl(Index, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *CDataFileReader::GetDataSwapped(int Index)
|
|
|
|
{
|
|
|
|
return GetDataImpl(Index, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CDataFileReader::UnloadData(int Index)
|
|
|
|
{
|
2019-03-25 15:28:54 +00:00
|
|
|
if(Index < 0 || Index >= m_pDataFile->m_Header.m_NumRawData)
|
2010-05-29 07:25:38 +00:00
|
|
|
return;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
//
|
2018-04-09 09:56:39 +00:00
|
|
|
free(m_pDataFile->m_ppDataPtrs[Index]);
|
2010-05-29 07:25:38 +00:00
|
|
|
m_pDataFile->m_ppDataPtrs[Index] = 0x0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CDataFileReader::GetItemSize(int Index)
|
|
|
|
{
|
2017-08-28 16:06:19 +00:00
|
|
|
if(!m_pDataFile)
|
|
|
|
return 0;
|
2010-05-29 07:25:38 +00:00
|
|
|
if(Index == m_pDataFile->m_Header.m_NumItems-1)
|
2017-08-28 16:06:19 +00:00
|
|
|
return m_pDataFile->m_Header.m_ItemSize-m_pDataFile->m_Info.m_pItemOffsets[Index] - sizeof(CDatafileItem);
|
2017-08-29 22:21:41 +00:00
|
|
|
return m_pDataFile->m_Info.m_pItemOffsets[Index+1]-m_pDataFile->m_Info.m_pItemOffsets[Index] - sizeof(CDatafileItem);
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
|
|
|
|
2018-10-05 09:20:30 +00:00
|
|
|
int CDataFileReader::GetExternalItemType(int InternalType)
|
|
|
|
{
|
|
|
|
if(InternalType <= OFFSET_UUID_TYPE || InternalType == ITEMTYPE_EX)
|
|
|
|
{
|
|
|
|
return InternalType;
|
|
|
|
}
|
|
|
|
int TypeIndex = FindItemIndex(ITEMTYPE_EX, InternalType);
|
|
|
|
if(TypeIndex < 0 || GetItemSize(TypeIndex) < (int)sizeof(CItemEx))
|
|
|
|
{
|
|
|
|
return InternalType;
|
|
|
|
}
|
|
|
|
const CItemEx *pItemEx = (const CItemEx *)GetItem(TypeIndex, 0, 0);
|
|
|
|
// Propagate UUID_UNKNOWN, it doesn't hurt.
|
|
|
|
return g_UuidManager.LookupUuid(pItemEx->ToUuid());
|
|
|
|
}
|
|
|
|
|
|
|
|
int CDataFileReader::GetInternalItemType(int ExternalType)
|
|
|
|
{
|
|
|
|
if(ExternalType < OFFSET_UUID)
|
|
|
|
{
|
|
|
|
return ExternalType;
|
|
|
|
}
|
|
|
|
CUuid Uuid = g_UuidManager.GetUuid(ExternalType);
|
|
|
|
int Start, Num;
|
|
|
|
GetType(ITEMTYPE_EX, &Start, &Num);
|
|
|
|
for(int i = Start; i < Start + Num; i++)
|
|
|
|
{
|
|
|
|
if(GetItemSize(i) < (int)sizeof(CItemEx))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int ID;
|
|
|
|
if(Uuid == ((const CItemEx *)GetItem(i, 0, &ID))->ToUuid())
|
|
|
|
{
|
|
|
|
return ID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-02-12 10:40:36 +00:00
|
|
|
void *CDataFileReader::GetItem(int Index, int *pType, int *pID)
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
2011-02-12 10:40:36 +00:00
|
|
|
if(!m_pDataFile) { if(pType) *pType = 0; if(pID) *pID = 0; return 0; }
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
CDatafileItem *i = (CDatafileItem *)(m_pDataFile->m_Info.m_pItemStart+m_pDataFile->m_Info.m_pItemOffsets[Index]);
|
|
|
|
if(pType)
|
2018-10-05 09:20:30 +00:00
|
|
|
{
|
|
|
|
// remove sign extension
|
|
|
|
*pType = GetExternalItemType((i->m_TypeAndID>>16)&0xffff);
|
|
|
|
}
|
2011-02-12 10:40:36 +00:00
|
|
|
if(pID)
|
2018-10-05 09:20:30 +00:00
|
|
|
{
|
2011-02-12 10:40:36 +00:00
|
|
|
*pID = i->m_TypeAndID&0xffff;
|
2018-10-05 09:20:30 +00:00
|
|
|
}
|
2010-05-29 07:25:38 +00:00
|
|
|
return (void *)(i+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CDataFileReader::GetType(int Type, int *pStart, int *pNum)
|
|
|
|
{
|
|
|
|
*pStart = 0;
|
|
|
|
*pNum = 0;
|
|
|
|
|
|
|
|
if(!m_pDataFile)
|
|
|
|
return;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2018-10-05 09:20:30 +00:00
|
|
|
Type = GetInternalItemType(Type);
|
2010-05-29 07:25:38 +00:00
|
|
|
for(int i = 0; i < m_pDataFile->m_Header.m_NumItemTypes; i++)
|
|
|
|
{
|
|
|
|
if(m_pDataFile->m_Info.m_pItemTypes[i].m_Type == Type)
|
|
|
|
{
|
|
|
|
*pStart = m_pDataFile->m_Info.m_pItemTypes[i].m_Start;
|
|
|
|
*pNum = m_pDataFile->m_Info.m_pItemTypes[i].m_Num;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 09:20:30 +00:00
|
|
|
int CDataFileReader::FindItemIndex(int Type, int ID)
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
2018-10-05 09:20:30 +00:00
|
|
|
if(!m_pDataFile)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
int Start, Num;
|
|
|
|
GetType(Type, &Start, &Num);
|
|
|
|
for(int i = 0; i < Num; i++)
|
|
|
|
{
|
2011-02-12 10:40:36 +00:00
|
|
|
int ItemID;
|
2018-10-05 09:20:30 +00:00
|
|
|
GetItem(Start + i, 0, &ItemID);
|
2011-02-12 10:40:36 +00:00
|
|
|
if(ID == ItemID)
|
2018-10-05 09:20:30 +00:00
|
|
|
{
|
|
|
|
return Start + i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *CDataFileReader::FindItem(int Type, int ID)
|
|
|
|
{
|
|
|
|
int Index = FindItemIndex(Type, ID);
|
|
|
|
if(Index < 0)
|
|
|
|
{
|
|
|
|
return 0;
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
2018-10-05 09:20:30 +00:00
|
|
|
return GetItem(Index, 0, 0);
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int CDataFileReader::NumItems()
|
|
|
|
{
|
|
|
|
if(!m_pDataFile) return 0;
|
|
|
|
return m_pDataFile->m_Header.m_NumItems;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CDataFileReader::Close()
|
|
|
|
{
|
|
|
|
if(!m_pDataFile)
|
|
|
|
return true;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// free the data that is loaded
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < m_pDataFile->m_Header.m_NumRawData; i++)
|
2018-04-09 09:56:39 +00:00
|
|
|
free(m_pDataFile->m_ppDataPtrs[i]);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
io_close(m_pDataFile->m_File);
|
2018-04-09 09:56:39 +00:00
|
|
|
free(m_pDataFile);
|
2010-05-29 07:25:38 +00:00
|
|
|
m_pDataFile = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-05 19:22:40 +00:00
|
|
|
SHA256_DIGEST CDataFileReader::Sha256()
|
|
|
|
{
|
|
|
|
if(!m_pDataFile)
|
|
|
|
{
|
|
|
|
SHA256_DIGEST Result;
|
|
|
|
for(unsigned i = 0; i < sizeof(Result.data); i++)
|
|
|
|
{
|
|
|
|
Result.data[i] = 0xff;
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
return m_pDataFile->m_Sha256;
|
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
unsigned CDataFileReader::Crc()
|
|
|
|
{
|
2010-08-16 00:21:18 +00:00
|
|
|
if(!m_pDataFile) return 0xFFFFFFFF;
|
2010-05-29 07:25:38 +00:00
|
|
|
return m_pDataFile->m_Crc;
|
|
|
|
}
|
|
|
|
|
2017-07-08 11:38:27 +00:00
|
|
|
int CDataFileReader::MapSize()
|
|
|
|
{
|
|
|
|
if(!m_pDataFile) return 0;
|
|
|
|
return m_pDataFile->m_Header.m_Size + 16;
|
|
|
|
}
|
|
|
|
|
2017-07-08 20:09:03 +00:00
|
|
|
IOHANDLE CDataFileReader::File()
|
|
|
|
{
|
|
|
|
if(!m_pDataFile) return 0;
|
|
|
|
return m_pDataFile->m_File;
|
|
|
|
}
|
|
|
|
|
2011-07-30 23:40:28 +00:00
|
|
|
|
|
|
|
CDataFileWriter::CDataFileWriter()
|
|
|
|
{
|
|
|
|
m_File = 0;
|
2018-04-09 09:56:39 +00:00
|
|
|
m_pItemTypes = static_cast<CItemTypeInfo *>(calloc(MAX_ITEM_TYPES, sizeof(CItemTypeInfo)));
|
|
|
|
m_pItems = static_cast<CItemInfo *>(calloc(MAX_ITEMS, sizeof(CItemInfo)));
|
|
|
|
m_pDatas = static_cast<CDataInfo *>(calloc(MAX_DATAS, sizeof(CDataInfo)));
|
2011-07-30 23:40:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CDataFileWriter::~CDataFileWriter()
|
|
|
|
{
|
2018-04-09 09:56:39 +00:00
|
|
|
free(m_pItemTypes);
|
2011-07-30 23:40:28 +00:00
|
|
|
m_pItemTypes = 0;
|
2018-04-09 09:56:39 +00:00
|
|
|
free(m_pItems);
|
2011-07-30 23:40:28 +00:00
|
|
|
m_pItems = 0;
|
2018-04-09 09:56:39 +00:00
|
|
|
free(m_pDatas);
|
2011-07-30 23:40:28 +00:00
|
|
|
m_pDatas = 0;
|
|
|
|
}
|
|
|
|
|
2015-08-21 21:02:50 +00:00
|
|
|
bool CDataFileWriter::OpenFile(class IStorage *pStorage, const char *pFilename)
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
2010-09-27 20:35:57 +00:00
|
|
|
dbg_assert(!m_File, "a file already exists");
|
2010-10-06 21:07:35 +00:00
|
|
|
m_File = pStorage->OpenFile(pFilename, IOFLAG_WRITE, IStorage::TYPE_SAVE);
|
2015-08-21 21:02:50 +00:00
|
|
|
return m_File != 0;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2015-08-21 21:02:50 +00:00
|
|
|
void CDataFileWriter::Init()
|
|
|
|
{
|
|
|
|
dbg_assert(!m_File, "a file already exists");
|
2010-05-29 07:25:38 +00:00
|
|
|
m_NumItems = 0;
|
|
|
|
m_NumDatas = 0;
|
|
|
|
m_NumItemTypes = 0;
|
2018-10-05 09:20:30 +00:00
|
|
|
m_NumExtendedItemTypes = 0;
|
2011-07-30 23:40:28 +00:00
|
|
|
mem_zero(m_pItemTypes, sizeof(CItemTypeInfo) * MAX_ITEM_TYPES);
|
2018-10-05 09:20:30 +00:00
|
|
|
mem_zero(m_aExtendedItemTypes, sizeof(m_aExtendedItemTypes));
|
2010-05-29 07:25:38 +00:00
|
|
|
|
2011-07-30 23:40:28 +00:00
|
|
|
for(int i = 0; i < MAX_ITEM_TYPES; i++)
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
2011-07-30 23:40:28 +00:00
|
|
|
m_pItemTypes[i].m_First = -1;
|
|
|
|
m_pItemTypes[i].m_Last = -1;
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
2015-08-21 21:02:50 +00:00
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2015-08-21 21:02:50 +00:00
|
|
|
bool CDataFileWriter::Open(class IStorage *pStorage, const char *pFilename)
|
|
|
|
{
|
|
|
|
Init();
|
|
|
|
return OpenFile(pStorage, pFilename);
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
|
|
|
|
2018-10-05 09:20:30 +00:00
|
|
|
int CDataFileWriter::GetExtendedItemTypeIndex(int Type)
|
|
|
|
{
|
|
|
|
for(int i = 0; i < m_NumExtendedItemTypes; i++)
|
|
|
|
{
|
|
|
|
if(m_aExtendedItemTypes[i] == Type)
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type not found, add it.
|
|
|
|
dbg_assert(m_NumExtendedItemTypes < MAX_EXTENDED_ITEM_TYPES, "too many extended item types");
|
|
|
|
int Index = m_NumExtendedItemTypes++;
|
|
|
|
m_aExtendedItemTypes[Index] = Type;
|
|
|
|
|
|
|
|
CItemEx ExtendedType = CItemEx::FromUuid(g_UuidManager.GetUuid(Type));
|
|
|
|
AddItem(ITEMTYPE_EX, GetTypeFromIndex(Index), sizeof(ExtendedType), &ExtendedType);
|
|
|
|
return Index;
|
|
|
|
}
|
|
|
|
|
2011-02-12 10:40:36 +00:00
|
|
|
int CDataFileWriter::AddItem(int Type, int ID, int Size, void *pData)
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
2018-10-05 09:20:30 +00:00
|
|
|
dbg_assert((Type >= 0 && Type < MAX_ITEM_TYPES) || Type >= OFFSET_UUID, "incorrect type");
|
2010-09-27 20:35:57 +00:00
|
|
|
dbg_assert(m_NumItems < 1024, "too many items");
|
|
|
|
dbg_assert(Size%sizeof(int) == 0, "incorrect boundary");
|
|
|
|
|
2018-10-05 09:20:30 +00:00
|
|
|
if(Type >= OFFSET_UUID)
|
|
|
|
{
|
|
|
|
Type = GetTypeFromIndex(GetExtendedItemTypeIndex(Type));
|
|
|
|
}
|
|
|
|
|
2011-07-30 23:40:28 +00:00
|
|
|
m_pItems[m_NumItems].m_Type = Type;
|
|
|
|
m_pItems[m_NumItems].m_ID = ID;
|
|
|
|
m_pItems[m_NumItems].m_Size = Size;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// copy data
|
2018-04-09 09:56:39 +00:00
|
|
|
m_pItems[m_NumItems].m_pData = malloc(Size);
|
2011-07-30 23:40:28 +00:00
|
|
|
mem_copy(m_pItems[m_NumItems].m_pData, pData, Size);
|
2010-05-29 07:25:38 +00:00
|
|
|
|
2011-07-30 23:40:28 +00:00
|
|
|
if(!m_pItemTypes[Type].m_Num) // count item types
|
2010-05-29 07:25:38 +00:00
|
|
|
m_NumItemTypes++;
|
|
|
|
|
|
|
|
// link
|
2011-07-30 23:40:28 +00:00
|
|
|
m_pItems[m_NumItems].m_Prev = m_pItemTypes[Type].m_Last;
|
|
|
|
m_pItems[m_NumItems].m_Next = -1;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2011-07-30 23:40:28 +00:00
|
|
|
if(m_pItemTypes[Type].m_Last != -1)
|
|
|
|
m_pItems[m_pItemTypes[Type].m_Last].m_Next = m_NumItems;
|
|
|
|
m_pItemTypes[Type].m_Last = m_NumItems;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2011-07-30 23:40:28 +00:00
|
|
|
if(m_pItemTypes[Type].m_First == -1)
|
|
|
|
m_pItemTypes[Type].m_First = m_NumItems;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2011-07-30 23:40:28 +00:00
|
|
|
m_pItemTypes[Type].m_Num++;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
m_NumItems++;
|
|
|
|
return m_NumItems-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CDataFileWriter::AddData(int Size, void *pData)
|
|
|
|
{
|
2010-09-27 20:35:57 +00:00
|
|
|
dbg_assert(m_NumDatas < 1024, "too much data");
|
|
|
|
|
2011-07-30 23:40:28 +00:00
|
|
|
CDataInfo *pInfo = &m_pDatas[m_NumDatas];
|
2010-05-29 07:25:38 +00:00
|
|
|
unsigned long s = compressBound(Size);
|
2018-04-09 09:56:39 +00:00
|
|
|
void *pCompData = malloc(s); // temporary buffer that we use during compression
|
2010-05-29 07:25:38 +00:00
|
|
|
|
|
|
|
int Result = compress((Bytef*)pCompData, &s, (Bytef*)pData, Size); // ignore_convention
|
|
|
|
if(Result != Z_OK)
|
|
|
|
{
|
|
|
|
dbg_msg("datafile", "compression error %d", Result);
|
|
|
|
dbg_assert(0, "zlib error");
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
pInfo->m_UncompressedSize = Size;
|
|
|
|
pInfo->m_CompressedSize = (int)s;
|
2018-04-09 09:56:39 +00:00
|
|
|
pInfo->m_pCompressedData = malloc(pInfo->m_CompressedSize);
|
2010-05-29 07:25:38 +00:00
|
|
|
mem_copy(pInfo->m_pCompressedData, pCompData, pInfo->m_CompressedSize);
|
2018-04-09 09:56:39 +00:00
|
|
|
free(pCompData);
|
2010-05-29 07:25:38 +00:00
|
|
|
|
|
|
|
m_NumDatas++;
|
|
|
|
return m_NumDatas-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CDataFileWriter::AddDataSwapped(int Size, void *pData)
|
|
|
|
{
|
2010-09-27 20:35:57 +00:00
|
|
|
dbg_assert(Size%sizeof(int) == 0, "incorrect boundary");
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
#if defined(CONF_ARCH_ENDIAN_BIG)
|
2018-04-09 09:56:39 +00:00
|
|
|
void *pSwapped = malloc(Size); // temporary buffer that we use during compression
|
2010-05-29 07:25:38 +00:00
|
|
|
mem_copy(pSwapped, pData, Size);
|
2010-09-27 20:35:57 +00:00
|
|
|
swap_endian(pSwapped, sizeof(int), Size/sizeof(int));
|
|
|
|
int Index = AddData(Size, pSwapped);
|
2018-04-09 09:56:39 +00:00
|
|
|
free(pSwapped);
|
2010-05-29 07:25:38 +00:00
|
|
|
return Index;
|
|
|
|
#else
|
|
|
|
return AddData(Size, pData);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int CDataFileWriter::Finish()
|
|
|
|
{
|
2010-09-27 20:35:57 +00:00
|
|
|
if(!m_File) return 1;
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
int ItemSize = 0;
|
|
|
|
int TypesSize, HeaderSize, OffsetSize, FileSize, SwapSize;
|
|
|
|
int DataSize = 0;
|
|
|
|
CDatafileHeader Header;
|
|
|
|
|
|
|
|
// we should now write this file!
|
|
|
|
if(DEBUG)
|
|
|
|
dbg_msg("datafile", "writing");
|
|
|
|
|
|
|
|
// calculate sizes
|
|
|
|
for(int i = 0; i < m_NumItems; i++)
|
|
|
|
{
|
|
|
|
if(DEBUG)
|
2017-07-26 01:58:00 +00:00
|
|
|
dbg_msg("datafile", "item=%d size=%d (%d)", i, m_pItems[i].m_Size, m_pItems[i].m_Size + (int)sizeof(CDatafileItem));
|
2011-07-30 23:40:28 +00:00
|
|
|
ItemSize += m_pItems[i].m_Size + sizeof(CDatafileItem);
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
for(int i = 0; i < m_NumDatas; i++)
|
2011-07-30 23:40:28 +00:00
|
|
|
DataSize += m_pDatas[i].m_CompressedSize;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// calculate the complete size
|
|
|
|
TypesSize = m_NumItemTypes*sizeof(CDatafileItemType);
|
|
|
|
HeaderSize = sizeof(CDatafileHeader);
|
2010-09-27 20:35:57 +00:00
|
|
|
OffsetSize = (m_NumItems + m_NumDatas + m_NumDatas) * sizeof(int); // ItemOffsets, DataOffsets, DataUncompressedSizes
|
2010-05-29 07:25:38 +00:00
|
|
|
FileSize = HeaderSize + TypesSize + OffsetSize + ItemSize + DataSize;
|
|
|
|
SwapSize = FileSize - DataSize;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
(void)SwapSize;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(DEBUG)
|
|
|
|
dbg_msg("datafile", "num_m_aItemTypes=%d TypesSize=%d m_aItemsize=%d DataSize=%d", m_NumItemTypes, TypesSize, ItemSize, DataSize);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// construct Header
|
|
|
|
{
|
2011-02-12 10:40:36 +00:00
|
|
|
Header.m_aID[0] = 'D';
|
|
|
|
Header.m_aID[1] = 'A';
|
|
|
|
Header.m_aID[2] = 'T';
|
|
|
|
Header.m_aID[3] = 'A';
|
2010-05-29 07:25:38 +00:00
|
|
|
Header.m_Version = 4;
|
|
|
|
Header.m_Size = FileSize - 16;
|
|
|
|
Header.m_Swaplen = SwapSize - 16;
|
|
|
|
Header.m_NumItemTypes = m_NumItemTypes;
|
|
|
|
Header.m_NumItems = m_NumItems;
|
|
|
|
Header.m_NumRawData = m_NumDatas;
|
|
|
|
Header.m_ItemSize = ItemSize;
|
|
|
|
Header.m_DataSize = DataSize;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// write Header
|
|
|
|
if(DEBUG)
|
2017-07-26 01:58:00 +00:00
|
|
|
dbg_msg("datafile", "HeaderSize=%d", (int)sizeof(Header));
|
2010-09-27 20:35:57 +00:00
|
|
|
#if defined(CONF_ARCH_ENDIAN_BIG)
|
|
|
|
swap_endian(&Header, sizeof(int), sizeof(Header)/sizeof(int));
|
|
|
|
#endif
|
2010-05-29 07:25:38 +00:00
|
|
|
io_write(m_File, &Header, sizeof(Header));
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// write types
|
2018-10-05 09:20:30 +00:00
|
|
|
for(int i = 0, Count = 0; i < MAX_ITEM_TYPES; i++)
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
2011-07-30 23:40:28 +00:00
|
|
|
if(m_pItemTypes[i].m_Num)
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
|
|
|
// write info
|
|
|
|
CDatafileItemType Info;
|
|
|
|
Info.m_Type = i;
|
|
|
|
Info.m_Start = Count;
|
2011-07-30 23:40:28 +00:00
|
|
|
Info.m_Num = m_pItemTypes[i].m_Num;
|
2010-05-29 07:25:38 +00:00
|
|
|
if(DEBUG)
|
|
|
|
dbg_msg("datafile", "writing type=%x start=%d num=%d", Info.m_Type, Info.m_Start, Info.m_Num);
|
2010-09-27 20:35:57 +00:00
|
|
|
#if defined(CONF_ARCH_ENDIAN_BIG)
|
|
|
|
swap_endian(&Info, sizeof(int), sizeof(CDatafileItemType)/sizeof(int));
|
|
|
|
#endif
|
2010-05-29 07:25:38 +00:00
|
|
|
io_write(m_File, &Info, sizeof(Info));
|
2011-07-30 23:40:28 +00:00
|
|
|
Count += m_pItemTypes[i].m_Num;
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// write item offsets
|
2018-10-05 09:20:30 +00:00
|
|
|
for(int i = 0, Offset = 0; i < MAX_ITEM_TYPES; i++)
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
2011-07-30 23:40:28 +00:00
|
|
|
if(m_pItemTypes[i].m_Num)
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
2011-07-30 23:40:28 +00:00
|
|
|
// write all m_pItems in of this type
|
|
|
|
int k = m_pItemTypes[i].m_First;
|
2010-05-29 07:25:38 +00:00
|
|
|
while(k != -1)
|
|
|
|
{
|
|
|
|
if(DEBUG)
|
|
|
|
dbg_msg("datafile", "writing item offset num=%d offset=%d", k, Offset);
|
2010-09-27 20:35:57 +00:00
|
|
|
int Temp = Offset;
|
|
|
|
#if defined(CONF_ARCH_ENDIAN_BIG)
|
|
|
|
swap_endian(&Temp, sizeof(int), sizeof(Temp)/sizeof(int));
|
|
|
|
#endif
|
|
|
|
io_write(m_File, &Temp, sizeof(Temp));
|
2011-07-30 23:40:28 +00:00
|
|
|
Offset += m_pItems[k].m_Size + sizeof(CDatafileItem);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// next
|
2011-07-30 23:40:28 +00:00
|
|
|
k = m_pItems[k].m_Next;
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// write data offsets
|
|
|
|
for(int i = 0, Offset = 0; i < m_NumDatas; i++)
|
|
|
|
{
|
|
|
|
if(DEBUG)
|
|
|
|
dbg_msg("datafile", "writing data offset num=%d offset=%d", i, Offset);
|
2010-09-27 20:35:57 +00:00
|
|
|
int Temp = Offset;
|
|
|
|
#if defined(CONF_ARCH_ENDIAN_BIG)
|
|
|
|
swap_endian(&Temp, sizeof(int), sizeof(Temp)/sizeof(int));
|
|
|
|
#endif
|
|
|
|
io_write(m_File, &Temp, sizeof(Temp));
|
2011-07-30 23:40:28 +00:00
|
|
|
Offset += m_pDatas[i].m_CompressedSize;
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// write data uncompressed sizes
|
|
|
|
for(int i = 0; i < m_NumDatas; i++)
|
|
|
|
{
|
|
|
|
if(DEBUG)
|
2011-07-30 23:40:28 +00:00
|
|
|
dbg_msg("datafile", "writing data uncompressed size num=%d size=%d", i, m_pDatas[i].m_UncompressedSize);
|
|
|
|
int UncompressedSize = m_pDatas[i].m_UncompressedSize;
|
2010-09-27 20:35:57 +00:00
|
|
|
#if defined(CONF_ARCH_ENDIAN_BIG)
|
|
|
|
swap_endian(&UncompressedSize, sizeof(int), sizeof(UncompressedSize)/sizeof(int));
|
|
|
|
#endif
|
|
|
|
io_write(m_File, &UncompressedSize, sizeof(UncompressedSize));
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2011-07-30 23:40:28 +00:00
|
|
|
// write m_pItems
|
2018-10-05 09:20:30 +00:00
|
|
|
for(int i = 0; i < MAX_ITEM_TYPES; i++)
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
2011-07-30 23:40:28 +00:00
|
|
|
if(m_pItemTypes[i].m_Num)
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
2011-07-30 23:40:28 +00:00
|
|
|
// write all m_pItems in of this type
|
|
|
|
int k = m_pItemTypes[i].m_First;
|
2010-05-29 07:25:38 +00:00
|
|
|
while(k != -1)
|
|
|
|
{
|
|
|
|
CDatafileItem Item;
|
2011-07-30 23:40:28 +00:00
|
|
|
Item.m_TypeAndID = (i<<16)|m_pItems[k].m_ID;
|
|
|
|
Item.m_Size = m_pItems[k].m_Size;
|
2010-05-29 07:25:38 +00:00
|
|
|
if(DEBUG)
|
2011-07-30 23:40:28 +00:00
|
|
|
dbg_msg("datafile", "writing item type=%x idx=%d id=%d size=%d", i, k, m_pItems[k].m_ID, m_pItems[k].m_Size);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-09-27 20:35:57 +00:00
|
|
|
#if defined(CONF_ARCH_ENDIAN_BIG)
|
|
|
|
swap_endian(&Item, sizeof(int), sizeof(Item)/sizeof(int));
|
2011-07-30 23:40:28 +00:00
|
|
|
swap_endian(m_pItems[k].m_pData, sizeof(int), m_pItems[k].m_Size/sizeof(int));
|
2010-09-27 20:35:57 +00:00
|
|
|
#endif
|
2010-05-29 07:25:38 +00:00
|
|
|
io_write(m_File, &Item, sizeof(Item));
|
2011-07-30 23:40:28 +00:00
|
|
|
io_write(m_File, m_pItems[k].m_pData, m_pItems[k].m_Size);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// next
|
2011-07-30 23:40:28 +00:00
|
|
|
k = m_pItems[k].m_Next;
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// write data
|
|
|
|
for(int i = 0; i < m_NumDatas; i++)
|
|
|
|
{
|
|
|
|
if(DEBUG)
|
2011-07-30 23:40:28 +00:00
|
|
|
dbg_msg("datafile", "writing data id=%d size=%d", i, m_pDatas[i].m_CompressedSize);
|
|
|
|
io_write(m_File, m_pDatas[i].m_pCompressedData, m_pDatas[i].m_CompressedSize);
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// free data
|
|
|
|
for(int i = 0; i < m_NumItems; i++)
|
2018-04-09 09:56:39 +00:00
|
|
|
free(m_pItems[i].m_pData);
|
2010-09-27 20:35:57 +00:00
|
|
|
for(int i = 0; i < m_NumDatas; ++i)
|
2018-04-09 09:56:39 +00:00
|
|
|
free(m_pDatas[i].m_pCompressedData);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
io_close(m_File);
|
2010-09-27 20:35:57 +00:00
|
|
|
m_File = 0;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(DEBUG)
|
|
|
|
dbg_msg("datafile", "done");
|
|
|
|
return 0;
|
|
|
|
}
|