summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAloXado320 <david.albujar.s.30@gmail.com>2026-01-24 00:46:11 -0500
committerLywx <kiritodev01@gmail.com>2026-01-24 00:15:07 -0600
commit9d468174cc752e31d06463a7fe7100a39ec86af7 (patch)
tree2a1d6a39d4a95b7a8e89dd71be95084dd217c2c5
parent963a1f6830f1080a33f581c8b935c49060899214 (diff)
Build using C++17 and replace sha1 library
-rw-r--r--CMakeLists.txt4
-rw-r--r--lib/TinySHA1.hpp196
-rw-r--r--lib/hj/sha1.h633
-rw-r--r--src/Companion.cpp60
-rw-r--r--src/factories/CompressedTextureFactory.cpp9
-rw-r--r--src/factories/DisplayListOverrides.cpp3
-rw-r--r--src/factories/TextureFactory.cpp5
-rw-r--r--src/factories/fzerox/SequenceFactory.cpp8
-rw-r--r--src/factories/fzerox/SoundFontFactory.cpp2
-rw-r--r--src/factories/naudio/v0/AudioManager.cpp11
-rw-r--r--src/factories/naudio/v1/AudioContext.cpp1
-rw-r--r--src/factories/naudio/v1/AudioTableFactory.cpp3
-rw-r--r--src/factories/sf64/MessageFactory.cpp10
-rw-r--r--src/factories/sm64/GeoLayoutFactory.cpp2
-rw-r--r--src/utils/Decompressor.cpp5
-rw-r--r--src/utils/TorchUtils.h9
16 files changed, 277 insertions, 684 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6f760d6..fffcd0d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -2,7 +2,9 @@ cmake_minimum_required(VERSION 3.12)
project(torch)
include(FetchContent)
-set(CMAKE_CXX_STANDARD 20)
+set(CMAKE_CXX_STANDARD 17)
+set(CMAKE_CXX_STANDARD_REQUIRED ON)
+set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_C_STANDARD 11)
# Set build options
diff --git a/lib/TinySHA1.hpp b/lib/TinySHA1.hpp
new file mode 100644
index 0000000..70af046
--- /dev/null
+++ b/lib/TinySHA1.hpp
@@ -0,0 +1,196 @@
+/*
+ *
+ * TinySHA1 - a header only implementation of the SHA1 algorithm in C++. Based
+ * on the implementation in boost::uuid::details.
+ *
+ * SHA1 Wikipedia Page: http://en.wikipedia.org/wiki/SHA-1
+ *
+ * Copyright (c) 2012-22 SAURAV MOHAPATRA <mohaps@gmail.com>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+#ifndef _TINY_SHA1_HPP_
+#define _TINY_SHA1_HPP_
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
+#include <stdint.h>
+namespace sha1
+{
+ class SHA1
+ {
+ public:
+ typedef uint32_t digest32_t[5];
+ typedef uint8_t digest8_t[20];
+ inline static uint32_t LeftRotate(uint32_t value, size_t count) {
+ return (value << count) ^ (value >> (32-count));
+ }
+ SHA1(){ reset(); }
+ virtual ~SHA1() {}
+ SHA1(const SHA1& s) { *this = s; }
+ const SHA1& operator = (const SHA1& s) {
+ memcpy(m_digest, s.m_digest, 5 * sizeof(uint32_t));
+ memcpy(m_block, s.m_block, 64);
+ m_blockByteIndex = s.m_blockByteIndex;
+ m_byteCount = s.m_byteCount;
+ return *this;
+ }
+ SHA1& reset() {
+ m_digest[0] = 0x67452301;
+ m_digest[1] = 0xEFCDAB89;
+ m_digest[2] = 0x98BADCFE;
+ m_digest[3] = 0x10325476;
+ m_digest[4] = 0xC3D2E1F0;
+ m_blockByteIndex = 0;
+ m_byteCount = 0;
+ return *this;
+ }
+ SHA1& processByte(uint8_t octet) {
+ this->m_block[this->m_blockByteIndex++] = octet;
+ ++this->m_byteCount;
+ if(m_blockByteIndex == 64) {
+ this->m_blockByteIndex = 0;
+ processBlock();
+ }
+ return *this;
+ }
+ SHA1& processBlock(const void* const start, const void* const end) {
+ const uint8_t* begin = static_cast<const uint8_t*>(start);
+ const uint8_t* finish = static_cast<const uint8_t*>(end);
+ while(begin != finish) {
+ processByte(*begin);
+ begin++;
+ }
+ return *this;
+ }
+ SHA1& processBytes(const void* const data, size_t len) {
+ const uint8_t* block = static_cast<const uint8_t*>(data);
+ processBlock(block, block + len);
+ return *this;
+ }
+ const uint32_t* getDigest(digest32_t digest) {
+ size_t bitCount = this->m_byteCount * 8;
+ processByte(0x80);
+ if (this->m_blockByteIndex > 56) {
+ while (m_blockByteIndex != 0) {
+ processByte(0);
+ }
+ while (m_blockByteIndex < 56) {
+ processByte(0);
+ }
+ } else {
+ while (m_blockByteIndex < 56) {
+ processByte(0);
+ }
+ }
+ processByte(0);
+ processByte(0);
+ processByte(0);
+ processByte(0);
+ processByte( static_cast<unsigned char>((bitCount>>24) & 0xFF));
+ processByte( static_cast<unsigned char>((bitCount>>16) & 0xFF));
+ processByte( static_cast<unsigned char>((bitCount>>8 ) & 0xFF));
+ processByte( static_cast<unsigned char>((bitCount) & 0xFF));
+
+ memcpy(digest, m_digest, 5 * sizeof(uint32_t));
+ return digest;
+ }
+ const uint8_t* getDigestBytes(digest8_t digest) {
+ digest32_t d32;
+ getDigest(d32);
+ size_t di = 0;
+ digest[di++] = ((d32[0] >> 24) & 0xFF);
+ digest[di++] = ((d32[0] >> 16) & 0xFF);
+ digest[di++] = ((d32[0] >> 8) & 0xFF);
+ digest[di++] = ((d32[0]) & 0xFF);
+
+ digest[di++] = ((d32[1] >> 24) & 0xFF);
+ digest[di++] = ((d32[1] >> 16) & 0xFF);
+ digest[di++] = ((d32[1] >> 8) & 0xFF);
+ digest[di++] = ((d32[1]) & 0xFF);
+
+ digest[di++] = ((d32[2] >> 24) & 0xFF);
+ digest[di++] = ((d32[2] >> 16) & 0xFF);
+ digest[di++] = ((d32[2] >> 8) & 0xFF);
+ digest[di++] = ((d32[2]) & 0xFF);
+
+ digest[di++] = ((d32[3] >> 24) & 0xFF);
+ digest[di++] = ((d32[3] >> 16) & 0xFF);
+ digest[di++] = ((d32[3] >> 8) & 0xFF);
+ digest[di++] = ((d32[3]) & 0xFF);
+
+ digest[di++] = ((d32[4] >> 24) & 0xFF);
+ digest[di++] = ((d32[4] >> 16) & 0xFF);
+ digest[di++] = ((d32[4] >> 8) & 0xFF);
+ digest[di++] = ((d32[4]) & 0xFF);
+ return digest;
+ }
+
+ protected:
+ void processBlock() {
+ uint32_t w[80];
+ for (size_t i = 0; i < 16; i++) {
+ w[i] = (m_block[i*4 + 0] << 24);
+ w[i] |= (m_block[i*4 + 1] << 16);
+ w[i] |= (m_block[i*4 + 2] << 8);
+ w[i] |= (m_block[i*4 + 3]);
+ }
+ for (size_t i = 16; i < 80; i++) {
+ w[i] = LeftRotate((w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16]), 1);
+ }
+
+ uint32_t a = m_digest[0];
+ uint32_t b = m_digest[1];
+ uint32_t c = m_digest[2];
+ uint32_t d = m_digest[3];
+ uint32_t e = m_digest[4];
+
+ for (std::size_t i=0; i<80; ++i) {
+ uint32_t f = 0;
+ uint32_t k = 0;
+
+ if (i<20) {
+ f = (b & c) | (~b & d);
+ k = 0x5A827999;
+ } else if (i<40) {
+ f = b ^ c ^ d;
+ k = 0x6ED9EBA1;
+ } else if (i<60) {
+ f = (b & c) | (b & d) | (c & d);
+ k = 0x8F1BBCDC;
+ } else {
+ f = b ^ c ^ d;
+ k = 0xCA62C1D6;
+ }
+ uint32_t temp = LeftRotate(a, 5) + f + e + k + w[i];
+ e = d;
+ d = c;
+ c = LeftRotate(b, 30);
+ b = a;
+ a = temp;
+ }
+
+ m_digest[0] += a;
+ m_digest[1] += b;
+ m_digest[2] += c;
+ m_digest[3] += d;
+ m_digest[4] += e;
+ }
+ private:
+ digest32_t m_digest;
+ uint8_t m_block[64];
+ size_t m_blockByteIndex;
+ size_t m_byteCount;
+ };
+}
+#endif
diff --git a/lib/hj/sha1.h b/lib/hj/sha1.h
deleted file mode 100644
index 6fd1f36..0000000
--- a/lib/hj/sha1.h
+++ /dev/null
@@ -1,633 +0,0 @@
-/*
- * Chocobo1/Hash
- *
- * Copyright 2017-2020 by Mike Tzou (Chocobo1)
- * https://github.com/Chocobo1/Hash
- *
- * Licensed under GNU General Public License 3 or later.
- *
- * @license GPL3 <https://www.gnu.org/licenses/gpl-3.0-standalone.html>
- */
-
-#ifndef CHOCOBO1_SHA1_H
-#define CHOCOBO1_SHA1_H
-
-#include <array>
-#include <cassert>
-#include <climits>
-#include <cmath>
-#include <cstdint>
-#include <initializer_list>
-#include <string>
-#include <type_traits>
-#include <vector>
-
-#if (__cplusplus > 201703L)
-#include <version>
-#endif
-
-#ifndef USE_STD_SPAN_CHOCOBO1_HASH
-#if (__cpp_lib_span >= 202002L)
-#define USE_STD_SPAN_CHOCOBO1_HASH 1
-#else
-#define USE_STD_SPAN_CHOCOBO1_HASH 0
-#endif
-#endif
-
-#if (USE_STD_SPAN_CHOCOBO1_HASH == 1)
-#include <span>
-#else
-#include "gsl/span"
-#endif
-
-
-namespace Chocobo1
-{
- // Use these!!
- // SHA1();
-}
-
-
-namespace Chocobo1
-{
-// users should ignore things in this namespace
-
- namespace Hash
- {
-#ifndef CONSTEXPR_CPP17_CHOCOBO1_HASH
-#if __cplusplus >= 201703L
-#define CONSTEXPR_CPP17_CHOCOBO1_HASH constexpr
-#else
-#define CONSTEXPR_CPP17_CHOCOBO1_HASH
-#endif
-#endif
-
-#if (USE_STD_SPAN_CHOCOBO1_HASH == 1)
- using IndexType = std::size_t;
-#else
- using IndexType = gsl::index;
-#endif
-
-#ifndef CHOCOBO1_HASH_BUFFER_IMPL
-#define CHOCOBO1_HASH_BUFFER_IMPL
- template <typename T, IndexType N>
- class Buffer
- {
- public:
- using value_type = T;
- using index_type = IndexType;
- using size_type = std::size_t;
-
- constexpr Buffer() = default;
-
- CONSTEXPR_CPP17_CHOCOBO1_HASH Buffer(const std::initializer_list<T> initList)
- {
-#if !defined(NDEBUG)
- // check if out-of-bounds
- static_cast<void>(m_array.at(m_dataEndIdx + initList.size() - 1));
-#endif
-
- for (const auto &i : initList)
- {
- m_array[m_dataEndIdx] = i;
- ++m_dataEndIdx;
- }
- }
-
- template <typename InputIt>
- constexpr Buffer(const InputIt first, const InputIt last)
- {
- for (InputIt iter = first; iter != last; ++iter)
- {
- this->fill(*iter);
- }
- }
-
- constexpr T& operator[](const index_type pos)
- {
- return m_array[pos];
- }
-
- constexpr T operator[](const index_type pos) const
- {
- return m_array[pos];
- }
-
- CONSTEXPR_CPP17_CHOCOBO1_HASH void fill(const T &value, const index_type count = 1)
- {
-#if !defined(NDEBUG)
- // check if out-of-bounds
- static_cast<void>(m_array.at(m_dataEndIdx + count - 1));
-#endif
-
- for (index_type i = 0; i < count; ++i)
- {
- m_array[m_dataEndIdx] = value;
- ++m_dataEndIdx;
- }
- }
-
- template <typename InputIt>
- constexpr void push_back(const InputIt first, const InputIt last)
- {
- for (InputIt iter = first; iter != last; ++iter)
- {
- this->fill(*iter);
- }
- }
-
- constexpr void clear()
- {
- m_array = {};
- m_dataEndIdx = 0;
- }
-
- constexpr bool empty() const
- {
- return (m_dataEndIdx == 0);
- }
-
- constexpr size_type size() const
- {
- return m_dataEndIdx;
- }
-
- constexpr const T* data() const
- {
- return m_array.data();
- }
-
- private:
- std::array<T, N> m_array {};
- index_type m_dataEndIdx = 0;
- };
-#endif
-
-#ifndef CHOCOBO1_HASH_ROR_IMPL
-#define CHOCOBO1_HASH_ROR_IMPL
- template <typename R, typename T>
- constexpr R ror(const T x, const unsigned int s)
- {
- static_assert(std::is_unsigned<R>::value, "");
- static_assert(std::is_unsigned<T>::value, "");
- return static_cast<R>(x >> s);
- }
-#endif
-
-#ifndef CHOCOBO1_HASH_ROTL_IMPL
-#define CHOCOBO1_HASH_ROTL_IMPL
- template <typename T>
- constexpr T rotl(const T x, const unsigned int s)
- {
- static_assert(std::is_unsigned<T>::value, "");
- if (s == 0)
- return x;
- return ((x << s) | (x >> ((sizeof(T) * 8) - s)));
- }
-#endif
-
-
- namespace SHA1_NS
- {
- class SHA1
- {
- // https://tools.ietf.org/html/rfc3174
-
- public:
- using Byte = uint8_t;
- using ResultArrayType = std::array<Byte, 20>;
-
-#if (USE_STD_SPAN_CHOCOBO1_HASH == 1)
- template <typename T, std::size_t Extent = std::dynamic_extent>
- using Span = std::span<T, Extent>;
-#else
- template <typename T, std::size_t Extent = gsl::dynamic_extent>
- using Span = gsl::span<T, Extent>;
-#endif
-
-
- constexpr SHA1();
-
- constexpr void reset();
- CONSTEXPR_CPP17_CHOCOBO1_HASH SHA1& finalize(); // after this, only `operator T()`, `reset()`, `toArray()`, `toString()`, `toVector()` are available
-
- std::string toString() const;
- std::vector<Byte> toVector() const;
- CONSTEXPR_CPP17_CHOCOBO1_HASH ResultArrayType toArray() const;
- template <typename T>
- CONSTEXPR_CPP17_CHOCOBO1_HASH operator T() const noexcept;
-
- constexpr SHA1& addData(Span<const Byte> inData);
- constexpr SHA1& addData(const void *ptr, std::size_t length);
- template <std::size_t N>
- constexpr SHA1& addData(const Byte (&array)[N]);
- template <typename T, std::size_t N>
- SHA1& addData(const T (&array)[N]);
- template <typename T>
- SHA1& addData(Span<T> inSpan);
-
- friend constexpr bool operator==(const SHA1 &left, const SHA1 &right)
- {
- for (int i = 0; i < 5; ++i)
- {
- if (left.m_state[i] != right.m_state[i])
- return false;
- }
- return true;
- }
- friend constexpr bool operator!=(const SHA1 &left, const SHA1 &right)
- {
- return !(left == right);
- }
-
- private:
- constexpr void addDataImpl(Span<const Byte> data);
-
- static constexpr int BLOCK_SIZE = 64;
-
- Buffer<Byte, (BLOCK_SIZE * 2)> m_buffer; // x2 for paddings
- uint64_t m_sizeCounter = 0;
-
- uint32_t m_state[5] = {};
- };
-
-
- // helpers
- template <typename T>
- class Loader
- {
- // this class workaround loading data from unaligned memory boundaries
- // also eliminate endianness issues
- public:
- explicit constexpr Loader(const uint8_t *ptr)
- : m_ptr(ptr)
- {
- }
-
- constexpr T operator[](const IndexType idx) const
- {
- static_assert(std::is_same<T, uint32_t>::value, "");
- // handle specific endianness here
- const uint8_t *ptr = m_ptr + (sizeof(T) * idx);
- return ( (static_cast<T>(*(ptr + 0)) << 24)
- | (static_cast<T>(*(ptr + 1)) << 16)
- | (static_cast<T>(*(ptr + 2)) << 8)
- | (static_cast<T>(*(ptr + 3)) << 0));
- }
-
- private:
- const uint8_t *m_ptr;
- };
-
-
- //
- constexpr SHA1::SHA1()
- {
- static_assert((CHAR_BIT == 8), "Sorry, we don't support exotic CPUs");
- reset();
- }
-
- constexpr void SHA1::reset()
- {
- m_buffer.clear();
- m_sizeCounter = 0;
-
- m_state[0] = 0x67452301;
- m_state[1] = 0xEFCDAB89;
- m_state[2] = 0x98BADCFE;
- m_state[3] = 0x10325476;
- m_state[4] = 0xC3D2E1F0;
- }
-
- CONSTEXPR_CPP17_CHOCOBO1_HASH SHA1& SHA1::finalize()
- {
- m_sizeCounter += m_buffer.size();
-
- // append 1 bit
- m_buffer.fill(1 << 7);
-
- // append paddings
- const auto len = static_cast<int>(((2 * BLOCK_SIZE) - (m_buffer.size() + 8)) % BLOCK_SIZE);
- m_buffer.fill(0, (len + 8));
-
- // append size in bits
- const uint64_t sizeCounterBits = m_sizeCounter * 8;
- const uint32_t sizeCounterBitsL = ror<uint32_t>(sizeCounterBits, 0);
- const uint32_t sizeCounterBitsH = ror<uint32_t>(sizeCounterBits, 32);
- for (int i = 0; i < 4; ++i)
- {
- m_buffer[m_buffer.size() - 8 + i] = ror<Byte>(sizeCounterBitsH, (8 * (3 - i)));
- m_buffer[m_buffer.size() - 4 + i] = ror<Byte>(sizeCounterBitsL, (8 * (3 - i)));
- }
-
- addDataImpl({m_buffer.data(), m_buffer.size()});
- m_buffer.clear();
-
- return (*this);
- }
-
- std::string SHA1::toString() const
- {
- const auto digest = toArray();
- std::string ret;
- ret.resize(2 * digest.size());
-
- auto *retPtr = &ret.front();
- for (const auto c : digest)
- {
- const Byte upper = ror<Byte>(c, 4);
- *(retPtr++) = static_cast<char>((upper < 10) ? (upper + '0') : (upper - 10 + 'a'));
-
- const Byte lower = c & 0xf;
- *(retPtr++) = static_cast<char>((lower < 10) ? (lower + '0') : (lower - 10 + 'a'));
- }
-
- return ret;
- }
-
- std::vector<SHA1::Byte> SHA1::toVector() const
- {
- const auto digest = toArray();
- return {digest.begin(), digest.end()};
- }
-
- CONSTEXPR_CPP17_CHOCOBO1_HASH SHA1::ResultArrayType SHA1::toArray() const
- {
- const Span<const uint32_t> state(m_state);
- const int dataSize = sizeof(decltype(state)::value_type);
-
- ResultArrayType ret {};
- auto *retPtr = ret.data();
- for (const auto i : state)
- {
- for (int j = (dataSize - 1); j >= 0; --j)
- *(retPtr++) = ror<Byte>(i, (j * 8));
- }
-
- return ret;
- }
-
- template <typename T>
- CONSTEXPR_CPP17_CHOCOBO1_HASH SHA1::operator T() const noexcept
- {
- static_assert(std::is_unsigned<T>::value, "");
-
- const auto digest = toArray();
- T ret = 0;
- for (int i = 0, iMax = static_cast<int>(std::min(sizeof(T), digest.size())); i < iMax; ++i)
- {
- ret <<= 8;
- ret |= digest[i];
- }
- return ret;
- }
-
- constexpr SHA1& SHA1::addData(const Span<const Byte> inData)
- {
- Span<const Byte> data = inData;
-
- if (!m_buffer.empty())
- {
- const size_t len = std::min<size_t>((BLOCK_SIZE - m_buffer.size()), data.size()); // try fill to BLOCK_SIZE bytes
- m_buffer.push_back(data.begin(), (data.begin() + len));
-
- if (m_buffer.size() < BLOCK_SIZE) // still doesn't fill the buffer
- return (*this);
-
- addDataImpl({m_buffer.data(), m_buffer.size()});
- m_buffer.clear();
-
- data = data.subspan(len);
- }
-
- const size_t dataSize = data.size();
- if (dataSize < BLOCK_SIZE)
- {
- m_buffer = {data.begin(), data.end()};
- return (*this);
- }
-
- const size_t len = dataSize - (dataSize % BLOCK_SIZE); // align on BLOCK_SIZE bytes
- addDataImpl(data.first(len));
-
- if (len < dataSize) // didn't consume all data
- m_buffer = {(data.begin() + len), data.end()};
-
- return (*this);
- }
-
- constexpr SHA1& SHA1::addData(const void *ptr, const std::size_t length)
- {
- // Span::size_type = std::size_t
- return addData({static_cast<const Byte*>(ptr), length});
- }
-
- template <std::size_t N>
- constexpr SHA1& SHA1::addData(const Byte (&array)[N])
- {
- return addData({array, N});
- }
-
- template <typename T, std::size_t N>
- SHA1& SHA1::addData(const T (&array)[N])
- {
- return addData({reinterpret_cast<const Byte*>(array), (sizeof(T) * N)});
- }
-
- template <typename T>
- SHA1& SHA1::addData(const Span<T> inSpan)
- {
- return addData({reinterpret_cast<const Byte*>(inSpan.data()), inSpan.size_bytes()});
- }
-
- constexpr void SHA1::addDataImpl(const Span<const Byte> data)
- {
- assert((data.size() % BLOCK_SIZE) == 0);
-
- m_sizeCounter += data.size();
-
- for (size_t i = 0, iend = static_cast<size_t>(data.size() / BLOCK_SIZE); i < iend; ++i)
- {
- const Loader<uint32_t> m(static_cast<const Byte *>(data.data() + (i * BLOCK_SIZE)));
-
- uint32_t a = m_state[0];
- uint32_t b = m_state[1];
- uint32_t c = m_state[2];
- uint32_t d = m_state[3];
- uint32_t e = m_state[4];
-
- uint32_t wTable[80] = {};
-
-#ifdef sha1Round1
-#error "macro name clash"
-#else
-#define sha1Round1(a, b, c, d, e, t) \
- wTable[t] = m[t]; \
- e = rotl(a, 5) + ((b & (c ^ d)) ^ d) + e + wTable[t] + 0x5A827999; /* alternative f */ \
- b = rotl(b, 30);
-
- sha1Round1(a, b, c, d, e, 0);
- sha1Round1(e, a, b, c, d, 1);
- sha1Round1(d, e, a, b, c, 2);
- sha1Round1(c, d, e, a, b, 3);
- sha1Round1(b, c, d, e, a, 4);
- sha1Round1(a, b, c, d, e, 5);
- sha1Round1(e, a, b, c, d, 6);
- sha1Round1(d, e, a, b, c, 7);
- sha1Round1(c, d, e, a, b, 8);
- sha1Round1(b, c, d, e, a, 9);
- sha1Round1(a, b, c, d, e, 10);
- sha1Round1(e, a, b, c, d, 11);
- sha1Round1(d, e, a, b, c, 12);
- sha1Round1(c, d, e, a, b, 13);
- sha1Round1(b, c, d, e, a, 14);
- sha1Round1(a, b, c, d, e, 15);
-#undef sha1Round1
-#endif
-
-#ifdef sha1Round1a
-#error "macro name clash"
-#else
-#define sha1Round1a(a, b, c, d, e, t) \
- wTable[t] = rotl((wTable[t - 3] ^ wTable[t - 8] ^ wTable[t - 14] ^ wTable[t - 16]), 1); \
- e = rotl(a, 5) + ((b & (c ^ d)) ^ d) + e + wTable[t] + 0x5A827999; /* alternative f */ \
- b = rotl(b, 30);
-
- sha1Round1a(e, a, b, c, d, 16);
- sha1Round1a(d, e, a, b, c, 17);
- sha1Round1a(c, d, e, a, b, 18);
- sha1Round1a(b, c, d, e, a, 19);
-#undef sha1Round1a
-#endif
-
-#ifdef sha1Round2
-#error "macro name clash"
-#else
-#define sha1Round2(a, b, c, d, e, t) \
- wTable[t] = rotl((wTable[t - 3] ^ wTable[t - 8] ^ wTable[t - 14] ^ wTable[t - 16]), 1); \
- e = rotl(a, 5) + (b ^ c ^ d) + e + wTable[t] + 0x6ED9EBA1; \
- b = rotl(b, 30);
-
- sha1Round2(a, b, c, d, e, 20);
- sha1Round2(e, a, b, c, d, 21);
- sha1Round2(d, e, a, b, c, 22);
- sha1Round2(c, d, e, a, b, 23);
- sha1Round2(b, c, d, e, a, 24);
- sha1Round2(a, b, c, d, e, 25);
- sha1Round2(e, a, b, c, d, 26);
- sha1Round2(d, e, a, b, c, 27);
- sha1Round2(c, d, e, a, b, 28);
- sha1Round2(b, c, d, e, a, 29);
- sha1Round2(a, b, c, d, e, 30);
- sha1Round2(e, a, b, c, d, 31);
-#undef sha1Round2
-#endif
-
-#ifdef sha1Round2a
-#error "macro name clash"
-#else
-#define sha1Round2a(a, b, c, d, e, t) \
- wTable[t] = rotl((wTable[t - 6] ^ wTable[t - 16] ^ wTable[t - 28] ^ wTable[t - 32]), 2); /* alternative */ \
- e = rotl(a, 5) + (b ^ c ^ d) + e + wTable[t] + 0x6ED9EBA1; \
- b = rotl(b, 30);
-
- sha1Round2a(d, e, a, b, c, 32);
- sha1Round2a(c, d, e, a, b, 33);
- sha1Round2a(b, c, d, e, a, 34);
- sha1Round2a(a, b, c, d, e, 35);
- sha1Round2a(e, a, b, c, d, 36);
- sha1Round2a(d, e, a, b, c, 37);
- sha1Round2a(c, d, e, a, b, 38);
- sha1Round2a(b, c, d, e, a, 39);
-#undef sha1Round2a
-#endif
-
-#ifdef sha1Round3
-#error "macro name clash"
-#else
-#define sha1Round3(a, b, c, d, e, t) \
- wTable[t] = rotl((wTable[t - 6] ^ wTable[t - 16] ^ wTable[t - 28] ^ wTable[t - 32]), 2); /* alternative */ \
- e = rotl(a, 5) + ((b & c) | (d & (b | c))) + e + wTable[t] + 0x8F1BBCDC; \
- b = rotl(b, 30);
-
- sha1Round3(a, b, c, d, e, 40);
- sha1Round3(e, a, b, c, d, 41);
- sha1Round3(d, e, a, b, c, 42);
- sha1Round3(c, d, e, a, b, 43);
- sha1Round3(b, c, d, e, a, 44);
- sha1Round3(a, b, c, d, e, 45);
- sha1Round3(e, a, b, c, d, 46);
- sha1Round3(d, e, a, b, c, 47);
- sha1Round3(c, d, e, a, b, 48);
- sha1Round3(b, c, d, e, a, 49);
- sha1Round3(a, b, c, d, e, 50);
- sha1Round3(e, a, b, c, d, 51);
- sha1Round3(d, e, a, b, c, 52);
- sha1Round3(c, d, e, a, b, 53);
- sha1Round3(b, c, d, e, a, 54);
- sha1Round3(a, b, c, d, e, 55);
- sha1Round3(e, a, b, c, d, 56);
- sha1Round3(d, e, a, b, c, 57);
- sha1Round3(c, d, e, a, b, 58);
- sha1Round3(b, c, d, e, a, 59);
-#undef sha1Round3
-#endif
-
-#ifdef sha1Round4
-#error "macro name clash"
-#else
-#define sha1Round4(a, b, c, d, e, t) \
- wTable[t] = rotl((wTable[t - 6] ^ wTable[t - 16] ^ wTable[t - 28] ^ wTable[t - 32]), 2); /* alternative */ \
- e = rotl(a, 5) + (b ^ c ^ d) + e + wTable[t] + 0xCA62C1D6; \
- b = rotl(b, 30);
-
- sha1Round4(a, b, c, d, e, 60);
- sha1Round4(e, a, b, c, d, 61);
- sha1Round4(d, e, a, b, c, 62);
- sha1Round4(c, d, e, a, b, 63);
- sha1Round4(b, c, d, e, a, 64);
- sha1Round4(a, b, c, d, e, 65);
- sha1Round4(e, a, b, c, d, 66);
- sha1Round4(d, e, a, b, c, 67);
- sha1Round4(c, d, e, a, b, 68);
- sha1Round4(b, c, d, e, a, 69);
- sha1Round4(a, b, c, d, e, 70);
- sha1Round4(e, a, b, c, d, 71);
- sha1Round4(d, e, a, b, c, 72);
- sha1Round4(c, d, e, a, b, 73);
- sha1Round4(b, c, d, e, a, 74);
- sha1Round4(a, b, c, d, e, 75);
- sha1Round4(e, a, b, c, d, 76);
- sha1Round4(d, e, a, b, c, 77);
- sha1Round4(c, d, e, a, b, 78);
- sha1Round4(b, c, d, e, a, 79);
-#undef sha1Round4
-#endif
-
- // Let H0 = H0 + A, H1 = H1 + B, H2 = H2 + C, H3 = H3 + D, H4 = H4 + E.
- m_state[0] += a;
- m_state[1] += b;
- m_state[2] += c;
- m_state[3] += d;
- m_state[4] += e;
- }
- }
- }
- }
-
- using SHA1 = Hash::SHA1_NS::SHA1;
-}
-
-namespace std
-{
- template <>
- struct hash<Chocobo1::SHA1>
- {
- CONSTEXPR_CPP17_CHOCOBO1_HASH size_t operator()(const Chocobo1::SHA1 &hash) const noexcept
- {
- return hash;
- }
- };
-}
-
-#endif // CHOCOBO1_SHA1_H
diff --git a/src/Companion.cpp b/src/Companion.cpp
index 6f023e6..2ef7cf2 100644
--- a/src/Companion.cpp
+++ b/src/Companion.cpp
@@ -1,11 +1,12 @@
#include "Companion.h"
#include "utils/Decompressor.h"
+#include "utils/StringHelper.h"
#include "utils/TorchUtils.h"
#include "archive/SWrapper.h"
#include "archive/ZWrapper.h"
#include "spdlog/spdlog.h"
-#include "hj/sha1.h"
+#include "TinySHA1.hpp"
#include <regex>
#include <fstream>
@@ -303,7 +304,7 @@ std::optional<ParseResultData> Companion::ParseNode(YAML::Node& node, std::strin
bool executeDef = true;
std::optional<std::shared_ptr<IParsedData>> result;
- if(this->gConfig.modding && impl->SupportModdedAssets() && this->gModdedAssetPaths.contains(name)) {
+ if(this->gConfig.modding && impl->SupportModdedAssets() && Torch::contains(this->gModdedAssetPaths, name)) {
auto path = fs::path(this->gConfig.moddingPath) / this->gModdedAssetPaths[name];
if(!exists(path)) {
SPDLOG_ERROR("Modded asset {} not found", this->gModdedAssetPaths[name]);
@@ -369,13 +370,13 @@ void Companion::ParseCurrentFileConfig(YAML::Node node) {
}
std::string externalFileName = (this->gSourceDirectory / externalFile.as<std::string>()).string();
- if (std::filesystem::relative(externalFileName, this->gAssetPath).string().starts_with("../")) {
+ if (StringHelper::StartsWith(std::filesystem::relative(externalFileName, this->gAssetPath).string(), "../")) {
throw std::runtime_error("External File " + externalFileName + " Not In Asset Directory " + this->gAssetPath);
} else if (std::filesystem::relative(externalFileName, this->gAssetPath).string() == "") {
throw std::runtime_error("External File " + externalFileName + " Not In Asset Directory " + this->gAssetPath);
}
- if (!this->gAddrMap.contains(externalFileName)) {
+ if (!Torch::contains(this->gAddrMap, externalFileName)) {
SPDLOG_INFO("Dependency on external file {}. Now processing {}", externalFileName, externalFileName);
auto currentFile = this->gCurrentFile;
auto currentDirectory = this->gCurrentDirectory;
@@ -386,7 +387,7 @@ void Companion::ParseCurrentFileConfig(YAML::Node node) {
YAML::Node root = YAML::LoadFile(externalFileName);
- if (!this->gProcessedFiles.contains(this->gCurrentFile)) {
+ if (!Torch::contains(this->gProcessedFiles, this->gCurrentFile)) {
ProcessFile(root);
this->gProcessedFiles.insert(this->gCurrentFile);
}
@@ -1313,7 +1314,7 @@ void Companion::Process() {
this->gCurrentDirectory = relative(entry.path(), this->gAssetPath).replace_extension("");
this->gCurrentFile = yamlPath;
- if (!this->gProcessedFiles.contains(this->gCurrentFile)) {
+ if (!Torch::contains(this->gProcessedFiles, this->gCurrentFile)) {
ProcessFile(root);
this->gProcessedFiles.insert(this->gCurrentFile);
}
@@ -1440,7 +1441,7 @@ void Companion::RegisterFactory(const std::string& type, const std::shared_ptr<B
}
std::optional<std::shared_ptr<BaseFactory>> Companion::GetFactory(const std::string &type) {
- if(!this->gFactories.contains(type)){
+ if(!Torch::contains(this->gFactories, type)){
return std::nullopt;
}
@@ -1458,11 +1459,11 @@ std::optional<Table> Companion::SearchTable(uint32_t addr){
}
std::optional<std::string> Companion::GetEnumFromValue(const std::string& key, int32_t id) {
- if(!this->gEnums.contains(key)){
+ if(!Torch::contains(this->gEnums, key)){
return std::nullopt;
}
- if(!this->gEnums[key].contains(id)){
+ if(!Torch::contains(this->gEnums[key], id)){
return std::nullopt;
}
@@ -1473,15 +1474,15 @@ std::optional<std::uint32_t> Companion::GetFileOffsetFromSegmentedAddr(const uin
auto segments = this->gConfig.segment;
- if(segments.temporal.contains(segment)) {
+ if(Torch::contains(segments.temporal, segment)) {
return segments.temporal[segment];
}
- if(segments.local.contains(segment)) {
+ if(Torch::contains(segments.local, segment)) {
return segments.local[segment];
}
- if(segments.global.contains(segment)) {
+ if(Torch::contains(segments.global, segment)) {
return segments.global[segment];
}
@@ -1490,7 +1491,7 @@ std::optional<std::uint32_t> Companion::GetFileOffsetFromSegmentedAddr(const uin
uint32_t Companion::PatchVirtualAddr(uint32_t addr) {
if (addr & 0x80000000) {
- if (gVirtualAddrMap.contains(gCurrentFile)) {
+ if (Torch::contains(gVirtualAddrMap, gCurrentFile)) {
addr -= std::get<0>(gVirtualAddrMap[gCurrentFile]);
addr += std::get<1>(gVirtualAddrMap[gCurrentFile]);
}
@@ -1500,21 +1501,21 @@ uint32_t Companion::PatchVirtualAddr(uint32_t addr) {
}
std::optional<std::tuple<std::string, YAML::Node>> Companion::GetNodeByAddr(uint32_t addr){
- if(!this->gAddrMap.contains(this->gCurrentFile)){
+ if(!Torch::contains(this->gAddrMap, this->gCurrentFile)){
return std::nullopt;
}
// HACK: Adjust address to rom address if virtual address
addr = PatchVirtualAddr(addr);
- if(!this->gAddrMap[this->gCurrentFile].contains(addr)){
+ if(!Torch::contains(this->gAddrMap[this->gCurrentFile], addr)){
for (auto &file : this->gCurrentExternalFiles) {
- if (!this->gAddrMap.contains(file)) {
+ if (!Torch::contains(this->gAddrMap, file)) {
SPDLOG_WARN("GetNodeByAddr: External File {} Not Found.", file);
continue;
}
- if (!this->gAddrMap[file].contains(addr)) {
+ if (!Torch::contains(this->gAddrMap[file], addr)) {
continue;
}
return this->gAddrMap[file][addr];
@@ -1526,7 +1527,7 @@ std::optional<std::tuple<std::string, YAML::Node>> Companion::GetNodeByAddr(uint
}
std::optional<std::string> Companion::GetStringByAddr(const uint32_t addr) {
- if(this->gManualSegments.contains(addr)) {
+ if(Torch::contains(this->gManualSegments, addr)) {
return this->gManualSegments[addr];
}
@@ -1558,7 +1559,7 @@ std::optional<std::tuple<std::string, YAML::Node>> Companion::GetSafeNodeByAddr(
}
std::optional<std::string> Companion::GetSafeStringByAddr(const uint32_t addr, std::string type) {
- if(this->gManualSegments.contains(addr)) {
+ if(Torch::contains(this->gManualSegments, addr)) {
return this->gManualSegments[addr];
}
@@ -1596,9 +1597,9 @@ std::string Companion::GetSymbolFromAddr(uint32_t address, bool validZero) {
}
std::optional<ParseResultData> Companion::GetParseDataByAddr(uint32_t addr) {
- if(!this->gParseResults.contains(this->gCurrentFile)){
+ if(!Torch::contains(this->gParseResults, this->gCurrentFile)){
for (auto &file : this->gCurrentExternalFiles) {
- if (!this->gParseResults.contains(file)) {
+ if (!Torch::contains(this->gParseResults, file)) {
SPDLOG_INFO("GetParseDataByAddr: External File {} Not Found.", file);
continue;
}
@@ -1622,7 +1623,7 @@ std::optional<ParseResultData> Companion::GetParseDataByAddr(uint32_t addr) {
}
std::optional<ParseResultData> Companion::GetParseDataBySymbol(const std::string& symbol) {
- if(!this->gParseResults.contains(this->gCurrentFile)){
+ if(!Torch::contains(this->gParseResults, this->gCurrentFile)){
return std::nullopt;
}
@@ -1641,7 +1642,7 @@ std::optional<ParseResultData> Companion::GetParseDataBySymbol(const std::string
std::optional<std::vector<std::tuple<std::string, YAML::Node>>> Companion::GetNodesByType(const std::string& type){
std::vector<std::tuple<std::string, YAML::Node>> nodes;
- if(!this->gAddrMap.contains(this->gCurrentFile)){
+ if(!Torch::contains(this->gAddrMap, this->gCurrentFile)){
return nodes;
}
@@ -1695,7 +1696,18 @@ std::string Companion::RelativePathToSrcDir(const std::string& path) const {
}
std::string Companion::CalculateHash(const std::vector<uint8_t>& data) {
- return Chocobo1::SHA1().addData(data).finalize().toString();
+ sha1::SHA1 s;
+ s.processBytes(data.data(), data.size());
+
+ uint32_t hash[5];
+ s.getDigest(hash);
+
+ char buf[41];
+ std::snprintf(buf, sizeof(buf),
+ "%08x%08x%08x%08x%08x",
+ hash[0], hash[1], hash[2], hash[3], hash[4]);
+
+ return std::string(buf);
}
std::optional<YAML::Node> Companion::AddAsset(YAML::Node asset) {
diff --git a/src/factories/CompressedTextureFactory.cpp b/src/factories/CompressedTextureFactory.cpp
index 34ccc61..685bc53 100644
--- a/src/factories/CompressedTextureFactory.cpp
+++ b/src/factories/CompressedTextureFactory.cpp
@@ -1,5 +1,6 @@
#include "CompressedTextureFactory.h"
#include "utils/Decompressor.h"
+#include "utils/TorchUtils.h"
#include "spdlog/spdlog.h"
#include "Companion.h"
#include <iomanip>
@@ -372,7 +373,7 @@ std::optional<std::shared_ptr<IParsedData>> CompressedTextureFactory::parse(std:
uint32_t size;
auto compression = GetSafeNode<std::string>(node, "compression");
CompressionType compressionType;
- if (!sCompressionTypes.contains(compression)) {
+ if (!Torch::contains(sCompressionTypes, compression)) {
SPDLOG_ERROR("Compresed Texture entry at {:X} in yaml missing compression type\n\
Please add one of the following compression types\n\
MIO0, YAY0, YAY1, YAZ0 (Unsupported)", offset);
@@ -399,7 +400,7 @@ std::optional<std::shared_ptr<IParsedData>> CompressedTextureFactory::parse(std:
return std::nullopt;
}
- if(!sTextureFormats.contains(format)) {
+ if(!Torch::contains(sTextureFormats, format)) {
return std::nullopt;
}
@@ -469,7 +470,7 @@ std::optional<std::shared_ptr<IParsedData>> CompressedTextureFactory::parse_modd
auto offset = GetSafeNode<uint32_t>(node, "offset");
auto compression = GetSafeNode<std::string>(node, "compression");
CompressionType compressionType;
- if (!sCompressionTypes.contains(compression)) {
+ if (!Torch::contains(sCompressionTypes, compression)) {
SPDLOG_ERROR("Compresed Texture entry at {:X} in yaml missing compression type\n\
Please add one of the following compression types\n\
MIO0, YAY0, YAY1, YAZ0 (Unsupported)", offset);
@@ -484,7 +485,7 @@ std::optional<std::shared_ptr<IParsedData>> CompressedTextureFactory::parse_modd
return std::nullopt;
}
- if(!sTextureFormats.contains(format)) {
+ if(!Torch::contains(sTextureFormats, format)) {
return std::nullopt;
}
diff --git a/src/factories/DisplayListOverrides.cpp b/src/factories/DisplayListOverrides.cpp
index 59a7344..5dc6086 100644
--- a/src/factories/DisplayListOverrides.cpp
+++ b/src/factories/DisplayListOverrides.cpp
@@ -1,6 +1,7 @@
#include "DisplayListOverrides.h"
#include "utils/Decompressor.h"
+#include "utils/TorchUtils.h"
#include "DisplayListFactory.h"
#include "spdlog/spdlog.h"
#include "Companion.h"
@@ -203,7 +204,7 @@ int Matrix(uint32_t ptr) {
#endif
std::optional<std::tuple<std::string, YAML::Node>> GetVtxOverlap(uint32_t ptr){
- if(mVtxOverlaps.contains(ptr)){
+ if(Torch::contains(mVtxOverlaps, ptr)){
SPDLOG_INFO("Found overlap for ptr 0x{:X}", ptr);
return mVtxOverlaps[ptr];
}
diff --git a/src/factories/TextureFactory.cpp b/src/factories/TextureFactory.cpp
index 8e9d53c..b7d555b 100644
--- a/src/factories/TextureFactory.cpp
+++ b/src/factories/TextureFactory.cpp
@@ -1,5 +1,6 @@
#include "TextureFactory.h"
#include "utils/Decompressor.h"
+#include "utils/TorchUtils.h"
#include "spdlog/spdlog.h"
#include "Companion.h"
#include <iomanip>
@@ -286,7 +287,7 @@ std::optional<std::shared_ptr<IParsedData>> TextureFactory::parse(std::vector<ui
return std::nullopt;
}
- if(!sTextureFormats.contains(format)) {
+ if(!Torch::contains(sTextureFormats, format)) {
return std::nullopt;
}
@@ -362,7 +363,7 @@ std::optional<std::shared_ptr<IParsedData>> TextureFactory::parse_modding(std::v
return std::nullopt;
}
- if(!sTextureFormats.contains(format)) {
+ if(!Torch::contains(sTextureFormats, format)) {
return std::nullopt;
}
diff --git a/src/factories/fzerox/SequenceFactory.cpp b/src/factories/fzerox/SequenceFactory.cpp
index bacdfe0..74d2a6a 100644
--- a/src/factories/fzerox/SequenceFactory.cpp
+++ b/src/factories/fzerox/SequenceFactory.cpp
@@ -325,7 +325,7 @@ ExportResult FZX::SequenceCodeExporter::Export(std::ostream &write, std::shared_
}
lastEndPos = command.pos + command.size;
- if (data->mLabels.contains(command.pos)) {
+ if (Torch::contains(data->mLabels,command.pos)) {
write << "// L____" << FORMAT_HEX2(command.pos, 3) << ":\n";
}
@@ -1336,10 +1336,10 @@ std::optional<std::shared_ptr<IParsedData>> FZX::SequenceFactory::parse(std::vec
command.channel = channel;
command.layer = layer;
- if (!existingPositions.contains(command.pos) && command.pos < size) {
+ if (!Torch::contains(existingPositions, command.pos) && command.pos < size) {
existingPositions.insert(command.pos);
} else {
- while (!posStack.empty() && existingPositions.contains(posStack.back().pos)) {
+ while (!posStack.empty() && Torch::contains(existingPositions, posStack.back().pos)) {
SPDLOG_INFO("POP BACK 0x{:X}", posStack.back().pos);
posStack.pop_back();
}
@@ -1990,7 +1990,7 @@ std::optional<std::shared_ptr<IParsedData>> FZX::SequenceFactory::parse(std::vec
uint16_t addr = envAddr;
reader.Seek(addr, LUS::SeekOffsetType::Start);
- while (!existingPositions.contains(addr) && addr < size) {
+ while (!Torch::contains(existingPositions, addr) && addr < size) {
auto delay = READ_S16;
auto arg = READ_S16;
command.args.emplace_back(delay);
diff --git a/src/factories/fzerox/SoundFontFactory.cpp b/src/factories/fzerox/SoundFontFactory.cpp
index 86fb250..1c95121 100644
--- a/src/factories/fzerox/SoundFontFactory.cpp
+++ b/src/factories/fzerox/SoundFontFactory.cpp
@@ -376,7 +376,7 @@ ExportResult FZX::SoundFontBinaryExporter::Export(std::ostream &write, std::shar
std::string FZX::SoundFontFactory::RegisterSoundFontData(std::string symbol, FZX::DataType dataType, uint32_t offset, std::map<uint32_t, std::pair<FZX::DataType, std::string>>& dataMap, std::unordered_map<FZX::DataType, uint32_t>& dataCountMap) {
std::string dataName;
- if (dataMap.contains(offset)) {
+ if (Torch::contains(dataMap, offset)) {
return dataMap.at(offset).second;
}
diff --git a/src/factories/naudio/v0/AudioManager.cpp b/src/factories/naudio/v0/AudioManager.cpp
index 1e7fc9a..51b44d5 100644
--- a/src/factories/naudio/v0/AudioManager.cpp
+++ b/src/factories/naudio/v0/AudioManager.cpp
@@ -13,6 +13,7 @@
#include "spdlog/spdlog.h"
#include "lib/binarytools/BinaryReader.h"
#include "spdlog/spdlog.h"
+#include "utils/TorchUtils.h"
std::unordered_map<std::string, uint32_t> name_table;
std::unordered_map<uint32_t, std::string> sample_table;
@@ -27,7 +28,7 @@ std::vector<uint32_t> PyUtils::range(uint32_t start, uint32_t end) {
}
std::string gen_name(const std::string& prefix){
- if(!name_table.contains(prefix)){
+ if(!Torch::contains(name_table, prefix)){
name_table[prefix] = 0;
}
return prefix + std::to_string(name_table[prefix]++);
@@ -42,7 +43,7 @@ AudioBankSample* SampleBank::AddSample(uint32_t addr, size_t sampleSize, const A
AudioBankSample* entry;
- if(this->entries.contains(addr)){
+ if(Torch::contains(this->entries, addr)){
entry = this->entries[addr];
assert(entry->book == book);
assert(entry->loop == loop);
@@ -428,7 +429,7 @@ TBLFile AudioManager::parse_tbl(std::vector<uint8_t>& data, std::vector<Entry>&
TBLFile tbl;
std::unordered_map<uint32_t, std::string> cache;
for(auto &entry : entries){
- if(!cache.contains(entry.offset)){
+ if(!Torch::contains(cache, entry.offset)){
std::string name = gen_name("sample_bank");
auto* sampleBank = new SampleBank{
name, entry.offset, PyUtils::slice(data, entry.offset, entry.offset + entry.length)
@@ -494,7 +495,7 @@ void AudioManager::bind_sample(YAML::Node& node, const std::string& path){
}
std::string& AudioManager::get_sample(uint32_t id) {
- if(!sample_table.contains(id)) {
+ if(!Torch::contains(sample_table, id)) {
throw std::runtime_error("Failed to find sample with id " + std::to_string(id));
}
return sample_table[id];
@@ -535,7 +536,7 @@ AudioBankSample AudioManager::get_aifc(int32_t index) {
}
uint32_t AudioManager::get_index(AudioBankSample* entry) {
- if(!this->sampleMap.contains(entry)){
+ if(!Torch::contains(this->sampleMap, entry)){
return -1;
}
return this->sampleMap[entry];
diff --git a/src/factories/naudio/v1/AudioContext.cpp b/src/factories/naudio/v1/AudioContext.cpp
index f2b0dd0..6529a58 100644
--- a/src/factories/naudio/v1/AudioContext.cpp
+++ b/src/factories/naudio/v1/AudioContext.cpp
@@ -1,7 +1,6 @@
#include "AudioContext.h"
#include "spdlog/spdlog.h"
#include "Companion.h"
-#include "utils/StringHelper.h"
std::unordered_map<AudioTableType, TableEntry> AudioContext::tables;
NAudioDrivers AudioContext::driver = NAudioDrivers::UNKNOWN;
diff --git a/src/factories/naudio/v1/AudioTableFactory.cpp b/src/factories/naudio/v1/AudioTableFactory.cpp
index 5f82b8c..f6e37dd 100644
--- a/src/factories/naudio/v1/AudioTableFactory.cpp
+++ b/src/factories/naudio/v1/AudioTableFactory.cpp
@@ -1,5 +1,6 @@
#include "AudioTableFactory.h"
#include "utils/Decompressor.h"
+#include "utils/TorchUtils.h"
#include "spdlog/spdlog.h"
#include "AudioContext.h"
#include "Companion.h"
@@ -109,7 +110,7 @@ std::optional<std::shared_ptr<IParsedData>> AudioTableFactory::parse(std::vector
auto format = GetSafeNode<std::string>(node, "format");
std::transform(format.begin(), format.end(), format.begin(), ::toupper);
- if(!gTableTypes.contains(format)) {
+ if(!Torch::contains(gTableTypes, format)) {
return std::nullopt;
}
diff --git a/src/factories/sf64/MessageFactory.cpp b/src/factories/sf64/MessageFactory.cpp
index ca1a624..0d1b527 100644
--- a/src/factories/sf64/MessageFactory.cpp
+++ b/src/factories/sf64/MessageFactory.cpp
@@ -1,6 +1,8 @@
#include "MessageFactory.h"
#include "utils/Decompressor.h"
+#include "utils/StringHelper.h"
+#include "utils/TorchUtils.h"
#include "spdlog/spdlog.h"
#include "Companion.h"
#include <regex>
@@ -201,9 +203,9 @@ std::optional<std::shared_ptr<IParsedData>> SF64::MessageFactory::parse(std::vec
mesgStr << whitespace;
whitespace = "";
}
- if(enumCode.starts_with("_")){
+ if (StringHelper::StartsWith(enumCode, "_")) {
mesgStr << enumCode.substr(1);
- } else if(ASCIITable.contains(enumCode)){
+ } else if (Torch::contains(ASCIITable, enumCode)){
mesgStr << ASCIITable[enumCode];
}
@@ -276,9 +278,9 @@ std::optional<std::shared_ptr<IParsedData>> SF64::MessageFactory::parse_modding(
mesgStr << whitespace;
whitespace = "";
}
- if(enumCode.starts_with("_")){
+ if (StringHelper::StartsWith(enumCode, "_")) {
mesgStr << enumCode.substr(1);
- } else if(ASCIITable.contains(enumCode)){
+ } else if (Torch::contains(ASCIITable, enumCode)){
mesgStr << ASCIITable[enumCode];
}
}
diff --git a/src/factories/sm64/GeoLayoutFactory.cpp b/src/factories/sm64/GeoLayoutFactory.cpp
index 37d0e74..73e4a2e 100644
--- a/src/factories/sm64/GeoLayoutFactory.cpp
+++ b/src/factories/sm64/GeoLayoutFactory.cpp
@@ -28,7 +28,7 @@ uint64_t RegisterAutoGen(uint32_t ptr, std::string type) {
void StoreFunc(uint32_t vram) {
return;
- if(!gFunctionMap.contains(vram)) {
+ if(!Torch::contains(gFunctionMap, vram)) {
return;
}
diff --git a/src/utils/Decompressor.cpp b/src/utils/Decompressor.cpp
index 934a84f..9181099 100644
--- a/src/utils/Decompressor.cpp
+++ b/src/utils/Decompressor.cpp
@@ -1,4 +1,5 @@
#include "Decompressor.h"
+#include "TorchUtils.h"
#include <stdexcept>
#include "spdlog/spdlog.h"
@@ -15,7 +16,7 @@ std::unordered_map<uint32_t, DataChunk*> gCachedChunks;
DataChunk* Decompressor::Decode(const std::vector<uint8_t>& buffer, const uint32_t offset, const CompressionType type, bool ignoreCache) {
- if(!ignoreCache && gCachedChunks.contains(offset)){
+ if(!ignoreCache && Torch::contains(gCachedChunks, offset)){
return gCachedChunks[offset];
}
@@ -61,7 +62,7 @@ DataChunk* Decompressor::Decode(const std::vector<uint8_t>& buffer, const uint32
}
DataChunk* Decompressor::DecodeTKMK00(const std::vector<uint8_t>& buffer, const uint32_t offset, const uint32_t size, const uint32_t alpha) {
- if(gCachedChunks.contains(offset)){
+ if(Torch::contains(gCachedChunks, offset)){
return gCachedChunks[offset];
}
diff --git a/src/utils/TorchUtils.h b/src/utils/TorchUtils.h
index 8b7a3f1..275ec47 100644
--- a/src/utils/TorchUtils.h
+++ b/src/utils/TorchUtils.h
@@ -24,6 +24,15 @@ std::string to_hex(T number, const bool append0x = true) {
return format;
}
+template <typename Container, typename Key>
+constexpr bool contains(const Container& c, const Key& k) {
+#if __cplusplus >= 202002L
+ return c.contains(k);
+#else
+ return c.find(k) != c.end();
+#endif
+}
+
uint32_t translate(uint32_t offset);
std::vector<std::filesystem::directory_entry> getRecursiveEntries(const std::filesystem::path baseDir);