'NetSlime' Initial port finished and working with 2 players (at least)

This commit is contained in:
2024-04-23 18:54:17 -04:00
parent 2574960fff
commit 82a33a0f0f
25 changed files with 799 additions and 401 deletions

View File

@ -1,67 +1,94 @@
#include "GasaNetLibrary.h"
#include "GasaNetLibrary_Inlines.h"
#include "GasaObject.h"
#include "Kismet/KismetMathLibrary.h"
#include "Kismet/KismetSystemLibrary.h"
DEFINE_LOG_CATEGORY(LogGasaNet);
void NetLog( UObject const* Context, FString Message, EGasaVerbosity Verbosity = EGasaVerbosity::Log
, FLogCategoryBase& Category = LogGasaNet
, bool DumpStack = false
, int32 Line = __builtin_LINE()
, const ANSICHAR* File = __builtin_FILE()
, const ANSICHAR* Func = __builtin_FUNCTION() )
namespace Gasa
{
#if !UE_BUILD_SHIPPING && !NO_LOGGING
ELogVerbosity::Type EngineVerbosity = (ELogVerbosity::Type) Verbosity;
if ((EngineVerbosity & ELogVerbosity::VerbosityMask) > ELogVerbosity::COMPILED_IN_MINIMUM_VERBOSITY)
return;
if ((EngineVerbosity & ELogVerbosity::VerbosityMask) > Category.GetVerbosity())
return;
if ( Category.IsSuppressed(EngineVerbosity))
return;
AActor const* Actor = nullptr;
FString ActorLevel;
void DrawNetCullingSphere(const UObject* Context, float Duration, float Thickness)
{
if (Context != nullptr)
{
if (Context->GetClass()->IsChildOf(AActor::StaticClass()))
Actor = Cast<AActor>(Context);
else if (Context->GetClass()->IsChildOf(UActorComponent::StaticClass()))
Actor = Cast<UActorComponent>(Context)->GetOwner();
// Its assumed that all GasaObjects have an outer actor
else if (Context->IsA(UGasaObject::StaticClass()))
Actor = Cast<AActor>(Context->GetOuter());
}
if (Actor)
{
if (Actor->HasLocalNetOwner())
ActorLevel = TEXT("Net Owner");
else if (Actor->HasAuthority())
ActorLevel = TEXT("Server Authorized");
else
ActorLevel = TEXT("No Authority");
}
else
ActorLevel = TEXT("Local");
const AActor* actor = nullptr;
if (Context->IsA(UGasaObject::StaticClass()))
actor = Cast<AActor>(Context->GetOuter());
else if (Context->IsA(AActor::StaticClass()))
actor = Cast<AActor>(Context);
if (actor)
UKismetSystemLibrary::DrawDebugSphere(actor
, actor->GetActorLocation()
, UKismetMathLibrary::Sqrt(actor->NetCullDistanceSquared) * 2
, 12
, FLinearColor(FColor::Emerald)
, Duration
, Thickness);
}
void NetLog( UObject const* Context, FString Message, EGasaVerbosity Verbosity
, FLogCategoryBase& Category
, bool DumpStack
, int32 Line
, const ANSICHAR* File
, const ANSICHAR* Func )
{
#if !UE_BUILD_SHIPPING && !NO_LOGGING
ELogVerbosity::Type EngineVerbosity = (ELogVerbosity::Type) Verbosity;
if ((EngineVerbosity & ELogVerbosity::VerbosityMask) > ELogVerbosity::COMPILED_IN_MINIMUM_VERBOSITY)
return;
if ((EngineVerbosity & ELogVerbosity::VerbosityMask) > Category.GetVerbosity())
return;
if ( Category.IsSuppressed(EngineVerbosity))
return;
FString NetMode = FString::Printf(TEXT("%-16s"), * GetNetworkModeStr(Context));
ActorLevel = FString::Printf(TEXT("%-18s"), * ActorLevel);
FString Name = FString::Printf(TEXT("%-40s"), * Context->GetName());
FString FullMsg = NetMode + " " + ActorLevel + " " + Name + " : " + Message;
static UE::Logging::Private::FStaticBasicLogDynamicData LOG_Dynamic;
static UE::Logging::Private::FStaticBasicLogRecord
LOG_Static(TEXT("%s -- %hs %hs(%d)"), File, Line, EngineVerbosity, LOG_Dynamic);
AActor const* Actor = nullptr;
FString ActorLevel;
{
if (Context != nullptr)
{
if (Context->GetClass()->IsChildOf(AActor::StaticClass()))
Actor = Cast<AActor>(Context);
else if (Context->GetClass()->IsChildOf(UActorComponent::StaticClass()))
Actor = Cast<UActorComponent>(Context)->GetOwner();
// Its assumed that all GasaObjects have an outer actor
else if (Context->IsA(UGasaObject::StaticClass()))
Actor = Cast<AActor>(Context->GetOuter());
}
if (Actor)
{
if (Actor->HasLocalNetOwner())
ActorLevel = TEXT("Net Owner");
else if (Actor->HasAuthority())
ActorLevel = TEXT("Server Authorized");
else
ActorLevel = TEXT("No Authority");
}
else
ActorLevel = TEXT("Local");
}
SET_WARN_COLOR(COLOR_PURPLE)
FString NetMode = FString::Printf(TEXT("%-16s"), * GetNetworkModeStr(Context));
ActorLevel = FString::Printf(TEXT("%-18s"), * ActorLevel);
FString Name = FString::Printf(TEXT("%-40s"), * Context->GetName());
FString FullMsg = NetMode + " " + ActorLevel + " " + Name + " : " + Message;
if (DumpStack)
FDebug::DumpStackTraceToLog(EngineVerbosity);
BasicLog(Category, &LOG_Static, * FullMsg, File, Func, Line);
static UE::Logging::Private::FStaticBasicLogDynamicData LOG_Dynamic;
static UE::Logging::Private::FStaticBasicLogRecord
LOG_Static(TEXT("%s -- %hs %hs(%d)"), File, Line, EngineVerbosity, LOG_Dynamic);
// SET_WARN_COLOR(COLOR_PURPLE)
CLEAR_WARN_COLOR()
#endif
}
if (DumpStack)
FDebug::DumpStackTraceToLog(EngineVerbosity);
BasicLog(Category, &LOG_Static, * FullMsg, File, Func, Line);
// CLEAR_WARN_COLOR()
#endif
}
}

