Skip to content

Commit cb6f899

Browse files
authored
Merge pull request #173 from OpenBrickProtocolFoundation/small-c-wrapper-compatibility-fixes
Small C wrapper compatibility fixes
2 parents 2a19562 + 093ef9e commit cb6f899

File tree

6 files changed

+43
-29
lines changed

6 files changed

+43
-29
lines changed

src/game/game.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ Game::Game(
3535
tetrion_index](InputEvent event, SimulationStep simulation_step_index) {
3636
spdlog::debug("event: {} (step {})", magic_enum::enum_name(event), simulation_step_index);
3737

38-
recording_writer->add_event(tetrion_index, simulation_step_index, event);
38+
//TODO(Totto): Remove all occurrences of std::ignore, where we shouldn't ignore this return value
39+
std::ignore = recording_writer->add_record(tetrion_index, simulation_step_index, event);
3940
});
4041
}
4142
}

src/game/tetrion.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ void Tetrion::spawn_next_tetromino(const helper::TetrominoType type, const Simul
252252
spdlog::info("game over");
253253
if (m_recording_writer.has_value()) {
254254
spdlog::info("writing snapshot");
255-
m_recording_writer.value()->add_snapshot(m_tetrion_index, simulation_step_index, core_information());
255+
std::ignore = m_recording_writer.value()->add_snapshot(simulation_step_index, core_information());
256256
}
257257
m_active_tetromino = {};
258258
m_ghost_tetromino = {};
@@ -456,7 +456,7 @@ void Tetrion::lock_active_tetromino(const SimulationStep simulation_step_index)
456456
#if !defined(NDEBUG)
457457
if (m_recording_writer) {
458458
spdlog::debug("adding snapshot at step {}", simulation_step_index);
459-
(*m_recording_writer)->add_snapshot(m_tetrion_index, simulation_step_index, core_information());
459+
std::ignore = (*m_recording_writer)->add_snapshot(simulation_step_index, core_information());
460460
}
461461
#endif
462462
}

