aboutsummaryrefslogtreecommitdiff
path: root/libs/uibase/src/steamutility.cpp
blob: a9dd80a417bc0e1176bacf78236fd4b78ad122ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
Mod Organizer shared UI functionality

Copyright (C) 2019 MO2 Contributors. All rights reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <uibase/steamutility.h>

#include <QDir>
#include <QList>
#include <QRegularExpression>
#include <QSettings>
#include <QString>
#include <QTextStream>

namespace MOBase
{

// Lines that contains libraries are in the format:
//    "1" "Path\to\library"
static const QRegularExpression
    kSteamLibraryFilter("^\\s*\"(?<idx>[0-9]+)\"\\s*\"(?<path>.*)\"");

QString findSteam()
{
#ifdef _WIN32
  QSettings steamRegistry("Valve", "Steam");
  return steamRegistry.value("SteamPath").toString();
#else
  // On Linux, check common Steam locations
  QString homePath = QDir::homePath();
  QStringList candidates = {
    homePath + "/.steam/steam",
    homePath + "/.local/share/Steam",
    homePath + "/.steam/debian-installation"
  };
  for (const auto& path : candidates) {
    if (QDir(path).exists()) {
      return path;
    }
  }
  return "";
#endif
}

QString findSteamGame(const QString& appName, const QString& validFile)
{
  QStringList libraryFolders;  // list of Steam libraries to search
  QDir steamDir(findSteam());  // Steam installation directory

  // Can do nothing if Steam doesn't exist
  if (!steamDir.exists())
    return "";

  // The Steam install is always a valid library
  libraryFolders << steamDir.absolutePath();

  // Search libraryfolders.vdf for additional libraries
  QFile libraryFoldersFile(steamDir.absoluteFilePath("steamapps/libraryfolders.vdf"));
  if (libraryFoldersFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
    QTextStream in(&libraryFoldersFile);
    while (!in.atEnd()) {
      QString line                  = in.readLine();
      QRegularExpressionMatch match = kSteamLibraryFilter.match(line);
      if (match.hasMatch()) {
        QString folder = match.captured("path");
#ifdef _WIN32
        folder.replace("/", "\\").replace("\\\\", "\\");
#else
        folder.replace("\\\\", "/").replace("\\", "/");
#endif
        libraryFolders << folder;
      }
    }
  }

  // Search the Steam libraries for the game directory
  for (auto library : libraryFolders) {
    QDir libraryDir(library);
    if (!libraryDir.cd("steamapps/common/" + appName))
      continue;
    QString normalizedValidFile = validFile;
    normalizedValidFile.replace("\\", "/");
    if (normalizedValidFile.startsWith('/')) {
      normalizedValidFile.remove(0, 1);
    }
    if (normalizedValidFile.isEmpty() || libraryDir.exists(normalizedValidFile))
      return libraryDir.absolutePath();
  }

  return "";
}

}  // namespace MOBase