diff --git a/include/TLuaEngine.h b/include/TLuaEngine.h index ade2d25..63e5c3d 100644 --- a/include/TLuaEngine.h +++ b/include/TLuaEngine.h @@ -106,7 +106,7 @@ public: } static void WaitForAll(std::vector>& Results, - const std::chrono::high_resolution_clock::duration& Max = std::chrono::hours(std::numeric_limits().max())); + const std::optional& Max = std::nullopt); void ReportErrors(const std::vector>& Results); bool HasState(TLuaStateId StateId); [[nodiscard]] std::shared_ptr EnqueueScript(TLuaStateId StateID, const TLuaChunk& Script); diff --git a/src/TConsole.cpp b/src/TConsole.cpp index afb44ac..857941a 100644 --- a/src/TConsole.cpp +++ b/src/TConsole.cpp @@ -265,7 +265,7 @@ void TConsole::Command_Status(const std::string&) { void TConsole::RunAsCommand(const std::string& cmd, bool IgnoreNotACommand) { auto FutureIsNonNil = [](const std::shared_ptr& Future) { - if (!Future->Error) { + if (!Future->Error && Future->Result.valid()) { auto Type = Future->Result.get_type(); return Type != sol::type::lua_nil && Type != sol::type::none; } @@ -274,7 +274,7 @@ void TConsole::RunAsCommand(const std::string& cmd, bool IgnoreNotACommand) { std::vector> NonNilFutures; { // Futures scope auto Futures = mLuaEngine->TriggerEvent("onConsoleInput", "", cmd); - TLuaEngine::WaitForAll(Futures); + TLuaEngine::WaitForAll(Futures, std::chrono::seconds(5)); size_t Count = 0; for (auto& Future : Futures) { if (!Future->Error) { diff --git a/src/TLuaEngine.cpp b/src/TLuaEngine.cpp index f3bd977..f473b21 100644 --- a/src/TLuaEngine.cpp +++ b/src/TLuaEngine.cpp @@ -145,18 +145,21 @@ TLuaStateId TLuaEngine::GetStateIDForPlugin(const fs::path& PluginPath) { return ""; } -void TLuaEngine::WaitForAll(std::vector>& Results, const std::chrono::high_resolution_clock::duration& max) { - size_t ms = 0; - bool Cancelled = false; +void TLuaEngine::WaitForAll(std::vector>& Results, const std::optional& Max) { for (const auto& Result : Results) { + bool Cancelled = false; + size_t ms = 0; while (!Result->Ready && !Cancelled) { std::this_thread::sleep_for(std::chrono::milliseconds(10)); ms += 10; - if (std::chrono::milliseconds(ms) > max) { + if (Max.has_value() && std::chrono::milliseconds(ms) > Max.value()) { + beammp_trace("'" + Result->Function + "' in '" + Result->StateId + "' did not finish executing in time (took: " + std::to_string(ms) + "ms)"); Cancelled = true; } } - if (Result->Error) { + if (Cancelled) { + beammp_lua_warn("'" + Result->Function + "' in '" + Result->StateId + "' failed to execute in time and was not waited for. It may still finish executing at a later time."); + } else if (Result->Error) { if (Result->ErrorMessage != BeamMPFnNotFoundError) { beammp_lua_error(Result->Function + ": " + Result->ErrorMessage); }