Skip to content

Commit

Permalink
Fix a crash if car's ini contained invalid data
Browse files Browse the repository at this point in the history
  • Loading branch information
Detegr committed Apr 30, 2024
1 parent 310b70c commit 4d805a6
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 30 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.8.2

- Fix a crash that occurred if the car's ini file had invalid data

## 0.8.1

- Fix a bug where the seat position was not loaded correctly if a stage was
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ set(HEADERS

set(openRBRVR_Major 0)
set(openRBRVR_Minor 8)
set(openRBRVR_Patch 1)
set(openRBRVR_Patch 2)
set(openRBRVR_Tweak 0)
# set(openRBRVR_TweakStr "-rc${openRBRVR_Tweak}")

Expand Down
69 changes: 43 additions & 26 deletions src/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,14 +341,20 @@ struct Config {
{
auto cars_ini_path = "Cars\\cars.ini";
if (!std::filesystem::exists(cars_ini_path)) {
dbg("Could not resolve car ini path");
return std::nullopt;
}

ini::IniFile cars_ini(cars_ini_path);
auto car_key = std::format("Car0{}", car_id);
return std::filesystem::path(cars_ini[car_key]["IniFile"].as<std::string>()
| std::ranges::views::filter([](char c) { return c != '"'; })
| std::ranges::to<std::string>());
try {
ini::IniFile cars_ini(cars_ini_path);
auto car_key = std::format("Car0{}", car_id);
return std::filesystem::path(cars_ini[car_key]["IniFile"].as<std::string>()
| std::ranges::views::filter([](char c) { return c != '"'; })
| std::ranges::to<std::string>());
} catch (...) {
dbg("Could not resolve car ini path");
return std::nullopt;
}
}

static std::optional<std::filesystem::path> resolve_personal_car_ini_path(uint32_t car_id)
Expand All @@ -374,9 +380,14 @@ struct Config {
return false;
}

ini::IniFile personal_ini(ini_path.value());
personal_ini["openRBRVR"]["seatPosition"] = vec3_as_space_separated_string(seat_translation);
personal_ini.save(ini_path.value());
try {
ini::IniFile personal_ini(ini_path.value());
personal_ini["openRBRVR"]["seatPosition"] = vec3_as_space_separated_string(seat_translation);
personal_ini.save(ini_path.value());
} catch (...) {
dbg("Updating seat translation failed");
return false;
}

return true;
}
Expand All @@ -392,30 +403,36 @@ struct Config {
bool is_openrbrvr_translation = false;
std::optional<glm::vec3> seat_translation = std::nullopt;

if (std::filesystem::exists(ini_path.value())) {
ini::IniFile personal_ini(ini_path.value());
try {
if (std::filesystem::exists(ini_path.value())) {
ini::IniFile personal_ini(ini_path.value());

seat_translation = vec3_from_space_separated_string(personal_ini["openRBRVR"]["seatPosition"].as<std::string>());
seat_translation = vec3_from_space_separated_string(personal_ini["openRBRVR"]["seatPosition"].as<std::string>());

if (!seat_translation) {
seat_translation = vec3_from_space_separated_string(personal_ini["Cam_internal"]["Pos"].as<std::string>());
} else {
is_openrbrvr_translation = true;
if (!seat_translation) {
seat_translation = vec3_from_space_separated_string(personal_ini["Cam_internal"]["Pos"].as<std::string>());
} else {
is_openrbrvr_translation = true;
}
}
}

if (!seat_translation) {
ini_path = resolve_car_ini_path(car_id).and_then(to_string);
if (!ini_path) {
return default_translation;
if (!seat_translation) {
ini_path = resolve_car_ini_path(car_id).and_then(to_string);
if (!ini_path) {
return default_translation;
}
ini::IniFile ini(ini_path.value());
seat_translation = vec3_from_space_separated_string(ini["Cam_internal"]["Pos"].as<std::string>());
}
ini::IniFile ini(ini_path.value());
seat_translation = vec3_from_space_separated_string(ini["Cam_internal"]["Pos"].as<std::string>());
}

return seat_translation
.and_then([&](const glm::vec3& t) { return std::optional(std::make_tuple(t, is_openrbrvr_translation)); })
.value_or(default_translation);
return seat_translation
.and_then([&](const glm::vec3& t) { return std::optional(std::make_tuple(t, is_openrbrvr_translation)); })
.value_or(default_translation);

} catch (...) {
dbg("Loading seat translation failed");
return default_translation;
}
}

static Config from_path(const std::filesystem::path& path)
Expand Down
9 changes: 6 additions & 3 deletions src/RBR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ namespace rbr {
g::game_mode = game_mode;
}

if(g::previous_game_mode != g::game_mode && g::game_mode == GameMode::PreStage) {
if (g::previous_game_mode != g::game_mode && g::game_mode == GameMode::PreStage) {
// Make sure we reload the seat position whenever the stage is restarted
g::seat_position_loaded = false;
}
Expand Down Expand Up @@ -329,8 +329,11 @@ namespace rbr {
}

if (!seat_saved && seat_still_frames > 250) {
g::cfg.insert_or_update_seat_translation(*g::car_id_ptr, g::seat_translation);
g::game->WriteGameMessage("openRBRVR seat position saved.", 2.0, 100.0, 100.0);
if (g::cfg.insert_or_update_seat_translation(*g::car_id_ptr, g::seat_translation)) {
g::game->WriteGameMessage("openRBRVR seat position saved.", 2.0, 100.0, 100.0);
} else {
g::game->WriteGameMessage("Failed to save openRBRVR seat position.", 2.0, 100.0, 100.0);
}
seat_saved = true;
}
} else if (g::seat_movement_request) {
Expand Down

0 comments on commit 4d805a6

Please sign in to comment.