@@ -39,6 +39,8 @@ along with Mod Organizer. If not, see <http://www.gnu.org/licenses/>.
3939#include < QMessageBox>
4040#include < QStandardPaths>
4141#include < cstdint>
42+ #include < memory>
43+ #include < mutex>
4244
4345using namespace MOBase ;
4446
@@ -57,11 +59,25 @@ QString Instance::displayName() const
5759
5860QString Instance::gameName () const
5961{
62+ if (!m_iniValuesRead && m_gameName.isEmpty ()) {
63+ std::scoped_lock lock (m_iniValuesMutex);
64+ if (!m_iniValuesRead) {
65+ readFromIni ();
66+ }
67+ }
68+
6069 return m_gameName;
6170}
6271
6372QString Instance::gameDirectory () const
6473{
74+ if (!m_iniValuesRead && m_gameDir.isEmpty ()) {
75+ std::scoped_lock lock (m_iniValuesMutex);
76+ if (!m_iniValuesRead) {
77+ readFromIni ();
78+ }
79+ }
80+
6581 return m_gameDir;
6682}
6783
@@ -72,6 +88,13 @@ QString Instance::directory() const
7288
7389QString Instance::baseDirectory () const
7490{
91+ if (!m_iniValuesRead) {
92+ std::scoped_lock lock (m_iniValuesMutex);
93+ if (!m_iniValuesRead) {
94+ readFromIni ();
95+ }
96+ }
97+
7598 return m_baseDir;
7699}
77100
@@ -82,6 +105,13 @@ MOBase::IPluginGame* Instance::gamePlugin() const
82105
83106QString Instance::profileName () const
84107{
108+ if (!m_iniValuesRead) {
109+ std::scoped_lock lock (m_iniValuesMutex);
110+ if (!m_iniValuesRead) {
111+ readFromIni ();
112+ }
113+ }
114+
85115 return m_profile;
86116}
87117
@@ -110,12 +140,13 @@ bool Instance::isActive() const
110140 return false ;
111141}
112142
113- bool Instance::readFromIni ()
143+ bool Instance::readFromIni () const
114144{
115145 Settings s (iniPath ());
116146
117147 if (s.iniStatus () != QSettings::NoError) {
118148 log::error (" can't read ini {}" , iniPath ());
149+ m_iniValuesRead = true ;
119150 return false ;
120151 }
121152
@@ -143,14 +174,18 @@ bool Instance::readFromIni()
143174 // figuring out profile from ini if it's missing
144175 getProfile (s);
145176
177+ m_iniValuesRead = true ;
146178 return true ;
147179}
148180
149181Instance::SetupResults Instance::setup (PluginContainer& plugins)
150182{
151- // read initial values from the ini
152- if (!readFromIni ()) {
153- return SetupResults::BadIni;
183+ if (!m_iniValuesRead) {
184+ std::scoped_lock lock (m_iniValuesMutex);
185+ // read initial values from the ini
186+ if (!m_iniValuesRead && !readFromIni ()) {
187+ return SetupResults::BadIni;
188+ }
154189 }
155190
156191 // getting game plugin
@@ -310,7 +345,7 @@ Instance::SetupResults Instance::getGamePlugin(PluginContainer& plugins)
310345 }
311346}
312347
313- void Instance::getProfile (const Settings& s)
348+ void Instance::getProfile (const Settings& s) const
314349{
315350 if (!m_profile.isEmpty ()) {
316351 // there's already a profile set up, probably an override
@@ -511,7 +546,7 @@ void InstanceManager::clearOverrides()
511546 m_overrideProfileName = {};
512547}
513548
514- std::unique_ptr <Instance> InstanceManager::currentInstance () const
549+ std::shared_ptr <Instance> InstanceManager::currentInstance () const
515550{
516551 const QString profile = m_overrideProfileName ? *m_overrideProfileName : " " ;
517552
@@ -520,20 +555,20 @@ std::unique_ptr<Instance> InstanceManager::currentInstance() const
520555
521556 if (!allowedToChangeInstance ()) {
522557 // force portable instance
523- return std::make_unique <Instance>(portablePath (), true , profile);
558+ return std::make_shared <Instance>(portablePath (), true , profile);
524559 }
525560
526561 if (name.isEmpty ()) {
527562 if (portableInstanceExists ()) {
528563 // use portable
529- return std::make_unique <Instance>(portablePath (), true , profile);
564+ return std::make_shared <Instance>(portablePath (), true , profile);
530565 } else {
531566 // no instance set
532567 return {};
533568 }
534569 }
535570
536- return std::make_unique <Instance>(instancePath (name), false , profile);
571+ return std::make_shared <Instance>(instancePath (name), false , profile);
537572}
538573
539574void InstanceManager::clearCurrentInstance ()
@@ -583,6 +618,16 @@ std::vector<QString> InstanceManager::globalInstancePaths() const
583618 return list;
584619}
585620
621+ std::shared_ptr<const Instance>
622+ InstanceManager::getGlobalInstance (const QString& instanceName) const
623+ {
624+ if (!instanceExists (instanceName)) {
625+ return nullptr ;
626+ }
627+
628+ return std::make_shared<const Instance>(instancePath (instanceName), false , " " );
629+ }
630+
586631bool InstanceManager::hasAnyInstances () const
587632{
588633 return portableInstanceExists () || !globalInstancePaths ().empty ();
@@ -696,7 +741,7 @@ bool InstanceManager::instanceExists(const QString& instanceName) const
696741 return root.exists (instanceName);
697742}
698743
699- std::unique_ptr <Instance> selectInstance ()
744+ std::shared_ptr <Instance> selectInstance ()
700745{
701746 auto & m = InstanceManager::singleton ();
702747
0 commit comments