From c674bbe831db8a86d5d0b52604763dd851b6be48 Mon Sep 17 00:00:00 2001 From: scawful Date: Tue, 6 Aug 2024 17:26:53 -0400 Subject: [PATCH] move string functions to labeling, inline common routines --- src/app/core/common.cc | 38 +++++++------------------------------- src/app/core/common.h | 15 +++++---------- src/app/core/labeling.cc | 31 ++++++++++++++++++++++++++++--- src/app/core/labeling.h | 9 +++++++-- 4 files changed, 47 insertions(+), 46 deletions(-) diff --git a/src/app/core/common.cc b/src/app/core/common.cc index 4de7edcd..dacd5fac 100644 --- a/src/app/core/common.cc +++ b/src/app/core/common.cc @@ -52,32 +52,17 @@ uint64_t decode(const std::vector &input, size_t &offset) { std::shared_ptr ExperimentFlags::flags_; -std::string UppercaseHexByte(uint8_t byte, bool leading) { - if (leading) { - std::string result = absl::StrFormat("0x%02X", byte); - return result; - } - std::string result = absl::StrFormat("%02X", byte); - return result; -} -std::string UppercaseHexWord(uint16_t word) { - std::string result = absl::StrFormat("0x%04X", word); - return result; -} -std::string UppercaseHexLong(uint32_t dword) { - std::string result = absl::StrFormat("0x%06X", dword); - return result; -} +constexpr uint32_t kFastRomRegion = 0x808000; -uint32_t SnesToPc(uint32_t addr) { - if (addr >= 0x808000) { - addr -= 0x808000; +inline uint32_t SnesToPc(uint32_t addr) noexcept { + if (addr >= kFastRomRegion) { + addr -= kFastRomRegion; } uint32_t temp = (addr & 0x7FFF) + ((addr / 2) & 0xFF8000); return (temp + 0x0); } -uint32_t PcToSnes(uint32_t addr) { +inline uint32_t PcToSnes(uint32_t addr) { uint8_t *b = reinterpret_cast(&addr); b[2] = static_cast(b[2] * 2); @@ -96,8 +81,8 @@ uint32_t MapBankToWordAddress(uint8_t bank, uint16_t addr) { return result; } -int AddressFromBytes(uint8_t addr1, uint8_t addr2, uint8_t addr3) { - return (addr1 << 16) | (addr2 << 8) | addr3; +int AddressFromBytes(uint8_t bank, uint8_t high, uint8_t low) noexcept { + return (bank << 16) | (high << 8) | low; } // hextodec has been imported from SNESDisasm to parse hex numbers @@ -130,15 +115,6 @@ int HexToDec(char *input, int length) { return result; } -bool StringReplace(std::string &str, const std::string &from, - const std::string &to) { - size_t start = str.find(from); - if (start == std::string::npos) return false; - - str.replace(start, from.length(), to); - return true; -} - void stle(uint8_t *const p_arr, size_t const p_index, unsigned const p_val) { uint8_t v = (p_val >> (8 * p_index)) & 0xff; diff --git a/src/app/core/common.h b/src/app/core/common.h index e79bc60e..310e55b0 100644 --- a/src/app/core/common.h +++ b/src/app/core/common.h @@ -194,21 +194,16 @@ class Logger { static std::vector logs; }; -std::string UppercaseHexByte(uint8_t byte, bool leading = false); -std::string UppercaseHexWord(uint16_t word); -std::string UppercaseHexLong(uint32_t dword); +inline uint32_t SnesToPc(uint32_t addr) noexcept; -uint32_t SnesToPc(uint32_t addr); -uint32_t PcToSnes(uint32_t addr); +inline uint32_t PcToSnes(uint32_t addr); -uint32_t MapBankToWordAddress(uint8_t bank, uint16_t addr); +inline uint32_t MapBankToWordAddress(uint8_t bank, uint16_t addr); + +inline int AddressFromBytes(uint8_t bank, uint8_t high, uint8_t low) noexcept; -int AddressFromBytes(uint8_t addr1, uint8_t addr2, uint8_t addr3); int HexToDec(char *input, int length); -bool StringReplace(std::string &str, const std::string &from, - const std::string &to); - void stle16b_i(uint8_t *const p_arr, size_t const p_index, uint16_t const p_val); uint16_t ldle16b_i(uint8_t const *const p_arr, size_t const p_index); diff --git a/src/app/core/labeling.cc b/src/app/core/labeling.cc index db464091..7db27a01 100644 --- a/src/app/core/labeling.cc +++ b/src/app/core/labeling.cc @@ -1,8 +1,5 @@ #include "app/core/labeling.h" -#include "imgui/imgui.h" -#include "imgui/misc/cpp/imgui_stdlib.h" - #include #include #include @@ -13,11 +10,39 @@ #include "app/core/common.h" #include "app/core/constants.h" #include "app/gui/icons.h" +#include "imgui/imgui.h" +#include "imgui/misc/cpp/imgui_stdlib.h" namespace yaze { namespace app { namespace core { +std::string UppercaseHexByte(uint8_t byte, bool leading) { + if (leading) { + std::string result = absl::StrFormat("0x%02X", byte); + return result; + } + std::string result = absl::StrFormat("%02X", byte); + return result; +} +std::string UppercaseHexWord(uint16_t word) { + std::string result = absl::StrFormat("0x%04X", word); + return result; +} +std::string UppercaseHexLong(uint32_t dword) { + std::string result = absl::StrFormat("0x%06X", dword); + return result; +} + +bool StringReplace(std::string& str, const std::string& from, + const std::string& to) { + size_t start = str.find(from); + if (start == std::string::npos) return false; + + str.replace(start, from.length(), to); + return true; +} + bool ResourceLabelManager::LoadLabels(const std::string& filename) { std::ifstream file(filename); if (!file.is_open()) { diff --git a/src/app/core/labeling.h b/src/app/core/labeling.h index 95787bdf..2f051a51 100644 --- a/src/app/core/labeling.h +++ b/src/app/core/labeling.h @@ -17,6 +17,13 @@ namespace yaze { namespace app { namespace core { +std::string UppercaseHexByte(uint8_t byte, bool leading = false); +std::string UppercaseHexWord(uint16_t word); +std::string UppercaseHexLong(uint32_t dword); + +bool StringReplace(std::string& str, const std::string& from, + const std::string& to); + // Default types static constexpr absl::string_view kDefaultTypes[] = { "Dungeon Names", "Dungeon Room Names", "Overworld Map Names"}; @@ -32,8 +39,6 @@ struct ResourceLabelManager { const std::string& defaultValue); std::string CreateOrGetLabel(const std::string& type, const std::string& key, const std::string& defaultValue); - std::string CreateOrGetLabel(const std::string& type, const std::string& key, - const absl::string_view& defaultValue); bool labels_loaded_ = false; std::string filename_;