Skip to content

Commit 52e524f

Browse files
Copilotadriengivry
andauthored
Set Lua script working directory to project folder
Agent-Logs-Url: https://github.com/Overload-Technologies/Overload/sessions/4c11f4cc-ab40-403c-a6e6-ddcb941eb9b8 Co-authored-by: adriengivry <33324216+adriengivry@users.noreply.github.com>
1 parent 278c935 commit 52e524f

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

Sources/OvCore/include/OvCore/Scripting/Lua/LuaScriptEngine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ namespace OvCore::Scripting
3232
std::unique_ptr<sol::state> luaState;
3333
std::filesystem::path projectAssetsPath;
3434
std::filesystem::path engineAssetsPath;
35+
std::filesystem::path previousWorkingDirectory;
3536
std::vector<std::reference_wrapper<OvCore::ECS::Components::Behaviour>> behaviours;
3637
uint32_t errorCount;
3738
};

Sources/OvCore/src/OvCore/Scripting/Lua/LuaScriptEngine.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,16 @@ void OvCore::Scripting::LuaScriptEngine::CreateContext()
342342
{
343343
OVASSERT(m_context.luaState == nullptr, "A Lua context already exists!");
344344

345+
m_context.previousWorkingDirectory = std::filesystem::current_path();
346+
347+
std::error_code ec;
348+
std::filesystem::current_path(m_context.projectAssetsPath.parent_path(), ec);
349+
if (ec)
350+
{
351+
OVLOG_WARNING("Failed to set working directory to project folder: " + ec.message());
352+
m_context.previousWorkingDirectory.clear();
353+
}
354+
345355
m_context.luaState = std::make_unique<sol::state>();
346356
m_context.luaState->open_libraries(
347357
sol::lib::base,
@@ -391,4 +401,15 @@ void OvCore::Scripting::LuaScriptEngine::DestroyContext()
391401
);
392402

393403
m_context.luaState.reset();
404+
405+
if (!m_context.previousWorkingDirectory.empty())
406+
{
407+
std::error_code ec;
408+
std::filesystem::current_path(m_context.previousWorkingDirectory, ec);
409+
if (ec)
410+
{
411+
OVLOG_WARNING("Failed to restore working directory: " + ec.message());
412+
}
413+
m_context.previousWorkingDirectory.clear();
414+
}
394415
}

0 commit comments

Comments
 (0)