mirror of
https://github.com/Kelsidavis/WoWee.git
synced 2026-03-23 07:40:14 +00:00
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:
parent
5ea5588c14
commit
062cfd1e4a
9 changed files with 177 additions and 2 deletions
|
|
@ -19,6 +19,27 @@ bool TocFile::isLoadOnDemand() const {
|
|||
return (it != directives.end()) && it->second == "1";
|
||||
}
|
||||
|
||||
std::vector<std::string> TocFile::getSavedVariables() const {
|
||||
std::vector<std::string> result;
|
||||
auto it = directives.find("SavedVariables");
|
||||
if (it == directives.end()) return result;
|
||||
// Parse comma-separated variable names
|
||||
std::string val = it->second;
|
||||
size_t pos = 0;
|
||||
while (pos <= val.size()) {
|
||||
size_t comma = val.find(',', pos);
|
||||
std::string name = (comma != std::string::npos) ? val.substr(pos, comma - pos) : val.substr(pos);
|
||||
// Trim whitespace
|
||||
size_t start = name.find_first_not_of(" \t");
|
||||
size_t end = name.find_last_not_of(" \t");
|
||||
if (start != std::string::npos)
|
||||
result.push_back(name.substr(start, end - start + 1));
|
||||
if (comma == std::string::npos) break;
|
||||
pos = comma + 1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::optional<TocFile> parseTocFile(const std::string& tocPath) {
|
||||
std::ifstream f(tocPath);
|
||||
if (!f.is_open()) return std::nullopt;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue