Skip to content

Commit

Permalink
Wrap game feature in shared pointer.
Browse files Browse the repository at this point in the history
  • Loading branch information
Holt59 committed Jun 1, 2024
1 parent 6915910 commit 629762d
Show file tree
Hide file tree
Showing 14 changed files with 117 additions and 112 deletions.
4 changes: 2 additions & 2 deletions src/directoryrefresher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ void DirectoryRefresher::addModBSAToStructure(DirectoryEntry* root,
{
QStringList loadOrder;

auto* gamePlugins = m_Core.gameFeatures().gameFeature<GamePlugins>();
auto gamePlugins = m_Core.gameFeatures().gameFeature<GamePlugins>();
if (gamePlugins) {
loadOrder = gamePlugins->getLoadOrder();
}
Expand Down Expand Up @@ -357,7 +357,7 @@ struct ModThread

if (Settings::instance().archiveParsing()) {
QStringList loadOrder;
GamePlugins* gamePlugins = gameFeatures->gameFeature<GamePlugins>();
auto gamePlugins = gameFeatures->gameFeature<GamePlugins>();
if (gamePlugins) {
loadOrder = gamePlugins->getLoadOrder();
}
Expand Down
20 changes: 14 additions & 6 deletions src/game_features.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ GameFeatures::GameFeatures(OrganizerCore* core, PluginContainer* plugins)
});
}

GameFeatures::~GameFeatures() {}

GameFeatures::CombinedModDataChecker& GameFeatures::modDataChecker() const
{
return dynamic_cast<CombinedModDataChecker&>(*m_modDataChecker);
Expand All @@ -176,6 +178,12 @@ void GameFeatures::updateCurrentFeatures(std::type_index const& index)
auto& features = m_allFeatures[index];

m_currentFeatures[index].clear();

// this can occur when starting MO2, just wait for the next update
if (!m_pluginContainer.managedGame()) {
return;
}

for (const auto& dataFeature : features) {

// registering plugin is disabled
Expand All @@ -202,7 +210,7 @@ void GameFeatures::updateCurrentFeatures(std::type_index const& index)
return std::dynamic_pointer_cast<ModDataChecker>(checker);
});
modDataChecker().setCheckers(std::move(checkers));
emit modDataCheckerUpdated(gameFeature<ModDataChecker>());
emit modDataCheckerUpdated(gameFeature<ModDataChecker>().get());
}

// update mod data content
Expand All @@ -215,7 +223,7 @@ void GameFeatures::updateCurrentFeatures(std::type_index const& index)
return std::dynamic_pointer_cast<ModDataContent>(checker);
});
modDataContent().setContents(std::move(contents));
emit modDataContentUpdated(gameFeature<ModDataContent>());
emit modDataContentUpdated(gameFeature<ModDataContent>().get());
}
}

Expand Down Expand Up @@ -313,20 +321,20 @@ int GameFeatures::unregisterGameFeatures(MOBase::IPlugin* plugin,
return removed;
}

GameFeature* GameFeatures::gameFeature(std::type_info const& info) const
std::shared_ptr<GameFeature> GameFeatures::gameFeature(std::type_info const& info) const
{
if (info == ModDataCheckerIndex) {
return modDataChecker().isValid() ? m_modDataChecker.get() : nullptr;
return modDataChecker().isValid() ? m_modDataChecker : nullptr;
}

if (info == ModDataContentIndex) {
return modDataContent().isValid() ? m_modDataContent.get() : nullptr;
return modDataContent().isValid() ? m_modDataContent : nullptr;
}

auto it = m_currentFeatures.find(info);
if (it == m_currentFeatures.end() || it->second.empty()) {
return nullptr;
}

return it->second.front().get();
return it->second.front();
}
12 changes: 7 additions & 5 deletions src/game_features.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class GameFeatures : public QObject
*/
GameFeatures(OrganizerCore* core, PluginContainer* plugins);

~GameFeatures();

// register game features
//
bool registerGameFeature(MOBase::IPlugin* plugin, QStringList const& games,
Expand All @@ -48,9 +50,9 @@ class GameFeatures : public QObject
// retrieve a game feature
//
template <class T>
T* gameFeature() const
std::shared_ptr<T> gameFeature() const
{
return dynamic_cast<T*>(gameFeature(typeid(T)));
return std::dynamic_pointer_cast<T>(gameFeature(typeid(T)));
}

signals:
Expand Down Expand Up @@ -88,7 +90,7 @@ class GameFeatures : public QObject

// retrieve a game feature from info
//
MOBase::GameFeature* gameFeature(std::type_info const& index) const;
std::shared_ptr<MOBase::GameFeature> gameFeature(std::type_info const& index) const;

// update current features by filtering
//
Expand All @@ -108,8 +110,8 @@ class GameFeatures : public QObject
std::unordered_map<std::type_index, std::vector<std::shared_ptr<MOBase::GameFeature>>>
m_currentFeatures;

std::unique_ptr<MOBase::ModDataChecker> m_modDataChecker;
std::unique_ptr<MOBase::ModDataContent> m_modDataContent;
std::shared_ptr<CombinedModDataChecker> m_modDataChecker;
std::shared_ptr<CombinedModDataContent> m_modDataContent;
};