View File

@ -8,6 +8,34 @@
DECLARE_LOG_CATEGORY_EXTERN(LogGasaNet, Log, All);
#define NullGuard( Object_, Logger_, Message_ ) \
do { \
if ( ! IsValid(Object_) ) \
{ \
Logger_( (Message_) , ELogV::Error ); \
ensure( IsValid(Object_) ); \
return; \
} \
} while (0)
#define NetGuard( Condition_, Logger_, Message_ ) \
do { \
if ( Condition_ ) \
{ \
Logger_( (Message_) , ELogV::Error ); \
ensure( Condition_ ); \
return; \
} \
} while (0)
#if UE_BUILD_DEVELOPMENT
# define NullGuard_DEV NullGuard
# define NetGuard_DEV NetGuard
#else
# define NullGuard_DEV( Object_, Logger_, Message_)
# define NetGuard_DEV( Object_, Logger_, Message_)
#endif
UENUM(BlueprintType)
enum class ENetworkMode : uint8
{
@ -39,13 +67,11 @@ namespace Gasa
bool IsListenServer(UObject const* Context);
bool IsNetOwner(UObject const* Context);
bool IsNetOwner(UGasaObject const* Context);
bool IsNetOwner(AActor const* Context);
bool IsServer(UObject const* Context);
bool IsSimulatedProxy(UObject const* Context);
bool IsSimulatedProxy(UGasaObject const* Context);
bool IsSimulatedProxy(AActor const* Context);
void NetLog( UObject const* Context, FString Message, EGasaVerbosity Verbosity = EGasaVerbosity::Log
@ -56,6 +82,7 @@ namespace Gasa
, const ANSICHAR* Func = __builtin_FUNCTION() );
bool ServerAuthorized(UObject const* Context);
bool ServerAuthorized(UGasaObject const* Context);
bool ServerAuthorized(AActor const* Context);
}
#include "GasaNetLibrary_Inlines.h"

View File

