Skip to content

Commit

Permalink
feat(skymp5-server): add OnPutItem/OnTakeItem support (skyrim-multipl…
Browse files Browse the repository at this point in the history
  • Loading branch information
barkinlove authored Apr 7, 2024
1 parent ce81275 commit f1d9f28
Show file tree
Hide file tree
Showing 25 changed files with 477 additions and 17 deletions.
1 change: 1 addition & 0 deletions libespm/include/libespm/ALCH.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class ALCH final : public RecordHeader
struct Data
{
std::vector<Effects::Effect> effects;
float weight;
};

Data GetData(CompressedFieldsCache& compressedFieldsCache) const noexcept;
Expand Down
25 changes: 25 additions & 0 deletions libespm/include/libespm/AMMO.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once
#include "RecordHeader.h"

#pragma pack(push, 1)

namespace espm {

class AMMO final : public RecordHeader
{
public:
static constexpr auto kType = "AMMO";

struct Data
{
float weight;
};

Data GetData(CompressedFieldsCache& compressedFieldsCache) const;
};

static_assert(sizeof(AMMO) == sizeof(RecordHeader));

}

#pragma pack(pop)
1 change: 1 addition & 0 deletions libespm/include/libespm/BOOK.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class BOOK final : public RecordHeader

Flags flags = Flags::None;
uint32_t spellOrSkillFormId = 0;
float weight;
};

Data GetData(CompressedFieldsCache& compressedFieldsCache) const noexcept;
Expand Down
7 changes: 7 additions & 0 deletions libespm/include/libespm/INGR.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@ class INGR final : public RecordHeader
public:
static constexpr auto kType = "INGR";

struct ItemData
{
uint32_t value;
float weight;
};

struct Data
{
ItemData itemData;
std::vector<Effects::Effect> effects;
};

Expand Down
25 changes: 25 additions & 0 deletions libespm/include/libespm/MISC.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once
#include "RecordHeader.h"

#pragma pack(push, 1)

namespace espm {

class MISC final : public RecordHeader
{
public:
static constexpr auto kType = "MISC";

struct Data
{
float weight;
};

Data GetData(CompressedFieldsCache& compressedFieldsCache) const;
};

static_assert(sizeof(MISC) == sizeof(RecordHeader));

}

#pragma pack(pop)
4 changes: 4 additions & 0 deletions libespm/include/libespm/Records.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "ACHR.h"
#include "ACTI.h"
#include "ALCH.h"
#include "AMMO.h"
#include "ARMO.h"
#include "BOOK.h"
#include "CELL.h"
Expand All @@ -18,11 +19,14 @@
#include "LVLI.h"
#include "LVLN.h"
#include "MGEF.h"
#include "MISC.h"
#include "NAVM.h"
#include "NPC_.h"
#include "OTFT.h"
#include "RACE.h"
#include "REFR.h"
#include "SCRL.h"
#include "SLGM.h"
#include "SPEL.h"
#include "TES4.h"
#include "TREE.h"
Expand Down
32 changes: 32 additions & 0 deletions libespm/include/libespm/SCRL.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once
#include "RecordHeader.h"

#pragma pack(push, 1)

namespace espm {

class SCRL final : public RecordHeader
{
public:
static constexpr auto kType = "SCRL";

private:
struct DATA
{
float weight;
};

public:
struct Data
{
DATA data;
};

Data GetData(CompressedFieldsCache& compressedFieldsCache) const;
};

static_assert(sizeof(SCRL) == sizeof(RecordHeader));

}

#pragma pack(pop)
25 changes: 25 additions & 0 deletions libespm/include/libespm/SLGM.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once
#include "RecordHeader.h"

#pragma pack(push, 1)

namespace espm {

class SLGM final : public RecordHeader
{
public:
static constexpr auto kType = "SLGM";

struct Data
{
float weight;
};

Data GetData(CompressedFieldsCache& compressedFieldsCache) const;
};

static_assert(sizeof(SLGM) == sizeof(RecordHeader));

}

#pragma pack(pop)
8 changes: 8 additions & 0 deletions libespm/src/ALCH.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ ALCH::Data ALCH::GetData(
{
Data result;
result.effects = Effects(this).GetData(compressedFieldsCache).effects;
RecordHeaderAccess::IterateFields(
this,
[&](const char* type, uint32_t size, const char* data) {
if (!std::memcmp(type, "DATA", 4)) {
result.weight = *reinterpret_cast<const float*>(data);
}
},
compressedFieldsCache);
return result;
}

Expand Down
21 changes: 21 additions & 0 deletions libespm/src/AMMO.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "libespm/AMMO.h"
#include "libespm/RecordHeaderAccess.h"

namespace espm {

AMMO::Data AMMO::GetData(CompressedFieldsCache& cache) const
{
Data res;
RecordHeaderAccess::IterateFields(
this,
[&](const char* type, uint32_t size, const char* data) {
if (!std::memcmp(type, "DATA", 4)) {
// NOTE: 0x10 offset is for SSE version only
res.weight = *reinterpret_cast<const float*>(data + 0x10);
}
},
cache);
return res;
}

}
1 change: 1 addition & 0 deletions libespm/src/BOOK.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ BOOK::Data BOOK::GetData(

result.spellOrSkillFormId =
*reinterpret_cast<const uint32_t*>(data + 0x4);
result.weight = *reinterpret_cast<const float*>(data + 0xc);
}
},
compressedFieldsCache);
Expand Down
10 changes: 10 additions & 0 deletions libespm/src/INGR.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "libespm/INGR.h"
#include "libespm/IterateFields.h"
#include "libespm/RecordHeaderAccess.h"
#include <cstring>