#endif
2 changes: 1 addition & 1 deletion src/gamefeaturesproxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ bool GameFeaturesProxy::unregisterFeature(std::shared_ptr<MOBase::GameFeature> f
return m_Features.unregisterGameFeature(feature);
}

MOBase::GameFeature*
std::shared_ptr<MOBase::GameFeature>
GameFeaturesProxy::gameFeatureImpl(std::type_info const& info) const
{
return m_Features.gameFeature(info);
Expand Down
4 changes: 2 additions & 2 deletions src/gamefeaturesproxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include "igamefeatures.h"


class GameFeatures;
class OrganizerProxy;

Expand All @@ -23,7 +22,8 @@ class GameFeaturesProxy : public MOBase::IGameFeatures
bool unregisterFeature(std::shared_ptr<MOBase::GameFeature> feature) override;

protected:
MOBase::GameFeature* gameFeatureImpl(std::type_info const& info) const override;
std::shared_ptr<MOBase::GameFeature>
gameFeatureImpl(std::type_info const& info) const override;
int unregisterFeaturesImpl(std::type_info const& info) override;

private:
Expand Down
17 changes: 6 additions & 11 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1796,16 +1796,14 @@ void MainWindow::on_profileBox_currentIndexChanged(int index)

activateSelectedProfile();

LocalSavegames* saveGames =
m_OrganizerCore.gameFeatures().gameFeature<LocalSavegames>();
auto saveGames = m_OrganizerCore.gameFeatures().gameFeature<LocalSavegames>();
if (saveGames != nullptr) {
if (saveGames->prepareProfile(m_OrganizerCore.currentProfile())) {
m_SavesTab->refreshSaveList();
}
}

BSAInvalidation* invalidation =
m_OrganizerCore.gameFeatures().gameFeature<BSAInvalidation>();
auto invalidation = m_OrganizerCore.gameFeatures().gameFeature<BSAInvalidation>();
if (invalidation != nullptr) {
if (invalidation->prepareProfile(m_OrganizerCore.currentProfile())) {
QTimer::singleShot(5, [this] {
Expand Down Expand Up @@ -1923,8 +1921,7 @@ void MainWindow::updateBSAList(const QStringList& defaultArchives,
ui->bsaList->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
std::vector<std::pair<UINT32, QTreeWidgetItem*>> items;

BSAInvalidation* invalidation =
m_OrganizerCore.gameFeatures().gameFeature<BSAInvalidation>();
auto invalidation = m_OrganizerCore.gameFeatures().gameFeature<BSAInvalidation>();
std::vector<FileEntryPtr> files = m_OrganizerCore.directoryStructure()->getFiles();

QStringList plugins =
Expand Down Expand Up @@ -2033,7 +2030,7 @@ void MainWindow::updateBSAList(const QStringList& defaultArchives,

void MainWindow::checkBSAList()
{
DataArchives* archives = m_OrganizerCore.gameFeatures().gameFeature<DataArchives>();
auto archives = m_OrganizerCore.gameFeatures().gameFeature<DataArchives>();

if (archives != nullptr) {
ui->bsaList->blockSignals(true);
Expand Down Expand Up @@ -2385,16 +2382,14 @@ void MainWindow::on_actionAdd_Profile_triggered()
}
}

LocalSavegames* saveGames =
m_OrganizerCore.gameFeatures().gameFeature<LocalSavegames>();
auto saveGames = m_OrganizerCore.gameFeatures().gameFeature<LocalSavegames>();
if (saveGames != nullptr) {
if (saveGames->prepareProfile(m_OrganizerCore.currentProfile())) {
m_SavesTab->refreshSaveList();
}
}

BSAInvalidation* invalidation =
m_OrganizerCore.gameFeatures().gameFeature<BSAInvalidation>();
auto invalidation = m_OrganizerCore.gameFeatures().gameFeature<BSAInvalidation>();
if (invalidation != nullptr) {
if (invalidation->prepareProfile(m_OrganizerCore.currentProfile())) {
QTimer::singleShot(5, [this] {
Expand Down
6 changes: 3 additions & 3 deletions src/modinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,9 @@ void ModInfo::updateFromDisc(const QString& modsDirectory, OrganizerCore& core,
}
}

auto* game = core.managedGame();
auto& features = core.pluginContainer().gameFeatures();
auto* unmanaged = features.gameFeature<UnmanagedMods>();
auto* game = core.managedGame();
auto& features = core.pluginContainer().gameFeatures();
auto unmanaged = features.gameFeature<UnmanagedMods>();
if (unmanaged != nullptr) {
for (const QString& modName : unmanaged->mods(!displayForeign)) {
ModInfo::EModType modType =
Expand Down
2 changes: 1 addition & 1 deletion src/modinforegular.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ std::vector<ModInfo::EFlag> ModInfoRegular::getFlags() const

std::set<int> ModInfoRegular::doGetContents() const
{
ModDataContent* contentFeature =
auto contentFeature =
m_Core.pluginContainer().gameFeatures().gameFeature<ModDataContent>();

if (contentFeature) {
Expand Down
Loading

0 comments on commit 629762d

Please sign in to comment.