Kelsidavis-WoWee/include/addons/lua_engine.hpp
Kelsi 062cfd1e4a feat: add SavedVariables persistence for Lua addons
Addons can now persist data across sessions using the standard WoW
SavedVariables pattern:

1. Declare in .toc: ## SavedVariables: MyAddonDB
2. Use the global in Lua: MyAddonDB = MyAddonDB or {default = true}
3. Data is automatically saved on logout and restored on next login

Implementation:
- TocFile::getSavedVariables() parses comma-separated variable names
- LuaEngine::loadSavedVariables() executes saved .lua file to restore globals
- LuaEngine::saveSavedVariables() serializes Lua tables/values to valid Lua
- Serializer handles tables (nested), strings, numbers, booleans, nil
- Save triggered on PLAYER_LEAVING_WORLD and AddonManager::shutdown()
- Files stored as <AddonDir>/<AddonName>.lua.saved

Updated HelloWorld addon to track login count across sessions.
2026-03-20 12:22:50 -07:00

53 lines
1.4 KiB
C++

#pragma once
#include <string>
#include <vector>
struct lua_State;
namespace wowee::game { class GameHandler; }
namespace wowee::addons {
class LuaEngine {
public:
LuaEngine();
~LuaEngine();
LuaEngine(const LuaEngine&) = delete;
LuaEngine& operator=(const LuaEngine&) = delete;
bool initialize();
void shutdown();
bool executeFile(const std::string& path);
bool executeString(const std::string& code);
void setGameHandler(game::GameHandler* handler);
// Fire a WoW event to all registered Lua handlers.
void fireEvent(const std::string& eventName,
const std::vector<std::string>& args = {});
// Try to dispatch a slash command via SlashCmdList. Returns true if handled.
bool dispatchSlashCommand(const std::string& command, const std::string& args);
// Call OnUpdate scripts on all frames that have one.
void dispatchOnUpdate(float elapsed);
// SavedVariables: load globals from file, save globals to file
bool loadSavedVariables(const std::string& path);
bool saveSavedVariables(const std::string& path, const std::vector<std::string>& varNames);
lua_State* getState() { return L_; }
bool isInitialized() const { return L_ != nullptr; }
private:
lua_State* L_ = nullptr;
game::GameHandler* gameHandler_ = nullptr;
void registerCoreAPI();
void registerEventAPI();
};
} // namespace wowee::addons