2014-06-10 13:56:35 -04:00
|
|
|
// Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.
|
|
|
|
|
|
|
|
|
|
#include "GameplayDebuggerPrivate.h"
|
|
|
|
|
#include "GameplayDebuggingComponent.h"
|
|
|
|
|
#include "GameplayDebuggingHUDComponent.h"
|
|
|
|
|
#include "GameplayDebuggingReplicator.h"
|
|
|
|
|
|
2014-06-11 09:13:09 -04:00
|
|
|
uint32 FGameplayDebuggerSettings::ShowFlagIndex = 0;
|
2014-06-10 13:56:35 -04:00
|
|
|
|
2014-06-25 09:28:40 -04:00
|
|
|
FGameplayDebuggerSettings GameplayDebuggerSettings(class AGameplayDebuggingReplicator* Replicator)
|
|
|
|
|
{
|
|
|
|
|
static uint32 Settings = (1 << EAIDebugDrawDataView::Basic) | (1 << EAIDebugDrawDataView::OverHead);
|
|
|
|
|
return FGameplayDebuggerSettings(Replicator == NULL ? Settings : Replicator->DebuggerShowFlags);
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-10 13:56:35 -04:00
|
|
|
class FGameplayDebugger : public IGameplayDebugger
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
// Begin IModuleInterface
|
|
|
|
|
virtual void StartupModule() override;
|
|
|
|
|
virtual void ShutdownModule() override;
|
|
|
|
|
// End IModuleInterface
|
|
|
|
|
|
|
|
|
|
void WorldAdded(UWorld* InWorld);
|
|
|
|
|
void WorldDestroyed(UWorld* InWorld);
|
2014-07-09 13:07:54 -04:00
|
|
|
#if WITH_EDITOR
|
2014-06-10 13:56:35 -04:00
|
|
|
void OnLevelActorAdded(AActor* InActor);
|
|
|
|
|
void OnLevelActorDeleted(AActor* InActor);
|
2014-07-09 13:07:54 -04:00
|
|
|
#endif
|
2014-07-22 16:23:09 -04:00
|
|
|
|
2014-08-07 17:34:29 -04:00
|
|
|
TArray<TWeakObjectPtr<AGameplayDebuggingReplicator> >& GetAllReplicators(UWorld* InWorld);
|
|
|
|
|
void AddReplicator(UWorld* InWorld, AGameplayDebuggingReplicator* InReplicator);
|
|
|
|
|
void RemoveReplicator(UWorld* InWorld, AGameplayDebuggingReplicator* InReplicator);
|
2014-07-22 16:23:09 -04:00
|
|
|
|
2014-08-07 17:34:29 -04:00
|
|
|
private:
|
|
|
|
|
virtual bool CreateGameplayDebuggerForPlayerController(APlayerController* PlayerController) override;
|
|
|
|
|
|
|
|
|
|
bool DoesGameplayDebuggingReplicatorExistForPlayerController(APlayerController* PlayerController);
|
|
|
|
|
|
|
|
|
|
TMap<TWeakObjectPtr<UWorld>, TArray<TWeakObjectPtr<AGameplayDebuggingReplicator> > > AllReplilcatorsPerWorlds;
|
2014-06-10 13:56:35 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
IMPLEMENT_MODULE(FGameplayDebugger, GameplayDebugger)
|
|
|
|
|
|
|
|
|
|
// This code will execute after your module is loaded into memory (but after global variables are initialized, of course.)
|
|
|
|
|
void FGameplayDebugger::StartupModule()
|
|
|
|
|
{
|
|
|
|
|
#if !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
|
|
|
|
|
if (GEngine)
|
|
|
|
|
{
|
|
|
|
|
GEngine->OnWorldAdded().AddRaw(this, &FGameplayDebugger::WorldAdded);
|
|
|
|
|
GEngine->OnWorldDestroyed().AddRaw(this, &FGameplayDebugger::WorldDestroyed);
|
2014-07-09 13:07:54 -04:00
|
|
|
#if WITH_EDITOR
|
2014-06-10 13:56:35 -04:00
|
|
|
GEngine->OnLevelActorAdded().AddRaw(this, &FGameplayDebugger::OnLevelActorAdded);
|
|
|
|
|
GEngine->OnLevelActorDeleted().AddRaw(this, &FGameplayDebugger::OnLevelActorDeleted);
|
2014-07-09 13:07:54 -04:00
|
|
|
#endif
|
2014-06-10 13:56:35 -04:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This function may be called during shutdown to clean up your module. For modules that support dynamic reloading,
|
|
|
|
|
// we call this function before unloading the module.
|
|
|
|
|
void FGameplayDebugger::ShutdownModule()
|
|
|
|
|
{
|
|
|
|
|
#if !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
|
|
|
|
|
if (GEngine)
|
|
|
|
|
{
|
|
|
|
|
GEngine->OnWorldAdded().RemoveAll(this);
|
|
|
|
|
GEngine->OnWorldDestroyed().RemoveAll(this);
|
2014-07-09 13:07:54 -04:00
|
|
|
#if WITH_EDITOR
|
2014-06-10 13:56:35 -04:00
|
|
|
GEngine->OnLevelActorAdded().RemoveAll(this);
|
|
|
|
|
GEngine->OnLevelActorDeleted().RemoveAll(this);
|
2014-07-09 13:07:54 -04:00
|
|
|
#endif
|
2014-06-10 13:56:35 -04:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-07 17:34:29 -04:00
|
|
|
bool FGameplayDebugger::DoesGameplayDebuggingReplicatorExistForPlayerController(APlayerController* PlayerController)
|
2014-07-22 16:23:09 -04:00
|
|
|
{
|
|
|
|
|
#if !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
|
|
|
|
|
if (PlayerController == NULL)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UWorld* World = PlayerController->GetWorld();
|
|
|
|
|
if (World == NULL)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-07 17:34:29 -04:00
|
|
|
for (auto It = GetAllReplicators(World).CreateConstIterator(); It; ++It)
|
2014-07-22 16:23:09 -04:00
|
|
|
{
|
2014-08-07 17:34:29 -04:00
|
|
|
TWeakObjectPtr<AGameplayDebuggingReplicator> Replicator = *It;
|
|
|
|
|
if (Replicator.IsValid())
|
2014-07-22 16:23:09 -04:00
|
|
|
{
|
|
|
|
|
if (Replicator->GetLocalPlayerOwner() == PlayerController)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-07 17:34:29 -04:00
|
|
|
bool FGameplayDebugger::CreateGameplayDebuggerForPlayerController(APlayerController* PlayerController)
|
2014-07-22 16:23:09 -04:00
|
|
|
{
|
|
|
|
|
#if !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
|
|
|
|
|
if (PlayerController == NULL)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool bIsServer = PlayerController->GetNetMode() < ENetMode::NM_Client; // (Only create on some sort of server)
|
|
|
|
|
if (!bIsServer)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UWorld* World = PlayerController->GetWorld();
|
|
|
|
|
if (World == NULL)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (DoesGameplayDebuggingReplicatorExistForPlayerController(PlayerController))
|
|
|
|
|
{
|
|
|
|
|
// No need to create one if we already have one.
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FActorSpawnParameters SpawnInfo;
|
|
|
|
|
SpawnInfo.bNoCollisionFail = true;
|
|
|
|
|
SpawnInfo.Name = *FString::Printf(TEXT("GameplayDebuggingReplicator_%s"), *PlayerController->GetName());
|
|
|
|
|
AGameplayDebuggingReplicator* DestActor = World->SpawnActor<AGameplayDebuggingReplicator>(SpawnInfo);
|
|
|
|
|
if (DestActor != NULL)
|
|
|
|
|
{
|
|
|
|
|
DestActor->SetLocalPlayerOwner(PlayerController);
|
|
|
|
|
DestActor->SetReplicates(true);
|
2014-08-07 17:34:29 -04:00
|
|
|
DestActor->SetAsGlobalInWorld(false);
|
|
|
|
|
AddReplicator(World, DestActor);
|
2014-07-22 16:23:09 -04:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return false;
|
2014-06-10 13:56:35 -04:00
|
|
|
}
|
|
|
|
|
|
2014-08-07 17:34:29 -04:00
|
|
|
TArray<TWeakObjectPtr<AGameplayDebuggingReplicator> >& FGameplayDebugger::GetAllReplicators(UWorld* InWorld)
|
|
|
|
|
{
|
|
|
|
|
return AllReplilcatorsPerWorlds.FindOrAdd(InWorld);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FGameplayDebugger::AddReplicator(UWorld* InWorld, AGameplayDebuggingReplicator* InReplicator)
|
|
|
|
|
{
|
|
|
|
|
GetAllReplicators(InWorld).Add(InReplicator);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FGameplayDebugger::RemoveReplicator(UWorld* InWorld, AGameplayDebuggingReplicator* InReplicator)
|
|
|
|
|
{
|
|
|
|
|
GetAllReplicators(InWorld).RemoveSwap(InReplicator);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FGameplayDebugger::WorldAdded(UWorld* InWorld)
|
|
|
|
|
{
|
|
|
|
|
bool bIsServer = InWorld && InWorld->GetNetMode() < ENetMode::NM_Client; // (Only server code)
|
|
|
|
|
if (!bIsServer)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (auto It = GetAllReplicators(InWorld).CreateConstIterator(); It; ++It)
|
|
|
|
|
{
|
|
|
|
|
TWeakObjectPtr<AGameplayDebuggingReplicator> Replicator = *It;
|
|
|
|
|
if (Replicator.IsValid() && Replicator->IsGlobalInWorld())
|
|
|
|
|
{
|
|
|
|
|
// Ok, we have global replicator on level
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// create global replicator on level
|
|
|
|
|
FActorSpawnParameters SpawnInfo;
|
|
|
|
|
SpawnInfo.bNoCollisionFail = true;
|
|
|
|
|
SpawnInfo.Name = *FString::Printf(TEXT("GameplayDebuggingReplicator_Global"));
|
|
|
|
|
AGameplayDebuggingReplicator* DestActor = InWorld->SpawnActor<AGameplayDebuggingReplicator>(SpawnInfo);
|
|
|
|
|
if (DestActor != NULL)
|
|
|
|
|
{
|
|
|
|
|
DestActor->SetLocalPlayerOwner(NULL);
|
|
|
|
|
DestActor->SetReplicates(false);
|
|
|
|
|
DestActor->SetActorTickEnabled(true);
|
|
|
|
|
DestActor->SetAsGlobalInWorld(true);
|
|
|
|
|
AddReplicator(InWorld, DestActor);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-10 13:56:35 -04:00
|
|
|
void FGameplayDebugger::WorldDestroyed(UWorld* InWorld)
|
|
|
|
|
{
|
2014-08-07 17:34:29 -04:00
|
|
|
bool bIsServer = InWorld && InWorld->GetNetMode() < ENetMode::NM_Client; // (Only work on server)
|
|
|
|
|
if (!bIsServer)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-06-10 13:56:35 -04:00
|
|
|
|
2014-08-07 17:34:29 -04:00
|
|
|
// remove global replicator from level
|
|
|
|
|
AllReplilcatorsPerWorlds.Remove(InWorld);
|
2014-06-10 13:56:35 -04:00
|
|
|
}
|
2014-07-09 13:07:54 -04:00
|
|
|
#if WITH_EDITOR
|
2014-06-10 13:56:35 -04:00
|
|
|
void FGameplayDebugger::OnLevelActorAdded(AActor* InActor)
|
|
|
|
|
{
|
2014-07-22 16:23:09 -04:00
|
|
|
// This function doesn't help much, because it's only called in EDITOR!
|
|
|
|
|
// We need a function that is called in the game! So instead of creating it automatically, I'm leaving it
|
|
|
|
|
// to be created explicitly by any player controller that needs to create it.
|
2014-08-07 17:34:29 -04:00
|
|
|
#if !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
|
|
|
|
|
APlayerController* PC = Cast<APlayerController>(InActor);
|
|
|
|
|
if (PC && PC->GetNetMode() < ENetMode::NM_Client)
|
|
|
|
|
{
|
|
|
|
|
CreateGameplayDebuggerForPlayerController(PC);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2014-06-10 13:56:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FGameplayDebugger::OnLevelActorDeleted(AActor* InActor)
|
|
|
|
|
{
|
2014-08-07 17:34:29 -04:00
|
|
|
#if !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
|
|
|
|
|
APlayerController* PC = Cast<APlayerController>(InActor);
|
|
|
|
|
if (!PC)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-06-10 13:56:35 -04:00
|
|
|
|
2014-08-07 17:34:29 -04:00
|
|
|
UWorld* World = PC->GetWorld();
|
|
|
|
|
if (!World)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (auto It = GetAllReplicators(World).CreateConstIterator(); It; ++It)
|
|
|
|
|
{
|
|
|
|
|
TWeakObjectPtr<AGameplayDebuggingReplicator> Replicator = *It;
|
|
|
|
|
if (Replicator != NULL)
|
|
|
|
|
{
|
|
|
|
|
if (Replicator->GetLocalPlayerOwner() == PC)
|
|
|
|
|
{
|
|
|
|
|
RemoveReplicator(World, Replicator.Get());
|
|
|
|
|
World->DestroyActor(Replicator.Get());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2014-06-10 13:56:35 -04:00
|
|
|
}
|
2014-07-09 13:07:54 -04:00
|
|
|
#endif
|