Skip to content

Commit

Permalink
remove PropertyTag from public interface
Browse files Browse the repository at this point in the history
  • Loading branch information
moritz-h committed Feb 24, 2024
1 parent 719edac commit 11a62f8
Show file tree
Hide file tree
Showing 23 changed files with 123 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ namespace SatisfactorySave {

void accept(PropertyVisitor& v) override;

[[nodiscard]] const FName& arrayType() const {
return Tag.InnerType;
[[nodiscard]] inline FName& arrayType() {
return tag_.InnerType;
}

[[nodiscard]] const std::unique_ptr<Array>& array() const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace SatisfactorySave {
public:
static std::unique_ptr<Property> create(IStreamArchive& ar, const std::string& parentClassName);

explicit Property(FName type);
explicit Property(PropertyTag tag);
virtual ~Property() = default;

Expand All @@ -25,10 +26,33 @@ namespace SatisfactorySave {
Property(Property&&) = default;
Property& operator=(Property&&) = default;

[[nodiscard]] inline FName& name() {
return tag_.Name;
}

[[nodiscard]] inline const FName& type() const {
return tag_.Type;
}

[[nodiscard]] inline int32_t& arrayIndex() {
return tag_.ArrayIndex;
}

[[nodiscard]] inline uint8_t& hasPropertyGuid() {
return tag_.HasPropertyGuid;
}

[[nodiscard]] inline FGuid& propertyGuid() {
return tag_.PropertyGuid;
}

virtual void serialize(Archive& ar) = 0;

virtual void accept(PropertyVisitor& v) = 0;

PropertyTag Tag;
protected:
PropertyTag tag_;

friend class PropertyList;
};
} // namespace SatisfactorySave
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ namespace SatisfactorySave {
template<typename Impl, typename T>
class PropertyImpl : public Property {
public:
using Property::Property;
PropertyImpl() : Property(FName(std::string(Impl::TypeName))) {}
explicit PropertyImpl(PropertyTag tag) : Property(std::move(tag)) {}

void serialize(Archive& ar) override {
ar << Value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ namespace SatisfactorySave {
}

template<typename T>
inline const T& get(const std::string& name) const {
inline T& get(const std::string& name) const {
for (const auto& p : properties_) {
if (p->Tag.Name == name) {
const T* property = dynamic_cast<const T*>(p.get());
if (p->name() == name) {
T* property = dynamic_cast<T*>(p.get());
if (property != nullptr) {
return *property;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ namespace SatisfactorySave {

void accept(PropertyVisitor& v) override;

[[nodiscard]] uint8_t value() const {
return Tag.BoolVal;
[[nodiscard]] inline bool getValue() const {
return !!tag_.BoolVal;
}

inline void setValue(bool v) {
tag_.BoolVal = v ? 1 : 0;
}
};
} // namespace SatisfactorySave
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ namespace SatisfactorySave {

void accept(PropertyVisitor& v) override;

[[nodiscard]] const FName& byteType() const {
return Tag.EnumName;
[[nodiscard]] inline FName& enumName() {
return tag_.EnumName;
}

[[nodiscard]] const FName& valueName() const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ namespace SatisfactorySave {

using PropertyImpl<EnumProperty, FName>::PropertyImpl;

[[nodiscard]] const FName& enumType() const {
return Tag.EnumName;
[[nodiscard]] inline FName& enumName() {
return tag_.EnumName;
}
};
} // namespace SatisfactorySave
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ namespace SatisfactorySave {

void accept(PropertyVisitor& v) override;

[[nodiscard]] const FName& keyType() const {
return Tag.InnerType;
[[nodiscard]] inline FName& keyType() {
return tag_.InnerType;
}

[[nodiscard]] const FName& valueType() const {
return Tag.ValueType;
[[nodiscard]] inline FName& valueType() {
return tag_.ValueType;
}

[[nodiscard]] const std::unique_ptr<MapTypeList>& keys() const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ namespace SatisfactorySave {

void accept(PropertyVisitor& v) override;

[[nodiscard]] const FName& setType() const {
return Tag.InnerType;
[[nodiscard]] inline FName& setType() {
return tag_.InnerType;
}

[[nodiscard]] const std::unique_ptr<Set>& set() const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ namespace SatisfactorySave {

void accept(PropertyVisitor& v) override;

[[nodiscard]] const std::string& structName() const {
return Tag.StructName.Name;
[[nodiscard]] inline FName& structName() {
return tag_.StructName;
}

[[nodiscard]] const FGuid& guid() const {
return Tag.StructGuid;
[[nodiscard]] inline FGuid& structGuid() {
return tag_.StructGuid;
}

[[nodiscard]] const std::unique_ptr<Struct>& value() const {
Expand Down
4 changes: 4 additions & 0 deletions libsave/include/SatisfactorySave/GameTypes/UE/UObject/Name.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <cstdint>
#include <ostream>
#include <string>
#include <utility>

#include "satisfactorysave_export.h"

Expand All @@ -14,6 +15,9 @@ namespace SatisfactorySave {
std::string Name;
uint32_t Number = 0;

FName() = default;
explicit FName(std::string Name, uint32_t Number = 0) : Name(std::move(Name)), Number(Number) {}

FName& operator=(const std::string& s) {
Name = s;
Number = 0;
Expand Down
2 changes: 1 addition & 1 deletion libsave/src/GameTypes/Properties/ArrayProperty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

void SatisfactorySave::ArrayProperty::serialize(SatisfactorySave::Archive& ar) {
if (ar.isIArchive()) {
array_ = Array::create(Tag.InnerType, ar);
array_ = Array::create(arrayType(), ar);
} else {
ar << *array_;
}
Expand Down
14 changes: 9 additions & 5 deletions libsave/src/GameTypes/Properties/Base/Property.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ std::unique_ptr<SatisfactorySave::Property> SatisfactorySave::Property::create(S
}

auto pos_before = ar.tell();
ar.pushReadLimit(property->Tag.Size);
ar.pushReadLimit(property->tag_.Size);
static int recursion_depth = 0; // Count recursion depth for better debug logging.
recursion_depth++;
try {
ar << *property;
recursion_depth--;
} catch (const std::exception& ex) {
recursion_depth--;
PropertyTag tagCopy = property->Tag;
PropertyTag tagCopy = property->tag_;
spdlog::error("Error parsing property {} (Type: {}, Class: {}) in recursion depth {}: {}",
tagCopy.Name.toString(), tagCopy.Type.toString(), parentClassName, recursion_depth, ex.what());

Expand All @@ -92,12 +92,16 @@ std::unique_ptr<SatisfactorySave::Property> SatisfactorySave::Property::create(S
}
ar.popReadLimit();
auto pos_after = ar.tell();
if (pos_after - pos_before != property->Tag.Size) {
if (pos_after - pos_before != property->tag_.Size) {
throw std::runtime_error(
std::string("Invalid Property size!\nName: ") + property->Tag.Name + "\nType: " + property->Tag.Type);
std::string("Invalid Property size!\nName: ") + property->tag_.Name + "\nType: " + property->tag_.Type);
}

return property;
}

SatisfactorySave::Property::Property(SatisfactorySave::PropertyTag tag) : Tag(std::move(tag)) {}
SatisfactorySave::Property::Property(SatisfactorySave::FName type) {
tag_.Type = std::move(type);
}

SatisfactorySave::Property::Property(SatisfactorySave::PropertyTag tag) : tag_(std::move(tag)) {}
8 changes: 4 additions & 4 deletions libsave/src/GameTypes/Properties/Base/PropertyList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ void SatisfactorySave::PropertyList::serialize(SatisfactorySave::Archive& ar) {
auto& outAr = dynamic_cast<OStreamArchive&>(ar);

for (const auto& p : properties_) {
outAr << p->Tag;
outAr << p->tag_;

auto pos_before = outAr.tell();

outAr << *p;

auto pos_after = outAr.tell();

p->Tag.Size = static_cast<int32_t>(pos_after - pos_before);
outAr.seek(p->Tag.SizeOffset);
outAr << p->Tag.Size;
p->tag_.Size = static_cast<int32_t>(pos_after - pos_before);
outAr.seek(p->tag_.SizeOffset);
outAr << p->tag_.Size;
outAr.seek(pos_after);
}
// None property to terminate property list
Expand Down
2 changes: 1 addition & 1 deletion libsave/src/GameTypes/Properties/ByteProperty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "GameTypes/Properties/Base/PropertyVisitor.h"

void SatisfactorySave::ByteProperty::serialize(Archive& ar) {
if (Tag.EnumName == "None") {
if (enumName() == "None") {
ar << value_byte_;
} else {
ar << value_name_;
Expand Down
4 changes: 2 additions & 2 deletions libsave/src/GameTypes/Properties/MapProperty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ void SatisfactorySave::MapProperty::serialize(Archive& ar) {

auto count = inAr.read<int32_t>();

keys_ = MapTypeList::create(Tag.InnerType, Tag.Name, parentClassName_, true);
values_ = MapTypeList::create(Tag.ValueType, Tag.Name, parentClassName_, false);
keys_ = MapTypeList::create(keyType(), name(), parentClassName_, true);
values_ = MapTypeList::create(valueType(), name(), parentClassName_, false);

keys_->resize(count);
values_->resize(count);
Expand Down
2 changes: 1 addition & 1 deletion libsave/src/GameTypes/Properties/SetProperty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void SatisfactorySave::SetProperty::serialize(Archive& ar) {
}

if (ar.isIArchive()) {
set_ = Set::create(Tag.InnerType, Tag.Name, parentClassName_, ar);
set_ = Set::create(setType(), name(), parentClassName_, ar);
} else {
ar << *set_;
}
Expand Down
2 changes: 1 addition & 1 deletion libsave/src/GameTypes/Properties/StructProperty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

void SatisfactorySave::StructProperty::serialize(Archive& ar) {
if (ar.isIArchive()) {
struct_ = Struct::create(Tag.StructName, ar);
struct_ = Struct::create(structName(), ar);
} else {
ar << *struct_;
}
Expand Down
4 changes: 2 additions & 2 deletions libsave/src/GameTypes/Properties/UnknownProperty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#include "GameTypes/Properties/Base/PropertyVisitor.h"

void SatisfactorySave::UnknownProperty::serialize(Archive& ar) {
value_.resize(Tag.Size);
ar.serializeRaw(value_.data(), Tag.Size);
value_.resize(tag_.Size);
ar.serializeRaw(value_.data(), tag_.Size);
}

void SatisfactorySave::UnknownProperty::accept(SatisfactorySave::PropertyVisitor& v) {
Expand Down
14 changes: 7 additions & 7 deletions libsave/src/Utils/SaveTextExporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ namespace {
}

void visit(SatisfactorySave::BoolProperty& p) override {
file_ << " " << static_cast<int>(p.value());
file_ << " " << static_cast<int>(p.getValue());
}

void visit(SatisfactorySave::ByteProperty& p) override {
file_ << " ByteType: " << p.byteType() << " ";
if (p.byteType() == "None") {
file_ << " ByteType: " << p.enumName() << " ";
if (p.enumName() == "None") {
file_ << static_cast<int>(p.valueByte());
} else {
file_ << p.valueName();
Expand All @@ -37,7 +37,7 @@ namespace {
}

void visit(SatisfactorySave::EnumProperty& p) override {
file_ << " EnumType: " << p.enumType() << " " << p.Value;
file_ << " EnumType: " << p.enumName() << " " << p.Value;
}

void visit(SatisfactorySave::FloatProperty& p) override {
Expand Down Expand Up @@ -88,7 +88,7 @@ namespace {
}

void visit(SatisfactorySave::StructProperty& p) override {
file_ << " " << p.structName() << " " << p.guid().toString();
file_ << " " << p.structName() << " " << p.structGuid().toString();
// TODO values
}

Expand All @@ -105,7 +105,7 @@ namespace {
}

void visit(SatisfactorySave::UnknownProperty& p) override {
file_ << " [UnknownProperty] " << p.Tag.Type;
file_ << " [UnknownProperty] " << p.type();
}
};

Expand All @@ -119,7 +119,7 @@ namespace {
<< std::endl;

for (const auto& p : obj->Properties) {
file << " " << p->Tag.Name << " " << p->Tag.Type;
file << " " << p->name() << " " << p->type();
PropertyValueWriter w(file);
p->accept(w);
file << std::endl;
Expand Down
Loading

0 comments on commit 11a62f8

Please sign in to comment.