From 82cbd4b849f67addb696c98389d9b4393786bf9e Mon Sep 17 00:00:00 2001 From: praydog Date: Fri, 17 May 2024 02:41:48 -0700 Subject: [PATCH] Lua: Only load scripts after scene is valid --- src/mods/ScriptRunner.cpp | 52 +++++++++++++++++++++++++++++++++++++++ src/mods/ScriptRunner.hpp | 3 +++ 2 files changed, 55 insertions(+) diff --git a/src/mods/ScriptRunner.cpp b/src/mods/ScriptRunner.cpp index da768d5f4..186224d3d 100644 --- a/src/mods/ScriptRunner.cpp +++ b/src/mods/ScriptRunner.cpp @@ -838,6 +838,58 @@ void ScriptRunner::hook_battle_rule() { } void ScriptRunner::on_frame() { + if (!m_scene_okay) try { + if (!m_checked_scene_once) { + m_checked_scene_once = true; + m_scene_check_time = std::chrono::system_clock::now(); + } + + // Just bail out of this if 5 seconds have passed and we still haven't found the scene or scene manager. + if (std::chrono::system_clock::now() - m_scene_check_time > std::chrono::seconds(5)) { + m_scene_okay = true; + spdlog::warn("[ScriptRunner] Scene or scene manager not found after 5 seconds. Loading scripts anyways..."); + return; + } + + const auto scene_manager_t = sdk::find_type_definition("via.SceneManager"); + if (scene_manager_t == nullptr) { + return; + } + + const auto get_CurrentScene = scene_manager_t->get_method("get_CurrentScene"); + + if (get_CurrentScene == nullptr) { + return; + } + + const auto scene_manager = sdk::get_native_singleton("via.SceneManager"); + + if (scene_manager == nullptr) { + return; + } + + const auto context = sdk::get_thread_context(); + + if (context == nullptr) { + return; + } + + const auto scene = get_CurrentScene->call_safe(context, scene_manager); + + if (scene == nullptr) { + return; + } + + m_scene_okay = true; + spdlog::info("[ScriptRunner] Scene and scene manager found. Loading scripts..."); + } catch (const std::exception& e) { + spdlog::error("[ScriptRunner] Error while checking for scene: {}", e.what()); + return; + } catch (...) { + spdlog::error("[ScriptRunner] Unknown error while checking for scene."); + return; + } + std::scoped_lock _{m_access_mutex}; hook_battle_rule(); diff --git a/src/mods/ScriptRunner.hpp b/src/mods/ScriptRunner.hpp index 68cfeff3e..81a07749e 100644 --- a/src/mods/ScriptRunner.hpp +++ b/src/mods/ScriptRunner.hpp @@ -338,6 +338,9 @@ class ScriptRunner : public Mod { std::shared_mutex m_script_error_mutex{}; std::chrono::system_clock::time_point m_last_script_error_time{}; + std::chrono::system_clock::time_point m_scene_check_time{}; + bool m_checked_scene_once{false}; + bool m_scene_okay{false}; bool m_console_spawned{false}; bool m_needs_first_reset{true}; bool m_last_online_match_state{false};