Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions source/utils/storage/StorageItemRoot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 2 additions & 0 deletions source/utils/storage/StorageItemRoot.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class StorageItemRoot : public StorageSubItem {

void wipe();

void migrate(StorageItemRoot &&other);

private:
std::string mPluginName;
};
51 changes: 20 additions & 31 deletions source/utils/storage/StorageUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<std::string>();
const auto val = it.value().get<std::string>();
res->setValue(val);
} else if (it.value().is_boolean()) {
auto val = it.value().get<bool>();
const auto val = it.value().get<bool>();
res->setValue(val);
} else if (it.value().is_number_unsigned()) {
auto val = it.value().get<std::uint64_t>();
const auto val = it.value().get<std::uint64_t>();
res->setValue(val);
} else if (it.value().is_number_integer()) {
auto val = it.value().get<std::int64_t>();
const auto val = it.value().get<std::int64_t>();
res->setValue(val);
} else if (it.value().is_number_float()) {
auto val = it.value().get<std::double_t>();
const auto val = it.value().get<std::double_t>();
res->setValue(val);
} else {
DEBUG_FUNCTION_LINE_ERR("Unknown type %s for value %s", it.value().type_name(), it.key().c_str());
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -196,27 +191,21 @@ 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;
}

static WUPSStorageError WriteStorageToSD(wups_storage_root_item root, bool forceSave) {
const StorageItemRoot *rootItem = nullptr;
for (const auto &cur : gStorage) {
if (cur.getHandle() == reinterpret_cast<uint32_t>(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;
}
Expand Down
2 changes: 1 addition & 1 deletion source/utils/storage/StorageUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down