forked from winnerspiros/UnleashedRecomp_Android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaths.cpp
More file actions
87 lines (76 loc) · 2.37 KB
/
Copy pathpaths.cpp
File metadata and controls
87 lines (76 loc) · 2.37 KB
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
#include "paths.h"
#include <os/process.h>
#ifdef ANDROID
#include <SDL.h>
#endif
std::filesystem::path g_executableRoot = os::process::GetExecutableRoot();
std::filesystem::path g_userPath = BuildUserPath();
bool CheckPortable()
{
return std::filesystem::exists(g_executableRoot / "portable.txt");
}
std::filesystem::path BuildUserPath()
{
if (CheckPortable())
return g_executableRoot;
std::filesystem::path userPath;
#if defined(_WIN32)
PWSTR knownPath = NULL;
if (SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, NULL, &knownPath) == S_OK)
userPath = std::filesystem::path{ knownPath } / USER_DIRECTORY;
CoTaskMemFree(knownPath);
#elif defined(ANDROID)
// Prefer external storage so users can access game files via file manager.
// External path: /storage/emulated/0/Android/data/<package>/files/UnleashedRecomp/
const char* extPath = SDL_AndroidGetExternalStoragePath();
if (extPath)
{
userPath = std::filesystem::path(extPath) / USER_DIRECTORY;
}
else
{
// Fall back to internal storage if external is unavailable
char* prefPath = SDL_GetPrefPath("hedge-dev", "UnleashedRecomp");
if (prefPath)
{
userPath = std::filesystem::path(prefPath);
SDL_free(prefPath);
}
}
// Ensure the directory exists
if (!userPath.empty())
{
std::error_code ec;
std::filesystem::create_directories(userPath, ec);
}
#elif defined(__linux__) || defined(__APPLE__)
const char* homeDir = getenv("HOME");
#if defined(__linux__)
if (homeDir == nullptr)
{
homeDir = getpwuid(getuid())->pw_dir;
}
#endif
if (homeDir != nullptr)
{
// Prefer to store in the .config directory if it exists. Use the home directory otherwise.
std::filesystem::path homePath = homeDir;
#if defined(__linux__)
std::filesystem::path configPath = homePath / ".config";
#else
std::filesystem::path configPath = homePath / "Library" / "Application Support";
#endif
if (std::filesystem::exists(configPath))
userPath = configPath / USER_DIRECTORY;
else
userPath = homePath / ("." USER_DIRECTORY);
}
#else
static_assert(false, "GetUserPath() not implemented for this platform.");
#endif
return userPath;
}
const std::filesystem::path& GetUserPath()
{
return g_userPath;
}