1
0
forked from 0ad/0ad
0ad/source/ps/FilePacker.cpp
Ykkrosh 22dd4dd67b Entities: Removed Tag attribute; it is taken from the filename instead. Made entity XML files be loaded on demand. Probably stopped crash when maps contain non-existent entities. Fixed a few bugs in entity definitions.
Maps: Stored non-entity objects in XML instead of PMP, for easier manual
editing. Updated existing maps to newest format, so that they can still
work. Added undocumented _rewriteMaps() JS function. Also renamed _mem
to vmem, and reclassified its undocumentedness as unintentional, since
it's reasonably useful.
Loader: added NonprogressiveLoad function, for ScEd/_rewriteMaps/etc
which don't care about progressiveness.
main.cpp: re-enabled vfs_display, since it doesn't crash now
Vector3D: stopped warning

This was SVN commit r2078.
2005-03-29 20:50:04 +00:00

82 lines
2.2 KiB
C++
Executable File

///////////////////////////////////////////////////////////////////////////////
//
// Name: FilePacker.cpp
// Author: Rich Cross
// Contact: rich@wildfiregames.com
//
///////////////////////////////////////////////////////////////////////////////
#include "precompiled.h"
#include "FilePacker.h"
#ifdef SCED
# include <stdio.h>
#endif
#include <string.h>
#include "lib/res/vfs.h"
////////////////////////////////////////////////////////////////////////////////////////
// CFilePacker constructor
// rationale for passing in version + signature here: see header
CFilePacker::CFilePacker(u32 version, const char magicstr[4])
{
// put header in our data array.
// (size will be updated on every Pack*() call)
m_Data.resize(12);
u8* header = (u8*)&m_Data[0];
strncpy((char*)(header+0), magicstr, 4); // not 0-terminated => no _s
*(u32*)(header+4) = version;
*(u32*)(header+8) = 0; // datasize
// FIXME m_Version: Byte order? -- Simon
}
////////////////////////////////////////////////////////////////////////////////////////
// Write: write out to file all packed data added so far
void CFilePacker::Write(const char* filename)
{
#ifndef SCED
// write out all data (including header)
if(vfs_store(filename, &m_Data[0], m_Data.size(), FILE_NO_AIO) < 0)
throw CFileWriteError();
#else
FILE* fp=fopen(filename,"wb");
if (!fp) {
throw CFileOpenError();
}
// write out one big chunk of data (includes header)
if (fwrite(&m_Data[0],m_Data.size(),1,fp)!=1) {
fclose(fp);
throw CFileWriteError();
}
// all done
fclose(fp);
#endif
}
////////////////////////////////////////////////////////////////////////////////////////
// PackRaw: pack given number of bytes onto the end of the data stream
void CFilePacker::PackRaw(const void* rawdata,u32 rawdatalen)
{
u32 start=(u32)m_Data.size();
m_Data.resize(m_Data.size()+rawdatalen);
memcpy(&m_Data[start],rawdata,rawdatalen);
*(u32*)&m_Data[8] += rawdatalen; // FIXME byte order?
}
////////////////////////////////////////////////////////////////////////////////////////
// PackString: pack a string onto the end of the data stream
void CFilePacker::PackString(const CStr& str)
{
u32 len=(u32)str.Length();
PackRaw(&len,sizeof(len));
PackRaw((const char*) str,len);
}