Skip to content

Commit

Permalink
feat(ThemeManager): Allow adding paths to lua package.path
Browse files Browse the repository at this point in the history
  • Loading branch information
Alkorith committed Sep 12, 2023
1 parent 9da3d43 commit 15eabb1
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/Etterna/Singletons/ThemeManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
#include "Core/Services/Locator.hpp"
#include "Core/Platform/Platform.hpp"

#include <fmt/format.h>
#include "PrefsManager.h"

#include <deque>
#include <regex>
#include <algorithm>
#include <set>
#include <map>
Expand Down Expand Up @@ -504,6 +506,59 @@ ThemeManager::ClearSubscribers()
}
}

void
ThemeManager::AppendToLuaPackagePath(const std::string& path)
{

Lua* L = nullptr;
std::string packagePath;
// Get current package.path value
{
L = LUA->Get();
LuaHelpers::RunExpression(L, "package.path");
LuaHelpers::Pop(L, packagePath);
LUA->Release(L);
L = nullptr;
}

// Verify whether the path already exists in lua package path.
// If it does, leave function call early.
{
const std::regex path_regex(R"([^;]+)");
const auto end = std::sregex_iterator();
for (auto it = std::sregex_iterator(
packagePath.begin(), packagePath.end(), path_regex);
it != end;
++it) {
// Path already exists. No need to add.
if (it->str() == path) {
Locator::getLogger()->warn(
"Path \"{}\" already in lua package.path.", path.c_str());
return;
}
}
}

// Append new path to `package.path`
{
const std::string expression =
fmt::format("package.path = package.path .. ';{}'", path);
std::string error =
fmt::format("Lua runtime error parsing \"{}\"", expression);

L = LUA->Get();

Locator::getLogger()->info("Appending \"{}\" to lua package.path",
path.c_str());
LuaHelpers::RunScript(
L, expression, std::string("in"), error, 0, 1, true, false);

LuaHelpers::Pop(L, packagePath); // no op to clean up stack
LUA->Release(L);
L = nullptr;
}
}

void
ThemeManager::RunLuaScripts(const std::string& sMask, bool bUseThemeDir)
{
Expand Down
3 changes: 3 additions & 0 deletions src/Etterna/Singletons/ThemeManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ class ThemeManager
std::string m_sRealCurThemeName = "";
std::string m_sCurLanguage;
bool m_bPseudoLocalize;

private:
void AppendToLuaPackagePath(const std::string& path);
};

extern ThemeManager*
Expand Down

0 comments on commit 15eabb1

Please sign in to comment.