Redoing some pool reading for better alignment

This commit is contained in:
Sergeanur 2020-05-13 05:31:14 +03:00
parent 4ac53960f5
commit 7aeac06fdb
2 changed files with 90 additions and 65 deletions

View File

@ -102,6 +102,9 @@ CPools::MakeSureSlotInObjectPoolIsEmpty(int32 slot)
} }
} }
#define CopyFromBuf(buf, data) memcpy(&data, buf, sizeof(data)); SkipSaveBuf(buf, sizeof(data));
#define CopyToBuf(buf, data) memcpy(buf, &data, sizeof(data)); SkipSaveBuf(buf, sizeof(data));
void CPools::LoadVehiclePool(uint8* buf, uint32 size) void CPools::LoadVehiclePool(uint8* buf, uint32 size)
{ {
INITSAVEBUF INITSAVEBUF
@ -266,10 +269,10 @@ INITSAVEBUF
if (pObject->ObjectCreatedBy == MISSION_OBJECT) if (pObject->ObjectCreatedBy == MISSION_OBJECT)
++nObjects; ++nObjects;
} }
*size = nObjects * (sizeof(int16) + sizeof(int) + sizeof(CCompressedMatrixNotAligned) + sizeof(uint32) + *size = nObjects * (sizeof(int16) + sizeof(int) + sizeof(CCompressedMatrix) +
sizeof(float) + sizeof(CCompressedMatrixNotAligned) + sizeof(uint32) + sizeof(int8) + 7 * sizeof(bool) + sizeof(float) + sizeof(float) + sizeof(CCompressedMatrix) + sizeof(int8) + 7 * sizeof(bool) + sizeof(float) +
sizeof(int8) + sizeof(int8) + sizeof(uint32) + 2 * sizeof(uint32)) + sizeof(int); sizeof(int8) + sizeof(int8) + sizeof(uint32) + 2 * sizeof(uint32)) + sizeof(int);
WriteSaveBuf(buf, nObjects); CopyToBuf(buf, nObjects);
for (int i = 0; i < nPoolSize; i++) { for (int i = 0; i < nPoolSize; i++) {
CObject* pObject = GetObjectPool()->GetSlot(i); CObject* pObject = GetObjectPool()->GetSlot(i);
if (!pObject) if (!pObject)
@ -282,33 +285,32 @@ INITSAVEBUF
bool bGlassBroken = pObject->bGlassBroken; bool bGlassBroken = pObject->bGlassBroken;
bool bHasBeenDamaged = pObject->bHasBeenDamaged; bool bHasBeenDamaged = pObject->bHasBeenDamaged;
bool bUseVehicleColours = pObject->bUseVehicleColours; bool bUseVehicleColours = pObject->bUseVehicleColours;
CCompressedMatrixNotAligned tmp; CCompressedMatrix tmp;
WriteSaveBuf(buf, pObject->GetModelIndex()); CopyToBuf(buf, pObject->m_modelIndex);
WriteSaveBuf(buf, GetObjectRef(pObject)); int32 ref = GetObjectRef(pObject);
CopyToBuf(buf, ref);
tmp.CompressFromFullMatrix(pObject->GetMatrix()); tmp.CompressFromFullMatrix(pObject->GetMatrix());
WriteSaveBuf(buf, tmp); CopyToBuf(buf, tmp);
WriteSaveBuf(buf, (uint32)0); // game writes ununitialized data here CopyToBuf(buf, pObject->m_fUprootLimit);
WriteSaveBuf(buf, pObject->m_fUprootLimit);
tmp.CompressFromFullMatrix(pObject->m_objectMatrix); tmp.CompressFromFullMatrix(pObject->m_objectMatrix);
WriteSaveBuf(buf, tmp); CopyToBuf(buf, tmp);
WriteSaveBuf(buf, (uint32)0); // same CopyToBuf(buf, pObject->ObjectCreatedBy);
WriteSaveBuf(buf, pObject->ObjectCreatedBy); CopyToBuf(buf, bIsPickup);
WriteSaveBuf(buf, bIsPickup); CopyToBuf(buf, bPickupObjWithMessage);
WriteSaveBuf(buf, bPickupObjWithMessage); CopyToBuf(buf, bOutOfStock);
WriteSaveBuf(buf, bOutOfStock); CopyToBuf(buf, bGlassCracked);
WriteSaveBuf(buf, bGlassCracked); CopyToBuf(buf, bGlassBroken);
WriteSaveBuf(buf, bGlassBroken); CopyToBuf(buf, bHasBeenDamaged);
WriteSaveBuf(buf, bHasBeenDamaged); CopyToBuf(buf, bUseVehicleColours);
WriteSaveBuf(buf, bUseVehicleColours); CopyToBuf(buf, pObject->m_fCollisionDamageMultiplier);
WriteSaveBuf(buf, pObject->m_fCollisionDamageMultiplier); CopyToBuf(buf, pObject->m_nCollisionDamageEffect);
WriteSaveBuf(buf, pObject->m_nCollisionDamageEffect); CopyToBuf(buf, pObject->m_nSpecialCollisionResponseCases);
WriteSaveBuf(buf, pObject->m_nSpecialCollisionResponseCases); CopyToBuf(buf, pObject->m_nEndOfLifeTime);
WriteSaveBuf(buf, pObject->m_nEndOfLifeTime);
#ifdef COMPATIBLE_SAVES #ifdef COMPATIBLE_SAVES
pObject->SaveEntityFlags(buf); pObject->SaveEntityFlags(buf);
#else #else
WriteSaveBuf(buf, (pObject->GetAddressOfEntityProperties())[0]); CopyToBuf(buf, (pObject->GetAddressOfEntityProperties())[0]);
WriteSaveBuf(buf, (pObject->GetAddressOfEntityProperties())[1]); CopyToBuf(buf, (pObject->GetAddressOfEntityProperties())[1]);
#endif #endif
} }
} }
@ -318,35 +320,44 @@ VALIDATESAVEBUF(*size)
void CPools::LoadObjectPool(uint8* buf, uint32 size) void CPools::LoadObjectPool(uint8* buf, uint32 size)
{ {
INITSAVEBUF INITSAVEBUF
int nObjects = ReadSaveBuf<int>(buf); int nObjects;
CopyFromBuf(buf, nObjects);
for (int i = 0; i < nObjects; i++) { for (int i = 0; i < nObjects; i++) {
int16 mi = ReadSaveBuf<int16>(buf); int16 mi;
int ref = ReadSaveBuf<int>(buf); CopyFromBuf(buf, mi);
int ref;
CopyFromBuf(buf, ref);
char* obuf = new char[sizeof(CObject)]; char* obuf = new char[sizeof(CObject)];
CObject* pBufferObject = (CObject*)obuf; CObject* pBufferObject = (CObject*)obuf;
CCompressedMatrixNotAligned tmp; CCompressedMatrix tmp;
tmp = ReadSaveBuf<CCompressedMatrixNotAligned>(buf); CopyFromBuf(buf, tmp);
tmp.DecompressIntoFullMatrix(pBufferObject->GetMatrix()); tmp.DecompressIntoFullMatrix(pBufferObject->GetMatrix());
ReadSaveBuf<uint32>(buf); CopyFromBuf(buf, pBufferObject->m_fUprootLimit);
pBufferObject->m_fUprootLimit = ReadSaveBuf<float>(buf); CopyFromBuf(buf, tmp);
tmp = ReadSaveBuf<CCompressedMatrixNotAligned>(buf);
tmp.DecompressIntoFullMatrix(pBufferObject->m_objectMatrix); tmp.DecompressIntoFullMatrix(pBufferObject->m_objectMatrix);
ReadSaveBuf<uint32>(buf); CopyFromBuf(buf, pBufferObject->ObjectCreatedBy);
pBufferObject->ObjectCreatedBy = ReadSaveBuf<int8>(buf); int8 bitFlag;
pBufferObject->bIsPickup = ReadSaveBuf<bool>(buf); CopyFromBuf(buf, bitFlag);
pBufferObject->bPickupObjWithMessage = ReadSaveBuf<bool>(buf); pBufferObject->bIsPickup = bitFlag;
pBufferObject->bOutOfStock = ReadSaveBuf<bool>(buf); CopyFromBuf(buf, bitFlag);
pBufferObject->bGlassCracked = ReadSaveBuf<bool>(buf); pBufferObject->bPickupObjWithMessage = bitFlag;
pBufferObject->bGlassBroken = ReadSaveBuf<bool>(buf); CopyFromBuf(buf, bitFlag);
pBufferObject->bHasBeenDamaged = ReadSaveBuf<bool>(buf); pBufferObject->bOutOfStock = bitFlag;
pBufferObject->bUseVehicleColours = ReadSaveBuf<bool>(buf); CopyFromBuf(buf, bitFlag);
pBufferObject->m_fCollisionDamageMultiplier = ReadSaveBuf<float>(buf); pBufferObject->bGlassCracked = bitFlag;
pBufferObject->m_nCollisionDamageEffect = ReadSaveBuf<uint8>(buf); CopyFromBuf(buf, bitFlag);
pBufferObject->m_nSpecialCollisionResponseCases = ReadSaveBuf<uint8>(buf); pBufferObject->bGlassBroken = bitFlag;
pBufferObject->m_nEndOfLifeTime = ReadSaveBuf<uint32>(buf); CopyFromBuf(buf, bitFlag);
pBufferObject->bHasBeenDamaged = bitFlag;
CopyFromBuf(buf, bitFlag);
pBufferObject->bUseVehicleColours = bitFlag;
CopyFromBuf(buf, pBufferObject->m_fCollisionDamageMultiplier);
CopyFromBuf(buf, pBufferObject->m_nCollisionDamageEffect);
CopyFromBuf(buf, pBufferObject->m_nSpecialCollisionResponseCases);
CopyFromBuf(buf, pBufferObject->m_nEndOfLifeTime);
#ifndef COMPATIBLE_SAVES #ifndef COMPATIBLE_SAVES
(pBufferObject->GetAddressOfEntityProperties())[0] = ReadSaveBuf<uint32>(buf); CopyFromBuf(buf, (pBufferObject->GetAddressOfEntityProperties())[0]);
(pBufferObject->GetAddressOfEntityProperties())[1] = ReadSaveBuf<uint32>(buf); CopyFromBuf(buf, (pBufferObject->GetAddressOfEntityProperties())[1]);
#endif #endif
if (GetObjectPool()->GetSlot(ref >> 8)) if (GetObjectPool()->GetSlot(ref >> 8))
CPopulation::ConvertToDummyObject(GetObjectPool()->GetSlot(ref >> 8)); CPopulation::ConvertToDummyObject(GetObjectPool()->GetSlot(ref >> 8));
@ -394,38 +405,45 @@ INITSAVEBUF
} }
*size = sizeof(int) + nNumPeds * (sizeof(uint32) + sizeof(int16) + sizeof(int) + CPlayerPed::nSaveStructSize + *size = sizeof(int) + nNumPeds * (sizeof(uint32) + sizeof(int16) + sizeof(int) + CPlayerPed::nSaveStructSize +
sizeof(CWanted::MaximumWantedLevel) + sizeof(CWanted::nMaximumWantedLevel) + MAX_MODEL_NAME); sizeof(CWanted::MaximumWantedLevel) + sizeof(CWanted::nMaximumWantedLevel) + MAX_MODEL_NAME);
WriteSaveBuf(buf, nNumPeds); CopyToBuf(buf, nNumPeds);
for (int i = 0; i < nPoolSize; i++) { for (int i = 0; i < nPoolSize; i++) {
CPed* pPed = GetPedPool()->GetSlot(i); CPed* pPed = GetPedPool()->GetSlot(i);
if (!pPed) if (!pPed)
continue; continue;
if (!pPed->bInVehicle && pPed->m_nPedType == PEDTYPE_PLAYER1) { if (!pPed->bInVehicle && pPed->m_nPedType == PEDTYPE_PLAYER1) {
WriteSaveBuf(buf, pPed->m_nPedType); CopyToBuf(buf, pPed->m_nPedType);
WriteSaveBuf(buf, pPed->GetModelIndex()); CopyToBuf(buf, pPed->m_modelIndex);
WriteSaveBuf(buf, GetPedRef(pPed)); int32 ref = GetPedRef(pPed);
CopyToBuf(buf, ref);
#ifdef COMPATIBLE_SAVES #ifdef COMPATIBLE_SAVES
pPed->Save(buf); pPed->Save(buf);
#else #else
memcpy(buf, pPed, sizeof(CPlayerPed)); memcpy(buf, pPed, sizeof(CPlayerPed));
SkipSaveBuf(buf, sizeof(CPlayerPed)); SkipSaveBuf(buf, sizeof(CPlayerPed));
#endif #endif
WriteSaveBuf(buf, CWanted::MaximumWantedLevel); CopyToBuf(buf, CWanted::MaximumWantedLevel);
WriteSaveBuf(buf, CWanted::nMaximumWantedLevel); CopyToBuf(buf, CWanted::nMaximumWantedLevel);
memcpy(buf, CModelInfo::GetModelInfo(pPed->GetModelIndex())->GetName(), MAX_MODEL_NAME); memcpy(buf, CModelInfo::GetModelInfo(pPed->GetModelIndex())->GetName(), MAX_MODEL_NAME);
SkipSaveBuf(buf, MAX_MODEL_NAME); SkipSaveBuf(buf, MAX_MODEL_NAME);
} }
} }
VALIDATESAVEBUF(*size); VALIDATESAVEBUF(*size);
#undef CopyToBuf
} }
void CPools::LoadPedPool(uint8* buf, uint32 size) void CPools::LoadPedPool(uint8* buf, uint32 size)
{ {
INITSAVEBUF INITSAVEBUF
int nPeds = ReadSaveBuf<int>(buf); int nPeds;
CopyFromBuf(buf, nPeds);
for (int i = 0; i < nPeds; i++) { for (int i = 0; i < nPeds; i++) {
uint32 pedtype = ReadSaveBuf<uint32>(buf); uint32 pedtype;
int16 model = ReadSaveBuf<int16>(buf); int16 model;
int ref = ReadSaveBuf<int>(buf); int ref;
CopyFromBuf(buf, pedtype);
CopyFromBuf(buf, model);
CopyFromBuf(buf, ref);
#ifdef COMPATIBLE_SAVES #ifdef COMPATIBLE_SAVES
CPed* pPed; CPed* pPed;
@ -443,8 +461,8 @@ INITSAVEBUF
pPed->Load(buf); pPed->Load(buf);
if (pedtype == PEDTYPE_PLAYER1) { if (pedtype == PEDTYPE_PLAYER1) {
CWanted::MaximumWantedLevel = ReadSaveBuf<int32>(buf); CopyFromBuf(buf, CWanted::MaximumWantedLevel);
CWanted::nMaximumWantedLevel = ReadSaveBuf<int32>(buf); CopyFromBuf(buf, CWanted::nMaximumWantedLevel);
SkipSaveBuf(buf, MAX_MODEL_NAME); SkipSaveBuf(buf, MAX_MODEL_NAME);
} }
@ -462,10 +480,9 @@ INITSAVEBUF
if (pedtype == PEDTYPE_PLAYER1) { // always true if (pedtype == PEDTYPE_PLAYER1) { // always true
memcpy(pbuf, buf, sizeof(CPlayerPed)); memcpy(pbuf, buf, sizeof(CPlayerPed));
SkipSaveBuf(buf, sizeof(CPlayerPed)); SkipSaveBuf(buf, sizeof(CPlayerPed));
CWanted::MaximumWantedLevel = ReadSaveBuf<int32>(buf); CopyFromBuf(buf, CWanted::MaximumWantedLevel);
CWanted::nMaximumWantedLevel = ReadSaveBuf<int32>(buf); CopyFromBuf(buf, CWanted::nMaximumWantedLevel);
memcpy(name, buf, MAX_MODEL_NAME); CopyFromBuf(buf, name);
SkipSaveBuf(buf, MAX_MODEL_NAME);
} }
CStreaming::RequestSpecialModel(model, name, STREAMFLAGS_DONT_REMOVE); CStreaming::RequestSpecialModel(model, name, STREAMFLAGS_DONT_REMOVE);
CStreaming::LoadAllRequestedModels(false); CStreaming::LoadAllRequestedModels(false);
@ -476,7 +493,7 @@ INITSAVEBUF
pPlayerPed->m_fMaxStamina = pBufferPlayer->m_fMaxStamina; pPlayerPed->m_fMaxStamina = pBufferPlayer->m_fMaxStamina;
pPed = pPlayerPed; pPed = pPlayerPed;
} }
pPed->GetPosition() = pBufferPlayer->GetPosition(); pPed->SetPosition(pBufferPlayer->GetPosition());
pPed->m_fHealth = pBufferPlayer->m_fHealth; pPed->m_fHealth = pBufferPlayer->m_fHealth;
pPed->m_fArmour = pBufferPlayer->m_fArmour; pPed->m_fArmour = pBufferPlayer->m_fArmour;
pPed->CharCreatedBy = pBufferPlayer->CharCreatedBy; pPed->CharCreatedBy = pBufferPlayer->CharCreatedBy;
@ -495,3 +512,6 @@ INITSAVEBUF
} }
VALIDATESAVEBUF(size) VALIDATESAVEBUF(size)
} }
#undef CopyFromBuf
#undef CopyToBuf

View File

@ -313,3 +313,8 @@ public:
other.Reorthogonalise(); other.Reorthogonalise();
} }
}; };
class CCompressedMatrix : public CCompressedMatrixNotAligned
{
int _alignment; // no clue what would this align to
};