Files
integration/Source/Integration/IntegrationGameModeBase.cpp

210 lines
5.2 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"
#include "lpx-drvutil.hpp"
#include "DebugPrint.h"
2023-09-02 01:33:11 -04:00
#include "TangibleManager.h"
2023-06-08 17:10:14 -04:00
#include <string>
#include <string_view>
2023-09-04 03:21:23 -04:00
using namespace DebugPrint;
2023-06-09 16:47:46 -04:00
AIntegrationGameModeBase::AIntegrationGameModeBase()
{
2023-06-23 12:45:23 -04:00
EngineSeconds = 0.0;
NextThreadTrigger = 1.0;
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-07-03 15:28:14 -04:00
SetActorTickInterval(0.0f);
2023-09-04 03:21:23 -04:00
DebugPrintControl::EnableCollection();
ResetToInitialState();
}
AIntegrationGameModeBase::~AIntegrationGameModeBase()
{
2023-06-23 16:27:23 -04:00
ResetToInitialState();
2023-06-08 17:10:14 -04:00
}
// This method runs in the background thread,
// at the moment we trigger it.
//
uint32 AIntegrationGameModeBase::Run() {
FLockedWrapper lockedwrap(LockableWrapper);
Sockets->Update(lockedwrap);
lockedwrap->play_invoke_event_update(lockedwrap.Get(), EngineSeconds);
Sockets->Update(lockedwrap);
return 0;
}
2023-06-23 16:27:23 -04:00
void AIntegrationGameModeBase::ResetToInitialState()
2023-06-09 16:47:46 -04:00
{
Playing = false;
// Shut down the thread
LuprexUpdateTask.Shutdown();
2023-06-23 16:27:23 -04:00
2023-09-03 03:40:44 -04:00
// Now that the thread's gone, we should be able to
// just claim and hold the lock on the wrapper.
FLockedWrapper w(LockableWrapper);
2023-07-03 15:28:14 -04:00
// Release and close all sockets.
2023-09-03 02:01:32 -04:00
if (Sockets != nullptr)
{
Sockets->ForceCloseEverything(w);
Sockets.Reset();
}
2023-07-03 15:28:14 -04:00
2023-06-23 16:27:23 -04:00
// Delete the engine.
2023-09-03 02:01:32 -04:00
if (w->release != nullptr)
2023-06-23 16:27:23 -04:00
{
2023-09-03 02:01:32 -04:00
w->release(w.Get());
2023-06-23 16:27:23 -04:00
}
// Reset the clocks.
EngineSeconds = 0;
NextThreadTrigger = 1.0;
}
void AIntegrationGameModeBase::UpdateConsoleOutput() {
// Copy Luprex Stdout into the console.
FLockedWrapper lockedwrap(LockableWrapper);
if (Playing) {
ConsoleOutput.Append(lockedwrap.FetchStdout());
}
2023-06-09 16:47:46 -04:00
// Copy Debugging Prints into the console.
2023-09-04 03:21:23 -04:00
TArray<FString> prints = DebugPrintControl::GetStored();
for (const FString& fs : prints) {
2023-06-08 17:10:14 -04:00
ConsoleOutput.AppendLine(fs);
}
// If the Console text has changed, update the widget.
if (ConsoleOutput.IsDirty()) {
2023-06-08 17:10:14 -04:00
ConsoleSetOutput(ConsoleOutput.Get());
ConsoleOutput.ClearDirty();
}
}
void AIntegrationGameModeBase::MaybeTriggerUpdateTask(float deltaseconds) {
if (!Playing) return;
FLockedWrapper lockedwrap(LockableWrapper);
if (lockedwrap->engine != nullptr)
2023-06-23 12:45:23 -04:00
{
EngineSeconds += deltaseconds;
if (EngineSeconds >= NextThreadTrigger)
{
LuprexUpdateTask.Trigger();
NextThreadTrigger += 0.05;
}
2023-06-23 12:45:23 -04:00
}
2023-06-08 17:10:14 -04:00
}
void AIntegrationGameModeBase::UpdateTangibles() {
if (!Playing) return;
FLockedWrapper w(LockableWrapper);
int64 actor = w.GetActor();
TangibleManager.SetActor(actor);
TangibleManager.SetNear(w.GetNear(actor, 100, 100, 100));
for (int64 id : TangibleManager.GetNear()) {
TangibleManager.MakeTangible(id);
}
}
2023-06-09 16:47:46 -04:00
void AIntegrationGameModeBase::ConsoleSendInput(const FString& fs)
{
2023-09-03 02:01:32 -04:00
FLockedWrapper w(LockableWrapper);
if (w->engine != nullptr)
2023-06-23 12:45:23 -04:00
{
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";
2023-09-03 02:01:32 -04:00
w->play_recv_incoming(w.Get(), 0, utf8.size(), utf8.c_str());
2023-06-08 17:10:14 -04:00
}
}
}
void AIntegrationGameModeBase::Tick(float deltaseconds)
{
Super::Tick(deltaseconds);
UpdateConsoleOutput();
UpdateTangibles();
MaybeTriggerUpdateTask(deltaseconds);
}
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-09-03 03:40:44 -04:00
// Make sure we're starting from a clean slate.
// Note: this claims the wrapper lock, so don't claim
// the lock before calling this.
ResetToInitialState();
// Now we're just going to claim the wrapper
// lock for the remainder. When we create the thread,
// the thread will hang until we release this lock.
2023-09-03 02:01:32 -04:00
FLockedWrapper w(LockableWrapper);
2023-09-03 03:40:44 -04:00
// Sanity checks. Make sure everything is clean.
checkf(!LuprexUpdateTask.IsRunning(), TEXT("There should be no thread here."));
2023-09-03 02:01:32 -04:00
checkf(w->engine == nullptr, TEXT("There should be no engine here."));
2023-06-23 12:45:23 -04:00
// Try to initialize the wrapper.
2023-09-03 02:01:32 -04:00
w.InitWrapper();
2023-06-23 16:27:23 -04:00
// If we failed to initialize the wrapper, print an error message.
2023-09-03 02:01:32 -04:00
if (w->play_initialize == nullptr)
2023-06-23 16:27:23 -04:00
{
2023-09-04 03:21:23 -04:00
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.
2023-09-03 02:01:32 -04:00
if (w->play_initialize != nullptr)
2023-06-23 12:45:23 -04:00
{
drvutil::ostringstream srcpak;
std::string srcpakerr = drvutil::package_lua_source("c:\\Luprex", &srcpak);
if (!srcpakerr.empty())
{
2023-09-04 03:21:23 -04:00
DPrint(srcpakerr.c_str());
2023-06-23 12:45:23 -04:00
}
std::string_view srcpakv = srcpak.view();
char* argv[1];
2023-07-03 15:28:14 -04:00
argv[0] = const_cast<char*>("lpxserver");
2023-09-03 02:01:32 -04:00
w->play_initialize(w.Get(), 1, argv, srcpakv.size(), srcpakv.data(), "");
if (w->error[0])
2023-06-23 12:45:23 -04:00
{
2023-09-04 03:21:23 -04:00
DPrint(w->error);
2023-06-23 12:45:23 -04:00
}
if (w->engine != nullptr) {
2023-09-04 03:21:23 -04:00
DPrint("Luprex initialize success");
Playing = true;
2023-06-23 12:45:23 -04:00
}
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.
if (Playing) {
2023-09-03 02:01:32 -04:00
Sockets.Reset(FLpxSockets::Create(w));
2023-07-03 15:28:14 -04:00
std::string error = Sockets->GetError();
check(error.empty());
LuprexUpdateTask.Startup(this);
2023-06-08 17:10:14 -04:00
}
2023-08-29 20:05:10 -04:00
// Initialize the tangible manager.
TangibleManager.Init(GetWorld(), ClassTangibleActor);
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
}