From 480a57cf037a46f176128e6c94aa5616fcf704a3 Mon Sep 17 00:00:00 2001 From: SulfurNitride Date: Tue, 17 Feb 2026 23:59:40 -0600 Subject: Fix Wine prefix deployment, INI handling, and data directory paths MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Fix data directory path to use ~/.var/app/com.fluorine.manager consistently (was ~/.local/share/fluorine in committed code) - Fix VFS helper path in fuseconnector.cpp to use fluorineDataDir() - Fix localAppFolder() to resolve Wine prefix AppData/Local on Linux instead of returning XDG ~/.local/share - Add localAppName() virtual method for correct AppData folder mapping (Enderal→"enderal", Nehrim→"Oblivion") - Fix mergeTweak() to use direct INI parser instead of QSettings which corrupts backslashes and URL-encodes spaces in keys - Make resolveWineDataDirName() more robust with existence checks and fallback chain (documentsDirectory → gameShortName → gameName) - Add comprehensive debug logging throughout Wine prefix deployment - Fix INI case handling: copy instead of symlink for case-mismatched INIs, ensure both proper-case and lowercase aliases exist - Remove native build script (Flatpak only) Co-Authored-By: Claude Opus 4.6 --- src/plugins/rootbuilder.py | 24 ++- src/src/CMakeLists.txt | 11 ++ src/src/commandline.cpp | 22 +-- src/src/fluorinepaths.cpp | 6 +- src/src/fluorinepaths.h | 8 +- src/src/fuseconnector.cpp | 51 +++++- src/src/fuseconnector.h | 1 + src/src/instancemanager.cpp | 15 +- src/src/instancemanagerdialog.cpp | 131 ++++++++++---- src/src/instancemanagerdialog.h | 4 + src/src/instancemanagerdialog.ui | 38 ++++ src/src/loglist.cpp | 2 +- src/src/mainwindow.cpp | 15 ++ src/src/moapplication.cpp | 43 ++++- src/src/organizercore.cpp | 64 +++++-- src/src/process_helper_main.cpp | 359 ++++++++++++++++++++++++++++++++++++++ src/src/processrunner.cpp | 32 +++- src/src/processrunner.h | 3 + src/src/profile.cpp | 37 ++-- src/src/protonlauncher.cpp | 118 ++++++++++++- src/src/protonlauncher.h | 8 + src/src/settingsdialog.ui | 32 ++++ src/src/settingsdialogproton.cpp | 101 +++++++++-- src/src/settingsdialogproton.h | 1 + src/src/settingsdialogtheme.cpp | 55 +++++- src/src/spawn.cpp | 3 +- src/src/spawn.h | 2 + src/src/vfs/vfs_helper_main.cpp | 17 +- src/src/wineprefix.cpp | 77 +++++++- 29 files changed, 1126 insertions(+), 154 deletions(-) create mode 100644 src/src/process_helper_main.cpp (limited to 'src') diff --git a/src/plugins/rootbuilder.py b/src/plugins/rootbuilder.py index fbe082c..dcc6706 100644 --- a/src/plugins/rootbuilder.py +++ b/src/plugins/rootbuilder.py @@ -69,8 +69,20 @@ def _host_cp(src: str, dst: str) -> bool: return False +def _ensure_readable(path: str): + """Ensure a file has owner-read permission (mod archives sometimes strip it).""" + try: + st = os.stat(path) + if not (st.st_mode & 0o400): + os.chmod(path, st.st_mode | 0o400) + except OSError: + pass + + def _reflink_copy(src: str, dst: str): """Copy with reflink (CoW) if supported, fallback to regular copy.""" + _ensure_readable(src) + last_err = None try: subprocess.run( ["cp", "--reflink=auto", "-f", "--", src, dst], @@ -78,16 +90,18 @@ def _reflink_copy(src: str, dst: str): capture_output=True, ) return - except (subprocess.CalledProcessError, FileNotFoundError): - pass + except subprocess.CalledProcessError as e: + last_err = f"cp failed (exit {e.returncode}): {e.stderr.decode(errors='replace').strip()}" + except FileNotFoundError: + last_err = "cp command not found" try: shutil.copy2(src, dst) return - except OSError: - pass + except OSError as e: + last_err = f"{e.strerror} (errno {e.errno})" if _IN_FLATPAK and _host_cp(src, dst): return - raise OSError(f"Root Builder: failed to copy {src} -> {dst}") + raise OSError(f"Root Builder: failed to copy {src} -> {dst}: {last_err}") def _ensure_writable(path: str): diff --git a/src/src/CMakeLists.txt b/src/src/CMakeLists.txt index 699dfae..ab8b6c3 100644 --- a/src/src/CMakeLists.txt +++ b/src/src/CMakeLists.txt @@ -52,6 +52,10 @@ list(REMOVE_ITEM ORGANIZER_HEADERS list(REMOVE_ITEM ORGANIZER_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/vfs/vfs_helper_main.cpp) +# Process helper has its own main() — exclude from organizer +list(REMOVE_ITEM ORGANIZER_SOURCES + ${CMAKE_CURRENT_SOURCE_DIR}/process_helper_main.cpp) + # Remove WebEngine-dependent sources when not available if(NOT Qt6WebEngineWidgets_FOUND) list(REMOVE_ITEM ORGANIZER_SOURCES @@ -153,6 +157,13 @@ if(NOT WIN32) target_compile_definitions(mo2-vfs-helper PRIVATE FUSE_USE_VERSION=35) target_compile_features(mo2-vfs-helper PRIVATE cxx_std_23) + # ── Standalone process helper for Flatpak game launching ── + # Keeps the flatpak-spawn proxy alive while monitoring the game process tree. + add_executable(mo2-process-helper + process_helper_main.cpp) + target_link_libraries(mo2-process-helper PRIVATE Threads::Threads) + target_compile_features(mo2-process-helper PRIVATE cxx_std_23) + option(MO2_BUNDLE_7Z_RUNTIME "Copy a Linux 7z module into organizer/dlls" ON) option(MO2_STAGE_PYTHON_PLUGIN_PAYLOAD "Stage shipped Python plugin payload into build plugins/ for Linux runs" ON) diff --git a/src/src/commandline.cpp b/src/src/commandline.cpp index 9c1fa85..09aabb8 100644 --- a/src/src/commandline.cpp +++ b/src/src/commandline.cpp @@ -984,16 +984,16 @@ std::optional CreatePortableCommand::runEarly() } } - // Create empty profile files - const QStringList profileFiles = {"modlist.txt", "plugins.txt", "loadorder.txt", "initweaks.ini"}; - for (const auto& f : profileFiles) { - QFile file(QDir(instanceDir).filePath("profiles/Default/" + f)); - if (!file.open(QIODevice::WriteOnly)) { - std::cerr << "Error: failed to create file: " << f.toStdString() << "\n"; - return 1; - } - file.close(); - } + // Create empty profile files + const QStringList profileFiles = {"modlist.txt", "plugins.txt", "loadorder.txt", "initweaks.ini"}; + for (const auto& f : profileFiles) { + QFile file(QDir(instanceDir).filePath("profiles/Default/" + f)); + if (!file.open(QIODevice::WriteOnly)) { + std::cerr << "Error: failed to create file: " << f.toStdString() << "\n"; + return 1; + } + file.close(); + } // Generate ModOrganizer.ini { @@ -1067,7 +1067,7 @@ std::optional ListInstancesCommand::runEarly() const QStringList searchPaths = { QDir::currentPath(), QDir::homePath(), - QDir(QDir::homePath()).filePath(".local/share/fluorine"), + QDir(QDir::homePath()).filePath(".var/app/com.fluorine.manager"), }; bool found = false; diff --git a/src/src/fluorinepaths.cpp b/src/src/fluorinepaths.cpp index 815f5aa..98cb7aa 100644 --- a/src/src/fluorinepaths.cpp +++ b/src/src/fluorinepaths.cpp @@ -8,13 +8,11 @@ #include static const QString OldRoot = - QDir::homePath() + "/.var/app/com.fluorine.manager"; + QDir::homePath() + "/.local/share/fluorine"; QString fluorineDataDir() { - // Use $HOME directly so this resolves the same path in both native - // and Flatpak builds (the Flatpak has --filesystem=home). - return QDir::homePath() + "/.local/share/fluorine"; + return QDir::homePath() + "/.var/app/com.fluorine.manager"; } void fluorineMigrateDataDir() diff --git a/src/src/fluorinepaths.h b/src/src/fluorinepaths.h index a32f6ec..ab7f119 100644 --- a/src/src/fluorinepaths.h +++ b/src/src/fluorinepaths.h @@ -3,13 +3,11 @@ #include -/// Returns the shared Fluorine data directory: ~/.local/share/fluorine -/// Uses $HOME directly to bypass Flatpak's XDG_DATA_HOME remapping -/// (the Flatpak has --filesystem=home). +/// Returns the Fluorine data directory: ~/.var/app/com.fluorine.manager QString fluorineDataDir(); -/// One-time migration from the old ~/.var/app/com.fluorine.manager/ path -/// to ~/.local/share/fluorine/. Call before initLogging(). +/// One-time migration from ~/.local/share/fluorine/ back to +/// ~/.var/app/com.fluorine.manager/. Call before initLogging(). void fluorineMigrateDataDir(); #endif // FLUORINEPATHS_H diff --git a/src/src/fuseconnector.cpp b/src/src/fuseconnector.cpp index a276074..2c5454f 100644 --- a/src/src/fuseconnector.cpp +++ b/src/src/fuseconnector.cpp @@ -1,5 +1,6 @@ #include "fuseconnector.h" +#include "fluorinepaths.h" #include "settings.h" #include "vfs/vfstree.h" @@ -277,6 +278,9 @@ bool FuseConnector::mount( std::error_code ec; fs::create_directories(m_stagingDir, ec); fs::create_directories(m_overwriteDir, ec); + if (!m_customOutputDir.empty()) { + fs::create_directories(m_customOutputDir, ec); + } // Scan + cache base game files BEFORE mounting (after mount they're hidden). // Reuse the cache across mount/unmount cycles since base game files don't @@ -432,9 +436,8 @@ void FuseConnector::rebuild( m_lastMods = mods; if (m_helperProcess) { - const QString dataDir = - QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation); - const QString configPath = QDir(dataDir).filePath("fluorine/vfs.cfg"); + const QString dataDir = fluorineDataDir(); + const QString configPath = QDir(dataDir).filePath("vfs.cfg"); writeVfsConfig(configPath, QString::fromStdString(m_mountPoint), overwrite_dir, QString::fromStdString(m_gameDir), data_dir_name, mods); @@ -471,6 +474,26 @@ void FuseConnector::updateMapping(const MappingType& mapping) auto mods = buildModsFromMapping(mapping, dataDirPath, overwriteDir); + // Check if any mapping has createTarget set — that mod directory should + // receive newly created files instead of the overwrite directory. + m_customOutputDir.clear(); + for (const auto& map : mapping) { + if (map.createTarget) { + log::debug("Found createTarget mapping: source='{}', dest='{}', isDir={}", + map.source, map.destination, map.isDirectory); + } + if (map.createTarget && map.isDirectory) { + m_customOutputDir = + QDir::cleanPath(QDir::fromNativeSeparators(map.source)).toStdString(); + log::debug("Custom output directory set to: {}", + QString::fromStdString(m_customOutputDir)); + break; + } + } + if (m_customOutputDir.empty()) { + log::debug("No createTarget mapping found, using overwrite dir"); + } + // Deploy non-data-dir mappings as real symlinks and collect file-level // data-dir mappings for VFS tree injection. deployExternalMappings(mapping, dataDirPath); @@ -623,8 +646,17 @@ void FuseConnector::flushStaging() } const fs::path staging(m_stagingDir); - const fs::path overwrite(m_overwriteDir); + const fs::path overwrite = m_customOutputDir.empty() + ? fs::path(m_overwriteDir) + : fs::path(m_customOutputDir); + + log::debug("flushStaging: staging='{}', customOutput='{}', dest='{}'", + QString::fromStdString(m_stagingDir), + QString::fromStdString(m_customOutputDir), + QString::fromStdString(overwrite.string())); + if (!fs::exists(staging)) { + log::debug("flushStaging: staging dir does not exist, nothing to flush"); return; } @@ -757,11 +789,10 @@ bool FuseConnector::mountViaHelper( const QString& data_dir_name, const std::vector>& mods) { - const QString dataDir = - QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation); - const QString configPath = QDir(dataDir).filePath("fluorine/vfs.cfg"); + const QString dataDir = fluorineDataDir(); + const QString configPath = QDir(dataDir).filePath("vfs.cfg"); const QString helperBin = - QDir(dataDir).filePath("fluorine/bin/mo2-vfs-helper"); + QDir(dataDir).filePath("bin/mo2-vfs-helper"); if (!QFile::exists(helperBin)) { throw FuseConnectorException( @@ -824,6 +855,10 @@ void FuseConnector::writeVfsConfig( out << "data_dir_name=" << data_dir_name << "\n"; out << "overwrite_dir=" << overwrite_dir << "\n"; + if (!m_customOutputDir.empty()) { + out << "output_dir=" << QString::fromStdString(m_customOutputDir) << "\n"; + } + for (const auto& [name, path] : mods) { out << "mod=" << QString::fromStdString(name) << "|" << QString::fromStdString(path) << "\n"; diff --git a/src/src/fuseconnector.h b/src/src/fuseconnector.h index fd5cb01..cf99d64 100644 --- a/src/src/fuseconnector.h +++ b/src/src/fuseconnector.h @@ -67,6 +67,7 @@ private: std::string m_mountPoint; std::string m_stagingDir; std::string m_overwriteDir; + std::string m_customOutputDir; std::string m_gameDir; std::string m_dataDirName; std::string m_dataDirPath; diff --git a/src/src/instancemanager.cpp b/src/src/instancemanager.cpp index 6620e3c..1269d26 100644 --- a/src/src/instancemanager.cpp +++ b/src/src/instancemanager.cpp @@ -802,9 +802,10 @@ std::unique_ptr selectInstance() PluginContainer pc(nullptr); pc.loadPlugins(); - if (m.hasAnyInstances()) { - // there is at least one instance available, show the instance manager - // dialog + { + // show the instance manager dialog; it has both "Create new instance" and + // "Open existing portable" buttons, so it works whether or not instances + // already exist InstanceManagerDialog dlg(pc); // the dialog normally restarts MO when an instance is selected, but this @@ -815,14 +816,6 @@ std::unique_ptr selectInstance() dlg.activateWindow(); dlg.raise(); - if (dlg.exec() != QDialog::Accepted) { - return {}; - } - - } else { - // no instances configured, ask the user to create one - CreateInstanceDialog dlg(pc, nullptr); - if (dlg.exec() != QDialog::Accepted) { return {}; } diff --git a/src/src/instancemanagerdialog.cpp b/src/src/instancemanagerdialog.cpp index d9a4a74..cd4b865 100644 --- a/src/src/instancemanagerdialog.cpp +++ b/src/src/instancemanagerdialog.cpp @@ -8,7 +8,9 @@ #include "shared/appconfig.h" #include "shared/util.h" #include "ui_instancemanagerdialog.h" +#include #include +#include #include #include #include @@ -205,6 +207,9 @@ InstanceManagerDialog::InstanceManagerDialog(PluginContainer& pc, QWidget* paren connect(ui->openINI, &QPushButton::clicked, [&] { openINI(); }); + connect(ui->removeFromList, &QPushButton::clicked, [&] { + removeFromList(); + }); connect(ui->deleteInstance, &QPushButton::clicked, [&] { deleteInstance(); }); @@ -522,6 +527,45 @@ void InstanceManagerDialog::openINI() } } +void InstanceManagerDialog::removeFromList() +{ + const auto* i = singleSelection(); + if (!i) { + return; + } + + auto& m = InstanceManager::singleton(); + if (i->isActive()) { + QMessageBox::information(this, tr("Remove from list"), + tr("The active instance cannot be removed.")); + return; + } + + const auto r = QMessageBox::question( + this, tr("Remove from list"), + tr("Remove \"%1\" from the instance list?\n\n" + "No files will be deleted.") + .arg(i->displayName()), + QMessageBox::Yes | QMessageBox::Cancel); + + if (r != QMessageBox::Yes) { + return; + } + + if (i->isPortable()) { + m.unregisterPortableInstance(i->directory()); + } else { + // for global instances, rename the INI so it's no longer auto-discovered + const QString ini = i->iniPath(); + if (!ini.isEmpty() && QFile::exists(ini)) { + QFile::rename(ini, ini + ".disabled"); + } + } + + updateInstances(); + updateList(); +} + void InstanceManagerDialog::deleteInstance() { const auto* i = singleSelection(); @@ -538,7 +582,6 @@ void InstanceManagerDialog::deleteInstance() // creating dialog - const auto Recycle = QMessageBox::Save; const auto Delete = QMessageBox::Yes; const auto Cancel = QMessageBox::Cancel; @@ -547,10 +590,9 @@ void InstanceManagerDialog::deleteInstance() MOBase::TaskDialog dlg(this); dlg.title(tr("Deleting instance")) - .main(tr("These files and folders will be deleted")) + .main(tr("These files and folders will be permanently deleted")) .content(tr("All checked items will be deleted.")) .icon(QMessageBox::Warning) - .button({tr("Move to the recycle bin"), Recycle}) .button({tr("Delete permanently"), Delete}) .button({tr("Cancel"), Cancel}); @@ -584,7 +626,7 @@ void InstanceManagerDialog::deleteInstance() const auto r = dlg.exec(); - if (r != Recycle && r != Delete) { + if (r != Delete) { return; } @@ -604,7 +646,7 @@ void InstanceManagerDialog::deleteInstance() } // deleting - if (!doDelete(selected, (r == Recycle))) { + if (!doDelete(selected, false)) { return; } @@ -690,37 +732,37 @@ void InstanceManagerDialog::createNew() select(dlg.creationInfo().instanceName); } -std::size_t InstanceManagerDialog::singleSelectionIndex() const -{ - const auto sel = - m_filter.mapSelectionToSource(ui->list->selectionModel()->selection()); - - if (sel.size() != 1) { - return NoSelection; - } - - const auto indexes = sel.indexes(); - if (indexes.size() != 1 || !indexes[0].isValid()) { - return NoSelection; - } - - const int row = indexes[0].row(); - if (row < 0 || static_cast(row) >= m_instances.size()) { - return NoSelection; - } - - return static_cast(row); -} - -const Instance* InstanceManagerDialog::singleSelection() const -{ - const auto i = singleSelectionIndex(); - if (i == NoSelection || i >= m_instances.size()) { - return nullptr; - } - - return m_instances[i].get(); -} +std::size_t InstanceManagerDialog::singleSelectionIndex() const +{ + const auto sel = + m_filter.mapSelectionToSource(ui->list->selectionModel()->selection()); + + if (sel.size() != 1) { + return NoSelection; + } + + const auto indexes = sel.indexes(); + if (indexes.size() != 1 || !indexes[0].isValid()) { + return NoSelection; + } + + const int row = indexes[0].row(); + if (row < 0 || static_cast(row) >= m_instances.size()) { + return NoSelection; + } + + return static_cast(row); +} + +const Instance* InstanceManagerDialog::singleSelection() const +{ + const auto i = singleSelectionIndex(); + if (i == NoSelection || i >= m_instances.size()) { + return nullptr; + } + + return m_instances[i].get(); +} void InstanceManagerDialog::fillData(const Instance& ii) { @@ -729,6 +771,20 @@ void InstanceManagerDialog::fillData(const Instance& ii) ui->baseDirectory->setText(ii.baseDirectory()); ui->gameName->setText(ii.gameName()); ui->gameDir->setText(ii.gameDirectory()); + + // read prefix info from the instance's INI + { + const QString ini = ii.iniPath(); + if (!ini.isEmpty() && QFile::exists(ini)) { + QSettings s(ini, QSettings::IniFormat); + ui->prefixPath->setText(s.value("Settings/proton_prefix_path").toString()); + ui->protonVersion->setText(s.value("fluorine/proton_name").toString()); + } else { + ui->prefixPath->clear(); + ui->protonVersion->clear(); + } + } + setButtonsEnabled(true); const auto& m = InstanceManager::singleton(); @@ -764,6 +820,8 @@ void InstanceManagerDialog::clearData() ui->baseDirectory->clear(); ui->gameName->clear(); ui->gameDir->clear(); + ui->prefixPath->clear(); + ui->protonVersion->clear(); setButtonsEnabled(false); @@ -779,6 +837,7 @@ void InstanceManagerDialog::setButtonsEnabled(bool b) ui->exploreGame->setEnabled(b); ui->convertToPortable->setEnabled(b); ui->convertToGlobal->setEnabled(b); + ui->removeFromList->setEnabled(b); ui->deleteInstance->setEnabled(b); ui->switchToInstance->setEnabled(b); } diff --git a/src/src/instancemanagerdialog.h b/src/src/instancemanagerdialog.h index 4df9cdc..c8862fd 100644 --- a/src/src/instancemanagerdialog.h +++ b/src/src/instancemanagerdialog.h @@ -68,6 +68,10 @@ public: // void openINI(); + // removes the selected instance from the list without deleting files + // + void removeFromList(); + // deletes the selected instance // void deleteInstance(); diff --git a/src/src/instancemanagerdialog.ui b/src/src/instancemanagerdialog.ui index eab814c..2b94a8d 100644 --- a/src/src/instancemanagerdialog.ui +++ b/src/src/instancemanagerdialog.ui @@ -286,6 +286,34 @@ + + + + Prefix + + + + + + + true + + + + + + + Proton + + + + + + + true + + + @@ -338,6 +366,16 @@ + + + + Remove from list + + + Remove this instance from the list without deleting any files. + + + diff --git a/src/src/loglist.cpp b/src/src/loglist.cpp index 15299f5..8ed28f1 100644 --- a/src/src/loglist.cpp +++ b/src/src/loglist.cpp @@ -406,7 +406,7 @@ bool createAndMakeWritable(const std::wstring& subPath) bool setLogDirectory(const QString& dir) { #ifndef _WIN32 - // On Linux, all logs go to ~/.local/share/fluorine/logs/ + // On Linux, all logs go to ~/.var/app/com.fluorine.manager/logs/ const QString logDir = fluorineDataDir() + "/logs"; QDir().mkpath(logDir); const auto logFile = logDir + "/" + diff --git a/src/src/mainwindow.cpp b/src/src/mainwindow.cpp index c0be6fe..425d20c 100644 --- a/src/src/mainwindow.cpp +++ b/src/src/mainwindow.cpp @@ -83,6 +83,8 @@ along with Mod Organizer. If not, see . #include #ifdef _WIN32 #include +#else +#include "fluorinepaths.h" #endif #include "directoryrefresher.h" @@ -2649,8 +2651,21 @@ void MainWindow::openPluginsFolder() void MainWindow::openStylesheetsFolder() { +#ifndef _WIN32 + // On Linux, open the instance's stylesheets directory (where custom themes + // from modlists live), or the user data dir as fallback. + QString ssPath; + if (auto ci = InstanceManager::singleton().currentInstance()) { + ssPath = ci->directory() + "/" + + QString::fromStdWString(AppConfig::stylesheetsPath()); + } else { + ssPath = fluorineDataDir() + "/stylesheets"; + } + QDir().mkpath(ssPath); +#else QString ssPath = AppConfig::basePath() + "/" + ToQString(AppConfig::stylesheetsPath()); +#endif shell::Explore(ssPath); } diff --git a/src/src/moapplication.cpp b/src/src/moapplication.cpp index b77570e..29e21c4 100644 --- a/src/src/moapplication.cpp +++ b/src/src/moapplication.cpp @@ -31,6 +31,7 @@ along with Mod Organizer. If not, see . #include "settings.h" #ifndef _WIN32 #include "fluorineconfig.h" +#include "fluorinepaths.h" #include "fuseconnector.h" #include "wineprefix.h" #include @@ -675,13 +676,43 @@ bool MOApplication::setStyleFile(const QString& styleName) } // set new stylesheet or clear it if (styleName.length() != 0) { - QString styleSheetName = applicationDirPath() + "/" + - MOBase::ToQString(AppConfig::stylesheetsPath()) + "/" + - styleName; - if (QFile::exists(styleSheetName)) { - m_styleWatcher.addPath(styleSheetName); - updateStyle(styleSheetName); + // Search for the stylesheet in multiple locations: + // 1. applicationDirPath()/stylesheets/ — bundled themes + // 2. instance baseDir/stylesheets/ — instance/portable themes (modlists) + // 3. fluorineDataDir()/stylesheets/ — user-installed custom themes + const QString ssSubdir = MOBase::ToQString(AppConfig::stylesheetsPath()); + QStringList searchDirs; + searchDirs << applicationDirPath() + "/" + ssSubdir; +#ifndef _WIN32 + if (m_instance) { + // Prefer baseDirectory() (populated after readFromIni), fall back to + // directory() which is always set by the constructor. + QString base = m_instance->baseDirectory(); + if (base.isEmpty()) + base = m_instance->directory(); + const QString instanceDir = base + "/" + ssSubdir; + if (!searchDirs.contains(instanceDir)) + searchDirs << instanceDir; + } + const QString userDir = fluorineDataDir() + "/stylesheets"; + if (!searchDirs.contains(userDir)) + searchDirs << userDir; +#endif + + QString resolved; + for (const auto& dir : searchDirs) { + QString candidate = dir + "/" + styleName; + if (QFile::exists(candidate)) { + resolved = candidate; + break; + } + } + + if (!resolved.isEmpty()) { + m_styleWatcher.addPath(resolved); + updateStyle(resolved); } else { + // Could be a built-in Qt style name (e.g. "Fusion") updateStyle(styleName); } } else { diff --git a/src/src/organizercore.cpp b/src/src/organizercore.cpp index e0e32cb..dd1b33b 100644 --- a/src/src/organizercore.cpp +++ b/src/src/organizercore.cpp @@ -126,14 +126,40 @@ QString resolveWinePrefixPath(const Settings& settings, QString resolveWineDataDirName(const IPluginGame* managedGame) { - if (managedGame != nullptr) { - const QString docsLeaf = managedGame->documentsDirectory().dirName().trimmed(); - if (!docsLeaf.isEmpty()) { + if (managedGame == nullptr) { + return {}; + } + + // Primary: the My Games subfolder name matches the AppData/Local folder + // for almost every Bethesda game. + const QDir docsDir = managedGame->documentsDirectory(); + if (docsDir.exists()) { + const QString docsLeaf = docsDir.dirName().trimmed(); + if (!docsLeaf.isEmpty() && docsLeaf != QStringLiteral(".")) { return docsLeaf; } - return managedGame->gameName(); } - return {}; + + // Fallback: gameShortName is used by the base Gamebryo mappings() for + // the AppData/Local folder and matches for most games. + const QString shortName = managedGame->gameShortName(); + if (!shortName.isEmpty()) { + log::warn("resolveWineDataDirName: documentsDirectory() is empty or " + "invalid, falling back to gameShortName '{}'", + shortName); + return shortName; + } + + log::warn("resolveWineDataDirName: both documentsDirectory() and " + "gameShortName() are empty, falling back to gameName '{}'", + managedGame->gameName()); + return managedGame->gameName(); +} + +QString resolvePrefixGameDocumentsDir(const WinePrefix& prefix, + const QString& dataDirName) +{ + return QDir(prefix.myGamesPath()).filePath(dataDirName); } QString resolveSaveRelativePath(std::shared_ptr profile, @@ -2166,20 +2192,29 @@ bool OrganizerCore::beforeRun( pluginsFile.close(); if (!plugins.isEmpty()) { - prefix.deployPlugins(plugins, dataDirName); - log::debug("Deployed {} plugins to prefix '{}'", plugins.size(), - prefixPathStr); + if (prefix.deployPlugins(plugins, dataDirName)) { + log::debug("Deployed {} plugins to prefix '{}' (dataDirName='{}')", + plugins.size(), prefixPathStr, dataDirName); + } else { + log::error("Failed to deploy {} plugins to prefix '{}' " + "(dataDirName='{}')", + plugins.size(), prefixPathStr, dataDirName); + } + } else { + log::warn("Profile plugins.txt is empty or contains only comments, " + "skipping plugin deployment"); } } if (m_CurrentProfile->localSettingsEnabled()) { + const QString targetIniBase = + resolvePrefixGameDocumentsDir(prefix, dataDirName); int deployedIniCount = 0; for (const QString& iniFile : managedGame()->iniFiles()) { const QString sourceIni = m_CurrentProfile->absoluteIniFilePath(iniFile); - const QString targetIni = MOBase::resolveFileCaseInsensitive( - QFileInfo(managedGame()->documentsDirectory(), iniFile) - .absoluteFilePath()); + const QString targetIni = + QDir(targetIniBase).filePath(QFileInfo(iniFile).fileName()); log::debug("INI deploy check: source='{}' exists={}, target='{}'", sourceIni, QFileInfo::exists(sourceIni), targetIni); if (QFileInfo::exists(sourceIni) && @@ -2265,13 +2300,14 @@ void OrganizerCore::afterRun(const QFileInfo& binary, DWORD exitCode) } if (m_CurrentProfile->localSettingsEnabled()) { + const QString targetIniBase = + resolvePrefixGameDocumentsDir(prefix, dataDirName); QList> iniMappings; for (const QString& iniFile : managedGame()->iniFiles()) { const QString profileIni = m_CurrentProfile->absoluteIniFilePath(iniFile); - const QString targetIni = MOBase::resolveFileCaseInsensitive( - QFileInfo(managedGame()->documentsDirectory(), iniFile) - .absoluteFilePath()); + const QString targetIni = + QDir(targetIniBase).filePath(QFileInfo(iniFile).fileName()); iniMappings.append({profileIni, targetIni}); log::debug("Sync profile INI '{}' <- '{}'", profileIni, targetIni); } diff --git a/src/src/process_helper_main.cpp b/src/src/process_helper_main.cpp new file mode 100644 index 0000000..185111c --- /dev/null +++ b/src/src/process_helper_main.cpp @@ -0,0 +1,359 @@ +// Standalone process helper for Flatpak game launching. +// Runs on the host via flatpak-spawn --host, keeping the flatpak-spawn +// proxy alive for MO2's PID polling while the game process tree is running. +// +// Protocol (stdin/stdout, line-oriented): +// Config phase: MO2 writes key=value lines terminated by a blank line +// program=, arg= (repeatable), env=KEY=VALUE (repeatable), +// workdir= +// Helper responds: "started " or "error " +// Runtime commands (MO2→helper): "kill" (SIGTERM child tree), "quit" +// Helper reports: "exited " when game process tree exits + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +// ── Helpers ── + +static void writeResponse(const std::string& msg) +{ + std::string line = msg + "\n"; + ::write(STDOUT_FILENO, line.data(), line.size()); +} + +static bool readLine(std::string& out, int timeoutMs) +{ + out.clear(); + + struct pollfd pfd{}; + pfd.fd = STDIN_FILENO; + pfd.events = POLLIN; + + while (true) { + int ret = ::poll(&pfd, 1, timeoutMs); + if (ret < 0) { + if (errno == EINTR) + continue; + return false; + } + if (ret == 0) { + // timeout + return false; + } + + // Try reading if data is available, even if HUP is also set + // (pipe can have buffered data when the writer closes). + if (!(pfd.revents & POLLIN)) { + // No data available — must be HUP or ERR only + return false; + } + + char ch = 0; + ssize_t n = ::read(STDIN_FILENO, &ch, 1); + if (n <= 0) { + return false; + } + if (ch == '\n') { + return true; + } + out.push_back(ch); + } +} + +// Collect all descendant PIDs of a given root by scanning /proc. +static std::unordered_set collectDescendants(pid_t root) +{ + std::unordered_set descendants; + + // Build parent→children map + struct ProcEntry { + pid_t pid; + pid_t ppid; + }; + std::vector entries; + + DIR* proc = opendir("/proc"); + if (!proc) { + return descendants; + } + + struct dirent* entry = nullptr; + while ((entry = readdir(proc)) != nullptr) { + if (entry->d_type != DT_DIR && entry->d_type != DT_UNKNOWN) { + continue; + } + + char* end = nullptr; + long pidLong = strtol(entry->d_name, &end, 10); + if (end == entry->d_name || *end != '\0' || pidLong <= 0) { + continue; + } + + pid_t pid = static_cast(pidLong); + + char statusPath[64]; + snprintf(statusPath, sizeof(statusPath), "/proc/%ld/status", pidLong); + + std::ifstream status(statusPath); + if (!status.is_open()) { + continue; + } + + std::string line; + pid_t ppid = 0; + while (std::getline(status, line)) { + if (line.rfind("PPid:", 0) == 0) { + ppid = static_cast(strtol(line.c_str() + 5, nullptr, 10)); + break; + } + } + + if (ppid > 0) { + entries.push_back({pid, ppid}); + } + } + closedir(proc); + + // BFS from root + std::vector queue; + queue.push_back(root); + + while (!queue.empty()) { + pid_t cur = queue.back(); + queue.pop_back(); + + for (const auto& e : entries) { + if (e.ppid == cur && descendants.find(e.pid) == descendants.end()) { + descendants.insert(e.pid); + queue.push_back(e.pid); + } + } + } + + return descendants; +} + +// Check if any process in the given set is still alive. +static bool anyAlive(const std::unordered_set& pids) +{ + for (pid_t p : pids) { + if (::kill(p, 0) == 0 || errno == EPERM) { + return true; + } + } + return false; +} + +// ── Main ── + +int main() +{ + // Make stdout line-buffered for reliable protocol messages. + setvbuf(stdout, nullptr, _IOLBF, 0); + + // ── Config phase: read key=value lines until blank line ── + std::string program; + std::vector args; + std::vector envVars; // "KEY=VALUE" + std::string workdir; + + while (true) { + std::string line; + if (!readLine(line, 30000)) { + writeResponse("error stdin closed or timeout during config"); + return 1; + } + + if (line.empty()) { + break; // blank line = end of config + } + + auto eq = line.find('='); + if (eq == std::string::npos) { + continue; + } + + std::string key = line.substr(0, eq); + std::string val = line.substr(eq + 1); + + if (key == "program") { + program = val; + } else if (key == "arg") { + args.push_back(val); + } else if (key == "env") { + envVars.push_back(val); + } else if (key == "workdir") { + workdir = val; + } + } + + if (program.empty()) { + writeResponse("error no program specified"); + return 1; + } + + // ── Pipe for exec error reporting ── + // Child writes errno to this pipe if execvp fails; parent reads it. + int errPipe[2]; + if (::pipe2(errPipe, O_CLOEXEC) != 0) { + writeResponse("error pipe2 failed: " + std::string(strerror(errno))); + return 1; + } + + // ── Fork ── + pid_t child = ::fork(); + if (child < 0) { + writeResponse("error fork failed: " + std::string(strerror(errno))); + return 1; + } + + if (child == 0) { + // ── Child ── + ::close(errPipe[0]); // close read end + + // New session so we can kill the whole process group later. + ::setsid(); + + // Set environment variables. + for (const auto& ev : envVars) { + ::putenv(const_cast(ev.c_str())); + } + + // Change working directory. + if (!workdir.empty()) { + if (::chdir(workdir.c_str()) != 0) { + int err = errno; + (void)::write(errPipe[1], &err, sizeof(err)); + ::_exit(127); + } + } + + // Build argv for execvp. + std::vector argv; + argv.push_back(program.c_str()); + for (const auto& a : args) { + argv.push_back(a.c_str()); + } + argv.push_back(nullptr); + + ::execvp(program.c_str(), const_cast(argv.data())); + + // If we get here, exec failed. + int err = errno; + (void)::write(errPipe[1], &err, sizeof(err)); + ::_exit(127); + } + + // ── Parent ── + ::close(errPipe[1]); // close write end + + // Check if exec succeeded (pipe closes on successful exec due to O_CLOEXEC). + int execErr = 0; + ssize_t n = ::read(errPipe[0], &execErr, sizeof(execErr)); + ::close(errPipe[0]); + + if (n > 0) { + // exec failed in child + ::waitpid(child, nullptr, 0); + writeResponse("error exec failed: " + std::string(strerror(execErr))); + return 1; + } + + // Success - report PID. + writeResponse("started " + std::to_string(child)); + + // ── Monitor loop ── + // Wait for the direct child and then monitor descendants (handles Proton + // chain: proton → wine → game.exe). + bool childExited = false; + int childStatus = 0; + bool quit = false; + + while (!quit) { + // Check for commands on stdin. + std::string cmd; + // Non-blocking: if readLine returns false due to timeout, that's fine. + // If it returns false due to pipe close, MO2 crashed — kill child group. + struct pollfd pfd{}; + pfd.fd = STDIN_FILENO; + pfd.events = POLLIN; + + int pollRet = ::poll(&pfd, 1, 200); + + if (pollRet > 0) { + if (pfd.revents & (POLLHUP | POLLERR)) { + // MO2 crashed or closed pipe — kill child group and exit. + ::kill(-child, SIGTERM); + break; + } + + if (pfd.revents & POLLIN) { + if (readLine(cmd, 0)) { + if (cmd == "kill") { + ::kill(-child, SIGTERM); + } else if (cmd == "quit") { + quit = true; + break; + } + } else { + // read failed = pipe closed + ::kill(-child, SIGTERM); + break; + } + } + } + + // Check child status. + if (!childExited) { + int status = 0; + pid_t ret = ::waitpid(child, &status, WNOHANG); + if (ret == child) { + childExited = true; + childStatus = status; + } else if (ret < 0 && errno != EINTR) { + // Child somehow lost + childExited = true; + childStatus = 0; + } + } + + if (childExited) { + // Check for surviving descendants (e.g., game.exe still running + // after the proton wrapper exits). + auto desc = collectDescendants(child); + if (desc.empty() || !anyAlive(desc)) { + // All done. + int exitCode = 0; + if (WIFEXITED(childStatus)) { + exitCode = WEXITSTATUS(childStatus); + } else if (WIFSIGNALED(childStatus)) { + exitCode = 128 + WTERMSIG(childStatus); + } + writeResponse("exited " + std::to_string(exitCode)); + return 0; + } + // Descendants still alive, keep monitoring. + } + } + + // If we broke out of the loop, reap child if needed. + if (!childExited) { + ::waitpid(child, nullptr, 0); + } + + writeResponse("exited 0"); + return 0; +} diff --git a/src/src/processrunner.cpp b/src/src/processrunner.cpp index a6a37ca..ac3c9eb 100644 --- a/src/src/processrunner.cpp +++ b/src/src/processrunner.cpp @@ -1175,6 +1175,7 @@ std::optional ProcessRunner::runBinary() #ifdef _WIN32 m_handle.reset(startBinary(parent, m_sp)); #else + m_sp.helperProcessOut = &m_processHelper; m_handle.reset(reinterpret_cast(static_cast(startBinary(parent, m_sp)))); #endif if (m_handle.get() == INVALID_HANDLE_VALUE) { @@ -1264,7 +1265,12 @@ ProcessRunner::Results ProcessRunner::postRun() const QFileInfo binary = m_sp.binary; QPointer core = &m_core; - std::thread([core, binary, pid]() { + QProcess* helper = m_processHelper; + m_processHelper = nullptr; + + std::thread([core, binary, pid, helper]() { + // For detached processes (including flatpak-spawn helper), + // waitpid will fail with ECHILD. Use kill(0) polling instead. int status = 0; pid_t waited = -1; do { @@ -1278,11 +1284,26 @@ ProcessRunner::Results ProcessRunner::postRun() } else if (WIFSIGNALED(status)) { exitCode = static_cast(128 + WTERMSIG(status)); } + } else if (errno == ECHILD) { + // Detached process — poll with kill(0) until gone. + while (::kill(pid, 0) == 0 || errno == EPERM) { + usleep(200000); // 200ms + } } else { MOBase::log::warn("process runner: waitpid failed for pid {}: {}", pid, errno); } + // Clean up helper QProcess on the main thread. + if (helper) { + QMetaObject::invokeMethod( + QCoreApplication::instance(), [helper]() { + helper->waitForFinished(1000); + delete helper; + }, + Qt::QueuedConnection); + } + if (!core) { return; } @@ -1331,6 +1352,15 @@ ProcessRunner::Results ProcessRunner::postRun() }); } +#ifndef _WIN32 + // Clean up the process helper (keeps flatpak-spawn alive during game). + if (m_processHelper) { + m_processHelper->waitForFinished(1000); + delete m_processHelper; + m_processHelper = nullptr; + } +#endif + if (shouldRefresh(r)) { QEventLoop loop; const bool wait = m_waitFlags.testFlag(WaitForRefresh); diff --git a/src/src/processrunner.h b/src/src/processrunner.h index 2736c86..e825e98 100644 --- a/src/src/processrunner.h +++ b/src/src/processrunner.h @@ -185,6 +185,9 @@ private: QFileInfo m_shellOpen; env::HandlePtr m_handle; DWORD m_exitCode; +#ifndef _WIN32 + QProcess* m_processHelper = nullptr; +#endif bool shouldRunShell() const; bool shouldRefresh(Results r) const; diff --git a/src/src/profile.cpp b/src/src/profile.cpp index 0060fa1..5489fc9 100644 --- a/src/src/profile.cpp +++ b/src/src/profile.cpp @@ -825,20 +825,33 @@ void Profile::mergeTweak(const QString& tweakName, const QString& tweakedIni) co } } #else - // On Linux, use QSettings to read/merge INI tweaks - QSettings source(tweakName, QSettings::IniFormat); - QSettings dest(tweakedIni, QSettings::IniFormat); - - for (const QString& group : source.childGroups()) { - source.beginGroup(group); - dest.beginGroup(group); + // On Linux, parse the tweak INI file line-by-line and merge each + // key=value into the destination using WriteRegistryValue (which uses + // the safe line-by-line writer that does NOT interpret backslashes as + // line continuations or URL-encode spaces in keys). + QFile sourceFile(tweakName); + if (!sourceFile.open(QIODevice::ReadOnly | QIODevice::Text)) { + log::warn("mergeTweak: could not open tweak file '{}'", tweakName); + return; + } - for (const QString& key : source.childKeys()) { - dest.setValue(key, source.value(key)); + QString currentSection; + QTextStream stream(&sourceFile); + while (!stream.atEnd()) { + QString line = stream.readLine().trimmed(); + if (line.isEmpty() || line.startsWith(';') || line.startsWith('#')) { + continue; + } + if (line.startsWith('[') && line.endsWith(']')) { + currentSection = line.mid(1, line.length() - 2).trimmed(); + continue; + } + const int eqPos = line.indexOf('='); + if (eqPos > 0 && !currentSection.isEmpty()) { + const QString key = line.left(eqPos).trimmed(); + const QString value = line.mid(eqPos + 1).trimmed(); + MOBase::WriteRegistryValue(currentSection, key, value, tweakedIni); } - - source.endGroup(); - dest.endGroup(); } #endif } diff --git a/src/src/protonlauncher.cpp b/src/src/protonlauncher.cpp index 6497fff..a2aa042 100644 --- a/src/src/protonlauncher.cpp +++ b/src/src/protonlauncher.cpp @@ -267,6 +267,12 @@ ProtonLauncher& ProtonLauncher::addEnvVar(const QString& key, const QString& val return *this; } +ProtonLauncher& ProtonLauncher::setHelperProcessOut(QProcess** out) +{ + m_helperProcessOut = out; + return *this; +} + std::pair ProtonLauncher::launch() const { qint64 pid = -1; @@ -349,9 +355,13 @@ bool ProtonLauncher::launchWithProton(qint64& pid) const } } - maybeWrapForFlatpak(program, arguments, env); - MOBase::log::info("Proton launch: '{}' run '{}'", protonScript, m_binary); + + if (isFlatpak()) { + return launchViaProcessHelper(program, arguments, env, m_workingDir, pid); + } + + maybeWrapForFlatpak(program, arguments, env); return startDetachedWithEnv(program, arguments, m_workingDir, env, pid); } @@ -485,8 +495,6 @@ bool ProtonLauncher::launchWithUmu(qint64& pid) const } } - maybeWrapForFlatpak(program, arguments, env); - MOBase::log::info("UMU launch: '{}' '{}' (game id: {}, steam: '{}')", umuRun, m_binary, (effectiveSteamAppId == 0 @@ -494,6 +502,12 @@ bool ProtonLauncher::launchWithUmu(qint64& pid) const : QStringLiteral("umu-") + QString::number(effectiveSteamAppId)), steamPath); + + if (isFlatpak()) { + return launchViaProcessHelper(program, arguments, env, m_workingDir, pid); + } + + maybeWrapForFlatpak(program, arguments, env); return startDetachedWithEnv(program, arguments, m_workingDir, env, pid); } @@ -517,11 +531,105 @@ bool ProtonLauncher::launchDirect(qint64& pid) const env.insert(it.key(), it.value()); } - maybeWrapForFlatpak(program, arguments, env); + if (isFlatpak()) { + return launchViaProcessHelper(program, arguments, env, m_workingDir, pid); + } + maybeWrapForFlatpak(program, arguments, env); return startDetachedWithEnv(program, arguments, m_workingDir, env, pid); } +bool ProtonLauncher::launchViaProcessHelper( + const QString& program, const QStringList& arguments, + const QProcessEnvironment& env, const QString& workingDir, + qint64& pid) const +{ + const QString helperBin = fluorineDataDir() + QStringLiteral("/bin/mo2-process-helper"); + if (!QFileInfo::exists(helperBin)) { + MOBase::log::warn("mo2-process-helper not found at '{}', falling back to " + "flatpak-spawn direct launch", helperBin); + // Fall back to old direct flatpak-spawn path. + QString prog = program; + QStringList args = arguments; + QProcessEnvironment envCopy = env; + maybeWrapForFlatpak(prog, args, envCopy); + return startDetachedWithEnv(prog, args, workingDir, envCopy, pid); + } + + auto* proc = new QProcess(); + proc->setProcessChannelMode(QProcess::SeparateChannels); + proc->start(QStringLiteral("flatpak-spawn"), + {QStringLiteral("--host"), helperBin}); + + if (!proc->waitForStarted(5000)) { + MOBase::log::error("Failed to start flatpak-spawn for process helper"); + delete proc; + return false; + } + + // Write config block to helper's stdin. + auto writeLine = [&](const QString& line) { + proc->write(line.toUtf8()); + proc->write("\n"); + }; + + writeLine(QStringLiteral("program=") + program); + for (const QString& arg : arguments) { + writeLine(QStringLiteral("arg=") + arg); + } + + // Send env vars that differ from system environment. + const QProcessEnvironment sysEnv = QProcessEnvironment::systemEnvironment(); + for (const QString& key : env.keys()) { + const QString val = env.value(key); + if (val != sysEnv.value(key)) { + writeLine(QStringLiteral("env=") + key + QStringLiteral("=") + val); + } + } + + if (!workingDir.isEmpty()) { + writeLine(QStringLiteral("workdir=") + workingDir); + } + + // Blank line terminates config. + proc->write("\n"); + proc->waitForBytesWritten(2000); + + // Read response: "started " or "error " + if (!proc->waitForReadyRead(10000)) { + MOBase::log::error("Process helper did not respond in time"); + proc->kill(); + proc->waitForFinished(2000); + delete proc; + return false; + } + + const QString response = QString::fromUtf8(proc->readLine()).trimmed(); + if (response.startsWith(QStringLiteral("started "))) { + MOBase::log::info("Process helper: {}", response); + } else { + MOBase::log::error("Process helper error: {}", response); + proc->kill(); + proc->waitForFinished(2000); + delete proc; + return false; + } + + // Use the flatpak-spawn PID for kill(pid,0) polling. + pid = proc->processId(); + + // Store the QProcess so it stays alive (keeping flatpak-spawn alive). + if (m_helperProcessOut) { + *m_helperProcessOut = proc; + } else { + // No owner provided — leak intentionally to keep the pipe alive. + // The process will clean up when the game exits. + MOBase::log::debug("No helper process owner set, helper will self-manage"); + } + + return true; +} + bool ProtonLauncher::ensureSteamRunning() { QProcess pgrep; diff --git a/src/src/protonlauncher.h b/src/src/protonlauncher.h index 95d22d1..d0213f6 100644 --- a/src/src/protonlauncher.h +++ b/src/src/protonlauncher.h @@ -2,11 +2,14 @@ #define PROTONLAUNCHER_H #include +#include #include #include #include #include +class QProcess; + class ProtonLauncher { public: @@ -23,6 +26,7 @@ public: ProtonLauncher& setPreferSystemUmu(bool preferSystemUmu); ProtonLauncher& setUseSteamRun(bool useSteamRun); ProtonLauncher& addEnvVar(const QString& key, const QString& value); + ProtonLauncher& setHelperProcessOut(QProcess** out); // Launch dispatch: UMU -> Proton -> Direct std::pair launch() const; @@ -31,6 +35,9 @@ private: bool launchWithProton(qint64& pid) const; bool launchWithUmu(qint64& pid) const; bool launchDirect(qint64& pid) const; + bool launchViaProcessHelper(const QString& program, const QStringList& arguments, + const QProcessEnvironment& env, + const QString& workingDir, qint64& pid) const; static bool ensureSteamRunning(); QString m_binary; @@ -45,6 +52,7 @@ private: bool m_useSteamRun; QMap m_envVars; QMap m_wrapperEnvVars; + QProcess** m_helperProcessOut = nullptr; }; #endif // PROTONLAUNCHER_H diff --git a/src/src/settingsdialog.ui b/src/src/settingsdialog.ui index 7755f6f..9e5c44d 100644 --- a/src/src/settingsdialog.ui +++ b/src/src/settingsdialog.ui @@ -1824,6 +1824,38 @@ If you disable this feature, MO will only display official DLCs this way. Please + + + + Show Install Log + + + true + + + false + + + + + + + true + + + false + + + 200 + + + + monospace + 9 + + + + diff --git a/src/src/settingsdialogproton.cpp b/src/src/settingsdialogproton.cpp index f12b9c0..e9d1bec 100644 --- a/src/src/settingsdialogproton.cpp +++ b/src/src/settingsdialogproton.cpp @@ -52,23 +52,36 @@ ProtonSettingsTab::ProtonSettingsTab(Settings& s, SettingsDialog& d) populateProtons(); QObject::connect(ui->protonVersionCombo, &QComboBox::currentIndexChanged, this, - [this](int) { - if (auto cfg = FluorineConfig::load(); - cfg.has_value() && cfg->prefixExists()) { - const QString protonName = - ui->protonVersionCombo->currentText().trimmed(); - const QString protonPath = ui->protonVersionCombo - ->currentData(Qt::UserRole + 1) - .toString() - .trimmed(); - - if (!protonName.isEmpty() && !protonPath.isEmpty() && - (cfg->proton_name != protonName || - cfg->proton_path != protonPath)) { - cfg->proton_name = protonName; - cfg->proton_path = protonPath; - cfg->save(); - } + [this](int index) { + if (index < 0) { + return; + } + + auto cfg = FluorineConfig::load(); + if (!cfg.has_value()) { + return; + } + + const QString protonName = + ui->protonVersionCombo->currentText().trimmed(); + const QString protonPath = ui->protonVersionCombo + ->itemData(index, Qt::UserRole + 1) + .toString() + .trimmed(); + + if (protonName.isEmpty() || protonPath.isEmpty()) { + MOBase::log::warn("Proton combo change: name='{}' path='{}' — " + "skipping save (empty)", protonName, protonPath); + return; + } + + if (cfg->proton_name != protonName || + cfg->proton_path != protonPath) { + cfg->proton_name = protonName; + cfg->proton_path = protonPath; + cfg->save(); + MOBase::log::info("Updated Proton config: {} ({})", + protonName, protonPath); } }); @@ -90,6 +103,15 @@ ProtonSettingsTab::ProtonSettingsTab(Settings& s, SettingsDialog& d) QObject::connect(&m_installWatcher, &QFutureWatcher::finished, this, &ProtonSettingsTab::onInstallFinished); + // install log viewer + ui->nakInstallLog->setVisible(false); + QObject::connect(ui->toggleInstallLog, &QPushButton::toggled, this, + [this](bool checked) { + ui->nakInstallLog->setVisible(checked); + ui->toggleInstallLog->setText( + checked ? tr("Hide Install Log") : tr("Show Install Log")); + }); + refreshState(); } @@ -130,6 +152,17 @@ void ProtonSettingsTab::populateProtons() const int idx = ui->protonVersionCombo->findText(cfg->proton_name); if (idx >= 0) { ui->protonVersionCombo->setCurrentIndex(idx); + } else if (ui->protonVersionCombo->count() > 0) { + // Saved Proton version no longer exists — select first available and + // update the config so the stale path doesn't cause umu-run fallback. + MOBase::log::warn("Saved Proton '{}' not found, defaulting to '{}'", + cfg->proton_name, + ui->protonVersionCombo->itemText(0)); + ui->protonVersionCombo->setCurrentIndex(0); + cfg->proton_name = ui->protonVersionCombo->itemText(0).trimmed(); + cfg->proton_path = ui->protonVersionCombo->itemData(0, Qt::UserRole + 1) + .toString().trimmed(); + cfg->save(); } } } @@ -206,6 +239,9 @@ void ProtonSettingsTab::onCreatePrefix() setBusy(true); ui->protonStatusLabel->setText(tr("Creating prefix...")); + ui->nakInstallLog->clear(); + ui->nakInstallLog->setVisible(true); + ui->toggleInstallLog->setChecked(true); startInstallTask(0, pfxPath, protonName, protonPath, ui->umuCheckBox->isChecked(), @@ -254,6 +290,9 @@ void ProtonSettingsTab::onRecreatePrefix() setBusy(true); ui->protonStatusLabel->setText(tr("Recreating prefix...")); + ui->nakInstallLog->clear(); + ui->nakInstallLog->setVisible(true); + ui->toggleInstallLog->setChecked(true); startInstallTask(cfg->app_id, cfg->prefix_path, cfg->proton_name, cfg->proton_path, ui->umuCheckBox->isChecked(), @@ -543,6 +582,9 @@ void ProtonSettingsTab::showGameRegistryDialog() setBusy(true); ui->protonStatusLabel->setText(tr("Fixing game registries...")); + ui->nakInstallLog->clear(); + ui->nakInstallLog->setVisible(true); + ui->toggleInstallLog->setChecked(true); g_activeInstallTab.store(this); @@ -692,10 +734,24 @@ void ProtonSettingsTab::enqueueProgress(float progress) Qt::QueuedConnection); } +void ProtonSettingsTab::appendInstallLog(const QString& message) +{ + ui->nakInstallLog->append(message); +} + void ProtonSettingsTab::statusCallback(const char* message) { if (auto* tab = g_activeInstallTab.load(); tab != nullptr) { - tab->enqueueStatus(QString::fromUtf8(message ? message : "")); + const QString msg = QString::fromUtf8(message ? message : ""); + tab->enqueueStatus(msg); + + if (!msg.isEmpty()) { + QMetaObject::invokeMethod(tab, + [tab, msg] { + tab->appendInstallLog(msg); + }, + Qt::QueuedConnection); + } } } @@ -704,6 +760,15 @@ void ProtonSettingsTab::logCallback(const char* message) if (message && *message) { MOBase::log::info("{}", message); } + + if (auto* tab = g_activeInstallTab.load(); tab != nullptr && message && *message) { + const QString msg = QString::fromUtf8(message); + QMetaObject::invokeMethod(tab, + [tab, msg] { + tab->appendInstallLog(msg); + }, + Qt::QueuedConnection); + } } void ProtonSettingsTab::progressCallback(float progress) diff --git a/src/src/settingsdialogproton.h b/src/src/settingsdialogproton.h index b3499d3..2b864c9 100644 --- a/src/src/settingsdialogproton.h +++ b/src/src/settingsdialogproton.h @@ -45,6 +45,7 @@ private: void enqueueStatus(const QString& message); void enqueueProgress(float progress); + void appendInstallLog(const QString& message); static void statusCallback(const char* message); static void logCallback(const char* message); diff --git a/src/src/settingsdialogtheme.cpp b/src/src/settingsdialogtheme.cpp index 363ae3b..8d41d35 100644 --- a/src/src/settingsdialogtheme.cpp +++ b/src/src/settingsdialogtheme.cpp @@ -1,11 +1,15 @@ #include "settingsdialogtheme.h" #include "categoriesdialog.h" #include "colortable.h" +#include "instancemanager.h" #include "modlist.h" #include "shared/appconfig.h" #include "ui_settingsdialog.h" #include #include +#ifndef _WIN32 +#include "fluorinepaths.h" +#endif using namespace MOBase; @@ -52,14 +56,35 @@ void ThemeSettingsTab::addStyles() ui->styleBox->insertSeparator(ui->styleBox->count()); - QDirIterator iter(QCoreApplication::applicationDirPath() + "/" + - QString::fromStdWString(AppConfig::stylesheetsPath()), - QStringList("*.qss"), QDir::Files); - - while (iter.hasNext()) { - iter.next(); - - ui->styleBox->addItem(iter.fileInfo().completeBaseName(), iter.fileName()); + // Collect .qss files from all stylesheet search directories, deduplicating + // by filename so bundled themes aren't listed twice. + const QString ssSubdir = QString::fromStdWString(AppConfig::stylesheetsPath()); + QStringList searchDirs; + searchDirs << QCoreApplication::applicationDirPath() + "/" + ssSubdir; +#ifndef _WIN32 + if (auto ci = InstanceManager::singleton().currentInstance()) { + // currentInstance() returns a bare Instance (readFromIni() not called), + // so baseDirectory() is empty. Use directory() which is always set. + const QString instanceDir = ci->directory() + "/" + ssSubdir; + if (!searchDirs.contains(instanceDir)) + searchDirs << instanceDir; + } + const QString userDir = fluorineDataDir() + "/stylesheets"; + if (!searchDirs.contains(userDir)) + searchDirs << userDir; +#endif + + QSet seen; + for (const auto& dir : searchDirs) { + QDirIterator iter(dir, QStringList("*.qss"), QDir::Files); + while (iter.hasNext()) { + iter.next(); + const QString fileName = iter.fileName(); + if (seen.contains(fileName)) + continue; + seen.insert(fileName); + ui->styleBox->addItem(iter.fileInfo().completeBaseName(), fileName); + } } } @@ -75,7 +100,21 @@ void ThemeSettingsTab::selectStyle() void ThemeSettingsTab::onExploreStyles() { + // On Linux, open the instance's stylesheets directory (where custom themes + // from modlists live). Falls back to the user data dir if no instance, or + // to the app dir on Windows. +#ifndef _WIN32 + QString ssPath; + if (auto ci = InstanceManager::singleton().currentInstance()) { + ssPath = ci->directory() + "/" + + QString::fromStdWString(AppConfig::stylesheetsPath()); + } else { + ssPath = fluorineDataDir() + "/stylesheets"; + } + QDir().mkpath(ssPath); +#else QString ssPath = QCoreApplication::applicationDirPath() + "/" + ToQString(AppConfig::stylesheetsPath()); +#endif shell::Explore(ssPath); } diff --git a/src/src/spawn.cpp b/src/src/spawn.cpp index f68389e..c2f15eb 100644 --- a/src/src/spawn.cpp +++ b/src/src/spawn.cpp @@ -643,7 +643,8 @@ int spawn(const SpawnParameters& sp, pid_t& processId) .setPreferSystemUmu( QSettings().value("fluorine/prefer_system_umu", false).toBool()) .setUseSteamRun( - QSettings().value("fluorine/use_steam_run", false).toBool()); + QSettings().value("fluorine/use_steam_run", false).toBool()) + .setHelperProcessOut(sp.helperProcessOut); const QString prefixPath = resolvePrefixPath(); if (prefixPath.isEmpty()) { diff --git a/src/src/spawn.h b/src/src/spawn.h index 48aaa05..72c81be 100644 --- a/src/src/spawn.h +++ b/src/src/spawn.h @@ -32,6 +32,7 @@ along with Mod Organizer. If not, see . #include #endif +class QProcess; class Settings; namespace spawn @@ -62,6 +63,7 @@ struct SpawnParameters #else int stdOut = -1; int stdErr = -1; + QProcess** helperProcessOut = nullptr; #endif }; diff --git a/src/src/vfs/vfs_helper_main.cpp b/src/src/vfs/vfs_helper_main.cpp index 9139d4c..d983574 100644 --- a/src/src/vfs/vfs_helper_main.cpp +++ b/src/src/vfs/vfs_helper_main.cpp @@ -31,6 +31,7 @@ struct HelperConfig std::string game_dir; std::string data_dir_name; std::string overwrite_dir; + std::string output_dir; std::vector> mods; std::vector> extra_files; }; @@ -62,6 +63,8 @@ static HelperConfig readConfig(const std::string& path) cfg.data_dir_name = val; } else if (key == "overwrite_dir") { cfg.overwrite_dir = val; + } else if (key == "output_dir") { + cfg.output_dir = val; } else if (key == "mod") { const auto pipe = val.find('|'); if (pipe != std::string::npos) { @@ -97,10 +100,13 @@ static void tryUnmountStale(const std::string& path) } static void flushStaging(const std::string& stagingDir, - const std::string& overwriteDir) + const std::string& overwriteDir, + const std::string& outputDir = {}) { const fs::path staging(stagingDir); - const fs::path overwrite(overwriteDir); + const fs::path overwrite = outputDir.empty() + ? fs::path(overwriteDir) + : fs::path(outputDir); if (!fs::exists(staging)) { return; } @@ -193,6 +199,9 @@ int main(int argc, char* argv[]) std::error_code ec; fs::create_directories(stagingDir, ec); fs::create_directories(config.overwrite_dir, ec); + if (!config.output_dir.empty()) { + fs::create_directories(config.output_dir, ec); + } // Scan base game files BEFORE mounting (after mount they're hidden) auto baseFileCache = scanDataDir(dataDirPath); @@ -289,7 +298,7 @@ int main(int argc, char* argv[]) config = newConfig; std::cout << "ok" << std::endl; } else if (line == "flush") { - flushStaging(stagingDir, config.overwrite_dir); + flushStaging(stagingDir, config.overwrite_dir, config.output_dir); fs::create_directories(stagingDir, ec); auto newTree = std::make_shared(buildDataDirVfs( @@ -320,7 +329,7 @@ int main(int argc, char* argv[]) fuse_session_destroy(session); g_session = nullptr; - flushStaging(stagingDir, config.overwrite_dir); + flushStaging(stagingDir, config.overwrite_dir, config.output_dir); close(backingFd); std::cout << "ok" << std::endl; diff --git a/src/src/wineprefix.cpp b/src/src/wineprefix.cpp index 64f5aa1..6d6f5e0 100644 --- a/src/src/wineprefix.cpp +++ b/src/src/wineprefix.cpp @@ -103,7 +103,9 @@ QStringList findCaseVariants(const QString& path) WinePrefix::WinePrefix(const QString& prefixPath) : m_prefixPath(QDir::cleanPath(prefixPath)) -{} +{ + MOBase::log::debug("WinePrefix: initialized with path '{}'", m_prefixPath); +} bool WinePrefix::isValid() const { @@ -133,16 +135,38 @@ QString WinePrefix::appdataLocal() const bool WinePrefix::deployPlugins(const QStringList& plugins, const QString& dataDir) const { if (!isValid()) { + MOBase::log::error("deployPlugins: prefix '{}' is not valid (drive_c not found)", + m_prefixPath); return false; } const QString pluginsDir = QDir(appdataLocal()).filePath(dataDir); + MOBase::log::debug("deployPlugins: target dir='{}', {} plugins to deploy", + pluginsDir, plugins.size()); + if (!QDir().mkpath(pluginsDir)) { + MOBase::log::error("deployPlugins: failed to create directory '{}'", pluginsDir); return false; } - QFile pluginsFile(QDir(pluginsDir).filePath("Plugins.txt")); + // Remove all case variants of plugins.txt and loadorder.txt before writing. + // Linux is case-sensitive, so a stale "plugins.txt" can coexist with + // "Plugins.txt" and the game may read the wrong one (e.g. FalloutNV reads + // lowercase "plugins.txt"). + const QString pluginsPath = QDir(pluginsDir).filePath("Plugins.txt"); + const QString loadOrderPath = QDir(pluginsDir).filePath("loadorder.txt"); + for (const QString& variant : findCaseVariants(pluginsPath)) { + MOBase::log::debug("deployPlugins: removing stale plugins variant '{}'", variant); + QFile::remove(variant); + } + for (const QString& variant : findCaseVariants(loadOrderPath)) { + MOBase::log::debug("deployPlugins: removing stale loadorder variant '{}'", variant); + QFile::remove(variant); + } + + QFile pluginsFile(pluginsPath); if (!pluginsFile.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) { + MOBase::log::error("deployPlugins: failed to open '{}' for writing", pluginsPath); return false; } @@ -151,9 +175,23 @@ bool WinePrefix::deployPlugins(const QStringList& plugins, const QString& dataDi pluginsStream << plugin << "\r\n"; } pluginsFile.close(); + MOBase::log::debug("deployPlugins: wrote {} plugins to '{}'", plugins.size(), + pluginsPath); + + // Also write lowercase "plugins.txt" for games that expect it (e.g. FalloutNV). + const QString pluginsLower = QDir(pluginsDir).filePath("plugins.txt"); + if (pluginsLower != pluginsPath) { + QFile::remove(pluginsLower); + if (!QFile::copy(pluginsPath, pluginsLower)) { + MOBase::log::warn("deployPlugins: failed to create lowercase copy '{}'", + pluginsLower); + } + } - QFile loadOrderFile(QDir(pluginsDir).filePath("loadorder.txt")); + QFile loadOrderFile(loadOrderPath); if (!loadOrderFile.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) { + MOBase::log::error("deployPlugins: failed to open '{}' for writing", + loadOrderPath); return false; } @@ -166,6 +204,7 @@ bool WinePrefix::deployPlugins(const QStringList& plugins, const QString& dataDi loadOrderStream << line << "\r\n"; } + MOBase::log::debug("deployPlugins: wrote loadorder.txt to '{}'", loadOrderPath); return true; } @@ -175,9 +214,12 @@ bool WinePrefix::deployProfileIni(const QString& sourceIniPath, { const QFileInfo iniInfo(sourceIniPath); if (!iniInfo.exists() || !iniInfo.isFile()) { + MOBase::log::warn("deployProfileIni: source '{}' does not exist or is not a file", + sourceIniPath); return false; } + MOBase::log::debug("deployProfileIni: '{}' -> '{}'", sourceIniPath, targetIniPath); const QString destination = QDir::cleanPath(targetIniPath); // Back up ALL case-insensitive variants (e.g. both skyrimprefs.ini and @@ -204,7 +246,22 @@ bool WinePrefix::deployProfileIni(const QString& sourceIniPath, } } - return copyFileWithParents(iniInfo.absoluteFilePath(), destination); + if (!copyFileWithParents(iniInfo.absoluteFilePath(), destination)) { + return false; + } + + // Create a lowercase alias so the game can find the INI regardless of + // which casing it uses (e.g. FalloutNV reads "fallout.ini" but we deploy + // "Fallout.ini"). + const QFileInfo destInfo(destination); + const QString lowerName = destInfo.fileName().toLower(); + if (lowerName != destInfo.fileName()) { + const QString lowerPath = QDir(destInfo.path()).filePath(lowerName); + QFile::remove(lowerPath); // remove stale copy/symlink if any + QFile::link(destInfo.fileName(), lowerPath); + } + + return true; } bool WinePrefix::deployProfileSaves(const QString& profileSaveDir, const QString& gameName, @@ -212,9 +269,13 @@ bool WinePrefix::deployProfileSaves(const QString& profileSaveDir, const QString bool clearDestination) const { if (!isValid()) { + MOBase::log::error("deployProfileSaves: prefix '{}' is not valid", m_prefixPath); return false; } + MOBase::log::debug("deployProfileSaves: profileSaveDir='{}', gameName='{}', " + "saveRelativePath='{}', clearDestination={}", + profileSaveDir, gameName, saveRelativePath, clearDestination); const QString gameRoot = QDir(myGamesPath()).filePath(gameName); const QString normalizedSavePath = QString(saveRelativePath).replace('\\', '/').trimmed(); @@ -258,9 +319,13 @@ bool WinePrefix::syncSavesBack(const QString& profileSaveDir, const QString& gam const QString& saveRelativePath) const { if (!isValid()) { + MOBase::log::error("syncSavesBack: prefix '{}' is not valid", m_prefixPath); return false; } + MOBase::log::debug("syncSavesBack: profileSaveDir='{}', gameName='{}', " + "saveRelativePath='{}'", + profileSaveDir, gameName, saveRelativePath); const QString gameRoot = QDir(myGamesPath()).filePath(gameName); const QString normalizedSavePath = QString(saveRelativePath).replace('\\', '/').trimmed(); @@ -350,10 +415,14 @@ void WinePrefix::restoreStaleBackups() const bool WinePrefix::syncProfileInisBack( const QList>& iniMappings) const { + MOBase::log::debug("syncProfileInisBack: {} INI mappings to sync back", + iniMappings.size()); bool allCopied = true; for (const auto& mapping : iniMappings) { const QString profileIniPath = QDir::cleanPath(mapping.first); const QString prefixIniPath = QDir::cleanPath(mapping.second); + MOBase::log::debug("syncProfileInisBack: profile='{}' <- prefix='{}'", + profileIniPath, prefixIniPath); // Find ALL case-insensitive variants of the INI file (e.g. both // skyrimprefs.ini and SkyrimPrefs.ini may exist on Linux). -- cgit v1.3.1