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

@ -11,3 +11,9 @@ void StorageItemRoot::wipe() {
mSubCategories.clear(); mSubCategories.clear();
mItems.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 wipe();
void migrate(StorageItemRoot &&other);
private: private:
std::string mPluginName; std::string mPluginName;
}; };

View File

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