Expand All @@ -9,6 +10,15 @@ INGR::Data INGR::GetData(
{
Data result;
result.effects = Effects(this).GetData(compressedFieldsCache).effects;
RecordHeaderAccess::IterateFields(
this,
[&](const char* type, uint32_t size, const char* data) {
if (!std::memcmp(type, "DATA", 4)) {
result.itemData.value = *reinterpret_cast<const uint32_t*>(data);
result.itemData.weight = *reinterpret_cast<const float*>(data + 0x4);
}
},
compressedFieldsCache);
return result;
}

Expand Down
21 changes: 21 additions & 0 deletions libespm/src/MISC.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "libespm/MISC.h"
#include "libespm/CompressedFieldsCache.h"
#include "libespm/RecordHeaderAccess.h"

namespace espm {

MISC::Data MISC::GetData(CompressedFieldsCache& cache) const
{
Data res;
RecordHeaderAccess::IterateFields(
this,
[&](const char* type, uint32_t size, const char* data) {
if (!std::memcmp(type, "DATA", 4)) {
res.weight = *reinterpret_cast<const float*>(data + 0x4);
}
},
cache);
return res;
}

}
21 changes: 21 additions & 0 deletions libespm/src/SCRL.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "libespm/SCRL.h"
#include "libespm/CompressedFieldsCache.h"
#include "libespm/RecordHeaderAccess.h"

namespace espm {

SCRL::Data SCRL::GetData(CompressedFieldsCache& cache) const
{
Data res;
RecordHeaderAccess::IterateFields(
this,
[&](const char* type, uint32_t size, const char* data) {
if (!std::memcmp(type, "DATA", 4)) {
res.data.weight = *reinterpret_cast<const float*>(data + 0x4);
}
},
cache);
return res;
}

}
21 changes: 21 additions & 0 deletions libespm/src/SLGM.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "libespm/SLGM.h"
#include "libespm/CompressedFieldsCache.h"
#include "libespm/RecordHeaderAccess.h"

namespace espm {

SLGM::Data SLGM::GetData(CompressedFieldsCache& cache) const
{
Data res;
RecordHeaderAccess::IterateFields(
this,
[&](const char* type, uint32_t size, const char* data) {
if (!std::memcmp(type, "DATA", 4)) {
res.weight = *reinterpret_cast<const float*>(data + 0x4);
}
},
cache);
return res;
}

}
75 changes: 75 additions & 0 deletions skymp5-server/cpp/server_guest_lib/GetWeightFromRecord.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include "GetWeightFromRecord.h"

#include "libespm/ALCH.h"
#include "libespm/AMMO.h"
#include "libespm/ARMO.h"
#include "libespm/BOOK.h"
#include "libespm/CompressedFieldsCache.h"
#include "libespm/Convert.h"
#include "libespm/INGR.h"
#include "libespm/LIGH.h"
#include "libespm/MISC.h"
#include "libespm/RecordHeader.h"
#include "libespm/SCRL.h"
#include "libespm/SLGM.h"
#include "libespm/WEAP.h"

float GetWeightFromRecord(const espm::RecordHeader* record,
espm::CompressedFieldsCache& cache)
{
if (auto* weap = espm::Convert<espm::WEAP>(record)) {
auto data = weap->GetData(cache);
return data.weapData->weight;
}

if (auto* ligh = espm::Convert<espm::LIGH>(record)) {
auto data = ligh->GetData(cache);
return data.data.weight;
}

if (auto* armo = espm::Convert<espm::ARMO>(record)) {
auto data = armo->GetData(cache);
return data.weight;
}

if (auto* ingr = espm::Convert<espm::INGR>(record)) {
auto data = ingr->GetData(cache);
return data.itemData.weight;
}

if (auto* alch = espm::Convert<espm::ALCH>(record)) {
auto data = alch->GetData(cache);
return data.weight;
}

if (auto* book = espm::Convert<espm::BOOK>(record)) {
auto data = book->GetData(cache);
return data.weight;
}

// TODO: add unit test
if (auto* ammo = espm::Convert<espm::AMMO>(record)) {
auto data = ammo->GetData(cache);
return data.weight;
}

// TODO: add unit test
if (auto* scroll = espm::Convert<espm::SCRL>(record)) {
auto data = scroll->GetData(cache);
return data.data.weight;
}

// TODO: add unit test
if (auto* slgm = espm::Convert<espm::SLGM>(record)) {
auto data = slgm->GetData(cache);
return data.weight;
}

// TODO: add unit test
if (auto* misc = espm::Convert<espm::MISC>(record)) {
auto data = misc->GetData(cache);
return data.weight;
}

return 0.f;
}
11 changes: 11 additions & 0 deletions skymp5-server/cpp/server_guest_lib/GetWeightFromRecord.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

namespace espm {

class RecordHeader;
class CompressedFieldsCache;

}

float GetWeightFromRecord(const espm::RecordHeader* record,
espm::CompressedFieldsCache& cache);
Loading

0 comments on commit f1d9f28

Please sign in to comment.