-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathGlobalAutoPaths.cpp
More file actions
246 lines (213 loc) · 7.4 KB
/
Copy pathGlobalAutoPaths.cpp
File metadata and controls
246 lines (213 loc) · 7.4 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/* Globals Auto-Paths
*
* From: https://github.com/PokemonAutomation/
*
*/
#include <QCoreApplication>
#include <QString>
#include <QFile>
#include <QStandardPaths>
#include <QFile>
#include <QFileInfo>
#include <QDir>
#include "Common/Cpp/Filesystem/Filesystem.h"
#include "GlobalAutoPaths.h"
//#include <iostream>
//using std::cout;
//using std::endl;
namespace PokemonAutomation{
#if defined(__APPLE__)
static std::string g_startup_profile;
void set_startup_profile(int& argc, char* argv[]){
for (int i = 1; i + 1 < argc; i++){
if (strcmp(argv[i], "--profile") == 0){
std::string profile = argv[i + 1];
for (char c : profile){
if (!(std::isalpha(c) || std::isdigit(c)) && c != u'_' && c != u'-') c = u'_';
}
g_startup_profile = std::move(profile);
// Shift everything after --profile <name> down by 2.
for (int j = i; j + 2 < argc; j++){
argv[j] = argv[j + 2];
}
argc -= 2;
return;
}
}
}
const std::string& STARTUP_PROFILE(){
return g_startup_profile;
}
#endif
namespace{
Filesystem::Path get_application_base_dir_path(){
#if defined(__linux__)
// Check for AppImage environment variables to find the root directory, if running as an AppImage.
// PA_APPIMAGE_DIR is set by Azure via a patched AppRun script.
QByteArray dir = qgetenv("PA_APPIMAGE_DIR");
if (!dir.isEmpty()){
return QString::fromUtf8(dir).toStdString();
}
QByteArray path = qgetenv("APPIMAGE");
if (!path.isEmpty()){
return QDir::cleanPath(QFileInfo(QString::fromUtf8(path)).dir().absolutePath()).toStdString();
}
QByteArray appDirBytes = qgetenv("APPDIR");
if (!appDirBytes.isEmpty()){
QString appDir = QString::fromUtf8(appDirBytes);
QFile mountinfo(QStringLiteral("/proc/self/mountinfo"));
if (mountinfo.open(QIODevice::ReadOnly | QIODevice::Text)){
while (!mountinfo.atEnd()){
QString line = QString::fromUtf8(mountinfo.readLine()).trimmed();
int dashSep = line.indexOf(QStringLiteral(" - "));
if (dashSep < 0){
continue;
}
QStringList pre = line.left(dashSep).split(u' ', Qt::SkipEmptyParts);
QStringList post = line.mid(dashSep + 3).split(u' ', Qt::SkipEmptyParts);
if (pre.size() < 5 || post.size() < 2){
continue;
}
QString mountPoint = pre[4].replace(QStringLiteral("\\040"), QStringLiteral(" "));
QString source = post[1].replace(QStringLiteral("\\040"), QStringLiteral(" "));
if (mountPoint == appDir && source.endsWith(QStringLiteral(".AppImage"))){
return QDir::cleanPath(QFileInfo(source).dir().absolutePath()).toStdString();
}
}
}
}
#endif
return Filesystem::application_working_path();
}
std::string get_resource_path(){
// Find the resource directory.
Filesystem::Path base = get_application_base_dir_path();
Filesystem::Path path = base;
for (size_t c = 0; c < 5; c++){
Filesystem::Path try_path = path / "Resources/";
if (exists(try_path)){
return try_path.string_slash_normalized();
}
path = path / "..";
}
return (base / "Resources/").string_slash_normalized();
}
std::string get_unittest_resource_path(){
// Find the unit test resource directory.
// Try the intended folder name first.
Filesystem::Path base = get_application_base_dir_path();
Filesystem::Path path = base;
for (size_t c = 0; c < 5; c++){
Filesystem::Path try_path = path / "UnitTestResources/";
if (exists(try_path)){
return try_path.string_slash_normalized();
}
path = path / "..";
}
// Now try with the old command-line folder.
path = base;
for (size_t c = 0; c < 5; c++){
Filesystem::Path try_path = path / "CommandLineTests/";
if (exists(try_path)){
return try_path.string_slash_normalized();
}
path = path / "..";
}
return (base / "UnitTestResources/").string_slash_normalized();
}
std::string get_training_path(){
// Find the training data directory.
Filesystem::Path base = get_application_base_dir_path();
Filesystem::Path path = base;
for (size_t c = 0; c < 5; c++){
Filesystem::Path try_path = path / "TrainingData/";
if (exists(try_path)){
return try_path.string_slash_normalized();
}
path = path / "..";
}
return (base / "TrainingData/").string_slash_normalized();
}
std::string get_runtime_base_path(){
#if defined(__APPLE__)
// QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) returns
// "/Users/$USERNAME/Library/Application Support/SerialPrograms"
QString appSupportPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
if (!g_startup_profile.empty()){
appSupportPath += "/Profiles/" + QString::fromStdString(g_startup_profile);
}
QDir().mkpath(appSupportPath);
return appSupportPath.toStdString() + "/";
#else
return "./";
#endif
}
std::string get_setting_path(){
return RUNTIME_BASE_PATH() + "UserSettings/";
}
std::string get_screenshot_path(){
return RUNTIME_BASE_PATH() + "Screenshots/";
}
std::string get_debug_path(){
return RUNTIME_BASE_PATH() + "DebugDumps/";
}
std::string get_error_path(){
return RUNTIME_BASE_PATH() + "ErrorDumps/";
}
std::string get_user_file_path(){
return RUNTIME_BASE_PATH();
}
} // anonymous namespace
const std::string& RUNTIME_BASE_PATH(){
static std::string path = get_runtime_base_path();
return path;
}
const std::string& SETTINGS_PATH(){
static std::string path = get_setting_path();
return path;
}
const std::string& PROGRAM_SETTING_JSON_PATH(){
static std::string path = SETTINGS_PATH() + QCoreApplication::applicationName().toStdString() + "-Settings.json";
return path;
}
const std::string& SCREENSHOTS_PATH(){
static std::string path = get_screenshot_path();
return path;
}
const std::string& DEBUG_PATH(){
static std::string path = get_debug_path();
return path;
}
const std::string& ERROR_PATH(){
static std::string path = get_error_path();
return path;
}
const std::string& USER_FILE_PATH(){
static std::string path = get_user_file_path();
return path;
}
const std::string& RESOURCE_PATH(){
static std::string path = get_resource_path();
return path;
}
const std::string& DOWNLOADED_RESOURCE_PATH(){
static std::string path = RUNTIME_BASE_PATH() + "DownloadedResources/";
return path;
}
const std::string& UNIT_TEST_RESOURCE_PATH(){
static std::string path = get_unittest_resource_path();
return path;
}
const std::string& TRAINING_PATH(){
static std::string path = get_training_path();
return path;
}
const std::string& ML_ANNOTATION_PATH(){
static const std::string path = RUNTIME_BASE_PATH() + "DataAnnotation/";
return path;
}
const std::string& ML_MODEL_CACHE_PATH(){
static const std::string path = RUNTIME_BASE_PATH() + "ModelCache/";
return path;
}
}