Skip to content

Commit

Permalink
use property constructors without PropertyTag, cleanup property value…
Browse files Browse the repository at this point in the history
… interface
  • Loading branch information
moritz-h committed Feb 27, 2024
1 parent 6726706 commit 5d80e9b
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ namespace SatisfactorySave {
public:
static constexpr std::string_view TypeName = "BoolProperty";

using Property::Property;
BoolProperty() : Property(FName(std::string(TypeName))) {}
explicit BoolProperty(PropertyTag tag) : Property(std::move(tag)) {}

void serialize([[maybe_unused]] Archive& ar) override{};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,23 @@
#pragma once

#include <variant>
#include <vector>

#include "Base/Property.h"
#include "Base/PropertyImpl.h"

namespace SatisfactorySave {

class SATISFACTORYSAVE_API ByteProperty : public Property {
class SATISFACTORYSAVE_API ByteProperty : public PropertyImplBase<ByteProperty, std::variant<FName, int8_t>> {
public:
static constexpr std::string_view TypeName = "ByteProperty";

using Property::Property;
using PropertyImplBase<ByteProperty, std::variant<FName, int8_t>>::PropertyImplBase;

void serialize(Archive& ar) override;

void accept(PropertyVisitor& v) override;

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

[[nodiscard]] const FName& valueName() const {
return value_name_;
}

[[nodiscard]] int8_t valueByte() const {
return value_byte_;
}

protected:
FName value_name_;
int8_t value_byte_ = 0;
};
} // namespace SatisfactorySave
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ namespace SatisfactorySave {
public:
static constexpr std::string_view TypeName = "MapProperty";

MapProperty(PropertyTag tag);
MapProperty() : Property(FName(std::string(TypeName))) {}
explicit MapProperty(PropertyTag tag) : Property(std::move(tag)) {}

void serialize(Archive& ar) override;

Expand All @@ -25,16 +26,7 @@ namespace SatisfactorySave {
return tag_.ValueType;
}

[[nodiscard]] const std::unique_ptr<MapTypeList>& keys() const {
return keys_;
}

[[nodiscard]] const std::unique_ptr<MapTypeList>& values() const {
return values_;
}

protected:
std::unique_ptr<MapTypeList> keys_;
std::unique_ptr<MapTypeList> values_;
std::unique_ptr<MapTypeList> Keys;
std::unique_ptr<MapTypeList> Values;
};
} // namespace SatisfactorySave
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ namespace SatisfactorySave {

void accept(PropertyVisitor& v) override;

[[nodiscard]] const std::vector<char>& value() const {
return value_;
}

protected:
std::vector<char> value_;
std::vector<char> Value;
};
} // namespace SatisfactorySave
17 changes: 9 additions & 8 deletions libsave/src/GameTypes/Properties/ByteProperty.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#include "GameTypes/Properties/ByteProperty.h"

#include "GameTypes/Properties/Base/PropertyVisitor.h"

void SatisfactorySave::ByteProperty::serialize(Archive& ar) {
if (ar.isIArchive()) {
if (enumName() == "None") {
Value = static_cast<int8_t>(0);
} else {
Value = FName();
}
}
if (enumName() == "None") {
ar << value_byte_;
ar << std::get<int8_t>(Value);
} else {
ar << value_name_;
ar << std::get<FName>(Value);
}
}

void SatisfactorySave::ByteProperty::accept(SatisfactorySave::PropertyVisitor& v) {
v.visit(*this);
}
22 changes: 10 additions & 12 deletions libsave/src/GameTypes/Properties/MapProperty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
#include "IO/Archive/IStreamArchive.h"
#include "IO/Archive/OStreamArchive.h"

SatisfactorySave::MapProperty::MapProperty(SatisfactorySave::PropertyTag tag) : Property(std::move(tag)) {}

