game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
paths_windows.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2#include "platform/paths.h"
3#include "platform/platform.h"
4
5#ifdef PLATFORM_WINDOWS
6
7#include <shlobj.h>
8#include <userenv.h>
9#include <windows.h>
10
11#pragma comment(lib, "userenv.lib")
12
13namespace platform
14{
15
16class WindowsPaths : public Paths
17{
18 public:
19 std::filesystem::path getDataDir() const override
20 {
21 wchar_t path[MAX_PATH];
22 if (SUCCEEDED(SHGetFolderPathW(nullptr, CSIDL_APPDATA, nullptr, 0, path)))
23 {
24 return std::filesystem::path(path) / "GameLauncher";
25 }
26 return getHomeDir() / "AppData" / "Roaming" / "GameLauncher";
27 }
28
29 std::filesystem::path getCacheDir() const override
30 {
31 wchar_t path[MAX_PATH];
32 if (SUCCEEDED(SHGetFolderPathW(nullptr, CSIDL_LOCAL_APPDATA, nullptr, 0, path)))
33 {
34 return std::filesystem::path(path) / "GameLauncher" / "cache";
35 }
36 return getHomeDir() / "AppData" / "Local" / "GameLauncher" / "cache";
37 }
38
39 std::filesystem::path getLogDir() const override
40 {
41 return getDataDir() / "logs";
42 }
43
44 std::filesystem::path getHomeDir() const override
45 {
46 // Try USERPROFILE first
47 wchar_t path[MAX_PATH];
48 DWORD size = MAX_PATH;
49 if (GetEnvironmentVariableW(L"USERPROFILE", path, size) > 0)
50 {
51 return std::filesystem::path(path);
52 }
53
54 // Try SHGetFolderPath
55 if (SUCCEEDED(SHGetFolderPathW(nullptr, CSIDL_PROFILE, nullptr, 0, path)))
56 {
57 return std::filesystem::path(path);
58 }
59
60 // Fallback to HOMEDRIVE + HOMEPATH
61 wchar_t drive[MAX_PATH], homePath[MAX_PATH];
62 if (GetEnvironmentVariableW(L"HOMEDRIVE", drive, MAX_PATH) > 0 &&
63 GetEnvironmentVariableW(L"HOMEPATH", homePath, MAX_PATH) > 0)
64 {
65 return std::filesystem::path(drive) / homePath;
66 }
67
68 // Last resort
69 return "C:\\Users\\Default";
70 }
71
72 std::filesystem::path getDocumentsDir() const override
73 {
74 wchar_t path[MAX_PATH];
75 if (SUCCEEDED(SHGetFolderPathW(nullptr, CSIDL_PERSONAL, nullptr, 0, path)))
76 {
77 return std::filesystem::path(path);
78 }
79 return getHomeDir() / "Documents";
80 }
81
82 std::filesystem::path expandPath(const std::string& path) const override
83 {
84 if (path.empty())
85 {
86 return std::filesystem::path();
87 }
88
89 // Convert to wide string
90 int wideLen = MultiByteToWideChar(CP_UTF8, 0, path.c_str(), -1, nullptr, 0);
91 std::wstring widePath(wideLen, L'\0');
92 MultiByteToWideChar(CP_UTF8, 0, path.c_str(), -1, widePath.data(), wideLen);
93
94 // Expand environment variables
95 wchar_t expanded[MAX_PATH * 2];
96 DWORD result = ExpandEnvironmentStringsW(widePath.c_str(), expanded, MAX_PATH * 2);
97
98 if (result > 0 && result < MAX_PATH * 2)
99 {
100 return std::filesystem::path(expanded);
101 }
102
103 return std::filesystem::path(path);
104 }
105};
106
107// Factory implementation
108std::unique_ptr<Paths> Paths::create()
109{
110 return std::make_unique<WindowsPaths>();
111}
112
113} // namespace platform
114
115#endif // PLATFORM_WINDOWS
static std::unique_ptr< Paths > create()