#include "script/script_engine.hpp" #include "script/scene_manager.hpp" #include "script/shader_manager.hpp" #include "script/gui_manager.hpp" #include "script/audio_manager.hpp" #include "script/lua_bindings.hpp" #include "app/audio_player.hpp" #include "logging/logger.hpp" #include #include #include #include #include namespace sdl3cpp::script { ScriptEngine::ScriptEngine(const std::filesystem::path& scriptPath, bool debugEnabled) : L_(luaL_newstate()), scriptDirectory_(scriptPath.parent_path()), debugEnabled_(debugEnabled), physicsBridge_(std::make_unique()), sceneManager_(std::make_unique(L_)), shaderManager_(std::make_unique(L_)), guiManager_(std::make_unique(L_)), audioManager_(std::make_unique(scriptDirectory_)) { TRACE_FUNCTION(); if (!L_) { LOG_ERROR("Failed to create Lua state"); throw std::runtime_error("Failed to create Lua state"); } LOG_DEBUG("Lua state created successfully"); luaL_openlibs(L_); LuaBindings::RegisterBindings(L_, this); lua_pushboolean(L_, debugEnabled_); lua_setglobal(L_, "lua_debug"); auto scriptDir = scriptPath.parent_path(); if (!scriptDir.empty()) { lua_getglobal(L_, "package"); if (lua_istable(L_, -1)) { lua_getfield(L_, -1, "path"); const char* currentPath = lua_tostring(L_, -1); std::string newPath = scriptDir.string() + "/?.lua;"; if (currentPath) { newPath += currentPath; } lua_pop(L_, 1); lua_pushstring(L_, newPath.c_str()); lua_setfield(L_, -2, "path"); } lua_pop(L_, 1); } if (luaL_dofile(L_, scriptPath.string().c_str()) != LUA_OK) { std::string message = sdl3cpp::script::GetLuaError(L_); lua_pop(L_, 1); lua_close(L_); L_ = nullptr; LOG_ERROR("Failed to load Lua script: " + message); throw std::runtime_error("Failed to load Lua script: " + message); } LOG_INFO("Lua script loaded successfully: " + scriptPath.string()); } ScriptEngine::~ScriptEngine() { if (L_) { lua_close(L_); } } std::vector ScriptEngine::LoadSceneObjects() { return sceneManager_->LoadSceneObjects(); } std::array ScriptEngine::ComputeModelMatrix(int functionRef, float time) { return sceneManager_->ComputeModelMatrix(functionRef, time); } std::array ScriptEngine::GetViewProjectionMatrix(float aspect) { return sceneManager_->GetViewProjectionMatrix(aspect); } std::unordered_map ScriptEngine::LoadShaderPathsMap() { return shaderManager_->LoadShaderPathsMap(); } PhysicsBridge& ScriptEngine::GetPhysicsBridge() { return *physicsBridge_; } std::vector ScriptEngine::LoadGuiCommands() { return guiManager_->LoadGuiCommands(); } void ScriptEngine::UpdateGuiInput(const GuiInputSnapshot& input) { guiManager_->UpdateGuiInput(input); } bool ScriptEngine::HasGuiCommands() const { return guiManager_->HasGuiCommands(); } std::filesystem::path ScriptEngine::GetScriptDirectory() const { return scriptDirectory_; } void ScriptEngine::SetAudioPlayer(app::AudioPlayer* audioPlayer) { audioManager_->SetAudioPlayer(audioPlayer); } bool ScriptEngine::QueueAudioCommand(AudioManager::AudioCommandType type, std::string path, bool loop, std::string& error) { return audioManager_->QueueAudioCommand(type, path, loop, error); } std::string ScriptEngine::GetLuaError() { const char* message = lua_tostring(L_, -1); return message ? message : "unknown lua error"; } } // namespace sdl3cpp::script