Merge pull request #12544 from lioncash/getmod

GraphicsModGroup: Allow heterogenous lookup for GetMod()
This commit is contained in:
Admiral H. Curtiss 2024-01-31 20:17:26 +01:00 committed by GitHub
commit 18abf7c768
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 14 additions and 17 deletions

View File

@ -185,7 +185,7 @@ void GraphicsModListWidget::ModItemChanged(QListWidgetItem* item)
m_needs_save = true; m_needs_save = true;
} }
void GraphicsModListWidget::OnModChanged(std::optional<std::string> absolute_path) void GraphicsModListWidget::OnModChanged(const std::optional<std::string>& absolute_path)
{ {
ClearLayoutRecursively(m_mod_meta_layout); ClearLayoutRecursively(m_mod_meta_layout);
@ -198,7 +198,7 @@ void GraphicsModListWidget::OnModChanged(std::optional<std::string> absolute_pat
return; return;
} }
GraphicsModConfig* mod = m_mod_group.GetMod(*absolute_path); const GraphicsModConfig* mod = m_mod_group.GetMod(*absolute_path);
if (!mod) if (!mod)
return; return;

View File

@ -52,7 +52,7 @@ private:
void ModSelectionChanged(); void ModSelectionChanged();
void ModItemChanged(QListWidgetItem* item); void ModItemChanged(QListWidgetItem* item);
void OnModChanged(std::optional<std::string> absolute_path); void OnModChanged(const std::optional<std::string>& absolute_path);
void SaveModList(); void SaveModList();

View File

@ -7,6 +7,8 @@
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <picojson.h>
#include "Common/CommonPaths.h" #include "Common/CommonPaths.h"
#include "Common/FileSearch.h" #include "Common/FileSearch.h"
#include "Common/FileUtil.h" #include "Common/FileUtil.h"
@ -83,7 +85,7 @@ void GraphicsModGroupConfig::Load()
auto mod_full_path = graphics_mod->GetAbsolutePath(); auto mod_full_path = graphics_mod->GetAbsolutePath();
known_paths.insert(std::move(mod_full_path)); known_paths.insert(std::move(mod_full_path));
m_graphics_mods.push_back(*graphics_mod); m_graphics_mods.push_back(std::move(*graphics_mod));
} }
} }
} }
@ -93,15 +95,11 @@ void GraphicsModGroupConfig::Load()
GraphicsModConfig::Source source) { GraphicsModConfig::Source source) {
auto file = dir + DIR_SEP + "metadata.json"; auto file = dir + DIR_SEP + "metadata.json";
UnifyPathSeparators(file); UnifyPathSeparators(file);
if (known_paths.find(file) != known_paths.end()) if (known_paths.contains(file))
{
return; return;
}
const auto mod = GraphicsModConfig::Create(file, source); if (auto mod = GraphicsModConfig::Create(file, source))
if (mod) m_graphics_mods.push_back(std::move(*mod));
{
m_graphics_mods.push_back(*mod);
}
}; };
const std::set<std::string> graphics_mod_user_directories = const std::set<std::string> graphics_mod_user_directories =
@ -174,7 +172,7 @@ std::vector<GraphicsModConfig>& GraphicsModGroupConfig::GetMods()
return m_graphics_mods; return m_graphics_mods;
} }
GraphicsModConfig* GraphicsModGroupConfig::GetMod(const std::string& absolute_path) const GraphicsModConfig* GraphicsModGroupConfig::GetMod(std::string_view absolute_path) const
{ {
if (const auto iter = m_path_to_graphics_mod.find(absolute_path); if (const auto iter = m_path_to_graphics_mod.find(absolute_path);
iter != m_path_to_graphics_mod.end()) iter != m_path_to_graphics_mod.end())

View File

@ -5,10 +5,9 @@
#include <map> #include <map>
#include <string> #include <string>
#include <string_view>
#include <vector> #include <vector>
#include <picojson.h>
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
struct GraphicsModConfig; struct GraphicsModConfig;
@ -34,7 +33,7 @@ public:
const std::vector<GraphicsModConfig>& GetMods() const; const std::vector<GraphicsModConfig>& GetMods() const;
std::vector<GraphicsModConfig>& GetMods(); std::vector<GraphicsModConfig>& GetMods();
GraphicsModConfig* GetMod(const std::string& absolute_path) const; GraphicsModConfig* GetMod(std::string_view absolute_path) const;
const std::string& GetGameID() const; const std::string& GetGameID() const;
@ -42,6 +41,6 @@ private:
std::string GetPath() const; std::string GetPath() const;
std::string m_game_id; std::string m_game_id;
std::vector<GraphicsModConfig> m_graphics_mods; std::vector<GraphicsModConfig> m_graphics_mods;
std::map<std::string, GraphicsModConfig*> m_path_to_graphics_mod; std::map<std::string, GraphicsModConfig*, std::less<>> m_path_to_graphics_mod;
u32 m_change_count = 0; u32 m_change_count = 0;
}; };