Skip to content

Commit

Permalink
refactor synclog caller relative addr and anandonproduction log
Browse files Browse the repository at this point in the history
  • Loading branch information
chaserli committed Jul 6, 2024
1 parent f62c650 commit 3d4fc26
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 27 deletions.
8 changes: 8 additions & 0 deletions src/Ext/Building/Body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ void BuildingExt::ExtData::UpdatePrimaryFactoryAI()
pBuilding->IsPrimaryFactory = false;

if (pBuilding->Factory)
{
auto const* prodType = pBuilding->Factory->Object->GetType();
pBuilding->Factory->AbandonProduction();
Debug::Log("%s is not CurrentAirFactory of %s, production of %s aborted\n", pBuilding->Type->ID, pOwner->PlainName, prodType->ID);
}
}
}

Expand Down Expand Up @@ -167,7 +171,11 @@ void BuildingExt::ExtData::UpdatePrimaryFactoryAI()
pBuilding->IsPrimaryFactory = false;

if (pBuilding->Factory)
{
auto const* prodType = pBuilding->Factory->Object->GetType();
pBuilding->Factory->AbandonProduction();
Debug::Log("%s of %s abandonded production of %s due to redundancies\n", pBuilding->Type->ID, pOwner->PlainName, prodType->ID);
}
}

return;
Expand Down
2 changes: 1 addition & 1 deletion src/Ext/Building/Hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ DEFINE_HOOK(0x4CA07A, FactoryClass_AbandonProduction_Phobos, 0x8)
GET_STACK(DWORD const, calledby, 0x18);

TechnoClass* pTechno = pFactory->Object;

if(calledby<0x7F0000)
// Replace the old log with this to figure out where keeps flushing the stream
Debug::LogGame("(%08x) : %s is abandoning production of %s[%s]\n",
calledby-5, pFactory->Owner->PlainName, pTechno->GetType()->Name, pTechno->get_ID());
Expand Down
63 changes: 37 additions & 26 deletions src/Misc/SyncLogging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,28 @@ SyncLogEventBuffer<TargetChangeSyncLogEvent, DestinationChanges_Size> SyncLogger
SyncLogEventBuffer<MissionOverrideSyncLogEvent, MissionOverrides_Size> SyncLogger::MissionOverrides;
SyncLogEventBuffer<AnimCreationSyncLogEvent, AnimCreations_Size> SyncLogger::AnimCreations;


void __forceinline MakeCallerRelative(unsigned int& caller)
{
// B for Bobos
if (caller > AresHelper::PhobosBaseAddress && caller < (AresHelper::PhobosBaseAddress + 0x100000))
caller = caller - AresHelper::PhobosBaseAddress + 0xB0000000;
// A for Ares
else if (caller > AresHelper::AresBaseAddress && caller < (AresHelper::AresBaseAddress + 0x100000))
caller = caller - AresHelper::AresBaseAddress + 0xA0000000;
}

void SyncLogger::AddRNGCallSyncLogEvent(Randomizer* pRandomizer, int type, unsigned int callerAddress, int min, int max)
{
MakeCallerRelative(callerAddress);
// Don't log non-critical RNG calls.
if (pRandomizer == &ScenarioClass::Instance->Random)
SyncLogger::RNGCalls.Add(RNGCallSyncLogEvent(type, true, pRandomizer->Next1, pRandomizer->Next2, callerAddress, Unsorted::CurrentFrame, min, max));
}

void SyncLogger::AddFacingChangeSyncLogEvent(unsigned short facing, unsigned int callerAddress)
{
MakeCallerRelative(callerAddress);
SyncLogger::FacingChanges.Add(FacingChangeSyncLogEvent(facing, callerAddress, Unsorted::CurrentFrame));
}

