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.
This commit is contained in:
Kelsi 2026-03-20 12:22:50 -07:00
parent 5ea5588c14
commit 062cfd1e4a
9 changed files with 177 additions and 2 deletions

View file

@ -25,11 +25,14 @@ public:
LuaEngine* getLuaEngine() { return &luaEngine_; }
bool isInitialized() const { return luaEngine_.isInitialized(); }
void saveAllSavedVariables();
private:
LuaEngine luaEngine_;
std::vector<TocFile> addons_;
bool loadAddon(const TocFile& addon);
std::string getSavedVariablesPath(const TocFile& addon) const;
};
} // namespace wowee::addons

View file

@ -35,6 +35,10 @@ public:
// 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; }

View file

@ -17,6 +17,7 @@ struct TocFile {
std::string getTitle() const;
std::string getInterface() const;
bool isLoadOnDemand() const;
std::vector<std::string> getSavedVariables() const;
};
std::optional<TocFile> parseTocFile(const std::string& tocPath);