This repository was archived by the owner on Nov 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOsuManager.cpp
More file actions
130 lines (114 loc) · 4.03 KB
/
OsuManager.cpp
File metadata and controls
130 lines (114 loc) · 4.03 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
#include <cstring>
#include <iostream>
#include "OsuManager.hpp"
#include "FileManager.hpp"
#include "WindowsManager.hpp"
#define IS_LINE_END(c) ((c) == '\r' || (c) == '\n')
namespace Osu {
static char osu_path[BUF_SIZE] = {0};
static char osu_replay_path[BUF_SIZE] = {0};
static char osu_songs_path[BUF_SIZE] = {0};
static void update_osu_path(const char *base_dir) {
strcpy(osu_path, base_dir);
strcpy(osu_replay_path, base_dir);
strcat(osu_replay_path, "\\Replays");
strcpy(osu_songs_path, base_dir);
strcat(osu_songs_path, "\\Songs");
}
static char *read_line(char *start, char *buf) {
while (!IS_LINE_END(*start)) {
if (buf) {
*buf = *start;
buf++;
}
start++;
}
if (buf) *buf = '\0';
return start + 1;
}
char *get_osu_base_path() {
if (*osu_path != '\0') {
return osu_path;
}
char buf[BUF_SIZE] = {0};
char path_buf[BUF_SIZE] = {0};
WindowsManager::execute_cmd("powershell \"get-process osu! | select-object path\"", buf);
/**
* If process osu! exists, it will print "\nPath\n----[path]"
* Otherwise, it will print "get-process :..."
*/
if (IS_LINE_END(buf[0])) {
char *pbuf = buf;
pbuf = read_line(pbuf, nullptr);
pbuf = read_line(pbuf, nullptr);
pbuf = read_line(pbuf, nullptr);
read_line(pbuf, path_buf);
FileManager::to_parent(path_buf);
update_osu_path(path_buf);
return osu_path;
} else {
return nullptr;
}
}
char *get_osu_replay_path() {
get_osu_base_path(); // ensure updated
return osu_replay_path;
}
char *get_osu_songs_path() {
get_osu_base_path(); // ensure updated
return osu_songs_path;
}
BeatmapHeader::BeatmapHeader(const std::string &replay_name) {
// replay format: player - artists - name - [difficulty] (date) OsuMania-n.osr
char base_name[1024];
FileManager::get_name(replay_name.c_str(), base_name);
int pos1 = replay_name.find(" - ");
int pos2 = replay_name.find(" - ", pos1 + 1);
int pos3 = replay_name.find(" [", pos2 + 1);
int pos4 = replay_name.rfind("] ");
artists = replay_name.substr(pos1 + 3, pos2 - pos1 - 3);
name = replay_name.substr(pos2 + 3, pos3 - pos2 - 3);
difficulty = replay_name.substr(pos3 + 2, pos4 - pos3 - 2);
}
static void replace_all(
std::string &str,
const std::string &old_value,
const std::string &new_value
) {
std::string::size_type pos(0);
while (true) {
if ((pos = str.find(old_value, pos)) != std::string::npos) {
str = str.replace(pos, old_value.length(), new_value);
pos = pos + new_value.size();
}
else break;
}
}
static void escape(std::string &str) {
std::string escapes[] = {"\\", "$", "(", ")", "*", "+", ".", "[", "]", "?", "^", "{", "}",
"|"};
for (const auto& s : escapes) {
replace_all(str, s, "\\" + s);
}
replace_all(str, " ", "\\s");
}
std::regex BeatmapHeader::to_beatmap_regex() {
// beatmap file format: artists - name (mapper) [difficulty].osu
std::stringstream pattern_stream;
escape(name);
escape(artists);
escape(difficulty);
pattern_stream << "^" << artists
<< "\\s-\\s"
<< name
<< "\\s"
<< "\\(.*\\)" // mapper
<< "\\s"
<< "\\["
<< difficulty
<< "\\]\\.osu$";
auto pattern = pattern_stream.str();
// std::cout << "Generate beatmap name regex pattern: " << pattern << std::endl;
return std::regex(pattern);
}
}