From 933570fd629b6fc738ee3e7bb9c779fb9b4dec82 Mon Sep 17 00:00:00 2001 From: Sidney <27780930+autinerd@users.noreply.github.com> Date: Sat, 8 Aug 2020 14:38:43 +0200 Subject: [PATCH] Close #12442: Refactor SCENARIO_SOURCE to use strong enum (#12608) --- .../windows/TitleScenarioSelect.cpp | 14 +++++++------- src/openrct2/rct1/S4Importer.cpp | 2 +- src/openrct2/scenario/Scenario.h | 18 +++++++++--------- src/openrct2/scenario/ScenarioRepository.cpp | 18 +++++++++--------- src/openrct2/scenario/ScenarioRepository.h | 3 ++- src/openrct2/scenario/ScenarioSources.cpp | 4 ++-- src/openrct2/title/TitleScreen.cpp | 6 +++--- 7 files changed, 33 insertions(+), 32 deletions(-) diff --git a/src/openrct2-ui/windows/TitleScenarioSelect.cpp b/src/openrct2-ui/windows/TitleScenarioSelect.cpp index 07e52d84a9..9247fc55cb 100644 --- a/src/openrct2-ui/windows/TitleScenarioSelect.cpp +++ b/src/openrct2-ui/windows/TitleScenarioSelect.cpp @@ -212,9 +212,9 @@ static void window_scenarioselect_init_tabs(rct_window* w) const scenario_index_entry* scenario = scenario_repository_get_by_index(i); if (gConfigGeneral.scenario_select_mode == SCENARIO_SELECT_MODE_ORIGIN || _titleEditor) { - if (_titleEditor && scenario->source_game == SCENARIO_SOURCE_OTHER) + if (_titleEditor && scenario->source_game == ScenarioSource::Other) continue; - showPages |= 1 << scenario->source_game; + showPages |= 1 << static_cast(scenario->source_game); } else { @@ -695,14 +695,14 @@ static void initialise_list_items(rct_window* w) if (!is_scenario_visible(w, scenario)) continue; - if (_titleEditor && scenario->source_game == SCENARIO_SOURCE_OTHER) + if (_titleEditor && scenario->source_game == ScenarioSource::Other) continue; // Category heading rct_string_id headingStringId = STR_NONE; if (gConfigGeneral.scenario_select_mode == SCENARIO_SELECT_MODE_ORIGIN || _titleEditor) { - if (w->selected_tab != SCENARIO_SOURCE_REAL && currentHeading != scenario->category) + if (w->selected_tab != static_cast(ScenarioSource::Real) && currentHeading != scenario->category) { currentHeading = scenario->category; headingStringId = ScenarioCategoryStringIds[currentHeading]; @@ -712,9 +712,9 @@ static void initialise_list_items(rct_window* w) { if (w->selected_tab <= SCENARIO_CATEGORY_EXPERT) { - if (currentHeading != scenario->source_game) + if (currentHeading != static_cast(scenario->source_game)) { - currentHeading = scenario->source_game; + currentHeading = static_cast(scenario->source_game); headingStringId = ScenarioOriginStringIds[currentHeading]; } } @@ -805,7 +805,7 @@ static bool is_scenario_visible(rct_window* w, const scenario_index_entry* scena { if (gConfigGeneral.scenario_select_mode == SCENARIO_SELECT_MODE_ORIGIN || _titleEditor) { - if (scenario->source_game != w->selected_tab) + if (static_cast(scenario->source_game) != w->selected_tab) { return false; } diff --git a/src/openrct2/rct1/S4Importer.cpp b/src/openrct2/rct1/S4Importer.cpp index 7f4f44d3f9..c8f89b08a6 100644 --- a/src/openrct2/rct1/S4Importer.cpp +++ b/src/openrct2/rct1/S4Importer.cpp @@ -222,7 +222,7 @@ public: bool isOfficial = ScenarioSources::TryGetById(_s4.scenario_slot_index, &desc); dst->category = desc.category; - dst->source_game = desc.source; + dst->source_game = ScenarioSource{ desc.source }; dst->source_index = desc.index; dst->sc_id = desc.id; diff --git a/src/openrct2/scenario/Scenario.h b/src/openrct2/scenario/Scenario.h index 63db57d7ee..f078b300bd 100644 --- a/src/openrct2/scenario/Scenario.h +++ b/src/openrct2/scenario/Scenario.h @@ -63,16 +63,16 @@ struct rct_s6_info }; assert_struct_size(rct_s6_info, 0x198); -enum SCENARIO_SOURCE +enum class ScenarioSource : uint8_t { - SCENARIO_SOURCE_RCT1, - SCENARIO_SOURCE_RCT1_AA, - SCENARIO_SOURCE_RCT1_LL, - SCENARIO_SOURCE_RCT2, - SCENARIO_SOURCE_RCT2_WW, - SCENARIO_SOURCE_RCT2_TT, - SCENARIO_SOURCE_REAL, - SCENARIO_SOURCE_OTHER, + RCT1, + RCT1_AA, + RCT1_LL, + RCT2, + RCT2_WW, + RCT2_TT, + Real, + Other }; struct rct_stex_entry diff --git a/src/openrct2/scenario/ScenarioRepository.cpp b/src/openrct2/scenario/ScenarioRepository.cpp index 513f8360e4..57790b0345 100644 --- a/src/openrct2/scenario/ScenarioRepository.cpp +++ b/src/openrct2/scenario/ScenarioRepository.cpp @@ -66,7 +66,7 @@ static int32_t scenario_index_entry_CompareByCategory(const scenario_index_entry default: if (entryA.source_game != entryB.source_game) { - return entryA.source_game - entryB.source_game; + return static_cast(entryA.source_game) - static_cast(entryB.source_game); } return strcmp(entryA.name, entryB.name); case SCENARIO_CATEGORY_REAL: @@ -80,11 +80,11 @@ static int32_t scenario_index_entry_CompareByIndex(const scenario_index_entry& e // Order by source game if (entryA.source_game != entryB.source_game) { - return entryA.source_game - entryB.source_game; + return static_cast(entryA.source_game) - static_cast(entryB.source_game); } // Then by index / category / name - uint8_t sourceGame = entryA.source_game; + ScenarioSource sourceGame = ScenarioSource{ entryA.source_game }; switch (sourceGame) { default: @@ -111,7 +111,7 @@ static int32_t scenario_index_entry_CompareByIndex(const scenario_index_entry& e { return entryA.source_index - entryB.source_index; } - case SCENARIO_SOURCE_REAL: + case ScenarioSource::Real: return scenario_index_entry_CompareByCategory(entryA, entryB); } } @@ -185,7 +185,7 @@ protected: item.timestamp = stream->ReadValue(); item.category = stream->ReadValue(); - item.source_game = stream->ReadValue(); + item.source_game = ScenarioSource{ stream->ReadValue() }; item.source_index = stream->ReadValue(); item.sc_id = stream->ReadValue(); @@ -302,7 +302,7 @@ private: { entry.sc_id = desc.id; entry.source_index = desc.index; - entry.source_game = desc.source; + entry.source_game = ScenarioSource{ desc.source }; entry.category = desc.category; } else @@ -311,11 +311,11 @@ private: entry.source_index = -1; if (entry.category == SCENARIO_CATEGORY_REAL) { - entry.source_game = SCENARIO_SOURCE_REAL; + entry.source_game = ScenarioSource::Real; } else { - entry.source_game = SCENARIO_SOURCE_OTHER; + entry.source_game = ScenarioSource::Other; } } @@ -401,7 +401,7 @@ public: { const scenario_index_entry* scenario = &_scenarios[i]; - if (scenario->source_game == SCENARIO_SOURCE_OTHER && scenario->sc_id == SC_UNIDENTIFIED) + if (scenario->source_game == ScenarioSource::Other && scenario->sc_id == SC_UNIDENTIFIED) continue; // Note: this is always case insensitive search for cross platform consistency diff --git a/src/openrct2/scenario/ScenarioRepository.h b/src/openrct2/scenario/ScenarioRepository.h index d98c8dd76e..d5ecae363a 100644 --- a/src/openrct2/scenario/ScenarioRepository.h +++ b/src/openrct2/scenario/ScenarioRepository.h @@ -10,6 +10,7 @@ #pragma once #include "../common.h" +#include "../scenario/Scenario.h" #include @@ -30,7 +31,7 @@ struct scenario_index_entry // Category / sequence uint8_t category; - uint8_t source_game; + ScenarioSource source_game; int16_t source_index; uint16_t sc_id; diff --git a/src/openrct2/scenario/ScenarioSources.cpp b/src/openrct2/scenario/ScenarioSources.cpp index abbc79745b..38f50f4c5a 100644 --- a/src/openrct2/scenario/ScenarioSources.cpp +++ b/src/openrct2/scenario/ScenarioSources.cpp @@ -357,7 +357,7 @@ namespace ScenarioSources outDesc->title = nullptr; outDesc->id = SC_UNIDENTIFIED; - outDesc->source = SCENARIO_SOURCE_OTHER; + outDesc->source = static_cast(ScenarioSource::Other); outDesc->index = -1; outDesc->category = SCENARIO_CATEGORY_OTHER; return false; @@ -388,7 +388,7 @@ namespace ScenarioSources outDesc->title = ""; outDesc->id = SC_UNIDENTIFIED; - outDesc->source = SCENARIO_SOURCE_OTHER; + outDesc->source = static_cast(ScenarioSource::Other); outDesc->index = -1; outDesc->category = SCENARIO_CATEGORY_OTHER; return false; diff --git a/src/openrct2/title/TitleScreen.cpp b/src/openrct2/title/TitleScreen.cpp index 4c2cab4922..85c5847e96 100644 --- a/src/openrct2/title/TitleScreen.cpp +++ b/src/openrct2/title/TitleScreen.cpp @@ -234,15 +234,15 @@ void TitleScreen::TitleInitialise() for (size_t s = 0; s < scenarioCount; s++) { - if (scenario_repository_get_by_index(s)->source_game == SCENARIO_SOURCE_RCT1) + if (scenario_repository_get_by_index(s)->source_game == ScenarioSource::RCT1) { RCT1Count++; } - if (scenario_repository_get_by_index(s)->source_game == SCENARIO_SOURCE_RCT1_AA) + if (scenario_repository_get_by_index(s)->source_game == ScenarioSource::RCT1_AA) { RCT1AAInstalled = true; } - if (scenario_repository_get_by_index(s)->source_game == SCENARIO_SOURCE_RCT1_LL) + if (scenario_repository_get_by_index(s)->source_game == ScenarioSource::RCT1_LL) { RCT1LLInstalled = true; }