diff --git a/src/app/core/common.cc b/src/app/core/common.cc index f43dda40..b1436b41 100644 --- a/src/app/core/common.cc +++ b/src/app/core/common.cc @@ -55,50 +55,12 @@ uint32_t crc32(const std::vector &data) { return ::crc32(crc, data.data(), data.size()); } -} // namespace - -std::shared_ptr ExperimentFlags::flags_; - -// Initialize the static member -std::stack ImGuiIdIssuer::idStack; - -uint32_t Get24LocalFromPC(uint8_t *data, int addr, bool pc) { - uint32_t ret = - (PcToSnes(addr) & 0xFF0000) | (data[addr + 1] << 8) | data[addr]; - if (pc) { - return SnesToPc(ret); - } - return ret; -} - -// hextodec has been imported from SNESDisasm to parse hex numbers -int HexToDec(char *input, int length) { - int result = 0; - int value; - int ceiling = length - 1; - int power16 = 16; - - int j = ceiling; - - for (; j >= 0; j--) { - if (input[j] >= 'A' && input[j] <= 'F') { - value = input[j] - 'F'; - value += 15; - } else { - value = input[j] - '9'; - value += 9; - } - - if (j == ceiling) { - result += value; - continue; - } - - result += (value * power16); - power16 *= 16; - } - - return result; +// "load little endian value at the given byte offset and shift to get its +// value relative to the base offset (powers of 256, essentially)" +unsigned ldle(uint8_t const *const p_arr, unsigned const p_index) { + uint32_t v = p_arr[p_index]; + v <<= (8 * p_index); + return v; } void stle(uint8_t *const p_arr, size_t const p_index, unsigned const p_val) { @@ -122,24 +84,6 @@ void stle2(uint8_t *const p_arr, unsigned const p_val) { void stle3(uint8_t *const p_arr, unsigned const p_val) { stle(p_arr, 3, p_val); } -void stle16b(uint8_t *const p_arr, uint16_t const p_val) { - stle0(p_arr, p_val); - stle1(p_arr, p_val); -} - -void stle16b_i(uint8_t *const p_arr, size_t const p_index, - uint16_t const p_val) { - stle16b(p_arr + (p_index * 2), p_val); -} -// "load little endian value at the given byte offset and shift to get its -// value relative to the base offset (powers of 256, essentially)" -unsigned ldle(uint8_t const *const p_arr, unsigned const p_index) { - uint32_t v = p_arr[p_index]; - - v <<= (8 * p_index); - - return v; -} // Helper function to get the first byte in a little endian number uint32_t ldle0(uint8_t const *const p_arr) { return ldle(p_arr, 0); } @@ -153,11 +97,32 @@ uint32_t ldle2(uint8_t const *const p_arr) { return ldle(p_arr, 2); } // Helper function to get the third byte in a little endian number uint32_t ldle3(uint8_t const *const p_arr) { return ldle(p_arr, 3); } +} // namespace + +std::shared_ptr ExperimentFlags::flags_; + +uint32_t Get24LocalFromPC(uint8_t *data, int addr, bool pc) { + uint32_t ret = + (PcToSnes(addr) & 0xFF0000) | (data[addr + 1] << 8) | data[addr]; + if (pc) { + return SnesToPc(ret); + } + return ret; +} + +void stle16b_i(uint8_t *const p_arr, size_t const p_index, + uint16_t const p_val) { + stle16b(p_arr + (p_index * 2), p_val); +} + +void stle16b(uint8_t *const p_arr, uint16_t const p_val) { + stle0(p_arr, p_val); + stle1(p_arr, p_val); +} + uint16_t ldle16b(uint8_t const *const p_arr) { uint16_t v = 0; - v |= (ldle0(p_arr) | ldle1(p_arr)); - return v; } @@ -175,82 +140,83 @@ void CreateBpsPatch(const std::vector &source, encode(target.size(), patch); encode(0, patch); // No metadata - size_t sourceOffset = 0; - size_t targetOffset = 0; - int64_t sourceRelOffset = 0; - int64_t targetRelOffset = 0; + size_t source_offset = 0; + size_t target_offset = 0; + int64_t source_rel_offset = 0; + int64_t target_rel_offset = 0; - while (targetOffset < target.size()) { - if (sourceOffset < source.size() && - source[sourceOffset] == target[targetOffset]) { + while (target_offset < target.size()) { + if (source_offset < source.size() && + source[source_offset] == target[target_offset]) { size_t length = 0; - while (sourceOffset + length < source.size() && - targetOffset + length < target.size() && - source[sourceOffset + length] == target[targetOffset + length]) { + while (source_offset + length < source.size() && + target_offset + length < target.size() && + source[source_offset + length] == target[target_offset + length]) { length++; } encode((length - 1) << 2 | 0, patch); // SourceRead - sourceOffset += length; - targetOffset += length; + source_offset += length; + target_offset += length; } else { size_t length = 0; - while (targetOffset + length < target.size() && - (sourceOffset + length >= source.size() || - source[sourceOffset + length] != target[targetOffset + length])) { + while ( + target_offset + length < target.size() && + (source_offset + length >= source.size() || + source[source_offset + length] != target[target_offset + length])) { length++; } if (length > 0) { encode((length - 1) << 2 | 1, patch); // TargetRead for (size_t i = 0; i < length; i++) { - patch.push_back(target[targetOffset + i]); + patch.push_back(target[target_offset + i]); } - targetOffset += length; + target_offset += length; } } // SourceCopy - if (sourceOffset < source.size()) { + if (source_offset < source.size()) { size_t length = 0; - int64_t offset = sourceOffset - sourceRelOffset; - while (sourceOffset + length < source.size() && - targetOffset + length < target.size() && - source[sourceOffset + length] == target[targetOffset + length]) { + int64_t offset = source_offset - source_rel_offset; + while (source_offset + length < source.size() && + target_offset + length < target.size() && + source[source_offset + length] == target[target_offset + length]) { length++; } if (length > 0) { encode((length - 1) << 2 | 2, patch); encode((offset < 0 ? 1 : 0) | (abs(offset) << 1), patch); - sourceOffset += length; - targetOffset += length; - sourceRelOffset = sourceOffset; + source_offset += length; + target_offset += length; + source_rel_offset = source_offset; } } // TargetCopy - if (targetOffset > 0) { + if (target_offset > 0) { size_t length = 0; - int64_t offset = targetOffset - targetRelOffset; - while (targetOffset + length < target.size() && - target[targetOffset - 1] == target[targetOffset + length]) { + int64_t offset = target_offset - target_rel_offset; + while (target_offset + length < target.size() && + target[target_offset - 1] == target[target_offset + length]) { length++; } if (length > 0) { encode((length - 1) << 2 | 3, patch); encode((offset < 0 ? 1 : 0) | (abs(offset) << 1), patch); - targetOffset += length; - targetRelOffset = targetOffset; + target_offset += length; + target_rel_offset = target_offset; } } } patch.resize(patch.size() + 12); // Make space for the checksums - uint32_t sourceChecksum = crc32(source); - uint32_t targetChecksum = crc32(target); - uint32_t patchChecksum = crc32(patch); + uint32_t source_checksum = crc32(source); + uint32_t target_checksum = crc32(target); + uint32_t patch_checksum = crc32(patch); - memcpy(patch.data() + patch.size() - 12, &sourceChecksum, sizeof(uint32_t)); - memcpy(patch.data() + patch.size() - 8, &targetChecksum, sizeof(uint32_t)); - memcpy(patch.data() + patch.size() - 4, &patchChecksum, sizeof(uint32_t)); + memcpy(patch.data() + patch.size() - 12, &source_checksum, sizeof(uint32_t)); + memcpy(patch.data() + patch.size() - 8, &target_checksum, sizeof(uint32_t)); + memcpy(patch.data() + patch.size() - 4, &patch_checksum, sizeof(uint32_t)); } void ApplyBpsPatch(const std::vector &source, @@ -261,48 +227,48 @@ void ApplyBpsPatch(const std::vector &source, throw std::runtime_error("Invalid patch format"); } - size_t patchOffset = 4; - uint64_t sourceSize = decode(patch, patchOffset); - uint64_t targetSize = decode(patch, patchOffset); - uint64_t metadataSize = decode(patch, patchOffset); - patchOffset += metadataSize; + size_t patch_offset = 4; + uint64_t source_size = decode(patch, patch_offset); + uint64_t target_size = decode(patch, patch_offset); + uint64_t metadata_size = decode(patch, patch_offset); + patch_offset += metadata_size; - target.resize(targetSize); - size_t sourceOffset = 0; - size_t targetOffset = 0; - int64_t sourceRelOffset = 0; - int64_t targetRelOffset = 0; + target.resize(target_size); + size_t source_offset = 0; + size_t target_offset = 0; + int64_t source_rel_offset = 0; + int64_t target_rel_offset = 0; - while (patchOffset < patch.size() - 12) { - uint64_t data = decode(patch, patchOffset); + while (patch_offset < patch.size() - 12) { + uint64_t data = decode(patch, patch_offset); uint64_t command = data & 3; uint64_t length = (data >> 2) + 1; switch (command) { case 0: // SourceRead while (length--) { - target[targetOffset++] = source[sourceOffset++]; + target[target_offset++] = source[source_offset++]; } break; case 1: // TargetRead while (length--) { - target[targetOffset++] = patch[patchOffset++]; + target[target_offset++] = patch[patch_offset++]; } break; case 2: // SourceCopy { - int64_t offsetData = decode(patch, patchOffset); - sourceRelOffset += (offsetData & 1 ? -1 : +1) * (offsetData >> 1); + int64_t offsetData = decode(patch, patch_offset); + source_rel_offset += (offsetData & 1 ? -1 : +1) * (offsetData >> 1); while (length--) { - target[targetOffset++] = source[sourceRelOffset++]; + target[target_offset++] = source[source_rel_offset++]; } } break; case 3: // TargetCopy { - uint64_t offsetData = decode(patch, patchOffset); - targetRelOffset += (offsetData & 1 ? -1 : +1) * (offsetData >> 1); + uint64_t offsetData = decode(patch, patch_offset); + target_rel_offset += (offsetData & 1 ? -1 : +1) * (offsetData >> 1); while (length--) { - target[targetOffset++] = target[targetRelOffset++]; + target[target_offset++] = target[target_rel_offset++]; } } default: @@ -310,15 +276,15 @@ void ApplyBpsPatch(const std::vector &source, } } - uint32_t sourceChecksum; - uint32_t targetChecksum; - uint32_t patchChecksum; - memcpy(&sourceChecksum, patch.data() + patch.size() - 12, sizeof(uint32_t)); - memcpy(&targetChecksum, patch.data() + patch.size() - 8, sizeof(uint32_t)); - memcpy(&patchChecksum, patch.data() + patch.size() - 4, sizeof(uint32_t)); + uint32_t source_checksum; + uint32_t target_checksum; + uint32_t patch_checksum; + memcpy(&source_checksum, patch.data() + patch.size() - 12, sizeof(uint32_t)); + memcpy(&target_checksum, patch.data() + patch.size() - 8, sizeof(uint32_t)); + memcpy(&patch_checksum, patch.data() + patch.size() - 4, sizeof(uint32_t)); - if (sourceChecksum != crc32(source) || targetChecksum != crc32(target) || - patchChecksum != + if (source_checksum != crc32(source) || target_checksum != crc32(target) || + patch_checksum != crc32(std::vector(patch.begin(), patch.end() - 4))) { throw std::runtime_error("Checksum mismatch"); } diff --git a/src/app/core/common.h b/src/app/core/common.h index 609c7bd9..7d810c51 100644 --- a/src/app/core/common.h +++ b/src/app/core/common.h @@ -159,27 +159,6 @@ class NotifyValue { T temp_value_; }; -class ImGuiIdIssuer { - private: - static std::stack idStack; - - public: - // Generate and push a new ID onto the stack - static ImGuiID GetNewID() { - static int counter = 1; // Start from 1 to ensure uniqueness - ImGuiID child_id = ImGui::GetID((void *)(intptr_t)counter++); - idStack.push(child_id); - return child_id; - } - - // Pop all IDs from the stack (can be called explicitly or upon program exit) - static void Cleanup() { - while (!idStack.empty()) { - idStack.pop(); - } - } -}; - static bool log_to_console = false; static std::string log_file_out = "log.txt"; @@ -236,23 +215,25 @@ inline uint32_t MapBankToWordAddress(uint8_t bank, uint16_t addr) noexcept { uint32_t Get24LocalFromPC(uint8_t *data, int addr, bool pc = true); -int HexToDec(char *input, int length); - -// "Store little endian 16-bit value using a byte pointer, offset by an -// index before dereferencing" +/** + * @brief Store little endian 16-bit value using a byte pointer, offset by an + * index before dereferencing + */ void stle16b_i(uint8_t *const p_arr, size_t const p_index, uint16_t const p_val); -// Load little endian halfword (16-bit) dereferenced from an arrays of bytes. -// This version provides an index that will be multiplied by 2 and added to the -// base address. +void stle16b(uint8_t *const p_arr, uint16_t const p_val); + +/** + * @brief Load little endian halfword (16-bit) dereferenced from an arrays of + * bytes. This version provides an index that will be multiplied by 2 and added + * to the base address. + */ uint16_t ldle16b_i(uint8_t const *const p_arr, size_t const p_index); // Load little endian halfword (16-bit) dereferenced from uint16_t ldle16b(uint8_t const *const p_arr); -void stle16b(uint8_t *const p_arr, uint16_t const p_val); - struct FolderItem { std::string name; std::vector subfolders; diff --git a/src/app/core/controller.h b/src/app/core/controller.h index 63360c74..e3261f5b 100644 --- a/src/app/core/controller.h +++ b/src/app/core/controller.h @@ -11,7 +11,7 @@ #include "imgui/imgui.h" #include "imgui/imgui_internal.h" #include "absl/status/status.h" -#include "app/core/common.h" + #include "app/core/platform/renderer.h" #include "app/editor/editor_manager.h" #include "app/editor/utils/editor.h" diff --git a/src/app/core/labeling.cc b/src/app/core/labeling.cc index 32a925ca..c2c2080a 100644 --- a/src/app/core/labeling.cc +++ b/src/app/core/labeling.cc @@ -8,7 +8,7 @@ #include #include "absl/strings/str_format.h" -#include "app/core/common.h" + #include "app/core/constants.h" #include "app/gui/icons.h" #include "imgui/imgui.h" diff --git a/src/app/core/labeling.h b/src/app/core/labeling.h index d7be52b5..ef509a76 100644 --- a/src/app/core/labeling.h +++ b/src/app/core/labeling.h @@ -10,7 +10,7 @@ #include "absl/status/status.h" #include "absl/status/statusor.h" -#include "app/core/common.h" + #include "app/core/constants.h" namespace yaze { diff --git a/src/app/core/message.h b/src/app/core/message.h index 0b477c31..8f08c219 100644 --- a/src/app/core/message.h +++ b/src/app/core/message.h @@ -21,6 +21,7 @@ struct Message { void* sender; std::any payload; + Message() = default; Message(const std::string& type, void* sender, const std::any& payload) : type(type), sender(sender), payload(payload) {} }; diff --git a/src/app/core/platform/renderer.h b/src/app/core/platform/renderer.h index a81f8e7f..d0115082 100644 --- a/src/app/core/platform/renderer.h +++ b/src/app/core/platform/renderer.h @@ -69,7 +69,8 @@ class Renderer : public ExperimentFlags { } absl::Status CreateAndRenderBitmap(int width, int height, int depth, - const std::vector& data, gfx::Bitmap& bitmap, + const std::vector& data, + gfx::Bitmap& bitmap, gfx::SnesPalette& palette) { bitmap.Create(width, height, depth, data); RETURN_IF_ERROR(bitmap.ApplyPalette(palette)); diff --git a/src/app/editor/code/assembly_editor.h b/src/app/editor/code/assembly_editor.h index 35022c99..b362efa3 100644 --- a/src/app/editor/code/assembly_editor.h +++ b/src/app/editor/code/assembly_editor.h @@ -1,13 +1,12 @@ #ifndef YAZE_APP_EDITOR_ASSEMBLY_EDITOR_H #define YAZE_APP_EDITOR_ASSEMBLY_EDITOR_H -#include "ImGuiColorTextEdit/TextEditor.h" -#include "ImGuiFileDialog/ImGuiFileDialog.h" - #include #include #include +#include "ImGuiColorTextEdit/TextEditor.h" +#include "ImGuiFileDialog/ImGuiFileDialog.h" #include "app/core/common.h" #include "app/editor/utils/editor.h" #include "app/gui/style.h" diff --git a/src/app/editor/code/memory_editor.h b/src/app/editor/code/memory_editor.h index 3fb5e086..d35c1bc4 100644 --- a/src/app/editor/code/memory_editor.h +++ b/src/app/editor/code/memory_editor.h @@ -1,12 +1,7 @@ #ifndef YAZE_APP_EDITOR_CODE_MEMORY_EDITOR_H #define YAZE_APP_EDITOR_CODE_MEMORY_EDITOR_H -#include "imgui/imgui.h" -#include "imgui/misc/cpp/imgui_stdlib.h" -#include "imgui_memory_editor.h" - #include "absl/status/status.h" -#include "app/core/common.h" #include "app/core/constants.h" #include "app/core/platform/file_dialog.h" #include "app/core/project.h" @@ -30,6 +25,9 @@ #include "app/gui/input.h" #include "app/gui/style.h" #include "app/rom.h" +#include "imgui/imgui.h" +#include "imgui/misc/cpp/imgui_stdlib.h" +#include "imgui_memory_editor.h" namespace yaze { namespace app { diff --git a/src/app/editor/dungeon/dungeon_editor.cc b/src/app/editor/dungeon/dungeon_editor.cc index b069f313..8a34c4e0 100644 --- a/src/app/editor/dungeon/dungeon_editor.cc +++ b/src/app/editor/dungeon/dungeon_editor.cc @@ -1,6 +1,5 @@ #include "dungeon_editor.h" -#include "app/core/common.h" #include "app/core/labeling.h" #include "app/core/platform/renderer.h" #include "app/gfx/snes_palette.h" diff --git a/src/app/editor/editor_manager.cc b/src/app/editor/editor_manager.cc index 7993ade8..acd5dc8f 100644 --- a/src/app/editor/editor_manager.cc +++ b/src/app/editor/editor_manager.cc @@ -4,7 +4,7 @@ #include "ImGuiFileDialog/ImGuiFileDialog.h" #include "abseil-cpp/absl/strings/match.h" #include "absl/status/status.h" -#include "app/core/common.h" + #include "app/core/constants.h" #include "app/core/platform/file_dialog.h" #include "app/core/platform/renderer.h" diff --git a/src/app/editor/editor_manager.h b/src/app/editor/editor_manager.h index bccb72ed..e0afefc7 100644 --- a/src/app/editor/editor_manager.h +++ b/src/app/editor/editor_manager.h @@ -6,7 +6,7 @@ #include "ImGuiColorTextEdit/TextEditor.h" #include "ImGuiFileDialog/ImGuiFileDialog.h" #include "absl/status/status.h" -#include "app/core/common.h" + #include "app/core/constants.h" #include "app/core/message.h" #include "app/core/project.h" diff --git a/src/app/editor/graphics/screen_editor.cc b/src/app/editor/graphics/screen_editor.cc index fa0767b7..7f31363b 100644 --- a/src/app/editor/graphics/screen_editor.cc +++ b/src/app/editor/graphics/screen_editor.cc @@ -9,7 +9,7 @@ #include "absl/status/statusor.h" #include "absl/strings/str_format.h" #include "absl/strings/string_view.h" -#include "app/core/common.h" + #include "app/core/constants.h" #include "app/core/platform/renderer.h" #include "app/gfx/bitmap.h" diff --git a/src/app/editor/graphics/tile16_editor.h b/src/app/editor/graphics/tile16_editor.h index a49833fc..65b047a9 100644 --- a/src/app/editor/graphics/tile16_editor.h +++ b/src/app/editor/graphics/tile16_editor.h @@ -5,6 +5,7 @@ #include "absl/status/status.h" #include "absl/status/statusor.h" +#include "app/core/common.h" #include "app/editor/graphics/palette_editor.h" #include "app/editor/utils/editor.h" #include "app/editor/utils/gfx_context.h" diff --git a/src/app/editor/message/message_editor.cc b/src/app/editor/message/message_editor.cc index 8559b782..da31d0c3 100644 --- a/src/app/editor/message/message_editor.cc +++ b/src/app/editor/message/message_editor.cc @@ -11,7 +11,7 @@ #include "absl/strings/str_format.h" #include "absl/strings/str_replace.h" #include "absl/strings/str_split.h" -#include "app/core/common.h" + #include "app/core/platform/renderer.h" #include "app/editor/utils/editor.h" #include "app/gfx/bitmap.h" diff --git a/src/app/editor/overworld/overworld_editor.cc b/src/app/editor/overworld/overworld_editor.cc index aeaea871..b8d88eaf 100644 --- a/src/app/editor/overworld/overworld_editor.cc +++ b/src/app/editor/overworld/overworld_editor.cc @@ -8,7 +8,7 @@ #include "absl/status/status.h" #include "absl/status/statusor.h" #include "absl/strings/str_format.h" -#include "app/core/common.h" + #include "app/core/constants.h" #include "app/core/platform/clipboard.h" #include "app/core/platform/renderer.h" diff --git a/src/app/editor/overworld/overworld_editor.h b/src/app/editor/overworld/overworld_editor.h index b9f1c36c..f70d94d3 100644 --- a/src/app/editor/overworld/overworld_editor.h +++ b/src/app/editor/overworld/overworld_editor.h @@ -8,7 +8,7 @@ #include "absl/status/status.h" #include "absl/status/statusor.h" #include "absl/strings/str_format.h" -#include "app/core/common.h" + #include "app/editor/graphics/gfx_group_editor.h" #include "app/editor/graphics/palette_editor.h" #include "app/editor/graphics/tile16_editor.h" diff --git a/src/app/gui/input.cc b/src/app/gui/input.cc index 9ceb7c07..c4ad7966 100644 --- a/src/app/gui/input.cc +++ b/src/app/gui/input.cc @@ -6,7 +6,7 @@ #include "ImGuiFileDialog/ImGuiFileDialog.h" #include "absl/strings/string_view.h" -#include "app/core/common.h" + #include "app/gfx/bitmap.h" #include "app/gfx/snes_palette.h" #include "app/gui/canvas.h" diff --git a/src/app/gui/input.h b/src/app/gui/input.h index cd7a024b..eb56235a 100644 --- a/src/app/gui/input.h +++ b/src/app/gui/input.h @@ -15,7 +15,7 @@ #include #include "absl/strings/string_view.h" -#include "app/core/common.h" + #include "app/gfx/bitmap.h" #include "app/gfx/snes_palette.h" #include "app/gui/canvas.h" diff --git a/src/app/zelda3/dungeon/room.cc b/src/app/zelda3/dungeon/room.cc index cba89cf5..040b2ddf 100644 --- a/src/app/zelda3/dungeon/room.cc +++ b/src/app/zelda3/dungeon/room.cc @@ -3,7 +3,6 @@ #include #include -#include "app/core/common.h" #include "app/core/constants.h" #include "app/gfx/bitmap.h" #include "app/gfx/snes_palette.h" @@ -72,9 +71,8 @@ void Room::LoadHeader() { // Existing room size address calculation... auto room_size_address = 0xF8000 + (room_id_ * 3); - if (flags()->kLogToConsole) - std::cout << "Room #" << room_id_ << " Address: " << std::hex - << room_size_address << std::endl; + std::cout << "Room #" << room_id_ << " Address: " << std::hex + << room_size_address << std::endl; // Reading bytes for long address construction uint8_t low = rom()->data()[room_size_address]; @@ -83,15 +81,13 @@ void Room::LoadHeader() { // Constructing the long address int long_address = (bank << 16) | (high << 8) | low; - if (flags()->kLogToConsole) - std::cout << std::hex << std::setfill('0') << std::setw(6) << long_address - << std::endl; + std::cout << std::hex << std::setfill('0') << std::setw(6) << long_address + << std::endl; room_size_pointer_ = long_address; if (long_address == 0x0A8000) { // Blank room disregard in size calculation - if (flags()->kLogToConsole) - std::cout << "Size of Room #" << room_id_ << ": 0 bytes" << std::endl; + std::cout << "Size of Room #" << room_id_ << ": 0 bytes" << std::endl; room_size_ = 0; } else { // use the long address to calculate the size of the room @@ -100,9 +96,8 @@ void Room::LoadHeader() { int next_room_address = 0xF8000 + ((room_id_ + 1) * 3); - if (flags()->kLogToConsole) - std::cout << "Next Room Address: " << std::hex << next_room_address - << std::endl; + std::cout << "Next Room Address: " << std::hex << next_room_address + << std::endl; // Reading bytes for long address construction uint8_t next_low = rom()->data()[next_room_address]; @@ -112,19 +107,17 @@ void Room::LoadHeader() { // Constructing the long address int next_long_address = (next_bank << 16) | (next_high << 8) | next_low; - if (flags()->kLogToConsole) - std::cout << std::hex << std::setfill('0') << std::setw(6) - << next_long_address << std::endl; + std::cout << std::hex << std::setfill('0') << std::setw(6) + << next_long_address << std::endl; // Calculate the size of the room int room_size = next_long_address - long_address; room_size_ = room_size; - if (flags()->kLogToConsole) - std::cout << "Size of Room #" << room_id_ << ": " << std::dec - << room_size << " bytes" << std::endl; + std::cout << "Size of Room #" << room_id_ << ": " << std::dec << room_size + << " bytes" << std::endl; } } catch (const std::exception& e) { - if (flags()->kLogToConsole) std::cout << "Error: " << e.what() << std::endl; + std::cout << "Error: " << e.what() << std::endl; } } diff --git a/src/app/zelda3/dungeon/room.h b/src/app/zelda3/dungeon/room.h index f58b7075..680a9f8e 100644 --- a/src/app/zelda3/dungeon/room.h +++ b/src/app/zelda3/dungeon/room.h @@ -4,7 +4,6 @@ #include #include -#include "app/core/common.h" #include "app/core/constants.h" #include "app/gfx/bitmap.h" #include "app/gfx/snes_palette.h" @@ -118,7 +117,7 @@ struct ChestData { struct StaircaseRooms {}; -class Room : public SharedRom, public core::ExperimentFlags { +class Room : public SharedRom { public: Room() = default; Room(int room_id) : room_id_(room_id) {} diff --git a/src/app/zelda3/music/tracker.h b/src/app/zelda3/music/tracker.h index f7ebb212..3d70b452 100644 --- a/src/app/zelda3/music/tracker.h +++ b/src/app/zelda3/music/tracker.h @@ -7,7 +7,7 @@ #include #include "absl/status/status.h" -#include "app/core/common.h" + #include "app/core/constants.h" #include "app/gfx/bitmap.h" #include "app/gfx/snes_tile.h" diff --git a/src/app/zelda3/overworld/overworld.cc b/src/app/zelda3/overworld/overworld.cc index f7ee44bc..d3c8a394 100644 --- a/src/app/zelda3/overworld/overworld.cc +++ b/src/app/zelda3/overworld/overworld.cc @@ -11,7 +11,7 @@ #include "absl/container/flat_hash_map.h" #include "absl/status/status.h" -#include "app/core/common.h" + #include "app/core/constants.h" #include "app/gfx/bitmap.h" #include "app/gfx/compression.h" diff --git a/src/app/zelda3/overworld/overworld_map.cc b/src/app/zelda3/overworld/overworld_map.cc index 8ff629d5..dbf8c31e 100644 --- a/src/app/zelda3/overworld/overworld_map.cc +++ b/src/app/zelda3/overworld/overworld_map.cc @@ -6,7 +6,7 @@ #include #include -#include "app/core/common.h" + #include "app/editor/utils/gfx_context.h" #include "app/gfx/bitmap.h" #include "app/gfx/snes_tile.h" diff --git a/src/app/zelda3/overworld/overworld_map.h b/src/app/zelda3/overworld/overworld_map.h index 0ca431ef..5135ad82 100644 --- a/src/app/zelda3/overworld/overworld_map.h +++ b/src/app/zelda3/overworld/overworld_map.h @@ -8,7 +8,7 @@ #include #include "absl/status/status.h" -#include "app/core/common.h" + #include "app/editor/utils/gfx_context.h" #include "app/gfx/bitmap.h" #include "app/gfx/snes_palette.h" diff --git a/src/app/zelda3/screen/title_screen.cc b/src/app/zelda3/screen/title_screen.cc index d95fbdeb..d4784537 100644 --- a/src/app/zelda3/screen/title_screen.cc +++ b/src/app/zelda3/screen/title_screen.cc @@ -2,7 +2,7 @@ #include -#include "app/core/common.h" + #include "app/gfx/bitmap.h" #include "app/gfx/snes_tile.h" #include "app/rom.h" diff --git a/src/app/zelda3/screen/title_screen.h b/src/app/zelda3/screen/title_screen.h index 17c4f1ae..49befd5f 100644 --- a/src/app/zelda3/screen/title_screen.h +++ b/src/app/zelda3/screen/title_screen.h @@ -3,7 +3,7 @@ #include -#include "app/core/common.h" + #include "app/gfx/bitmap.h" #include "app/gfx/snes_tile.h" #include "app/rom.h" diff --git a/src/cli/z3ed.cc b/src/cli/z3ed.cc index 99d3fde1..13420a34 100644 --- a/src/cli/z3ed.cc +++ b/src/cli/z3ed.cc @@ -9,7 +9,6 @@ #include #include "absl/status/status.h" -#include "app/core/common.h" #include "app/core/constants.h" #include "app/rom.h" #include "cli/command_handler.h" diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 94798cea..ad0e49d9 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -5,6 +5,7 @@ add_executable( test/yaze_test.cc # test/libc_test.cc test/rom_test.cc + test/core/message_test.cc test/gfx/compression_test.cc test/gfx/snes_palette_test.cc test/integration/test_editor.cc diff --git a/src/test/core/message_test.cc b/src/test/core/message_test.cc new file mode 100644 index 00000000..89a22ec1 --- /dev/null +++ b/src/test/core/message_test.cc @@ -0,0 +1,137 @@ +#include "core/message.h" + +#include + +namespace yaze { +namespace test { +namespace message_system { + +using app::core::IMessageListener; +using app::core::IMessageProtocol; +using app::core::Message; +using app::core::MessageDispatcher; +using app::core::MessageFilter; + +class TestListener : public IMessageListener { + public: + ~TestListener() = default; + + absl::Status OnMessageReceived(const Message& message) override { + last_message_ = message; + message_count_++; + return absl::OkStatus(); + } + + const Message& last_message() const { return last_message_; } + int message_count() const { return message_count_; } + + private: + Message last_message_; + int message_count_ = 0; +}; + +class TestProtocol : public IMessageProtocol { + public: + bool CanHandleMessage(const Message& message) const override { + return message.type == "TestMessage"; + } +}; + +class TestFilter : public MessageFilter { + public: + bool ShouldReceiveMessage(const Message& message) const override { + return std::any_cast(message.payload) > 10; + } +}; + +class MessageDispatcherTest : public ::testing::Test { + protected: + void SetUp() override { + message_count_ = 0; + protocol_ = std::make_unique(); + filter_ = std::make_unique(); + } + + void TearDown() override { + protocol_.reset(); + filter_.reset(); + } + + int message_count_ = 0; + int message_count1_ = 0; + int message_count2_ = 0; + TestListener listener1_; + TestListener listener2_; + std::unique_ptr protocol_; + std::unique_ptr filter_; +}; +TEST_F(MessageDispatcherTest, RegisterAndSendMessage) { + MessageDispatcher dispatcher; + + dispatcher.RegisterListener("TestMessage", &listener1_); + + Message message("TestMessage", nullptr, 42); + dispatcher.SendMessage(message); + + EXPECT_EQ(listener1_.message_count(), 1); + EXPECT_EQ(std::any_cast(listener1_.last_message().payload), 42); +} + +TEST_F(MessageDispatcherTest, UnregisterListener) { + MessageDispatcher dispatcher; + + dispatcher.RegisterListener("TestMessage", &listener1_); + dispatcher.UnregisterListener("TestMessage", &listener1_); + + Message message("TestMessage", nullptr, 42); + dispatcher.SendMessage(message); + + EXPECT_EQ(listener1_.message_count(), 0); +} + +TEST_F(MessageDispatcherTest, MultipleListeners) { + MessageDispatcher dispatcher; + + dispatcher.RegisterListener("TestMessage", &listener1_); + dispatcher.RegisterListener("TestMessage", &listener2_); + + Message message("TestMessage", nullptr, 42); + dispatcher.SendMessage(message); + + EXPECT_EQ(listener1_.message_count(), 1); + EXPECT_EQ(listener2_.message_count(), 1); + EXPECT_EQ(std::any_cast(listener1_.last_message().payload), 42); + EXPECT_EQ(std::any_cast(listener2_.last_message().payload), 42); +} + +TEST_F(MessageDispatcherTest, ProtocolBasedHandling) { + MessageDispatcher dispatcher; + + dispatcher.RegisterProtocol(protocol_.get()); + dispatcher.RegisterListener("TestMessage", &listener1_); + + Message message("TestMessage", nullptr, 42); + dispatcher.DispatchMessage(message); + + EXPECT_EQ(listener1_.message_count(), 1); + EXPECT_EQ(std::any_cast(listener1_.last_message().payload), 42); +} + +TEST_F(MessageDispatcherTest, FilteredMessageHandling) { + MessageDispatcher dispatcher; + + dispatcher.RegisterFilteredListener("TestMessage", &listener1_, + std::move(filter_)); + + Message valid_message("TestMessage", nullptr, 15); + dispatcher.DispatchMessage(valid_message); + EXPECT_EQ(listener1_.message_count(), 1); + + Message invalid_message("TestMessage", nullptr, 5); + dispatcher.DispatchMessage(invalid_message); + EXPECT_EQ(listener1_.message_count(), 1); +} + +} // namespace message_system +} // namespace test +} // namespace yaze diff --git a/src/test/gfx/compression_test.cc b/src/test/gfx/compression_test.cc index 8b34ec33..f7875f1d 100644 --- a/src/test/gfx/compression_test.cc +++ b/src/test/gfx/compression_test.cc @@ -33,7 +33,7 @@ using ::testing::TypedEq; namespace { -Bytes ExpectCompressOk(Rom& rom, uchar* in, int in_size) { +std::vector ExpectCompressOk(Rom& rom, uchar* in, int in_size) { auto load_status = rom.LoadFromPointer(in, in_size, false); EXPECT_TRUE(load_status.ok()); auto compression_status = CompressV3(rom.vector(), 0, in_size); @@ -42,7 +42,8 @@ Bytes ExpectCompressOk(Rom& rom, uchar* in, int in_size) { return compressed_bytes; } -Bytes ExpectDecompressBytesOk(Rom& rom, Bytes& in) { +std::vector ExpectDecompressBytesOk(Rom& rom, + std::vector& in) { auto load_status = rom.LoadFromBytes(in); EXPECT_TRUE(load_status.ok()); auto decompression_status = DecompressV2(rom.data(), 0, in.size()); @@ -51,7 +52,7 @@ Bytes ExpectDecompressBytesOk(Rom& rom, Bytes& in) { return decompressed_bytes; } -Bytes ExpectDecompressOk(Rom& rom, uchar* in, int in_size) { +std::vector ExpectDecompressOk(Rom& rom, uchar* in, int in_size) { auto load_status = rom.LoadFromPointer(in, in_size, false); EXPECT_TRUE(load_status.ok()); auto decompression_status = DecompressV2(rom.data(), 0, in_size); @@ -73,16 +74,16 @@ std::shared_ptr ExpectNewCompressionPieceOk( void AssertCompressionQuality( const std::vector& uncompressed_data, const std::vector& expected_compressed_data) { - absl::StatusOr result = + absl::StatusOr> result = CompressV3(uncompressed_data, 0, uncompressed_data.size(), 0, false); ASSERT_TRUE(result.ok()); auto compressed_data = std::move(*result); EXPECT_THAT(compressed_data, ElementsAreArray(expected_compressed_data)); } -Bytes ExpectCompressV3Ok(const std::vector& uncompressed_data, +std::vector ExpectCompressV3Ok(const std::vector& uncompressed_data, const std::vector& expected_compressed_data) { - absl::StatusOr result = + absl::StatusOr> result = CompressV3(uncompressed_data, 0, uncompressed_data.size(), 0, false); EXPECT_TRUE(result.ok()); auto compressed_data = std::move(*result); @@ -396,7 +397,7 @@ TEST(CheckIncByteV3Test, NotAnIncreasingSequence) { TEST(LC_LZ2_CompressionTest, DecompressionValidCommand) { Rom rom; - Bytes simple_copy_input = {BUILD_HEADER(0x00, 0x02), 0x2A, 0x45, 0xFF}; + std::vector simple_copy_input = {BUILD_HEADER(0x00, 0x02), 0x2A, 0x45, 0xFF}; uchar simple_copy_output[2] = {0x2A, 0x45}; auto decomp_result = ExpectDecompressBytesOk(rom, simple_copy_input); EXPECT_THAT(simple_copy_output, ElementsAreArray(decomp_result.data(), 2));