1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-30 09:15:38 +00:00

Implements serialize/deserialize

fixes test compilation.
This commit is contained in:
florent.teppe 2023-04-03 18:41:25 +02:00
parent 3258fa4f98
commit 0d17e20490
6 changed files with 37 additions and 23 deletions

View file

@ -342,6 +342,12 @@ namespace ESM
static RefId call() { return RefId::index(REC_BOOK, 7); }
};
template <>
struct GenerateRefId<ESM3ExteriorCellRefId>
{
static RefId call() { return RefId::esm3ExteriorCell(-12, 7); }
};
template <class T>
struct ESMRefIdTypesTest : Test
{

View file

@ -248,16 +248,10 @@ namespace ESM
record.mObject.mRef.mRefID = generateRandomRefId();
std::generate_n(std::inserter(record.mPreviousItems, record.mPreviousItems.end()), 2,
[&] { return std::make_pair(generateRandomRefId(), generateRandomRefId()); });
record.mCellId.mWorldspace = "worldspace1";
record.mCellId.mIndex.mX = 42;
record.mCellId.mIndex.mY = 13;
record.mCellId.mPaged = true;
record.mCellId = ESM::RefId::esm3ExteriorCell(0, 0);
generateArray(record.mLastKnownExteriorPosition);
record.mHasMark = true;
record.mMarkedCell.mWorldspace = "worldspace2";
record.mMarkedCell.mIndex.mX = 0;
record.mMarkedCell.mIndex.mY = 0;
record.mMarkedCell.mPaged = false;
record.mMarkedCell = ESM::RefId::esm3ExteriorCell(0, 0);
generateArray(record.mMarkedPosition.pos);
generateArray(record.mMarkedPosition.rot);
record.mCurrentCrimeId = 42;
@ -267,16 +261,11 @@ namespace ESM
EXPECT_EQ(record.mBirthsign, result.mBirthsign);
EXPECT_EQ(record.mPreviousItems, result.mPreviousItems);
EXPECT_EQ(record.mPreviousItems, result.mPreviousItems);
EXPECT_EQ(record.mCellId.mWorldspace, result.mCellId.mWorldspace);
EXPECT_EQ(record.mCellId.mIndex.mX, result.mCellId.mIndex.mX);
EXPECT_EQ(record.mCellId.mIndex.mY, result.mCellId.mIndex.mY);
EXPECT_EQ(record.mCellId.mPaged, result.mCellId.mPaged);
EXPECT_EQ(record.mCellId, result.mCellId);
EXPECT_THAT(record.mLastKnownExteriorPosition, ElementsAreArray(result.mLastKnownExteriorPosition));
EXPECT_EQ(record.mHasMark, result.mHasMark);
EXPECT_EQ(record.mMarkedCell.mWorldspace, result.mMarkedCell.mWorldspace);
EXPECT_EQ(record.mMarkedCell.mIndex.mX, result.mMarkedCell.mIndex.mX);
EXPECT_EQ(record.mMarkedCell.mIndex.mY, result.mMarkedCell.mIndex.mY);
EXPECT_EQ(record.mMarkedCell.mPaged, result.mMarkedCell.mPaged);
EXPECT_EQ(record.mMarkedCell, result.mMarkedCell);
EXPECT_THAT(record.mMarkedPosition.pos, ElementsAreArray(result.mMarkedPosition.pos));
EXPECT_THAT(record.mMarkedPosition.rot, ElementsAreArray(result.mMarkedPosition.rot));
EXPECT_EQ(record.mCurrentCrimeId, result.mCurrentCrimeId);

View file

@ -1,4 +1,5 @@
#include "esm3exteriorcellrefid.hpp"
#include "serializerefid.hpp"
#include <ostream>
#include <sstream>
@ -7,16 +8,25 @@ namespace ESM
{
std::string ESM3ExteriorCellRefId::toString() const
{
std::ostringstream stream;
stream << "# " << mY << ", " << mY;
return stream.str();
std::string result;
std::size_t integralSizeX = getIntegralSize(mX);
result.resize(integralSizeX + getIntegralSize(mY) + 3, '\0');
serializeIntegral(mX, 0, result);
result[integralSizeX] = ':';
serializeIntegral(mY, integralSizeX + 1, result);
return result;
}
std::string ESM3ExteriorCellRefId::toDebugString() const
{
std::ostringstream stream;
stream << *this;
return stream.str();
std::string result;
std::size_t integralSizeX = getIntegralSize(mX);
serializeRefIdPrefix(integralSizeX + getIntegralSize(mY) + 1, esm3ExteriorCellRefIdPrefix, result);
serializeIntegral(mX, esm3ExteriorCellRefIdPrefix.size(), result);
result[esm3ExteriorCellRefIdPrefix.size() + integralSizeX] = ':';
serializeIntegral(mY, esm3ExteriorCellRefIdPrefix.size() + integralSizeX + 1, result);
return result;
}
std::ostream& operator<<(std::ostream& stream, ESM3ExteriorCellRefId value)

View file

@ -237,6 +237,13 @@ namespace ESM
return ESM::RefId::index(recordType,
deserializeIntegral<std::uint32_t>(indexRefIdPrefix.size() + sizeof(recordType) + 1, value));
}
if (value.starts_with(esm3ExteriorCellRefIdPrefix))
{
std::int32_t x = deserializeIntegral<std::int32_t>(esm3ExteriorCellRefIdPrefix.size(), value);
std::int32_t y
= deserializeIntegral<std::int32_t>(esm3ExteriorCellRefIdPrefix.size() + getIntegralSize(x) + 1, value);
return ESM::ESM3ExteriorCellRefId(x, y);
}
return ESM::RefId::stringRefId(value);
}

View file

@ -50,7 +50,8 @@ namespace ESM
public:
const static RefId sEmpty;
using Value = std::variant<EmptyRefId, StringRefId, FormIdRefId, GeneratedRefId, IndexRefId>;
using Value
= std::variant<EmptyRefId, StringRefId, FormIdRefId, GeneratedRefId, IndexRefId, ESM3ExteriorCellRefId>;
// Constructs RefId from a serialized string containing byte by byte copy of RefId::mValue.
static ESM::RefId deserialize(std::string_view value);

View file

@ -12,6 +12,7 @@ namespace ESM
constexpr std::string_view formIdRefIdPrefix = "FormId:";
constexpr std::string_view generatedRefIdPrefix = "Generated:";
constexpr std::string_view indexRefIdPrefix = "Index:";
constexpr std::string_view esm3ExteriorCellRefIdPrefix = "Esm3ExteriorCell:";
template <class T>
std::size_t getIntegralSize(T value)