Got rid of multithreading in GameMode

This commit is contained in:
2026-02-27 17:49:29 -05:00
parent 7bb4854844
commit 8825fe3a1e
2 changed files with 48 additions and 70 deletions

View File

@@ -19,57 +19,19 @@
using namespace LpxCommonTypes;
ALuprexGameModeBase::ALuprexGameModeBase()
{
}
ALuprexGameModeBase::ALuprexGameModeBase() {}
ALuprexGameModeBase::~ALuprexGameModeBase() {}
ALuprexGameModeBase::~ALuprexGameModeBase()
{
}
// This method runs in the background thread,
// at the moment we trigger it.
//
uint32 ALuprexGameModeBase::Run() {
FlxLockedWrapper lockedwrap;
if (lockedwrap->get_rescan_lua_source(lockedwrap.Get()))
{
drvutil::ostringstream srcpak;
FString LuprexRoot = FPaths::Combine(FPaths::ProjectDir(), TEXT("luprex"));
std::string srcpakerr = drvutil::package_lua_source(TCHAR_TO_UTF8(*LuprexRoot), &srcpak);
if (!srcpakerr.empty())
{
FString FMessage((const UTF8CHAR *)(srcpakerr.c_str()));
UE_LOG(LogLuprexIntegration, Error, TEXT("Trying to read Lua source: %s"), *FMessage);
}
else
{
std::string_view srcpakv = srcpak.view();
lockedwrap->play_access(lockedwrap.Get(), AccessKind::INVOKE_LUA_SOURCE, 0, srcpakv.size(), srcpakv.data(), nullptr, nullptr);
}
}
Sockets->Update(lockedwrap);
lockedwrap->play_update(lockedwrap.Get(), EngineSeconds);
Sockets->Update(lockedwrap);
return 0;
}
void ALuprexGameModeBase::ResetToInitialState()
{
Playing = false;
TickEnabled = true;
// Stop the tick functions.
FWorldDelegates::OnWorldPreActorTick.Remove(OnWorldPreActorTickHandle);
// Stop the tick function.
FWorldDelegates::OnWorldPostActorTick.Remove(OnWorldPostActorTickHandle);
OnWorldPreActorTickHandle.Reset();
OnWorldPostActorTickHandle.Reset();
// Shut down the thread
LuprexUpdateTask.Shutdown();
// Now that the thread's gone, we should be able to
// just claim and hold the lock on the wrapper.
FlxLockedWrapper w;
// Release and close all sockets.
@@ -172,25 +134,45 @@ void ALuprexGameModeBase::UpdatePossessedTangible() {
}
}
void ALuprexGameModeBase::OnWorldPreActorTick(UWorld* InWorld, ELevelTick InLevelTick, float deltaseconds)
{
if(Playing && TickEnabled && (GetWorld() == InWorld) && (InLevelTick == LEVELTICK_All))
void ALuprexGameModeBase::UpdateLuaSourceCode() {
FlxLockedWrapper lockedwrap;
if (lockedwrap->get_rescan_lua_source(lockedwrap.Get()))
{
LuprexUpdateTask.Wait();
EngineSeconds += deltaseconds;
UpdateConsoleOutput();
UpdateTangibles();
UpdatePossessedTangible();
UpdateLookAt();
drvutil::ostringstream srcpak;
FString LuprexRoot = FPaths::Combine(FPaths::ProjectDir(), TEXT("luprex"));
std::string srcpakerr = drvutil::package_lua_source(TCHAR_TO_UTF8(*LuprexRoot), &srcpak);
if (!srcpakerr.empty())
{
FString FMessage((const UTF8CHAR *)(srcpakerr.c_str()));
UE_LOG(LogLuprexIntegration, Error, TEXT("Trying to read Lua source: %s"), *FMessage);
}
else
{
std::string_view srcpakv = srcpak.view();
lockedwrap->play_access(lockedwrap.Get(), AccessKind::INVOKE_LUA_SOURCE, 0, srcpakv.size(), srcpakv.data(), nullptr, nullptr);
}
}
}
void ALuprexGameModeBase::UpdateSocketsAndLua()
{
FlxLockedWrapper lockedwrap;
Sockets->Update(lockedwrap);
lockedwrap->play_update(lockedwrap.Get(), EngineSeconds);
Sockets->Update(lockedwrap);
}
void ALuprexGameModeBase::OnWorldPostActorTick(UWorld* InWorld, ELevelTick InLevelTick, float deltaseconds)
{
if(Playing && TickEnabled && (GetWorld() == InWorld) && (InLevelTick == LEVELTICK_All))
{
LuprexUpdateTask.Trigger();
EngineSeconds += deltaseconds;
UpdateLuaSourceCode();
UpdateSocketsAndLua();
UpdateConsoleOutput();
UpdateTangibles();
UpdatePossessedTangible();
UpdateLookAt();
}
}
@@ -206,7 +188,6 @@ void ALuprexGameModeBase::InitializeGlobalState()
FlxLockedWrapper w;
// Sanity checks. Make sure everything is clean.
checkf(!LuprexUpdateTask.IsRunning(), TEXT("There should be no thread here."));
checkf(w->engine == nullptr, TEXT("There should be no engine here."));
// If we failed to initialize the wrapper, print an error message.
@@ -243,16 +224,14 @@ void ALuprexGameModeBase::InitializeGlobalState()
}
}
// If we successfully created a luprex engine, create a socket system and a worker thread.
// If we successfully created a luprex engine, create a socket system.
if (Playing) {
Sockets.Reset(FlxSockets::Create(w));
std::string error = Sockets->GetError();
check(error.empty());
LuprexUpdateTask.Startup(this);
}
if (Playing) {
OnWorldPreActorTickHandle = FWorldDelegates::OnWorldPreActorTick.AddUObject(this, &ALuprexGameModeBase::OnWorldPreActorTick);
OnWorldPostActorTickHandle = FWorldDelegates::OnWorldPostActorTick.AddUObject(this, &ALuprexGameModeBase::OnWorldPostActorTick);
}