| 253 | } |
| 254 | |
| 255 | void LuaStorage::load(lua_State* state, const std::filesystem::path& path) |
| 256 | { |
| 257 | assert(mData.empty()); // Shouldn't be used before loading |
| 258 | try |
| 259 | { |
| 260 | std::uintmax_t fileSize = std::filesystem::file_size(path); |
| 261 | Log(Debug::Info) << "Loading Lua storage \"" << path << "\" (" << fileSize << " bytes)"; |
| 262 | if (fileSize == 0) |
| 263 | throw std::runtime_error("Storage file has zero length"); |
| 264 | |
| 265 | std::ifstream fin(path, std::fstream::binary); |
| 266 | std::string serializedData((std::istreambuf_iterator<char>(fin)), std::istreambuf_iterator<char>()); |
| 267 | sol::table data = deserialize(state, serializedData); |
| 268 | for (const auto& [sectionName, sectionTable] : data) |
| 269 | { |
| 270 | const std::shared_ptr<Section>& section = getSection(cast<std::string_view>(sectionName)); |
| 271 | for (const auto& [key, value] : cast<sol::table>(sectionTable)) |
| 272 | section->set(cast<std::string_view>(key), value); |
| 273 | } |
| 274 | } |
| 275 | catch (std::exception& e) |
| 276 | { |
| 277 | Log(Debug::Error) << "Cannot read \"" << path << "\": " << e.what(); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | void LuaStorage::save(lua_State* state, const std::filesystem::path& path) const |
| 282 | { |
no test coverage detected