src/libs/recordings/utility/recording_writer.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,22 @@ recorder::RecordingWriter::RecordingWriter(RecordingWriter&& old) noexcept
1919
helper::expected<recorder::RecordingWriter, std::string> recorder::RecordingWriter::get_writer(
2020
const std::filesystem::path& path,
2121
std::vector<TetrionHeader>&& tetrion_headers,
22-
AdditionalInformation&& information
22+
AdditionalInformation&& information,
23+
bool overwrite
2324
) {
25+
auto mode = std::ios::out | std::ios::binary;
26+
if (overwrite) {
27+
if (std::filesystem::exists(path)) {
28+
return helper::unexpected<std::string>{
29+
fmt::format("file already exists, not overwriting it: \"{}\"", path.string())
30+
};
31+
}
32+
} else {
33+
mode |= std::ios::trunc;
34+
}
35+
2436

25-
auto output_file = std::ofstream{ path, std::ios::out | std::ios::binary };
37+
auto output_file = std::ofstream{ path, mode };
2638

2739
if (not output_file) {
2840
return helper::unexpected<std::string>{ fmt::format("failed to open output file \"{}\"", path.string()) };
@@ -74,7 +86,7 @@ helper::expected<recorder::RecordingWriter, std::string> recorder::RecordingWrit
7486
return RecordingWriter{ std::move(output_file), std::move(tetrion_headers), std::move(information) };
7587
}
7688

77-
helper::expected<void, std::string> recorder::RecordingWriter::add_event(
89+
helper::expected<void, std::string> recorder::RecordingWriter::add_record(
7890
const u8 tetrion_index, // NOLINT(bugprone-easily-swappable-parameters)
7991
const u64 simulation_step_index,
8092
const InputEvent event
@@ -108,7 +120,6 @@ helper::expected<void, std::string> recorder::RecordingWriter::add_event(
108120
}
109121

110122
helper::expected<void, std::string> recorder::RecordingWriter::add_snapshot(
111-
const u8 tetrion_index,
112123
const u64 simulation_step_index,
113124
std::unique_ptr<TetrionCoreInformation> information
114125
) {
@@ -121,9 +132,8 @@ helper::expected<void, std::string> recorder::RecordingWriter::add_snapshot(
121132
return helper::unexpected<std::string>{ result.error() };
122133
}
123134

124-
const auto snapshot = TetrionSnapshot{ tetrion_index, information->level,
125-
information->score, information->lines_cleared,
126-
simulation_step_index, information->mino_stack };
135+
const auto snapshot = TetrionSnapshot{ information->tetrion_index, information->level, information->score,
136+
information->lines_cleared, simulation_step_index, information->mino_stack };
127137

128138
const auto bytes = snapshot.to_bytes();
129139
result = helper::writer::write_vector_to_file(m_output_file, bytes);

src/libs/recordings/utility/recording_writer.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,17 @@ namespace recorder {
2626
static helper::expected<RecordingWriter, std::string> get_writer(
2727
const std::filesystem::path& path,
2828
std::vector<TetrionHeader>&& tetrion_headers,
29-
AdditionalInformation&& information
29+
AdditionalInformation&& information,
30+
bool overwrite = false
3031
);
3132

32-
helper::expected<void, std::string> add_event(
33+
[[nodiscard]] helper::expected<void, std::string> add_record(
3334
u8 tetrion_index, // NOLINT(bugprone-easily-swappable-parameters)
3435
u64 simulation_step_index,
3536
InputEvent event
3637
);
37-
helper::expected<void, std::string>
38-
add_snapshot(u8 tetrion_index, u64 simulation_step_index, std::unique_ptr<TetrionCoreInformation> information);
38+
[[nodiscard]] helper::expected<void, std::string>
39+
add_snapshot(u64 simulation_step_index, std::unique_ptr<TetrionCoreInformation> information);
3940

4041
private:
4142
static helper::expected<void, std::string>

src/lobby/api.hpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ namespace lobby {
4646
// lobby commit used: https://github.com/OpenBrickProtocolFoundation/lobby/commit/2e0c8d05592f4e4d08437e6cb754a30f02c4e97c
4747
static constexpr StaticString supported_version{ "0.1.0" };
4848

49-
helper::expected<void, std::string> check_compatibility();
49+
[[nodiscard]] helper::expected<void, std::string> check_compatibility();
5050

51-
helper::expected<void, std::string> check_reachability();
51+
[[nodiscard]] helper::expected<void, std::string> check_reachability();
5252

5353
explicit Client(const std::string& api_url);
5454

@@ -66,30 +66,32 @@ namespace lobby {
6666

6767
~Client();
6868

69-
helper::expected<Client, std::string> static get_client(const std::string& url);
69+
[[nodiscard]] helper::expected<Client, std::string> static get_client(const std::string& url);
7070

7171

72-
bool is_authenticated();
72+
[[nodiscard]] bool is_authenticated();
7373

74-
bool authenticate(const Credentials& credentials);
74+
[[nodiscard]] bool authenticate(const Credentials& credentials);
7575

76-
helper::expected<std::vector<LobbyInfo>, std::string> get_lobbies();
76+
[[nodiscard]] helper::expected<std::vector<LobbyInfo>, std::string> get_lobbies();
7777

78-
helper::expected<void, std::string> join_lobby(int lobby_id);
78+
[[nodiscard]] helper::expected<void, std::string> join_lobby(int lobby_id);
7979

80-
helper::expected<LobbyDetail, std::string> get_lobby_detail(int lobby_id);
80+
[[nodiscard]] helper::expected<LobbyDetail, std::string> get_lobby_detail(int lobby_id);
8181

82-
helper::expected<void, std::string> delete_lobby(int lobby_id);
82+
[[nodiscard]] helper::expected<void, std::string> delete_lobby(int lobby_id);
8383

84-
helper::expected<void, std::string> leave_lobby(int lobby_id);
84+
[[nodiscard]] helper::expected<void, std::string> leave_lobby(int lobby_id);
8585

86-
helper::expected<void, std::string> start_lobby(int lobby_id);
86+
[[nodiscard]] helper::expected<void, std::string> start_lobby(int lobby_id);
8787

88-
helper::expected<LobbyCreateResponse, std::string> create_lobby(const CreateLobbyRequest& arguments);
88+
[[nodiscard]] helper::expected<LobbyCreateResponse, std::string> create_lobby(
89+
const CreateLobbyRequest& arguments
90+
);
8991

90-
helper::expected<std::vector<PlayerInfo>, std::string> get_users();
92+
[[nodiscard]] helper::expected<std::vector<PlayerInfo>, std::string> get_users();
9193

92-
helper::expected<void, std::string> register_user(const RegisterRequest& register_request);
94+
[[nodiscard]] helper::expected<void, std::string> register_user(const RegisterRequest& register_request);
9395
};
9496

9597

0 commit comments

Comments
 (0)