Skip to content

Commit 5f62440

Browse files
committed
Switch to JSON for config file
rel #88
1 parent 8f3a71e commit 5f62440

File tree

5 files changed

+72
-76
lines changed

5 files changed

+72
-76
lines changed

app/CMakeLists.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
include(FetchContent)
2+
3+
FetchContent_Declare(
4+
json
5+
GIT_REPOSITORY https://github.com/nlohmann/json
6+
GIT_TAG v3.11.2)
7+
8+
FetchContent_MakeAvailable(json)
9+
110
add_executable(maniac-app app.cpp window.cpp
211
dependencies/imgui/imgui.cpp
312
dependencies/imgui/backends/imgui_impl_dx9.cpp
@@ -16,6 +25,6 @@ endif()
1625
target_include_directories(maniac-app PRIVATE dependencies)
1726
target_include_directories(maniac-app PRIVATE dependencies/imgui)
1827

19-
target_link_libraries(maniac-app PRIVATE maniac d3d9.lib)
28+
target_link_libraries(maniac-app PRIVATE maniac d3d9.lib nlohmann_json::nlohmann_json)
2029

2130
set_target_properties(maniac-app PROPERTIES OUTPUT_NAME "maniac")

app/app.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "window.h"
2+
#include "config.h"
23
#include <maniac/maniac.h>
34

45
static void help_marker(const char *desc) {
@@ -33,7 +34,7 @@ static void set_priority_class(int priority) {
3334
int main(int, char **) {
3435
std::string message;
3536

36-
maniac::config.read_from_file();
37+
config::read_from_file(maniac::config);
3738

3839
auto run = [&message](osu::Osu &osu) {
3940
maniac::osu = &osu;
@@ -141,7 +142,7 @@ int main(int, char **) {
141142
ImGui::End();
142143
});
143144

144-
maniac::config.write_to_file();
145+
config::write_to_file(maniac::config);
145146

146147
thread.request_stop();
147148

app/config.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#pragma once
2+
3+
#include <maniac/maniac.h>
4+
#include <nlohmann/json.hpp>
5+
6+
namespace config {
7+
using json = nlohmann::json;
8+
9+
constexpr auto format_version = 1;
10+
constexpr auto file_name = "maniac-config.json";
11+
12+
void read_from_file(struct maniac::config &c);
13+
void write_to_file(struct maniac::config &c);
14+
}
15+
16+
void config::read_from_file(struct maniac::config &c) {
17+
std::ifstream file(file_name);
18+
19+
try {
20+
const auto data = json::parse(file);
21+
22+
c.tap_time = data["tap_time"];
23+
c.mirror_mod = data["mirror_mod"];
24+
c.compensation_offset = data["compensation_offset"];
25+
c.randomization_mean = data["randomization_mean"];
26+
c.randomization_stddev = data["randomization_stddev"];
27+
c.humanization_type = data["humanization_type"];
28+
c.humanization_modifier = data["humanization_modifier"];
29+
c.keys = data["keys"];
30+
31+
debug("loaded config from file");
32+
} catch (json::parse_error &err) {
33+
debug("failed parsing config: '%s'", err.what());
34+
}
35+
}
36+
37+
void config::write_to_file(struct maniac::config &c) {
38+
json data = {
39+
{"format_version", format_version},
40+
{"tap_time", c.tap_time},
41+
{"mirror_mod", c.mirror_mod},
42+
{"compensation_offset", c.compensation_offset},
43+
{"randomization_mean", c.randomization_mean},
44+
{"randomization_stddev", c.randomization_stddev},
45+
{"humanization_type", c.humanization_type},
46+
{"humanization_modifier", c.humanization_modifier},
47+
{"keys", c.keys}
48+
};
49+
50+
std::ofstream file(file_name);
51+
52+
file << data.dump(4) << std::endl;
53+
54+
debug("wrote config to file");
55+
}

lib/include/maniac/config.h

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -20,64 +20,7 @@ namespace maniac {
2020
int randomization_stddev = 0;
2121
int humanization_type = DYNAMIC_HUMANIZATION;
2222

23-
// TODO: This isn't configurable yet, use a non-shit config format
2423
std::string keys = "asdfjkl;";
25-
26-
// TODO: Would be good to have the read/write stuff be in a constructor/destructor
27-
28-
void read_from_file() {
29-
std::fstream file{"maniac-config", std::fstream::binary | std::fstream::in};
30-
31-
if (!file.is_open()) {
32-
debug("couldn't open config file for reading");
33-
34-
return;
35-
}
36-
37-
int version = -1;
38-
39-
file.read(reinterpret_cast<char *>(&version), sizeof version);
40-
41-
if (version != VERSION) {
42-
// TODO: Use a non-shit config format
43-
debug("config has outdated version, ignoring");
44-
45-
return;
46-
}
47-
48-
file.read(reinterpret_cast<char *>(&tap_time), sizeof tap_time);
49-
file.read(reinterpret_cast<char *>(&mirror_mod), sizeof mirror_mod);
50-
file.read(reinterpret_cast<char *>(&compensation_offset), sizeof compensation_offset);
51-
file.read(reinterpret_cast<char *>(&humanization_modifier), sizeof humanization_modifier);
52-
file.read(reinterpret_cast<char *>(&randomization_mean), sizeof randomization_mean);
53-
file.read(reinterpret_cast<char *>(&randomization_stddev), sizeof randomization_stddev);
54-
file.read(reinterpret_cast<char *>(&humanization_type), sizeof humanization_type);
55-
56-
debug("loaded config from file");
57-
}
58-
59-
void write_to_file() {
60-
std::fstream file{"maniac-config", std::fstream::binary | std::fstream::trunc | std::fstream::out};
61-
62-
if (!file.is_open()) {
63-
debug("couldn't open config file for writing");
64-
65-
return;
66-
}
67-
68-
int version = VERSION;
69-
70-
file.write(reinterpret_cast<char *>(&version), sizeof version);
71-
file.write(reinterpret_cast<char *>(&tap_time), sizeof tap_time);
72-
file.write(reinterpret_cast<char *>(&mirror_mod), sizeof mirror_mod);
73-
file.write(reinterpret_cast<char *>(&compensation_offset), sizeof compensation_offset);
74-
file.write(reinterpret_cast<char *>(&humanization_modifier), sizeof humanization_modifier);
75-
file.write(reinterpret_cast<char *>(&randomization_mean), sizeof randomization_mean);
76-
file.write(reinterpret_cast<char *>(&randomization_stddev), sizeof randomization_stddev);
77-
file.write(reinterpret_cast<char *>(&humanization_type), sizeof humanization_type);
78-
79-
debug("wrote config to file");
80-
}
8124
};
8225
}
8326

lib/include/maniac/process.h

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <vector>
66
#include <stdexcept>
77
#include <functional>
8+
#include <format>
89
#include <maniac/common.h>
910
#include <maniac/signature.h>
1011
// maniac/common.h includes windows.h, win32 headers can't be included beforehand
@@ -108,29 +109,16 @@ inline T Process::read_memory(uintptr_t address) const {
108109

109110
template<typename T, typename Any>
110111
T Process::read_memory_safe(const char *name, Any addr) const {
111-
// TODO: So much for "safe".
112112
auto address = (uintptr_t)(void *)addr;
113113

114114
if (!address) {
115-
// TODO: Get rid of this ASAP once std::format is out.
116-
char msg[128];
117-
msg[127] = '\0';
118-
119-
sprintf_s(msg, 128, "pointer to %s was invalid", name);
120-
121-
throw std::runtime_error(msg);
115+
throw std::runtime_error(std::format("pointer to {} was invalid", name));
122116
}
123117

124118
T out;
125119

126120
if (!read_memory(address, &out, 1)) {
127-
// TODO: See above.
128-
char msg[128];
129-
msg[127] = '\0';
130-
131-
sprintf_s(msg, 128, "failed reading %s at %#x", name, address);
132-
133-
throw std::runtime_error(msg);
121+
throw std::runtime_error(std::format("failed reading {} at {}", name, address));
134122
}
135123

136124
debug("%s: %#x", name, (unsigned int)address);
@@ -148,7 +136,7 @@ inline void Process::send_scan_code(short code, bool down) {
148136
static INPUT in;
149137

150138
in.type = INPUT_KEYBOARD;
151-
in.ki.time = 238423874;
139+
in.ki.time = 0;
152140
in.ki.wScan = 0;
153141
in.ki.dwExtraInfo = 0;
154142
in.ki.dwFlags = down ? 0 : KEYEVENTF_KEYUP;

0 commit comments

Comments
 (0)