• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ParadoxGameConverters / commonItems / 46555577590

01 Dec 2026 01:43AM UTC coverage: 74.894% (-3.9%) from 78.841%
46555577590

Pull #351

github

web-flow
Merge 0e9fbdfe4 into b9e513a97
Pull Request #351: getGOGInstallPath function

3 of 124 new or added lines in 1 file covered. (2.42%)

1 existing line in 1 file now uncovered.

1760 of 2350 relevant lines covered (74.89%)

220.45 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

1.32
/LinuxUtils.cpp
1
#include "Log.h"
2
#include "OSCompatibilityLayer.h"
3
#include <algorithm>
4
#include <cctype>
5
#include <cerrno>
6
#include <cstdarg>
7
#include <cstdlib>
8
#include <cstring>
9
#include <dirent.h>
10
#include <fstream>
11
#include <fcntl.h>
12
#include <filesystem>
13
#include <iconv.h>
14
#include <iostream>
15
#include <set>
16
#include <sys/sendfile.h>
17
#include <sys/stat.h>
18
#include <unistd.h>
19
#include <vector>
20

21

22
namespace fs = std::filesystem;
23

24

25

26
void sprintf_s_Linux(char* __restrict __s, size_t __maxlength, const char* __restrict __format, ...)
×
27
{
28
        va_list arg_ptr;
29
        va_start(arg_ptr, __format);
×
30
        snprintf(__s, __maxlength, __format, arg_ptr);
×
31
        va_end(arg_ptr);
×
32
}
×
33

34

35
void strcpy_s_Linux(char* __restrict __dest, const char* __restrict __src)
×
36
{
37
        strcpy(__dest, __src);
×
38
}
×
39

40

41
int fopen_s_Linux(FILE** file, const char* filename, const char* mode)
×
42
{
43
        *file = fopen(filename, mode);
×
44
        return *file == NULL;
×
45
}
46

47

48
void fprintf_s_Linux(FILE* file, const char* format, ...)
×
49
{
50
        va_list arg_ptr;
51
        va_start(arg_ptr, format);
×
52
        fprintf(file, format, arg_ptr);
×
53
        va_end(arg_ptr);
×
54
}
×
55

56
#ifdef __linux__
57
HANDLE GetStdHandle(int nothing)
×
58
{
59
        return 1;
×
60
}
61
#endif
62

