mirror of
https://github.com/scp-fs2open/fs2open.github.com.git
synced 2026-11-10 12:41:35 -10:00
This is a new implementation of the JSON dumping which was already proposed in #22 but this version tries to allow implementing both reading and writing JSON files without having to change the existing code too much. This is done by using an abstract class to hide the differences between writing the JSON and binary files. This is currently only capable of writing the files but not reading them so if the option is enabled all changes to the pilot file are lost. I will implement reading after this PR has been merged since I would like to reduce the amount of changes that need to be reviewed.
90 lines
2.4 KiB
C++
90 lines
2.4 KiB
C++
|
|
#include "pilotfile/BinaryFileHandler.h"
|
|
|
|
pilot::BinaryFileHandler::BinaryFileHandler(CFILE* cfp) : _cfp(cfp) {
|
|
Assertion(cfp != nullptr, "File pointer must be valid!");
|
|
}
|
|
|
|
pilot::BinaryFileHandler::~BinaryFileHandler() {
|
|
cfclose(_cfp);
|
|
_cfp = nullptr;
|
|
}
|
|
|
|
void pilot::BinaryFileHandler::writeUByte(const char*, std::uint8_t value) {
|
|
cfwrite_ubyte(value, _cfp);
|
|
}
|
|
void pilot::BinaryFileHandler::writeShort(const char*, std::int16_t value) {
|
|
cfwrite_short(value, _cfp);
|
|
}
|
|
|
|
void pilot::BinaryFileHandler::writeInt(const char*, std::int32_t value) {
|
|
cfwrite_int(value, _cfp);
|
|
}
|
|
|
|
void pilot::BinaryFileHandler::writeUInt(const char*, std::uint32_t value) {
|
|
cfwrite_uint(value, _cfp);
|
|
}
|
|
|
|
void pilot::BinaryFileHandler::writeFloat(const char*, float value) {
|
|
cfwrite_float(value, _cfp);
|
|
}
|
|
|
|
void pilot::BinaryFileHandler::writeString(const char*, const char* str) {
|
|
cfwrite_string_len(str, _cfp);
|
|
}
|
|
|
|
void pilot::BinaryFileHandler::startSectionWrite(Section id) {
|
|
if (id == Section::Unnamed) {
|
|
_sectionOffsets.push_back({id, 0});
|
|
return;
|
|
}
|
|
|
|
cfwrite_ushort((ushort) id, _cfp);
|
|
|
|
// to be updated when endSection() is called
|
|
cfwrite_int(0, _cfp);
|
|
|
|
// starting offset, for size of section
|
|
_sectionOffsets.push_back({id, (size_t)cftell(_cfp)});
|
|
}
|
|
|
|
void pilot::BinaryFileHandler::endSectionWrite() {
|
|
Assertion(!_sectionOffsets.empty(), "No active section!");
|
|
auto previous_off = _sectionOffsets.back();
|
|
|
|
if (previous_off.id == Section::Unnamed) {
|
|
// ignore unnamed sections, these are only needed for JSON
|
|
return;
|
|
}
|
|
|
|
|
|
size_t cur = (size_t) cftell(_cfp);
|
|
_sectionOffsets.pop_back();
|
|
|
|
Assert(cur >= previous_off.offset);
|
|
|
|
size_t section_size = cur - previous_off.offset;
|
|
|
|
if (section_size) {
|
|
// go back to section size in file and write proper value
|
|
cfseek(_cfp, (int) (cur - section_size - sizeof(int)), CF_SEEK_SET);
|
|
cfwrite_int((int) section_size, _cfp);
|
|
|
|
// go back to previous location for next section
|
|
cfseek(_cfp, (int) cur, CF_SEEK_SET);
|
|
}
|
|
}
|
|
void pilot::BinaryFileHandler::startArrayWrite(const char*, size_t size, bool short_index) {
|
|
if (short_index) {
|
|
cfwrite_ushort((short)size, _cfp);
|
|
} else {
|
|
cfwrite_int((int)size, _cfp);
|
|
}
|
|
}
|
|
void pilot::BinaryFileHandler::endArrayWrite() {
|
|
// Nothing to do here for the binary version
|
|
}
|
|
void pilot::BinaryFileHandler::flush() {
|
|
cflush(_cfp);
|
|
}
|