Expand All @@ -39,6 +52,7 @@ void SyncLogger::AddTargetChangeSyncLogEvent(AbstractClass* pObject, AbstractCla
if (!pObject)
return;

MakeCallerRelative(callerAddress);
auto targetRTTI = AbstractType::None;
unsigned int targetID = 0;

Expand All @@ -56,6 +70,7 @@ void SyncLogger::AddDestinationChangeSyncLogEvent(AbstractClass* pObject, Abstra
if (!pObject)
return;

MakeCallerRelative(callerAddress);
auto targetRTTI = AbstractType::None;
unsigned int targetID = 0;

Expand All @@ -73,6 +88,7 @@ void SyncLogger::AddMissionOverrideSyncLogEvent(AbstractClass* pObject, int miss
if (!pObject)
return;

MakeCallerRelative(callerAddress);
SyncLogger::MissionOverrides.Add(MissionOverrideSyncLogEvent(pObject->WhatAmI(), pObject->UniqueID, mission, callerAddress, Unsorted::CurrentFrame));
}

Expand All @@ -87,6 +103,7 @@ void SyncLogger::AddAnimCreationSyncLogEvent(const CoordStruct& coords, unsigned
if (coords.Z > SyncLogger::AnimCreations_HighestZ)
SyncLogger::AnimCreations_HighestZ = coords.Z;

MakeCallerRelative(callerAddress);
if (SyncLogger::AnimCreations.Add(AnimCreationSyncLogEvent(coords, callerAddress, Unsorted::CurrentFrame)))
{
SyncLogger::AnimCreations_HighestX = 0;
Expand Down Expand Up @@ -288,22 +305,10 @@ DEFINE_HOOK(0x64CD11, ExecuteDoList_WriteDesyncLog, 0x8)

// RNG call logging

DWORD __forceinline GetCallerAddress(REGISTERS* R)
{
GET_STACK(DWORD, caller, 0x0);
// B for Bobos
if (caller > AresHelper::PhobosBaseAddress && caller < (AresHelper::PhobosBaseAddress + 0x100000))
caller = caller - AresHelper::PhobosBaseAddress + 0xB0000000;
// A for Ares
else if (caller > AresHelper::AresBaseAddress && caller < (AresHelper::AresBaseAddress + 0x100000))
caller = caller - AresHelper::AresBaseAddress + 0xA0000000;
return caller;
}

DEFINE_HOOK(0x65C7D0, Random2Class_Random_SyncLog, 0x6)
{
GET(Randomizer*, pThis, ECX);
DWORD callerAddress = GetCallerAddress(R);
GET_STACK(unsigned int, callerAddress, 0x0);

SyncLogger::AddRNGCallSyncLogEvent(pThis, 1, callerAddress);

Expand All @@ -313,7 +318,7 @@ DEFINE_HOOK(0x65C7D0, Random2Class_Random_SyncLog, 0x6)
DEFINE_HOOK(0x65C88A, Random2Class_RandomRanged_SyncLog, 0x6)
{
GET(Randomizer*, pThis, EDX);
DWORD callerAddress = GetCallerAddress(R);
GET_STACK(unsigned int, callerAddress, 0x0);
GET_STACK(int, min, 0x4);
GET_STACK(int, max, 0x8);

Expand All @@ -327,7 +332,7 @@ DEFINE_HOOK(0x65C88A, Random2Class_RandomRanged_SyncLog, 0x6)
DEFINE_HOOK(0x4C9300, FacingClass_Set_SyncLog, 0x5)
{
GET_STACK(DirStruct*, facing, 0x4);
DWORD callerAddress = GetCallerAddress(R);
GET_STACK(unsigned int, callerAddress, 0x0);

SyncLogger::AddFacingChangeSyncLogEvent(facing->Raw, callerAddress);

Expand All @@ -340,7 +345,7 @@ DEFINE_HOOK(0x51B1F0, InfantryClass_AssignTarget_SyncLog, 0x5)
{
GET(InfantryClass*, pThis, ECX);
GET_STACK(AbstractClass*, pTarget, 0x4);
DWORD callerAddress = GetCallerAddress(R);
GET_STACK(unsigned int, callerAddress, 0x0);

SyncLogger::AddTargetChangeSyncLogEvent(pThis, pTarget, callerAddress);

Expand All @@ -351,7 +356,7 @@ DEFINE_HOOK(0x443B90, BuildingClass_AssignTarget_SyncLog, 0xB)
{
GET(BuildingClass*, pThis, ECX);
GET_STACK(AbstractClass*, pTarget, 0x4);
DWORD callerAddress = GetCallerAddress(R);
GET_STACK(unsigned int, callerAddress, 0x0);

SyncLogger::AddTargetChangeSyncLogEvent(pThis, pTarget, callerAddress);

Expand All @@ -362,7 +367,7 @@ DEFINE_HOOK(0x6FCDB0, TechnoClass_AssignTarget_SyncLog, 0x5)
{
GET(TechnoClass*, pThis, ECX);
GET_STACK(AbstractClass*, pTarget, 0x4);
DWORD callerAddress = GetCallerAddress(R);
GET_STACK(unsigned int, callerAddress, 0x0);

auto const RTTI = pThis->WhatAmI();

Expand All @@ -378,7 +383,7 @@ DEFINE_HOOK(0x41AA80, AircraftClass_AssignDestination_SyncLog, 0x7)
{
GET(AircraftClass*, pThis, ECX);
GET_STACK(AbstractClass*, pDest, 0x4);
DWORD callerAddress = GetCallerAddress(R);
GET_STACK(unsigned int, callerAddress, 0x0);

SyncLogger::AddDestinationChangeSyncLogEvent(pThis, pDest, callerAddress);

Expand All @@ -389,7 +394,7 @@ DEFINE_HOOK(0x455D50, BuildingClass_AssignDestination_SyncLog, 0xA)
{
GET(BuildingClass*, pThis, ECX);
GET_STACK(AbstractClass*, pDest, 0x4);
DWORD callerAddress = GetCallerAddress(R);
GET_STACK(unsigned int, callerAddress, 0x0);

SyncLogger::AddDestinationChangeSyncLogEvent(pThis, pDest, callerAddress);

Expand All @@ -400,7 +405,7 @@ DEFINE_HOOK(0x51AA40, InfantryClass_AssignDestination_SyncLog, 0x5)
{
GET(InfantryClass*, pThis, ECX);
GET_STACK(AbstractClass*, pDest, 0x4);
DWORD callerAddress = GetCallerAddress(R);
GET_STACK(unsigned int, callerAddress, 0x0);

SyncLogger::AddDestinationChangeSyncLogEvent(pThis, pDest, callerAddress);

Expand All @@ -411,7 +416,7 @@ DEFINE_HOOK(0x741970, UnitClass_AssignDestination_SyncLog, 0x6)
{
GET(UnitClass*, pThis, ECX);
GET_STACK(AbstractClass*, pDest, 0x4);
DWORD callerAddress = GetCallerAddress(R);
GET_STACK(unsigned int, callerAddress, 0x0);

SyncLogger::AddDestinationChangeSyncLogEvent(pThis, pDest, callerAddress);

Expand All @@ -424,7 +429,7 @@ DEFINE_HOOK(0x41BB30, AircraftClass_OverrideMission_SyncLog, 0x6)
{
GET(AircraftClass*, pThis, ECX);
GET_STACK(int, mission, 0x4);
DWORD callerAddress = GetCallerAddress(R);
GET_STACK(unsigned int, callerAddress, 0x0);

SyncLogger::AddMissionOverrideSyncLogEvent(pThis, mission, callerAddress);

Expand All @@ -435,7 +440,7 @@ DEFINE_HOOK(0x4D8F40, FootClass_OverrideMission_SyncLog, 0x5)
{
GET(FootClass*, pThis, ECX);
GET_STACK(int, mission, 0x4);
DWORD callerAddress = GetCallerAddress(R);
GET_STACK(unsigned int, callerAddress, 0x0);

SyncLogger::AddMissionOverrideSyncLogEvent(pThis, mission, callerAddress);

Expand All @@ -446,7 +451,7 @@ DEFINE_HOOK(0x7013A0, TechnoClass_OverrideMission_SyncLog, 0x5)
{
GET(TechnoClass*, pThis, ECX);
GET_STACK(int, mission, 0x4);
DWORD callerAddress = GetCallerAddress(R);
GET_STACK(unsigned int, callerAddress, 0x0);

if (pThis->WhatAmI() == AbstractType::Building)
SyncLogger::AddMissionOverrideSyncLogEvent(pThis, mission, callerAddress);
Expand All @@ -457,7 +462,13 @@ DEFINE_HOOK(0x7013A0, TechnoClass_OverrideMission_SyncLog, 0x5)
// Disable sync logging hooks in non-MP games
DEFINE_HOOK(0x683AB0, ScenarioClass_Start_DisableSyncLog, 0x6)
{
if (SessionClass::IsMultiplayer() || SyncLogger::HooksDisabled)
if (SessionClass::IsMultiplayer())
{
Patch::Apply_LJMP(0x55DBCD, 0x55DC99); // Disable MainLoop_SaveGame
return 0;
}

if (SyncLogger::HooksDisabled)
return 0;

SyncLogger::HooksDisabled = true;
Expand Down

0 comments on commit 3d4fc26

Please sign in to comment.