diff --git a/launcher/BaseInstance.cpp b/launcher/BaseInstance.cpp index 1aa01568c..2187bc09a 100644 --- a/launcher/BaseInstance.cpp +++ b/launcher/BaseInstance.cpp @@ -69,6 +69,7 @@ BaseInstance::BaseInstance(SettingsObjectPtr globalSettings, SettingsObjectPtr s m_settings->registerSetting("lastTimePlayed", 0); m_settings->registerSetting("linkedInstances", "[]"); + m_settings->registerSetting("shortcuts", QVariant::fromValue(QList{})); // Game time override auto gameTimeOverride = m_settings->registerSetting("OverrideGameTime", false); @@ -400,13 +401,21 @@ bool BaseInstance::syncInstanceDirName(const QString& newRoot) const void BaseInstance::registerShortcut(const ShortcutData& data) { - m_shortcuts.append(data); + auto currentShortcuts = shortcuts(); + currentShortcuts.append(data); qDebug() << "Registering shortcut for instance" << id() << "with name" << data.name << "and path" << data.filePath; + setShortcuts(currentShortcuts); } -QList& BaseInstance::getShortcuts() +void BaseInstance::setShortcuts(const QList& shortcuts) { - return m_shortcuts; + // FIXME: if no change, do not set. setting involves saving a file. + m_settings->set("shortcuts", QVariant::fromValue(shortcuts)); +} + +QList BaseInstance::shortcuts() const +{ + return m_settings->get("shortcuts").value>(); } QString BaseInstance::name() const diff --git a/launcher/BaseInstance.h b/launcher/BaseInstance.h index d2ff64e7e..52aa39067 100644 --- a/launcher/BaseInstance.h +++ b/launcher/BaseInstance.h @@ -38,6 +38,7 @@ #pragma once #include +#include #include #include #include @@ -74,7 +75,18 @@ enum class ShortcutTarget { Desktop, Applications, Other }; struct ShortcutData { QString name; QString filePath; - ShortcutTarget target; + ShortcutTarget target = ShortcutTarget::Other; + + friend QDataStream& operator<<(QDataStream& out, const ShortcutData& data) + { + out << data.name << data.filePath << data.target; + return out; + } + friend QDataStream& operator>>(QDataStream& in, ShortcutData& data) + { + in >> data.name >> data.filePath >> data.target; + return in; + } }; /*! @@ -142,7 +154,8 @@ class BaseInstance : public QObject, public std::enable_shared_from_this& getShortcuts(); + QList shortcuts() const; + void setShortcuts(const QList& shortcuts); /// Value used for instance window titles QString windowTitle() const; @@ -323,10 +336,9 @@ class BaseInstance : public QObject, public std::enable_shared_from_this m_shortcuts; }; Q_DECLARE_METATYPE(shared_qobject_ptr) +Q_DECLARE_METATYPE(ShortcutData) // Q_DECLARE_METATYPE(BaseInstance::InstanceFlag) // Q_DECLARE_OPERATORS_FOR_FLAGS(BaseInstance::InstanceFlags) diff --git a/launcher/InstanceList.cpp b/launcher/InstanceList.cpp index ef8be4919..b98f51d82 100644 --- a/launcher/InstanceList.cpp +++ b/launcher/InstanceList.cpp @@ -357,25 +357,20 @@ bool InstanceList::trashInstance(const InstanceId& id) m_trashHistory.push({ id, inst->instanceRoot(), trashedLoc, cachedGroupId }); // Also trash all of its shortcuts; we remove the shortcuts if trash fails since it is invalid anyway - auto& shortcuts = inst->getShortcuts(); - for (auto it = shortcuts.begin(); it != shortcuts.end();) { - const auto& [name, filePath, target] = *it; + for (const auto& [name, filePath, target] : inst->shortcuts()) { if (!FS::trash(filePath, &trashedLoc)) { qWarning() << "Trash of shortcut" << name << "at path" << filePath << "for instance" << id << "has not been successful, trying to delete it instead..."; if (!FS::deletePath(filePath)) { qWarning() << "Deletion of shortcut" << name << "at path" << filePath << "for instance" << id << "has not been successful, given up..."; - ++it; } else { qDebug() << "Shortcut" << name << "at path" << filePath << "for instance" << id << "has been deleted by the launcher."; - it = shortcuts.erase(it); } continue; } qDebug() << "Shortcut" << name << "at path" << filePath << "for instance" << id << "has been trashed by the launcher."; - m_trashHistory.top().shortcuts.append({ *it, trashedLoc }); - it = shortcuts.erase(it); + m_trashHistory.top().shortcuts.append({ { name, filePath, target }, trashedLoc }); } return true; @@ -453,7 +448,7 @@ void InstanceList::deleteInstance(const InstanceId& id) qDebug() << "Instance" << id << "has been deleted by the launcher."; - for (const auto& [name, filePath, target] : inst->getShortcuts()) { + for (const auto& [name, filePath, target] : inst->shortcuts()) { if (!FS::deletePath(filePath)) { qWarning() << "Deletion of shortcut" << name << "at path" << filePath << "for instance" << id << "has not been successful..."; continue; @@ -675,6 +670,7 @@ InstancePtr InstanceList::loadInstance(const InstanceId& id) } auto instanceRoot = FS::PathCombine(m_instDir, id); + qRegisterMetaType>("QList"); auto instanceSettings = std::make_shared(FS::PathCombine(instanceRoot, "instance.cfg")); InstancePtr inst; @@ -690,6 +686,22 @@ InstancePtr InstanceList::loadInstance(const InstanceId& id) inst.reset(new NullInstance(m_globalSettings, instanceSettings, instanceRoot)); } qDebug() << "Loaded instance" << inst->name() << "from" << inst->instanceRoot(); + + // Fixup the shortcuts by pruning all non-existing links + auto shortcut = inst->shortcuts(); + for (auto it = shortcut.begin(); it != shortcut.end();) { + const auto& [name, filePath, target] = *it; + if (!QDir(filePath).exists()) { + qWarning() << "Shortcut" << name << "have non-existent path" << filePath; + it = shortcut.erase(it); + continue; + } + ++it; + } + inst->setShortcuts(shortcut); + if (!shortcut.isEmpty()) + qDebug() << "Loaded" << shortcut.size() << "shortcut(s) for instance" << inst->name(); + return inst; } diff --git a/launcher/minecraft/MinecraftInstance.cpp b/launcher/minecraft/MinecraftInstance.cpp index 635cecfac..72c863bdb 100644 --- a/launcher/minecraft/MinecraftInstance.cpp +++ b/launcher/minecraft/MinecraftInstance.cpp @@ -1040,6 +1040,10 @@ QString MinecraftInstance::getStatusbarDescription() .arg(Time::prettifyDuration(totalTimePlayed(), APPLICATION->settings()->get("ShowGameTimeWithoutDays").toBool()))); } } + auto currentShortcuts = shortcuts(); + if (!currentShortcuts.isEmpty()) { + description.append(tr(", %n shortcut(s) registered", "", currentShortcuts.size())); + } if (hasCrashed()) { description.append(tr(", has crashed.")); }