Skip to content

Commit 18bcfd4

Browse files
committed
save and load settings into replaybot.txt
1 parent e631af8 commit 18bcfd4

File tree

4 files changed

+89
-4
lines changed

4 files changed

+89
-4
lines changed

src/overlay_layer.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ bool OverlayLayer::init() {
1414

1515
auto win_size = CCDirector::sharedDirector()->getWinSize();
1616
auto& rs = ReplaySystem::get();
17+
18+
static bool has_loaded = false;
19+
if (!has_loaded) {
20+
has_loaded = true;
21+
rs.load();
22+
}
1723

1824
auto menu = CCMenu::create();
1925
menu->setPosition({0, win_size.height});
@@ -143,7 +149,7 @@ bool OverlayLayer::init() {
143149

144150
addChild(NodeFactory<CCLabelBMFont>::start("Speedhack", "bigFont.fnt")
145151
.setAnchorPoint(ccp(0, .5f))
146-
.setPosition(ccp(20, win_size.height - 110))
152+
.setPosition(ccp(20, win_size.height - 115))
147153
.setScale(0.6f)
148154
);
149155

@@ -152,11 +158,10 @@ bool OverlayLayer::init() {
152158
input->set_value(rs.speed_hack);
153159
input->input_node->setMaxLabelScale(0.7f);
154160
input->input_node->setMaxLabelLength(10);
155-
input->setPosition(ccp(162, win_size.height - 110));
161+
input->setPosition(ccp(162, win_size.height - 115));
156162
input->callback = [&](auto& input) {
157163
const auto value = input.get_value();
158164
rs.speed_hack = value ? value.value() : 1.f;
159-
std::cout << "value is now " << rs.speed_hack << std::endl;
160165
};
161166
addChild(input);
162167
}
@@ -248,6 +253,7 @@ void OverlayLayer::on_load(CCObject*) {
248253
}
249254

250255
void OverlayLayer::keyBackClicked() {
256+
ReplaySystem::get().save();
251257
gd::FLAlertLayer::keyBackClicked();
252258
}
253259

src/replay_system.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "replay_system.hpp"
22
#include "hooks.hpp"
3+
#include <filesystem>
4+
#include <fstream>
35

46
void ReplaySystem::record_action(bool hold, bool player1, bool flip) {
57
if (is_recording()) {
@@ -179,4 +181,73 @@ void ReplaySystem::_update_status_label() {
179181
} else if (recorder.m_recording) {
180182
recorder.stop();
181183
}
184+
}
185+
186+
std::filesystem::path get_save_file_path() {
187+
const auto path = CCFileUtils::sharedFileUtils()->getWritablePath();
188+
return std::filesystem::path(path) / "replaybot.txt";
189+
}
190+
191+
void ReplaySystem::save() {
192+
const auto path = get_save_file_path();
193+
std::ofstream file(path);
194+
// incredible
195+
file << "fps=" << default_fps << '\n';
196+
file << "type=" << static_cast<int>(default_type) << '\n';
197+
file << "real_time=" << real_time_mode << '\n';
198+
file << "showcase_mode=" << showcase_mode << '\n';
199+
file << "recorder_width=" << recorder.m_width << '\n';
200+
file << "recorder_height=" << recorder.m_height << '\n';
201+
file << "recorder_fps=" << recorder.m_fps << '\n';
202+
file << "recorder_until_end=" << recorder.m_until_end << '\n';
203+
file << "recorder_codec=" << recorder.m_codec << '\n';
204+
file << "recorder_bitrate=" << recorder.m_bitrate << '\n';
205+
file << "recorder_extra_args=" << recorder.m_extra_args << '\n';
206+
file << "recorder_extra_audio_args=" << recorder.m_extra_audio_args << '\n';
207+
file << "recorder_after_end_duration=" << recorder.m_after_end_duration << '\n';
208+
}
209+
210+
void ReplaySystem::load() {
211+
const auto path = get_save_file_path();
212+
if (!std::filesystem::exists(path)) return;
213+
std::ifstream file(path);
214+
std::string line;
215+
// incredible
216+
try {
217+
while (std::getline(file, line)) {
218+
if (line.empty()) continue;
219+
const auto [key, value] = split_once(line, '=');
220+
using namespace std::literals::string_view_literals;
221+
// incredible
222+
if (key == "fps"sv)
223+
default_fps = std::stof(std::string(value));
224+
else if (key == "type"sv)
225+
default_type = ReplayType(std::stoi(std::string(value)));
226+
else if (key == "real_time"sv)
227+
real_time_mode = value == "1"sv;
228+
else if (key == "showcase_mode"sv)
229+
showcase_mode = value == "1"sv;
230+
else if (key == "recorder_width"sv)
231+
recorder.m_width = std::stoul(std::string(value));
232+
else if (key == "recorder_height"sv)
233+
recorder.m_height = std::stoul(std::string(value));
234+
else if (key == "recorder_fps"sv)
235+
recorder.m_fps = std::stoul(std::string(value));
236+
else if (key == "recorder_until_end"sv)
237+
recorder.m_until_end = value == "1"sv;
238+
else if (key == "recorder_codec"sv)
239+
recorder.m_codec = value;
240+
else if (key == "recorder_bitrate"sv)
241+
recorder.m_bitrate = value;
242+
else if (key == "recorder_extra_args"sv)
243+
recorder.m_extra_args = value;
244+
else if (key == "recorder_extra_audio_args"sv)
245+
recorder.m_extra_audio_args = value;
246+
else if (key == "recorder_after_end_duration"sv)
247+
recorder.m_after_end_duration = std::stof(std::string(value));
248+
}
249+
} catch (...) {
250+
// no care
251+
}
252+
182253
}

src/replay_system.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ class ReplaySystem {
7474

7575
unsigned get_frame();
7676

77+
void save();
78+
void load();
79+
7780
bool real_time_mode = true; // fuck it we going public
7881
bool showcase_mode = false;
7982
Recorder recorder;

src/utils.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,9 @@ S& operator<<(S& stream, const CCPoint& point) {
5454
return stream;
5555
}
5656

57-
using u8 = unsigned char;
57+
using u8 = unsigned char;
58+
59+
inline std::pair<std::string_view, std::string_view> split_once(const std::string_view str, char split) {
60+
const auto n = str.find(split);
61+
return { str.substr(0, n), str.substr(n + 1) };
62+
}

0 commit comments

Comments
 (0)