mirror of
https://github.com/J-D-K/JKSV.git
synced 2026-04-26 01:59:55 -05:00
ConfigContext: Use unordered_map and unordered_set instead of map and vector.
This commit is contained in:
parent
418cd3c3d3
commit
808381a51b
|
|
@ -3,8 +3,9 @@
|
|||
|
||||
#include <cstdint>
|
||||
#include <json-c/json.h>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
|
||||
namespace config
|
||||
|
|
@ -81,8 +82,23 @@ namespace config
|
|||
void get_custom_path(uint64_t applicationID, char *buffer, size_t bufferSize);
|
||||
|
||||
private:
|
||||
/// @brief These allow unordered_map with string key and string_view finds.
|
||||
// clang-format off
|
||||
struct StringViewHash
|
||||
{
|
||||
using is_transparent = void;
|
||||
size_t operator() (std::string_view view) const noexcept { return std::hash<std::string_view>{}(view); }
|
||||
};
|
||||
|
||||
struct StringViewEqual
|
||||
{
|
||||
using is_transparent = void;
|
||||
bool operator() (std::string_view viewA, std::string_view viewB) const noexcept { return viewA == viewB; }
|
||||
};
|
||||
// clang-format on
|
||||
|
||||
/// @brief This is where the majority of the config values are.
|
||||
std::map<std::string, uint8_t, std::less<>> m_configMap{};
|
||||
std::unordered_map<std::string, uint8_t, StringViewHash, StringViewEqual> m_configMap{};
|
||||
|
||||
/// @brief The main directory JKSV operates from.
|
||||
fslib::Path m_workingDirectory{};
|
||||
|
|
@ -91,13 +107,13 @@ namespace config
|
|||
double m_animationScaling{};
|
||||
|
||||
/// @brief This holds the favorite titles.
|
||||
std::vector<uint64_t> m_favorites{};
|
||||
std::unordered_set<uint64_t> m_favorites{};
|
||||
|
||||
/// @brief This holds the blacklisted titles.
|
||||
std::vector<uint64_t> m_blacklist{};
|
||||
std::unordered_set<uint64_t> m_blacklist{};
|
||||
|
||||
/// @brief This holds user defined paths.
|
||||
std::map<uint64_t, std::string> m_paths{};
|
||||
std::unordered_map<uint64_t, std::string> m_paths{};
|
||||
|
||||
/// @brief Loads the config file from SD.
|
||||
bool load_config_file();
|
||||
|
|
@ -106,16 +122,12 @@ namespace config
|
|||
void save_config_file();
|
||||
|
||||
/// @brief Reads a json array to the vector passed.
|
||||
void read_array_to_vector(std::vector<uint64_t> &vector, json_object *array);
|
||||
void read_array_to_set(std::unordered_set<uint64_t> &set, json_object *array);
|
||||
|
||||
/// @brief Loads the custom output path file from the SD if possible.
|
||||
void load_custom_paths();
|
||||
|
||||
/// @brief Saves the custom output paths to the SD card.
|
||||
void save_custom_paths();
|
||||
|
||||
/// @brief Searches the vector passed for the application ID passed.
|
||||
std::vector<uint64_t>::const_iterator find_application_id(const std::vector<uint64_t> &vector,
|
||||
uint64_t applicationID) const;
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -105,35 +105,39 @@ void config::ConfigContext::set_animation_scaling(double scaling) noexcept { m_a
|
|||
|
||||
void config::ConfigContext::add_favorite(uint64_t applicationID)
|
||||
{
|
||||
const auto findFav = ConfigContext::find_application_id(m_favorites, applicationID);
|
||||
if (findFav != m_favorites.end()) { return; }
|
||||
m_favorites.push_back(applicationID);
|
||||
const auto findID = m_favorites.find(applicationID);
|
||||
if (findID != m_favorites.end()) { return; }
|
||||
|
||||
m_favorites.insert(applicationID);
|
||||
}
|
||||
|
||||
void config::ConfigContext::remove_favorite(uint64_t applicationID) noexcept
|
||||
{
|
||||
const auto findFav = ConfigContext::find_application_id(m_favorites, applicationID);
|
||||
if (findFav == m_favorites.end()) { return; }
|
||||
m_favorites.erase(findFav);
|
||||
const auto findID = m_favorites.find(applicationID);
|
||||
if (findID == m_favorites.end()) { return; }
|
||||
|
||||
m_favorites.erase(findID);
|
||||
}
|
||||
|
||||
bool config::ConfigContext::is_favorite(uint64_t applicationID) const noexcept
|
||||
{
|
||||
return ConfigContext::find_application_id(m_favorites, applicationID) != m_favorites.end();
|
||||
return m_favorites.find(applicationID) != m_favorites.end();
|
||||
}
|
||||
|
||||
void config::ConfigContext::add_to_blacklist(uint64_t applicationID)
|
||||
{
|
||||
const auto findTitle = ConfigContext::find_application_id(m_blacklist, applicationID);
|
||||
if (findTitle != m_blacklist.end()) { return; }
|
||||
m_blacklist.push_back(applicationID);
|
||||
auto findID = m_blacklist.find(applicationID);
|
||||
if (findID != m_blacklist.end()) { return; }
|
||||
|
||||
m_blacklist.insert(applicationID);
|
||||
}
|
||||
|
||||
void config::ConfigContext::remove_from_blacklist(uint64_t applicationID) noexcept
|
||||
{
|
||||
const auto findTitle = ConfigContext::find_application_id(m_blacklist, applicationID);
|
||||
if (findTitle == m_blacklist.end()) { return; }
|
||||
m_blacklist.erase(findTitle);
|
||||
const auto findID = m_blacklist.find(applicationID);
|
||||
if (findID == m_blacklist.end()) { return; }
|
||||
|
||||
m_blacklist.erase(findID);
|
||||
}
|
||||
|
||||
void config::ConfigContext::get_blacklist(std::vector<uint64_t> &listOut)
|
||||
|
|
@ -143,7 +147,7 @@ void config::ConfigContext::get_blacklist(std::vector<uint64_t> &listOut)
|
|||
|
||||
bool config::ConfigContext::is_blacklisted(uint64_t applicationID) const noexcept
|
||||
{
|
||||
return ConfigContext::find_application_id(m_blacklist, applicationID) != m_blacklist.end();
|
||||
return m_blacklist.find(applicationID) != m_blacklist.end();
|
||||
}
|
||||
|
||||
bool config::ConfigContext::blacklist_empty() const noexcept { return m_blacklist.empty(); }
|
||||
|
|
@ -161,9 +165,10 @@ void config::ConfigContext::add_custom_path(uint64_t applicationID, std::string_
|
|||
|
||||
void config::ConfigContext::get_custom_path(uint64_t applicationID, char *buffer, size_t bufferSize)
|
||||
{
|
||||
if (!ConfigContext::has_custom_path(applicationID)) { return; }
|
||||
const auto findPath = m_paths.find(applicationID);
|
||||
if (findPath == m_paths.end()) { return; }
|
||||
|
||||
const std::string &path = m_paths[applicationID];
|
||||
const std::string &path = findPath->second;
|
||||
if (path.length() >= bufferSize) { return; }
|
||||
|
||||
std::memset(buffer, 0x00, bufferSize);
|
||||
|
|
@ -193,8 +198,8 @@ bool config::ConfigContext::load_config_file()
|
|||
|
||||
if (workDir) { m_workingDirectory = json_object_get_string(value); }
|
||||
else if (scaling) { m_animationScaling = json_object_get_double(value); }
|
||||
else if (favorites) { ConfigContext::read_array_to_vector(m_favorites, value); }
|
||||
else if (blacklist) { ConfigContext::read_array_to_vector(m_blacklist, value); }
|
||||
else if (favorites) { ConfigContext::read_array_to_set(m_favorites, value); }
|
||||
else if (blacklist) { ConfigContext::read_array_to_set(m_blacklist, value); }
|
||||
else { m_configMap[key] = json_object_get_uint64(value); }
|
||||
|
||||
json_object_iter_next(&configIter);
|
||||
|
|
@ -247,7 +252,7 @@ void config::ConfigContext::save_config_file()
|
|||
configFile << jsonString;
|
||||
}
|
||||
|
||||
void config::ConfigContext::read_array_to_vector(std::vector<uint64_t> &vector, json_object *array)
|
||||
void config::ConfigContext::read_array_to_set(std::unordered_set<uint64_t> &set, json_object *array)
|
||||
{
|
||||
const size_t arrayLength = json_object_array_length(array);
|
||||
for (size_t i = 0; i < arrayLength; i++)
|
||||
|
|
@ -256,7 +261,7 @@ void config::ConfigContext::read_array_to_vector(std::vector<uint64_t> &vector,
|
|||
const char *idHex = json_object_get_string(element);
|
||||
const uint64_t applicationID = std::strtoull(idHex, nullptr, 16);
|
||||
|
||||
vector.push_back(applicationID);
|
||||
set.insert(applicationID);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -300,9 +305,3 @@ void config::ConfigContext::save_custom_paths()
|
|||
if (error::fslib(pathsFile.is_open())) { return; }
|
||||
pathsFile << jsonString;
|
||||
}
|
||||
|
||||
std::vector<uint64_t>::const_iterator config::ConfigContext::find_application_id(const std::vector<uint64_t> &vector,
|
||||
uint64_t applicationID) const
|
||||
{
|
||||
return std::find(vector.begin(), vector.end(), applicationID);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -60,7 +60,6 @@ bool fs::move_directory_recursively(const fslib::Path &oldPath, const fslib::Pat
|
|||
const bool exists = fslib::directory_exists(fullDest);
|
||||
const bool renameError = !exists && error::fslib(fslib::rename_directory(fullSource, fullDest));
|
||||
const bool moved = exists && fs::move_directory_recursively(fullSource, fullDest);
|
||||
|
||||
if (renameError && !moved) { return false; }
|
||||
}
|
||||
else
|
||||
|
|
@ -70,6 +69,5 @@ bool fs::move_directory_recursively(const fslib::Path &oldPath, const fslib::Pat
|
|||
if (!exists && !renamed) { return false; }
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user