2014-06-18 04:51:18 +00:00
|
|
|
#ifndef ATHENA_NO_ZQUEST
|
2014-04-20 09:14:15 +00:00
|
|
|
// This file is part of libAthena.
|
2013-07-21 07:49:07 +00:00
|
|
|
//
|
2014-04-20 09:14:15 +00:00
|
|
|
// libAthena is free software: you can redistribute it and/or modify
|
2013-07-21 07:49:07 +00:00
|
|
|
// 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,
|
2013-07-21 07:49:07 +00:00
|
|
|
// 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/>
|
2013-07-21 07:49:07 +00:00
|
|
|
|
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
|
|
|
{
|
2013-07-21 07:49:07 +00:00
|
|
|
namespace io
|
|
|
|
{
|
2013-07-21 03:51:26 +00:00
|
|
|
|
2014-06-18 04:51:18 +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)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-07-22 03:06:54 +00:00
|
|
|
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);
|
2014-06-18 04:51:18 +00:00
|
|
|
atUint8* questData = quest->data();
|
|
|
|
atUint32 compLen;
|
2015-05-19 03:24:56 +00:00
|
|
|
|
2013-07-21 03:51:26 +00:00
|
|
|
if (compress)
|
|
|
|
{
|
2014-06-18 04:51:18 +00:00
|
|
|
atUint8* compData = new atUint8[quest->length() + 0x40]; // add 20 bytes because sometimes the file grows with compression
|
2013-07-22 03:06:54 +00:00
|
|
|
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
|
2013-07-22 03:06:54 +00:00
|
|
|
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
|
2013-07-22 03:06:54 +00:00
|
|
|
{
|
|
|
|
compLen = quest->length();
|
2014-04-20 09:14:15 +00:00
|
|
|
base::writeUint32(quest->length());
|
2013-07-22 03:06:54 +00:00
|
|
|
}
|
2013-07-21 03:51:26 +00:00
|
|
|
|
2014-04-20 09:14:15 +00:00
|
|
|
base::writeUint32(quest->length());
|
2014-06-18 04:51:18 +00:00
|
|
|
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);
|
2013-07-21 07:49:07 +00:00
|
|
|
|
|
|
|
base::save();
|
2015-05-19 03:24:56 +00:00
|
|
|
|
2013-07-28 01:42:11 +00:00
|
|
|
// Delete compressed data to prevent memory leaks
|
2013-07-22 03:06:54 +00:00
|
|
|
if (questData != quest->data())
|
|
|
|
{
|
|
|
|
delete[] questData;
|
|
|
|
questData = NULL;
|
|
|
|
}
|
2013-07-21 03:51:26 +00:00
|
|
|
}
|
|
|
|
|
2013-07-21 07:49:07 +00:00
|
|
|
} // io
|
2013-07-21 03:51:26 +00:00
|
|
|
} // zelda
|
2014-06-18 04:51:18 +00:00
|
|
|
|
|
|
|
#endif // ATHENA_NO_ZQUEST
|