Skip to content

Fix a crash when setting float3 type soundevent field #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 28 additions & 65 deletions src/network/soundevents/soundevent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@
return return_value;

#define CHECK_FIELD_TYPE(check_type, return_value) \
if (field->type != check_type) return return_value;
if (field->GetType() != check_type) return return_value;

#define SET_FIELD(check_type, value) \
SosField field; \
field.type = check_type; \
field.data = value; \
this->AddOrReplaceField(pszFieldName, &field);
SosField field(check_type, &value); \
this->AddOrReplaceField(pszFieldName, field);

template<typename T>
void insert(std::vector<uint8_t>& vec, const T* value, int size = -1) {
#define RETURN_FIELD(return_type) \
return *reinterpret_cast<const return_type*>(field->GetData());

void insert(std::vector<uint8_t>& vec, const void* value, uint8_t size) {
const char* bytes = reinterpret_cast<const char*>(value);

for (size_t i = 0; i < (size == -1 ? sizeof(T) : size); ++i) {
for (int i = 0; i < size; i++) {
vec.push_back(bytes[i]);
}
}
Expand All @@ -46,36 +46,12 @@ uint32_t Soundevent::Emit()
for (const auto& pair : this->parameters) {
auto fieldName = pair.first;
auto fieldData = &pair.second;
auto data = &fieldData->data;
uint32_t paramNameHash = MurmurHash2LowerCase(fieldName.c_str(), SOUNDEVENT_FIELD_NAME_HASH_SEED);
insert(buffer, &paramNameHash); // name hash
buffer.push_back(fieldData->type); // data type

char size;
switch (fieldData->type) {
case SE_Bool:
buffer.push_back(1); // data size
buffer.push_back(0); // padding
buffer.push_back(data->m_bool); // data
break;
case SE_Int:
case SE_UInt32:
case SE_Float:
buffer.push_back(4);
buffer.push_back(0);
insert(buffer, data, 4); // insert in little endian order
break;
case SE_UInt64:
buffer.push_back(8);
buffer.push_back(0);
insert(buffer, data, 8);
break;
case SE_Float3:
buffer.push_back(12);
buffer.push_back(0);
insert(buffer, data->m_pVector, 12);
break;
}
insert(buffer, &paramNameHash, 4); // name hash
buffer.push_back(fieldData->GetType()); // data type
buffer.push_back(SosFieldTypeSize(fieldData->GetType())); // data size
buffer.push_back(0); // padding
insert(buffer, fieldData->GetData(), SosFieldTypeSize(fieldData->GetType())); // data
}

auto soundeventHash = MurmurHash2LowerCase(this->name.c_str(), SOUNDEVENT_NAME_HASH_SEED);
Expand All @@ -102,7 +78,6 @@ uint32_t Soundevent::Emit()

Soundevent::Soundevent()
{
this->clients = CRecipientFilter();
}

void Soundevent::SetName(std::string name)
Expand Down Expand Up @@ -169,9 +144,9 @@ SosField* Soundevent::GetField(std::string pszFieldName)
return (it != this->parameters.end()) ? &it->second : nullptr;
}

void Soundevent::AddOrReplaceField(std::string pszFieldName, const SosField* field)
void Soundevent::AddOrReplaceField(std::string pszFieldName, const SosField field)
{
this->parameters[pszFieldName] = *field;
this->parameters[pszFieldName] = field;
}

bool Soundevent::HasField(std::string pszFieldName)
Expand All @@ -181,85 +156,73 @@ bool Soundevent::HasField(std::string pszFieldName)

void Soundevent::SetBool(std::string pszFieldName, bool value)
{
CVariant data(value);
SET_FIELD(SE_Bool, data);
SET_FIELD(SE_Bool, value);
}

bool Soundevent::GetBool(std::string pszFieldName)
{
GETCHECK_FIELD(false);
CHECK_FIELD_TYPE(SE_Bool, false);

return field->data.Get<bool>();
RETURN_FIELD(bool);
}

void Soundevent::SetInt(std::string pszFieldName, int value)
{
CVariant data((int32)value);
SET_FIELD(SE_Int, data);
SET_FIELD(SE_Int, value);
}

int Soundevent::GetInt(std::string pszFieldName)
{
GETCHECK_FIELD(0);
CHECK_FIELD_TYPE(SE_Int, 0);

return (int)field->data.Get<int32>();
RETURN_FIELD(int);
}

void Soundevent::SetUInt32(std::string pszFieldName, uint32_t value)
{
CVariant data((uint32)value);
SET_FIELD(SE_UInt32, data);
SET_FIELD(SE_UInt32, value);
}

uint32_t Soundevent::GetUInt32(std::string pszFieldName)
{
GETCHECK_FIELD(0);
CHECK_FIELD_TYPE(SE_UInt32, 0);

return (uint32_t)field->data.Get<uint32>();
RETURN_FIELD(uint32_t);
}

void Soundevent::SetUInt64(std::string pszFieldName, uint64_t value)
{
CVariant data((uint64)value);
SET_FIELD(SE_UInt64, data);
SET_FIELD(SE_UInt64, value);
}

uint64_t Soundevent::GetUInt64(std::string pszFieldName)
{
GETCHECK_FIELD(0);
CHECK_FIELD_TYPE(SE_UInt64, 0);

return (uint64_t)field->data.Get<uint64>();
RETURN_FIELD(uint64_t);
}

void Soundevent::SetFloat(std::string pszFieldName, float value)
{
CVariant data(value);
SET_FIELD(SE_Float, data);
SET_FIELD(SE_Float, value);
}

float Soundevent::GetFloat(std::string pszFieldName)
{
GETCHECK_FIELD(0.0f);
CHECK_FIELD_TYPE(SE_Float, 0.0f);

return field->data.Get<float>();
RETURN_FIELD(float);
}

void Soundevent::SetFloat3(std::string pszFieldName, Vector& value)
{
CVariant data(value);
SET_FIELD(SE_Float3, data);
SET_FIELD(SE_Float3, value);
}

Vector Soundevent::GetFloat3(std::string pszFieldName)
{
Vector def;
Vector def(0, 0, 0);
GETCHECK_FIELD(def);
CHECK_FIELD_TYPE(SE_Float3, def);

return field->data.Get<Vector>();
RETURN_FIELD(Vector);
}
35 changes: 30 additions & 5 deletions src/network/soundevents/soundevent.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <string>
#include <map>
#include <vector>
#include <variant.h>
#include <public/bitvec.h>
#include <mathlib/vector.h>
#include <sdk/components/CSingleRecipientFilter.h>
Expand All @@ -25,20 +24,46 @@ enum SosFieldType {
SE_Float3 = 0xA
};

struct SosField {
constexpr uint8_t SosFieldTypeSize(SosFieldType t) {
switch (t) {
case SosFieldType::SE_Bool: return 1;
case SosFieldType::SE_Int: return 4;
case SosFieldType::SE_UInt32: return 4;
case SosFieldType::SE_UInt64: return 8;
case SosFieldType::SE_Float: return 4;
case SosFieldType::SE_Float3: return 12;
default: return 0;
}
}

class SosField {
private:
SosFieldType type;
CVariant data;
char data[32];

public:
SosField() {
// for container, never use this, i fell guilty
}
SosField(SosFieldType type, const void* src) {
int length = SosFieldTypeSize(type);
this->type = type;
memcpy(data, src, length);
}

SosFieldType GetType() const { return type; }
const void* GetData() const { return data; }
};

class Soundevent {
private:
std::string name;
int sourceEntityIndex = -1;
int sourceEntityIndex = -1; // if source entity index is -1, the sound will emit at recipient position
std::map<std::string, SosField> parameters;
CRecipientFilter clients;

SosField* GetField(std::string pszFieldName);
void AddOrReplaceField(std::string pszFieldName, const SosField* field);
void AddOrReplaceField(std::string pszFieldName, const SosField field);

public:
Soundevent();
Expand Down
Loading