From b510ad3fc662b300fd7a0c5ccd3d451163c15ba3 Mon Sep 17 00:00:00 2001 From: Justin Scofield <47263509+scawful@users.noreply.github.com> Date: Sun, 7 Aug 2022 13:25:34 -0400 Subject: [PATCH 1/7] feat: add initial hook parameter with default val * untested --- src/app/asm/script.cc | 19 +++++++++++++++---- src/app/asm/script.h | 3 ++- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/app/asm/script.cc b/src/app/asm/script.cc index 6203353f..e65c94c6 100644 --- a/src/app/asm/script.cc +++ b/src/app/asm/script.cc @@ -76,7 +76,8 @@ absl::Status Script::ApplyPatchToROM(ROM &rom) { } absl::Status Script::GenerateMosaicChangeAssembly( - ROM &rom, char mosaic_tiles[core::kNumOverworldMaps], int routine_offset) { + ROM &rom, char mosaic_tiles[core::kNumOverworldMaps], int routine_offset, + int hook_offset) { std::fstream file("assets/asm/mosaic_change.asm", std::ios::out | std::ios::in); if (!file.is_open()) { @@ -89,9 +90,19 @@ absl::Status Script::GenerateMosaicChangeAssembly( file.close(); auto assembly_string = assembly.str(); - if (!string_replace(assembly_string, "", kMosaicChangeOffset)) { - return absl::InternalError( - "Mosaic template did not have proper `` to replace."); + + if (!hook_offset) { + // TODO: TESTME + if (!string_replace(assembly_string, "", + absl::StrFormat("$%06x", hook_offset))) { + return absl::InternalError( + "Mosaic template did not have proper `` to replace."); + } + } else { + if (!string_replace(assembly_string, "", kMosaicChangeOffset)) { + return absl::InternalError( + "Mosaic template did not have proper `` to replace."); + } } if (!string_replace( diff --git a/src/app/asm/script.h b/src/app/asm/script.h index e5edcfe9..c0eb30ac 100644 --- a/src/app/asm/script.h +++ b/src/app/asm/script.h @@ -28,7 +28,8 @@ class Script { Script() { asar_init_with_dll_path("assets/libasar.dll"); } absl::Status GenerateMosaicChangeAssembly( - ROM& rom, char mosaic_tiles[core::kNumOverworldMaps], int routine_offset); + ROM& rom, char mosaic_tiles[core::kNumOverworldMaps], int routine_offset, + int hook_offset = 0); private: absl::Status ApplyPatchToROM(ROM& rom); From 713edac333530e728874ded9890c65e3f965f1de Mon Sep 17 00:00:00 2001 From: Justin Scofield <47263509+scawful@users.noreply.github.com> Date: Sun, 7 Aug 2022 13:46:44 -0400 Subject: [PATCH 2/7] chore: add error checking to ApplyPatchToROM --- src/app/asm/script.cc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/app/asm/script.cc b/src/app/asm/script.cc index e65c94c6..2affd9b1 100644 --- a/src/app/asm/script.cc +++ b/src/app/asm/script.cc @@ -64,6 +64,10 @@ std::string GenerateBytePool(char mosaic_tiles[core::kNumOverworldMaps]) { } absl::Status Script::ApplyPatchToROM(ROM &rom) { + if (patch_contents_.empty() || patch_filename_.empty()) { + return absl::InvalidArgumentError("No patch loaded!"); + } + char *data = (char *)rom.data(); int size = rom.GetSize(); int count = 0; From 513b2d1034bd10446a23f73847363d97f20f7b21 Mon Sep 17 00:00:00 2001 From: Justin Scofield <47263509+scawful@users.noreply.github.com> Date: Sun, 7 Aug 2022 13:46:58 -0400 Subject: [PATCH 3/7] feat: Create virtual class for mock testing --- src/app/asm/script.h | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/app/asm/script.h b/src/app/asm/script.h index c0eb30ac..61be3609 100644 --- a/src/app/asm/script.h +++ b/src/app/asm/script.h @@ -23,17 +23,21 @@ namespace snes_asm { const std::string kMosaicChangeOffset = "$02AADB"; constexpr int kSNESToPCOffset = 0x138000; -class Script { +class ScriptTemplate { + public: + virtual absl::Status ApplyPatchToROM(ROM& rom) = 0; +}; + +class Script : public ScriptTemplate { public: Script() { asar_init_with_dll_path("assets/libasar.dll"); } + absl::Status ApplyPatchToROM(ROM& rom) override; absl::Status GenerateMosaicChangeAssembly( ROM& rom, char mosaic_tiles[core::kNumOverworldMaps], int routine_offset, int hook_offset = 0); private: - absl::Status ApplyPatchToROM(ROM& rom); - int64_t patch_size_; std::string patch_filename_; std::string patch_contents_; From 1db18b1d2ad623fde14ce76a3c54655c229e960b Mon Sep 17 00:00:00 2001 From: Justin Scofield <47263509+scawful@users.noreply.github.com> Date: Sun, 7 Aug 2022 13:47:16 -0400 Subject: [PATCH 4/7] test: add asm_test for snes_asm::Script --- test/CMakeLists.txt | 5 ++++- test/asm_test.cc | 48 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 test/asm_test.cc diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 66023f68..76c7c061 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -14,18 +14,21 @@ add_executable( yaze_test yaze_test.cc rom_test.cc + asm_test.cc ../src/app/rom.cc ../src/app/asm/script.cc ../src/app/gfx/bitmap.cc ../src/app/gfx/snes_tile.cc ../src/app/gfx/snes_palette.cc ../src/app/core/common.cc + ../src/lib/asar/src/asar-dll-bindings/c/asardll.c ) target_include_directories( yaze_test PUBLIC - ../src/lib/ ../src/ + ../src/lib/ + ../src/lib/asar/src/asar-dll-bindings/c ${SDL_INCLUDE_DIRS} ) diff --git a/test/asm_test.cc b/test/asm_test.cc new file mode 100644 index 00000000..cff67106 --- /dev/null +++ b/test/asm_test.cc @@ -0,0 +1,48 @@ +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/string_view.h" +#include "app/asm/script.h" +#include "app/core/constants.h" +#include "app/rom.h" + +namespace yaze_test { +namespace asm_test { + +using yaze::app::ROM; +using yaze::app::snes_asm::Script; + +using ::testing::_; +using ::testing::ElementsAreArray; +using ::testing::Return; +using ::testing::TypedEq; + +class MockScript : public Script { + public: + MOCK_METHOD(absl::Status, ApplyPatchToROM, (ROM & rom)); +}; + +TEST(ASMTest, NoPatchLoadedError) { + ROM rom; + MockScript script; + EXPECT_CALL(script, ApplyPatchToROM(_)) + .WillOnce(Return(absl::InvalidArgumentError("No patch loaded!"))); + + EXPECT_THAT(script.ApplyPatchToROM(rom), + absl::InvalidArgumentError("No patch loaded!")); +} + +TEST(ASMTest, ApplyPatchOk) {} + +} // namespace asm_test +} // namespace yaze_test \ No newline at end of file From 1a6c82ec89a1d06cdf7109b5c0433fd173bcb00b Mon Sep 17 00:00:00 2001 From: Justin Scofield <47263509+scawful@users.noreply.github.com> Date: Sun, 7 Aug 2022 13:51:04 -0400 Subject: [PATCH 5/7] fix: save patch contents when generated --- src/app/asm/script.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/src/app/asm/script.cc b/src/app/asm/script.cc index 2affd9b1..ee0687e0 100644 --- a/src/app/asm/script.cc +++ b/src/app/asm/script.cc @@ -117,6 +117,7 @@ absl::Status Script::GenerateMosaicChangeAssembly( } assembly_string += GenerateBytePool(mosaic_tiles); + patch_contents_ = assembly_string; patch_filename_ = "assets/asm/mosaic_change_generated.asm"; std::ofstream new_file(patch_filename_, std::ios::out); if (new_file.is_open()) { From 395ec4887ccf6077ed71a9ecf4abdb3103d90d1c Mon Sep 17 00:00:00 2001 From: Justin Scofield <47263509+scawful@users.noreply.github.com> Date: Sun, 7 Aug 2022 14:45:37 -0400 Subject: [PATCH 6/7] test: add mosaic generation sanity test --- test/asm_test.cc | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/test/asm_test.cc b/test/asm_test.cc index cff67106..6ce8cfcc 100644 --- a/test/asm_test.cc +++ b/test/asm_test.cc @@ -24,14 +24,34 @@ using yaze::app::snes_asm::Script; using ::testing::_; using ::testing::ElementsAreArray; +using ::testing::Eq; using ::testing::Return; -using ::testing::TypedEq; class MockScript : public Script { public: MOCK_METHOD(absl::Status, ApplyPatchToROM, (ROM & rom)); + MOCK_METHOD(absl::Status, GenerateMosaicChangeAssembly, + (ROM & rom, char mosaic_tiles[yaze::app::core::kNumOverworldMaps], + int routine_offset, int hook_offset)); }; +TEST(ASMTest, ApplyMosaicChangePatchOk) { + ROM rom; + MockScript script; + char mosaic_tiles[yaze::app::core::kNumOverworldMaps]; + + EXPECT_CALL(script, GenerateMosaicChangeAssembly(_, Eq(mosaic_tiles), + Eq(0x1301D0 + 0x138000), 0)) + .WillOnce(Return(absl::OkStatus())); + + EXPECT_CALL(script, ApplyPatchToROM(_)).WillOnce(Return(absl::OkStatus())); + + EXPECT_THAT(script.GenerateMosaicChangeAssembly(rom, mosaic_tiles, + 0x1301D0 + 0x138000, 0), + absl::OkStatus()); + EXPECT_THAT(script.ApplyPatchToROM(rom), absl::OkStatus()); +} + TEST(ASMTest, NoPatchLoadedError) { ROM rom; MockScript script; @@ -42,7 +62,5 @@ TEST(ASMTest, NoPatchLoadedError) { absl::InvalidArgumentError("No patch loaded!")); } -TEST(ASMTest, ApplyPatchOk) {} - } // namespace asm_test } // namespace yaze_test \ No newline at end of file From c6b6eb74bb22846981a865a99435c9f067ca3b96 Mon Sep 17 00:00:00 2001 From: Justin Scofield <47263509+scawful@users.noreply.github.com> Date: Sun, 7 Aug 2022 14:46:26 -0400 Subject: [PATCH 7/7] chore: add virtual method for mock test --- src/app/asm/script.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/app/asm/script.h b/src/app/asm/script.h index 61be3609..4b8e4a7c 100644 --- a/src/app/asm/script.h +++ b/src/app/asm/script.h @@ -26,6 +26,9 @@ constexpr int kSNESToPCOffset = 0x138000; class ScriptTemplate { public: virtual absl::Status ApplyPatchToROM(ROM& rom) = 0; + virtual absl::Status GenerateMosaicChangeAssembly( + ROM& rom, char mosaic_tiles[core::kNumOverworldMaps], int routine_offset, + int hook_offset = 0) = 0; }; class Script : public ScriptTemplate { @@ -35,7 +38,7 @@ class Script : public ScriptTemplate { absl::Status ApplyPatchToROM(ROM& rom) override; absl::Status GenerateMosaicChangeAssembly( ROM& rom, char mosaic_tiles[core::kNumOverworldMaps], int routine_offset, - int hook_offset = 0); + int hook_offset = 0) override; private: int64_t patch_size_;