diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 41a7589..f63b745 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -3,8 +3,36 @@ name: Build on: [push, pull_request] jobs: + test: + runs-on: windows-latest + + steps: + # setup + - uses: actions/checkout@v4 + with: + submodules: true + + - uses: microsoft/setup-msbuild@v1.1 + - uses: ilammy/msvc-dev-cmd@v1 + + - name: Download premake5 + run: | + curl.exe -o premake5.zip -L https://github.com/premake/premake-core/releases/download/v5.0.0-beta2/premake-5.0.0-beta2-windows.zip + tar -xf premake5.zip + + - name: Generate project files + run: .\premake5 vs2022 --with-tests + + # tests + - name: Build tests + run: MSBuild TRAE-menu-hook.sln /t:Tests /p:Configuration=Release /p:Platform=TR7 + + - name: Test + run: .\bin\TR7\Release\Tests.exe + build: runs-on: windows-latest + needs: test steps: # setup @@ -29,16 +57,23 @@ jobs: run: .\premake5 vs2022 # compile + - name: Build Legend + run: MSBuild /p:Configuration=Release /p:Platform=TR7 + - name: Build Anniversary run: MSBuild /p:Configuration=Release /p:Platform=TRAE - name: Build Underworld run: MSBuild /p:Configuration=Release /p:Platform=TR8 - - name: Build Legend - run: MSBuild /p:Configuration=Release /p:Platform=TR7 - # upload + - uses: actions/upload-artifact@v3 + with: + name: Legend + path: | + bin/TR7/Release/TR7-Menu-Hook.asi + bin/TR7/Release/TR7-Menu-Hook.pdb + - uses: actions/upload-artifact@v3 with: name: Anniversary @@ -52,10 +87,3 @@ jobs: path: | bin/TR8/Release/TR8-Menu-Hook.asi bin/TR8/Release/TR8-Menu-Hook.pdb - - - uses: actions/upload-artifact@v3 - with: - name: Legend - path: | - bin/TR7/Release/TR7-Menu-Hook.asi - bin/TR7/Release/TR7-Menu-Hook.pdb diff --git a/.gitmodules b/.gitmodules index 0bc59d2..8a3afc8 100644 --- a/.gitmodules +++ b/.gitmodules @@ -7,3 +7,6 @@ [submodule "vendor/minhook"] path = vendor/minhook url = https://github.com/TsudaKageyu/minhook +[submodule "vendor/catch2"] + path = vendor/catch2 + url = https://github.com/catchorg/Catch2 diff --git a/premake5.lua b/premake5.lua index 1d2e1e1..a390560 100644 --- a/premake5.lua +++ b/premake5.lua @@ -1,8 +1,14 @@ +newoption { + trigger = "with-tests", + description = "Include the tests project" +} + workspace "TRAE-menu-hook" architecture "x86" configurations { "Debug", "Release" } platforms { "TR7", "TRAE", "TR8" } +-- Main project project "TRAE-menu-hook" kind "SharedLib" targetextension ".asi" @@ -12,25 +18,12 @@ project "TRAE-menu-hook" links { "d3d9.lib" } - -- Source files' + -- Source files files "src/**" includedirs { "src" } -- Vendor files - files { - "vendor/minhook/src/**", - "vendor/patterns/*.cpp", - "vendor/imgui/*.cpp", - "vendor/imgui/backends/imgui_impl_win32.cpp", - "vendor/imgui/backends/imgui_impl_dx9.cpp" - } - - includedirs { - "vendor/minhook/include", - "vendor/patterns", - "vendor/imgui", - "vendor/imgui/backends" - } + dofile "vendor.lua" defines { "IMGUI_IMPL_WIN32_DISABLE_GAMEPAD" } @@ -56,3 +49,33 @@ project "TRAE-menu-hook" filter "platforms:TR8" defines { "TR8" } targetname "TR8-Menu-Hook" + +-- Tests +if _OPTIONS["with-tests"] then + +project "Tests" + kind "ConsoleApp" + + language "C++" + cppdialect "C++17" + + files { + "tests/**", + "src/**", + "vendor/catch2/extras/catch_amalgamated.cpp" + } + + includedirs { "src", "vendor/catch2/extras" } + dofile "vendor.lua" + + filter "configurations:Debug" + defines { "DEBUG", "_DEBUG" } + + filter "configurations:Release" + defines { "NDEBUG" } + optimize "On" + + -- Define this as dummy game + defines { "TR7" } + +end \ No newline at end of file diff --git a/src/Hook.cpp b/src/Hook.cpp index 327f725..6e80c08 100644 --- a/src/Hook.cpp +++ b/src/Hook.cpp @@ -118,14 +118,6 @@ void Hook::OnDevice() PostInitialize(); } -template -void Hook::RegisterModule() -{ - auto mod = std::make_shared(); - - m_modules.insert({ typeid(T).hash_code(), mod }); -} - void Hook::RegisterModules() { // Register these first diff --git a/src/Hook.h b/src/Hook.h index c3c94be..cdc8af3 100644 --- a/src/Hook.h +++ b/src/Hook.h @@ -14,8 +14,6 @@ class Hook void PostInitialize(); - template - void RegisterModule(); void RegisterModules(); void OnMessage(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); @@ -30,6 +28,14 @@ class Hook // These need to be defined here, else the linker becomes angry + // Adds a module + template + void RegisterModule() + { + auto mod = std::make_shared(); + m_modules.insert({ typeid(T).hash_code(), mod }); + } + // Gets all modules const auto& GetModules() const noexcept { diff --git a/src/util/Helpers.h b/src/util/Helpers.h index 1d76388..d5ad377 100644 --- a/src/util/Helpers.h +++ b/src/util/Helpers.h @@ -6,5 +6,5 @@ class Helpers { public: // Converts a value to integer with support for units such as megabytes - static int StringToInt(const std::string& value, int defaultValue); + static int StringToInt(const std::string& value, int defaultValue = 0); }; \ No newline at end of file diff --git a/tests/TestHelpers.cpp b/tests/TestHelpers.cpp new file mode 100644 index 0000000..b5c5603 --- /dev/null +++ b/tests/TestHelpers.cpp @@ -0,0 +1,29 @@ +#include + +#include "util/Helpers.h" + +TEST_CASE("can convert string to int") +{ + SECTION("read numbers") + { + auto value = Helpers::StringToInt("1024"); + + REQUIRE(value == 1024); + } + + SECTION("read units") + { + auto value1 = Helpers::StringToInt("256K"); + auto value2 = Helpers::StringToInt("256M"); + + REQUIRE(value1 == 0x40000); + REQUIRE(value2 == 0x10000000); + } + + SECTION("returns a default") + { + auto value = Helpers::StringToInt("lara", 42); + + REQUIRE(value == 42); + } +} \ No newline at end of file diff --git a/tests/TestModules.cpp b/tests/TestModules.cpp new file mode 100644 index 0000000..34c618e --- /dev/null +++ b/tests/TestModules.cpp @@ -0,0 +1,23 @@ +#include + +#include "Hook.h" + +class TestModule : public Module +{ +public: + int GetValue() const noexcept { return 42; } +}; + +TEST_CASE("can register and get modules") +{ + auto& hook = Hook::GetInstance(); + hook.RegisterModule(); + + auto test = hook.GetModule(); + + REQUIRE_FALSE(test == nullptr); + + auto value = test->GetValue(); + + REQUIRE(value == 42); +} \ No newline at end of file diff --git a/vendor.lua b/vendor.lua new file mode 100644 index 0000000..7c43400 --- /dev/null +++ b/vendor.lua @@ -0,0 +1,14 @@ +files { + "vendor/minhook/src/**", + "vendor/patterns/*.cpp", + "vendor/imgui/*.cpp", + "vendor/imgui/backends/imgui_impl_win32.cpp", + "vendor/imgui/backends/imgui_impl_dx9.cpp" +} + +includedirs { + "vendor/minhook/include", + "vendor/patterns", + "vendor/imgui", + "vendor/imgui/backends" +} \ No newline at end of file diff --git a/vendor/catch2 b/vendor/catch2 new file mode 160000 index 0000000..4e8d92b --- /dev/null +++ b/vendor/catch2 @@ -0,0 +1 @@ +Subproject commit 4e8d92bf02f7d1c8006a0e7a5ecabd8e62d98502