Fix for OP_ZoneEntry spawn struct sometimes containing garbage data in flags causing random crashes.

This commit is contained in:
KimLS 2024-11-16 22:04:44 -08:00
parent 018308bfca
commit b85344f779
3 changed files with 65 additions and 61 deletions

View File

@ -1603,6 +1603,8 @@ namespace Larion
buffer.WriteUInt8(emu->NPC);
structs::Spawn_Struct_Bitfields flags;
memset(&flags, 0, sizeof(structs::Spawn_Struct_Bitfields));
flags.gender = emu->gender;
flags.ispet = emu->is_pet;
flags.afk = emu->afk;
@ -1626,7 +1628,10 @@ namespace Larion
}
//write flags
buffer.WriteStructure(flags);
//buffer.WriteStructure(flags);
for (int j = 0; j < 5; ++j) {
buffer.WriteUInt8(flags.raw[j]);
}
/*
float EmitterScalingRadius;
@ -1868,6 +1873,7 @@ namespace Larion
//u8 CPhysicsData[20];
structs::Spawn_Struct_Position position;
memset(&position, 0, sizeof(structs::Spawn_Struct_Position));
position.y = emu->y;
position.deltaZ = emu->deltaZ;
@ -1879,7 +1885,10 @@ namespace Larion
position.animation = emu->animation;
position.deltaY = emu->deltaY;
buffer.WriteStructure(position);
//buffer.WriteStructure(position);
for (int j = 0; j < 5; ++j) {
buffer.WriteUInt32(position.raw[j]);
}
/*
if(Flags.title) {

View File

@ -193,6 +193,8 @@ namespace Larion {
struct Spawn_Struct_Bitfields
{
union {
struct {
// byte 1
/*00*/ unsigned gender : 2; // Gender (0=male, 1=female, 2=monster)
/*02*/ unsigned ispet : 1; // Guessed based on observing live spawns
@ -225,9 +227,14 @@ namespace Larion {
/*38*/ unsigned unk38 : 1;
/*39*/ unsigned unk39 : 1;
};
uint8 raw[5];
};
};
struct Spawn_Struct_Position
{
union {
struct {
signed y : 19;
signed deltaX : 13;
@ -246,6 +253,9 @@ namespace Larion {
signed x : 19;
unsigned pad4 : 13;
};
uint32_t raw[5];
};
};
#pragma pack()

View File

@ -181,21 +181,6 @@ public:
m_pos += len;
}
template<typename T>
void WriteStructurePtr(T *value) {
auto type_size = sizeof(T);
if (m_pos + type_size > m_capacity)
Grow(m_capacity + type_size);
memcpy(m_buffer + m_pos, value, type_size);
m_pos += sizeof(type_size);
}
template<typename T>
void WriteStructure(T& value) {
WriteStructurePtr(&value);
}
size_t size() const { return m_pos; }
size_t length() const { return size(); }
size_t capacity() const { return m_capacity; }