aboutsummaryrefslogtreecommitdiff
path: root/core/src/Utils/StandardDirectories.cpp
blob: 2202f51af618478b714f87c11fd14e270261d993 (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
#include "StandardDirectories.hpp"

#include <filesystem>
#include <stdexcept>

namespace fs = std::filesystem;

#if defined(_WIN32)
// https://stackoverflow.com/questions/54499256/how-to-find-the-saved-games-folder-programmatically-in-c-c
#	include <ShlObj_core.h>
#	include <objbase.h>
#	pragma comment(lib, "shell32.lib")
#	pragma comment(lib, "ole32.lib")

static fs::path GetAppDataRoaming()
{
	PWSTR path = nullptr;
	HRESULT hr = SHGetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_CREATE, nullptr, &path);
	if (SUCCEEDED(hr)) {
		auto dataDir = fs::path(path);
		CoTaskMemFree(path);

		fs::create_directories(dataDir);
		return dataDir;
	} else {
		fs::path dataDir("~/AppData/Roaming");
		fs::create_directories(dataDir);
		return dataDir;
	}
}

#elif defined(__APPLE__)
// TODO
#elif defined(__linux__)
#	include <cstdlib>

static fs::path GetEnvVar(const char* name, const char* backup)
{
	if (const char* path = std::getenv(name)) {
		fs::path dataDir(path);
		fs::create_directories(dataDir);
		return dataDir;
	} else {
		fs::path dataDir(backup);
		fs::create_directories(dataDir);
		return dataDir;
	}
}

#endif

const std::filesystem::path& StandardDirectories::UserData()
{
	static auto userDataDir = []() -> fs::path {
#if defined(_WIN32)
		return GetAppDataRoaming();
#elif defined(__APPLE__)
	// TODO where?
#elif defined(__linux__)
		return GetEnvVar("XDG_DATA_HOME", "~/.local/share");
#endif
	}();
	return userDataDir;
}

const std::filesystem::path& StandardDirectories::UserConfig()
{
	static auto userConfigDir = []() -> fs::path {
#if defined(_WIN32)
		return GetAppDataRoaming();
#elif defined(__APPLE__)
	// TODO where?
#elif defined(__linux__)
		return GetEnvVar("XDG_CONFIG_HOME", "~/.config");
#endif
	}();
	return userConfigDir;
}