MassBuilderSaveTool/src/UESaveFile/Serialisers/SetPropertySerialiser.cpp

83 lines
3 KiB
C++
Raw Normal View History

2021-09-22 17:37:50 +02:00
// MassBuilderSaveTool
// Copyright (C) 2021-2023 Guillaume Jacquemin
2021-09-22 17:37:50 +02:00
//
// This program 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.
//
// This program 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
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "../BinaryReader.h"
#include "../BinaryWriter.h"
#include "../PropertySerialiser.h"
2022-11-24 09:12:41 +01:00
#include "../../Logger/Logger.h"
2021-09-22 17:37:50 +02:00
#include "SetPropertySerialiser.h"
2022-12-03 16:49:39 +01:00
UnrealPropertyBase::ptr
SetPropertySerialiser::deserialiseProperty(Containers::StringView name, Containers::StringView type,
std::size_t value_length, BinaryReader& reader,
2022-12-03 16:49:39 +01:00
PropertySerialiser& serialiser)
2021-09-22 17:37:50 +02:00
{
Containers::String item_type;
2021-09-22 17:37:50 +02:00
if(!reader.readUEString(item_type)) {
2022-11-24 09:12:41 +01:00
LOG_ERROR_FORMAT("Couldn't read set property {}'s item type.", name);
2021-09-22 17:37:50 +02:00
return nullptr;
}
char terminator;
if(!reader.readChar(terminator) || terminator != '\0') {
2022-11-24 09:12:41 +01:00
LOG_ERROR_FORMAT("Couldn't read a null byte in set property {}.", name);
2021-09-22 17:37:50 +02:00
return nullptr;
}
std::uint32_t four_bytes;
if(!reader.readUint32(four_bytes) || four_bytes != 0u) {
2022-11-24 09:12:41 +01:00
LOG_ERROR_FORMAT("Couldn't read four null bytes in set property {}.", name);
2021-09-22 17:37:50 +02:00
return nullptr;
}
std::uint32_t item_count;
if(!reader.readUint32(item_count)) {
2022-11-24 09:12:41 +01:00
LOG_ERROR_FORMAT("Couldn't read set property {}'s item count.", name);
2021-09-22 17:37:50 +02:00
return nullptr;
}
auto prop = Containers::pointer<SetProperty>();
prop->itemType = std::move(item_type);
prop->items = serialiser.readSet(reader, prop->itemType, item_count);
return prop;
}
2022-12-03 16:49:39 +01:00
bool
SetPropertySerialiser::serialiseProperty(UnrealPropertyBase::ptr& prop, std::size_t& bytes_written,
2022-12-03 16:49:39 +01:00
BinaryWriter& writer, PropertySerialiser& serialiser)
2021-09-22 17:37:50 +02:00
{
auto set_prop = dynamic_cast<SetProperty*>(prop.get());
if(!set_prop) {
2022-11-24 09:12:41 +01:00
LOG_ERROR("The property is not a valid set property.");
2021-09-22 17:37:50 +02:00
return false;
}
writer.writeUEStringToArray(set_prop->itemType);
writer.writeValueToArray<char>('\0');
bytes_written += writer.writeValueToArray<std::uint32_t>(0u);
bytes_written += writer.writeValueToArray<std::uint32_t>(std::uint32_t(set_prop->items.size()));
2021-09-22 17:37:50 +02:00
std::size_t start_pos = writer.arrayPosition();
std::size_t dummy_bytes_written = 0;
2021-09-22 17:37:50 +02:00
serialiser.writeSet(set_prop->items, set_prop->itemType, dummy_bytes_written, writer);
bytes_written += writer.arrayPosition() - start_pos;
return true;
}