From bcf93aeecab1dd6223bac3895e1dd461b7526948 Mon Sep 17 00:00:00 2001 From: MistEO Date: Fri, 31 Oct 2025 14:56:56 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=9B=86=E6=88=90=20MaaUtils=20(#1?= =?UTF-8?q?4578)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .devcontainer/1/devcontainer.json | 2 +- .devcontainer/1/post-create.sh | 4 +- .github/workflows/ci.yml | 17 +- .github/workflows/release-nightly-ota.yml | 3 +- .github/workflows/res-update-game.yml | 6 +- .github/workflows/smoke-testing.yml | 4 +- .gitignore | 2 +- .gitmodules | 3 + .prettierignore | 2 +- 3rdparty/include/meojson/common/array.hpp | 688 ---------- 3rdparty/include/meojson/common/exception.hpp | 35 - 3rdparty/include/meojson/common/object.hpp | 605 --------- .../include/meojson/common/serialization.hpp | 207 --- 3rdparty/include/meojson/common/types.hpp | 7 - 3rdparty/include/meojson/common/utils.hpp | 473 ------- 3rdparty/include/meojson/common/value.hpp | 1142 ----------------- 3rdparty/include/meojson/json.hpp | 9 - 3rdparty/include/meojson/parser/bitops.hpp | 139 -- .../include/meojson/parser/packed_bytes.hpp | 161 --- .../meojson/parser/packed_bytes_arm.hpp | 79 -- .../meojson/parser/packed_bytes_x86.hpp | 140 -- 3rdparty/include/meojson/parser/parser.hpp | 862 ------------- .../include/meojson/reflection/extensions.hpp | 656 ---------- .../meojson/reflection/jsonization.hpp | 648 ---------- CMakeLists.txt | 16 +- cmake/config.cmake | 81 -- cmake/macos.cmake | 6 +- cmake/modules/FindONNXRuntime.cmake | 41 - cmake/utils.cmake | 47 - cmake/version.cmake | 27 - docs/en-us/develop/linux-tutorial.md | 4 +- docs/zh-cn/develop/linux-tutorial.md | 4 +- src/MaaCore/CMakeLists.txt | 26 +- src/MaaCore/Utils/Encoding.cpp | 143 --- src/MaaCore/Utils/Encoding.h | 10 - src/MaaCore/Vision/OCRer.cpp | 10 +- src/MaaUtils | 1 + tools/ResourceUpdater/CMakeLists.txt | 27 +- tools/ResourceUpdater/main.cpp | 6 +- tools/maadeps-download.py | 223 +--- 40 files changed, 81 insertions(+), 6485 deletions(-) delete mode 100644 3rdparty/include/meojson/common/array.hpp delete mode 100644 3rdparty/include/meojson/common/exception.hpp delete mode 100644 3rdparty/include/meojson/common/object.hpp delete mode 100644 3rdparty/include/meojson/common/serialization.hpp delete mode 100644 3rdparty/include/meojson/common/types.hpp delete mode 100644 3rdparty/include/meojson/common/utils.hpp delete mode 100644 3rdparty/include/meojson/common/value.hpp delete mode 100644 3rdparty/include/meojson/json.hpp delete mode 100644 3rdparty/include/meojson/parser/bitops.hpp delete mode 100644 3rdparty/include/meojson/parser/packed_bytes.hpp delete mode 100644 3rdparty/include/meojson/parser/packed_bytes_arm.hpp delete mode 100644 3rdparty/include/meojson/parser/packed_bytes_x86.hpp delete mode 100644 3rdparty/include/meojson/parser/parser.hpp delete mode 100644 3rdparty/include/meojson/reflection/extensions.hpp delete mode 100644 3rdparty/include/meojson/reflection/jsonization.hpp delete mode 100644 cmake/modules/FindONNXRuntime.cmake delete mode 100644 cmake/version.cmake delete mode 100644 src/MaaCore/Utils/Encoding.cpp delete mode 100644 src/MaaCore/Utils/Encoding.h create mode 160000 src/MaaUtils diff --git a/.devcontainer/1/devcontainer.json b/.devcontainer/1/devcontainer.json index d641b320a3..9089ca040b 100644 --- a/.devcontainer/1/devcontainer.json +++ b/.devcontainer/1/devcontainer.json @@ -66,7 +66,7 @@ // CMake settings "cmake.configureSettings": { "BUILD_DEBUG_DEMO": "ON", - "CMAKE_TOOLCHAIN_FILE": "MaaDeps/cmake/maa-x64-linux-toolchain.cmake" + "CMAKE_TOOLCHAIN_FILE": "src/MaaUtils/MaaDeps/cmake/maa-x64-linux-toolchain.cmake" }, "cmake.configureOnOpen": false, diff --git a/.devcontainer/1/post-create.sh b/.devcontainer/1/post-create.sh index 8d1a355e83..6610f452b0 100644 --- a/.devcontainer/1/post-create.sh +++ b/.devcontainer/1/post-create.sh @@ -38,5 +38,5 @@ cd "$WORKSPACE" echo "Installing MaaDeps..." python tools/maadeps-download.py # Link clang-format & clangd to /usr/local/bin for easy access -sudo ln -s $WORKSPACE/MaaDeps/x-tools/llvm/bin/clang-format /usr/local/bin/clang-format -# sudo ln -s $WORKSPACE/MaaDeps/x-tools/llvm/bin/clangd /usr/local/bin/clangd +sudo ln -s $WORKSPACE/src/MaaUtils/MaaDeps/x-tools/llvm/bin/clang-format /usr/local/bin/clang-format +# sudo ln -s $WORKSPACE/src/MaaUtils/MaaDeps/x-tools/llvm/bin/clangd /usr/local/bin/clangd diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 88cbf4f2ef..4bec47f195 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -117,6 +117,7 @@ jobs: - name: Fetch submodules run: | + git submodule update --init --depth 1 src/MaaUtils git submodule update --init --depth 1 3rdparty/EmulatorExtras - name: Cache MaaDeps @@ -124,7 +125,7 @@ jobs: uses: actions/cache@v4 with: path: | - ./MaaDeps + ./src/MaaUtils/MaaDeps key: ${{ runner.os }}-${{ matrix.arch }}-maadeps-${{ hashFiles('tools/maadeps-download.py') }} - name: Bootstrap MaaDeps @@ -225,6 +226,7 @@ jobs: - name: Fetch submodules run: | + git submodule update --init --depth 1 src/MaaUtils git submodule update --init --depth 1 3rdparty/EmulatorExtras git submodule update --init --depth 1 src/maa-cli @@ -232,7 +234,7 @@ jobs: id: cache-maadeps uses: actions/cache@v4 with: - path: ./MaaDeps + path: ./src/MaaUtils/MaaDeps key: ${{ runner.os }}-${{ matrix.arch == 'x86_64' && 'x64' || 'arm64' }}-maadeps-${{ hashFiles('tools/maadeps-download.py') }} - name: Bootstrap MaaDeps @@ -251,7 +253,7 @@ jobs: -DINSTALL_RESOURCE=ON \ -DINSTALL_PYTHON=ON \ -DMAA_HASH_VERSION='${{ needs.meta.outputs.tag }}' \ - -DCMAKE_TOOLCHAIN_FILE=MaaDeps/cmake/maa-${{ matrix.arch == 'x86_64' && 'x64' || 'arm64' }}-linux-toolchain.cmake + -DCMAKE_TOOLCHAIN_FILE=src/MaaUtils/MaaDeps/cmake/maa-${{ matrix.arch == 'x86_64' && 'x64' || 'arm64' }}-linux-toolchain.cmake - name: Build run: | @@ -348,6 +350,10 @@ jobs: with: show-progress: false + - name: Fetch submodules + run: | + git submodule update --init --depth 1 src/MaaUtils + # ninja 1.13.1 is already installed and up-to-date. # - name: Install dependencies # run: | @@ -357,13 +363,12 @@ jobs: id: cache-maadeps uses: actions/cache@v4 with: - path: ./MaaDeps + path: ./src/MaaUtils/MaaDeps key: ${{ runner.os }}-${{ matrix.arch == 'x86_64' && 'x64' || 'arm64' }}-maadeps-${{ hashFiles('tools/maadeps-download.py') }} - name: Bootstrap MaaDeps run: | - [[ ${{ matrix.arch }} = "arm64" ]] && triplet="arm64-osx" || triplet="x64-osx" - python3 tools/maadeps-download.py ${triplet} + python3 tools/maadeps-download.py ${{ matrix.arch == 'x86_64' && 'x64' || 'arm64' }}-osx env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/release-nightly-ota.yml b/.github/workflows/release-nightly-ota.yml index ba4c37422c..4b80d89271 100644 --- a/.github/workflows/release-nightly-ota.yml +++ b/.github/workflows/release-nightly-ota.yml @@ -53,6 +53,7 @@ jobs: - name: Fetch submodules run: | + git submodule update --init --depth 1 src/MaaUtils git submodule update --init --depth 1 3rdparty/EmulatorExtras - name: Checkout ref (if provided) @@ -173,7 +174,7 @@ jobs: uses: actions/cache@v4 with: path: | - ./MaaDeps + ./src/MaaUtils/MaaDeps key: ${{ runner.os }}-${{ matrix.arch }}-maadeps-${{ hashFiles('tools/maadeps-download.py') }} - name: Bootstrap MaaDeps diff --git a/.github/workflows/res-update-game.yml b/.github/workflows/res-update-game.yml index 51f369c77d..dcb4ba4a4e 100644 --- a/.github/workflows/res-update-game.yml +++ b/.github/workflows/res-update-game.yml @@ -144,6 +144,10 @@ jobs: show-progress: false fetch-depth: 3 + - name: Fetch submodules + run: | + git submodule update --init --depth 1 src/MaaUtils + - name: Restore ResourceUpdater from cache id: resupd-cache uses: actions/cache/restore@v4 @@ -159,7 +163,7 @@ jobs: uses: actions/cache@v4 with: path: | - ./MaaDeps + ./src/MaaUtils/MaaDeps key: ${{ runner.os }}-arm64-maadeps-${{ hashFiles('tools/maadeps-download.py') }} - name: Bootstrap MaaDeps diff --git a/.github/workflows/smoke-testing.yml b/.github/workflows/smoke-testing.yml index be7d404ecd..75fe78e0a0 100644 --- a/.github/workflows/smoke-testing.yml +++ b/.github/workflows/smoke-testing.yml @@ -63,14 +63,14 @@ jobs: - name: Fetch submodules if: steps.smoke-cache.outputs.cache-hit != 'true' run: | - git submodule update --init --depth 1 3rdparty/EmulatorExtras + git submodule update --init --depth 1 src/MaaUtils - name: Cache MaaDeps if: steps.smoke-cache.outputs.cache-hit != 'true' id: maadeps-cache uses: actions/cache@v4 with: - path: ./MaaDeps + path: ./src/MaaUtils/MaaDeps key: ${{ runner.os }}-arm64-maadeps-${{ hashFiles('tools/maadeps-download.py') }} - name: Bootstrap MaaDeps diff --git a/.gitignore b/.gitignore index b1fd3510dd..a96f0dcf42 100644 --- a/.gitignore +++ b/.gitignore @@ -457,7 +457,7 @@ tools/RoguelikeRecruitmentTool/output .lycheecache # MaaDeps -/MaaDeps/* +src/MaaUtils/* # ResourceUpdater workflow /original/* diff --git a/.gitmodules b/.gitmodules index fb7968fc4e..ff83e318b0 100644 --- a/.gitmodules +++ b/.gitmodules @@ -10,3 +10,6 @@ [submodule "3rdparty/EmulatorExtras"] path = 3rdparty/EmulatorExtras url = https://github.com/MaaXYZ/EmulatorExtras.git +[submodule "src/MaaUtils"] + path = src/MaaUtils + url = https://github.com/MaaXYZ/MaaUtils diff --git a/.prettierignore b/.prettierignore index e45245d24d..02b3b9ea21 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,9 +1,9 @@ **/pnpm-lock.yaml -MaaDeps/ 3rdparty/ src/maa-cli src/MaaMacGui +src/MaaUtils resource/Arknights-Tile-Pos/ tools/OptimizeTemplates/optimize_templates.json diff --git a/3rdparty/include/meojson/common/array.hpp b/3rdparty/include/meojson/common/array.hpp deleted file mode 100644 index 675e938332..0000000000 --- a/3rdparty/include/meojson/common/array.hpp +++ /dev/null @@ -1,688 +0,0 @@ -// IWYU pragma: private, include - -#pragma once - -#include -#include -#include -#include -#include -#include -#include - -#include "exception.hpp" -#include "utils.hpp" - -namespace json -{ -template -class basic_array -{ - friend class basic_value; - friend class basic_object; - -public: - using raw_array = std::vector>; - using value_type = typename raw_array::value_type; - using iterator = typename raw_array::iterator; - using const_iterator = typename raw_array::const_iterator; - using reverse_iterator = typename raw_array::reverse_iterator; - using const_reverse_iterator = typename raw_array::const_reverse_iterator; - using char_t = typename string_t::value_type; - -public: - basic_array() = default; - basic_array(const basic_array& rhs) = default; - basic_array(basic_array&& rhs) noexcept = default; - basic_array(std::initializer_list init_list); - basic_array(typename raw_array::size_type size); - - // explicit basic_array(const basic_value& val); - // explicit basic_array(basic_value&& val); - - template < - typename jsonization_t, - std::enable_if_t< - _utils::has_to_json_in_templ_spec::value - && !_utils::has_to_json_array_in_templ_spec::value, - bool> = true> - basic_array(const jsonization_t& value) - : basic_array(ext::jsonization().to_json(value)) - { - } - - template < - typename jsonization_t, - std::enable_if_t< - _utils::has_to_json_array_in_templ_spec::value, - bool> = true> - basic_array(const jsonization_t& value) - : basic_array(ext::jsonization().to_json_array(value)) - { - } - - template < - typename jsonization_t, - std::enable_if_t< - std::is_rvalue_reference_v - && _utils::has_move_to_json_in_templ_spec::value - && !_utils::has_move_to_json_array_in_templ_spec::value, - bool> = true> - basic_array(jsonization_t&& value) - : basic_array(ext::jsonization().move_to_json(std::move(value))) - { - } - - //template < - // typename jsonization_t, - // std::enable_if_t< - // std::is_rvalue_reference_v - // && _utils::has_move_to_json_array_in_templ_spec::value, - // bool> = true> - //basic_array(jsonization_t&& value) - // : basic_array( - // ext::jsonization().move_to_json_array(std::move(value))) - //{ - //} - - ~basic_array() noexcept = default; - - bool empty() const noexcept { return _array_data.empty(); } - - size_t size() const noexcept { return _array_data.size(); } - - bool contains(size_t pos) const { return pos < _array_data.size(); } - - bool exists(size_t pos) const { return contains(pos); } - - const basic_value& at(size_t pos) const; - - string_t dumps(std::optional indent = std::nullopt) const - { - return indent ? format(*indent) : to_string(); - } - - string_t to_string() const; - - string_t format(size_t indent = 4) const { return format(indent, 0); } - - template - bool all() const; - template typename collection_t = std::vector> - collection_t as_collection() const; - template < - typename value_t, - size_t Size, - template typename fixed_array_t = std::array> - fixed_array_t as_fixed_array() const; - template - std::tuple as_tuple() const; - template - std::pair as_pair() const; - - template < - typename value_t, - std::enable_if_t< - _utils::has_from_json_array_in_templ_spec::value, - bool> = true> - value_t as() const& - { - value_t res; - ext::jsonization().from_json_array(*this, res); - return res; - } - - template < - typename value_t, - std::enable_if_t< - _utils::has_move_from_json_array_in_templ_spec::value, - bool> = true> - value_t as() && - { - value_t res; - ext::jsonization().move_from_json_array(std::move(*this), res); - return res; - } - - // Usage: get(key_1, key_2, ..., default_value); - template - auto get(key_then_default_value_t&&... keys_then_default_value) const; - - template > - std::optional find(size_t pos) const; - - template - decltype(auto) emplace_back(args_t&&... args); - template - decltype(auto) push_back(args_t&&... args); - - void clear() noexcept; - bool erase(size_t pos); - bool erase(iterator iter); - - iterator begin() noexcept; - iterator end() noexcept; - const_iterator begin() const noexcept; - const_iterator end() const noexcept; - const_iterator cbegin() const noexcept; - const_iterator cend() const noexcept; - - reverse_iterator rbegin() noexcept; - reverse_iterator rend() noexcept; - const_reverse_iterator rbegin() const noexcept; - const_reverse_iterator rend() const noexcept; - const_reverse_iterator crbegin() const noexcept; - const_reverse_iterator crend() const noexcept; - - const basic_value& operator[](size_t pos) const; - basic_value& operator[](size_t pos); - - basic_array operator+(const basic_array& rhs) const&; - basic_array operator+(basic_array&& rhs) const&; - basic_array operator+(const basic_array& rhs) &&; - basic_array operator+(basic_array&& rhs) &&; - - basic_array& operator+=(const basic_array& rhs); - basic_array& operator+=(basic_array&& rhs); - - basic_array& operator=(const basic_array&) = default; - basic_array& operator=(basic_array&&) noexcept = default; - - template < - typename value_t, - std::enable_if_t>, bool> = true> - basic_array& operator=(value_t rhs) - { - return *this = basic_array(std::move(rhs)); - } - - bool operator==(const basic_array& rhs) const; - - bool operator!=(const basic_array& rhs) const { return !(*this == rhs); } - - template < - typename value_t, - template typename collection_t = std::vector, - std::enable_if_t<_utils::is_collection>, bool> = true> - explicit operator collection_t() const - { - return as_collection(); - } - - template < - typename value_t, - size_t Size, - template typename fixed_array_t = std::array, - std::enable_if_t<_utils::is_fixed_array>, bool> = true> - explicit operator fixed_array_t() const - { - return as>(); - } - - template < - typename jsonization_t, - std::enable_if_t< - _utils::has_from_json_in_templ_spec::value - && !_utils::has_from_json_array_in_templ_spec::value, - bool> = true> - explicit operator jsonization_t() const& - { - jsonization_t dst {}; - if (!ext::jsonization().from_json(*this, dst)) { - throw exception("Wrong JSON"); - } - return dst; - } - - template < - typename jsonization_t, - std::enable_if_t< - _utils::has_from_json_array_in_templ_spec::value, - bool> = true> - explicit operator jsonization_t() const& - { - jsonization_t dst {}; - if (!ext::jsonization().from_json_array(*this, dst)) { - throw exception("Wrong JSON"); - } - return dst; - } - - template < - typename jsonization_t, - std::enable_if_t< - _utils::has_move_from_json_in_templ_spec::value - && !_utils::has_move_from_json_array_in_templ_spec::value, - bool> = true> - explicit operator jsonization_t() && - { - jsonization_t dst {}; - if (!ext::jsonization().from_json(std::move(*this), dst)) { - throw exception("Wrong JSON"); - } - return dst; - } - - template < - typename jsonization_t, - std::enable_if_t< - _utils::has_move_from_json_array_in_templ_spec::value, - bool> = true> - explicit operator jsonization_t() && - { - jsonization_t dst {}; - if (!ext::jsonization().move_from_json_array( - std::move(*this), - dst)) { - throw exception("Wrong JSON"); - } - return dst; - } - -private: - template - auto - get(std::tuple keys_then_default_value, - std::index_sequence) const; - template - auto get_helper(const value_t& default_value, size_t pos, rest_keys_t&&... rest) const; - template - auto get_helper(const value_t& default_value, size_t pos) const; - - string_t format(size_t indent, size_t indent_times) const; - -private: - raw_array _array_data; -}; - -template -inline basic_array::basic_array(std::initializer_list init_list) - : _array_data(init_list) -{ -} - -template -inline basic_array::basic_array(typename raw_array::size_type size) - : _array_data(size) -{ -} - -// template -// inline basic_array::basic_array(const basic_value& val) : -// basic_array(val.as_array()) -//{} -// -// template -// inline basic_array::basic_array(basic_value&& val) -// : basic_array(std::move(val.as_array())) -//{} - -template -inline void basic_array::clear() noexcept -{ - _array_data.clear(); -} - -template -inline bool basic_array::erase(size_t pos) -{ - return erase(_array_data.begin() + pos); -} - -template -inline bool basic_array::erase(iterator iter) -{ - return _array_data.erase(iter) != _array_data.end(); -} - -template -template -inline decltype(auto) basic_array::emplace_back(args_t&&... args) -{ - static_assert( - std::is_constructible_v, - "Parameter can't be used to construct a raw_array::value_type"); - return _array_data.emplace_back(std::forward(args)...); -} - -template -template -inline decltype(auto) basic_array::push_back(args_t&&... args) -{ - return emplace_back(std::forward(args)...); -} - -template -inline const basic_value& basic_array::at(size_t pos) const -{ - return _array_data.at(pos); -} - -template -inline string_t basic_array::to_string() const -{ - string_t str { '[' }; - for (auto iter = _array_data.cbegin(); iter != _array_data.cend();) { - str += iter->to_string(); - if (++iter != _array_data.cend()) { - str += ','; - } - } - str += char_t(']'); - return str; -} - -template -inline string_t basic_array::format(size_t indent, size_t indent_times) const -{ - const string_t tail_indent(indent * indent_times, ' '); - const string_t body_indent(indent * (indent_times + 1), ' '); - - string_t str { '[', '\n' }; - for (auto iter = _array_data.cbegin(); iter != _array_data.cend();) { - str += body_indent + iter->format(indent, indent_times + 1); - if (++iter != _array_data.cend()) { - str += ','; - } - str += '\n'; - } - str += tail_indent + char_t(']'); - return str; -} - -template -template -inline bool basic_array::all() const -{ - for (const auto& elem : _array_data) { - if (!elem.template is()) { - return false; - } - } - return true; -} - -template -template typename collection_t> -inline collection_t basic_array::as_collection() const -{ - return as>(); -} - -template -template typename fixed_array_t> -inline fixed_array_t basic_array::as_fixed_array() const -{ - return as>(); -} - -template -template -inline std::tuple basic_array::as_tuple() const -{ - return as>(); -} - -template -template -inline std::pair basic_array::as_pair() const -{ - return as>(); -} - -template -template -inline auto basic_array::get(key_then_default_value_t&&... keys_then_default_value) const -{ - return get( - std::forward_as_tuple(keys_then_default_value...), - std::make_index_sequence {}); -} - -template -template -inline auto basic_array::get( - std::tuple keys_then_default_value, - std::index_sequence) const -{ - constexpr unsigned long default_value_index = sizeof...(key_then_default_value_t) - 1; - return get_helper( - std::get(keys_then_default_value), - std::get(keys_then_default_value)...); -} - -template -template -inline auto basic_array::get_helper( - const value_t& default_value, - size_t pos, - rest_keys_t&&... rest) const -{ - constexpr bool is_json = std::is_same_v, value_t> - || std::is_same_v, value_t> - || std::is_same_v, value_t>; - constexpr bool is_string = std::is_constructible_v && !is_json; - - if (!contains(pos)) { - if constexpr (is_string) { - return string_t(default_value); - } - else { - return value_t(default_value); - } - } - - return at(pos).get_helper(default_value, std::forward(rest)...); -} - -template -template -inline auto basic_array::get_helper(const value_t& default_value, size_t pos) const -{ - constexpr bool is_json = std::is_same_v, value_t> - || std::is_same_v, value_t> - || std::is_same_v, value_t>; - constexpr bool is_string = std::is_constructible_v && !is_json; - - if (!contains(pos)) { - if constexpr (is_string) { - return string_t(default_value); - } - else { - return value_t(default_value); - } - } - - auto val = _array_data.at(pos); - if (val.template is()) { - if constexpr (is_string) { - return val.template as(); - } - else { - return val.template as(); - } - } - else { - if constexpr (is_string) { - return string_t(default_value); - } - else { - return value_t(default_value); - } - } -} - -template -template -inline std::optional basic_array::find(size_t pos) const -{ - if (!contains(pos)) { - return std::nullopt; - } - const auto& val = _array_data.at(pos); - return val.template is() ? std::optional(val.template as()) - : std::nullopt; -} - -template -inline typename basic_array::iterator basic_array::begin() noexcept -{ - return _array_data.begin(); -} - -template -inline typename basic_array::iterator basic_array::end() noexcept -{ - return _array_data.end(); -} - -template -inline typename basic_array::const_iterator basic_array::begin() const noexcept -{ - return _array_data.begin(); -} - -template -inline typename basic_array::const_iterator basic_array::end() const noexcept -{ - return _array_data.end(); -} - -template -inline typename basic_array::const_iterator basic_array::cbegin() const noexcept -{ - return _array_data.cbegin(); -} - -template -inline typename basic_array::const_iterator basic_array::cend() const noexcept -{ - return _array_data.cend(); -} - -template -inline typename basic_array::reverse_iterator basic_array::rbegin() noexcept -{ - return _array_data.rbegin(); -} - -template -inline typename basic_array::reverse_iterator basic_array::rend() noexcept -{ - return _array_data.rend(); -} - -template -inline typename basic_array::const_reverse_iterator - basic_array::rbegin() const noexcept -{ - return _array_data.rbegin(); -} - -template -inline typename basic_array::const_reverse_iterator - basic_array::rend() const noexcept -{ - return _array_data.rend(); -} - -template -inline typename basic_array::const_reverse_iterator - basic_array::crbegin() const noexcept -{ - return _array_data.crbegin(); -} - -template -inline typename basic_array::const_reverse_iterator - basic_array::crend() const noexcept -{ - return _array_data.crend(); -} - -template -inline basic_value& basic_array::operator[](size_t pos) -{ - return _array_data[pos]; -} - -template -inline const basic_value& basic_array::operator[](size_t pos) const -{ - return _array_data[pos]; -} - -template -inline basic_array - basic_array::operator+(const basic_array& rhs) const& -{ - basic_array temp = *this; - temp._array_data.insert(_array_data.end(), rhs.begin(), rhs.end()); - return temp; -} - -template -inline basic_array basic_array::operator+(basic_array&& rhs) const& -{ - basic_array temp = *this; - temp._array_data.insert( - _array_data.end(), - std::make_move_iterator(rhs.begin()), - std::make_move_iterator(rhs.end())); - return temp; -} - -template -inline basic_array basic_array::operator+(const basic_array& rhs) && -{ - _array_data.insert(_array_data.end(), rhs.begin(), rhs.end()); - return std::move(*this); -} - -template -inline basic_array basic_array::operator+(basic_array&& rhs) && -{ - _array_data.insert( - _array_data.end(), - std::make_move_iterator(rhs.begin()), - std::make_move_iterator(rhs.end())); - return std::move(*this); -} - -template -inline basic_array& basic_array::operator+=(const basic_array& rhs) -{ - _array_data.insert(_array_data.end(), rhs.begin(), rhs.end()); - return *this; -} - -template -inline basic_array& basic_array::operator+=(basic_array&& rhs) -{ - _array_data.insert( - _array_data.end(), - std::make_move_iterator(rhs.begin()), - std::make_move_iterator(rhs.end())); - return *this; -} - -template -inline bool basic_array::operator==(const basic_array& rhs) const -{ - return _array_data == rhs._array_data; -} - -template < - typename ostream_t, - typename string_t, - typename std_ostream_t = std::basic_ostream< - typename string_t::value_type, - std::char_traits>, - typename enable_t = std::enable_if_t< - std::is_same_v || std::is_base_of_v>> -ostream_t& operator<<(ostream_t& out, const basic_array& arr) -{ - out << arr.format(); - return out; -} -} // namespace json diff --git a/3rdparty/include/meojson/common/exception.hpp b/3rdparty/include/meojson/common/exception.hpp deleted file mode 100644 index ec102e1391..0000000000 --- a/3rdparty/include/meojson/common/exception.hpp +++ /dev/null @@ -1,35 +0,0 @@ -// IWYU pragma: private, include - -#pragma once - -#include -#include - -namespace json -{ -class exception : public std::exception -{ -public: - exception() = default; - - exception(const std::string& msg) - : _what(msg) - { - } - - exception(const exception&) = default; - exception& operator=(const exception&) = default; - exception(exception&&) = default; - exception& operator=(exception&&) = default; - - virtual ~exception() noexcept override = default; - - virtual const char* what() const noexcept override - { - return _what.empty() ? "Unknown exception" : _what.c_str(); - } - -protected: - std::string _what; -}; -} diff --git a/3rdparty/include/meojson/common/object.hpp b/3rdparty/include/meojson/common/object.hpp deleted file mode 100644 index 161b4a5d0d..0000000000 --- a/3rdparty/include/meojson/common/object.hpp +++ /dev/null @@ -1,605 +0,0 @@ -// IWYU pragma: private, include - -#pragma once - -#include -#include -#include -#include -#include -#include - -#include "exception.hpp" -#include "utils.hpp" - -namespace json -{ -template -class basic_object -{ - friend class basic_value; - friend class basic_array; - -public: - using raw_object = std::map>; - using key_type = typename raw_object::key_type; - using mapped_type = typename raw_object::mapped_type; - using value_type = typename raw_object::value_type; - using iterator = typename raw_object::iterator; - using const_iterator = typename raw_object::const_iterator; - using char_t = typename string_t::value_type; - -public: - basic_object() = default; - basic_object(const basic_object& rhs) = default; - basic_object(basic_object&& rhs) noexcept = default; - basic_object(std::initializer_list init_list); - - // explicit basic_object(const basic_value& val); - // explicit basic_object(basic_value&& val); - - template < - typename jsonization_t, - std::enable_if_t< - _utils::has_to_json_in_templ_spec::value - && !_utils::has_to_json_object_in_templ_spec::value, - bool> = true> - basic_object(const jsonization_t& value) - : basic_object(ext::jsonization().to_json(value)) - { - } - - template < - typename jsonization_t, - std::enable_if_t< - _utils::has_to_json_object_in_templ_spec::value, - bool> = true> - basic_object(const jsonization_t& value) - : basic_object(ext::jsonization().to_json_object(value)) - { - } - - template < - typename jsonization_t, - std::enable_if_t< - std::is_rvalue_reference_v - && _utils::has_move_to_json_in_templ_spec::value - && !_utils::has_move_to_json_object_in_templ_spec::value, - bool> = true> - basic_object(jsonization_t&& value) - : basic_object(ext::jsonization().move_to_json(std::move(value))) - { - } - - //template < - // typename jsonization_t, - // std::enable_if_t< - // std::is_rvalue_reference_v - // && _utils::has_move_to_json_object_in_templ_spec::value, - // bool> = true> - //basic_object(jsonization_t&& value) - // : basic_object( - // ext::jsonization().move_to_json_object(std::move(value))) - //{ - //} - - ~basic_object() = default; - - bool empty() const noexcept { return _object_data.empty(); } - - size_t size() const noexcept { return _object_data.size(); } - - bool contains(const string_t& key) const; - - bool exists(const string_t& key) const { return contains(key); } - - const basic_value& at(const string_t& key) const; - - string_t dumps(std::optional indent = std::nullopt) const - { - return indent ? format(*indent) : to_string(); - } - - string_t to_string() const; - - string_t format(size_t indent = 4) const { return format(indent, 0); } - - template - bool all() const; - template typename map_t = std::map> - map_t as_map() const; - - template < - typename value_t, - std::enable_if_t< - _utils::has_from_json_object_in_templ_spec::value, - bool> = true> - value_t as() const& - { - value_t res; - ext::jsonization().from_json_object(*this, res); - return res; - } - - template < - typename value_t, - std::enable_if_t< - _utils::has_move_from_json_object_in_templ_spec::value, - bool> = true> - value_t as() && - { - value_t res; - ext::jsonization().move_from_json_object(std::move(*this), res); - return res; - } - - // Usage: get(key_1, key_2, ..., default_value); - template - auto get(key_then_default_value_t&&... keys_then_default_value) const; - - template > - std::optional find(const string_t& key) const; - - template - decltype(auto) emplace(args_t&&... args); - template - decltype(auto) insert(args_t&&... args); - - void clear() noexcept; - bool erase(const string_t& key); - bool erase(iterator iter); - - iterator begin() noexcept; - iterator end() noexcept; - const_iterator begin() const noexcept; - const_iterator end() const noexcept; - const_iterator cbegin() const noexcept; - const_iterator cend() const noexcept; - - basic_value& operator[](const string_t& key); - basic_value& operator[](string_t&& key); - - basic_object operator|(const basic_object& rhs) const&; - basic_object operator|(basic_object&& rhs) const&; - basic_object operator|(const basic_object& rhs) &&; - basic_object operator|(basic_object&& rhs) &&; - - basic_object& operator|=(const basic_object& rhs); - basic_object& operator|=(basic_object&& rhs); - - basic_object& operator=(const basic_object&) = default; - basic_object& operator=(basic_object&&) = default; - - template < - typename value_t, - std::enable_if_t>, bool> = true> - basic_object& operator=(value_t rhs) - { - return *this = basic_object(std::move(rhs)); - } - - bool operator==(const basic_object& rhs) const; - - bool operator!=(const basic_object& rhs) const { return !(*this == rhs); } - - template < - typename value_t, - template typename map_t = std::map, - std::enable_if_t<_utils::is_map>, bool> = true> - explicit operator map_t() const - { - return as_map(); - } - - template < - typename jsonization_t, - std::enable_if_t< - _utils::has_from_json_in_templ_spec::value - && !_utils::has_from_json_object_in_templ_spec::value, - bool> = true> - explicit operator jsonization_t() const& - { - jsonization_t dst {}; - if (!ext::jsonization().from_json(*this, dst)) { - throw exception("Wrong JSON"); - } - return dst; - } - - template < - typename jsonization_t, - std::enable_if_t< - _utils::has_from_json_object_in_templ_spec::value, - bool> = true> - explicit operator jsonization_t() const& - { - jsonization_t dst {}; - if (!ext::jsonization().from_json_object(*this, dst)) { - throw exception("Wrong JSON"); - } - return dst; - } - - template < - typename jsonization_t, - std::enable_if_t< - _utils::has_move_from_json_in_templ_spec::value - && !_utils::has_move_from_json_object_in_templ_spec::value, - bool> = true> - explicit operator jsonization_t() && - { - jsonization_t dst {}; - if (!ext::jsonization().from_json(std::move(*this), dst)) { - throw exception("Wrong JSON"); - } - return dst; - } - - template < - typename jsonization_t, - std::enable_if_t< - _utils::has_move_from_json_object_in_templ_spec::value, - bool> = true> - explicit operator jsonization_t() && - { - jsonization_t dst {}; - if (!ext::jsonization().move_from_json_object( - std::move(*this), - dst)) { - throw exception("Wrong JSON"); - } - return dst; - } - -private: - template - auto - get(std::tuple keys_then_default_value, - std::index_sequence) const; - template - auto get_helper(const value_t& default_value, const string_t& key, rest_keys_t&&... rest) const; - template - auto get_helper(const value_t& default_value, const string_t& key) const; - - string_t format(size_t indent, size_t indent_times) const; - -private: - raw_object _object_data; -}; - -template -inline basic_object::basic_object(std::initializer_list init_list) - : _object_data( - std::make_move_iterator(init_list.begin()), - std::make_move_iterator(init_list.end())) -{ -} - -// template -// inline basic_object::basic_object(const basic_value& val) : -// basic_object(val.as_object()) -//{} -// -// template -// inline basic_object::basic_object(basic_value&& val) -// : basic_object(std::move(val.as_object())) -//{} - -template -inline bool basic_object::contains(const string_t& key) const -{ - return _object_data.find(key) != _object_data.cend(); -} - -template -inline const basic_value& basic_object::at(const string_t& key) const -{ - return _object_data.at(key); -} - -template -inline void basic_object::clear() noexcept -{ - _object_data.clear(); -} - -template -inline bool basic_object::erase(const string_t& key) -{ - return _object_data.erase(key) > 0 ? true : false; -} - -template -inline bool basic_object::erase(iterator iter) -{ - return _object_data.erase(iter) != _object_data.end(); -} - -template -template -inline decltype(auto) basic_object::emplace(args_t&&... args) -{ - static_assert( - std::is_constructible_v, - "Parameter can't be used to construct a raw_object::value_type"); - return _object_data.insert_or_assign(std::forward(args)...); -} - -template -template -inline decltype(auto) basic_object::insert(args_t&&... args) -{ - return emplace(std::forward(args)...); -} - -template -inline string_t basic_object::to_string() const -{ - string_t str { '{' }; - for (auto iter = _object_data.cbegin(); iter != _object_data.cend();) { - const auto& [key, val] = *iter; - str += - char_t('"') + _utils::unescape_string(key) + string_t { '\"', ':' } + val.to_string(); - if (++iter != _object_data.cend()) { - str += ','; - } - } - str += char_t('}'); - return str; -} - -template -inline string_t basic_object::format(size_t indent, size_t indent_times) const -{ - const string_t tail_indent(indent * indent_times, ' '); - const string_t body_indent(indent * (indent_times + 1), ' '); - - string_t str { '{', '\n' }; - for (auto iter = _object_data.cbegin(); iter != _object_data.cend();) { - const auto& [key, val] = *iter; - str += body_indent + char_t('"') + _utils::unescape_string(key) - + string_t { '\"', ':', ' ' } + val.format(indent, indent_times + 1); - if (++iter != _object_data.cend()) { - str += ','; - } - str += '\n'; - } - str += tail_indent + char_t('}'); - return str; -} - -template -template -inline bool basic_object::all() const -{ - for (const auto& [_, val] : _object_data) { - if (!val.template is()) { - return false; - } - } - return true; -} - -template -template typename map_t> -inline map_t basic_object::as_map() const -{ - return as>(); -} - -template -template -inline auto basic_object::get(key_then_default_value_t&&... keys_then_default_value) const -{ - return get( - std::forward_as_tuple(keys_then_default_value...), - std::make_index_sequence {}); -} - -template -template -inline auto basic_object::get( - std::tuple keys_then_default_value, - std::index_sequence) const -{ - constexpr unsigned long default_value_index = sizeof...(key_then_default_value_t) - 1; - return get_helper( - std::get(keys_then_default_value), - std::get(keys_then_default_value)...); -} - -template -template -inline auto basic_object::get_helper( - const value_t& default_value, - const string_t& key, - rest_keys_t&&... rest) const -{ - constexpr bool is_json = std::is_same_v, value_t> - || std::is_same_v, value_t> - || std::is_same_v, value_t>; - constexpr bool is_string = std::is_constructible_v && !is_json; - - if (!contains(key)) { - if constexpr (is_string) { - return string_t(default_value); - } - else { - return value_t(default_value); - } - } - - return at(key).get_helper(default_value, std::forward(rest)...); -} - -template -template -inline auto - basic_object::get_helper(const value_t& default_value, const string_t& key) const -{ - constexpr bool is_json = std::is_same_v, value_t> - || std::is_same_v, value_t> - || std::is_same_v, value_t>; - constexpr bool is_string = std::is_constructible_v && !is_json; - - if (!contains(key)) { - if constexpr (is_string) { - return string_t(default_value); - } - else { - return value_t(default_value); - } - } - - auto val = _object_data.at(key); - if (val.template is()) { - if constexpr (is_string) { - return val.template as(); - } - else { - return val.template as(); - } - } - else { - if constexpr (is_string) { - return string_t(default_value); - } - else { - return value_t(default_value); - } - } -} - -template -template -inline std::optional basic_object::find(const string_t& key) const -{ - auto iter = _object_data.find(key); - if (iter == _object_data.end()) { - return std::nullopt; - } - const auto& val = iter->second; - return val.template is() ? std::optional(val.template as()) - : std::nullopt; -} - -template -inline typename basic_object::iterator basic_object::begin() noexcept -{ - return _object_data.begin(); -} - -template -inline typename basic_object::iterator basic_object::end() noexcept -{ - return _object_data.end(); -} - -template -inline typename basic_object::const_iterator - basic_object::begin() const noexcept -{ - return _object_data.begin(); -} - -template -inline typename basic_object::const_iterator basic_object::end() const noexcept -{ - return _object_data.end(); -} - -template -inline typename basic_object::const_iterator - basic_object::cbegin() const noexcept -{ - return _object_data.cbegin(); -} - -template -inline typename basic_object::const_iterator basic_object::cend() const noexcept -{ - return _object_data.cend(); -} - -template -inline basic_value& basic_object::operator[](const string_t& key) -{ - return _object_data[key]; -} - -template -inline basic_value& basic_object::operator[](string_t&& key) -{ - return _object_data[std::move(key)]; -} - -template -inline basic_object - basic_object::operator|(const basic_object& rhs) const& -{ - basic_object temp = *this; - temp._object_data.insert(rhs.begin(), rhs.end()); - return temp; -} - -template -inline basic_object basic_object::operator|(basic_object&& rhs) const& -{ - basic_object temp = *this; - // temp._object_data.merge(std::move(rhs._object_data)); - temp._object_data.insert( - std::make_move_iterator(rhs.begin()), - std::make_move_iterator(rhs.end())); - return temp; -} - -template -inline basic_object - basic_object::operator|(const basic_object& rhs) && -{ - _object_data.insert(rhs.begin(), rhs.end()); - return std::move(*this); -} - -template -inline basic_object basic_object::operator|(basic_object&& rhs) && -{ - //_object_data.merge(std::move(rhs._object_data)); - _object_data.insert(std::make_move_iterator(rhs.begin()), std::make_move_iterator(rhs.end())); - return std::move(*this); -} - -template -inline basic_object& basic_object::operator|=(const basic_object& rhs) -{ - _object_data.insert(rhs.begin(), rhs.end()); - return *this; -} - -template -inline basic_object& basic_object::operator|=(basic_object&& rhs) -{ - _object_data.insert(std::make_move_iterator(rhs.begin()), std::make_move_iterator(rhs.end())); - return *this; -} - -template -inline bool basic_object::operator==(const basic_object& rhs) const -{ - return _object_data == rhs._object_data; -} - -template < - typename ostream_t, - typename string_t, - typename std_ostream_t = std::basic_ostream< - typename string_t::value_type, - std::char_traits>, - typename enable_t = std::enable_if_t< - std::is_same_v || std::is_base_of_v>> -ostream_t& operator<<(ostream_t& out, const basic_object& obj) -{ - out << obj.format(); - return out; -} -} // namespace json diff --git a/3rdparty/include/meojson/common/serialization.hpp b/3rdparty/include/meojson/common/serialization.hpp deleted file mode 100644 index 445f8a1974..0000000000 --- a/3rdparty/include/meojson/common/serialization.hpp +++ /dev/null @@ -1,207 +0,0 @@ -// IWYU pragma: private, include - -#pragma once - -#include - -#include "types.hpp" -#include "utils.hpp" - -namespace json -{ -namespace _serialization_helper -{ -template -class is_serializable -{ - template - static auto test(int) - -> decltype(std::declval()(std::declval()), std::true_type()); - - template - static std::false_type test(...); - -public: - static constexpr bool value = decltype(test(0))::value; -}; - -struct empty_serializer -{ - // sample: - // json::value operator()(const type_1&) const { return ...; } - // json::value operator()(const type_2&) const { return ...; } - // json::value operator()(const type_3&) const { return ...; } -}; - -template -void unable_to_serialize() -{ - static_assert( - !sizeof(T), - "Unable to serialize T. " -#ifdef _MSC_VER - "See T below: " __FUNCSIG__ -#else - // "See T below: " __PRETTY_FUNCTION__ - -#endif - ); -} -} - -namespace _serialization_helper -{ -template -class is_deserializable -{ - template - static auto test(int) - -> decltype(std::declval()(std::declval>(), std::declval()), std::true_type()); - - template - static std::false_type test(...); - -public: - static constexpr bool value = decltype(test(0))::value; -}; - -struct empty_deserializer -{ - // sample: - // bool operator()(const json::value&, type_1&) const { return ...; } - // bool operator()(const json::value&, type_2&) const { return ...; } - // bool operator()(const json::value&, type_3&) const { return ...; } -}; - -template -void unable_to_deserialize() -{ - static_assert( - !sizeof(T), - "Unable to deserialize T. " -#ifdef _MSC_VER - "See T below: " __FUNCSIG__ -#else - // "See T below: " __PRETTY_FUNCTION__ - -#endif - ); -} -} - -template < - typename in_t, - typename serializer_t = _serialization_helper::empty_serializer, - typename string_t = default_string_t> -basic_value serialize(in_t&& in, const serializer_t& serializer = {}) -{ - if constexpr (_serialization_helper::is_serializable::value) { - return serializer(std::forward(in)); - } - else if constexpr ( - _utils::is_collection> || _utils::is_fixed_array>) { - basic_array arr; - for (auto&& elem : in) { - using elem_t = decltype(elem); - - auto j_elem = - serialize(std::forward(elem), serializer); - arr.emplace_back(std::move(j_elem)); - } - return arr; - } - else if constexpr (_utils::is_map>) { - basic_object obj; - for (auto&& [key, elem] : in) { - using key_t = decltype(key); - using elem_t = decltype(elem); - - auto j_elem = - serialize(std::forward(elem), serializer); - obj.emplace(std::forward(key), std::move(j_elem)); - } - return obj; - } - else if constexpr (std::is_constructible_v, in_t>) { - return basic_value(std::forward(in)); - } - else { - _serialization_helper::unable_to_serialize(); - } -} - -template < - typename out_t, - typename deserializer_t = _serialization_helper::empty_deserializer, - typename string_t = default_string_t> -bool deserialize( - const basic_value& in, - out_t& out, - const deserializer_t& deserializer = {}) -{ - if constexpr (_serialization_helper::is_deserializable::value) { - return deserializer(in, out); - } - else if constexpr (_utils::is_collection>) { - if (!in.is_array()) { - return false; - } - for (auto&& j_elem : in.as_array()) { - using elem_t = typename out_t::value_type; - elem_t elem {}; - if (!deserialize(j_elem, elem, deserializer)) { - return false; - } - if constexpr (_as_collection_helper::has_emplace_back::value) { - out.emplace_back(std::move(elem)); - } - else { - out.emplace(std::move(elem)); - } - } - return true; - } - else if constexpr (_utils::is_fixed_array>) { - if (!in.is_array()) { - return false; - } - auto&& in_as_arr = in.as_array(); - constexpr size_t out_size = _utils::fixed_array_size; - if (in_as_arr.size() != out_size) { - return false; - } - - for (size_t i = 0; i < out_size; ++i) { - auto&& j_elem = in_as_arr.at(i); - using elem_t = typename out_t::value_type; - elem_t elem {}; - if (!deserialize(j_elem, elem, deserializer)) { - return false; - } - out.at(i) = std::move(elem); - } - return true; - } - else if constexpr (_utils::is_map>) { - if (!in.is_object()) { - return false; - } - for (auto&& [key, j_elem] : in.as_object()) { - using elem_t = typename out_t::mapped_type; - elem_t elem {}; - if (!deserialize(j_elem, elem, deserializer)) { - return false; - } - out.emplace(std::forward(key), std::move(elem)); - } - return true; - } - else if constexpr (std::is_constructible_v>) { - out = out_t(in); - return true; - } - else { - _serialization_helper::unable_to_deserialize(); - } -} -} // namespace json diff --git a/3rdparty/include/meojson/common/types.hpp b/3rdparty/include/meojson/common/types.hpp deleted file mode 100644 index 93f9e2c8af..0000000000 --- a/3rdparty/include/meojson/common/types.hpp +++ /dev/null @@ -1,7 +0,0 @@ -// IWYU pragma: private, include - -#pragma once - -#include "array.hpp" -#include "object.hpp" -#include "value.hpp" diff --git a/3rdparty/include/meojson/common/utils.hpp b/3rdparty/include/meojson/common/utils.hpp deleted file mode 100644 index a416853e38..0000000000 --- a/3rdparty/include/meojson/common/utils.hpp +++ /dev/null @@ -1,473 +0,0 @@ -// IWYU pragma: private, include - -#pragma once - -#include -#include -#include -#include -#include -#include -#include - -namespace json -{ -template -class basic_value; -template -class basic_array; -template -class basic_object; - -using default_string_t = std::string; - -using value = basic_value; -using array = basic_array; -using object = basic_object; - -using wvalue = basic_value; -using warray = basic_array; -using wobject = basic_object; -} - -namespace json::ext -{ -template -class jsonization -{ -public: - // json::value to_json(const T&) const; - // bool check_json(const json::value&) const; - // bool from_json(const json::value&, T&) const; -}; -} - -namespace json::_utils -{ -template -using iterator_t = decltype(std::declval().begin()); -template -using remove_cvref_t = std::remove_cv_t>; -template -using iter_value_t = typename std::iterator_traits>::value_type; -template -using range_value_t = iter_value_t>; - -template -constexpr bool is_string = false; -template -constexpr bool is_string> = - std::is_same_v>; - -template -constexpr bool is_container = false; -template -constexpr bool is_container>> = - std::is_same_v> && !is_string; - -template -constexpr bool is_map = false; -template -constexpr bool is_map> = - is_container; - -template -constexpr bool is_fixed_array = false; -template