@ -1,35 +1,12 @@
#include "GasaNetLibrary.h"
#include "GasaObject.h"
#pragma once
#include "GasaNetLibrary.h"
#include "Engine/NetDriver.h"
#include "Game/GasaGameMode.h"
#include "Kismet/KismetMathLibrary.h"
#include "Kismet/KismetSystemLibrary.h"
#include "Engine/World.h"
namespace Gasa
{
// TODO(Ed): Profile these...
inline
void DrawNetCullingSphere(const UObject* Context, float Duration, float Thickness)
{
const AActor* actor = nullptr;
if (Context->IsA(UGasaObject::StaticClass()))
actor = Cast<AActor>(Context->GetOuter());
else if (Context->IsA(AActor::StaticClass()))
actor = Cast<AActor>(Context);
if (actor)
UKismetSystemLibrary::DrawDebugSphere(actor
, actor->GetActorLocation()
, UKismetMathLibrary::Sqrt(actor->NetCullDistanceSquared) * 2
, 12
, FLinearColor(FColor::Emerald)
, Duration
, Thickness);
}
inline
ENetworkMode GetNetworkMode(UObject const* Context)
{
@ -110,8 +87,6 @@ namespace Gasa
else if (Context->GetClass()->IsChildOf(UActorComponent::StaticClass()))
Actor = Cast<UActorComponent>(Context)->GetOwner();
// Its assumed that all GasaObjects have an outer actor
else if (Context->IsA(UGasaObject::StaticClass()))
Actor = Cast<AActor>(Context->GetOuter());
else
{
UObject const* Outermost = Context->GetOutermostObject();
@ -127,23 +102,7 @@ namespace Gasa
bool Result = Actor->HasLocalNetOwner();
return Result;
}
inline
bool IsNetOwner(UGasaObject const* Context)
{
if (Context == nullptr || Context->GetWorld() == nullptr)
return false;
AActor const* Actor = Cast<AActor>(Context->GetOuter());
if (Actor == nullptr)
{
Log("Could not get actor reference", ELogV::Warning, LogGasaNet);
return false;
}
bool Result = Actor->HasLocalNetOwner();
return Result;
}
inline
bool IsNetOwner(AActor const* Actor)
{
@ -177,8 +136,6 @@ namespace Gasa
else if (Context->GetClass()->IsChildOf(UActorComponent::StaticClass()))
Actor = Cast<UActorComponent>(Context)->GetOwner();
// Its assumed that all GasaObjects have an outer actor
else if (Context->IsA(UGasaObject::StaticClass()))
Actor = Cast<AActor>(Context->GetOuter());
else
{
UObject const* Outermost = Context->GetOutermostObject();
@ -194,22 +151,6 @@ namespace Gasa
bool Result = Actor->GetLocalRole() == ENetRole::ROLE_SimulatedProxy;
return Result;
}
inline
bool IsSimulatedProxy(UGasaObject const* Context)
{
if (Context == nullptr || Context->GetWorld() == nullptr)
return false;
AActor const* Actor = Cast<AActor>(Context->GetOuter());
if (Actor == nullptr)
{
Log("Could not get actor reference", ELogV::Warning, LogGasaNet);
return false;
}
bool Result = Actor->GetLocalRole() == ENetRole::ROLE_SimulatedProxy;
return Result;
}
inline
bool IsSimulatedProxy(AActor const* Actor)
@ -233,8 +174,6 @@ namespace Gasa
else if (Context->GetClass()->IsChildOf(UActorComponent::StaticClass()))
Actor = Cast<UActorComponent>(Context)->GetOwner();
// Its assumed that all GasaObjects have an outer actor
else if (Context->IsA(UGasaObject::StaticClass()))
Actor = Cast<AActor>(Context->GetOuter());
else
{
UObject const* Outermost = Context->GetOutermostObject();
@ -251,22 +190,6 @@ namespace Gasa
return Result;
}
inline
bool ServerAuthorized(UGasaObject const* Context)
{
if (Context == nullptr || Context->GetWorld() == nullptr)
return false;
AActor const* Actor = Cast<AActor>(Context->GetOuter());
if (Actor == nullptr)
{
Log("Could not get actor reference", ELogV::Warning, LogGasaNet);
return false;
}
bool Result = Actor->HasAuthority();
return Result;
}
inline
bool ServerAuthorized(AActor const* Actor)
{