StorageAPI: Make sure we keep mHandle of rootItem when force-reloading storage

This commit is contained in:
Maschell 2025-01-25 22:20:10 +01:00
parent f859f13978
commit 1cef78d8bf
3 changed files with 14 additions and 6 deletions

View File

@ -10,4 +10,10 @@ StorageItemRoot::StorageItemRoot(const std::string_view plugin_name) : StorageSu
void StorageItemRoot::wipe() {
mSubCategories.clear();
mItems.clear();
}
void StorageItemRoot::migrate(StorageItemRoot &&other) {
mPluginName = std::move(other.mPluginName);
mSubCategories = std::move(other.mSubCategories);
mItems = std::move(other.mItems);
}

View File

@ -12,6 +12,8 @@ public:
void wipe();
void migrate(StorageItemRoot &&other);
private:
std::string mPluginName;
};

View File

@ -196,16 +196,16 @@ namespace StorageUtils {
return err;
}
std::unique_ptr<StorageItemRoot> storage;
std::unique_ptr<StorageItemRoot> newStorage;
if (j.empty() || !j.is_object() || !j.contains("storageitems") || j["storageitems"].empty() || !j["storageitems"].is_object()) {
storage = make_unique_nothrow<StorageItemRoot>(plugin_id);
newStorage = make_unique_nothrow<StorageItemRoot>(plugin_id);
} else {
storage = StorageUtils::Helper::deserializeFromJson(j["storageitems"], plugin_id);
if (!storage) {
storage = make_unique_nothrow<StorageItemRoot>(plugin_id);
newStorage = deserializeFromJson(j["storageitems"], plugin_id);
if (!newStorage) {
newStorage = make_unique_nothrow<StorageItemRoot>(plugin_id);
}
}
rootItem = std::move(*storage);
rootItem.migrate(std::move(*newStorage));
return WUPS_STORAGE_ERROR_SUCCESS;
}