From 5066b64bb3b13fcaabf6241f7bfeb1826fb1fca0 Mon Sep 17 00:00:00 2001 From: Edo Date: Mon, 3 Jul 2023 12:18:27 +0200 Subject: [PATCH] feature(map_rotation): better error handling (#4) --- .github/workflows/build.yml | 17 ++++++----- premake5.lua | 9 ++++-- src/component/map_rotation.cpp | 53 +++++++++++++++++++--------------- src/component/map_rotation.hpp | 36 +++++++++++++++++++---- src/main.cpp | 2 +- src/std_include.hpp | 1 + 6 files changed, 78 insertions(+), 40 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index bf7d7a0..22c3aa7 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -14,14 +14,13 @@ concurrency: cancel-in-progress: true jobs: - build-win: + build-windows: name: Build Windows runs-on: windows-2022 strategy: fail-fast: false matrix: configuration: - - Debug - Release arch: - x64 @@ -68,14 +67,11 @@ jobs: fail-fast: false matrix: configuration: - - Debug - Release arch: - x64 - arm64 include: - - configuration: Debug - config: debug - configuration: Release config: release steps: @@ -90,7 +86,7 @@ jobs: if: matrix.arch == 'arm64' run: | sudo apt-get update - sudo apt-get -y install crossbuild-essential-arm64 + sudo apt-get install crossbuild-essential-arm64 -y - name: Install Premake5 uses: abel0b/setup-premake@v2.2 @@ -103,7 +99,12 @@ jobs: - name: Set up problem matching uses: ammaraskar/gcc-problem-matcher@master - - name: Set up environment variables + - name: Set up CC environment variable + if: matrix.arch == 'arm64' + run: | + echo "CC=aarch64-linux-gnu-gcc" >> $GITHUB_ENV + + - name: Set up CXX environment variable if: matrix.arch == 'arm64' run: | echo "CXX=aarch64-linux-gnu-g++" >> $GITHUB_ENV @@ -122,7 +123,7 @@ jobs: deploy: name: Deploy artifacts - needs: [build-linux, build-win] + needs: [build-linux, build-windows] runs-on: ubuntu-latest if: github.event_name == 'push' && (github.ref == 'refs/heads/master') steps: diff --git a/premake5.lua b/premake5.lua index 49740e4..73ab622 100644 --- a/premake5.lua +++ b/premake5.lua @@ -42,10 +42,15 @@ editandcontinue "Off" warnings "Extra" characterset "ASCII" -if os.istarget("linux") or os.istarget("darwin") then +filter {"system:linux", "system:macosx"} buildoptions "-pthread" linkoptions "-pthread" -end +filter {} + +filter {"system:macosx", "platforms:arm64"} + buildoptions "-arch arm64" + linkoptions "-arch arm64" +filter {} if os.getenv("CI") then defines "CI" diff --git a/src/component/map_rotation.cpp b/src/component/map_rotation.cpp index 130e2c3..5484ebe 100644 --- a/src/component/map_rotation.cpp +++ b/src/component/map_rotation.cpp @@ -8,22 +8,41 @@ using namespace std::literals; namespace map_rotation { + namespace + { + bool is_valid_key(const std::string& key) + { + static std::array keys = + { + "map", + "gametype", + "exec", + }; + + return std::ranges::find(keys, key) != std::end(keys); + } + } + rotation_data::rotation_data() : index_(0) { } - void rotation_data::randomize() - { - std::random_device rd; - std::mt19937 gen(rd()); - - std::ranges::shuffle(this->rotation_entries_, gen); - } - void rotation_data::add_entry(const std::string& key, const std::string& value) { - this->rotation_entries_.emplace_back(std::make_pair(key, value)); + this->rotation_entries_.emplace_back(key, value); + } + + std::size_t rotation_data::get_entries_size() const noexcept + { + return this->rotation_entries_.size(); + } + + rotation_data::rotation_entry& rotation_data::get_next_entry() + { + const auto index = this->index_; + ++this->index_ %= this->rotation_entries_.size(); + return this->rotation_entries_.at(index); } bool rotation_data::contains(const std::string& key, const std::string& value) const @@ -39,18 +58,6 @@ namespace map_rotation return this->rotation_entries_.empty(); } - std::size_t rotation_data::get_entries_size() const noexcept - { - return this->rotation_entries_.size(); - } - - rotation_data::rotation_entry& rotation_data::get_next_entry() - { - const auto index = this->index_; - ++this->index_ %= this->rotation_entries_.size(); - return this->rotation_entries_.at(index); - } - void rotation_data::parse(const std::string& data) { const auto tokens = utils::string::split(data, ' '); @@ -59,13 +66,13 @@ namespace map_rotation const auto& key = tokens[i]; const auto& value = tokens[i + 1]; - if (key == "map"s || key == "gametype"s) + if (is_valid_key(key)) { this->add_entry(key, value); } else { - throw map_rotation_parse_error(); + throw map_rotation_parse_error(utils::string::va("Invalid key '%s'", key.data())); } } } diff --git a/src/component/map_rotation.hpp b/src/component/map_rotation.hpp index b335e05..f54e644 100644 --- a/src/component/map_rotation.hpp +++ b/src/component/map_rotation.hpp @@ -2,9 +2,33 @@ namespace map_rotation { - struct map_rotation_parse_error : public std::exception +#define DEFAULT_ERROR_MSG "Map Rotation Parse Error" + + class map_rotation_parse_error : public std::runtime_error { - [[nodiscard]] const char* what() const noexcept override { return "Map Rotation Parse Error"; } + static std::string fmt(const std::string& message) + { + std::string error = DEFAULT_ERROR_MSG; + + if (!message.empty()) + { + error.append(": "); + error.append(message); + } + + return error; + } + + public: + map_rotation_parse_error(const std::string& message) + : std::runtime_error(fmt(message)) + { + } + + map_rotation_parse_error() + : std::runtime_error(DEFAULT_ERROR_MSG) + { + } }; class rotation_data @@ -14,14 +38,14 @@ namespace map_rotation rotation_data(); - void randomize(); - void add_entry(const std::string& key, const std::string& value); + [[nodiscard]] std::size_t get_entries_size() const noexcept; + + [[nodiscard]] rotation_entry& get_next_entry(); + [[nodiscard]] bool contains(const std::string& key, const std::string& value) const; [[nodiscard]] bool empty() const noexcept; - [[nodiscard]] std::size_t get_entries_size() const noexcept; - [[nodiscard]] rotation_entry& get_next_entry(); void parse(const std::string& data); diff --git a/src/main.cpp b/src/main.cpp index f10a3ff..5af86ef 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -50,7 +50,7 @@ namespace } catch (const std::exception& ex) { - console::error(utils::string::va("%s: '%s' contains invalid data!", ex.what(), filename.data())); + console::error(utils::string::va("%s. '%s' contains invalid data!\n", ex.what(), filename.data())); } } diff --git a/src/std_include.hpp b/src/std_include.hpp index 45d390f..ca2fa3c 100644 --- a/src/std_include.hpp +++ b/src/std_include.hpp @@ -32,6 +32,7 @@ #include #include +#include #include #include #include