feat: add Lua 5.1 addon system with .toc loader and /run command

Foundation for WoW-compatible addon support:

- Vendor Lua 5.1.5 source as a static library (extern/lua-5.1.5)
- TocParser: parses .toc files (## directives + file lists)
- LuaEngine: Lua 5.1 VM with sandboxed stdlib (no io/os/debug),
  WoW-compatible print() that outputs to chat, GetTime() stub
- AddonManager: scans Data/interface/AddOns/ for .toc files,
  loads .lua files on world entry, skips LoadOnDemand addons
- /run <code> slash command for inline Lua execution
- HelloWorld test addon that prints to chat on load

Integration: AddonManager initialized after asset manager, addons
loaded once on first world entry, reset on logout. XML frame
parsing is deferred to a future step.
This commit is contained in:
Kelsi 2026-03-20 11:12:07 -07:00
parent 52064eb438
commit 290e9bfbd8
115 changed files with 29035 additions and 2 deletions

View file

@ -0,0 +1,93 @@
#include "addons/addon_manager.hpp"
#include "core/logger.hpp"
#include <algorithm>
#include <filesystem>
namespace fs = std::filesystem;
namespace wowee::addons {
AddonManager::AddonManager() = default;
AddonManager::~AddonManager() { shutdown(); }
bool AddonManager::initialize(game::GameHandler* gameHandler) {
if (!luaEngine_.initialize()) return false;
luaEngine_.setGameHandler(gameHandler);
return true;
}
void AddonManager::scanAddons(const std::string& addonsPath) {
addons_.clear();
std::error_code ec;
if (!fs::is_directory(addonsPath, ec)) {
LOG_INFO("AddonManager: no AddOns directory at ", addonsPath);
return;
}
std::vector<fs::path> dirs;
for (const auto& entry : fs::directory_iterator(addonsPath, ec)) {
if (entry.is_directory()) dirs.push_back(entry.path());
}
// Sort alphabetically for deterministic load order
std::sort(dirs.begin(), dirs.end());
for (const auto& dir : dirs) {
std::string dirName = dir.filename().string();
std::string tocPath = (dir / (dirName + ".toc")).string();
auto toc = parseTocFile(tocPath);
if (!toc) continue;
if (toc->isLoadOnDemand()) {
LOG_DEBUG("AddonManager: skipping LoadOnDemand addon: ", dirName);
continue;
}
LOG_INFO("AddonManager: registered addon '", toc->getTitle(),
"' (", toc->files.size(), " files)");
addons_.push_back(std::move(*toc));
}
LOG_INFO("AddonManager: scanned ", addons_.size(), " addons");
}
void AddonManager::loadAllAddons() {
int loaded = 0, failed = 0;
for (const auto& addon : addons_) {
if (loadAddon(addon)) loaded++;
else failed++;
}
LOG_INFO("AddonManager: loaded ", loaded, " addons",
(failed > 0 ? (", " + std::to_string(failed) + " failed") : ""));
}
bool AddonManager::loadAddon(const TocFile& addon) {
bool success = true;
for (const auto& filename : addon.files) {
// For Step 1: only load .lua files, skip .xml (frame system not yet implemented)
std::string lower = filename;
for (char& c : lower) c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
if (lower.size() >= 4 && lower.substr(lower.size() - 4) == ".lua") {
std::string fullPath = addon.basePath + "/" + filename;
if (!luaEngine_.executeFile(fullPath)) {
success = false;
}
} else if (lower.size() >= 4 && lower.substr(lower.size() - 4) == ".xml") {
LOG_DEBUG("AddonManager: skipping XML file '", filename,
"' in addon '", addon.addonName, "' (XML frames not yet implemented)");
}
}
return success;
}
bool AddonManager::runScript(const std::string& code) {
return luaEngine_.executeString(code);
}
void AddonManager::shutdown() {
addons_.clear();
luaEngine_.shutdown();
}
} // namespace wowee::addons

173
src/addons/lua_engine.cpp Normal file
View file

@ -0,0 +1,173 @@
#include "addons/lua_engine.hpp"
#include "game/game_handler.hpp"
#include "core/logger.hpp"
extern "C" {
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
}
namespace wowee::addons {
// Retrieve GameHandler pointer stored in Lua registry
static game::GameHandler* getGameHandler(lua_State* L) {
lua_getfield(L, LUA_REGISTRYINDEX, "wowee_game_handler");
auto* gh = static_cast<game::GameHandler*>(lua_touserdata(L, -1));
lua_pop(L, 1);
return gh;
}
// WoW-compatible print() — outputs to chat window instead of stdout
static int lua_wow_print(lua_State* L) {
int nargs = lua_gettop(L);
std::string result;
for (int i = 1; i <= nargs; i++) {
if (i > 1) result += '\t';
// Lua 5.1: use lua_tostring (luaL_tolstring is 5.3+)
if (lua_isstring(L, i) || lua_isnumber(L, i)) {
const char* s = lua_tostring(L, i);
if (s) result += s;
} else if (lua_isboolean(L, i)) {
result += lua_toboolean(L, i) ? "true" : "false";
} else if (lua_isnil(L, i)) {
result += "nil";
} else {
result += lua_typename(L, lua_type(L, i));
}
}
auto* gh = getGameHandler(L);
if (gh) {
game::MessageChatData msg;
msg.type = game::ChatType::SYSTEM;
msg.language = game::ChatLanguage::UNIVERSAL;
msg.message = result;
gh->addLocalChatMessage(msg);
}
LOG_INFO("[Lua] ", result);
return 0;
}
// WoW-compatible message() — same as print for now
static int lua_wow_message(lua_State* L) {
return lua_wow_print(L);
}
// Stub for GetTime() — returns elapsed seconds
static int lua_wow_gettime(lua_State* L) {
static auto start = std::chrono::steady_clock::now();
auto now = std::chrono::steady_clock::now();
double elapsed = std::chrono::duration<double>(now - start).count();
lua_pushnumber(L, elapsed);
return 1;
}
LuaEngine::LuaEngine() = default;
LuaEngine::~LuaEngine() {
shutdown();
}
bool LuaEngine::initialize() {
if (L_) return true;
L_ = luaL_newstate();
if (!L_) {
LOG_ERROR("LuaEngine: failed to create Lua state");
return false;
}
// Open safe standard libraries (no io, os, debug, package)
luaopen_base(L_);
luaopen_table(L_);
luaopen_string(L_);
luaopen_math(L_);
// Remove unsafe globals from base library
const char* unsafeGlobals[] = {
"dofile", "loadfile", "load", "collectgarbage", "newproxy", nullptr
};
for (const char** g = unsafeGlobals; *g; ++g) {
lua_pushnil(L_);
lua_setglobal(L_, *g);
}
registerCoreAPI();
LOG_INFO("LuaEngine: initialized (Lua 5.1)");
return true;
}
void LuaEngine::shutdown() {
if (L_) {
lua_close(L_);
L_ = nullptr;
LOG_INFO("LuaEngine: shut down");
}
}
void LuaEngine::setGameHandler(game::GameHandler* handler) {
gameHandler_ = handler;
if (L_) {
lua_pushlightuserdata(L_, handler);
lua_setfield(L_, LUA_REGISTRYINDEX, "wowee_game_handler");
}
}
void LuaEngine::registerCoreAPI() {
// Override print() to go to chat
lua_pushcfunction(L_, lua_wow_print);
lua_setglobal(L_, "print");
// WoW API stubs
lua_pushcfunction(L_, lua_wow_message);
lua_setglobal(L_, "message");
lua_pushcfunction(L_, lua_wow_gettime);
lua_setglobal(L_, "GetTime");
}
bool LuaEngine::executeFile(const std::string& path) {
if (!L_) return false;
int err = luaL_dofile(L_, path.c_str());
if (err != 0) {
const char* errMsg = lua_tostring(L_, -1);
std::string msg = errMsg ? errMsg : "(unknown error)";
LOG_ERROR("LuaEngine: error loading '", path, "': ", msg);
if (gameHandler_) {
game::MessageChatData errChat;
errChat.type = game::ChatType::SYSTEM;
errChat.language = game::ChatLanguage::UNIVERSAL;
errChat.message = "|cffff4040[Lua Error] " + msg + "|r";
gameHandler_->addLocalChatMessage(errChat);
}
lua_pop(L_, 1);
return false;
}
return true;
}
bool LuaEngine::executeString(const std::string& code) {
if (!L_) return false;
int err = luaL_dostring(L_, code.c_str());
if (err != 0) {
const char* errMsg = lua_tostring(L_, -1);
std::string msg = errMsg ? errMsg : "(unknown error)";
LOG_ERROR("LuaEngine: script error: ", msg);
if (gameHandler_) {
game::MessageChatData errChat;
errChat.type = game::ChatType::SYSTEM;
errChat.language = game::ChatLanguage::UNIVERSAL;
errChat.message = "|cffff4040[Lua Error] " + msg + "|r";
gameHandler_->addLocalChatMessage(errChat);
}
lua_pop(L_, 1);
return false;
}
return true;
}
} // namespace wowee::addons

84
src/addons/toc_parser.cpp Normal file
View file

@ -0,0 +1,84 @@
#include "addons/toc_parser.hpp"
#include <fstream>
#include <algorithm>
namespace wowee::addons {
std::string TocFile::getTitle() const {
auto it = directives.find("Title");
return (it != directives.end()) ? it->second : addonName;
}
std::string TocFile::getInterface() const {
auto it = directives.find("Interface");
return (it != directives.end()) ? it->second : "";
}
bool TocFile::isLoadOnDemand() const {
auto it = directives.find("LoadOnDemand");
return (it != directives.end()) && it->second == "1";
}
std::optional<TocFile> parseTocFile(const std::string& tocPath) {
std::ifstream f(tocPath);
if (!f.is_open()) return std::nullopt;
TocFile toc;
toc.basePath = tocPath;
// Strip filename to get directory
size_t lastSlash = tocPath.find_last_of("/\\");
if (lastSlash != std::string::npos) {
toc.basePath = tocPath.substr(0, lastSlash);
toc.addonName = tocPath.substr(lastSlash + 1);
}
// Strip .toc extension from addon name
size_t dotPos = toc.addonName.rfind(".toc");
if (dotPos != std::string::npos) toc.addonName.resize(dotPos);
std::string line;
while (std::getline(f, line)) {
// Strip trailing CR (Windows line endings)
if (!line.empty() && line.back() == '\r') line.pop_back();
// Skip empty lines
if (line.empty()) continue;
// ## directives
if (line.size() >= 3 && line[0] == '#' && line[1] == '#') {
std::string directive = line.substr(2);
size_t colon = directive.find(':');
if (colon != std::string::npos) {
std::string key = directive.substr(0, colon);
std::string val = directive.substr(colon + 1);
// Trim whitespace
auto trim = [](std::string& s) {
size_t start = s.find_first_not_of(" \t");
size_t end = s.find_last_not_of(" \t");
s = (start == std::string::npos) ? "" : s.substr(start, end - start + 1);
};
trim(key);
trim(val);
if (!key.empty()) toc.directives[key] = val;
}
continue;
}
// Single # comment
if (line[0] == '#') continue;
// Whitespace-only line
size_t firstNonSpace = line.find_first_not_of(" \t");
if (firstNonSpace == std::string::npos) continue;
// File entry — normalize backslashes to forward slashes
std::string filename = line.substr(firstNonSpace);
size_t lastNonSpace = filename.find_last_not_of(" \t");
if (lastNonSpace != std::string::npos) filename.resize(lastNonSpace + 1);
std::replace(filename.begin(), filename.end(), '\\', '/');
toc.files.push_back(std::move(filename));
}
return toc;
}
} // namespace wowee::addons