diff --git a/.github/workflows/.gitignore b/.github/workflows/.gitignore new file mode 100644 index 000000000..e63fcbedb --- /dev/null +++ b/.github/workflows/.gitignore @@ -0,0 +1,4 @@ +!build-linux.yml +!build-linux-release.yml +!build-windows.yml +!build-windows-release.yml \ No newline at end of file diff --git a/.github/workflows/branch_rules.yml b/.github/workflows/branch_rules.yml index ea653c685..04b296388 100644 --- a/.github/workflows/branch_rules.yml +++ b/.github/workflows/branch_rules.yml @@ -1,4 +1,4 @@ -name: branch_rules +name: branch-rules on: pull_request: diff --git a/.github/workflows/build-linux-release.yml b/.github/workflows/build-linux-release.yml new file mode 100644 index 000000000..f1f8709c3 --- /dev/null +++ b/.github/workflows/build-linux-release.yml @@ -0,0 +1,43 @@ +name: build-linux-release + +on: + pull_request: + branches: + - main + types: + - opened + - reopened + - synchronize + - edited + +permissions: + contents: read + pull-requests: read + +concurrency: + group: build-linux-release-${{ github.event.pull_request.number }} + cancel-in-progress: true + +jobs: + build: + name: build linux release + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + repository: ${{ github.event.pull_request.head.repo.full_name }} + ref: ${{ github.head_ref }} + fetch-depth: 0 + + - name: Setup Nix + uses: cachix/install-nix-action@v30 + + - name: Setup Rust + uses: dtolnay/rust-toolchain@stable + + - name: Build (Linux) + run: | + eval "$(nix --extra-experimental-features 'nix-command flakes' print-dev-env .)" + xmake f -m release -y + xmake --all -y diff --git a/.github/workflows/build-linux.yml b/.github/workflows/build-linux.yml new file mode 100644 index 000000000..35d050c24 --- /dev/null +++ b/.github/workflows/build-linux.yml @@ -0,0 +1,44 @@ +name: build-linux + +on: + pull_request: + branches: + - dev + - main + types: + - opened + - reopened + - synchronize + - edited + +permissions: + contents: read + pull-requests: read + +concurrency: + group: build-linux-${{ github.event.pull_request.number }} + cancel-in-progress: true + +jobs: + build: + name: build linux + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + repository: ${{ github.event.pull_request.head.repo.full_name }} + ref: ${{ github.head_ref }} + fetch-depth: 0 + + - name: Setup Nix + uses: cachix/install-nix-action@v30 + + - name: Setup Rust + uses: dtolnay/rust-toolchain@stable + + - name: Build (Linux) + run: | + eval "$(nix --extra-experimental-features 'nix-command flakes' print-dev-env .)" + xmake f -m debug -y + xmake --all -y diff --git a/.github/workflows/build-windows-release.yml b/.github/workflows/build-windows-release.yml new file mode 100644 index 000000000..46daa3ae4 --- /dev/null +++ b/.github/workflows/build-windows-release.yml @@ -0,0 +1,45 @@ +name: build-windows-release + +on: + pull_request: + branches: + - main + types: + - opened + - reopened + - synchronize + - edited + +permissions: + contents: read + pull-requests: read + +concurrency: + group: build-windows-release-${{ github.event.pull_request.number }} + cancel-in-progress: true + +jobs: + build: + name: build windows release + runs-on: windows-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + repository: ${{ github.event.pull_request.head.repo.full_name }} + ref: ${{ github.head_ref }} + fetch-depth: 0 + + - name: Setup xmake + uses: xmake-io/github-action-setup-xmake@v1 + + - name: Setup .NET + uses: actions/setup-dotnet@v4 + with: + dotnet-version: "10.0.x" + + - name: Setup Rust + uses: dtolnay/rust-toolchain@stable + + - name: Build (Windows) + run: xmake f -m release -y && xmake --all -y diff --git a/.github/workflows/build-windows.yml b/.github/workflows/build-windows.yml new file mode 100644 index 000000000..dd5b3259c --- /dev/null +++ b/.github/workflows/build-windows.yml @@ -0,0 +1,46 @@ +name: build-windows + +on: + pull_request: + branches: + - dev + - main + types: + - opened + - reopened + - synchronize + - edited + +permissions: + contents: read + pull-requests: read + +concurrency: + group: build-windows-${{ github.event.pull_request.number }} + cancel-in-progress: true + +jobs: + build: + name: build windows + runs-on: windows-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + repository: ${{ github.event.pull_request.head.repo.full_name }} + ref: ${{ github.head_ref }} + fetch-depth: 0 + + - name: Setup xmake + uses: xmake-io/github-action-setup-xmake@v1 + + - name: Setup .NET + uses: actions/setup-dotnet@v4 + with: + dotnet-version: "10.0.x" + + - name: Setup Rust + uses: dtolnay/rust-toolchain@stable + + - name: Build (Windows) + run: xmake f -m debug -y && xmake --all -y diff --git a/.github/workflows/clang-format.yml b/.github/workflows/clang-format.yml new file mode 100644 index 000000000..0ffbdfb08 --- /dev/null +++ b/.github/workflows/clang-format.yml @@ -0,0 +1,40 @@ +name: clang-format + +on: + pull_request: + branches: + - dev + - main + types: + - opened + - reopened + - synchronize + - edited + +permissions: + contents: read + pull-requests: read + +concurrency: + group: clang-format-${{ github.event.pull_request.number }} + cancel-in-progress: true + +jobs: + format: + name: clang-format + runs-on: ubuntu-latest + steps: + - name: Checkout PR branch + uses: actions/checkout@v4 + with: + repository: ${{ github.event.pull_request.head.repo.full_name }} + ref: ${{ github.head_ref }} + fetch-depth: 0 + + - name: Install Nix + uses: cachix/install-nix-action@v30 + + - name: Run clang-format + run: | + eval "$(nix --extra-experimental-features 'nix-command flakes' print-dev-env .)" + bash ./tools/ci/clang_format.sh diff --git a/.github/workflows/clang-tidy.yml b/.github/workflows/clang-tidy.yml new file mode 100644 index 000000000..36e27be8d --- /dev/null +++ b/.github/workflows/clang-tidy.yml @@ -0,0 +1,45 @@ +name: clang-tidy + +on: + pull_request: + branches: + - dev + - main + types: + - opened + - reopened + - synchronize + - edited + +permissions: + contents: read + pull-requests: read + +concurrency: + group: clang-tidy-${{ github.event.pull_request.number }} + cancel-in-progress: true + +jobs: + tidy: + name: clang-tidy + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + repository: ${{ github.event.pull_request.head.repo.full_name }} + ref: ${{ github.head_ref }} + fetch-depth: 0 + + - name: Install Nix + uses: cachix/install-nix-action@v30 + + - name: Generate compile_commands + run: | + eval "$(nix --extra-experimental-features 'nix-command flakes' print-dev-env .)" + xmake project -k compile_commands -y + + - name: Run clang-tidy + run: | + eval "$(nix --extra-experimental-features 'nix-command flakes' print-dev-env .)" + bash ./tools/ci/clang_tidy.sh diff --git a/.github/workflows/draft-dev-to-main.yml b/.github/workflows/draft-dev-to-main.yml new file mode 100644 index 000000000..8f4490fd1 --- /dev/null +++ b/.github/workflows/draft-dev-to-main.yml @@ -0,0 +1,103 @@ +name: draft-dev-to-main + +on: + pull_request: + branches: + - dev + types: + - closed + +permissions: + contents: read + pull-requests: write + issues: write + +jobs: + draft: + name: create dev -> main draft + runs-on: ubuntu-latest + if: github.event.pull_request.merged == true && github.event.pull_request.base.repo.fork == false && github.event.pull_request.base.repo.full_name == github.repository + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + fetch-tags: true + + - name: Fetch main and dev + shell: bash + run: | + set -euo pipefail + git fetch origin main dev --prune --tags + + - name: Determine next tag + id: version + shell: bash + run: | + set -euo pipefail + + ahead_count="$(git rev-list --count origin/main..origin/dev)" + if [[ "${ahead_count}" -le 0 ]]; then + echo "dev is not ahead of main; skipping." + echo "create_pr=false" >> "${GITHUB_OUTPUT}" + exit 0 + fi + + latest_tag="$(git tag --list "2.*.*" --sort=-version:refname | head -n 1 || true)" + if [[ -z "${latest_tag}" ]]; then + new_tag="2.0.0" + else + IFS='.' read -r major minor patch <<< "${latest_tag}" + minor=$((minor + 1)) + patch=0 + new_tag="${major}.${minor}.${patch}" + fi + + echo "new_tag=${new_tag}" >> "${GITHUB_OUTPUT}" + echo "create_pr=true" >> "${GITHUB_OUTPUT}" + + - name: Create draft PR if missing + if: steps.version.outputs.create_pr == 'true' + uses: actions/github-script@v7 + with: + script: | + const owner = context.repo.owner; + const repo = context.repo.repo; + const tag = "${{ steps.version.outputs.new_tag }}"; + + const existing = await github.rest.pulls.list({ + owner, + repo, + state: "open", + base: "main", + head: `${owner}:dev` + }); + + if (existing.data.length > 0) { + core.info("Open dev -> main PR already exists; skipping."); + return; + } + + const pr = await github.rest.pulls.create({ + owner, + repo, + title: `Toast Engine v${tag}`, + head: "dev", + base: "main", + body: "", + draft: true + }); + + await github.rest.pulls.requestReviewers({ + owner, + repo, + pull_number: pr.data.number, + reviewers: ["ynks"] + }); + + await github.rest.issues.addAssignees({ + owner, + repo, + issue_number: pr.data.number, + assignees: ["ynks"] + }); diff --git a/.github/workflows/release-on-merge.yml b/.github/workflows/release-on-merge.yml new file mode 100644 index 000000000..2cb3fe064 --- /dev/null +++ b/.github/workflows/release-on-merge.yml @@ -0,0 +1,87 @@ +name: release-on-merge + +on: + pull_request: + branches: + - main + types: + - closed + +permissions: + contents: write + +jobs: + release: + name: tag and release + runs-on: ubuntu-latest + if: github.event.pull_request.merged == true && github.event.pull_request.base.repo.fork == false && github.event.pull_request.base.repo.full_name == github.repository + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + fetch-tags: true + persist-credentials: true + + - name: Determine next tag + id: version + shell: bash + env: + HEAD_REF: ${{ github.event.pull_request.head.ref }} + MERGE_SHA: ${{ github.event.pull_request.merge_commit_sha }} + run: | + set -euo pipefail + + latest_tag="$(git tag --list "2.*.*" --sort=-version:refname | head -n 1 || true)" + if [[ -z "${latest_tag}" ]]; then + if [[ "${HEAD_REF}" == "dev" ]]; then + new_tag="2.0.0" + else + echo "No existing 2.*.* tags and merge is not dev -> main. Skipping." + echo "create_release=false" >> "${GITHUB_OUTPUT}" + exit 0 + fi + else + IFS='.' read -r major minor patch <<< "${latest_tag}" + if [[ "${HEAD_REF}" == "dev" ]]; then + minor=$((minor + 1)) + patch=0 + elif [[ "${HEAD_REF}" == hotfix/* ]]; then + patch=$((patch + 1)) + else + echo "Head ref '${HEAD_REF}' does not match dev or hotfix/*; skipping." + echo "create_release=false" >> "${GITHUB_OUTPUT}" + exit 0 + fi + new_tag="${major}.${minor}.${patch}" + fi + + if git rev-parse -q --verify "refs/tags/${new_tag}" > /dev/null; then + echo "Tag ${new_tag} already exists; skipping." + echo "create_release=false" >> "${GITHUB_OUTPUT}" + exit 0 + fi + + echo "new_tag=${new_tag}" >> "${GITHUB_OUTPUT}" + echo "merge_sha=${MERGE_SHA}" >> "${GITHUB_OUTPUT}" + echo "create_release=true" >> "${GITHUB_OUTPUT}" + + - name: Create tag + if: steps.version.outputs.create_release == 'true' + run: | + git tag "${{ steps.version.outputs.new_tag }}" "${{ steps.version.outputs.merge_sha }}" + git push origin "refs/tags/${{ steps.version.outputs.new_tag }}" + + - name: Create GitHub release + if: steps.version.outputs.create_release == 'true' + uses: actions/github-script@v7 + with: + script: | + const tag = "${{ steps.version.outputs.new_tag }}"; + await github.rest.repos.createRelease({ + owner: context.repo.owner, + repo: context.repo.repo, + tag_name: tag, + name: `Toast Engine v${tag}`, + body: "" + }); diff --git a/.github/workflows/test-linux-release.yml b/.github/workflows/test-linux-release.yml new file mode 100644 index 000000000..f6ec14525 --- /dev/null +++ b/.github/workflows/test-linux-release.yml @@ -0,0 +1,43 @@ +name: test-linux-release + +on: + pull_request: + branches: + - main + types: + - opened + - reopened + - synchronize + - edited + +permissions: + contents: read + pull-requests: read + +concurrency: + group: test-linux-release-${{ github.event.pull_request.number }} + cancel-in-progress: true + +jobs: + test: + name: test linux release + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + repository: ${{ github.event.pull_request.head.repo.full_name }} + ref: ${{ github.head_ref }} + fetch-depth: 0 + + - name: Install Nix + uses: cachix/install-nix-action@v30 + + - name: Setup Rust + uses: dtolnay/rust-toolchain@stable + + - name: Run tests (Linux) + run: | + eval "$(nix --extra-experimental-features 'nix-command flakes' print-dev-env .)" + xmake f -m release -y + xmake test -y diff --git a/.github/workflows/test-linux.yml b/.github/workflows/test-linux.yml new file mode 100644 index 000000000..b6b332e27 --- /dev/null +++ b/.github/workflows/test-linux.yml @@ -0,0 +1,44 @@ +name: test-linux + +on: + pull_request: + branches: + - dev + - main + types: + - opened + - reopened + - synchronize + - edited + +permissions: + contents: read + pull-requests: read + +concurrency: + group: test-linux-${{ github.event.pull_request.number }} + cancel-in-progress: true + +jobs: + test: + name: test linux + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + repository: ${{ github.event.pull_request.head.repo.full_name }} + ref: ${{ github.head_ref }} + fetch-depth: 0 + + - name: Install Nix + uses: cachix/install-nix-action@v30 + + - name: Setup Rust + uses: dtolnay/rust-toolchain@stable + + - name: Run tests (Linux) + run: | + eval "$(nix --extra-experimental-features 'nix-command flakes' print-dev-env .)" + xmake f -m debug -y + xmake test -y diff --git a/.github/workflows/test-windows-release.yml b/.github/workflows/test-windows-release.yml new file mode 100644 index 000000000..0211882cf --- /dev/null +++ b/.github/workflows/test-windows-release.yml @@ -0,0 +1,47 @@ +name: test-windows-release + +on: + pull_request: + branches: + - main + types: + - opened + - reopened + - synchronize + - edited + +permissions: + contents: read + pull-requests: read + +concurrency: + group: test-windows-release-${{ github.event.pull_request.number }} + cancel-in-progress: true + +jobs: + test: + name: test windows release + runs-on: windows-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + repository: ${{ github.event.pull_request.head.repo.full_name }} + ref: ${{ github.head_ref }} + fetch-depth: 0 + + - name: Setup xmake + uses: xmake-io/github-action-setup-xmake@v1 + + - name: Setup .NET 10 + uses: actions/setup-dotnet@v4 + with: + dotnet-version: "10.0.x" + + - name: Setup Rust + uses: dtolnay/rust-toolchain@stable + + - name: Run tests (Windows) + run: | + xmake f -m release -y + xmake test -y diff --git a/.github/workflows/test-windows.yml b/.github/workflows/test-windows.yml new file mode 100644 index 000000000..8e801f5bd --- /dev/null +++ b/.github/workflows/test-windows.yml @@ -0,0 +1,48 @@ +name: test-windows + +on: + pull_request: + branches: + - dev + - main + types: + - opened + - reopened + - synchronize + - edited + +permissions: + contents: read + pull-requests: read + +concurrency: + group: test-windows-${{ github.event.pull_request.number }} + cancel-in-progress: true + +jobs: + test: + name: test windows + runs-on: windows-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + repository: ${{ github.event.pull_request.head.repo.full_name }} + ref: ${{ github.head_ref }} + fetch-depth: 0 + + - name: Setup xmake + uses: xmake-io/github-action-setup-xmake@v1 + + - name: Setup .NET 10 + uses: actions/setup-dotnet@v4 + with: + dotnet-version: "10.0.x" + + - name: Setup Rust + uses: dtolnay/rust-toolchain@stable + + - name: Run tests (Windows) + run: | + xmake f -m debug -y + xmake test -y diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml deleted file mode 100644 index a1bd59af7..000000000 --- a/.github/workflows/tests.yml +++ /dev/null @@ -1,163 +0,0 @@ -name: tests - -on: - pull_request: - branches: - - dev - types: - - opened - - reopened - - synchronize - - edited - -permissions: - contents: read - pull-requests: read - -concurrency: - group: tests-${{ github.event.pull_request.number }} - cancel-in-progress: true - -jobs: - format: - name: clang-format - runs-on: ubuntu-latest - permissions: - contents: write - pull-requests: read - steps: - - name: Checkout PR branch - uses: actions/checkout@v4 - with: - repository: ${{ github.event.pull_request.head.repo.full_name }} - ref: ${{ github.head_ref }} - fetch-depth: 0 - persist-credentials: true - - - name: Install Nix - uses: cachix/install-nix-action@v30 - - - name: Run clang-format - run: | - eval "$(nix --extra-experimental-features 'nix-command flakes' print-dev-env .)" - bash ./tools/ci/clang_format.sh - - build: - name: build ${{ matrix.os }} - needs: format - runs-on: ${{ matrix.os }} - strategy: - fail-fast: true - matrix: - os: - - ubuntu-latest - - windows-latest - steps: - - name: Checkout repository - uses: actions/checkout@v4 - with: - repository: ${{ github.event.pull_request.head.repo.full_name }} - ref: ${{ github.head_ref }} - fetch-depth: 0 - - - name: Setup Nix - if: runner.os == 'Linux' - uses: cachix/install-nix-action@v30 - - - name: Setup xmake - if: runner.os == 'Windows' - uses: xmake-io/github-action-setup-xmake@v1 - - - name: Setup .NET - if: runner.os == 'Windows' - uses: actions/setup-dotnet@v4 - with: - dotnet-version: "10.0.x" - - - name: Setup Rust - uses: dtolnay/rust-toolchain@stable - - - name: Build (Linux) - if: runner.os == 'Linux' - run: | - eval "$(nix --extra-experimental-features 'nix-command flakes' print-dev-env .)" - xmake f -m debug - xmake --all - - - name: Build (Windows) - if: runner.os == 'Windows' - run: xmake f -m debug && xmake --all - - test: - name: test ${{ matrix.os }} - needs: build - runs-on: ${{ matrix.os }} - strategy: - fail-fast: true - matrix: - os: - - ubuntu-latest - - windows-latest - steps: - - name: Checkout repository - uses: actions/checkout@v4 - with: - repository: ${{ github.event.pull_request.head.repo.full_name }} - ref: ${{ github.head_ref }} - fetch-depth: 0 - - - name: Install Nix - if: runner.os == 'Linux' - uses: cachix/install-nix-action@v30 - - - name: Setup xmake - if: runner.os == 'Windows' - uses: xmake-io/github-action-setup-xmake@v1 - - - name: Setup .NET 10 - if: runner.os == 'Windows' - uses: actions/setup-dotnet@v4 - with: - dotnet-version: "10.0.x" - - - name: Setup Rust - uses: dtolnay/rust-toolchain@stable - - - name: Run tests (Linux) - if: runner.os == 'Linux' - run: | - eval "$(nix --extra-experimental-features 'nix-command flakes' print-dev-env .)" - xmake f -m debug - xmake test - - - name: Run tests (Windows) - if: runner.os == 'Windows' - run: | - xmake f -m debug - xmake test - - tidy: - name: clang-tidy - needs: test - runs-on: ubuntu-latest - steps: - - name: Checkout repository - uses: actions/checkout@v4 - with: - repository: ${{ github.event.pull_request.head.repo.full_name }} - ref: ${{ github.head_ref }} - fetch-depth: 0 - - - name: Install Nix - uses: cachix/install-nix-action@v30 - - - name: Generate compile_commands - run: | - eval "$(nix --extra-experimental-features 'nix-command flakes' print-dev-env .)" - xmake project -k compile_commands - - - name: Run clang-tidy - run: | - eval "$(nix --extra-experimental-features 'nix-command flakes' print-dev-env .)" - bash ./tools/ci/clang_tidy.sh - diff --git a/engine/src/toast/events/event.cpp b/engine/src/toast/events/event.cpp index 91dfcdb91..adeec8418 100644 --- a/engine/src/toast/events/event.cpp +++ b/engine/src/toast/events/event.cpp @@ -1,5 +1,6 @@ #include "event.hpp" +#include #include #include #include @@ -11,6 +12,11 @@ namespace event { +std::unordered_map EventSystem::event_data; +std::mutex EventSystem::pool_mutex; +std::unordered_map> EventSystem::unsubscribe_map; +std::vector> EventSystem::deletion_queue; + namespace { struct Pool { alignas(64) std::array buffer; @@ -38,12 +44,12 @@ void pollEvents() noexcept { TOAST_INFO(_detail::IEvent, "Polling Events"); // delete callbacks - _detail::deletion_queue.clear(); + EventSystem::deletion_queue.clear(); // swap memory pool uint32_t idx; { - std::scoped_lock _(_detail::mutex); + std::scoped_lock _(EventSystem::pool_mutex); idx = index; index = (index + 1) % pools.size(); } diff --git a/engine/src/toast/events/event.hpp b/engine/src/toast/events/event.hpp index 23fc50fb0..221427a74 100644 --- a/engine/src/toast/events/event.hpp +++ b/engine/src/toast/events/event.hpp @@ -10,6 +10,8 @@ */ #pragma once +#include "toast/log.hpp" + #include #include #include @@ -21,7 +23,6 @@ #include #include #include -#include #include namespace event { @@ -42,23 +43,12 @@ struct TOAST_API IEvent { #endif private: - friend void event::pollEvents() noexcept; + friend void TOAST_API event::pollEvents() noexcept; virtual void notify() noexcept = 0; }; -/// @brief mutex for the event system -inline std::mutex mutex; - -/// @brief vtable for unsubscribing callbacks -inline std::unordered_map> unsubscribe_map; - -/// @brief callbacks will be cleaned up only before pollEvents -/// @note unique pointers have the option of storing a pointer to the function that deletes the object -/// so thats how i implement the deletion_queue -inline std::vector> deletion_queue; - /// @brief allocates memory in the event queue pool -auto allocate(std::size_t size, std::size_t align) noexcept -> void*; +auto TOAST_API allocate(std::size_t size, std::size_t align) noexcept -> void*; } /// @brief queue's up an event @@ -66,7 +56,7 @@ auto allocate(std::size_t size, std::size_t align) noexcept -> void*; /// @param args arguments for the constructor template requires std::is_base_of_v<_detail::IEvent, T> -void TOAST_API send(Args&&... args) noexcept; +void send(Args&&... args) noexcept; /// @class Event /// @brief Base class for all events. Derive your event type as `struct Derived : Event`. @@ -91,20 +81,18 @@ void TOAST_API send(Args&&... args) noexcept; /// the callback itself /// template -struct TOAST_API Event : _detail::IEvent { +struct Event : _detail::IEvent { friend class Listener; + friend struct EventSystem; /// @brief callbacks that return 'true' are consumed and do not propogate using callback_t = std::move_only_function; - using iterator_t = std::multimap::iterator; + using iterator_t = std::multimap>::iterator; private: - static inline struct G { - G() noexcept; - std::multimap> callbacks; - std::mutex mutex; - std::vector processing; - bool cached; - } g; + static inline struct Registrar { + Registrar(); + bool registered = false; + } registrar; /// @brief registers a callback to the event callbacks /// @param priority higher number callbacks first @@ -119,6 +107,49 @@ struct TOAST_API Event : _detail::IEvent { void notify() noexcept override; }; +struct TOAST_API EventSystem { + struct EventInfo { + std::mutex mutex; + std::multimap> callbacks; + std::vector processing; + bool cached = false; + }; + + /// @brief callback map for each type of event + static std::unordered_map event_data; + + /// @brief mutex for the event system pool + static std::mutex pool_mutex; + + /// @brief vtable for unsubscribing callbacks + static std::unordered_map> unsubscribe_map; + + /// @brief callbacks will be cleaned up only before pollEvents + /// @note unique pointers have the option of storing a pointer to the function that deletes the object + /// so thats how i implement the deletion_queue + static std::vector> deletion_queue; + + template + static void registerEvent() { + static_assert(std::is_base_of_v, T>, "CONTRACT VIOLATION: You Must Inhert as 'struct Derived : Event'"); + if (event_data.contains(typeid(T))) { + TOAST_INFO(_detail::IEvent, "Event type {} already registered (Windows Is Shit)", typeid(T).name()); + return; + } + TOAST_INFO(_detail::IEvent, "Registering Event Type: {}", typeid(T).name()); + event_data.emplace( // this is goofy because eventinfo has a mutex inside it + std::piecewise_construct, + std::forward_as_tuple(typeid(T)), + std::forward_as_tuple() + ); + + EventSystem::unsubscribe_map.emplace(typeid(T), [](const std::any& iter) { + auto it = std::any_cast::iterator_t>(iter); + Event::unsubscribe(it); + }); + } +}; + } #include "event.inl" diff --git a/engine/src/toast/events/event.inl b/engine/src/toast/events/event.inl index 11b9609e6..2f9c98aa7 100644 --- a/engine/src/toast/events/event.inl +++ b/engine/src/toast/events/event.inl @@ -8,35 +8,34 @@ namespace event { template -Event::G::G() noexcept { - static_assert(std::is_base_of_v, T>, "CONTRACT VIOLATION: You Must Inhert as 'struct Derived : Event'"); - // add function to the Event vtable - _detail::unsubscribe_map.emplace(typeid(T), [](std::any iter) { - auto it = std::any_cast(iter); - unsubscribe(it); - }); +Event::Registrar::Registrar() : registered(true) { + EventSystem::registerEvent(); } template auto Event::subscribe(char priority, callback_t&& callback) noexcept -> iterator_t { + (void)registrar.registered; TOAST_TRACE(_detail::IEvent, "Subscribing Callback To: {}", typeid(T).name()); auto cb = new callback_t(std::move(callback)); + auto& g = EventSystem::event_data[typeid(T)]; { std::scoped_lock _(g.mutex); g.cached = false; - return g.callbacks.emplace(priority, cb); + return g.callbacks.emplace(priority, static_cast(cb)); } } template void Event::unsubscribe(iterator_t it) noexcept { + (void)registrar.registered; TOAST_TRACE(_detail::IEvent, "Unsubscribing Callback To: {}", typeid(T).name()); + auto& g = EventSystem::event_data[typeid(T)]; { std::scoped_lock _(g.mutex); auto deleter = [](void* p) { delete static_cast*>(p); }; - _detail::deletion_queue.emplace_back((*it).second, deleter); + EventSystem::deletion_queue.emplace_back((*it).second, deleter); g.cached = false; g.callbacks.erase(it); } @@ -44,7 +43,10 @@ void Event::unsubscribe(iterator_t it) noexcept { template void Event::notify() noexcept { + (void)registrar.registered; TOAST_TRACE(_detail::IEvent, "Notifying Event: {}", typeid(T).name()); + + auto& g = EventSystem::event_data[typeid(T)]; { // build cached list of all the callbacks in order // locks mutex for the shortest period possible @@ -61,7 +63,7 @@ void Event::notify() noexcept { // disptaches all of the callbacks for (auto& callback : g.processing) { - bool handled = (*callback)(static_cast(*this)); + bool handled = (*static_cast(callback))(static_cast(*this)); if (handled) { return; } @@ -75,7 +77,7 @@ void send(Args&&... args) noexcept { TOAST_TRACE(_detail::IEvent, "Sending Event: {}", typeid(T).name()); // Allocate and enqueue event { - std::scoped_lock _(_detail::mutex); + std::scoped_lock _(EventSystem::pool_mutex); void* memory = _detail::allocate(sizeof(T), alignof(T)); assert(memory); _detail::IEvent* event = new (memory) T(std::forward(args)...); diff --git a/engine/src/toast/events/listener.cpp b/engine/src/toast/events/listener.cpp index 9a998c215..10dbc22a7 100644 --- a/engine/src/toast/events/listener.cpp +++ b/engine/src/toast/events/listener.cpp @@ -8,9 +8,13 @@ Listener::Listener() { m.enabled = true; } +Listener::Listener(bool state) { + m.enabled = state; +} + Listener::~Listener() { for (auto& [type, name, callback] : m.callbacks) { - _detail::unsubscribe_map[type](callback); + EventSystem::unsubscribe_map[type](callback); } } @@ -22,4 +26,5 @@ void Listener::enabled(bool state) { auto Listener::enabled() const -> bool { return m.enabled; } + } diff --git a/engine/src/toast/events/listener.hpp b/engine/src/toast/events/listener.hpp index 19ccc35c7..348440ee8 100644 --- a/engine/src/toast/events/listener.hpp +++ b/engine/src/toast/events/listener.hpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -49,12 +50,15 @@ class TOAST_API Listener { public: Listener(); + + Listener(bool state); + ~Listener(); /// @brief unsubscribes every event in type TEvent with that name /// @param name event callback name/s to remove template - void unsubscribe(std::string name) noexcept; + void unsubscribe(std::string_view name) noexcept; /// @brief subscribes a callback to an TEvent /// @param name of the callback diff --git a/engine/src/toast/events/listener.inl b/engine/src/toast/events/listener.inl index 554c73e43..6eed2288f 100644 --- a/engine/src/toast/events/listener.inl +++ b/engine/src/toast/events/listener.inl @@ -7,7 +7,7 @@ namespace event { template -void Listener::unsubscribe(std::string name) noexcept { +void Listener::unsubscribe(std::string_view name) noexcept { TOAST_TRACE(Listener, "Unsubscribing Callback {} to {}", name, typeid(TEvent).name()); std::erase_if(m.callbacks, [&](auto& callback) { auto& [type, sub_name, any] = callback; diff --git a/engine/src/toast/log.hpp b/engine/src/toast/log.hpp index 274cc5ca0..5c38b73eb 100644 --- a/engine/src/toast/log.hpp +++ b/engine/src/toast/log.hpp @@ -11,11 +11,12 @@ #include #include #include +#include #include /// @internal namespace logging::_detail { -constexpr auto TOAST_API getOnlyName(std::string_view path) -> std::string_view { +constexpr auto getOnlyName(std::string_view path) -> std::string_view { size_t last_slash = path.find_last_of("\\/"); if (last_slash == std::string_view::npos) { return path; diff --git a/engine/src/toast/logger.cpp b/engine/src/toast/logger.cpp index fd560045e..3561b4632 100644 --- a/engine/src/toast/logger.cpp +++ b/engine/src/toast/logger.cpp @@ -94,7 +94,7 @@ auto Logger::create() noexcept -> std::unique_ptr { #if defined(_WIN32) // On Windows, 'start /B' runs the command in the background without opening a new window // For output redirection, we need to wrap it in 'cmd /C' - if (SHOW_SERVER_LOGS) { + if (show_server_logs) { cmd = "start /B " + server_path.string(); } else { cmd = "start /B cmd /C \"" + server_path.string() + " >nul 2>&1\""; diff --git a/engine/xmake.lua b/engine/xmake.lua index c39a841ff..89a67c06f 100644 --- a/engine/xmake.lua +++ b/engine/xmake.lua @@ -11,18 +11,32 @@ target("toast.engine", function() add_files("**.cpp") add_headerfiles("src/(**.hpp)", { public = false, extra = { check = true } }) add_headerfiles("ffi/(**.h)", "include/toast/(**.hpp)", { prefixdir = "toast", extra = { check = true } }) + set_pcxxheader("../pch.h") -- External libraries go here -x add_packages("asio") - add_ldflags("-lstdc++exp", { tools = { "clang", "gcc" }, public = true }) -- adds library for stacktrace + if not is_plat("windows") then + add_syslinks("stdc++exp") + end + if is_plat("windows") and (get_config("toolchain") == "mingw" or get_config("toolchain") == "gcc") then + add_syslinks("stdc++exp") + add_defines("__cpp_lib_constexpr_exceptions=0") + on_load(function(target) + local implib = path.join(target:targetdir(), "lib" .. target:name() .. ".dll.a") + target:add("shflags", "-Wl,--out-implib," .. implib, { force = true }) + end) + end -- Apply clang-format rule - add_rules("clang-format") + -- FIX THIS SO CLANG FORMAT ALWAYS RUNS BEFORE THE DANTE HEADERS BULLSHIT + --add_rules("clang-format") add_rules("sync-headers") if is_plat("linux") then add_syslinks("pthread") add_shflags("-Wl,--no-as-needed") + elseif is_plat("windows", "mingw") then + add_syslinks("ws2_32", "mswsock") end end) diff --git a/pch.h b/pch.h new file mode 100644 index 000000000..e021e921d --- /dev/null +++ b/pch.h @@ -0,0 +1,32 @@ +/** + * @file pch.h + * @author Dante Harper + */ + +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include diff --git a/tests/xmake.lua b/tests/xmake.lua index 7cb400de7..45dd44da2 100644 --- a/tests/xmake.lua +++ b/tests/xmake.lua @@ -13,7 +13,12 @@ for _, filepath in ipairs(os.files(path.join(scriptdir, "**.cpp"))) do add_files(filepath) add_deps("toast.engine") - + if not is_plat("windows") then + add_syslinks("stdc++exp") + end + if is_plat("windows") and (get_config("toolchain") == "mingw" or get_config("toolchain") == "gcc") then + add_syslinks("stdc++exp") + end add_defines("UNIT_TESTING") diff --git a/tools/ci/clang_format.sh b/tools/ci/clang_format.sh index dcf575d26..9dc87f54a 100644 --- a/tools/ci/clang_format.sh +++ b/tools/ci/clang_format.sh @@ -22,20 +22,6 @@ nix --extra-experimental-features "nix-command flakes" develop .#default -c bash exit 0 fi - git config user.name "github-actions[bot]" - git config user.email "41898282+github-actions[bot]@users.noreply.github.com" - git add -- "${files[@]}" - - if ! git commit -m "ci: apply clang-format to engine"; then - echo "Formatting changes detected, but nothing was committed." - exit 1 - fi - - if git push; then - echo "Formatting fixes were pushed to the PR branch. Continuing with the updated commit in this run." - exit 0 - fi - - echo "::error::Formatting changes were detected, but this workflow could not push fixes. Format locally and push." + echo "::error::Formatting changes detected. Run clang-format locally and push the results." exit 1 ' diff --git a/tools/xmake.lua b/tools/xmake.lua index 59fdde062..d99271756 100644 --- a/tools/xmake.lua +++ b/tools/xmake.lua @@ -1,19 +1,20 @@ add_requires("cargo::anyhow 1.0") add_requires("cargo::chrono 0.4") -add_requires("cargo::clap", {configs = {features = "derive"}}) +add_requires("cargo::clap", { configs = { features = "derive" } }) add_requires("cargo::csv 1.3.1") add_requires("cargo::crossterm 0.28.1") add_requires("cargo::prost 0.14") add_requires("cargo::prost-types 0.13.5") -add_requires("cargo::serde", {configs = {features = "derive"}}) +add_requires("cargo::serde", { configs = { features = "derive" } }) add_requires("cargo::textwrap 0.16") -add_requires("cargo::tokio", {configs = {features = "full"}}) +add_requires("cargo::tokio", { configs = { features = "full" } }) add_requires("cargo::tui-input 0.11") local tools_dir = os.scriptdir() target("player", function() set_kind("binary") + set_default(false) add_deps("toast.engine", "toast.dummy_game") add_files("player/**.cs") remove_files("player/obj/**") @@ -25,6 +26,7 @@ end) target("toast.dummy_game", function() set_kind("shared") + set_default(false) add_files("dummy_game/**.cpp") add_headerfiles("dummy_game/**.h") add_deps("toast.engine") @@ -37,31 +39,32 @@ target("kenzo", function() set_default(false) on_build(function(target) local oldir = os.cd("$(projectdir)/tools/kenzo") - + if is_mode("release") then - os.execv("cargo", {"build", "--release"}) + os.execv("cargo", { "build", "--release" }) else - os.execv("cargo", {"build"}) + os.execv("cargo", { "build" }) end - + -- Copy binary to xmake's output directory so xmake run works local target_subdir = is_mode("release") and "release" or "debug" local src_binary = "target/" .. target_subdir .. "/kenzo" local dst_dir = target:targetdir() os.mkdir(dst_dir) os.cp(src_binary, path.join(dst_dir, "kenzo")) - + os.cd(oldir) end) on_clean(function(target) local oldir = os.cd("$(projectdir)/tools/kenzo") - os.execv("cargo", {"clean"}) + os.execv("cargo", { "clean" }) os.cd(oldir) end) end) target("log_server", function() set_kind("binary") + set_default(false) -- set_languages("rust") add_files("log_server/src/main.rs") add_packages( @@ -76,6 +79,6 @@ end) target("toast.tools", function() set_kind("phony") + set_default(false) add_deps("kenzo", "log_server") end) - diff --git a/xmake.lua b/xmake.lua index 261a3e31b..ab33d7587 100644 --- a/xmake.lua +++ b/xmake.lua @@ -1,7 +1,7 @@ add_rules("mode.debug", "mode.release") set_defaultmode("debug") -set_languages("c++23") +set_languages("c++latest") rule("clang-format") before_build(function(target) @@ -24,7 +24,7 @@ end) rule_end() add_rules("plugin.compile_commands.autoupdate") -add_cxxflags("-stdlib=libc++", { tools = { "clang", "gcc" } }) -- Use LLVM STL by default + -- Makes release have flto, fast math and SIMD intrinsic optimizations if is_mode("release") then @@ -39,7 +39,9 @@ end -- Enables debug optimizations, needed on newer clang and gcc versions to avoid a warning if is_mode("debug") then set_optimize("none") - add_cxxflags("-Og") + if not is_plat("windows") then + add_cxxflags("-Og") + end add_defines("DEBUG") end