athena/src/Athena/ZQuestFileWriter.cpp

96 lines
2.8 KiB
C++
Raw Normal View History

#ifndef ATHENA_NO_ZQUEST
2014-04-20 09:14:15 +00:00
// This file is part of libAthena.
//
2014-04-20 09:14:15 +00:00
// libAthena is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
2014-04-20 09:14:15 +00:00
// libAthena is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
2014-04-20 09:14:15 +00:00
// along with libAthena. If not, see <http://www.gnu.org/licenses/>
2014-04-20 09:14:15 +00:00
#include "Athena/ZQuestFileWriter.hpp"
#include "Athena/InvalidOperationException.hpp"
#include "Athena/ZQuestFile.hpp"
#include "Athena/Compression.hpp"
#include "Athena/Checksums.hpp"
2013-07-21 03:51:26 +00:00
2014-04-20 09:14:15 +00:00
namespace Athena
2013-07-21 03:51:26 +00:00
{
namespace io
{
2013-07-21 03:51:26 +00:00
ZQuestFileWriter::ZQuestFileWriter(atUint8* data, atUint64 length)
2013-07-21 03:51:26 +00:00
: base(data, length)
{
}
ZQuestFileWriter::ZQuestFileWriter(const std::string& filename)
: base(filename)
{
}
void ZQuestFileWriter::write(ZQuestFile* quest, bool compress)
2013-07-21 03:51:26 +00:00
{
if (!quest)
2014-04-20 09:14:15 +00:00
THROW_INVALID_OPERATION_EXCEPTION("quest cannot be NULL");
2013-07-21 03:51:26 +00:00
2014-04-20 09:14:15 +00:00
base::writeUint32(ZQuestFile::Magic);
base::writeUint32(ZQuestFile::Version);
atUint8* questData = quest->data();
atUint32 compLen;
2015-05-19 03:24:56 +00:00
2013-07-21 03:51:26 +00:00
if (compress)
{
atUint8* compData = new atUint8[quest->length() + 0x40]; // add 20 bytes because sometimes the file grows with compression
compLen = quest->length() + 0x40;
compLen = io::Compression::compressZlib(questData, quest->length(), compData, compLen);
2013-07-21 03:51:26 +00:00
// if the compressed data is the same length or larger than the original data, just store the original
if (compLen >= quest->length() || compLen <= 0)
2013-07-21 03:51:26 +00:00
{
compLen = quest->length();
// Delete the compressed data since we won't be using it
delete[] compData;
compData = NULL;
2014-04-20 09:14:15 +00:00
base::writeUint32(quest->length());
2013-07-21 03:51:26 +00:00
}
else
{
// Don't do delete on data
questData = compData;
2014-04-20 09:14:15 +00:00
base::writeUint32(compLen);
2013-07-21 03:51:26 +00:00
}
}
else
{
compLen = quest->length();
2014-04-20 09:14:15 +00:00
base::writeUint32(quest->length());
}
2013-07-21 03:51:26 +00:00
2014-04-20 09:14:15 +00:00
base::writeUint32(quest->length());
base::writeBytes((atInt8*)quest->gameString().substr(0, 0x0A).c_str(), 0x0A);
2014-04-20 09:14:15 +00:00
base::writeUint16(quest->endian() == Endian::BigEndian ? 0xFFFE : 0xFEFF);
base::writeUint32(Athena::Checksums::crc32(questData, compLen));
2013-07-21 03:51:26 +00:00
base::writeUBytes(questData, compLen);
base::save();
2015-05-19 03:24:56 +00:00
// Delete compressed data to prevent memory leaks
if (questData != quest->data())
{
delete[] questData;
questData = NULL;
}
2013-07-21 03:51:26 +00:00
}
} // io
2013-07-21 03:51:26 +00:00
} // zelda
#endif // ATHENA_NO_ZQUEST