From fe31538a27b6ba7d116d72f9d50e58afb75b2148 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Wed, 5 Feb 2025 20:08:12 +0000 Subject: [PATCH] Codechange: Use EnumBitSet for Scanner::Modes. (#13471) --- src/ai/ai_core.cpp | 4 ++-- src/fileio.cpp | 12 ++++++------ src/fileio_func.h | 21 ++++++++++----------- src/game/game_core.cpp | 4 ++-- src/network/network_content_gui.cpp | 8 ++++---- src/newgrf_config.cpp | 2 +- src/openttd.cpp | 6 +++--- 7 files changed, 28 insertions(+), 29 deletions(-) diff --git a/src/ai/ai_core.cpp b/src/ai/ai_core.cpp index 358ab4f834..050803b6ab 100644 --- a/src/ai/ai_core.cpp +++ b/src/ai/ai_core.cpp @@ -168,7 +168,7 @@ AI::frame_counter = 0; if (AI::scanner_info == nullptr) { - TarScanner::DoScan(TarScanner::AI); + TarScanner::DoScan(TarScanner::Mode::AI); AI::scanner_info = new AIScannerInfo(); AI::scanner_info->Initialize(); AI::scanner_library = new AIScannerLibrary(); @@ -325,7 +325,7 @@ /* static */ void AI::Rescan() { - TarScanner::DoScan(TarScanner::AI); + TarScanner::DoScan(TarScanner::Mode::AI); AI::scanner_info->RescanDir(); AI::scanner_library->RescanDir(); diff --git a/src/fileio.cpp b/src/fileio.cpp index 809e36a810..d55782425a 100644 --- a/src/fileio.cpp +++ b/src/fileio.cpp @@ -382,26 +382,26 @@ uint TarScanner::DoScan(Subdirectory sd) return num; } -/* static */ uint TarScanner::DoScan(TarScanner::Mode mode) +/* static */ uint TarScanner::DoScan(TarScanner::Modes modes) { Debug(misc, 2, "Scanning for tars"); TarScanner fs; uint num = 0; - if (mode & TarScanner::BASESET) { + if (modes.Test(TarScanner::Mode::Baseset)) { num += fs.DoScan(BASESET_DIR); } - if (mode & TarScanner::NEWGRF) { + if (modes.Test(TarScanner::Mode::NewGRF)) { num += fs.DoScan(NEWGRF_DIR); } - if (mode & TarScanner::AI) { + if (modes.Test(TarScanner::Mode::AI)) { num += fs.DoScan(AI_DIR); num += fs.DoScan(AI_LIBRARY_DIR); } - if (mode & TarScanner::GAME) { + if (modes.Test(TarScanner::Mode::Game)) { num += fs.DoScan(GAME_DIR); num += fs.DoScan(GAME_LIBRARY_DIR); } - if (mode & TarScanner::SCENARIO) { + if (modes.Test(TarScanner::Mode::Scenario)) { num += fs.DoScan(SCENARIO_DIR); num += fs.DoScan(HEIGHTMAP_DIR); } diff --git a/src/fileio_func.h b/src/fileio_func.h index a6b11538b3..2b65924a33 100644 --- a/src/fileio_func.h +++ b/src/fileio_func.h @@ -60,24 +60,23 @@ class TarScanner : FileScanner { uint DoScan(Subdirectory sd); public: /** The mode of tar scanning. */ - enum Mode : uint8_t { - NONE = 0, ///< Scan nothing. - BASESET = 1 << 0, ///< Scan for base sets. - NEWGRF = 1 << 1, ///< Scan for non-base sets. - AI = 1 << 2, ///< Scan for AIs and its libraries. - SCENARIO = 1 << 3, ///< Scan for scenarios and heightmaps. - GAME = 1 << 4, ///< Scan for game scripts. - ALL = BASESET | NEWGRF | AI | SCENARIO | GAME, ///< Scan for everything. + enum class Mode : uint8_t { + Baseset, ///< Scan for base sets. + NewGRF, ///< Scan for non-base sets. + AI, ///< Scan for AIs and its libraries. + Scenario, ///< Scan for scenarios and heightmaps. + Game, ///< Scan for game scripts. }; + using Modes = EnumBitSet; + + static constexpr Modes MODES_ALL = {Mode::Baseset, Mode::NewGRF, Mode::AI, Mode::Scenario, Mode::Game}; ///< Scan for everything. bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename = {}) override; bool AddFile(Subdirectory sd, const std::string &filename); /** Do the scan for Tars. */ - static uint DoScan(TarScanner::Mode mode); + static uint DoScan(TarScanner::Modes modes); }; -DECLARE_ENUM_AS_BIT_SET(TarScanner::Mode) - #endif /* FILEIO_FUNC_H */ diff --git a/src/game/game_core.cpp b/src/game/game_core.cpp index de1edf37ec..93d6c7781b 100644 --- a/src/game/game_core.cpp +++ b/src/game/game_core.cpp @@ -61,7 +61,7 @@ Game::frame_counter = 0; if (Game::scanner_info == nullptr) { - TarScanner::DoScan(TarScanner::GAME); + TarScanner::DoScan(TarScanner::Mode::Game); Game::scanner_info = new GameScannerInfo(); Game::scanner_info->Initialize(); Game::scanner_library = new GameScannerLibrary(); @@ -190,7 +190,7 @@ /* static */ void Game::Rescan() { - TarScanner::DoScan(TarScanner::GAME); + TarScanner::DoScan(TarScanner::Mode::Game); Game::scanner_info->RescanDir(); Game::scanner_library->RescanDir(); diff --git a/src/network/network_content_gui.cpp b/src/network/network_content_gui.cpp index 7adbbe077c..a0934e457d 100644 --- a/src/network/network_content_gui.cpp +++ b/src/network/network_content_gui.cpp @@ -204,7 +204,7 @@ public: void Close([[maybe_unused]] int data = 0) override { - TarScanner::Mode mode = TarScanner::NONE; + TarScanner::Modes modes{}; for (auto ctype : this->receivedTypes) { switch (ctype) { case CONTENT_TYPE_AI: @@ -219,7 +219,7 @@ public: case CONTENT_TYPE_BASE_GRAPHICS: case CONTENT_TYPE_BASE_SOUNDS: case CONTENT_TYPE_BASE_MUSIC: - mode |= TarScanner::BASESET; + modes.Set(TarScanner::Mode::Baseset); break; case CONTENT_TYPE_NEWGRF: @@ -228,7 +228,7 @@ public: case CONTENT_TYPE_SCENARIO: case CONTENT_TYPE_HEIGHTMAP: - mode |= TarScanner::SCENARIO; + modes.Set(TarScanner::Mode::Scenario); break; default: @@ -236,7 +236,7 @@ public: } } - TarScanner::DoScan(mode); + TarScanner::DoScan(modes); /* Tell all the backends about what we've downloaded */ for (auto ctype : this->receivedTypes) { diff --git a/src/newgrf_config.cpp b/src/newgrf_config.cpp index 0e7ab0d289..5c538311ff 100644 --- a/src/newgrf_config.cpp +++ b/src/newgrf_config.cpp @@ -559,7 +559,7 @@ static bool GRFSorter(std::unique_ptr const &c1, std::unique_ptr arguments) * The next two functions are needed to list the graphics sets. We can't do them earlier * because then we cannot show it on the debug console as that hasn't been configured yet. */ DeterminePaths(arguments[0], only_local_path); - TarScanner::DoScan(TarScanner::BASESET); + TarScanner::DoScan(TarScanner::Mode::Baseset); BaseGraphics::FindSets(); BaseSounds::FindSets(); BaseMusic::FindSets(); @@ -663,7 +663,7 @@ int openttd_main(std::span arguments) } DeterminePaths(arguments[0], only_local_path); - TarScanner::DoScan(TarScanner::BASESET); + TarScanner::DoScan(TarScanner::Mode::Baseset); if (dedicated) Debug(net, 3, "Starting dedicated server, version {}", _openttd_revision); if (_dedicated_forks && !dedicated) _dedicated_forks = false;