Skip to content

Commit 2d14641

Browse files
Copilotadriengivry
andauthored
Expose project folder as PROJECT_DIR Lua global instead of changing CWD
Agent-Logs-Url: https://github.com/Overload-Technologies/Overload/sessions/6068997a-0821-4ca8-a5a7-24a16f8a021d Co-authored-by: adriengivry <33324216+adriengivry@users.noreply.github.com>
1 parent 52e524f commit 2d14641

2 files changed

Lines changed: 4 additions & 22 deletions

File tree

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ 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;
3635
std::vector<std::reference_wrapper<OvCore::ECS::Components::Behaviour>> behaviours;
3736
uint32_t errorCount;
3837
};

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

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -342,16 +342,6 @@ 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-
355345
m_context.luaState = std::make_unique<sol::state>();
356346
m_context.luaState->open_libraries(
357347
sol::lib::base,
@@ -370,6 +360,10 @@ void OvCore::Scripting::LuaScriptEngine::CreateContext()
370360
callback(*m_context.luaState);
371361
}
372362

363+
// Expose the project folder as a global so Lua scripts can construct absolute paths
364+
// (e.g. io.open(PROJECT_DIR .. "/save.dat", "w")) without relying on the process CWD.
365+
m_context.luaState->set("PROJECT_DIR", m_context.projectAssetsPath.parent_path().string());
366+
373367
m_context.errorCount = 0;
374368

375369
std::for_each(m_context.behaviours.begin(), m_context.behaviours.end(),
@@ -401,15 +395,4 @@ void OvCore::Scripting::LuaScriptEngine::DestroyContext()
401395
);
402396

403397
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-
}
415398
}

0 commit comments

Comments
 (0)