SkyPortal-plugin/Source/SkyPortal/Private/SkyPortalRunner.cpp

104 lines
2.8 KiB
C++
Raw Normal View History

2024-09-24 22:17:52 +00:00
#include "SkyPortalRunner.h"
2024-09-25 22:19:16 +00:00
#include "SkyPortalFigure.h"
2024-09-25 15:33:25 +00:00
#include "SkyPortalSubsystem.h"
2024-09-24 22:17:52 +00:00
2024-09-25 15:33:25 +00:00
FPortalStatusChecker::FPortalStatusChecker(UEngineSubsystem* InSubsystem, float InCheckInterval)
: SkyPortalSubsystemRef(InSubsystem), CheckInterval(InCheckInterval), bShouldRun(true)
2024-09-25 11:47:11 +00:00
{
}
bool FPortalStatusChecker::Init()
{
// Initialization logic, if necessary (e.g., logging)
return true;
}
uint32 FPortalStatusChecker::Run()
{
// Main loop of the thread, runs until Stop() is called
while (bShouldRun)
{
2024-09-27 12:18:20 +00:00
USkyPortalSubsystem* subref = Cast<USkyPortalSubsystem>(SkyPortalSubsystemRef);
if (subref && !(subref->bShouldPauseRunner)) {
// Check the portal status
CheckPortalStatus();
}
2024-09-25 11:47:11 +00:00
// Sleep for the specified interval
FPlatformProcess::Sleep(CheckInterval);
}
return 0; // Exit code for the thread
}
void FPortalStatusChecker::Stop()
{
// Signal the thread to stop running
bShouldRun = false;
}
void FPortalStatusChecker::Exit()
{
// Cleanup after the thread has stopped
}
void FPortalStatusChecker::CheckPortalStatus()
{
// Ensure the subsystem is valid
2024-09-25 15:33:25 +00:00
if (SkyPortalSubsystemRef && Cast<USkyPortalSubsystem>(SkyPortalSubsystemRef))
2024-09-25 11:47:11 +00:00
{
2024-09-25 22:19:16 +00:00
USkyPortalSubsystem* subref = Cast<USkyPortalSubsystem>(SkyPortalSubsystemRef);
USkyPortalIO* PortalHandleRef = subref->PortalHandle;
2024-09-29 09:36:40 +00:00
UE_LOG(LogSkyportalIO, VeryVerbose, TEXT("Check portal"));
2024-09-25 15:33:25 +00:00
if (PortalHandleRef && PortalHandleRef->bPortalReady)
{
// Call the subsystem function to get portal status
2024-09-25 22:19:16 +00:00
uint8* output = PortalHandleRef->Read();
EPortalCommand CommandResponse = GetPortalCommandFromChar(output[0]);
2024-09-29 09:36:40 +00:00
//UFigureData figData;
2024-09-25 22:19:16 +00:00
switch (CommandResponse)
{
case S:
CurrentStatusData = ParsePortalStatus(output);
2024-09-29 09:36:40 +00:00
UE_LOG(LogSkyportalIO, VeryVerbose, TEXT("Output Data: %s"), *BytesToHex(output, sizeof(output)));
2024-09-25 11:47:11 +00:00
2024-09-25 22:19:16 +00:00
//Send delegate when new informations are received
if (OldStatusData != CurrentStatusData) {
2024-09-26 11:10:47 +00:00
2024-09-25 22:19:16 +00:00
for (int i = 0; i < CurrentStatusData.StatusArray.Num(); i++) {
if (CurrentStatusData.StatusArray[i] != OldStatusData.StatusArray[i]) {
if (
//!FalsePositive() //filter conflicting infos
true) {
//FigureDataBlock FigureData;
switch (CurrentStatusData.StatusArray[i])
{
case EFigureStatus::NOT_PRESENT:
case EFigureStatus::PRESENT:
break;
case EFigureStatus::ADDED:
2024-09-26 23:39:33 +00:00
//figData = PortalHandleRef->ReadFigureBlocks(i);
subref->OnSkylanderAdded.Broadcast(00, i);
2024-09-26 11:10:47 +00:00
break;
2024-09-25 22:19:16 +00:00
case EFigureStatus::REMOVED:
subref->OnSkylanderRemoved.Broadcast(00, i);
2024-09-26 11:10:47 +00:00
break;
2024-09-25 22:19:16 +00:00
}
}
}
2024-09-26 11:10:47 +00:00
}
2024-09-25 22:19:16 +00:00
OldStatusData = CurrentStatusData;
subref->StatusData = CurrentStatusData;
}
break;
default:
return;
}
2024-09-25 11:47:11 +00:00
2024-09-25 15:33:25 +00:00
// Do something with the status (log, notify, etc.)
2024-09-29 09:36:40 +00:00
UE_LOG(LogSkyportalIO, VeryVerbose, TEXT("Portal Status:"));
2024-09-25 15:33:25 +00:00
}
2024-09-25 11:47:11 +00:00
}
}