From cf4b9ac44be401c963e2b4c423aa64f18bbc053f Mon Sep 17 00:00:00 2001 From: FireMario211 <17692105+FireMario211@users.noreply.github.com> Date: Fri, 6 Sep 2024 18:06:50 -0400 Subject: [PATCH] safety --- mod.json | 2 +- src/main.cpp | 18 +-- src/ui/ObjectWorkshop.cpp | 201 ++++++++++++++++++++++++---------- src/ui/auth/AuthMenu.cpp | 12 +- src/ui/popups/EditPopup.cpp | 8 +- src/ui/popups/ReportPopup.cpp | 8 +- 6 files changed, 180 insertions(+), 69 deletions(-) diff --git a/mod.json b/mod.json index 3ad1b0a..c9b70d8 100644 --- a/mod.json +++ b/mod.json @@ -8,7 +8,7 @@ }, "id": "firee.object-workshop", "name": "Object Workshop", - "version": "v1.1.1", + "version": "v1.1.2", "developer": "Firee", "description": "Download, upload, or find custom objects made by other creators!", "resources": { diff --git a/src/main.cpp b/src/main.cpp index 9f69986..cb48ddf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -170,14 +170,16 @@ bool CustomObjects::init(LevelEditorLayer* editorLayer) { log::error("{}", isError); return; } - int uploads = jsonRes.get("uploads"); - labelOther1->setString( - fmt::format( - "{} Upload{} from you", - uploads, - (uploads == 1) ? "" : "s" - ) - ); + auto uploads = jsonRes.try_get("uploads"); + if (uploads) { + labelOther1->setString( + fmt::format( + "{} Upload{} from you", + uploads.value(), + (uploads.value() == 1) ? "" : "s" + ) + ); + } } else if (web::WebProgress* progress = e->getProgress()) { // The request is still in progress... } else if (e->isCancelled()) { diff --git a/src/ui/ObjectWorkshop.cpp b/src/ui/ObjectWorkshop.cpp index 0be30b7..5250a19 100644 --- a/src/ui/ObjectWorkshop.cpp +++ b/src/ui/ObjectWorkshop.cpp @@ -209,14 +209,26 @@ bool ObjectWorkshop::setup(bool authenticated) { auto jsonObj = jsonRes.as_object(); auto isError = jsonRes.try_get("error"); if (isError) return Notification::create(isError->c_str(), NotificationIcon::Error)->show(); - m_user = { - jsonRes.get("account_id"), - jsonRes.get("name"), - jsonRes.get("downloaded"), - jsonRes.get("favorites"), - jsonRes.get("uploads"), - jsonRes.get("role") - }; + // i know its a lotta more lines but gotta check! + auto r_account_id = jsonRes.try_get("account_id"); + auto r_name = jsonRes.try_get("name"); + auto r_downloaded = jsonRes.try_get("downloaded"); + auto r_favorites = jsonRes.try_get("favorites"); + auto r_uploads = jsonRes.try_get("uploads"); + auto r_role = jsonRes.try_get("role"); + if (r_account_id && r_name && r_downloaded && r_favorites && r_uploads && r_role) { + m_user = { + r_account_id.value(), + r_name->c_str(), + r_downloaded.value(), + r_favorites.value(), + r_uploads.value(), + r_role.value() + }; + } else { + log::error("Something went wrong when getting keys from the users object. {}", jsonRes.dump()); + Notification::create("Couldn't parse user object.", NotificationIcon::Warning)->show(); + } auto uploadsLabel = CCLabelBMFont::create(fmt::format("{} Upload{}", m_user.uploads, (m_user.uploads == 1) ? "" : "s").c_str(), "bigFont.fnt"); uploadsLabel->limitLabelWidth(90.F, 1.0F, 0.2F); uploadsLabel->setAnchorPoint({0, 0.5}); @@ -513,29 +525,61 @@ void ObjectWorkshop::load() { auto isError = jsonRes.try_get("error"); if (isError) return Notification::create(isError->c_str(), NotificationIcon::Error)->show(); if (!value->ok()) return Notification::create("An unknown error occured.", NotificationIcon::Error)->show(); - auto array = jsonRes.get("results"); + auto arrayRes = jsonRes.try_get("results"); + matjson::Array array; + if (arrayRes) { + array = arrayRes.value(); + } for (auto item : array) { auto obj = item; - auto cell = CCMenuItemSpriteExtra::create(ObjectItem::create({ - obj.get("id"), - obj.get("name"), - obj.get("description"), - obj.get("account_name"), - obj.get("rating"), - obj.get("account_id"), - obj.get("downloads"), - obj.get("favorites"), - obj.get("rating_count"), - obj.get("data"), - Utils::arrayIncludes(m_user.favorites, obj.get("id")), - Utils::arrayToUnorderedSet(obj.get("tags")), - obj.get("status") == 0 - }), this, menu_selector(ObjectWorkshop::onClickObject)); - categoryItems->addChild(cell); + // soooo bad + auto o_id = obj.try_get("id"); + auto o_name = obj.try_get("name"); + auto o_desc = obj.try_get("description"); + auto o_acc_name = obj.try_get("account_name"); + auto o_rating = obj.try_get("rating"); + auto o_acc_id = obj.try_get("account_id"); + auto o_downloads = obj.try_get("downloads"); + auto o_favorites = obj.try_get("favorites"); + auto o_rating_count = obj.try_get("rating_count"); + auto o_data = obj.try_get("data"); + auto o_tags = obj.try_get("tags"); + auto o_status = obj.try_get("status"); + if ( + o_id && + o_name && o_desc && + o_acc_name && o_acc_id && + o_rating && o_downloads && o_favorites && o_rating_count && + o_data && o_tags && o_status + ) { + auto cell = CCMenuItemSpriteExtra::create(ObjectItem::create({ + o_id.value(), + o_name.value(), + o_desc.value(), + o_acc_name.value(), + o_rating.value(), + o_acc_id.value(), + o_downloads.value(), + o_favorites.value(), + o_rating_count.value(), + o_data.value(), + Utils::arrayIncludes(m_user.favorites, o_id.value()), + Utils::arrayToUnorderedSet(o_tags.value()), + o_status.value() == 0 + }), this, menu_selector(ObjectWorkshop::onClickObject)); + categoryItems->addChild(cell); + } else { + log::error("One of the cells could not be properly parsed! {}", obj.dump()); + } } m_buttonMenu->removeChildByID("bottompage"_spr); - bottomPageLabel = CCLabelBMFont::create(fmt::format("Page {} of {} ({} Results found)", jsonRes.get("page"), jsonRes.get("pageAmount"), jsonRes.get("total")).c_str(), "goldFont.fnt"); - m_maxPage = jsonRes.get("pageAmount"); + auto o_page = jsonRes.try_get("page"); + auto o_pageAmount = jsonRes.try_get("pageAmount"); + auto o_total = jsonRes.try_get("total"); + if (o_page && o_pageAmount && o_total) { + bottomPageLabel = CCLabelBMFont::create(fmt::format("Page {} of {} ({} Results found)", o_page.value(), o_pageAmount.value(), o_total.value()).c_str(), "goldFont.fnt"); + m_maxPage = o_pageAmount.value(); + } bottomPageLabel->setScale(0.4F); bottomPageLabel->setID("bottompage"_spr); m_buttonMenu->addChildAtPosition(bottomPageLabel, Anchor::Bottom, {55, 17}); @@ -612,30 +656,57 @@ void ObjectWorkshop::load() { auto isError = jsonRes.try_get("error"); if (isError) return Notification::create(isError->c_str(), NotificationIcon::Error)->show(); if (!value->ok()) return Notification::create("An unknown error occured.", NotificationIcon::Error)->show(); - auto array = jsonRes.get("results"); + auto arrayRes = jsonRes.try_get("results"); + matjson::Array array; + if (arrayRes) { + array = arrayRes.value(); + } if (array.size() > 0) { myUploadsMenu->removeAllChildrenWithCleanup(true); } CCSize defaultContentSize; for (auto item : array) { auto obj = item; - auto cell = CCMenuItemSpriteExtra::create(ObjectItem::create({ - obj.get("id"), - obj.get("name"), - obj.get("description"), - obj.get("account_name"), - obj.get("rating"), - obj.get("account_id"), - obj.get("downloads"), - obj.get("favorites"), - obj.get("rating_count"), - obj.get("data"), - Utils::arrayIncludes(m_user.favorites, obj.get("id")), - Utils::arrayToUnorderedSet(obj.get("tags")), - obj.get("status") == 0 - }), this, menu_selector(ObjectWorkshop::onClickObject)); - defaultContentSize = cell->getContentSize(); - myUploadsMenu->addChild(cell); + // soooo bad + auto o_id = obj.try_get("id"); + auto o_name = obj.try_get("name"); + auto o_desc = obj.try_get("description"); + auto o_acc_name = obj.try_get("account_name"); + auto o_rating = obj.try_get("rating"); + auto o_acc_id = obj.try_get("account_id"); + auto o_downloads = obj.try_get("downloads"); + auto o_favorites = obj.try_get("favorites"); + auto o_rating_count = obj.try_get("rating_count"); + auto o_data = obj.try_get("data"); + auto o_tags = obj.try_get("tags"); + auto o_status = obj.try_get("status"); + if ( + o_id && + o_name && o_desc && + o_acc_name && o_acc_id && + o_rating && o_downloads && o_favorites && o_rating_count && + o_data && o_tags && o_status + ) { + auto cell = CCMenuItemSpriteExtra::create(ObjectItem::create({ + o_id.value(), + o_name.value(), + o_desc.value(), + o_acc_name.value(), + o_rating.value(), + o_acc_id.value(), + o_downloads.value(), + o_favorites.value(), + o_rating_count.value(), + o_data.value(), + Utils::arrayIncludes(m_user.favorites, o_id.value()), + Utils::arrayToUnorderedSet(o_tags.value()), + o_status.value() == 0 + }), this, menu_selector(ObjectWorkshop::onClickObject)); + defaultContentSize = cell->getContentSize(); + myUploadsMenu->addChild(cell); + } else { + log::error("One of the cells could not be properly parsed! {}", obj.dump()); + } } if (array.size() > 0) { myUploadsMenu->setPosition({127,-36}); @@ -1119,7 +1190,13 @@ void ObjectWorkshop::onFavBtn(CCObject*) { auto jsonObj = jsonRes.as_object(); auto isError = jsonRes.try_get("error"); if (isError) return Notification::create(isError->c_str(), NotificationIcon::Error)->show(); - Notification::create(jsonRes.get("message").c_str(), NotificationIcon::Success)->show(); + auto message = jsonRes.try_get("message"); + if (message) { + Notification::create(message->c_str(), NotificationIcon::Success)->show(); + } else { + log::error("Unknown response, expected message. {}", jsonRes.dump()); + Notification::create("Got an unknown response, check logs for details.", NotificationIcon::Warning)->show(); + } m_currentObject.favorited = !m_currentObject.favorited; if (m_currentObject.favorited) { m_currentObject.favorites++; @@ -1131,7 +1208,7 @@ void ObjectWorkshop::onFavBtn(CCObject*) { m_user.favorites.erase(it); } } - favoritesLabel->setString(std::to_string(m_currentObject.favorites).c_str()); + if (favoritesLabel != nullptr) favoritesLabel->setString(std::to_string(m_currentObject.favorites).c_str()); return; } else if (web::WebProgress* progress = e->getProgress()) { // The request is still in progress... @@ -1181,11 +1258,7 @@ void ObjectWorkshop::onDownloadBtn(CCObject*) { auto jsonObj = jsonRes.as_object(); auto isError = jsonRes.try_get("error"); if (isError) return Notification::create(isError->c_str(), NotificationIcon::Error)->show(); - auto message = jsonRes.try_get("message"); - if (message) { - log::info("{}", message); - } - downloadsLabel->setString(std::to_string(m_currentObject.downloads).c_str()); + if (downloadsLabel != nullptr) downloadsLabel->setString(std::to_string(m_currentObject.downloads).c_str()); return; } else if (web::WebProgress* progress = e->getProgress()) { // The request is still in progress... @@ -1269,7 +1342,13 @@ void ObjectWorkshop::onTrashBtn(CCObject*) { auto jsonObj = jsonRes.as_object(); auto isError = jsonRes.try_get("error"); if (isError) return Notification::create(isError->c_str(), NotificationIcon::Error)->show(); - Notification::create(jsonRes.get("message").c_str(), NotificationIcon::Success)->show(); + auto message = jsonRes.try_get("message"); + if (message) { + Notification::create(message->c_str(), NotificationIcon::Success)->show(); + } else { + log::error("Unknown response, expected message. {}", jsonRes.dump()); + Notification::create("Got an unknown response, check logs for details.", NotificationIcon::Warning)->show(); + } onBackBtn(nullptr); RegenCategory(); return; @@ -1310,7 +1389,13 @@ void ObjectWorkshop::onVerifyBtn(CCObject*) { auto jsonObj = jsonRes.as_object(); auto isError = jsonRes.try_get("error"); if (isError) return Notification::create(isError->c_str(), NotificationIcon::Error)->show(); - Notification::create(jsonRes.get("message").c_str(), NotificationIcon::Success)->show(); + auto message = jsonRes.try_get("message"); + if (message) { + Notification::create(message->c_str(), NotificationIcon::Success)->show(); + } else { + log::error("Unknown response, expected message. {}", jsonRes.dump()); + Notification::create("Got an unknown response, check logs for details.", NotificationIcon::Warning)->show(); + } return; } else if (web::WebProgress* progress = e->getProgress()) { // The request is still in progress... @@ -1346,7 +1431,13 @@ void ObjectWorkshop::onRejectBtn(CCObject*) { auto jsonObj = jsonRes.as_object(); auto isError = jsonRes.try_get("error"); if (isError) return Notification::create(isError->c_str(), NotificationIcon::Error)->show(); - Notification::create(jsonRes.get("message").c_str(), NotificationIcon::Success)->show(); + auto message = jsonRes.try_get("message"); + if (message) { + Notification::create(message->c_str(), NotificationIcon::Success)->show(); + } else { + log::error("Unknown response, expected message. {}", jsonRes.dump()); + Notification::create("Got an unknown response, check logs for details.", NotificationIcon::Warning)->show(); + } onBackBtn(nullptr); RegenCategory(); return; diff --git a/src/ui/auth/AuthMenu.cpp b/src/ui/auth/AuthMenu.cpp index 27a8316..dee3c50 100644 --- a/src/ui/auth/AuthMenu.cpp +++ b/src/ui/auth/AuthMenu.cpp @@ -106,9 +106,15 @@ void AuthMenu::genAuthToken(AuthMethod method, std::string token, bool showFLAle } callback(0); } else { - Mod::get()->setSettingValue("token", jsonRes.get("token")); - Mod::get()->setSettingValue("auth-server", method); - callback(1); + auto token = jsonRes.try_get("token"); + if (token) { + Mod::get()->setSettingValue("token", token->c_str()); + Mod::get()->setSettingValue("auth-server", method); + callback(1); + } else { + log::error("Expected token, got an unknown result. {}", jsonRes.dump()); + callback(0); + } } } else { auto strValue = value->string()->c_str(); diff --git a/src/ui/popups/EditPopup.cpp b/src/ui/popups/EditPopup.cpp index 72afa81..3d3aea1 100644 --- a/src/ui/popups/EditPopup.cpp +++ b/src/ui/popups/EditPopup.cpp @@ -84,7 +84,13 @@ void EditPopup::onUpdateBtn(CCObject*) { auto isError = jsonRes.try_get("error"); if (isError) return Notification::create(isError->c_str(), NotificationIcon::Error)->show(); notif->hide(); - Notification::create(jsonRes.get("message").c_str(), NotificationIcon::Success)->show(); + auto message = jsonRes.try_get("message"); + if (message) { + Notification::create(message->c_str(), NotificationIcon::Success)->show(); + } else { + log::error("Unknown response, expected message. {}", jsonRes.dump()); + Notification::create("Got an unknown response, check logs for details.", NotificationIcon::Warning)->show(); + } this->onClose(nullptr); return; } else if (web::WebProgress* progress = e->getProgress()) { diff --git a/src/ui/popups/ReportPopup.cpp b/src/ui/popups/ReportPopup.cpp index d343b32..cb005fa 100644 --- a/src/ui/popups/ReportPopup.cpp +++ b/src/ui/popups/ReportPopup.cpp @@ -38,7 +38,13 @@ void ReportPopup::onReportBtn(CCObject*) { auto jsonObj = jsonRes.as_object(); auto isError = jsonRes.try_get("error"); if (isError) return Notification::create(isError->c_str(), NotificationIcon::Error)->show(); - Notification::create(jsonRes.get("message").c_str(), NotificationIcon::Success)->show(); + auto message = jsonRes.try_get("message"); + if (message) { + Notification::create(message->c_str(), NotificationIcon::Success)->show(); + } else { + log::error("Unknown response, expected message. {}", jsonRes.dump()); + Notification::create("Got an unknown response, check logs for details.", NotificationIcon::Warning)->show(); + } onClose(nullptr); return; } else if (web::WebProgress* progress = e->getProgress()) {