From 0ff36b9a85f0b731055fd22a4532ae240ca4e0c4 Mon Sep 17 00:00:00 2001 From: Maschell Date: Sat, 25 Jan 2025 22:20:10 +0100 Subject: [PATCH 1/2] StorageAPI: Make sure we keep mHandle of rootItem when force-reloading storage --- source/utils/storage/StorageItemRoot.cpp | 6 ++++++ source/utils/storage/StorageItemRoot.h | 2 ++ source/utils/storage/StorageUtils.cpp | 12 ++++++------ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/source/utils/storage/StorageItemRoot.cpp b/source/utils/storage/StorageItemRoot.cpp index d6a4a66..95ed41e 100644 --- a/source/utils/storage/StorageItemRoot.cpp +++ b/source/utils/storage/StorageItemRoot.cpp @@ -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); } \ No newline at end of file diff --git a/source/utils/storage/StorageItemRoot.h b/source/utils/storage/StorageItemRoot.h index f8ec6bc..af6d57a 100644 --- a/source/utils/storage/StorageItemRoot.h +++ b/source/utils/storage/StorageItemRoot.h @@ -12,6 +12,8 @@ class StorageItemRoot : public StorageSubItem { void wipe(); + void migrate(StorageItemRoot &&other); + private: std::string mPluginName; }; diff --git a/source/utils/storage/StorageUtils.cpp b/source/utils/storage/StorageUtils.cpp index 3450923..e3d4f76 100644 --- a/source/utils/storage/StorageUtils.cpp +++ b/source/utils/storage/StorageUtils.cpp @@ -196,16 +196,16 @@ namespace StorageUtils { return err; } - std::unique_ptr storage; + std::unique_ptr newStorage; if (j.empty() || !j.is_object() || !j.contains("storageitems") || j["storageitems"].empty() || !j["storageitems"].is_object()) { - storage = make_unique_nothrow(plugin_id); + newStorage = make_unique_nothrow(plugin_id); } else { - storage = StorageUtils::Helper::deserializeFromJson(j["storageitems"], plugin_id); - if (!storage) { - storage = make_unique_nothrow(plugin_id); + newStorage = deserializeFromJson(j["storageitems"], plugin_id); + if (!newStorage) { + newStorage = make_unique_nothrow(plugin_id); } } - rootItem = std::move(*storage); + rootItem.migrate(std::move(*newStorage)); return WUPS_STORAGE_ERROR_SUCCESS; } From 62ba5caab3c1b820409c26effcd7e2c362f31218 Mon Sep 17 00:00:00 2001 From: Maschell Date: Sat, 25 Jan 2025 22:20:26 +0100 Subject: [PATCH 2/2] StorageAPI: code cleanup --- source/utils/storage/StorageUtils.cpp | 39 ++++++++++----------------- source/utils/storage/StorageUtils.h | 2 +- 2 files changed, 15 insertions(+), 26 deletions(-) diff --git a/source/utils/storage/StorageUtils.cpp b/source/utils/storage/StorageUtils.cpp index e3d4f76..980e782 100644 --- a/source/utils/storage/StorageUtils.cpp +++ b/source/utils/storage/StorageUtils.cpp @@ -34,7 +34,7 @@ namespace StorageUtils { for (auto it = json.begin(); it != json.end(); ++it) { StorageSubItem::StorageSubItemError subItemError = StorageSubItem::STORAGE_SUB_ITEM_ERROR_NONE; if (it.value().is_object()) { - auto res = item.createSubItem(it.key().c_str(), subItemError); + const auto res = item.createSubItem(it.key().c_str(), subItemError); if (!res) { DEBUG_FUNCTION_LINE_WARN("Failed to create sub item: Error %d", subItemError); return false; @@ -46,26 +46,26 @@ namespace StorageUtils { } } } else { - auto res = item.createItem(it.key().c_str(), subItemError); + const auto res = item.createItem(it.key().c_str(), subItemError); if (!res) { DEBUG_FUNCTION_LINE_WARN("Failed to create Item for key %s. Error %d", it.key().c_str(), subItemError); return false; } if (it.value().is_string()) { - auto val = it.value().get(); + const auto val = it.value().get(); res->setValue(val); } else if (it.value().is_boolean()) { - auto val = it.value().get(); + const auto val = it.value().get(); res->setValue(val); } else if (it.value().is_number_unsigned()) { - auto val = it.value().get(); + const auto val = it.value().get(); res->setValue(val); } else if (it.value().is_number_integer()) { - auto val = it.value().get(); + const auto val = it.value().get(); res->setValue(val); } else if (it.value().is_number_float()) { - auto val = it.value().get(); + const auto val = it.value().get(); res->setValue(val); } else { DEBUG_FUNCTION_LINE_ERR("Unknown type %s for value %s", it.value().type_name(), it.key().c_str()); @@ -97,36 +97,31 @@ namespace StorageUtils { for (const auto &[key, value] : baseItem.getItems()) { switch ((StorageItemType) value.getType()) { case StorageItemType::String: { - std::string res; - if (value.getValue(res)) { + if (std::string res; value.getValue(res)) { json[key] = res; } break; } case StorageItemType::Boolean: { - bool res; - if (value.getValue(res)) { + if (bool res; value.getValue(res)) { json[key] = res; } break; } case StorageItemType::S64: { - int64_t res; - if (value.getValue(res)) { + if (int64_t res; value.getValue(res)) { json[key] = res; } break; } case StorageItemType::U64: { - uint64_t res; - if (value.getValue(res)) { + if (uint64_t res; value.getValue(res)) { json[key] = res; } break; } case StorageItemType::Double: { - double res; - if (value.getValue(res)) { + if (double res; value.getValue(res)) { json[key] = res; } break; @@ -209,14 +204,8 @@ namespace StorageUtils { return WUPS_STORAGE_ERROR_SUCCESS; } - static WUPSStorageError WriteStorageToSD(wups_storage_root_item root, bool forceSave) { - const StorageItemRoot *rootItem = nullptr; - for (const auto &cur : gStorage) { - if (cur.getHandle() == reinterpret_cast(root)) { - rootItem = &cur; - break; - } - } + static WUPSStorageError WriteStorageToSD(wups_storage_root_item root, const bool forceSave) { + const auto rootItem = Helper::getRootItem(root); if (!rootItem) { return WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED; } diff --git a/source/utils/storage/StorageUtils.h b/source/utils/storage/StorageUtils.h index 995fd25..d402b30 100644 --- a/source/utils/storage/StorageUtils.h +++ b/source/utils/storage/StorageUtils.h @@ -9,7 +9,7 @@ namespace StorageUtils::API { namespace Internal { WUPSStorageError OpenStorage(std::string_view plugin_id, wups_storage_root_item &outItem); - WUPSStorageError CloseStorage(wups_storage_root_item item); + WUPSStorageError CloseStorage(wups_storage_root_item root); } // namespace Internal WUPSStorageError SaveStorage(wups_storage_root_item root, bool force);