UESaveFile: use Logger.

This commit is contained in:
Guillaume Jacquemin 2022-11-22 11:03:45 +01:00
parent 83e9169dae
commit 393ec4a372
4 changed files with 54 additions and 21 deletions

View file

@ -19,14 +19,15 @@
#include <Corrade/Containers/Array.h> #include <Corrade/Containers/Array.h>
#include <Corrade/Containers/String.h> #include <Corrade/Containers/String.h>
#include "../Logger/Logger.h"
#include "BinaryReader.h" #include "BinaryReader.h"
BinaryReader::BinaryReader(Containers::StringView filename) { BinaryReader::BinaryReader(Containers::StringView filename) {
_file = std::fopen(filename.data(), "rb"); _file = std::fopen(filename.data(), "rb");
if(!_file) { if(!_file) {
Utility::Error{} << "Couldn't open" << filename << "for reading:\n" LOG_ERROR_FORMAT("Couldn't open {} for reading: {}", filename, std::strerror(errno));
<< std::strerror(errno);
} }
} }

View file

@ -16,6 +16,8 @@
#include <cstring> #include <cstring>
#include "../Logger/Logger.h"
#include "BinaryWriter.h" #include "BinaryWriter.h"
using namespace Containers::Literals; using namespace Containers::Literals;
@ -23,8 +25,7 @@ using namespace Containers::Literals;
BinaryWriter::BinaryWriter(Containers::StringView filename) { BinaryWriter::BinaryWriter(Containers::StringView filename) {
_file = std::fopen(filename.data(), "wb"); _file = std::fopen(filename.data(), "wb");
if(!_file) { if(!_file) {
Utility::Error{} << "Couldn't open"_s << filename << "for reading:\n"_s LOG_ERROR_FORMAT("Couldn't open {} for reading: {}", filename, std::strerror(errno));
<< std::strerror(errno);
} }
} }
@ -116,7 +117,7 @@ auto BinaryWriter::writeArray(Containers::ArrayView<const char> array) -> bool {
auto BinaryWriter::writeUEString(Containers::StringView str) -> bool { auto BinaryWriter::writeUEString(Containers::StringView str) -> bool {
if(str.size() > UINT32_MAX) { 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; return false;
} }

View file

@ -40,6 +40,8 @@
#include "BinaryReader.h" #include "BinaryReader.h"
#include "BinaryWriter.h" #include "BinaryWriter.h"
#include "../Logger/Logger.h"
#include "PropertySerialiser.h" #include "PropertySerialiser.h"
PropertySerialiser::PropertySerialiser() { PropertySerialiser::PropertySerialiser() {
@ -164,7 +166,7 @@ auto PropertySerialiser::deserialise(Containers::String name, Containers::String
prop = serialiser->deserialise(name, type, value_length, reader, *this); prop = serialiser->deserialise(name, type, value_length, reader, *this);
if(!prop) { if(!prop) {
!Utility::Error{} << "No prop in" << __func__; LOG_ERROR("No property.");
return nullptr; return nullptr;
} }

View file

@ -15,11 +15,11 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <Corrade/Containers/Optional.h> #include <Corrade/Containers/Optional.h>
#include <Corrade/Utility/Format.h>
#include <Corrade/Utility/Path.h> #include <Corrade/Utility/Path.h>
#include "BinaryReader.h" #include "BinaryReader.h"
#include "BinaryWriter.h" #include "BinaryWriter.h"
#include "../Logger/Logger.h"
#include "UESaveFile.h" #include "UESaveFile.h"
@ -67,10 +67,13 @@ auto UESaveFile::props() -> Containers::ArrayView<UnrealPropertyBase::ptr> {
} }
auto UESaveFile::saveToFile() -> bool { auto UESaveFile::saveToFile() -> bool {
LOG_INFO_FORMAT("Writing to {}.", _filepath);
bool temp_file = _filepath.hasSuffix(".tmp"); bool temp_file = _filepath.hasSuffix(".tmp");
if(!temp_file && !Utility::Path::copy(_filepath, _filepath + ".bak"_s)) { if(!temp_file && !Utility::Path::copy(_filepath, _filepath + ".bak"_s)) {
_lastError = "Couldn't create a backup for " + _filepath; _lastError = "Couldn't create a backup for " + _filepath;
LOG_ERROR(_lastError);
return false; return false;
} }
@ -78,6 +81,7 @@ auto UESaveFile::saveToFile() -> bool {
if(!writer.open()) { if(!writer.open()) {
_lastError = "Couldn't open the file for saving."_s; _lastError = "Couldn't open the file for saving."_s;
LOG_ERROR(_lastError);
return false; return false;
} }
@ -91,13 +95,15 @@ auto UESaveFile::saveToFile() -> bool {
!writer.writeUEString(_engineVersion.buildId)) !writer.writeUEString(_engineVersion.buildId))
{ {
_lastError = "Couldn't write the header."_s; _lastError = "Couldn't write the header."_s;
LOG_ERROR(_lastError);
return false; return false;
} }
if(!writer.writeUnsignedInt(_customFormatVersion) || if(!writer.writeUnsignedInt(_customFormatVersion) ||
!writer.writeUnsignedInt(_customFormatData.size())) !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; return false;
} }
@ -105,13 +111,15 @@ auto UESaveFile::saveToFile() -> bool {
if(!writer.writeStaticArray(Containers::StaticArrayView<16, const char>{_customFormatData[i].id}) || if(!writer.writeStaticArray(Containers::StaticArrayView<16, const char>{_customFormatData[i].id}) ||
!writer.writeUnsignedInt(_customFormatData[i].value)) !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; return false;
} }
} }
if(!writer.writeUEString(_saveType)) { if(!writer.writeUEString(_saveType)) {
_lastError = "Couldn't write the header."_s; _lastError = "Couldn't write the save type."_s;
LOG_ERROR(_lastError);
return false; return false;
} }
@ -119,11 +127,13 @@ auto UESaveFile::saveToFile() -> bool {
UnsignedLong bytes_written = 0; UnsignedLong bytes_written = 0;
if(!_propSerialiser->write(prop, bytes_written, writer)) { if(!_propSerialiser->write(prop, bytes_written, writer)) {
_lastError = "Couldn't write the property "_s + *prop->name + " to the array."_s; _lastError = "Couldn't write the property "_s + *prop->name + " to the array."_s;
LOG_ERROR(_lastError);
return false; return false;
} }
if(!writer.flushToFile()) { if(!writer.flushToFile()) {
_lastError = "Couldn't write the property "_s + *prop->name + " to the file."_s; _lastError = "Couldn't write the property "_s + *prop->name + " to the file."_s;
LOG_ERROR(_lastError);
return false; return false;
} }
} }
@ -134,11 +144,11 @@ auto UESaveFile::saveToFile() -> bool {
if(!Utility::Path::copy(_filepath + ".tmp"_s, _filepath)) { if(!Utility::Path::copy(_filepath + ".tmp"_s, _filepath)) {
_lastError = "Couldn't save the file properly."; _lastError = "Couldn't save the file properly.";
LOG_ERROR(_lastError);
return false; return false;
} }
else {
Utility::Path::remove(_filepath + ".tmp"_s); Utility::Path::remove(_filepath + ".tmp"_s);
}
_noReloadAfterSave = true; _noReloadAfterSave = true;
@ -146,9 +156,13 @@ auto UESaveFile::saveToFile() -> bool {
} }
void UESaveFile::loadData() { void UESaveFile::loadData() {
LOG_INFO_FORMAT("Reading data from {}.", _filepath);
_valid = false; _valid = false;
if(!Utility::Path::exists(_filepath)) { if(!Utility::Path::exists(_filepath)) {
_lastError = "The file couldn't be found.";
LOG_ERROR(_lastError);
return; return;
} }
@ -156,19 +170,22 @@ void UESaveFile::loadData() {
if(!reader.open()) { if(!reader.open()) {
_lastError = _filepath + " couldn't be opened."_s; _lastError = _filepath + " couldn't be opened."_s;
LOG_ERROR(_lastError);
return; return;
} }
Containers::Array<char> magic; Containers::Array<char> magic;
if(!reader.readArray(magic, 4)) { if(!reader.readArray(magic, 4)) {
_lastError = "Couldn't read magic bytes in "_s + _filepath; _lastError = "Couldn't read magic bytes in "_s + _filepath;
LOG_ERROR(_lastError);
return; return;
} }
Containers::String invalid = _filepath + " isn't a valid UE4 save."_s;
if(std::strncmp(magic.data(), _magicBytes.data(), 4) != 0) { 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; return;
} }
@ -180,19 +197,22 @@ void UESaveFile::loadData() {
!reader.readUnsignedInt(_engineVersion.build) || !reader.readUnsignedInt(_engineVersion.build) ||
!reader.readUEString(_engineVersion.buildId)) !reader.readUEString(_engineVersion.buildId))
{ {
_lastError = std::move(invalid); _lastError = "Couldn't read version data.";
LOG_ERROR(_lastError);
return; return;
} }
if(!reader.readUnsignedInt(_customFormatVersion)) { if(!reader.readUnsignedInt(_customFormatVersion)) {
_lastError = std::move(invalid); _lastError = "Couldn't read the custom format version.";
LOG_ERROR(_lastError);
return; return;
} }
UnsignedInt custom_format_data_size = 0; UnsignedInt custom_format_data_size = 0;
if(!reader.readUnsignedInt(custom_format_data_size)) { if(!reader.readUnsignedInt(custom_format_data_size)) {
_lastError = std::move(invalid); _lastError = "Couldn't read the custom format data size.";
LOG_ERROR(_lastError);
return; return;
} }
@ -204,7 +224,8 @@ void UESaveFile::loadData() {
if(!reader.readStaticArray(entry.id) || if(!reader.readStaticArray(entry.id) ||
!reader.readInt(entry.value)) !reader.readInt(entry.value))
{ {
_lastError = std::move(invalid); _lastError = "Couldn't read the custom format data";
LOG_ERROR(_lastError);
return; return;
} }
@ -212,7 +233,8 @@ void UESaveFile::loadData() {
} }
if(!reader.readUEString(_saveType)) { if(!reader.readUEString(_saveType)) {
_lastError = std::move(invalid); _lastError = "Couldn't read the save type.";
LOG_ERROR(_lastError);
return; return;
} }
@ -221,8 +243,15 @@ void UESaveFile::loadData() {
arrayAppend(_properties, std::move(prop)); 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) { if(_properties.back()->name != "None"_s && _properties.back()->propertyType != "NoneProperty"_s) {
_lastError = "Couldn't find a final NoneProperty."_s; _lastError = "Couldn't find a final NoneProperty."_s;
LOG_ERROR(_lastError);
return; return;
} }