From 393ec4a372bdbe0e2c1cf128199e96aa5ad49dfb Mon Sep 17 00:00:00 2001 From: William JCM Date: Tue, 22 Nov 2022 11:03:45 +0100 Subject: [PATCH] UESaveFile: use Logger. --- src/UESaveFile/BinaryReader.cpp | 5 ++- src/UESaveFile/BinaryWriter.cpp | 7 ++-- src/UESaveFile/PropertySerialiser.cpp | 4 +- src/UESaveFile/UESaveFile.cpp | 59 ++++++++++++++++++++------- 4 files changed, 54 insertions(+), 21 deletions(-) diff --git a/src/UESaveFile/BinaryReader.cpp b/src/UESaveFile/BinaryReader.cpp index 0f0599e..f1d88a5 100644 --- a/src/UESaveFile/BinaryReader.cpp +++ b/src/UESaveFile/BinaryReader.cpp @@ -19,14 +19,15 @@ #include #include +#include "../Logger/Logger.h" + #include "BinaryReader.h" BinaryReader::BinaryReader(Containers::StringView filename) { _file = std::fopen(filename.data(), "rb"); if(!_file) { - Utility::Error{} << "Couldn't open" << filename << "for reading:\n" - << std::strerror(errno); + LOG_ERROR_FORMAT("Couldn't open {} for reading: {}", filename, std::strerror(errno)); } } diff --git a/src/UESaveFile/BinaryWriter.cpp b/src/UESaveFile/BinaryWriter.cpp index 1d1c482..10fa76d 100644 --- a/src/UESaveFile/BinaryWriter.cpp +++ b/src/UESaveFile/BinaryWriter.cpp @@ -16,6 +16,8 @@ #include +#include "../Logger/Logger.h" + #include "BinaryWriter.h" using namespace Containers::Literals; @@ -23,8 +25,7 @@ using namespace Containers::Literals; BinaryWriter::BinaryWriter(Containers::StringView filename) { _file = std::fopen(filename.data(), "wb"); if(!_file) { - Utility::Error{} << "Couldn't open"_s << filename << "for reading:\n"_s - << std::strerror(errno); + LOG_ERROR_FORMAT("Couldn't open {} for reading: {}", filename, std::strerror(errno)); } } @@ -116,7 +117,7 @@ auto BinaryWriter::writeArray(Containers::ArrayView array) -> bool { auto BinaryWriter::writeUEString(Containers::StringView str) -> bool { if(str.size() > UINT32_MAX) { - Utility::Error{} << "BinaryWriter::writeUEString(): string is too big."_s; + LOG_ERROR_FORMAT("String is too big. Expected size() < UINT32_MAX, got {} instead.", str.size()); return false; } diff --git a/src/UESaveFile/PropertySerialiser.cpp b/src/UESaveFile/PropertySerialiser.cpp index 81061e7..77e8e4a 100644 --- a/src/UESaveFile/PropertySerialiser.cpp +++ b/src/UESaveFile/PropertySerialiser.cpp @@ -40,6 +40,8 @@ #include "BinaryReader.h" #include "BinaryWriter.h" +#include "../Logger/Logger.h" + #include "PropertySerialiser.h" PropertySerialiser::PropertySerialiser() { @@ -164,7 +166,7 @@ auto PropertySerialiser::deserialise(Containers::String name, Containers::String prop = serialiser->deserialise(name, type, value_length, reader, *this); if(!prop) { - !Utility::Error{} << "No prop in" << __func__; + LOG_ERROR("No property."); return nullptr; } diff --git a/src/UESaveFile/UESaveFile.cpp b/src/UESaveFile/UESaveFile.cpp index 365f50f..44942ba 100644 --- a/src/UESaveFile/UESaveFile.cpp +++ b/src/UESaveFile/UESaveFile.cpp @@ -15,11 +15,11 @@ // along with this program. If not, see . #include -#include #include #include "BinaryReader.h" #include "BinaryWriter.h" +#include "../Logger/Logger.h" #include "UESaveFile.h" @@ -67,10 +67,13 @@ auto UESaveFile::props() -> Containers::ArrayView { } auto UESaveFile::saveToFile() -> bool { + LOG_INFO_FORMAT("Writing to {}.", _filepath); + bool temp_file = _filepath.hasSuffix(".tmp"); if(!temp_file && !Utility::Path::copy(_filepath, _filepath + ".bak"_s)) { _lastError = "Couldn't create a backup for " + _filepath; + LOG_ERROR(_lastError); return false; } @@ -78,6 +81,7 @@ auto UESaveFile::saveToFile() -> bool { if(!writer.open()) { _lastError = "Couldn't open the file for saving."_s; + LOG_ERROR(_lastError); return false; } @@ -91,13 +95,15 @@ auto UESaveFile::saveToFile() -> bool { !writer.writeUEString(_engineVersion.buildId)) { _lastError = "Couldn't write the header."_s; + LOG_ERROR(_lastError); return false; } if(!writer.writeUnsignedInt(_customFormatVersion) || !writer.writeUnsignedInt(_customFormatData.size())) { - _lastError = "Couldn't write the header."_s; + _lastError = "Couldn't write the custom format data."_s; + LOG_ERROR(_lastError); return false; } @@ -105,13 +111,15 @@ auto UESaveFile::saveToFile() -> bool { if(!writer.writeStaticArray(Containers::StaticArrayView<16, const char>{_customFormatData[i].id}) || !writer.writeUnsignedInt(_customFormatData[i].value)) { - _lastError = "Couldn't write the header."_s; + _lastError = "Couldn't write the custom format data."_s; + LOG_ERROR(_lastError); return false; } } if(!writer.writeUEString(_saveType)) { - _lastError = "Couldn't write the header."_s; + _lastError = "Couldn't write the save type."_s; + LOG_ERROR(_lastError); return false; } @@ -119,11 +127,13 @@ auto UESaveFile::saveToFile() -> bool { UnsignedLong bytes_written = 0; if(!_propSerialiser->write(prop, bytes_written, writer)) { _lastError = "Couldn't write the property "_s + *prop->name + " to the array."_s; + LOG_ERROR(_lastError); return false; } if(!writer.flushToFile()) { _lastError = "Couldn't write the property "_s + *prop->name + " to the file."_s; + LOG_ERROR(_lastError); return false; } } @@ -134,11 +144,11 @@ auto UESaveFile::saveToFile() -> bool { if(!Utility::Path::copy(_filepath + ".tmp"_s, _filepath)) { _lastError = "Couldn't save the file properly."; + LOG_ERROR(_lastError); return false; } - else { - Utility::Path::remove(_filepath + ".tmp"_s); - } + + Utility::Path::remove(_filepath + ".tmp"_s); _noReloadAfterSave = true; @@ -146,9 +156,13 @@ auto UESaveFile::saveToFile() -> bool { } void UESaveFile::loadData() { + LOG_INFO_FORMAT("Reading data from {}.", _filepath); + _valid = false; if(!Utility::Path::exists(_filepath)) { + _lastError = "The file couldn't be found."; + LOG_ERROR(_lastError); return; } @@ -156,19 +170,22 @@ void UESaveFile::loadData() { if(!reader.open()) { _lastError = _filepath + " couldn't be opened."_s; + LOG_ERROR(_lastError); return; } Containers::Array magic; if(!reader.readArray(magic, 4)) { _lastError = "Couldn't read magic bytes in "_s + _filepath; + LOG_ERROR(_lastError); return; } - Containers::String invalid = _filepath + " isn't a valid UE4 save."_s; - if(std::strncmp(magic.data(), _magicBytes.data(), 4) != 0) { - _lastError = std::move(invalid); + _lastError = Utility::format("Magic bytes don't match. Expected {{{}, {}, {}, {}}}, got {{{}, {}, {}, {}}} instead", + _magicBytes[0], _magicBytes[1], _magicBytes[2], _magicBytes[3], + magic[0], magic[1], magic[2], magic[3]); + LOG_ERROR(_lastError); return; } @@ -180,19 +197,22 @@ void UESaveFile::loadData() { !reader.readUnsignedInt(_engineVersion.build) || !reader.readUEString(_engineVersion.buildId)) { - _lastError = std::move(invalid); + _lastError = "Couldn't read version data."; + LOG_ERROR(_lastError); return; } if(!reader.readUnsignedInt(_customFormatVersion)) { - _lastError = std::move(invalid); + _lastError = "Couldn't read the custom format version."; + LOG_ERROR(_lastError); return; } UnsignedInt custom_format_data_size = 0; if(!reader.readUnsignedInt(custom_format_data_size)) { - _lastError = std::move(invalid); + _lastError = "Couldn't read the custom format data size."; + LOG_ERROR(_lastError); return; } @@ -204,7 +224,8 @@ void UESaveFile::loadData() { if(!reader.readStaticArray(entry.id) || !reader.readInt(entry.value)) { - _lastError = std::move(invalid); + _lastError = "Couldn't read the custom format data"; + LOG_ERROR(_lastError); return; } @@ -212,7 +233,8 @@ void UESaveFile::loadData() { } if(!reader.readUEString(_saveType)) { - _lastError = std::move(invalid); + _lastError = "Couldn't read the save type."; + LOG_ERROR(_lastError); return; } @@ -221,8 +243,15 @@ void UESaveFile::loadData() { arrayAppend(_properties, std::move(prop)); } + if(_properties.isEmpty()) { + _lastError = "No properties were found."_s; + LOG_ERROR(_lastError); + return; + } + if(_properties.back()->name != "None"_s && _properties.back()->propertyType != "NoneProperty"_s) { _lastError = "Couldn't find a final NoneProperty."_s; + LOG_ERROR(_lastError); return; }