-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
214 lines (182 loc) · 7.24 KB
/
utils.cpp
File metadata and controls
214 lines (182 loc) · 7.24 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
//
// utils.cpp
// Entry point for Vortex utility program
//
// Copyright (c) 2026 Infinite
//
// This work is licensed under the terms of the Apache-2.0 license.
// For a copy, see <https://github.com/infiniteHQ/Vortex/blob/main/LICENSE>.
//
#define SDL_MAIN_HANDLED
#include <fstream>
#include <iostream>
#include <string>
#include <thread>
#include "lib/json/single_include/nlohmann/json.hpp"
std::string get_home_directory() {
#if defined(_WIN32) || defined(_WIN64)
const char *homePath = std::getenv("USERPROFILE");
if (homePath == nullptr) {
const char *homeDrive = std::getenv("HOMEDRIVE");
const char *homePathEnv = std::getenv("HOMEPATH");
if (homeDrive == nullptr || homePathEnv == nullptr) {
throw std::runtime_error("HOMEPATH environment variables not set");
}
return std::string(homeDrive) + std::string(homePathEnv);
}
return std::string(homePath);
#else
const char *homePath = std::getenv("HOME");
if (homePath == nullptr) {
throw std::runtime_error("HOME environment variable not set");
}
return std::string(homePath);
#endif
}
void remove_session_from_json(const std::string &session_id) {
// Set path depending on platform
std::string json_path;
#if defined(_WIN32) || defined(_WIN64)
json_path = get_home_directory() + "\\.vx\\sessions\\active_sessions.json";
#else
json_path = get_home_directory() + "/.vx/sessions/active_sessions.json";
#endif
std::ifstream file_in(json_path);
nlohmann::json active_sessions;
// Load existing active sessions from the JSON file
if (file_in.is_open()) {
file_in >> active_sessions;
file_in.close();
}
// Find and remove the session with the given session_id
for (auto it = active_sessions["sessions"].begin(); it != active_sessions["sessions"].end(); ++it) {
if ((*it)["session_id"] == session_id) {
active_sessions["sessions"].erase(it);
break; // Exit loop after removing the session
}
}
// Save the updated sessions back to the JSON file
std::ofstream file_out(json_path);
file_out << active_sessions.dump(4); // Pretty print with indentation
}
void add_session_to_json(
const std::string &session_id,
const std::string &version,
const std::string &user,
const std::string &project_path) {
#if defined(_WIN32) || defined(_WIN64)
std::string json_path = get_home_directory() + "\\.vx\\sessions\\active_sessions.json";
#else
std::string json_path = get_home_directory() + "/.vx/sessions/active_sessions.json";
#endif
std::ifstream file_in(json_path);
nlohmann::json active_sessions;
if (file_in.is_open()) {
file_in >> active_sessions;
file_in.close();
}
nlohmann::json new_session = { { "session_path", project_path },
{ "session_id", session_id },
{ "session_start_at", std::time(nullptr) },
{ "session_owner", user },
{ "vortex_version", version },
{ "vortex_exec_path", "/opt/Vortex/" + version + "/bin/vortex" } };
active_sessions["sessions"].push_back(new_session);
std::ofstream file_out(json_path);
file_out << active_sessions.dump(4);
}
// Called by handle_crash executions scripts to write the finality of the session
void write_session_end_state(const std::string &session_id, const std::string &state) {
if (session_id.empty()) {
return;
}
// Set the session directory path depending on the platform
#if defined(_WIN32) || defined(_WIN64)
std::string session_dir = get_home_directory() + "\\.vx\\sessions\\" + session_id;
#else
std::string session_dir = get_home_directory() + "/.vx/sessions/" + session_id;
#endif
std::string session_file_path = session_dir + "/session.json";
// Get current time in ISO 8601 format
auto now = std::chrono::system_clock::now();
std::time_t now_time = std::chrono::system_clock::to_time_t(now);
std::tm tm = *std::localtime(&now_time);
char buffer[32];
std::strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%S", &tm);
nlohmann::json session_data = { { "SessionID", session_id },
{ "SessionEndedAt", buffer },
{ "SessionEndedWithState", state } };
// Create the session directory if it does not exist
std::filesystem::create_directories(session_dir);
// Write session data to the JSON file
std::ofstream session_file(session_file_path);
if (session_file.is_open()) {
session_file << session_data.dump(4); // Pretty print with indentation
session_file.close();
} else {
std::cerr << "Error: Could not open session file for writing." << std::endl;
}
// TODO: if the session is in active_session, remove it
}
int main(int argc, char *argv[]) {
if (argc < 2) {
std::cout << "Usage : " << std::endl;
} else {
if (std::string(argv[1]) == "-h" || std::string(argv[1]) == "--help") {
std::cout << "Usage : " << std::endl;
} else if (std::string(argv[1]) == "-test") {
std::cout << "ok" << std::endl;
return 0;
} else if (std::string(argv[1]) == "-rms" || std::string(argv[1]) == "--remove_session") {
std::string session_id;
if (argc > 2) {
std::string arg2 = argv[2];
if (arg2.rfind("--session_id=\"", 0) == 0 && arg2.back() == '\"')
session_id = arg2.substr(13, arg2.length() - 14);
else if (arg2.rfind("--session_id=", 0) == 0)
session_id = arg2.substr(13);
}
remove_session_from_json(session_id);
} else if (std::string(argv[1]) == "-wses" || std::string(argv[1]) == "--write_session_end_state") {
std::string session_id;
std::string session_state;
auto parse_arg = [](const std::string &arg, const std::string &prefix) -> std::string {
std::string with_quotes = prefix + "\"";
if (arg.rfind(with_quotes, 0) == 0 && arg.back() == '\"')
return arg.substr(with_quotes.size(), arg.length() - with_quotes.size() - 1);
if (arg.rfind(prefix, 0) == 0)
return arg.substr(prefix.size());
return "";
};
if (argc > 2)
session_id = parse_arg(argv[2], "--session_id=");
if (argc > 3)
session_state = parse_arg(argv[3], "--state=");
write_session_end_state(session_id, session_state);
remove_session_from_json(session_id);
} else if (std::string(argv[1]) == "-astj" || std::string(argv[1]) == "--add_session_to_json") {
std::string session_id;
std::string version;
std::string user;
std::string project_path;
auto parse_arg = [](const std::string &arg, const std::string &prefix) -> std::string {
std::string with_quotes = prefix + "\"";
if (arg.rfind(with_quotes, 0) == 0 && arg.back() == '\"')
return arg.substr(with_quotes.size(), arg.length() - with_quotes.size() - 1);
if (arg.rfind(prefix, 0) == 0)
return arg.substr(prefix.size());
return "";
};
if (argc > 2)
session_id = parse_arg(argv[2], "--session_id=");
if (argc > 3)
version = parse_arg(argv[3], "--version=");
if (argc > 4)
user = parse_arg(argv[4], "--user=");
if (argc > 5)
project_path = parse_arg(argv[5], "--project_path=");
add_session_to_json(session_id, version, user, project_path);
}
}
return 0;
}