diff --git a/.github/workflows/analysis.yml b/.github/workflows/analysis.yml index 3cc43461f..440c48e17 100644 --- a/.github/workflows/analysis.yml +++ b/.github/workflows/analysis.yml @@ -7,7 +7,7 @@ on: jobs: codeql: name: ๐Ÿ› CodeQL - runs-on: ubuntu-latest + runs-on: ubuntu-22.04 permissions: actions: read contents: read @@ -41,7 +41,7 @@ jobs: run: | mkdir -p build cd build - CC=gcc-11 CXX=g++-11 cmake \ + CC=gcc-12 CXX=g++-12 cmake \ -DCMAKE_BUILD_TYPE=$BUILD_TYPE \ -DCMAKE_INSTALL_PREFIX="$PWD/install" \ -DCMAKE_C_COMPILER_LAUNCHER=ccache \ diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 0e500f0e0..21ee0ef6a 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -106,8 +106,8 @@ jobs: # MacOS build macos: - runs-on: macos-11.0 - name: ๐ŸŽ macOS 11.0 + runs-on: macos-12 + name: ๐ŸŽ macOS 12.0 steps: - name: ๐Ÿงฐ Checkout @@ -122,8 +122,7 @@ jobs: - name: ๐Ÿ“œ Restore cache uses: actions/cache@v3 with: - path: | - ~/.ccache + path: ~/Library/Caches/ccache key: ${{ runner.os }}-${{ secrets.CACHE_VERSION }}-build-${{ hashFiles('**/CMakeLists.txt') }} restore-keys: ${{ runner.os }}-${{ secrets.CACHE_VERSION }}-build-${{ hashFiles('**/CMakeLists.txt') }} @@ -131,16 +130,20 @@ jobs: run: | mkdir -p build cd build - CC=$(brew --prefix llvm)/bin/clang \ - CXX=$(brew --prefix llvm)/bin/clang++ \ + CC=$(brew --prefix gcc@12)/bin/gcc-12 \ + CXX=$(brew --prefix gcc@12)/bin/g++-12 \ + OBJC=$(brew --prefix llvm)/bin/clang \ + OBJCXX=$(brew --prefix llvm)/bin/clang++ \ PKG_CONFIG_PATH="$(brew --prefix openssl)/lib/pkgconfig":"$(brew --prefix)/lib/pkgconfig" \ - MACOSX_DEPLOYMENT_TARGET="10.15" \ - cmake \ - -DCMAKE_BUILD_TYPE=$BUILD_TYPE \ - -DCREATE_BUNDLE=ON \ - -DCREATE_PACKAGE=ON \ - -DCMAKE_C_COMPILER_LAUNCHER=ccache \ - -DCMAKE_CXX_COMPILER_LAUNCHER=ccache \ + MACOSX_DEPLOYMENT_TARGET="10.15" \ + cmake \ + -DCMAKE_BUILD_TYPE=$BUILD_TYPE \ + -DCREATE_BUNDLE=ON \ + -DCREATE_PACKAGE=ON \ + -DCMAKE_C_COMPILER_LAUNCHER=ccache \ + -DCMAKE_CXX_COMPILER_LAUNCHER=ccache \ + -DCMAKE_OBJC_COMPILER_LAUNCHER=ccache \ + -DCMAKE_OBJCXX_COMPILER_LAUNCHER=ccache \ .. make -j4 package @@ -196,7 +199,7 @@ jobs: run: | mkdir -p build cd build - CC=gcc-11 CXX=g++-11 cmake \ + CC=gcc-12 CXX=g++-12 cmake \ -DCMAKE_BUILD_TYPE=$BUILD_TYPE \ -DCMAKE_INSTALL_PREFIX="/usr" \ -DCMAKE_C_COMPILER_LAUNCHER=ccache \ diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index d8b05ed43..a509f0b44 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -9,7 +9,7 @@ on: jobs: tests: name: ๐Ÿงช Unit Tests - runs-on: ubuntu-latest + runs-on: ubuntu-22.04 permissions: actions: read contents: read @@ -38,7 +38,7 @@ jobs: run: | mkdir -p build cd build - CC=gcc-11 CXX=g++-11 cmake \ + CC=gcc-12 CXX=g++-12 cmake \ -DCMAKE_BUILD_TYPE=$BUILD_TYPE \ -DCMAKE_INSTALL_PREFIX="$PWD/install" \ -DCMAKE_C_COMPILER_LAUNCHER=ccache \ diff --git a/cmake/build_helpers.cmake b/cmake/build_helpers.cmake index fab250eb4..26692f17f 100644 --- a/cmake/build_helpers.cmake +++ b/cmake/build_helpers.cmake @@ -77,6 +77,8 @@ macro(detectOS) set(CMAKE_INSTALL_LIBDIR ".") set(PLUGINS_INSTALL_LOCATION "plugins") set(MAGIC_INSTALL_LOCATION "magic") + enable_language(OBJC) + enable_language(OBJCXX) elseif (UNIX AND NOT APPLE) add_compile_definitions(OS_LINUX) set(CMAKE_INSTALL_BINDIR "bin") diff --git a/dist/Brewfile b/dist/Brewfile index 7c9f3ba4f..0fcd0494b 100644 --- a/dist/Brewfile +++ b/dist/Brewfile @@ -7,6 +7,5 @@ brew "python3" brew "freetype2" brew "libmagic" brew "pkg-config" - -# TODO: Remove this when XCode version of clang will support the same level as LLVM 10 brew "llvm" +brew "gcc@12" diff --git a/dist/get_deps_debian.sh b/dist/get_deps_debian.sh index 3b8f50bb7..e171db6af 100755 --- a/dist/get_deps_debian.sh +++ b/dist/get_deps_debian.sh @@ -1,9 +1,5 @@ #!/usr/bin/env sh -echo "As of 2020-12, Debian stable does not include g++-10, needs debian testing or unstable." - -# Tested on 2020-12-09 with Docker image bitnami/minideb:unstable - # Install pkgconf (adds minimum dependencies) only if the equivalent pkf-config is not already installed. if ! which pkg-config then @@ -12,8 +8,8 @@ fi apt install -y \ build-essential \ - gcc-11 \ - g++-11 \ + gcc-12 \ + g++-12 \ lld \ ${PKGCONF:-} \ cmake \ @@ -25,7 +21,4 @@ apt install -y \ libmbedtls-dev \ python3-dev \ libfreetype-dev \ - libgtk-3-dev \ - -echo "Please consider this before running cmake (useful on e.g. Ubuntu 20.04):" -echo "export CXX=g++-11" + libgtk-3-dev \ No newline at end of file diff --git a/lib/external/libromfs b/lib/external/libromfs index f14e88a72..12063078f 160000 --- a/lib/external/libromfs +++ b/lib/external/libromfs @@ -1 +1 @@ -Subproject commit f14e88a72759f7121cb611abb7afa6d6a126c7cf +Subproject commit 12063078f081694442a274d0e925b2ab204752c4 diff --git a/lib/external/pattern_language b/lib/external/pattern_language index d399836b6..3717689c3 160000 --- a/lib/external/pattern_language +++ b/lib/external/pattern_language @@ -1 +1 @@ -Subproject commit d399836b693feaab3b323acd4374c1371164c0eb +Subproject commit 3717689c374cd5e8645de258c09c15ebf7a8a91a diff --git a/lib/libimhex/CMakeLists.txt b/lib/libimhex/CMakeLists.txt index 52f52edf2..63d6829f1 100644 --- a/lib/libimhex/CMakeLists.txt +++ b/lib/libimhex/CMakeLists.txt @@ -123,7 +123,6 @@ set(LIBIMHEX_SOURCES source/helpers/patches.cpp source/helpers/project_file_handler.cpp source/helpers/encoding_file.cpp - source/helpers/loader_script_handler.cpp source/helpers/logger.cpp source/helpers/tar.cpp @@ -143,7 +142,9 @@ if (APPLE) endif () endif () - set(LIBIMHEX_SOURCES ${LIBIMHEX_SOURCES} source/helpers/fs_macos.mm) + set(LIBIMHEX_SOURCES ${LIBIMHEX_SOURCES} + source/helpers/fs_macos.m + source/helpers/utils_macos.m) endif () add_compile_definitions(IMHEX_PROJECT_NAME="${PROJECT_NAME}") diff --git a/lib/libimhex/include/hex/api/content_registry.hpp b/lib/libimhex/include/hex/api/content_registry.hpp index d9006e367..c1a15e014 100644 --- a/lib/libimhex/include/hex/api/content_registry.hpp +++ b/lib/libimhex/include/hex/api/content_registry.hpp @@ -156,7 +156,7 @@ namespace hex { } - template T, typename... Args> + template T, typename... Args> void add(Args &&...args) { return impl::add(new T(std::forward(args)...)); } @@ -234,7 +234,7 @@ namespace hex { } - template T, typename... Args> + template T, typename... Args> void add(const std::string &unlocalizedCategory, const std::string &unlocalizedName, Args &&...args) { add(impl::Entry { unlocalizedCategory.c_str(), unlocalizedName.c_str(), [=] { auto node = new T(std::forward(args)...); @@ -326,7 +326,7 @@ namespace hex { } - template T> + template T> void add(const std::string &unlocalizedName, bool addToList = true) { (void)EventManager::subscribe([expectedName = unlocalizedName](const std::string &name, hex::prv::Provider **provider) { if (name != expectedName) return; @@ -415,7 +415,7 @@ namespace hex { } - template T, typename... Args> + template T, typename... Args> void addDataVisualizer(const std::string &unlocalizedName, Args &&...args) { return impl::addDataVisualizer(unlocalizedName, new T(std::forward(args)...)); } diff --git a/lib/libimhex/include/hex/api/imhex_api.hpp b/lib/libimhex/include/hex/api/imhex_api.hpp index d50c97fb5..02bd242b4 100644 --- a/lib/libimhex/include/hex/api/imhex_api.hpp +++ b/lib/libimhex/include/hex/api/imhex_api.hpp @@ -127,7 +127,7 @@ namespace hex { void add(prv::Provider *provider); - template T> + template T> void add(auto &&...args) { add(new T(std::forward(args)...)); } diff --git a/lib/libimhex/include/hex/helpers/concepts.hpp b/lib/libimhex/include/hex/helpers/concepts.hpp index 78edb0979..9e3f57106 100644 --- a/lib/libimhex/include/hex/helpers/concepts.hpp +++ b/lib/libimhex/include/hex/helpers/concepts.hpp @@ -5,151 +5,7 @@ #include #include -namespace hex { - - template - struct is_integral_helper : public std::false_type { }; - - template<> - struct is_integral_helper : public std::true_type { }; - - template<> - struct is_integral_helper : public std::true_type { }; - - template<> - struct is_integral_helper : public std::true_type { }; - - template<> - struct is_integral_helper : public std::true_type { }; - - template<> - struct is_integral_helper : public std::true_type { }; - - template<> - struct is_integral_helper : public std::true_type { }; - - template<> - struct is_integral_helper : public std::true_type { }; - - template<> - struct is_integral_helper : public std::true_type { }; - - template<> - struct is_integral_helper : public std::true_type { }; - - template<> - struct is_integral_helper : public std::true_type { }; - - template<> - struct is_integral_helper : public std::true_type { }; - - template<> - struct is_integral_helper : public std::true_type { }; - - template<> - struct is_integral_helper : public std::true_type { }; - - template<> - struct is_integral_helper : public std::true_type { }; - - template<> - struct is_integral_helper : public std::true_type { }; - - template<> - struct is_integral_helper : public std::true_type { }; - - template - struct is_integral : public is_integral_helper>::type { }; - - - template - struct is_signed_helper : public std::false_type { }; - - template<> - struct is_signed_helper : public std::true_type { }; - - template<> - struct is_signed_helper : public std::true_type { }; - - template<> - struct is_signed_helper : public std::true_type { }; - - template<> - struct is_signed_helper : public std::true_type { }; - - template<> - struct is_signed_helper : public std::true_type { }; - - template<> - struct is_signed_helper : public std::true_type { }; - - template<> - struct is_signed_helper : public std::true_type { }; - - template<> - struct is_signed_helper : public std::true_type { }; - - template<> - struct is_signed_helper : public std::true_type { }; - - template - struct is_signed : public is_signed_helper>::type { }; - - - template - struct is_floating_point_helper : public std::false_type { }; - - template<> - struct is_floating_point_helper : public std::true_type { }; - - template<> - struct is_floating_point_helper : public std::true_type { }; - - template<> - struct is_floating_point_helper : public std::true_type { }; - - template - struct is_floating_point : public is_floating_point_helper>::type { }; - -} - -#if defined(_LIBCPP_VERSION) && _LIBCPP_VERSION <= 12000 - #if __has_include() - // Make sure we break when derived_from is implemented in libc++. Then we can fix a compatibility version above - #include - #endif -// libcxx 12 still doesn't have many default concepts implemented, as a result we need to define it ourself using clang built-ins. -// [concept.derived] (patch from https://reviews.llvm.org/D74292) -namespace hex { - template - concept derived_from = - __is_base_of(_Bp, _Dp) && __is_convertible_to(const volatile _Dp *, const volatile _Bp *); -} - -#else - // Assume supported - #include -namespace hex { - using std::derived_from; -} -#endif - -// [concepts.arithmetic] -namespace hex { - - template - concept integral = hex::is_integral::value; - - template - concept signed_integral = integral && hex::is_signed::value; - - template - concept unsigned_integral = integral && !signed_integral; - - template - concept floating_point = std::is_floating_point::value; - -} +#include namespace hex { @@ -159,11 +15,6 @@ namespace hex { template concept has_size = sizeof(T) == Size; -} - - -namespace hex { - template class Cloneable { public: diff --git a/lib/libimhex/include/hex/helpers/fs_macos.h b/lib/libimhex/include/hex/helpers/fs_macos.h deleted file mode 100644 index a8a16a299..000000000 --- a/lib/libimhex/include/hex/helpers/fs_macos.h +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once - -#if defined(OS_MACOS) - #include - -namespace hex { - std::string getMacExecutableDirectoryPath(); - std::string getMacApplicationSupportDirectoryPath(); -} -#endif diff --git a/lib/libimhex/include/hex/helpers/fs_macos.hpp b/lib/libimhex/include/hex/helpers/fs_macos.hpp new file mode 100644 index 000000000..1a1ae2c74 --- /dev/null +++ b/lib/libimhex/include/hex/helpers/fs_macos.hpp @@ -0,0 +1,12 @@ +#pragma once + +#if defined(OS_MACOS) + + #include + + extern "C" char * getMacExecutableDirectoryPath(); + extern "C" char * getMacApplicationSupportDirectoryPath(); + + extern "C" void macFree(void *ptr); + +#endif diff --git a/lib/libimhex/include/hex/helpers/loader_script_handler.hpp b/lib/libimhex/include/hex/helpers/loader_script_handler.hpp deleted file mode 100644 index bf8b51c9c..000000000 --- a/lib/libimhex/include/hex/helpers/loader_script_handler.hpp +++ /dev/null @@ -1,38 +0,0 @@ -#pragma once - -#include -#include - -#include - -struct _object; -typedef struct _object PyObject; - -namespace hex { - - namespace prv { - class Provider; - } - - class LoaderScript { - public: - LoaderScript() = delete; - - static bool processFile(const std::fs::path &scriptPath); - - static void setFilePath(const std::fs::path &filePath) { LoaderScript::s_filePath = filePath; } - static void setDataProvider(prv::Provider *provider) { LoaderScript::s_dataProvider = provider; } - - private: - static inline std::fs::path s_filePath; - static inline prv::Provider *s_dataProvider; - - static PyObject *Py_getFilePath(PyObject *self, PyObject *args); - static PyObject *Py_addPatch(PyObject *self, PyObject *args); - static PyObject *Py_addBookmark(PyObject *self, PyObject *args); - - static PyObject *Py_addStruct(PyObject *self, PyObject *args); - static PyObject *Py_addUnion(PyObject *self, PyObject *args); - }; - -} \ No newline at end of file diff --git a/lib/libimhex/include/hex/helpers/utils.hpp b/lib/libimhex/include/hex/helpers/utils.hpp index b20be7ae9..5f4e29afc 100644 --- a/lib/libimhex/include/hex/helpers/utils.hpp +++ b/lib/libimhex/include/hex/helpers/utils.hpp @@ -43,7 +43,7 @@ namespace hex { std::string encodeByteString(const std::vector &bytes); std::vector decodeByteString(const std::string &string); - [[nodiscard]] constexpr inline u64 extract(u8 from, u8 to, const hex::unsigned_integral auto &value) { + [[nodiscard]] constexpr inline u64 extract(u8 from, u8 to, const std::unsigned_integral auto &value) { if (from < to) std::swap(from, to); using ValueType = std::remove_cvref_t; @@ -72,7 +72,7 @@ namespace hex { return (value ^ mask) - mask; } - template + template constexpr inline T swapBitOrder(size_t numBits, T value) { T result = 0x00; @@ -200,7 +200,7 @@ namespace hex { return T(1) << bit_width(T(x - 1)); } - template + template auto powi(T base, U exp) { using ResultType = decltype(T{} * U{}); @@ -264,7 +264,7 @@ namespace hex { return result; } - inline std::string toBinaryString(hex::unsigned_integral auto number) { + inline std::string toBinaryString(std::unsigned_integral auto number) { if (number == 0) return "0"; std::string result; @@ -317,7 +317,7 @@ namespace hex { return *value; } - template + template T alignTo(T value, T alignment) { T remainder = value % alignment; diff --git a/lib/libimhex/include/hex/helpers/utils_macos.hpp b/lib/libimhex/include/hex/helpers/utils_macos.hpp new file mode 100644 index 000000000..8a3b3b055 --- /dev/null +++ b/lib/libimhex/include/hex/helpers/utils_macos.hpp @@ -0,0 +1,9 @@ +#pragma once + +#if defined(OS_MACOS) + + #include + + extern "C" void openWebpageMacos(const char *url); + +#endif \ No newline at end of file diff --git a/lib/libimhex/source/helpers/crypto.cpp b/lib/libimhex/source/helpers/crypto.cpp index 5e9d0b83b..5a02c98e3 100644 --- a/lib/libimhex/source/helpers/crypto.cpp +++ b/lib/libimhex/source/helpers/crypto.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -15,7 +16,6 @@ #include #include -#include #include #include #include diff --git a/lib/libimhex/source/helpers/fs.cpp b/lib/libimhex/source/helpers/fs.cpp index ac1f27fde..1f8159764 100644 --- a/lib/libimhex/source/helpers/fs.cpp +++ b/lib/libimhex/source/helpers/fs.cpp @@ -1,7 +1,7 @@ #include #include -#include +#include #include #include @@ -34,7 +34,15 @@ namespace hex::fs { return exePath; #elif defined(OS_MACOS) - return getMacExecutableDirectoryPath(); + std::string result; + + { + auto string = getMacExecutableDirectoryPath(); + result = string; + macFree(string); + } + + return result; #else return std::nullopt; #endif @@ -183,25 +191,31 @@ namespace hex::fs { } #elif defined(OS_MACOS) // Get path to special directories - const std::fs::path applicationSupportDir(getMacApplicationSupportDirectoryPath()); + std::string applicationSupportDir; + { + auto string = getMacApplicationSupportDirectoryPath(); + applicationSupportDir = string; + macFree(string); + } + const std::fs::path applicationSupportDirPath(applicationSupportDir); - std::vector paths = { applicationSupportDir }; + std::vector paths = { applicationSupportDirPath }; if (exePath.has_value()) paths.push_back(exePath.value()); switch (path) { case ImHexPath::Patterns: - result.push_back((applicationSupportDir / "patterns").string()); + result.push_back((applicationSupportDirPath / "patterns").string()); break; case ImHexPath::PatternsInclude: - result.push_back((applicationSupportDir / "includes").string()); + result.push_back((applicationSupportDirPath / "includes").string()); break; case ImHexPath::Magic: - result.push_back((applicationSupportDir / "magic").string()); + result.push_back((applicationSupportDirPath / "magic").string()); break; case ImHexPath::Python: - result.push_back((applicationSupportDir / "python").string()); + result.push_back((applicationSupportDirPath / "python").string()); break; case ImHexPath::Plugins: std::transform(paths.begin(), paths.end(), std::back_inserter(result), [](auto &path) { @@ -209,22 +223,22 @@ namespace hex::fs { }); break; case ImHexPath::Yara: - result.push_back((applicationSupportDir / "yara").string()); + result.push_back((applicationSupportDirPath / "yara").string()); break; case ImHexPath::Config: - result.push_back((applicationSupportDir / "config").string()); + result.push_back((applicationSupportDirPath / "config").string()); break; case ImHexPath::Resources: - result.push_back((applicationSupportDir / "resources").string()); + result.push_back((applicationSupportDirPath / "resources").string()); break; case ImHexPath::Constants: - result.push_back((applicationSupportDir / "constants").string()); + result.push_back((applicationSupportDirPath / "constants").string()); break; case ImHexPath::Encodings: - result.push_back((applicationSupportDir / "encodings").string()); + result.push_back((applicationSupportDirPath / "encodings").string()); break; case ImHexPath::Logs: - result.push_back((applicationSupportDir / "logs").string()); + result.push_back((applicationSupportDirPath / "logs").string()); break; default: hex::unreachable(); diff --git a/lib/libimhex/source/helpers/fs_macos.m b/lib/libimhex/source/helpers/fs_macos.m new file mode 100644 index 000000000..665b7493f --- /dev/null +++ b/lib/libimhex/source/helpers/fs_macos.m @@ -0,0 +1,44 @@ +#if defined(OS_MACOS) + + #include + #include + #include + + char* getMacExecutableDirectoryPath() { + @autoreleasepool { + const char *pathString = [[[[[NSBundle mainBundle] executableURL] URLByDeletingLastPathComponent] path] UTF8String]; + + char *result = malloc(strlen(pathString) + 1); + strcpy(result, pathString); + + return result; + } + } + + char* getMacApplicationSupportDirectoryPath() { + @autoreleasepool { + NSError* error = nil; + NSURL* dirUrl = [[NSFileManager defaultManager] URLForDirectory:NSApplicationSupportDirectory + inDomain:NSUserDomainMask + appropriateForURL:nil + create:YES + error:&error]; + + if (error != nil) { + return NULL; + } + + const char *pathString = [[[dirUrl URLByAppendingPathComponent:(@"imhex")] path] UTF8String]; + + char *result = malloc(strlen(pathString) + 1); + strcpy(result, pathString); + + return result; + } + } + + void macFree(void *ptr) { + free(ptr); + } + +#endif \ No newline at end of file diff --git a/lib/libimhex/source/helpers/fs_macos.mm b/lib/libimhex/source/helpers/fs_macos.mm deleted file mode 100644 index 4aa869399..000000000 --- a/lib/libimhex/source/helpers/fs_macos.mm +++ /dev/null @@ -1,30 +0,0 @@ -#if defined(OS_MACOS) - #include - - #include - - namespace hex { - std::string getMacExecutableDirectoryPath() { - @autoreleasepool { - return {[[[[[NSBundle mainBundle] executableURL] URLByDeletingLastPathComponent] path] UTF8String]}; - } - } - - std::string getMacApplicationSupportDirectoryPath() { - @autoreleasepool { - NSError* error = nil; - NSURL* dirUrl = [[NSFileManager defaultManager] URLForDirectory:NSApplicationSupportDirectory - inDomain:NSUserDomainMask - appropriateForURL:nil - create:YES - error:&error]; - - if (error != nil) { - __builtin_unreachable(); - } - - return {[[[dirUrl URLByAppendingPathComponent:(@"imhex")] path] UTF8String]}; - } - } - } -#endif diff --git a/lib/libimhex/source/helpers/loader_script_handler.cpp b/lib/libimhex/source/helpers/loader_script_handler.cpp deleted file mode 100644 index 15c532127..000000000 --- a/lib/libimhex/source/helpers/loader_script_handler.cpp +++ /dev/null @@ -1,248 +0,0 @@ -#include - -#include -#include -#include -#include -#include -#include - -#define PY_SSIZE_T_CLEAN -#include -#include - -#include -#include - -using namespace std::literals::string_literals; - -namespace hex { - - PyObject *LoaderScript::Py_getFilePath(PyObject *self, PyObject *args) { - hex::unused(self, args); - - return PyUnicode_FromString(LoaderScript::s_filePath.string().c_str()); - } - - PyObject *LoaderScript::Py_addPatch(PyObject *self, PyObject *args) { - hex::unused(self); - - u64 address; - u8 *patches; - Py_ssize_t count; - - if (!PyArg_ParseTuple(args, "K|y#", &address, &patches, &count)) { - PyErr_BadArgument(); - return nullptr; - } - - if (patches == nullptr || count == 0) { - PyErr_SetString(PyExc_TypeError, "Invalid patch provided"); - return nullptr; - } - - if (address >= LoaderScript::s_dataProvider->getActualSize()) { - PyErr_SetString(PyExc_IndexError, "address out of range"); - return nullptr; - } - - LoaderScript::s_dataProvider->write(address, patches, count); - - Py_RETURN_NONE; - } - - PyObject *LoaderScript::Py_addBookmark(PyObject *self, PyObject *args) { - hex::unused(self); - - u64 address; - size_t size; - - char *name = nullptr; - char *comment = nullptr; - - if (!PyArg_ParseTuple(args, "K|n|s|s", &address, &size, &name, &comment)) { - PyErr_BadArgument(); - return nullptr; - } - - if (name == nullptr || comment == nullptr) { - PyErr_SetString(PyExc_IndexError, "address out of range"); - return nullptr; - } - - ImHexApi::Bookmarks::add(address, size, name, comment); - - Py_RETURN_NONE; - } - - static PyObject *createStructureType(const std::string &keyword, PyObject *args) { - if (args == nullptr) { - PyErr_BadArgument(); - return nullptr; - } - - auto type = PyTuple_GetItem(args, 0); - if (type == nullptr) { - PyErr_BadArgument(); - return nullptr; - } - - auto instance = PyObject_CallObject(type, nullptr); - if (instance == nullptr) { - PyErr_BadArgument(); - return nullptr; - } - - ON_SCOPE_EXIT { Py_DECREF(instance); }; - - if (instance->ob_type->tp_base == nullptr || instance->ob_type->tp_base->tp_name != "ImHexType"s) { - PyErr_SetString(PyExc_TypeError, "class type must extend from ImHexType"); - return nullptr; - } - - auto dict = instance->ob_type->tp_dict; - if (dict == nullptr) { - PyErr_BadArgument(); - return nullptr; - } - - auto annotations = PyDict_GetItemString(dict, "__annotations__"); - if (annotations == nullptr) { - PyErr_BadArgument(); - return nullptr; - } - - auto list = PyDict_Items(annotations); - if (list == nullptr) { - PyErr_BadArgument(); - return nullptr; - } - - ON_SCOPE_EXIT { Py_DECREF(list); }; - - std::string code = keyword + " " + instance->ob_type->tp_name + " {\n"; - - for (Py_ssize_t i = 0; i < PyList_Size(list); i++) { - auto item = PyList_GetItem(list, i); - if (item == nullptr) { - PyErr_SetString(PyExc_TypeError, "failed to get item from list"); - return nullptr; - } - - auto memberName = PyUnicode_AsUTF8(PyTuple_GetItem(item, 0)); - if (memberName == nullptr) { - PyErr_SetString(PyExc_TypeError, "invalid member name"); - return nullptr; - } - - auto memberType = PyTuple_GetItem(item, 1); - if (!PyTuple_Check(memberType) || memberType == nullptr) { - PyErr_SetString(PyExc_TypeError, "member needs to have a annotation extending from ImHexType"); - return nullptr; - } - - // Array already is an object - if (memberType->ob_type->tp_name == "array"s) { - - auto arrayType = PyObject_GetAttrString(memberType, "array_type"); - if (arrayType == nullptr) { - PyErr_BadArgument(); - return nullptr; - } - - code += " "s + arrayType->ob_type->tp_name + " " + memberName; - - auto arraySize = PyObject_GetAttrString(memberType, "size"); - if (arraySize == nullptr) { - PyErr_BadArgument(); - return nullptr; - } - - if (PyUnicode_Check(arraySize)) - code += "["s + PyUnicode_AsUTF8(arraySize) + "];\n"; - else if (PyLong_Check(arraySize)) - code += "["s + std::to_string(PyLong_AsLong(arraySize)) + "];\n"; - } else { - auto memberTypeInstance = PyObject_CallObject(memberType, nullptr); - if (memberTypeInstance == nullptr || memberTypeInstance->ob_type->tp_base == nullptr || memberTypeInstance->ob_type->tp_base->tp_name != "ImHexType"s) { - PyErr_SetString(PyExc_TypeError, "member needs to have a annotation extending from ImHexType"); - if (memberTypeInstance != nullptr) - Py_DECREF(memberTypeInstance); - - return nullptr; - } - - code += " "s + memberTypeInstance->ob_type->tp_name + " "s + memberName + ";\n"; - - Py_DECREF(memberTypeInstance); - } - } - - code += "};\n"; - - EventManager::post(code); - - Py_RETURN_NONE; - } - - PyObject *LoaderScript::Py_addStruct(PyObject *self, PyObject *args) { - hex::unused(self); - - return createStructureType("struct", args); - } - - PyObject *LoaderScript::Py_addUnion(PyObject *self, PyObject *args) { - hex::unused(self); - - return createStructureType("union", args); - } - - bool LoaderScript::processFile(const std::fs::path &scriptPath) { - Py_SetProgramName(Py_DecodeLocale("ImHex", nullptr)); - - for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Python)) { - if (fs::exists(std::fs::path(dir / "lib" / "python" PYTHON_VERSION_MAJOR_MINOR))) { - Py_SetPythonHome(Py_DecodeLocale(dir.string().c_str(), nullptr)); - break; - } - } - - PyImport_AppendInittab("_imhex", []() -> PyObject * { - static PyMethodDef ImHexMethods[] = { - {"get_file_path", &LoaderScript::Py_getFilePath, METH_NOARGS, "Returns the path of the file being loaded."}, - { "patch", &LoaderScript::Py_addPatch, METH_VARARGS, "Patches a region of memory" }, - { "add_bookmark", &LoaderScript::Py_addBookmark, METH_VARARGS, "Adds a bookmark" }, - { "add_struct", &LoaderScript::Py_addStruct, METH_VARARGS, "Adds a struct" }, - { "add_union", &LoaderScript::Py_addUnion, METH_VARARGS, "Adds a union" }, - { nullptr, nullptr, 0, nullptr } - }; - - static PyModuleDef ImHexModule = { - PyModuleDef_HEAD_INIT, "imhex", nullptr, -1, ImHexMethods, nullptr, nullptr, nullptr, nullptr - }; - - auto module = PyModule_Create(&ImHexModule); - if (module == nullptr) - return nullptr; - - return module; - }); - - Py_Initialize(); - - { - auto sysPath = PySys_GetObject("path"); - auto path = PyUnicode_FromString("lib"); - - PyList_Insert(sysPath, 0, path); - } - - fs::File scriptFile(scriptPath, fs::File::Mode::Read); - PyRun_SimpleFile(scriptFile.getHandle(), scriptFile.getPath().string().c_str()); - - Py_Finalize(); - - return true; - } - -} \ No newline at end of file diff --git a/lib/libimhex/source/helpers/utils.cpp b/lib/libimhex/source/helpers/utils.cpp index 48d5f0e04..c08a46132 100644 --- a/lib/libimhex/source/helpers/utils.cpp +++ b/lib/libimhex/source/helpers/utils.cpp @@ -2,8 +2,6 @@ #include #include -#include -#include #include @@ -18,13 +16,10 @@ #elif defined(OS_LINUX) #include #elif defined(OS_MACOS) - #include - #include + #include + #include #endif -#include -#include - namespace hex { long double operator""_scaled(long double value) { @@ -252,13 +247,14 @@ namespace hex { void runCommand(const std::string &command) { -#if defined(OS_WINDOWS) - auto result = system(hex::format("start {0}", command).c_str()); -#elif defined(OS_MACOS) - auto result = system(hex::format("open {0}", command).c_str()); -#elif defined(OS_LINUX) - auto result = system(hex::format("xdg-open {0}", command).c_str()); -#endif + #if defined(OS_WINDOWS) + auto result = system(hex::format("start {0}", command).c_str()); + #elif defined(OS_MACOS) + auto result = system(hex::format("open {0}", command).c_str()); + #elif defined(OS_LINUX) + auto result = system(hex::format("xdg-open {0}", command).c_str()); + #endif + hex::unused(result); } @@ -267,18 +263,16 @@ namespace hex { if (url.find("://") == std::string::npos) url = "https://" + url; -#if defined(OS_WINDOWS) - ShellExecute(nullptr, "open", url.c_str(), nullptr, nullptr, SW_SHOWNORMAL); -#elif defined(OS_MACOS) - CFURLRef urlRef = CFURLCreateWithBytes(nullptr, reinterpret_cast(url.data()), url.length(), kCFStringEncodingASCII, nullptr); - LSOpenCFURLRef(urlRef, nullptr); - CFRelease(urlRef); -#elif defined(OS_LINUX) - auto result = system(hex::format("xdg-open {0}", url).c_str()); - hex::unused(result); -#else - #warning "Unknown OS, can't open webpages" -#endif + #if defined(OS_WINDOWS) + ShellExecute(nullptr, "open", url.c_str(), nullptr, nullptr, SW_SHOWNORMAL); + #elif defined(OS_MACOS) + openWebpageMacos(url.c_str()); + #elif defined(OS_LINUX) + auto result = system(hex::format("xdg-open {0}", url).c_str()); + hex::unused(result); + #else + #warning "Unknown OS, can't open webpages" + #endif } std::string encodeByteString(const std::vector &bytes) { diff --git a/lib/libimhex/source/helpers/utils_macos.m b/lib/libimhex/source/helpers/utils_macos.m new file mode 100644 index 000000000..a1799ff04 --- /dev/null +++ b/lib/libimhex/source/helpers/utils_macos.m @@ -0,0 +1,16 @@ +#if defined(OS_MACOS) + + #include + #include + + #include + #include + #include + + void openWebpageMacos(const char *url) { + CFURLRef urlRef = CFURLCreateWithBytes(NULL, (uint8_t*)(url), strlen(url), kCFStringEncodingASCII, NULL); + LSOpenCFURLRef(urlRef, NULL); + CFRelease(urlRef); + } + +#endif \ No newline at end of file diff --git a/main/source/init/tasks.cpp b/main/source/init/tasks.cpp index 67e487aa9..b2f66fbbb 100644 --- a/main/source/init/tasks.cpp +++ b/main/source/init/tasks.cpp @@ -177,16 +177,23 @@ namespace hex::init { ImHexApi::HexEditor::impl::getBackgroundHighlightingFunctions().clear(); ImHexApi::HexEditor::impl::getForegroundHighlightingFunctions().clear(); ImHexApi::HexEditor::impl::getTooltips().clear(); + ImHexApi::HexEditor::impl::getTooltipFunctions().clear(); ContentRegistry::Settings::getEntries().clear(); ContentRegistry::Settings::getSettingsData().clear(); ContentRegistry::CommandPaletteCommands::getEntries().clear(); - ContentRegistry::PatternLanguage::getFunctions().clear(); - for (auto &[name, view] : ContentRegistry::Views::getEntries()) - delete view; - ContentRegistry::Views::getEntries().clear(); + ContentRegistry::PatternLanguage::getFunctions().clear(); + ContentRegistry::PatternLanguage::getPragmas().clear(); + + { + auto &views = ContentRegistry::Views::getEntries(); + for (auto &[name, view] : views) + delete view; + views.clear(); + } + ContentRegistry::Tools::getEntries().clear(); ContentRegistry::DataInspector::getEntries().clear(); @@ -213,6 +220,13 @@ namespace hex::init { ContentRegistry::DataFormatter::getEntries().clear(); ContentRegistry::FileHandler::getEntries().clear(); + { + auto &visualizers = ContentRegistry::HexEditor::impl::getVisualizers(); + for (auto &[name, visualizer] : visualizers) + delete visualizer; + visualizers.clear(); + } + while (ImHexApi::Provider::isValid()) ImHexApi::Provider::remove(ImHexApi::Provider::get()); ContentRegistry::Provider::getEntries().clear(); diff --git a/plugins/builtin/source/content/data_visualizers.cpp b/plugins/builtin/source/content/data_visualizers.cpp index 0826af324..41a9e2a98 100644 --- a/plugins/builtin/source/content/data_visualizers.cpp +++ b/plugins/builtin/source/content/data_visualizers.cpp @@ -22,7 +22,7 @@ namespace hex::plugin::builtin { else static_assert(hex::always_false::value, "Invalid data type!"); } - template + template class DataVisualizerHexadecimal : public hex::ContentRegistry::HexEditor::DataVisualizer { public: DataVisualizerHexadecimal() : DataVisualizer(ByteCount, CharCount) { } @@ -114,7 +114,7 @@ namespace hex::plugin::builtin { } }; - template + template class DataVisualizerDecimal : public hex::ContentRegistry::HexEditor::DataVisualizer { public: DataVisualizerDecimal() : DataVisualizer(ByteCount, CharCount) { } @@ -123,7 +123,7 @@ namespace hex::plugin::builtin { hex::unused(address, upperCase); if (size == ByteCount) { - if (hex::is_signed::value) + if (std::is_signed::value) ImGui::Text(getFormatString(), static_cast(*reinterpret_cast(data))); else ImGui::Text(getFormatString(), static_cast(*reinterpret_cast(data))); @@ -153,14 +153,14 @@ namespace hex::plugin::builtin { constexpr static inline auto ByteCount = sizeof(T); constexpr static inline auto CharCount = std::numeric_limits::digits10 + 2; - const static inline auto FormatString = hex::format("%{}{}", CharCount, hex::is_signed::value ? "lld" : "llu"); + const static inline auto FormatString = hex::format("%{}{}", CharCount, std::is_signed::value ? "lld" : "llu"); const char *getFormatString() { return FormatString.c_str(); } }; - template + template class DataVisualizerFloatingPoint : public hex::ContentRegistry::HexEditor::DataVisualizer { public: DataVisualizerFloatingPoint() : DataVisualizer(ByteCount, CharCount) { } diff --git a/plugins/builtin/source/content/views/view_hex_editor.cpp b/plugins/builtin/source/content/views/view_hex_editor.cpp index 739596e7d..cf250981f 100644 --- a/plugins/builtin/source/content/views/view_hex_editor.cpp +++ b/plugins/builtin/source/content/views/view_hex_editor.cpp @@ -199,11 +199,7 @@ namespace hex::plugin::builtin { reader.seek(this->m_searchPosition.value_or(editor->getSelection().getEndAddress())); constexpr static auto searchFunction = [](const auto &haystackBegin, const auto &haystackEnd, const auto &needleBegin, const auto &needleEnd) { - #if defined(OS_MACOS) - return std::search(haystackBegin, haystackEnd, needleBegin, needleEnd); - #else - return std::search(haystackBegin, haystackEnd, std::boyer_moore_horspool_searcher(needleBegin, needleEnd)); - #endif + return std::search(haystackBegin, haystackEnd, std::boyer_moore_horspool_searcher(needleBegin, needleEnd)); }; if (!backwards) { diff --git a/plugins/builtin/source/math_evaluator.cpp b/plugins/builtin/source/math_evaluator.cpp index 0a035a23c..1c230c851 100644 --- a/plugins/builtin/source/math_evaluator.cpp +++ b/plugins/builtin/source/math_evaluator.cpp @@ -117,11 +117,11 @@ namespace hex { for (char *pos = prevPos; *pos != 0x00;) { if (std::isdigit(*pos) || *pos == '.') { auto number = [&] { - if constexpr (hex::floating_point) + if constexpr (std::floating_point) return std::strtold(pos, &pos); - else if constexpr (hex::signed_integral) + else if constexpr (std::signed_integral) return std::strtoll(pos, &pos, 10); - else if constexpr (hex::unsigned_integral) + else if constexpr (std::unsigned_integral) return std::strtoull(pos, &pos, 10); else static_assert(hex::always_false::value, "Can't parse literal of this type"); @@ -440,7 +440,7 @@ namespace hex { template void MathEvaluator::registerStandardFunctions() { - if constexpr (hex::floating_point) { + if constexpr (std::floating_point) { this->setFunction( "sin", [](auto args) { return std::sin(args[0]); }, 1, 1); this->setFunction(