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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
|
#include "gamegamebryo.h"
#include <uibase/filesystemutilities.h>
#include "bsainvalidation.h"
#include "dataarchives.h"
#include "gamebryomoddatacontent.h"
#include "gamebryosavegame.h"
#include "gameplugins.h"
#include "iprofile.h"
#include "log.h"
#include "registry.h"
#include "savegameinfo.h"
#include "scopeguard.h"
#include "scriptextender.h"
#include "utility.h"
#include "vdf_parser.h"
#include <QDir>
#include <QDirIterator>
#include <QFile>
#include <QFileInfo>
#include <QIcon>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
#include <QSettings>
#include <QStandardPaths>
#include <QTextStream>
#include <QtDebug>
#include <QtGlobal>
#ifdef _WIN32
#include <Knownfolders.h>
#include <Shlobj.h>
#include <Windows.h>
#include <winreg.h>
#include <winver.h>
#endif
#ifdef HAS_NAK_FFI
#include <nak_ffi.h>
#endif
#include <optional>
#include <string>
#include <vector>
// Local resolveFileCaseInsensitive moved to MOBase::resolveFileCaseInsensitive
using MOBase::resolveFileCaseInsensitive;
GameGamebryo::GameGamebryo() {}
void GameGamebryo::detectGame()
{
m_GamePath = identifyGamePath();
m_MyGamesPath = determineMyGamesPath(gameName());
}
bool GameGamebryo::init(MOBase::IOrganizer* moInfo)
{
m_Organizer = moInfo;
m_Organizer->onAboutToRun([this](const auto& binary) {
return prepareIni(binary);
});
return true;
}
bool GameGamebryo::isInstalled() const
{
return !m_GamePath.isEmpty();
}
QIcon GameGamebryo::gameIcon() const
{
return MOBase::iconForExecutable(gameDirectory().absoluteFilePath(binaryName()));
}
QDir GameGamebryo::gameDirectory() const
{
return QDir(m_GamePath);
}
QDir GameGamebryo::dataDirectory() const
{
return gameDirectory().absoluteFilePath("Data");
}
void GameGamebryo::setGamePath(const QString& path)
{
m_GamePath = path;
}
QDir GameGamebryo::documentsDirectory() const
{
return m_MyGamesPath;
}
QDir GameGamebryo::savesDirectory() const
{
return QDir(myGamesPath() + "/Saves");
}
std::vector<std::shared_ptr<const MOBase::ISaveGame>>
GameGamebryo::listSaves(QDir folder) const
{
QStringList filters;
filters << QString("*.") + savegameExtension();
std::vector<std::shared_ptr<const MOBase::ISaveGame>> saves;
for (auto info : folder.entryInfoList(filters, QDir::Files)) {
try {
saves.push_back(makeSaveGame(info.filePath()));
} catch (std::exception& e) {
MOBase::log::error("{}", e.what());
continue;
}
}
return saves;
}
void GameGamebryo::setGameVariant(const QString& variant)
{
m_GameVariant = variant;
}
QString GameGamebryo::binaryName() const
{
return gameShortName() + ".exe";
}
MOBase::IPluginGame::LoadOrderMechanism GameGamebryo::loadOrderMechanism() const
{
return LoadOrderMechanism::FileTime;
}
MOBase::IPluginGame::SortMechanism GameGamebryo::sortMechanism() const
{
return SortMechanism::LOOT;
}
bool GameGamebryo::looksValid(QDir const& path) const
{
// Check for <prog>.exe for now.
return path.exists(binaryName());
}
QString GameGamebryo::gameVersion() const
{
// We try the file version, but if it looks invalid (starts with the fallback
// version), we look the product version instead. If the product version is
// not empty, we use it.
QString binaryAbsPath = gameDirectory().absoluteFilePath(binaryName());
QString version = MOBase::getFileVersion(binaryAbsPath);
if (version.startsWith(FALLBACK_GAME_VERSION)) {
QString pversion = MOBase::getProductVersion(binaryAbsPath);
if (!pversion.isEmpty()) {
version = pversion;
}
}
return version;
}
QString GameGamebryo::getLauncherName() const
{
return gameShortName() + "Launcher.exe";
}
WORD GameGamebryo::getArch(QString const& program) const
{
#ifdef _WIN32
WORD arch = 0;
// This *really* needs to be factored out
std::wstring app_name =
L"\\\\?\\" +
QDir::toNativeSeparators(this->gameDirectory().absoluteFilePath(program))
.toStdWString();
WIN32_FIND_DATAW FindFileData;
HANDLE hFind = ::FindFirstFileW(app_name.c_str(), &FindFileData);
// exit if the binary was not found
if (hFind == INVALID_HANDLE_VALUE)
return arch;
HANDLE hFile = INVALID_HANDLE_VALUE;
HANDLE hMapping = INVALID_HANDLE_VALUE;
LPVOID addrHeader = nullptr;
PIMAGE_NT_HEADERS peHdr = nullptr;
hFile = CreateFileW(app_name.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
if (hFile == INVALID_HANDLE_VALUE)
goto cleanup;
hMapping = CreateFileMappingW(hFile, NULL, PAGE_READONLY | SEC_IMAGE, 0, 0,
program.toStdWString().c_str());
if (hMapping == INVALID_HANDLE_VALUE)
goto cleanup;
addrHeader = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
if (addrHeader == NULL)
goto cleanup; // couldn't memory map the file
peHdr = ImageNtHeader(addrHeader);
if (peHdr == NULL)
goto cleanup; // couldn't read the header
arch = peHdr->FileHeader.Machine;
cleanup: // release all of our handles
FindClose(hFind);
if (hFile != INVALID_HANDLE_VALUE)
CloseHandle(hFile);
if (hMapping != INVALID_HANDLE_VALUE)
CloseHandle(hMapping);
return arch;
#else
Q_UNUSED(program);
return 0;
#endif
}
QFileInfo GameGamebryo::findInGameFolder(const QString& relativePath) const
{
return QFileInfo(m_GamePath + "/" + relativePath);
}
QString GameGamebryo::identifyGamePath() const
{
#ifdef HAS_NAK_FFI
// Use NaK to detect game installations on Linux
NakGameList gameList = nak_detect_all_games();
ON_BLOCK_EXIT([&]() {
nak_game_list_free(gameList);
});
QString shortName = gameShortName();
QString fullName = gameName();
auto tokensMatch = [](const QString& detected, const QString& candidate) {
const QString detectedLower = detected.toLower();
const QStringList tokens =
candidate.toLower().split(QRegularExpression("[^a-z0-9]+"),
Qt::SkipEmptyParts);
bool anyToken = false;
for (const QString& token : tokens) {
if (token.size() < 3) {
continue;
}
anyToken = true;
if (!detectedLower.contains(token)) {
return false;
}
}
return anyToken;
};
for (size_t i = 0; i < gameList.count; ++i) {
const NakGame& game = gameList.games[i];
QString detectedName = QString::fromUtf8(game.name);
QString detectedPath = QString::fromUtf8(game.install_path);
if (detectedName.compare(fullName, Qt::CaseInsensitive) == 0 ||
detectedName.compare(shortName, Qt::CaseInsensitive) == 0 ||
detectedName.contains(fullName, Qt::CaseInsensitive) ||
detectedName.contains(shortName, Qt::CaseInsensitive) ||
tokensMatch(detectedName, fullName) || tokensMatch(detectedName, shortName)) {
if (looksValid(QDir(detectedPath))) {
return detectedPath;
}
}
}
return {};
#elif defined(_WIN32)
QString path = "Software\\Bethesda Softworks\\" + gameShortName();
return findInRegistry(HKEY_LOCAL_MACHINE, path.toStdWString().c_str(),
L"Installed Path");
#else
return {};
#endif
}
bool GameGamebryo::prepareIni(const QString&)
{
const auto profile = m_Organizer->profile();
QString basePath = profile->localSettingsEnabled()
? profile->absolutePath()
: documentsDirectory().absolutePath();
// Ensure all INI files exist with adequate content before writing settings.
// On Linux, the game launcher often doesn't work properly and can't create
// the INI files. If an INI is missing or is a stub (< 200 bytes, meaning
// only MO2-written keys exist), seed it from the game's default INI template.
ensureIniFilesExist(basePath);
if (!iniFiles().isEmpty()) {
// Resolve case-insensitively (e.g., fallout.ini vs Fallout.ini on Linux)
QString profileIni =
resolveFileCaseInsensitive(basePath + "/" + iniFiles()[0]);
QString setting = readIniValue(profileIni, "Launcher", "bEnableFileSelection", "0");
if (setting.toLong() != 1) {
MOBase::WriteRegistryValue("Launcher", "bEnableFileSelection", "1", profileIni);
}
}
return true;
}
#ifndef _WIN32
// Ensure both the canonical (proper-case) and lowercase versions of an INI file
// exist in a directory. One will be the real file, the other a symlink.
// This prevents case-sensitivity issues on Linux where different code paths or
// the game itself may reference either casing.
static void ensureCaseAliases(const QDir& dir, const QString& canonicalName,
const QString& actualFileName)
{
QString lowerName = canonicalName.toLower();
auto ensureAlias = [&](const QString& aliasName) {
if (aliasName != actualFileName) {
QString aliasPath = dir.absoluteFilePath(aliasName);
if (!QFileInfo::exists(aliasPath)) {
QFile::link(actualFileName, aliasPath);
}
}
};
// Ensure the canonical (proper-case) name exists
ensureAlias(canonicalName);
// Ensure the lowercase name exists
if (lowerName != canonicalName) {
ensureAlias(lowerName);
}
}
#endif
void GameGamebryo::ensureIniFilesExist(const QString& basePath)
{
// Make sure the target directory exists
QDir baseDir(basePath);
if (!baseDir.exists()) {
baseDir.mkpath(".");
}
for (const QString& iniFile : iniFiles()) {
QString targetPath = basePath + "/" + iniFile;
QFileInfo targetInfo(targetPath);
// Check if the target file already has adequate content
// (> 200 bytes = more than just MO2's BSA invalidation stub)
if (targetInfo.exists() && targetInfo.size() > 200) {
continue;
}
#ifndef _WIN32
// On Linux, search for a differently-cased version of this INI file
// (e.g., FalloutPrefs.ini when we expect falloutprefs.ini).
// We can't use resolveFileCaseInsensitive here because the exact-case
// file might exist as an empty stub while the real one has different case.
QString caseMismatchPath;
{
const QString target = targetInfo.fileName();
const QStringList entries =
baseDir.entryList(QDir::Files | QDir::Hidden | QDir::System);
for (const QString& entry : entries) {
if (entry != target &&
entry.compare(target, Qt::CaseInsensitive) == 0) {
caseMismatchPath = baseDir.absoluteFilePath(entry);
break;
}
}
}
// If a differently-cased version exists with adequate content,
// replace the stub (if any) with a copy of the real file.
if (!caseMismatchPath.isEmpty()) {
QFileInfo altInfo(caseMismatchPath);
if (altInfo.exists() && altInfo.size() > 200) {
if (targetInfo.exists()) {
QFile::remove(targetPath);
}
if (QFile::copy(altInfo.absoluteFilePath(), targetPath)) {
QFile::setPermissions(
targetPath,
QFile::permissions(targetPath) | QFile::WriteUser | QFile::WriteOwner);
MOBase::log::info("Copied case-mismatched INI '{}' -> '{}'",
altInfo.fileName(), iniFile);
} else {
MOBase::log::warn("Failed to copy case-mismatched INI '{}' -> '{}'",
altInfo.fileName(), iniFile);
}
// Ensure both proper-case and lowercase aliases exist
ensureCaseAliases(baseDir, iniFile, targetInfo.fileName());
continue;
}
}
#endif
// The INI doesn't exist or is a stub, and no adequate differently-cased
// version was found. Try to seed from the game's default INI template
// (e.g., Fallout_default.ini, Oblivion_default.ini).
QString baseName = QFileInfo(iniFile).completeBaseName();
QString defaultIniName = baseName + "_default.ini";
QString defaultIniPath =
resolveFileCaseInsensitive(gameDirectory().absoluteFilePath(defaultIniName));
if (QFileInfo::exists(defaultIniPath)) {
// Remove the stub file if it exists so we can replace it
if (targetInfo.exists()) {
QFile::remove(targetPath);
}
if (QFile::copy(defaultIniPath, targetPath)) {
// Make the copy writable
QFile::setPermissions(
targetPath,
QFile::permissions(targetPath) | QFile::WriteUser | QFile::WriteOwner);
MOBase::log::info("Seeded '{}' from default INI '{}'", iniFile, defaultIniPath);
} else {
MOBase::log::warn("Failed to copy default INI '{}' -> '{}'",
defaultIniPath, targetPath);
}
}
#ifndef _WIN32
// After creating/finding the INI, ensure both proper-case and lowercase
// versions exist so that any code path or the game itself can find it
// regardless of which casing it uses.
QFileInfo finalInfo(targetPath);
if (finalInfo.exists() || finalInfo.isSymLink()) {
ensureCaseAliases(baseDir, iniFile, finalInfo.fileName());
}
#endif
}
}
QString GameGamebryo::readIniValue(const QString& iniFile, const QString& section,
const QString& key, const QString& defaultValue)
{
// Read INI values directly without QSettings to avoid QSettings
// misinterpreting backslashes as line continuations.
QFile file(iniFile);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
return defaultValue;
}
QString sectionHeader = "[" + section + "]";
QTextStream in(&file);
bool inSection = false;
while (!in.atEnd()) {
QString line = in.readLine().trimmed();
if (line.startsWith('[') && line.endsWith(']')) {
if (inSection) {
break; // Left the target section without finding key
}
if (line.compare(sectionHeader, Qt::CaseInsensitive) == 0) {
inSection = true;
}
continue;
}
if (inSection && !line.isEmpty() && !line.startsWith(';') && !line.startsWith('#')) {
int eqPos = line.indexOf('=');
if (eqPos > 0) {
QString existingKey = line.left(eqPos).trimmed();
if (existingKey.compare(key, Qt::CaseInsensitive) == 0) {
return line.mid(eqPos + 1).trimmed();
}
}
}
}
return defaultValue;
}
QString GameGamebryo::selectedVariant() const
{
return m_GameVariant;
}
QString GameGamebryo::myGamesPath() const
{
return m_MyGamesPath;
}
/*static*/ QString GameGamebryo::getLootPath()
{
#ifdef _WIN32
return findInRegistry(HKEY_LOCAL_MACHINE, L"Software\\LOOT", L"Installed Path") +
"/Loot.exe";
#else
// On Linux, look for loot in PATH
QString systemLoot = QStandardPaths::findExecutable("loot");
if (!systemLoot.isEmpty())
return systemLoot;
return "loot";
#endif
}
static QString readFluorinePrefixPath()
{
QString configRoot =
QStandardPaths::writableLocation(QStandardPaths::ConfigLocation);
if (configRoot.isEmpty())
configRoot = QDir::homePath() + "/.config";
QString configPath = QDir(configRoot).filePath("fluorine/config.json");
QFile f(configPath);
if (!f.open(QIODevice::ReadOnly))
return {};
auto json = QJsonDocument::fromJson(f.readAll());
if (!json.isObject())
return {};
return json.object().value("prefix_path").toString().trimmed();
}
QString GameGamebryo::localAppFolder()
{
#ifdef _WIN32
QString result = getKnownFolderPath(FOLDERID_LocalAppData, false);
if (result.isEmpty()) {
// fallback: try the registry
result = getSpecialPath("Local AppData");
}
return result;
#else
// On Linux, AppData/Local lives inside the Wine prefix.
const QString configuredPrefix = readFluorinePrefixPath();
if (!configuredPrefix.isEmpty()) {
const QString appDataLocal =
QDir(configuredPrefix).filePath("drive_c/users/steamuser/AppData/Local");
if (QDir(appDataLocal).exists() || QDir().mkpath(appDataLocal)) {
return appDataLocal;
}
}
// Fallback: search Steam Proton prefixes
const QString steamRoot =
QDir::homePath() + "/.steam/steam/steamapps/compatdata";
QDir compatDir(steamRoot);
if (compatDir.exists()) {
for (const QString& appId :
compatDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
const QString appDataLocal = steamRoot + "/" + appId +
"/pfx/drive_c/users/steamuser/AppData/Local";
if (QDir(appDataLocal).exists()) {
return appDataLocal;
}
}
}
// Last resort: GenericDataLocation (won't work for Wine games but
// prevents crashes)
MOBase::log::warn("localAppFolder: could not find Wine prefix "
"AppData/Local, falling back to XDG data location");
return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
#endif
}
void GameGamebryo::copyToProfile(QString const& sourcePath,
QDir const& destinationDirectory,
QString const& sourceFileName)
{
copyToProfile(sourcePath, destinationDirectory, sourceFileName, sourceFileName);
}
void GameGamebryo::copyToProfile(QString const& sourcePath,
QDir const& destinationDirectory,
QString const& sourceFileName,
QString const& destinationFileName)
{
// Use case-insensitive check so we don't create duplicates on Linux
// (e.g. a profile with "SkyrimPrefs.ini" from Windows is not overwritten
// by a new "skyrimprefs.ini" copy).
QString filePath = resolveFileCaseInsensitive(
destinationDirectory.absoluteFilePath(destinationFileName));
if (!QFileInfo(filePath).exists()) {
QString sourceFile = sourcePath + "/" + sourceFileName;
// On Linux, try case-insensitive match if the exact source doesn't exist
sourceFile = resolveFileCaseInsensitive(sourceFile);
if (!MOBase::shellCopy(sourceFile, filePath)) {
// if copy file fails, create the file empty
QFile outputFile(filePath);
if (!outputFile.open(QIODevice::WriteOnly)) {
MOBase::log::warn("Failed to create fallback file '{}': {}", filePath,
outputFile.errorString());
}
}
}
#ifndef _WIN32
// Ensure both proper-case and lowercase versions exist (one as symlink)
QFileInfo actualFile(filePath);
if (actualFile.exists() || actualFile.isSymLink()) {
ensureCaseAliases(destinationDirectory, destinationFileName,
actualFile.fileName());
}
#endif
}
QString GameGamebryo::localAppName() const
{
// Default: derive from the My Games path. If myGamesPath() is
// e.g. ".../Documents/My Games/Skyrim Special Edition", we return
// "Skyrim Special Edition". This matches the AppData/Local subfolder
// for the vast majority of Bethesda games.
const QString mgp = myGamesPath();
if (!mgp.isEmpty()) {
const QString leaf = QDir(mgp).dirName();
if (!leaf.isEmpty() && leaf != QStringLiteral(".")) {
return leaf;
}
}
// Fallback: gameShortName is used by the base mappings()
return gameShortName();
}
MappingType GameGamebryo::mappings() const
{
MappingType result;
for (const QString& profileFile : {"plugins.txt", "loadorder.txt"}) {
result.push_back({m_Organizer->profilePath() + "/" + profileFile,
localAppFolder() + "/" + localAppName() + "/" + profileFile,
false});
}
return result;
}
#ifdef _WIN32
std::unique_ptr<BYTE[]> GameGamebryo::getRegValue(HKEY key, LPCWSTR path, LPCWSTR value,
DWORD flags, LPDWORD type)
{
DWORD size = 0;
HKEY subKey;
LONG res = ::RegOpenKeyExW(key, path, 0, KEY_QUERY_VALUE | KEY_WOW64_32KEY, &subKey);
if (res != ERROR_SUCCESS) {
res = ::RegOpenKeyExW(key, path, 0, KEY_QUERY_VALUE | KEY_WOW64_64KEY, &subKey);
if (res != ERROR_SUCCESS)
return std::unique_ptr<BYTE[]>();
}
res = ::RegGetValueW(subKey, L"", value, flags, type, nullptr, &size);
if (res == ERROR_FILE_NOT_FOUND || res == ERROR_UNSUPPORTED_TYPE) {
return std::unique_ptr<BYTE[]>();
}
if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) {
throw MOBase::MyException(
QObject::tr("failed to query registry path (preflight): %1").arg(res, 0, 16));
}
std::unique_ptr<BYTE[]> result(new BYTE[size]);
res = ::RegGetValueW(subKey, L"", value, flags, type, result.get(), &size);
if (res != ERROR_SUCCESS) {
throw MOBase::MyException(
QObject::tr("failed to query registry path (read): %1").arg(res, 0, 16));
}
return result;
}
QString GameGamebryo::findInRegistry(HKEY baseKey, LPCWSTR path, LPCWSTR value)
{
std::unique_ptr<BYTE[]> buffer =
getRegValue(baseKey, path, value, RRF_RT_REG_SZ | RRF_NOEXPAND);
return QString::fromUtf16(reinterpret_cast<const char16_t*>(buffer.get()));
}
QString GameGamebryo::getKnownFolderPath(REFKNOWNFOLDERID folderId, bool useDefault)
{
PWSTR path = nullptr;
ON_BLOCK_EXIT([&]() {
if (path != nullptr)
::CoTaskMemFree(path);
});
if (::SHGetKnownFolderPath(folderId, useDefault ? KF_FLAG_DEFAULT_PATH : 0, NULL,
&path) == S_OK) {
return QDir::fromNativeSeparators(QString::fromWCharArray(path));
} else {
return QString();
}
}
QString GameGamebryo::getSpecialPath(const QString& name)
{
QString base = findInRegistry(
HKEY_CURRENT_USER,
L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders",
name.toStdWString().c_str());
WCHAR temp[MAX_PATH];
if (::ExpandEnvironmentStringsW(base.toStdWString().c_str(), temp, MAX_PATH) != 0) {
return QString::fromWCharArray(temp);
} else {
return base;
}
}
#endif // _WIN32
QString GameGamebryo::determineMyGamesPath(const QString& gameName)
{
const QString pattern = "%1/My Games/" + gameName;
auto tryDir = [&](const QString& dir) -> std::optional<QString> {
if (dir.isEmpty()) {
return {};
}
const auto path = pattern.arg(dir);
if (!QFileInfo(path).exists()) {
return {};
}
return path;
};
#ifdef _WIN32
// a) this is the way it should work. get the configured My Documents directory
if (auto d = tryDir(getKnownFolderPath(FOLDERID_Documents, false))) {
return *d;
}
// b) if there is no <game> directory there, look in the default directory
if (auto d = tryDir(getKnownFolderPath(FOLDERID_Documents, true))) {
return *d;
}
// c) finally, look in the registry. This is discouraged
if (auto d = tryDir(getSpecialPath("Personal"))) {
return *d;
}
#else
// On Linux, My Games is inside the Wine prefix's Documents folder.
// Check common Wine prefix locations for steamuser Documents.
QStringList prefixDocPaths;
// First check the configured prefix from fluorine config (most reliable).
const QString configuredPrefix = readFluorinePrefixPath();
if (!configuredPrefix.isEmpty()) {
const QString configuredDocs =
QDir(configuredPrefix).filePath("drive_c/users/steamuser/Documents");
prefixDocPaths.append(configuredDocs);
}
// Standard Steam Proton prefix paths
QString steamRoot = QDir::homePath() + "/.steam/steam/steamapps/compatdata";
QDir compatDir(steamRoot);
if (compatDir.exists()) {
QStringList appIds = compatDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
for (const QString& appId : appIds) {
prefixDocPaths.append(steamRoot + "/" + appId +
"/pfx/drive_c/users/steamuser/Documents");
}
}
// Also check XDG Documents (for native games or manual setups)
prefixDocPaths.append(
QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation));
for (const QString& docPath : prefixDocPaths) {
if (auto d = tryDir(docPath)) {
MOBase::log::debug("determineMyGamesPath: found '{}' for game '{}'", *d, gameName);
return *d;
}
}
// If no existing directory was found, try to create it in the configured prefix
// so that the game launcher can populate it on first run.
if (!configuredPrefix.isEmpty()) {
const QString configuredDocs =
QDir(configuredPrefix).filePath("drive_c/users/steamuser/Documents");
const QString newPath = pattern.arg(configuredDocs);
if (QDir().mkpath(newPath)) {
MOBase::log::info("determineMyGamesPath: created '{}' for game '{}'", newPath,
gameName);
return newPath;
}
}
MOBase::log::warn("determineMyGamesPath: could not find My Games path for '{}'",
gameName);
#endif
return {};
}
QString GameGamebryo::parseEpicGamesLocation(const QStringList& manifests)
{
#ifdef _WIN32
// Use the registry entry to find the EGL Data dir first, just in case something
// changes
QString manifestDir = findInRegistry(
HKEY_LOCAL_MACHINE, L"Software\\Epic Games\\EpicGamesLauncher", L"AppDataPath");
if (manifestDir.isEmpty())
manifestDir = getKnownFolderPath(FOLDERID_ProgramData, false) +
"\\Epic\\EpicGamesLauncher\\Data\\";
manifestDir += "Manifests";
#else
// Epic Games on Linux (via Heroic or Legendary)
QString manifestDir = QDir::homePath() +
"/.config/heroic/store_cache/egl_manifests";
if (!QDir(manifestDir).exists()) {
// Try Legendary
manifestDir = QDir::homePath() + "/.config/legendary/installed.json";
// Legendary uses a different format - skip for now
return "";
}
#endif
QDir epicManifests(manifestDir, "*.item",
QDir::SortFlags(QDir::Name | QDir::IgnoreCase), QDir::Files);
if (epicManifests.exists()) {
QDirIterator it(epicManifests);
while (it.hasNext()) {
QString manifestFile = it.next();
QFile manifest(manifestFile);
if (!manifest.open(QIODevice::ReadOnly)) {
qWarning("Couldn't open Epic Games manifest file.");
continue;
}
QByteArray manifestData = manifest.readAll();
QJsonDocument manifestJson(QJsonDocument::fromJson(manifestData));
if (manifests.contains(manifestJson["AppName"].toString())) {
return manifestJson["InstallLocation"].toString();
}
}
}
return "";
}
QString GameGamebryo::parseSteamLocation(const QString& appid,
const QString& directoryName)
{
#ifdef HAS_NAK_FFI
// Use NaK to find Steam path
char* steamPathC = nak_find_steam_path();
QString steamLocation;
if (steamPathC) {
steamLocation = QString::fromUtf8(steamPathC);
nak_string_free(steamPathC);
}
#elif defined(_WIN32)
QString path = "Software\\Valve\\Steam";
QString steamLocation =
findInRegistry(HKEY_CURRENT_USER, path.toStdWString().c_str(), L"SteamPath");
#else
// Fallback: common Steam locations on Linux
QString steamLocation = QDir::homePath() + "/.steam/steam";
if (!QDir(steamLocation).exists()) {
steamLocation = QDir::homePath() + "/.local/share/Steam";
}
#endif
if (!steamLocation.isEmpty()) {
QString steamLibraryLocation;
QString steamLibraries(steamLocation + "/" + "config" + "/" +
"libraryfolders.vdf");
if (QFile(steamLibraries).exists()) {
std::ifstream file(steamLibraries.toStdString());
auto root = tyti::vdf::read(file);
for (auto child : root.childs) {
tyti::vdf::object* library = child.second.get();
auto apps = library->childs["apps"];
if (apps->attribs.contains(appid.toStdString())) {
steamLibraryLocation = QString::fromStdString(library->attribs["path"]);
break;
}
}
}
if (!steamLibraryLocation.isEmpty()) {
if (!directoryName.isEmpty()) {
QString gameLocation = steamLibraryLocation + "/" + "steamapps" + "/" +
"common" + "/" + directoryName;
if (QDir(gameLocation).exists()) {
return gameLocation;
}
}
// Fallback: resolve install dir from appmanifest_<appid>.acf.
QString manifestPath = steamLibraryLocation + "/steamapps/appmanifest_" + appid +
".acf";
if (QFile::exists(manifestPath)) {
std::ifstream manifestFile(manifestPath.toStdString());
auto manifest = tyti::vdf::read(manifestFile);
auto appStateIt = manifest.childs.find("AppState");
if (appStateIt != manifest.childs.end()) {
const auto& attrs = appStateIt->second->attribs;
auto installdirIt = attrs.find("installdir");
if (installdirIt != attrs.end()) {
QString installdir = QString::fromStdString(installdirIt->second);
QString gameLocation = steamLibraryLocation + "/steamapps/common/" + installdir;
if (QDir(gameLocation).exists()) {
return gameLocation;
}
}
}
}
}
}
return "";
}
void GameGamebryo::registerFeature(std::shared_ptr<MOBase::GameFeature> feature)
{
// priority does not matter, this is a game plugin so will get lowest priority in MO2
m_Organizer->gameFeatures()->registerFeature(this, feature, 0, true);
}
|