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 pathmain.cpp
More file actions
167 lines (147 loc) · 5.46 KB
/
main.cpp
File metadata and controls
167 lines (147 loc) · 5.46 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
#include <windows.h>
#include <iostream>
#include <set>
#include <pthread.h>
#include <fstream>
#include <tchar.h>
#include "OsuManager.hpp"
#include "FileManager.hpp"
#include "ConfigManager.hpp"
#include "WindowsManager.hpp"
using namespace std;
const char *RENDER_NAME = "ManiaReplayMaster.jar";
const char *RENDER_PATH = "library\\ManiaReplayMaster.jar";
void get_replays(set<string> &set) {
FileManager::walk_dir(Osu::get_osu_replay_path(),
[&](const char *full_name, const char *file_name) {
set.emplace(file_name);
return true;
});
}
bool find_replay(set<string> &old_replays, char *replay_name) {
FileManager::walk_dir(Osu::get_osu_replay_path(),
[&](const char *full_name, const char *file_name) -> bool {
string current_file(file_name);
bool in = false;
for (auto &old_file : old_replays) {
if (old_file == current_file) {
in = true;
break;
}
}
if (!in) {
strcpy(replay_name, full_name);
return false;
}
return true;
});
return *replay_name != '\0';
}
bool find_beatmap(const std::regex ®ex, char *beatmap_name) {
FileManager::walk_dir(Osu::get_osu_songs_path(),
[&](const char *full_name, const char *file_name) -> bool {
std::smatch match;
std::string file(file_name);
if (std::regex_match(file, match, regex)) {
strcpy(beatmap_name, full_name);
return false;
}
return true;
});
return *beatmap_name != '\0';
}
void *process(void *) {
// 0. check if osu exists
if (!Osu::get_osu_base_path()) {
cerr << "Cannot find osu process. "
<< "Please open Osu!Mania rating interface before using this extension."
<< endl;
return nullptr;
}
// 1. record replay files
set<string> old_replays;
get_replays(old_replays);
// 2. send F2 key to export replay.osr
Sleep(500); // wait user for releasing shortcut key
WindowsManager::send_key(VK_F2);
// 3. find if there is a new replay fetched (timeout: 20 seconds)
char replay_file[1024] = {0};
for (int i = 0; i < 20; i++) {
Sleep(1000);
if (find_replay(old_replays, replay_file)) {
break;
}
}
if (!*replay_file) {
cerr << "Timeout: cannot find replay file!" << endl;
return nullptr;
}
cout << "Find replay: " << replay_file << endl;
// 4. search for beatmap according to replay file name
Osu::BeatmapHeader beatmap(replay_file);
auto regex = beatmap.to_beatmap_regex();
char beatmap_file[1024] = {0};
if (find_beatmap(regex, beatmap_file)) {
cout << "Find beatmap: " << beatmap_file << endl;
} else {
cerr << "Cannot find beatmap file! "
<< "Please use ManiaReplayMaster.bat to input file names manually."
<< endl;
return nullptr;
}
// 5. invoke render!
char bat_name[1024] = ".\\batch\\";
FileManager::create_folder("batch");
FileManager::get_name(beatmap_file, bat_name + strlen(bat_name));
strcpy(bat_name + strlen(bat_name) - 3, "bat");
stringstream command;
command << "@echo off\ncd .\\library\njava -jar " << RENDER_NAME
<< " -speed=" << ConfigManager::get(KEY_SPEED)
<< " \"" << beatmap_file << "\""
<< " \"" << replay_file << "\""
<< "\npause";
ofstream batch(bat_name);
batch << command.str();
batch.close();
WindowsManager::execute_cmd_blocking(bat_name);
remove(bat_name);
return nullptr;
}
void pause_exit(int code) {
system("pause");
exit(code);
}
int main(int argc, char *argv[]) {
// check java
char buf[BUF_SIZE];
if (WindowsManager::execute_cmd("java -version 2>&1", buf) != 0) {
cerr << "Cannot find Java. Please install Java 8 or latter version." << endl;
pause_exit(-1);
}
// check render
if (!FileManager::exist(RENDER_PATH)) {
cerr << "Cannot find " << RENDER_PATH << ". Please copy this extension outside library dir." << endl;
pause_exit(-2);
}
// check osu
auto base_path = Osu::get_osu_base_path();
if (base_path) {
cout << "Find osu base path: " << base_path << endl;
}
ConfigManager::initialize_config();
cout << "Use speed = " << ConfigManager::get(KEY_SPEED) << ". You can change this speed in "
<< CONFIG_FILE_NAME << "." << endl;
if (RegisterHotKey(nullptr, 1, 0, VK_F1)) {
cout << "Press F1 in Osu!Mania rating interface." << endl;
} else {
cerr << "Error in register hot key!" << endl;
pause_exit(-3);
}
MSG msg = {nullptr};
while (GetMessage(&msg, nullptr, 0, 0)) {
if (msg.message == WM_HOTKEY) {
pthread_t pthread;
pthread_create(&pthread, nullptr, process, nullptr);
}
}
}