1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
#include "storage.hpp"
#include <filesystem>
#include <fstream>
#include <components/debug/debuglog.hpp>
#include "luastate.hpp"
namespace sol
{
template <>
struct is_automagical<LuaUtil::LuaStorage::SectionView> : std::false_type
{
};
}
namespace LuaUtil
{
LuaStorage::Value LuaStorage::Section::sEmpty;
void LuaStorage::registerLifeTime(LuaUtil::LuaView& view, sol::table& res)
{
res["LIFE_TIME"] = LuaUtil::makeStrictReadOnly(tableFromPairs<std::string_view, Section::LifeTime>(view.sol(),
{
{ "Persistent", Section::LifeTime::Persistent },
{ "GameSession", Section::LifeTime::GameSession },
{ "Temporary", Section::LifeTime::Temporary },
}));
}
sol::object LuaStorage::Value::getCopy(lua_State* L) const
{
return deserialize(L, mSerializedValue);
}
sol::object LuaStorage::Value::getReadOnly(lua_State* L) const
{
if (mReadOnlyValue == sol::nil && !mSerializedValue.empty())
mReadOnlyValue = sol::main_object(deserialize(L, mSerializedValue, nullptr, true));
return mReadOnlyValue;
}
const LuaStorage::Value& LuaStorage::Section::get(std::string_view key) const
{
checkIfActive();
auto it = mValues.find(key);
if (it != mValues.end())
return it->second;
else
return sEmpty;
}
void LuaStorage::Section::runCallbacks(sol::optional<std::string_view> changedKey)
{
mStorage->mRunningCallbacks.insert(this);
mCallbacks.erase(std::remove_if(mCallbacks.begin(), mCallbacks.end(),
[&](const Callback& callback) {
bool valid = callback.isValid();
if (valid)
callback.tryCall(mSectionName, changedKey);
return !valid;
}),
mCallbacks.end());
mMenuScriptsCallbacks.erase(std::remove_if(mMenuScriptsCallbacks.begin(), mMenuScriptsCallbacks.end(),
[&](const Callback& callback) {
bool valid = callback.isValid();
if (valid)
callback.tryCall(mSectionName, changedKey);
return !valid;
}),
mMenuScriptsCallbacks.end());
mStorage->mRunningCallbacks.erase(this);
}
void LuaStorage::Section::throwIfCallbackRecursionIsTooDeep()
{
if (mStorage->mRunningCallbacks.count(this) > 0)
throw std::runtime_error(
"Storage handler shouldn't change the storage section it handles (leads to an infinite recursion)");
if (mStorage->mRunningCallbacks.size() > 10)
throw std::runtime_error(
"Too many subscribe callbacks triggering in a chain, likely an infinite recursion");
}
void LuaStorage::Section::set(std::string_view key, const sol::object& value)
{
checkIfActive();
throwIfCallbackRecursionIsTooDeep();
if (value != sol::nil)
mValues[std::string(key)] = Value(value);
else
{
auto it = mValues.find(key);
if (it != mValues.end())
mValues.erase(it);
}
if (mStorage->mListener)
mStorage->mListener->valueChanged(mSectionName, key, value);
runCallbacks(key);
}
void LuaStorage::Section::setAll(const sol::optional<sol::table>& values)
{
checkIfActive();
throwIfCallbackRecursionIsTooDeep();
mValues.clear();
if (values)
{
for (const auto& [k, v] : *values)
mValues[cast<std::string>(k)] = Value(v);
}
if (mStorage->mListener)
mStorage->mListener->sectionReplaced(mSectionName, values);
runCallbacks(sol::nullopt);
}
sol::table LuaStorage::Section::asTable(lua_State* L)
{
checkIfActive();
sol::table res(L, sol::create);
for (const auto& [k, v] : mValues)
res[k] = v.getCopy(L);
return res;
}
void LuaStorage::initLuaBindings(LuaUtil::LuaView& view)
{
sol::usertype<SectionView> sview = view.sol().new_usertype<SectionView>("Section");
sview["get"] = [](sol::this_state s, const SectionView& section, std::string_view key) {
return section.mSection->get(key).getReadOnly(s);
};
sview["getCopy"] = [](sol::this_state s, const SectionView& section, std::string_view key) {
return section.mSection->get(key).getCopy(s);
};
sview["asTable"]
= [](sol::this_state lua, const SectionView& section) { return section.mSection->asTable(lua); };
sview["subscribe"] = [](const SectionView& section, const sol::table& callback) {
std::vector<Callback>& callbacks
= section.mForMenuScripts ? section.mSection->mMenuScriptsCallbacks : section.mSection->mCallbacks;
if (!callbacks.empty() && callbacks.size() == callbacks.capacity())
{
callbacks.erase(
std::remove_if(callbacks.begin(), callbacks.end(), [&](const Callback& c) { return !c.isValid(); }),
callbacks.end());
}
callbacks.push_back(Callback::fromLua(callback));
};
sview["reset"] = [](const SectionView& section, const sol::optional<sol::table>& newValues) {
if (section.mReadOnly)
throw std::runtime_error("Access to storage is read only");
section.mSection->setAll(newValues);
};
sview["removeOnExit"] = [](const SectionView& section) {
if (section.mReadOnly)
throw std::runtime_error("Access to storage is read only");
section.mSection->mLifeTime = Section::Temporary;
};
sview["setLifeTime"] = [](const SectionView& section, Section::LifeTime lifeTime) {
if (section.mReadOnly)
throw std::runtime_error("Access to storage is read only");
section.mSection->mLifeTime = lifeTime;
};
sview["set"] = [](const SectionView& section, std::string_view key, const sol::object& value) {
if (section.mReadOnly)
throw std::runtime_error("Access to storage is read only");
section.mSection->set(key, value);
};
}
sol::table LuaStorage::initGlobalPackage(LuaUtil::LuaView& view, LuaStorage* globalStorage)
{
sol::table res(view.sol(), sol::create);
registerLifeTime(view, res);
res["globalSection"] = [globalStorage](sol::this_state lua, std::string_view section) {
return globalStorage->getMutableSection(lua, section);
};
res["allGlobalSections"] = [globalStorage](sol::this_state lua) { return globalStorage->getAllSections(lua); };
return LuaUtil::makeReadOnly(res);
}
sol::table LuaStorage::initLocalPackage(LuaUtil::LuaView& view, LuaStorage* globalStorage)
{
sol::table res(view.sol(), sol::create);
registerLifeTime(view, res);
res["globalSection"] = [globalStorage](sol::this_state lua, std::string_view section) {
return globalStorage->getReadOnlySection(lua, section);
};
return LuaUtil::makeReadOnly(res);
}
sol::table LuaStorage::initPlayerPackage(
LuaUtil::LuaView& view, LuaStorage* globalStorage, LuaStorage* playerStorage)
{
sol::table res(view.sol(), sol::create);
registerLifeTime(view, res);
res["globalSection"] = [globalStorage](sol::this_state lua, std::string_view section) {
return globalStorage->getReadOnlySection(lua, section);
};
res["playerSection"] = [playerStorage](sol::this_state lua, std::string_view section) {
return playerStorage->getMutableSection(lua, section);
};
res["allPlayerSections"] = [playerStorage](sol::this_state lua) { return playerStorage->getAllSections(lua); };
return LuaUtil::makeReadOnly(res);
}
sol::table LuaStorage::initMenuPackage(LuaUtil::LuaView& view, LuaStorage* globalStorage, LuaStorage* playerStorage)
{
sol::table res(view.sol(), sol::create);
registerLifeTime(view, res);
res["playerSection"] = [playerStorage](sol::this_state lua, std::string_view section) {
return playerStorage->getMutableSection(lua, section, /*forMenuScripts=*/true);
};
res["globalSection"] = [globalStorage](sol::this_state lua, std::string_view section) {
return globalStorage->getReadOnlySection(lua, section);
};
res["allPlayerSections"] = [playerStorage](sol::this_state lua) { return playerStorage->getAllSections(lua); };
return LuaUtil::makeReadOnly(res);
}
void LuaStorage::clearTemporaryAndRemoveCallbacks()
{
auto it = mData.begin();
while (it != mData.end())
{
it->second->mCallbacks.clear();
// Note that we don't clear menu callbacks for permanent sections
// because starting/loading a game doesn't reset menu scripts.
if (it->second->mLifeTime == Section::Temporary)
{
it->second->mMenuScriptsCallbacks.clear();
it->second->mValues.clear();
it = mData.erase(it);
}
else
++it;
}
}
void LuaStorage::load(lua_State* L, const std::filesystem::path& path)
{
assert(mData.empty()); // Shouldn't be used before loading
try
{
std::uintmax_t fileSize = std::filesystem::file_size(path);
Log(Debug::Info) << "Loading Lua storage \"" << path << "\" (" << fileSize << " bytes)";
if (fileSize == 0)
throw std::runtime_error("Storage file has zero length");
std::ifstream fin(path, std::fstream::binary);
std::string serializedData((std::istreambuf_iterator<char>(fin)), std::istreambuf_iterator<char>());
sol::table data = deserialize(L, serializedData);
for (const auto& [sectionName, sectionTable] : data)
{
const std::shared_ptr<Section>& section = getSection(cast<std::string_view>(sectionName));
for (const auto& [key, value] : cast<sol::table>(sectionTable))
section->set(cast<std::string_view>(key), value);
}
}
catch (std::exception& e)
{
Log(Debug::Error) << "Cannot read \"" << path << "\": " << e.what();
}
}
void LuaStorage::save(lua_State* L, const std::filesystem::path& path) const
{
sol::table data(L, sol::create);
for (const auto& [sectionName, section] : mData)
{
if (section->mLifeTime == Section::Persistent && !section->mValues.empty())
data[sectionName] = section->asTable(L);
}
std::string serializedData = serialize(data);
Log(Debug::Info) << "Saving Lua storage \"" << path << "\" (" << serializedData.size() << " bytes)";
std::ofstream fout(path, std::fstream::binary);
fout.write(serializedData.data(), serializedData.size());
fout.close();
}
const std::shared_ptr<LuaStorage::Section>& LuaStorage::getSection(std::string_view sectionName)
{
checkIfActive();
auto it = mData.find(sectionName);
if (it != mData.end())
return it->second;
auto section = std::make_shared<Section>(this, std::string(sectionName));
sectionName = section->mSectionName;
auto [newIt, _] = mData.emplace(sectionName, std::move(section));
return newIt->second;
}
sol::object LuaStorage::getSection(lua_State* L, std::string_view sectionName, bool readOnly, bool forMenuScripts)
{
checkIfActive();
const std::shared_ptr<Section>& section = getSection(sectionName);
return sol::make_object<SectionView>(L, SectionView{ section, readOnly, forMenuScripts });
}
sol::table LuaStorage::getAllSections(lua_State* L, bool readOnly)
{
checkIfActive();
sol::table res(L, sol::create);
for (const auto& [sectionName, _] : mData)
res[sectionName] = getSection(L, sectionName, readOnly);
return res;
}
}
|