void SatisfactorySave::MapProperty::serialize(Archive& ar) {
if (ar.isIArchive()) {
auto& inAr = dynamic_cast<IStreamArchive&>(ar);
Expand All @@ -22,30 +20,30 @@ void SatisfactorySave::MapProperty::serialize(Archive& ar) {

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

keys_ = MapTypeList::create(keyType(), name(), inAr.ParentClassInfo.top(), true);
values_ = MapTypeList::create(valueType(), name(), inAr.ParentClassInfo.top(), false);
Keys = MapTypeList::create(keyType(), name(), inAr.ParentClassInfo.top(), true);
Values = MapTypeList::create(valueType(), name(), inAr.ParentClassInfo.top(), false);

keys_->resize(count);
values_->resize(count);
Keys->resize(count);
Values->resize(count);

for (int32_t i = 0; i < count; i++) {
keys_->serializeEntry(inAr, i);
values_->serializeEntry(inAr, i);
Keys->serializeEntry(inAr, i);
Values->serializeEntry(inAr, i);
}
} else {
auto& outAr = dynamic_cast<OStreamArchive&>(ar);

outAr.write<int32_t>(0);

auto count = keys_->size();
if (count != values_->size()) {
auto count = Keys->size();
if (count != Values->size()) {
throw std::runtime_error("Invalid map size!");
}
outAr.write(static_cast<int32_t>(count));

for (std::size_t i = 0; i < count; i++) {
keys_->serializeEntry(outAr, i);
values_->serializeEntry(outAr, i);
Keys->serializeEntry(outAr, i);
Values->serializeEntry(outAr, i);
}
}
}
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
4 changes: 2 additions & 2 deletions libsave/src/Utils/SaveTextExporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ namespace {
void visit(SatisfactorySave::ByteProperty& p) override {
file_ << " ByteType: " << p.enumName() << " ";
if (p.enumName() == "None") {
file_ << static_cast<int>(p.valueByte());
file_ << static_cast<int>(std::get<int8_t>(p.Value));
} else {
file_ << p.valueName();
file_ << std::get<SatisfactorySave::FName>(p.Value);
}
}

Expand Down
30 changes: 7 additions & 23 deletions libsavepy/GameTypes/Properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,6 @@ namespace {
} // namespace

void init_GameTypes_Properties(py::module_& m) {
py::class_<s::PropertyTag>(m, "PropertyTag")
.def(py::init<>())
.def_readwrite("Name", &s::PropertyTag::Name)
.def_readwrite("Type", &s::PropertyTag::Type)
.def_readwrite("Size", &s::PropertyTag::Size)
.def_readwrite("ArrayIndex", &s::PropertyTag::ArrayIndex)
.def_readwrite("StructName", &s::PropertyTag::StructName)
.def_readwrite("StructGuid", &s::PropertyTag::StructGuid)
.def_readwrite("BoolVal", &s::PropertyTag::BoolVal)
.def_readwrite("EnumName", &s::PropertyTag::EnumName)
.def_readwrite("InnerType", &s::PropertyTag::InnerType)
.def_readwrite("ValueType", &s::PropertyTag::ValueType)
.def_readwrite("HasPropertyGuid", &s::PropertyTag::HasPropertyGuid)
.def_readwrite("PropertyGuid", &s::PropertyTag::PropertyGuid);

py::class_<s::Property, PyProperty>(m, "Property")
.def_property("Name",
[](PyProperty& p) -> const s::FName& { return p.name(); },
Expand Down Expand Up @@ -69,21 +54,20 @@ void init_GameTypes_Properties(py::module_& m) {
;

py::class_<s::BoolProperty, s::Property>(m, "BoolProperty")
.def(py::init<s::PropertyTag>())
.def(py::init<>())
.def_property("Value", &s::BoolProperty::getValue, &s::BoolProperty::setValue);

py::class_<s::ByteProperty, s::Property>(m, "ByteProperty")
.def(py::init<s::PropertyTag>())
.def(py::init<>())
.def("enumName", &s::ByteProperty::enumName)
.def("valueName", &s::ByteProperty::valueName)
.def("valueByte", &s::ByteProperty::valueByte);
.def_readwrite("Value", &s::ByteProperty::Value);

py::class_<s::DoubleProperty, s::Property>(m, "DoubleProperty")
.def(py::init<>())
.def_readwrite("Value", &s::DoubleProperty::Value);

py::class_<s::EnumProperty, s::Property>(m, "EnumProperty")
.def(py::init<s::PropertyTag>())
.def(py::init<>())
.def_readwrite("Value", &s::EnumProperty::Value)
.def("enumName", &s::EnumProperty::enumName);

Expand All @@ -104,7 +88,7 @@ void init_GameTypes_Properties(py::module_& m) {
.def_readwrite("Value", &s::IntProperty::Value);

py::class_<s::MapProperty, s::Property>(m, "MapProperty")
//.def(py::init<s::PropertyTag>()) // TODO
.def(py::init<>())
.def("keyType", &s::MapProperty::keyType)
.def("valueType", &s::MapProperty::valueType)
//.def("keys", &s::MapProperty::keys) // TODO
Expand Down Expand Up @@ -155,6 +139,6 @@ void init_GameTypes_Properties(py::module_& m) {
.def_readwrite("Value", &s::UInt64Property::Value);

py::class_<s::UnknownProperty, s::Property>(m, "UnknownProperty")
.def(py::init<s::PropertyTag>())
.def("value", &s::UnknownProperty::value);
.def(py::init<s::FName>())
.def_readwrite("Value", &s::UnknownProperty::Value);
}
12 changes: 6 additions & 6 deletions map/src/MapWindow/UI/PropertyTableGuiRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,9 @@ namespace {
void visit(SatisfactorySave::ByteProperty& p) override {
ImGui::TextDisabled("EnumName: %s", p.enumName().toString().c_str());
if (p.enumName() == "None") {
ImGui::Text("%i", p.valueByte());
ImGui::Text("%i", std::get<int8_t>(p.Value));
} else {
ImGui::Text("%s", p.valueName().toString().c_str());
ImGui::Text("%s", std::get<SatisfactorySave::FName>(p.Value).toString().c_str());
}
}

Expand Down Expand Up @@ -405,8 +405,8 @@ namespace {
ImGui::TextDisabled("KeyType: %s", p.keyType().toString().c_str());
ImGui::TextDisabled("ValueType: %s", p.valueType().toString().c_str());

auto& keys = *p.keys();
auto& values = *p.values();
auto& keys = *p.Keys;
auto& values = *p.Values;
if (keys.size() != values.size()) {
throw std::runtime_error("Invalid MapProperty!");
}
Expand Down Expand Up @@ -495,10 +495,10 @@ namespace {
}

void visit(SatisfactorySave::UnknownProperty& p) override {
ImGui::Text("[UnknownProperty] %s, size: %zu", p.type().toString().c_str(), p.value().size());
ImGui::Text("[UnknownProperty] %s, size: %zu", p.type().toString().c_str(), p.Value.size());
if (ImGui::SmallButton(("Copy Hex##" + p.name().toString()).c_str())) {
std::stringstream stream;
for (const auto& c : p.value()) {
for (const auto& c : p.Value) {
stream << std::setfill('0') << std::setw(2) << std::hex
<< static_cast<int>(static_cast<unsigned char>(c)) << " ";
}
Expand Down

0 comments on commit 5d80e9b

Please sign in to comment.