Skip to content

Commit

Permalink
use std::optional instead HasGuid flag
Browse files Browse the repository at this point in the history
  • Loading branch information
moritz-h committed Sep 4, 2024
1 parent 538e1ce commit c9c7ff9
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <cstdint>
#include <istream>
#include <memory>
#include <optional>
#include <ostream>
#include <string>
#include <vector>
Expand Down Expand Up @@ -45,8 +46,7 @@ namespace SatisfactorySave {
FObjectReferenceDisc Reference;

PropertyList Properties;
bool HasGuid = false;
FGuid Guid;
std::optional<FGuid> Guid;
std::vector<char> ExtraProperties;
};

Expand Down
14 changes: 14 additions & 0 deletions libsave/include/SatisfactorySave/IO/Archive/Archive.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <concepts>
#include <filesystem>
#include <optional>
#include <stdexcept>
#include <type_traits>
#include <vector>
Expand Down Expand Up @@ -70,6 +71,19 @@ namespace SatisfactorySave {
return *this;
}

template<typename T>
inline Archive& operator<<(std::optional<T>& v) {
bool hasValue = v.has_value();
*this << hasValue;
if (hasValue) {
if (isIArchive()) {
v.emplace();
}
*this << v.value();
}
return *this;
}

inline Archive& operator<<(bool& v) {
uint32_t tmp = v ? 1 : 0;
serialize(&tmp, sizeof(uint32_t));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#pragma once

#include <optional>
#include <vector>

#include "../../GameTypes/Properties/Base/PropertyList.h"
#include "../../GameTypes/UE/Misc/Guid.h"
#include "../../IO/Archive/Archive.h"
Expand Down Expand Up @@ -267,8 +270,7 @@ namespace SatisfactorySave {

protected:
PropertyList properties_;
bool hasGuid_ = false;
FGuid guid_;
std::optional<FGuid> guid_;
FStaticMeshRenderData RenderData;
};
} // namespace SatisfactorySave
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#pragma once

#include <optional>
#include <string>
#include <vector>

#include "../../GameTypes/Properties/Base/PropertyList.h"
#include "../../GameTypes/UE/Misc/Guid.h"
#include "../../GameTypes/UE/UObject/Serialization/BulkData.h"
Expand Down Expand Up @@ -83,8 +87,7 @@ namespace SatisfactorySave {

protected:
PropertyList properties_;
bool hasGuid_ = false;
FGuid guid_;
std::optional<FGuid> guid_;

FTexturePlatformData RunningPlatformData;
};
Expand Down
11 changes: 2 additions & 9 deletions libsave/src/GameTypes/Save/SaveObjectBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ void SatisfactorySave::SaveObjectBase::serializeProperties(Archive& ar, int32_t
inAr.popParentClassInfo();

// https://github.com/EpicGames/UnrealEngine/blob/4.26.2-release/Engine/Source/Runtime/CoreUObject/Private/UObject/Obj.cpp#L1399
ar << HasGuid;
if (HasGuid) {
ar << Guid;
}
ar << Guid;

auto pos_after = inAr.tell();

Expand All @@ -49,11 +46,7 @@ void SatisfactorySave::SaveObjectBase::serializeProperties(Archive& ar, int32_t
auto& outAr = dynamic_cast<OStreamArchive&>(ar);

ar << Properties;

ar << HasGuid;
if (HasGuid) {
ar << Guid;
}
ar << Guid;

if (!ExtraProperties.empty()) {
outAr.write_buffer(ExtraProperties);
Expand Down
6 changes: 1 addition & 5 deletions libsave/src/Pak/Serialization/StaticMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ void SatisfactorySave::StaticMesh::serialize(Archive& ar) {
// From UObject::Serialize

ar << properties_;

ar << hasGuid_;
if (hasGuid_) {
ar << guid_;
}
ar << guid_;

// UStaticMesh::Serialize

Expand Down
6 changes: 1 addition & 5 deletions libsave/src/Pak/Serialization/Texture2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ void SatisfactorySave::Texture2D::serialize(Archive& ar) {
// From UObject::Serialize

ar << properties_;

ar << hasGuid_;
if (hasGuid_) {
ar << guid_;
}
ar << guid_;

// UTexture::Serialize
// https://github.com/EpicGames/UnrealEngine/blob/4.26.2-release/Engine/Source/Runtime/Engine/Private/Texture.cpp#L366
Expand Down
1 change: 0 additions & 1 deletion libsavepy/GameTypes/Save.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ void init_GameTypes_Save(py::module_& m) {
.def_readwrite("ClassName", &s::SaveObjectBase::ClassName)
.def_readwrite("Reference", &s::SaveObjectBase::Reference)
.def_readwrite("Properties", &s::SaveObjectBase::Properties)
.def_readwrite("HasGuid", &s::SaveObjectBase::HasGuid)
.def_readwrite("Guid", &s::SaveObjectBase::Guid)
.def_property("ExtraProperties",
[](s::SaveObjectBase& o) -> py::bytes {
Expand Down
4 changes: 1 addition & 3 deletions libsavepy/examples/print_object_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ def print_object(obj: s.SaveObjectBase):
print(f' Properties:')
for p in obj.Properties:
print_property(p, 2)
print(f' HasGuid: {obj.HasGuid}')
if obj.HasGuid:
print(f' Guid: {obj.Guid}')
print(f' Guid: {obj.Guid}')
print(f' ExtraProperties: {obj.ExtraProperties}')


Expand Down

0 comments on commit c9c7ff9

Please sign in to comment.