63
namespace commonItems
64
{
65

66
/*
67
        Helper function to determine that returns false if the filename is a:
68
        - a hidden folder or folder (starting with '.')
69
        - empty
70
        - a parent directory ('.')
71
        - the current directory ('..')
72
*/
73
bool IsRegularNodeName(const std::string& name)
×
74
{
75
        return !name.empty() && *name.begin() != '.';
×
76
}
77

78

79
/*
80
                returns false if a node name is empty or a placeholder for the current or parent directory, true otherwise
81
*/
82
bool IsActualNodeName(const std::string& name)
×
83
{
84
        for (auto i = name.begin(); i != name.end(); ++i)
×
85
        {
86
                if (*i != '.')
×
87
                {
88
                        return true;
×
89
                }
90
        }
91
        return false;
×
92
}
93

94

95
const char* StripTrailingSeparators(const char* path, std::size_t length)
×
96
{
97
        switch (length)
×
98
        {
99
                case 0:
×
100
                        return path;
×
101
                case 1:
×
102
                        if (*path != '/')
×
103
                        {
104
                                ++path;
×
105
                        }
106
                        return path;
×
107
                default:
×
108
                        const char* i = path + length - 1;
×
109
                        while (i != path && *i == '/')
×
110
                        {
111
                                --i;
×
112
                        }
113
                        ++i;
×
114
                        return i;
×
115
        }
116
}
117

118
const char* StripLeadingSeparators(const char* path)
×
119
{
120
        while (true)
121
        {
122
                if (*path != '/')
×
123
                {
124
                        return path;
×
125
                }
126
                ++path;
×
127
        }
128
}
129

130

131
/*
132
        Concatenates two paths and strips out leading and trailing '/'
133
*/
134
std::string ConcatenatePaths(const std::string& first, const std::string& second)
×
135
{
136
        const char* first_begin = first.c_str();
×
137
        const char* first_end = StripTrailingSeparators(first_begin, first.length());
×
138
        const char* second_begin = StripLeadingSeparators(second.c_str());
×
139
        return std::string(first_begin, first_end) + "/" + std::string(second_begin);
×
140
}
141

142
/*
143
        Concatenates a path and a valid node name
144
        A valid node name is a non empty string that does not contain '/'
145
        use ConcatenatePaths for other concatenations
146
*/
147
std::string ConcatenateNodeName(const std::string& path, const std::string& name)
×
148
{
149
        const char* path_begin = path.c_str();
×
150
        const char* path_end = StripTrailingSeparators(path_begin, path.length());
×
151
        return std::string(path_begin, path_end) + "/" + name;
×
152
}
153

154
/*
155
        Splits the node name from the rest of the path and returns both values as a pair (path, node_name)
156
*/
157
std::pair<std::string, std::string> SplitNodeNameFromPath(const std::string& path)
×
158
{
159
        if (path.empty())
×
160
        {
161
                return std::make_pair(std::string(), std::string());
×
162
        }
163
        const char* buffer = path.c_str();
×
164
        const char* end = buffer + path.length() - 1;
×
165
        while (end != buffer && *end == '/')
×
166
        {
167
                --end;
×
168
        }
169
        if (end == buffer)
×
170
        {
171
                return std::make_pair(std::string(buffer, buffer + 1), std::string());
×
172
        }
173
        const char* begin = end;
×
174
        ++end;
×
175
        while (begin != buffer && *begin != '/')
×
176
        {
177
                --begin;
×
178
        }
179
        if (*begin == '/')
×
180
        {
181
                ++begin;
×
182
        }
183

184
        return std::make_pair(std::string(buffer, begin), std::string(begin, end));
×
185
}
186

187
bool IsLinuxPathElementSeparator(char c)
×
188
{
189
        return c == '/';
×
190
}
191

192
bool isLinuxPathCharacter(char c)
×
193
{
194
        return c != '/';
×
195
}
196

197
char* CopyFolderPathElement(const char*& input_begin, const char* input_end, char* output)
×
198
{
199
        while (input_begin != input_end)
×
200
        {
201
                const char c = *input_begin;
×
202
                ++input_begin;
×
203
                if (c == '/')
×
204
                {
205
                        return output;
×
206
                }
207

208
                *output = c;
×
209
                ++output;
×
210
        }
211
        return output;
×
212
}
213

214

215

216
bool GetFileMode(const std::string& path, mode_t& result)
×
217
{
218
        struct stat status;
219
        if (stat(path.c_str(), &status) != 0)
×
220
        {
221
                return false;
×
222
        }
223

224
        result = status.st_mode;
×
225
        return true;
×
226
}
227

228
bool IsRegularFile(const std::string& path)
×
229
{
230
        return !fs::is_directory(fs::u8path(path));
×
231
}
232

233
bool IsDirectory(const std::string& path)
×
234
{
235
        return fs::is_directory(fs::u8path(path));
×
236
}
237

238

239
void WriteToConsole(LogLevel level, const std::string& logMessage)
×
240
{
241
        if (level != LogLevel::Debug) // Don't log debug messages to console.
×
242
        {
243
                std::cout << logMessage;
×
244
        }
245
}
×
246

247
/*
248
        Implemented all messages for error codes used in LinuxUtils.cpp.
249
*/
250
std::string GetLastErrorString()
×
251
{
252
        return strerror(errno);
×
253
}
254

255
bool deleteFile(const std::string& unresolvedFile)
×
256
{
257
        fs::remove(fs::u8path(unresolvedFile));
×
258
        return true;
×
259
}
260

261

262
/*
263
 * Forward declarations for conversion helper function and types
264
 */
265
class ConversionInputBuffer;
266

267
class ConversionOutputBuffer;
268

269
bool ConvertString(const char* toCode, const char* fromCode, ConversionInputBuffer& in, ConversionOutputBuffer& out);
270

271
/*
272
 * helper class to copy buffer from string and keep track of remainder and current position
273
 */
274
class ConversionInputBuffer
275
{
276
        char* data;
277
        char* in_buffer;
278
        std::size_t remainder;
279

280
  public:
281
        explicit ConversionInputBuffer(const std::string& input): data(new char[input.length()]), in_buffer(data), remainder(input.length())
×
282
        {
283
                // POSIX iconv expects a pointer to char *, not to const char * and consequently does not guarantee that the input
284
                // sequence is not modified so we copy it into the buffer before attempting conversion
285
                std::copy(input.begin(), input.end(), data);
×
286
        }
×
287
        ConversionInputBuffer(const ConversionInputBuffer&) = delete;
288
        ConversionInputBuffer& operator=(const ConversionInputBuffer&) = delete;
289
        ConversionInputBuffer(ConversionInputBuffer&&) = delete;
290
        ConversionInputBuffer& operator=(ConversionInputBuffer&&) = delete;
291

292
        template <typename String> explicit ConversionInputBuffer(const String& input)
×
293
        {
294
                typedef typename String::value_type Char;
295
                remainder = sizeof(Char) * input.length();
×
296
                data = new char[remainder];
×
297
                in_buffer = data;
×
298
                const auto* const input_str = reinterpret_cast<const char*>(input.c_str());
×
299
                std::copy(input_str, input_str + remainder, data);
×
300
        }
×
301

302
        ~ConversionInputBuffer() { delete[] data; }
×
303

304
        [[nodiscard]] bool has_remaining_bytes() const { return remainder != 0; }
×
305

306
        friend bool ConvertBuffer(const char* fromCode, const char* toCode, ConversionInputBuffer& from, ConversionOutputBuffer& to);
307
};
308

309
/*
310
 * helper class to manage buffer pointers and sizes while providing access to internal buffers for use in iconv
311
 * functions
312
 */
313
class ConversionOutputBuffer
314
{
315
        std::size_t size;
316
        std::size_t remainder;
317
        std::size_t block_size;
318
        char* data = nullptr;
319
        char* out_buffer = nullptr;
320

321
        template <typename String> struct OutputStrHelper
322
        {
323
                typedef typename String::value_type Char;
324

325
                static void str(String& output, const char* buffer, std::size_t length)
×
326
                {
327
                        size_t output_length = length / sizeof(Char);
×
328
                        wchar_t output_buffer[output_length];
×
329
                        memset(output_buffer, 0, output_length * sizeof(wchar_t));
×
330
                        std::mbstowcs(output_buffer, buffer, output_length);
×
331
                        output.assign(output_buffer, output_length);
×
332
                }
×
333
        };
334

335
        template <typename Traits, typename Alloc> struct OutputStrHelper<std::basic_string<char, Traits, Alloc>>
336
        {
337
                static void str(std::basic_string<char, Traits, Alloc>& output, char* buffer, std::size_t length) { output.assign(buffer, length); }
×
338
        };
339

340
  public:
341
        explicit ConversionOutputBuffer(std::size_t initial_size = 0, std::size_t increment_block_size = 1024 * 1024):
×
342
                 size(initial_size), remainder(initial_size), block_size(increment_block_size)
×
343
        {
344
                if (size != 0)
×
345
                {
346
                        data = new char[size];
×
347
                        out_buffer = data;
×
348
                }
349
        }
×
350
        ConversionOutputBuffer(const ConversionOutputBuffer&) = delete;
351
        ConversionOutputBuffer& operator=(const ConversionOutputBuffer&) = delete;
352
        ConversionOutputBuffer(ConversionOutputBuffer&&) = delete;
353
        ConversionOutputBuffer& operator=(ConversionOutputBuffer&&) = delete;
354

355
        ~ConversionOutputBuffer() { delete[] data; }
×
356

357
        [[nodiscard]] bool full() const { return remainder != 0; }
358

359
        [[nodiscard]] size_t length() const { return size - remainder; }
×
360

361
        [[nodiscard]] std::string str() const { return std::string(data, data + length()); }
362

363
        template <typename String> void str(String& output) const { OutputStrHelper<String>::str(output, data, length()); }
×
364

365
        bool ensure_capacity(std::size_t capacity)
×
366
        {
367
                if (size < capacity)
×
368
                {
369
                        const size_t len = length();
×
370
                        char* new_data = new char[capacity];
×
371
                        std::copy(data, data + len, new_data);
×
372
                        delete[] data;
×
373
                        data = new_data;
×
374
                        out_buffer = data + len;
×
375
                        remainder += capacity - size;
×
376
                        size = capacity;
×
377
                        return true;
×
378
                }
379

380
                return false;
×
381
        }
382

383
        bool grow() { return ensure_capacity(size + block_size); }
×
384

385
        friend bool ConvertBuffer(const char* fromCode, const char* toCode, ConversionInputBuffer& from, ConversionOutputBuffer& to);
386
};
387

388
bool ConvertBuffer(const char* fromCode, const char* toCode, ConversionInputBuffer& from, ConversionOutputBuffer& to)
×
389
{
390
        iconv_t descriptor = iconv_open(toCode, fromCode);
×
391
        if (descriptor == reinterpret_cast<iconv_t>(-1))
×
392
        {
393
                LOG(LogLevel::Error) << "unable to recode string from '" << fromCode << "' to '" << toCode << ": not supported on this system";
×
394
                return false;
×
395
        }
396
        while (from.has_remaining_bytes())
×
397
        {
398
                if (iconv(descriptor, &from.in_buffer, &from.remainder, &to.out_buffer, &to.remainder) == static_cast<size_t>(-1))
×
399
                {
400
                        switch (errno)
×
401
                        {
402
                                case E2BIG:
×
403
                                        to.grow();
×
404
                                        break;
×
405
                                case EILSEQ:
×
406
                                        LOG(LogLevel::Error) << "invalid input sequence encountered during conversion of " << from.data << " from " << fromCode << " to " << toCode
×
407
                                                                                                << " : " << std::hex << *reinterpret_cast<uint16_t*>(from.in_buffer);
×
408
                                        return false;
×
409
                                case EINVAL:
×
410
                                        LOG(LogLevel::Error) << "incomplete input sequence encountered during conversion from " << fromCode << " to " << toCode;
×
411
                                        return false;
×
412
                                default:
×
413
                                        break;
×
414
                        }
415
                }
416
        }
417
        iconv_close(descriptor);
×
418
        return true;
×
419
}
420

421
/*
422
 * InputString and OutputString should be instantiations of basic_string, or have a similar API
423
 * InputString should implement:
424
 * member type value_type
425
 * member function value_type begin() const
426
 * member function value_type end() const
427
 * member function size_t length() const
428
 *
429
 */
430
template <typename InputString, typename OutputString>
431
bool ConvertString(const char* fromCode, const char* toCode, const InputString& from, OutputString& to, std::size_t to_buffer_size = 0)
×
432
{
433
        if (to_buffer_size == 0)
×
434
        {
435
                to_buffer_size = from.length();
×
436
        }
437
        ConversionInputBuffer from_buffer(from);
×
438
        ConversionOutputBuffer to_buffer(to_buffer_size);
×
439
        if (ConvertBuffer(fromCode, toCode, from_buffer, to_buffer))
×
440
        {
441
                to_buffer.str(to);
×
442
                return true;
×
443
        }
444

445
        return false;
×
446
}
×
447

448
template <typename InputString, typename OutputString>
449
OutputString ConvertString(const char* fromCode, const char* toCode, const InputString& from, std::size_t to_buffer_size = 0)
×
450
{
451
        OutputString to;
×
452
        ConvertString(fromCode, toCode, from, to, to_buffer_size);
×
453
        return to;
×
454
}
×
455

456
std::string convertUTF8ToASCII(const std::string& UTF8)
×
457
{
458
        return ConvertString<std::string, std::string>("UTF-8", "ASCII//TRANSLIT", UTF8);
×
459
}
460

461
std::string convertUTF8To8859_15(const std::string& UTF8)
×
462
{
463
        return ConvertString<std::string, std::string>("UTF−8", "ISO−8859−15//TRANSLIT", UTF8);
×
464
}
465

466

467
std::string convertUTF8ToWin1252(const std::string& UTF8)
×
468
{
469
        return ConvertString<std::string, std::string>("UTF−8", "CP1252//TRANSLIT", UTF8);
×
470
}
471

472
std::string convertUTF8ToWin1251(const std::string& UTF8)
×
473
{
474
        return ConvertString<std::string, std::string>("UTF−8", "CP1251//TRANSLIT", UTF8);
×
475
}
476

477
std::string convertUTF8ToWin1250(const std::string& UTF8)
×
478
{
479
        return ConvertString<std::string, std::string>("UTF−8", "CP1250//TRANSLIT", UTF8);
×
480
}
481

482
std::string convert8859_15ToUTF8(const std::string& input)
×
483
{
484
        return ConvertString<std::string, std::string>("ISO−8859−15", "UTF-8//TRANSLIT", input);
×
485
}
486

487
/*
488
        Warning: Does not actually return an UTF-16 sequence.
489
        This is an implementation of the original Windows-based API which uses UTF-16 LE as the system dependent wchar_t
490
        encoding This behaviour is replicated on Linux but it uses the (system dependent) wchar_t encoding.
491
*/
492
std::wstring convert8859_15ToUTF16(const std::string& input)
×
493
{
494
        return ConvertString<std::string, std::wstring>("ISO−8859−15", "wchar_t//TRANSLIT", input);
×
495
}
496

497
std::string convertWin1250ToUTF8(const std::string& Win1250)
×
498
{
499
        return ConvertString<std::string, std::string>("CP1250", "UTF-8//TRANSLIT", Win1250);
×
500
}
501

502
std::string convertWin1251ToUTF8(const std::string& Win1251)
×
503
{
504
        return ConvertString<std::string, std::string>("CP1251", "UTF-8//TRANSLIT", Win1251);
×
505
}
506

507
std::string convertWin1252ToUTF8(const std::string& Win1252)
×
508
{
509
        return ConvertString<std::string, std::string>("CP1252", "UTF-8//TRANSLIT", Win1252);
×
510
}
511

512

513
std::wstring convertWin1252ToUTF16(const std::string& Win1252)
×
514
{
515
        return ConvertString<std::string, std::wstring>("CP1252", "wchar_t//TRANSLIT", Win1252);
×
516
}
517

518
/*
519
        Warning: Does not actually return an UTF-16 sequence.
520
        This is an implementation of the original Windows-based API which uses UTF-16 LE as the system dependent wchar_t
521
        encoding This behaviour is replicated on Linux but it uses the (system dependent) wchar_t encoding.
522
*/
523
std::wstring convertUTF8ToUTF16(const std::string& UTF8)
×
524
{
525
        std::vector<unsigned long> unicode;
×
526
        for (size_t i = 0; i < UTF8.size();)
×
527
        {
528
                uint32_t uni = '\0';
×
529
                size_t todo = 0;
×
530
                unsigned char ch = UTF8[i++];
×
531
                if (ch <= 0x7F)
×
532
                {
533
                        uni = ch;
×
534
                        todo = 0;
×
535
                }
536
                else if (ch <= 0xBF)
×
537
                {
538
                        throw std::logic_error("not a UTF-8 string");
×
539
                }
540
                else if (ch <= 0xDF)
×
541
                {
542
                        uni = ch & 0x1F;
×
543
                        todo = 1;
×
544
                }
545
                else if (ch <= 0xEF)
×
546
                {
547
                        uni = ch & 0x0F;
×
548
                        todo = 2;
×
549
                }
550
                else if (ch <= 0xF7)
×
551
                {
552
                        uni = ch & 0x07;
×
553
                        todo = 3;
×
554
                }
555
                else
556
                {
557
                        throw std::logic_error("not a UTF-8 string");
×
558
                }
559
                for (size_t j = 0; j < todo; ++j)
×
560
                {
561
                        if (i == UTF8.size())
×
562
                                throw std::logic_error("not a UTF-8 string");
×
563
                        ch = UTF8[i++];
×
564
                        if (ch < 0x80 || ch > 0xBF)
×
565
                                throw std::logic_error("not a UTF-8 string");
×
566
                        uni <<= 6;
×
567
                        uni += ch & 0x3F;
×
568
                }
569
                if (uni >= 0xD800 && uni <= 0xDFFF)
×
570
                        throw std::logic_error("not a UTF-8 string");
×
571
                if (uni > 0x10FFFF)
×
572
                        throw std::logic_error("not a UTF-8 string");
×
573
                unicode.push_back(uni);
×
574
        }
575
        std::wstring utf16;
×
576
        for (size_t i = 0; i < unicode.size(); ++i)
×
577
        {
578
                unsigned long uni = unicode[i];
×
579
                if (uni <= 0xFFFF)
×
580
                {
581
                        utf16 += static_cast<wchar_t>(uni);
×
582
                }
583
                else
584
                {
585
                        uni -= 0x10000;
×
586
                        utf16 += static_cast<wchar_t>((uni >> 10) + 0xD800);
×
587
                        utf16 += static_cast<wchar_t>((uni & 0x3FF) + 0xDC00);
×
588
                }
589
        }
590
        return utf16;
×
591
}
×
592

593

594
std::string convertToUTF8(const std::wstring& input)
×
595
{
596
        return ConvertString<std::wstring, std::string>("wchar_t", "UTF-8//TRANSLIT", input);
×
597
}
598

599

600
std::string utf16_to_utf8(const std::u16string& utf16_string)
×
601
{
602
        std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> conversion;
×
603
        return conversion.to_bytes(utf16_string);
×
604
}
×
605

606

607
std::string UTF16ToUTF8(const std::wstring& UTF16)
×
608
{
609
        const std::u16string u16str(UTF16.begin(), UTF16.end());
×
610
        return utf16_to_utf8(u16str);
×
611
}
×
612

613

614
std::optional<std::filesystem::path> getSteamInstallPath(const std::string& steamID)
1 ✔
615
{
616
        // TODO: Write some actual code for this when able.
617
        return std::nullopt;
1 ✔
618
}
619

620
namespace
621
{
622

NEW
623
std::string TrimString(const std::string& input)
×
624
{
NEW
625
        const auto firstNonWhitespace = input.find_first_not_of(" \t\r\n");
×
NEW
626
        if (firstNonWhitespace == std::string::npos)
×
627
        {
NEW
628
                return "";
×
629
        }
630

NEW
631
        const auto lastNonWhitespace = input.find_last_not_of(" \t\r\n");
×
NEW
632
        return input.substr(firstNonWhitespace, lastNonWhitespace - firstNonWhitespace + 1);
×
633
}
634

NEW
635
std::string ToLower(const std::string& input)
×
636
{
NEW
637
        std::string lowered = input;
×
NEW
638
        std::transform(lowered.begin(),
×
639
                 lowered.end(),
640
                 lowered.begin(),
NEW
641
                 [](const unsigned char character) { return static_cast<char>(std::tolower(character)); });
×
NEW
642
        return lowered;
×
643
}
644

NEW
645
std::string Unquote(const std::string& input)
×
646
{
NEW
647
        auto trimmedInput = TrimString(input);
×
NEW
648
        if (trimmedInput.size() >= 2 && trimmedInput.front() == '"' && trimmedInput.back() == '"')
×
649
        {
NEW
650
                trimmedInput = trimmedInput.substr(1, trimmedInput.size() - 2);
×
651
        }
NEW
652
        return trimmedInput;
×
NEW
653
}
×
654

NEW
655
std::string UnescapeRegistryString(const std::string& input)
×
656
{
NEW
657
        std::string output;
×
NEW
658
        output.reserve(input.size());
×
659

NEW
660
        for (size_t i = 0; i < input.size(); ++i)
×
661
        {
NEW
662
                if (input[i] == '\\' && i + 1 < input.size())
×
663
                {
NEW
664
                        ++i;
×
665
                }
NEW
666
                output.push_back(input[i]);
×
667
        }
668

NEW
669
        return output;
×
NEW
670
}
×
671

NEW
672
std::optional<fs::path> ConvertWinePathToLinuxPath(const std::string& windowsPath, const fs::path& winePrefixPath)
×
673
{
NEW
674
        if (windowsPath.size() < 3 || windowsPath[1] != ':')
×
675
        {
NEW
676
                return std::nullopt;
×
677
        }
678

NEW
679
        const char driveLetter = static_cast<char>(std::tolower(static_cast<unsigned char>(windowsPath[0])));
×
NEW
680
        if (!std::isalpha(static_cast<unsigned char>(driveLetter)))
×
681
        {
NEW
682
                return std::nullopt;
×
683
        }
684

NEW
685
        std::string relativePath = windowsPath.substr(2);
×
NEW
686
        std::replace(relativePath.begin(), relativePath.end(), '\\', '/');
×
NEW
687
        while (!relativePath.empty() && relativePath.front() == '/')
×
688
        {
NEW
689
                relativePath.erase(relativePath.begin());
×
690
        }
691

NEW
692
        fs::path translatedPath = winePrefixPath / ("drive_" + std::string(1, driveLetter));
×
NEW
693
        if (!relativePath.empty())
×
694
        {
NEW
695
                translatedPath /= fs::path(relativePath);
×
696
        }
697

NEW
698
        std::error_code fileError;
×
NEW
699
        if (fs::exists(translatedPath, fileError))
×
700
        {
NEW
701
                return translatedPath;
×
702
        }
703

NEW
704
        return std::nullopt;
×
NEW
705
}
×
706

NEW
707
std::optional<fs::path> GetGOGInstallPathInRegistryFile(const fs::path& registryFilePath, const std::string& gogID)
×
708
{
NEW
709
        std::ifstream registryFile(registryFilePath);
×
NEW
710
        if (!registryFile.is_open())
×
711
        {
NEW
712
                return std::nullopt;
×
713
        }
714

NEW
715
        const auto loweredGOGID = ToLower(gogID);
×
716
        const std::set<std::string> targetSections = {
717
                 "software\\\\gog.com\\\\games\\\\" + loweredGOGID,
718
                 "software\\\\wow6432node\\\\gog.com\\\\games\\\\" + loweredGOGID,
719
                 "hkey_local_machine\\\\software\\\\gog.com\\\\games\\\\" + loweredGOGID,
NEW
720
                 "hkey_local_machine\\\\software\\\\wow6432node\\\\gog.com\\\\games\\\\" + loweredGOGID};
×
721

NEW
722
        bool inTargetSection = false;
×
NEW
723
        std::string line;
×
NEW
724
        while (std::getline(registryFile, line))
×
725
        {
NEW
726
                const auto trimmedLine = TrimString(line);
×
NEW
727
                if (trimmedLine.empty() || trimmedLine.front() == ';' || trimmedLine.front() == '#')
×
728
                {
NEW
729
                        continue;
×
730
                }
731

NEW
732
                if (trimmedLine.front() == '[' && trimmedLine.back() == ']')
×
733
                {
NEW
734
                        const auto sectionName = ToLower(trimmedLine.substr(1, trimmedLine.size() - 2));
×
NEW
735
                        inTargetSection = targetSections.contains(sectionName);
×
NEW
736
                        continue;
×
NEW
737
                }
×
738

NEW
739
                if (!inTargetSection)
×
740
                {
NEW
741
                        continue;
×
742
                }
743

NEW
744
                const auto separatorPosition = trimmedLine.find('=');
×
NEW
745
                if (separatorPosition == std::string::npos)
×
746
                {
NEW
747
                        continue;
×
748
                }
749

NEW
750
                const auto valueName = ToLower(Unquote(trimmedLine.substr(0, separatorPosition)));
×
NEW
751
                if (valueName != "path")
×
752
                {
NEW
753
                        continue;
×
754
                }
755

NEW
756
                const auto rawValue = UnescapeRegistryString(Unquote(trimmedLine.substr(separatorPosition + 1)));
×
NEW
757
                if (rawValue.empty())
×
758
                {
NEW
759
                        continue;
×
760
                }
761

NEW
762
                if (const auto translatedPath = ConvertWinePathToLinuxPath(rawValue, registryFilePath.parent_path()); translatedPath)
×
763
                {
NEW
764
                        return translatedPath;
×
NEW
765
                }
×
766

NEW
767
                const fs::path existingAbsolutePath(rawValue);
×
NEW
768
                std::error_code fileError;
×
NEW
769
                if (existingAbsolutePath.is_absolute() && fs::exists(existingAbsolutePath, fileError))
×
770
                {
NEW
771
                        return existingAbsolutePath;
×
772
                }
NEW
773
        }
×
774

NEW
775
        return std::nullopt;
×
NEW
776
}
×
777

NEW
778
std::set<fs::path> GetWineRegistryFiles()
×
779
{
NEW
780
        std::set<fs::path> registryFiles;
×
781

NEW
782
        const char* homeDirectory = std::getenv("HOME");
×
NEW
783
        if (homeDirectory == nullptr || *homeDirectory == '\0')
×
784
        {
NEW
785
                return registryFiles;
×
786
        }
787

NEW
788
        const fs::path homePath(homeDirectory);
×
NEW
789
        const fs::path defaultWineRegistry = homePath / ".wine" / "system.reg";
×
NEW
790
        std::error_code fileError;
×
NEW
791
        if (fs::exists(defaultWineRegistry, fileError))
×
792
        {
NEW
793
                registryFiles.insert(defaultWineRegistry);
×
794
        }
795

NEW
796
        for (const auto& steamRoot: {homePath / ".steam" / "steam", homePath / ".local" / "share" / "Steam"})
×
797
        {
NEW
798
                const fs::path compatdataPath = steamRoot / "steamapps" / "compatdata";
×
NEW
799
                fileError.clear();
×
NEW
800
                if (!fs::exists(compatdataPath, fileError) || !fs::is_directory(compatdataPath, fileError))
×
801
                {
NEW
802
                        continue;
×
803
                }
804

NEW
805
                std::error_code iteratorError;
×
NEW
806
                for (fs::directory_iterator iterator(compatdataPath, fs::directory_options::skip_permission_denied, iteratorError), end;
×
NEW
807
                          iterator != end;
×
NEW
808
                          iterator.increment(iteratorError))
×
809
                {
NEW
810
                        if (iteratorError)
×
811
                        {
NEW
812
                                iteratorError.clear();
×
NEW
813
                                continue;
×
814
                        }
815

NEW
816
                        fileError.clear();
×
NEW
817
                        if (!iterator->is_directory(fileError))
×
818
                        {
NEW
819
                                continue;
×
820
                        }
821

NEW
822
                        const fs::path protonRegistryFile = iterator->path() / "pfx" / "system.reg";
×
NEW
823
                        fileError.clear();
×
NEW
824
                        if (fs::exists(protonRegistryFile, fileError))
×
825
                        {
NEW
826
                                registryFiles.insert(protonRegistryFile);
×
827
                        }
NEW
828
                }
×
NEW
829
        }
×
830

NEW
831
        return registryFiles;
×
NEW
832
}
×
833

834
} // namespace
835

836
std::optional<std::filesystem::path> getGOGInstallPath(const std::string& gogID)
1 ✔
837
{
838
        if (gogID.empty())
1 ✔
839
        {
840
                return std::nullopt;
1 ✔
841
        }
842

NEW
843
        for (const auto& registryFile: GetWineRegistryFiles())
×
844
        {
NEW
845
                if (const auto installPath = GetGOGInstallPathInRegistryFile(registryFile, gogID); installPath)
×
846
                {
NEW
847
                        return installPath;
×
NEW
848
                }
×
NEW
849
        }
×
850

NEW
UNCOV
851
        return std::nullopt;
×
852
}
853

854
} // namespace commonItems
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc