diff options
author | Vittorio Romeo <vittorio.romeo@outlook.com> | 2021-03-02 16:21:51 +0000 |
---|---|---|
committer | Vittorio Romeo <vittorio.romeo@outlook.com> | 2021-03-02 16:21:51 +0000 |
commit | 8e8fa38c4ec0fd1281d47f35381bd488f2a3c87f (patch) | |
tree | 0e7a29d7ad14aab73d0796197498303792496496 | |
parent | a48af4813be261703d44396c363db86efd7ce4c8 (diff) |
Minor refactoring and bug fixingMenu-levelDataIds-optimization
-rw-r--r-- | build/cmake_mingw_debug.sh | 2 | ||||
-rw-r--r-- | include/SSVOpenHexagon/Data/ProfileData.hpp | 25 | ||||
-rw-r--r-- | include/SSVOpenHexagon/Global/Assets.hpp | 15 | ||||
-rw-r--r-- | src/SSVOpenHexagon/Core/MenuGame.cpp | 21 | ||||
-rw-r--r-- | src/SSVOpenHexagon/Core/Replay.cpp | 38 | ||||
-rw-r--r-- | src/SSVOpenHexagon/Data/ProfileData.cpp | 6 | ||||
-rw-r--r-- | src/SSVOpenHexagon/Global/Assets.cpp | 22 |
7 files changed, 70 insertions, 59 deletions
diff --git a/build/cmake_mingw_debug.sh b/build/cmake_mingw_debug.sh index 85389231..d296c2e0 100644 --- a/build/cmake_mingw_debug.sh +++ b/build/cmake_mingw_debug.sh @@ -1,2 +1,2 @@ #!/bin/bash
-cmake .. -G"MinGW Makefiles" -DCMAKE_BUILD_TYPE=DEBUG -DCMAKE_CXX_FLAGS="-O0 -fno-omit-frame-pointer"
+cmake .. -G"MinGW Makefiles" -DCMAKE_BUILD_TYPE=DEBUG -DCMAKE_CXX_FLAGS="-O0 -fno-omit-frame-pointer -Wall -Wextra -Wpedantic"
diff --git a/include/SSVOpenHexagon/Data/ProfileData.hpp b/include/SSVOpenHexagon/Data/ProfileData.hpp index 3223854b..9c69be54 100644 --- a/include/SSVOpenHexagon/Data/ProfileData.hpp +++ b/include/SSVOpenHexagon/Data/ProfileData.hpp @@ -8,6 +8,7 @@ #include "SSVOpenHexagon/SSVUtilsJson/SSVUtilsJson.hpp" #include <unordered_set> +#include <string> namespace hg { @@ -17,26 +18,28 @@ class HGAssets; class ProfileData { private: - HGAssets& assets; GameVersion version; std::string name; ssvuj::Obj scores; std::vector<std::string> trackedNames; std::unordered_set<std::string> favoriteLevelsDataIDs; - void checkFavoriteLevelsHealth(); + void checkFavoriteLevelsHealth(HGAssets& assets); public: ProfileData(HGAssets& mAssets, const GameVersion mVersion, const std::string& mName, const ssvuj::Obj& mScores, const std::vector<std::string>& mTrackedNames, const std::vector<std::string>& mFavorites) - : assets{mAssets}, version{mVersion}, name{mName}, scores{mScores}, - trackedNames{mTrackedNames} + : version{mVersion}, name{mName}, scores{mScores}, trackedNames{ + mTrackedNames} { - std::copy(mFavorites.begin(), mFavorites.end(), - std::inserter(favoriteLevelsDataIDs, favoriteLevelsDataIDs.end())); - checkFavoriteLevelsHealth(); + for(const std::string& favID : mFavorites) + { + favoriteLevelsDataIDs.emplace(favID); + } + + checkFavoriteLevelsHealth(mAssets); } [[nodiscard]] constexpr GameVersion getVersion() const noexcept @@ -66,7 +69,7 @@ public: return favoriteLevelsDataIDs; } - void setScore(const std::string& mId, float mScore) + void setScore(const std::string& mId, const float mScore) { ssvuj::arch(scores, mId, mScore); } @@ -88,14 +91,16 @@ public: void addFavoriteLevel(const std::string& mLevelID) { - favoriteLevelsDataIDs.insert(mLevelID); + favoriteLevelsDataIDs.emplace(mLevelID); } + void removeFavoriteLevel(const std::string& mLevelID) { favoriteLevelsDataIDs.erase(mLevelID); } - [[nodiscard]] bool isLevelFavorite(const std::string& mLevelID) + [[nodiscard]] bool isLevelFavorite( + const std::string& mLevelID) const noexcept { return favoriteLevelsDataIDs.find(mLevelID) != favoriteLevelsDataIDs.end(); diff --git a/include/SSVOpenHexagon/Global/Assets.hpp b/include/SSVOpenHexagon/Global/Assets.hpp index 8e4068db..2813c9f9 100644 --- a/include/SSVOpenHexagon/Global/Assets.hpp +++ b/include/SSVOpenHexagon/Global/Assets.hpp @@ -19,7 +19,6 @@ #include <SFML/System.hpp> #include <unordered_map> -#include <unordered_set> #include <map> #include <vector> #include <string> @@ -159,15 +158,6 @@ public: return getLevelData(mPackId + "_" + mId); } - void addFavoriteLevel(const std::string& mLevelID) - { - getCurrentLocalProfile().addFavoriteLevel(mLevelID); - } - void removeFavoriteLevel(const std::string& mLevelID) - { - getCurrentLocalProfile().removeFavoriteLevel(mLevelID); - } - bool checkLevelIDValidity(const std::string& mAssetId) { return levelDatas.find(mAssetId) != levelDatas.end(); @@ -180,11 +170,6 @@ public: return levelDataIdsByPack.at(mPackId); } - const std::unordered_set<std::string>& getFavoriteLevelIds() - { - return getCurrentLocalProfile().getFavoriteLevelIds(); - } - const std::unordered_map<std::string, PackData>& getPacksData() { return packDatas; diff --git a/src/SSVOpenHexagon/Core/MenuGame.cpp b/src/SSVOpenHexagon/Core/MenuGame.cpp index cf8e10b8..7a007b1f 100644 --- a/src/SSVOpenHexagon/Core/MenuGame.cpp +++ b/src/SSVOpenHexagon/Core/MenuGame.cpp @@ -1897,6 +1897,7 @@ void MenuGame::update(ssvu::FT mFT) { addRemoveFavoriteLevel(); } + if(hg::Joystick::favoritesMenuRisingEdge()) { switchToFromFavoriteLevels(); @@ -3712,11 +3713,12 @@ void MenuGame::formatLevelDescription() void MenuGame::changeFavoriteLevelsToProfile() { // Each profile has its own favorite levels. - // Copy the ProfileData favorites into the menu's - // favorites vector. + // Copy the `ProfileData` favorites into the menu's favorites vector. favoriteLevelDataIds.clear(); - for(const std::string& id : assets.getFavoriteLevelIds()) + + for(const std::string& id : + assets.getCurrentLocalProfile().getFavoriteLevelIds()) { favoriteLevelDataIds.push_back(id); } @@ -3731,13 +3733,15 @@ void MenuGame::changeFavoriteLevelsToProfile() // If the new current profile has no favorites force the level selection // to use the regular drawer. - if(!sz) + if(sz == 0) { lvlDrawer = &lvlSlct; + return; } // Make sure the level index is within boundaries. ssvu::clamp(favSlct.currentIndex, 0, sz - 1); + // Resize the level offset parameters. favSlct.lvlOffsets.resize(sz); } @@ -3746,12 +3750,13 @@ void MenuGame::addRemoveFavoriteLevel() { const LevelData& data{assets.getLevelData( lvlDrawer->levelDataIds->at(lvlDrawer->currentIndex))}; + const std::string levelID{data.packId + "_" + data.id}; // Level is a favorite so remove it. if(isLevelFavorite) { - assets.removeFavoriteLevel(levelID); + assets.getCurrentLocalProfile().removeFavoriteLevel(levelID); favoriteLevelDataIds.erase(std::find( favoriteLevelDataIds.begin(), favoriteLevelDataIds.end(), levelID)); favSlct.lvlOffsets.pop_back(); @@ -3759,6 +3764,7 @@ void MenuGame::addRemoveFavoriteLevel() // Make sure the index is within bounds. ssvu::clamp(favSlct.currentIndex, 0, static_cast<int>(favSlct.levelDataIds->size()) - 1); + if(isFavoriteLevels()) { adjustLevelsOffset(); @@ -3790,7 +3796,7 @@ void MenuGame::addRemoveFavoriteLevel() } else { - assets.addFavoriteLevel(levelID); + assets.getCurrentLocalProfile().addFavoriteLevel(levelID); favSlct.lvlOffsets.emplace_back(0.f); isLevelFavorite = true; @@ -3800,6 +3806,7 @@ void MenuGame::addRemoveFavoriteLevel() const auto end{favoriteLevelDataIds.end()}; const std::string tweakedFavName{ ssvu::toLower(assets.getLevelData(levelID).name)}; + std::string tweakedLevelName; while(it != end) { @@ -3808,8 +3815,10 @@ void MenuGame::addRemoveFavoriteLevel() { break; } + ++it; } + if(it == end) { favoriteLevelDataIds.emplace_back(levelID); diff --git a/src/SSVOpenHexagon/Core/Replay.cpp b/src/SSVOpenHexagon/Core/Replay.cpp index c2351458..e3fd7c03 100644 --- a/src/SSVOpenHexagon/Core/Replay.cpp +++ b/src/SSVOpenHexagon/Core/Replay.cpp @@ -4,6 +4,8 @@ #include "SSVOpenHexagon/Core/Replay.hpp" +#include <SSVUtils/Core/Common/LikelyUnlikely.hpp> + #include <cassert> #include <fstream> #include <sstream> @@ -11,17 +13,22 @@ namespace hg { -#define SSVOH_TRY(...) \ - do \ - { \ - __VA_ARGS__; \ - if(!result._success) \ - { \ - ::std::cerr << "Failed [de]serialization operation '" #__VA_ARGS__ \ - "'\n"; \ - \ - return result; \ - } \ +[[gnu::cold]] static void printTryFailure(const char* code) +{ + ::std::cerr << "Failed [de]serialization operation '" << code << "'\n"; +} + +#define SSVOH_TRY(...) \ + do \ + { \ + __VA_ARGS__; \ + \ + if(SSVU_UNLIKELY(!result._success)) \ + { \ + printTryFailure(#__VA_ARGS__); \ + return result; \ + } \ + \ } while(false) static auto make_write(serialization_result& result, std::byte*& buffer, @@ -78,7 +85,6 @@ void replay_data::record_input(const bool left, const bool right, return _inputs.size(); } - [[nodiscard]] bool replay_data::operator==( const replay_data& rhs) const noexcept { @@ -135,8 +141,12 @@ void replay_data::record_input(const bool left, const bool right, for(std::size_t i = 0; i < n_inputs; ++i) { + // TODO: gcc bug? +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wmaybe-uninitialized" std::uint8_t ib_byte; SSVOH_TRY(read(ib_byte)); +#pragma GCC diagnostic pop _inputs[i] = input_bitset{static_cast<unsigned long>(ib_byte)}; } @@ -257,6 +267,9 @@ void replay_player::reset() noexcept s.resize(s_size); +// TODO: gcc bug? +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wmaybe-uninitialized" for(std::uint32_t i = 0; i < s_size; ++i) { char c; @@ -264,6 +277,7 @@ void replay_player::reset() noexcept s[i] = c; } +#pragma GCC diagnostic pop return result; }; diff --git a/src/SSVOpenHexagon/Data/ProfileData.cpp b/src/SSVOpenHexagon/Data/ProfileData.cpp index a8222b82..3f692185 100644 --- a/src/SSVOpenHexagon/Data/ProfileData.cpp +++ b/src/SSVOpenHexagon/Data/ProfileData.cpp @@ -10,16 +10,16 @@ namespace hg { -void ProfileData::checkFavoriteLevelsHealth() +void ProfileData::checkFavoriteLevelsHealth(HGAssets& mAssets) { // Remove invalid levelIDs that might have been added to the files. for(const std::string& favID : favoriteLevelsDataIDs) { - if(!assets.checkLevelIDValidity(favID)) + if(!mAssets.checkLevelIDValidity(favID)) { removeFavoriteLevel(favID); } } } -} // namespace hg
\ No newline at end of file +} // namespace hg diff --git a/src/SSVOpenHexagon/Global/Assets.cpp b/src/SSVOpenHexagon/Global/Assets.cpp index 2f7289e0..718b2688 100644 --- a/src/SSVOpenHexagon/Global/Assets.cpp +++ b/src/SSVOpenHexagon/Global/Assets.cpp @@ -419,11 +419,6 @@ void HGAssets::saveCurrentLocalProfile() void HGAssets::saveAllProfiles() { - if(currentProfilePtr == nullptr) - { - return; - } - ssvuj::Obj currentVersion; ssvuj::arch(currentVersion, "major", Config::GAME_VERSION.major); ssvuj::arch(currentVersion, "minor", Config::GAME_VERSION.minor); @@ -431,25 +426,28 @@ void HGAssets::saveAllProfiles() std::vector<std::string> favorites; - for(const auto& profile : profileDataMap) + for(const auto& [key, profileData] : profileDataMap) { ssvuj::Obj profileRoot; ssvuj::arch(profileRoot, "version", currentVersion); - ssvuj::arch(profileRoot, "name", profile.second.getName()); - ssvuj::arch(profileRoot, "scores", profile.second.getScores()); + ssvuj::arch(profileRoot, "name", profileData.getName()); + ssvuj::arch(profileRoot, "scores", profileData.getScores()); favorites.clear(); - const auto& favSet{profile.second.getFavoriteLevelIds()}; - std::copy(favSet.begin(), favSet.end(), std::back_inserter(favorites)); + for(const std::string& favID : profileData.getFavoriteLevelIds()) + { + favorites.emplace_back(favID); + } + ssvuj::arch(profileRoot, "favorites", favorites); - for(const auto& n : profile.second.getTrackedNames()) + for(const auto& n : profileData.getTrackedNames()) { profileRoot["trackedNames"].append(n); } ssvuj::writeToFile( - profileRoot, "Profiles/" + profile.second.getName() + ".json"); + profileRoot, "Profiles/" + profileData.getName() + ".json"); } } |