Files
integration/Source/Integration/IntegrationGameModeBase.cpp

210 lines
5.1 KiB
C++
Raw Normal View History

2023-06-08 17:10:14 -04:00
// Copyright Epic Games, Inc. All Rights Reserved.
2023-06-09 14:36:47 -04:00
#include "IntegrationGameModeBase.h"
2023-06-08 17:10:14 -04:00
#include "drvutil.hpp"
#include "engineutil.hpp"
#include <string>
#include <string_view>
//// Main loop.
//while (!engw.get_stop_driver(&engw)) {
// handle_lua_source();
// handle_console_output();
// handle_new_outgoing_sockets();
// handle_socket_input_output();
// handle_console_input();
// handle_console_output();
// engw.play_invoke_event_update(&engw, drvutil::get_monotonic_clock());
//}
2023-06-09 16:47:46 -04:00
AIntegrationGameModeBase::AIntegrationGameModeBase()
{
Thread = nullptr;
ThreadStopRequested = false;
2023-06-23 12:45:23 -04:00
EngineSeconds = 0.0;
NextThreadTrigger = 1.0;
2023-06-23 16:27:23 -04:00
ThreadEvent = nullptr;
2023-06-08 17:10:14 -04:00
//PrimaryActorTick.bCanEverTick = true; // Probably wrong
//PrimaryActorTick.bTickEvenWhenPaused = true; // Probably wrong
//PrimaryActorTick.TickGroup = TG_PrePhysics; // Probably wrong
SetActorTickEnabled(true);
2023-06-23 12:45:23 -04:00
SetActorTickInterval(0.05f);
}
AIntegrationGameModeBase::~AIntegrationGameModeBase()
{
2023-06-23 16:27:23 -04:00
ResetToInitialState();
2023-06-08 17:10:14 -04:00
}
// Run routine called by the worker thread.
uint32 AIntegrationGameModeBase::Run()
{
2023-06-23 12:45:23 -04:00
while (true)
{
2023-06-23 12:45:23 -04:00
bool triggered = ThreadEvent->Wait(3000);
if (ThreadStopRequested) {
engineutil::DPrint("Thread stopping as requested");
break;
}
if (!triggered) {
engineutil::DPrint("Thread waiting a long time...");
continue;
}
engineutil::DPrint("Thread triggered.");
{
FScopeLock lk(&LuprexMutex);
2023-06-23 16:27:23 -04:00
Sockets->Update();
2023-06-23 12:45:23 -04:00
Luprex.play_invoke_event_update(&Luprex, EngineSeconds);
}
}
return 0;
}
2023-06-23 16:27:23 -04:00
void AIntegrationGameModeBase::ResetToInitialState()
2023-06-09 16:47:46 -04:00
{
2023-06-23 16:27:23 -04:00
// Shut down the thread and release the ThreadEvent
if (Thread != nullptr)
{
ThreadStopRequested = true;
ThreadEvent->Trigger();
delete Thread; // This waits for the thread to complete.
Thread = nullptr;
FPlatformProcess::ReturnSynchEventToPool(ThreadEvent);
ThreadEvent = nullptr;
}
ThreadStopRequested = false;
// Delete the engine.
if (Luprex.release != nullptr)
{
Luprex.release(&Luprex);
}
// Release and close all sockets.
Sockets.Reset();
// Reset the clocks.
EngineSeconds = 0;
NextThreadTrigger = 1.0;
}
void AIntegrationGameModeBase::HandleLuprexConsoleOutput()
{
uint32_t ndata; const char* data;
Luprex.get_outgoing(&Luprex, 0, &ndata, &data);
if (ndata == 0) return;
std::string_view src(data, ndata);
int consumed;
std::u16string cps = drvutil::utf8_to_ucs2(src, &consumed);
Luprex.play_sent_outgoing(&Luprex, 0, consumed);
FString fs(cps.size(), (const UCS2CHAR*)(&cps[0]));
ConsoleOutput.Append(fs);
2023-06-09 16:47:46 -04:00
}
void AIntegrationGameModeBase::Tick(float DeltaSeconds)
{
2023-06-08 17:10:14 -04:00
Super::Tick(DeltaSeconds);
2023-06-23 12:45:23 -04:00
{
FScopeLock lk(&LuprexMutex);
2023-06-23 16:27:23 -04:00
if (Luprex.engine != nullptr)
{
EngineSeconds += DeltaSeconds;
HandleLuprexConsoleOutput();
}
2023-06-08 17:10:14 -04:00
}
TArray<FString> prints = engineutil::DPrintGetStored();
for (const FString& fs : prints) {
2023-06-08 17:10:14 -04:00
ConsoleOutput.AppendLine(fs);
}
2023-06-23 12:45:23 -04:00
if (ConsoleOutput.IsDirty())
{
2023-06-08 17:10:14 -04:00
ConsoleSetOutput(ConsoleOutput.Get());
ConsoleOutput.ClearDirty();
}
2023-06-23 12:45:23 -04:00
if ((Thread != nullptr) && (EngineSeconds >= NextThreadTrigger))
{
ThreadEvent->Trigger();
NextThreadTrigger += 1.0;
}
2023-06-08 17:10:14 -04:00
}
2023-06-09 16:47:46 -04:00
void AIntegrationGameModeBase::ConsoleSendInput(const FString& fs)
{
2023-06-23 12:45:23 -04:00
if (Luprex.engine != nullptr)
{
FScopeLock lk(&LuprexMutex);
2023-06-08 17:10:14 -04:00
const TCHAR* fstchar = *fs;
2023-06-23 12:45:23 -04:00
if (sizeof(TCHAR) == 2)
{
2023-06-08 17:10:14 -04:00
ConsoleOutput.AppendLine(FString("> ") + fs);
std::u16string_view fsview((const char16_t*)fstchar, fs.Len());
std::string utf8 = drvutil::utf16_to_utf8(fsview);
utf8 = utf8 + "\n";
Luprex.play_recv_incoming(&Luprex, 0, utf8.size(), utf8.c_str());
}
}
}
2023-06-09 16:47:46 -04:00
void AIntegrationGameModeBase::BeginPlay()
{
2023-06-08 17:10:14 -04:00
Super::BeginPlay();
2023-06-23 12:45:23 -04:00
2023-06-23 16:27:23 -04:00
// Sanity checks.
2023-06-23 12:45:23 -04:00
checkf(Thread == nullptr, TEXT("There should be no thread here."));
2023-06-23 16:27:23 -04:00
checkf(Luprex.engine == nullptr, TEXT("There should be no engine here."));
2023-06-23 12:45:23 -04:00
2023-06-23 16:27:23 -04:00
// Make sure we're starting from a clean slate.
ResetToInitialState();
2023-06-23 12:45:23 -04:00
// Try to initialize the wrapper.
if (Luprex.play_initialize == nullptr)
{
2023-06-08 17:10:14 -04:00
engineutil::init_wrapper(&Luprex);
2023-06-23 16:27:23 -04:00
}
// If we failed to initialize the wrapper, print an error message.
if (Luprex.play_initialize == nullptr)
{
engineutil::DPrint("Luprex wrapper initialization failed");
2023-06-08 17:10:14 -04:00
}
2023-06-23 12:45:23 -04:00
// If wrapper is initialized, try to initialize the luprex engine.
if (Luprex.play_initialize != nullptr)
{
drvutil::ostringstream srcpak;
std::string srcpakerr = drvutil::package_lua_source("c:\\Luprex", &srcpak);
if (!srcpakerr.empty())
{
engineutil::DPrint(srcpakerr.c_str());
}
std::string_view srcpakv = srcpak.view();
char* argv[1];
argv[0] = const_cast<char*>("lpxserver");
Luprex.play_initialize(&Luprex, 1, argv, srcpakv.size(), srcpakv.data(), "");
if (Luprex.error[0])
{
engineutil::DPrint(Luprex.error);
}
else
{
engineutil::DPrint("Luprex initialize success");
}
2023-06-08 17:10:14 -04:00
}
2023-06-23 12:45:23 -04:00
2023-06-23 16:27:23 -04:00
// If we successfully created a luprex engine, create a socket system and a worker thread.
2023-06-23 12:45:23 -04:00
if (Luprex.engine != nullptr)
{
2023-06-23 16:27:23 -04:00
Sockets.Reset(FLpxSockets::Create(&Luprex));
ThreadEvent = FPlatformProcess::GetSynchEventFromPool(false);
2023-06-23 12:45:23 -04:00
Thread = FRunnableThread::Create(this, TEXT("Worker Thread"));
2023-06-08 17:10:14 -04:00
}
}
2023-06-23 12:45:23 -04:00
void AIntegrationGameModeBase::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
2023-06-23 16:27:23 -04:00
ResetToInitialState();
2023-06-09 16:47:46 -04:00
}