RunicVTT Open Source Virtual Tabletop for TTRPG using P2P
Loading...
Searching...
No Matches
PathManager.h
Go to the documentation of this file.
1#pragma once
2#include <filesystem>
3#include <string>
4#include <iostream>
5#include <cstdlib>
6#include <cctype>
7
8//#include <shlobj.h> // for SHGetKnownFolderPath
9//#include <ShlObj_core.h> // or <ShlObj.h>
10//#include <KnownFolders.h>
11
12namespace fs = std::filesystem;
13
15{
16private:
17 static fs::path getDocumentsFolder()
18 {
19#ifdef _WIN32
20 //PWSTR path_w = nullptr;
21 //HRESULT result = SHGetKnownFolderPath(FOLDERID_Documents, 0, NULL, &path_w);
22 //if (SUCCEEDED(result)) {
23 // std::wstring wpath(path_w);
24 // CoTaskMemFree(path_w); // free COM-allocated string
25 // return std::filesystem::path(wpath);
26 //} else {
27 // throw std::runtime_error("Failed to get Documents folder");
28 //}
29 //
30 const char* userProfile = std::getenv("USERPROFILE");
31 return fs::path(userProfile ? userProfile : "C:\\Users\\Default") / "Documents";
32#elif __APPLE__
33 const char* home = std::getenv("HOME");
34 return fs::path(home ? home : "/Users/Default") / "Documents";
35#else // Linux
36 const char* home = std::getenv("HOME");
37 return fs::path(home ? home : "/home/default") / "Documents";
38#endif
39 }
40
41 static void createIfNotExists(const fs::path& path)
42 {
43 if (!fs::exists(path))
44 {
45 fs::create_directories(path);
46 std::cout << "[PathManager] Created folder: " << path << std::endl;
47 }
48 }
49
50 inline static fs::path executableDir = fs::current_path();
51 inline static fs::path baseDocumentsPath = PathManager::getDocumentsFolder() / "RunicVTT";
52
53public:
54 // --- USER-ACCESSIBLE FOLDERS ---
55 static fs::path getRootDirectory()
56 {
57 return getExecutableRoot();
58 //return baseDocumentsPath;
59 }
60
61 static fs::path getMapsPath()
62 {
63 return getExecutableRoot() / "Maps";
64 }
65
66 static fs::path getMarkersPath()
67 {
68 return getExecutableRoot() / "Markers";
69 }
70
71 static fs::path getNotesPath()
72 {
73 return getExecutableRoot() / "Notes";
74 }
75
76 static fs::path getGameTablesPath()
77 {
78 return getExecutableRoot() / "GameTables";
79 }
80
81 static fs::path getBoardsPath(std::string gametable_name)
82 {
83 return getGameTablesPath() / gametable_name / "Boards";
84 }
85
86 static fs::path getConfigPath()
87 {
88 return getExecutableRoot() / "Config";
89 }
90
91 // --- APP/INSTALLATION-LOCAL FOLDERS ---
92 //C:\Dev\RunicVTT\external\node\node.exe .\node_modules\localtunnel\bin\client -p 7778 -s runics
93 static fs::path getExternalPath()
94 {
95 return getExecutableRoot() / "external";
96 }
97 static fs::path getNodeExePath()
98 {
99 return getExternalPath() / "node" / "node.exe";
100 }
101 static fs::path getLocalTunnelClientPath()
102 {
103 return getExternalPath() / "localtunnel" / "node_modules" / "localtunnel" / "bin" / "client";
104 }
105
107 {
108 return getExternalPath() / "lt-controller.cjs";
109 }
110 static fs::path getUpnpcExePath()
111 {
112 return executableDir / "upnpc-static.exe";
113 }
114 static fs::path getExecutableDirectory()
115 {
116 return executableDir;
117 }
118
119 static fs::path getExecutableRoot()
120 {
121 return (executableDir.filename() == "bin") ? executableDir.parent_path() : executableDir;
122 }
123
124 static fs::path getResPath()
125 {
126 return getExecutableRoot() / "res";
127 }
128
129 static fs::path getCertsPath()
130 {
131 return getResPath() / "certs";
132 }
133
134 static fs::path getShaderPath()
135 {
136 return getResPath() / "shaders";
137 }
138
139 // --- FOLDER INITIALIZATION ---
149
150 //helpers
151 static bool hasDirSep(const std::string& s)
152 {
153 return s.find('\\') != std::string::npos || s.find('/') != std::string::npos;
154 }
155
156 static bool isUNC(const std::string& s)
157 {
158 // \\server\share or //server/share
159 return (s.rfind("\\\\", 0) == 0) || (s.rfind("//", 0) == 0);
160 }
161
162 static bool isDriveAbsolute(const std::string& s)
163 {
164 // C:\foo or C:/foo
165 return s.size() > 2 && std::isalpha(static_cast<unsigned char>(s[0])) && s[1] == ':' && (s[2] == '\\' || s[2] == '/');
166 }
167
168 static bool isDriveRelative(const std::string& s)
169 {
170 // C:foo (relative to current dir on drive C)
171 return s.size() > 1 && std::isalpha(static_cast<unsigned char>(s[0])) && s[1] == ':' && (s.size() == 2 || (s[2] != '\\' && s[2] != '/'));
172 }
173
174 static bool isAbsolutePath(const std::string& s)
175 {
176 // std::filesystem covers POSIX abspaths; supplement for Windows special cases
177 std::filesystem::path p(s);
178 if (p.is_absolute())
179 return true; // works for POSIX and many Windows cases
180 if (isUNC(s) || isDriveAbsolute(s))
181 return true;
182 return false;
183 }
184
185 static bool isPathLike(const std::string& s)
186 {
187 // Treat anything with directory separators OR absolute/UNC/drive-relative prefixes as a path
188 if (isAbsolutePath(s))
189 return true;
190 if (isDriveRelative(s))
191 return true; // still a path, just not absolute
192 if (hasDirSep(s))
193 return true; // e.g. "markers/goblin.png"
194 return false;
195 }
196
197 static bool isFilenameOnly(const std::string& s)
198 {
199 // No separators, no drive/UNC prefixes => just a filename, like "goblin.png"
200 return !isPathLike(s);
201 }
202};
static fs::path baseDocumentsPath
Definition PathManager.h:51
static fs::path getMarkersPath()
Definition PathManager.h:66
static fs::path getRootDirectory()
Definition PathManager.h:55
static bool isFilenameOnly(const std::string &s)
static fs::path getExecutableRoot()
static fs::path getConfigPath()
Definition PathManager.h:86
static fs::path getGameTablesPath()
Definition PathManager.h:76
static bool isPathLike(const std::string &s)
static fs::path getLocalTunnelControllerPath()
static fs::path getNodeExePath()
Definition PathManager.h:97
static void ensureDirectories()
static fs::path getExternalPath()
Definition PathManager.h:93
static bool hasDirSep(const std::string &s)
static fs::path getDocumentsFolder()
Definition PathManager.h:17
static fs::path getLocalTunnelClientPath()
static fs::path executableDir
Definition PathManager.h:50
static bool isUNC(const std::string &s)
static fs::path getResPath()
static bool isAbsolutePath(const std::string &s)
static fs::path getUpnpcExePath()
static bool isDriveRelative(const std::string &s)
static bool isDriveAbsolute(const std::string &s)
static void createIfNotExists(const fs::path &path)
Definition PathManager.h:41
static fs::path getExecutableDirectory()
static fs::path getMapsPath()
Definition PathManager.h:61
static fs::path getShaderPath()
static fs::path getBoardsPath(std::string gametable_name)
Definition PathManager.h:81
static fs::path getNotesPath()
Definition PathManager.h:71
static fs::path getCertsPath()