190 lines
5 KiB
C++
190 lines
5 KiB
C++
|
// MassBuilderSaveTool
|
||
|
// Copyright (C) 2021 Guillaume Jacquemin
|
||
|
//
|
||
|
// 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 <Corrade/Containers/ArrayView.h>
|
||
|
#include <Corrade/Containers/Optional.h>
|
||
|
#include <Corrade/Utility/Directory.h>
|
||
|
|
||
|
#include "BinaryReader.h"
|
||
|
#include "BinaryWriter.h"
|
||
|
|
||
|
#include "UESaveFile.h"
|
||
|
|
||
|
UESaveFile::UESaveFile(std::string filepath)
|
||
|
{
|
||
|
_filepath = std::move(filepath);
|
||
|
|
||
|
loadData();
|
||
|
}
|
||
|
|
||
|
auto UESaveFile::valid() const -> bool {
|
||
|
return _valid;
|
||
|
}
|
||
|
|
||
|
auto UESaveFile::lastError() const -> const std::string& {
|
||
|
return _lastError;
|
||
|
}
|
||
|
|
||
|
auto UESaveFile::reloadData() -> bool {
|
||
|
loadData();
|
||
|
return valid();
|
||
|
}
|
||
|
|
||
|
auto UESaveFile::at(const std::string& name) -> UnrealPropertyBase* {
|
||
|
for(auto& prop : _properties) {
|
||
|
if(*(prop->name) == name) {
|
||
|
return prop.get();
|
||
|
}
|
||
|
}
|
||
|
return nullptr;
|
||
|
}
|
||
|
|
||
|
auto UESaveFile::props() -> Containers::ArrayView<UnrealPropertyBase::ptr> {
|
||
|
return _properties;
|
||
|
}
|
||
|
|
||
|
auto UESaveFile::saveToFile() -> bool {
|
||
|
BinaryWriter writer{_filepath + ".other"};
|
||
|
|
||
|
if(!writer.open()) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
if(!writer.writeStaticArray(staticArrayView(_magicBytes)) ||
|
||
|
!writer.writeUnsignedInt(_saveVersion) ||
|
||
|
!writer.writeUnsignedInt(_packageVersion) ||
|
||
|
!writer.writeUnsignedShort(_engineVersion.major) ||
|
||
|
!writer.writeUnsignedShort(_engineVersion.minor) ||
|
||
|
!writer.writeUnsignedShort(_engineVersion.patch) ||
|
||
|
!writer.writeUnsignedInt(_engineVersion.build) ||
|
||
|
!writer.writeUEString(_engineVersion.buildId))
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
if(!writer.writeUnsignedInt(_customFormatVersion) ||
|
||
|
!writer.writeUnsignedInt(_customFormatData.size()))
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
for(UnsignedLong i = 0; i < _customFormatData.size(); i++) {
|
||
|
if(!writer.writeStaticArray(Containers::StaticArrayView<16, const char>{_customFormatData[i].id}) ||
|
||
|
!writer.writeUnsignedInt(_customFormatData[i].value))
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if(!writer.writeUEString(_saveType)) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
for(auto& prop : _properties) {
|
||
|
UnsignedLong bytes_written = 0;
|
||
|
if(!_propSerialiser.write(prop, bytes_written, writer)) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
if(!writer.flushToFile()) {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
writer.writeUnsignedInt(0);
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
void UESaveFile::loadData() {
|
||
|
_valid = false;
|
||
|
|
||
|
if(!Utility::Directory::exists(_filepath)) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
BinaryReader reader{_filepath};
|
||
|
|
||
|
if(!reader.open()) {
|
||
|
_lastError = _filepath + " couldn't be opened.";
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
Containers::Array<char> magic;
|
||
|
if(!reader.readArray(magic, 4)) {
|
||
|
_lastError = "Couldn't read magic bytes in " + _filepath;
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
std::string invalid = _filepath + " isn't a valid UE4 save.";
|
||
|
|
||
|
if(std::strncmp(magic.data(), _magicBytes.data(), 4) != 0) {
|
||
|
_lastError = std::move(invalid);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if(!reader.readUnsignedInt(_saveVersion) ||
|
||
|
!reader.readUnsignedInt(_packageVersion) ||
|
||
|
!reader.readUnsignedShort(_engineVersion.major) ||
|
||
|
!reader.readUnsignedShort(_engineVersion.minor) ||
|
||
|
!reader.readUnsignedShort(_engineVersion.patch) ||
|
||
|
!reader.readUnsignedInt(_engineVersion.build) ||
|
||
|
!reader.readUEString(_engineVersion.buildId))
|
||
|
{
|
||
|
_lastError = std::move(invalid);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if(!reader.readUnsignedInt(_customFormatVersion)) {
|
||
|
_lastError = std::move(invalid);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
UnsignedInt custom_format_data_size = 0;
|
||
|
|
||
|
if(!reader.readUnsignedInt(custom_format_data_size)) {
|
||
|
_lastError = std::move(invalid);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
arrayReserve(_customFormatData, custom_format_data_size);
|
||
|
|
||
|
for(UnsignedInt i = 0; i < custom_format_data_size; i++) {
|
||
|
CustomFormatDataEntry entry;
|
||
|
|
||
|
if(!reader.readStaticArray(entry.id) ||
|
||
|
!reader.readInt(entry.value))
|
||
|
{
|
||
|
_lastError = std::move(invalid);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
arrayAppend(_customFormatData, entry);
|
||
|
}
|
||
|
|
||
|
if(!reader.readUEString(_saveType)) {
|
||
|
_lastError = std::move(invalid);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
UnrealPropertyBase::ptr prop;
|
||
|
while((prop = _propSerialiser.read(reader)) != nullptr) {
|
||
|
arrayAppend(_properties, std::move(prop));
|
||
|
}
|
||
|
|
||
|
_valid = true;
|
||
|
}
|