From 1fbabe36dca1cb080696a5ba25cd358f90a3f123 Mon Sep 17 00:00:00 2001 From: Smiril Date: Sat, 11 May 2024 21:11:50 +0200 Subject: [PATCH 01/42] Create cmakeMacOSMakeGcc.sh add MacOS Support --- _Build/cmakeMacOSMakeGcc.sh | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 _Build/cmakeMacOSMakeGcc.sh diff --git a/_Build/cmakeMacOSMakeGcc.sh b/_Build/cmakeMacOSMakeGcc.sh new file mode 100644 index 0000000..74d2df9 --- /dev/null +++ b/_Build/cmakeMacOSMakeGcc.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash + +PS3='Please enter your choice: ' +options=("Debug" "Debug Final" "Release" "Release Final" "Exit") +select opt in "${options[@]}" +do + case $opt in + "Debug") + DirName=_DebugMacOSMakeGcc + Options="-DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -D CMAKE_BUILD_TYPE=Debug -D CMAKE_INSTALL_PREFIX=_bin ../../source" + ;; + "Debug Final") + DirName=_DebugFinalMacOSMakeGcc + Options="-DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -D CMAKE_BUILD_TYPE=Debug -D KYTY_FINAL=1 -D CMAKE_INSTALL_PREFIX=_bin ../../source" + ;; + "Release") + DirName=_ReleaseMacOSMakeGcc + Options="-DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=_bin ../../source" + ;; + "Release Final") + DirName=_ReleaseFinalMacOSMakeGcc + Options="-DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -D CMAKE_BUILD_TYPE=Release -D KYTY_FINAL=1 -D CMAKE_INSTALL_PREFIX=_bin ../../source" + ;; + esac + break +done + +if [ -z "$DirName" ]; then + exit +fi + +mkdir $DirName +cd $DirName +echo make >_build +echo make install/strip >>_build +chmod +x _build +cmake -G "Unix Makefiles" $Options From d07e401f9d47b91a10ead7faa3db8d26d62f9c99 Mon Sep 17 00:00:00 2001 From: Smiril Date: Sat, 11 May 2024 21:12:32 +0200 Subject: [PATCH 02/42] Rename cmakeMacOSMakeGcc.sh to cmake_MacOSMakeGcc.sh typo fix --- _Build/{cmakeMacOSMakeGcc.sh => cmake_MacOSMakeGcc.sh} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename _Build/{cmakeMacOSMakeGcc.sh => cmake_MacOSMakeGcc.sh} (100%) diff --git a/_Build/cmakeMacOSMakeGcc.sh b/_Build/cmake_MacOSMakeGcc.sh similarity index 100% rename from _Build/cmakeMacOSMakeGcc.sh rename to _Build/cmake_MacOSMakeGcc.sh From 368ecb8a68725cfea57ba55d13401c891227a7c5 Mon Sep 17 00:00:00 2001 From: Smiril Date: Sat, 11 May 2024 21:15:47 +0200 Subject: [PATCH 03/42] Create SysMacOSDbg.h --- source/include/Kyty/Sys/MacOS/SysMacOSDbg.h | 35 +++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 source/include/Kyty/Sys/MacOS/SysMacOSDbg.h diff --git a/source/include/Kyty/Sys/MacOS/SysMacOSDbg.h b/source/include/Kyty/Sys/MacOS/SysMacOSDbg.h new file mode 100644 index 0000000..2276afb --- /dev/null +++ b/source/include/Kyty/Sys/MacOS/SysMacOSDbg.h @@ -0,0 +1,35 @@ +#ifndef SYS_MACOS_INCLUDE_KYTY_SYSDBG_H_ +#define SYS_MACOS_INCLUDE_KYTY_SYSDBG_H_ + +// IWYU pragma: private + +#if KYTY_PLATFORM != KYTY_PLATFORM_MACOS +//#error "KYTY_PLATFORM != KYTY_PLATFORM_LINUX" +#else + +namespace Kyty { + +// NOLINTNEXTLINE(readability-identifier-naming) +struct sys_dbg_stack_info_t +{ + uintptr_t code_addr; + uintptr_t addr; + uintptr_t commited_addr; + size_t commited_size; + size_t total_size; + size_t code_size; +}; + +using exception_filter_func_t = void (*)(void* addr); + +void sys_stack_walk(void** stack, int* depth); +void sys_stack_usage(sys_dbg_stack_info_t& s); +void sys_stack_usage_print(sys_dbg_stack_info_t& stack); +void sys_get_code_info(uintptr_t* addr, size_t* size); +void sys_set_exception_filter(exception_filter_func_t func); + +} // namespace Kyty + +#endif + +#endif /* SYS_LINUX_INCLUDE_KYTY_SYSDBG_H_ */ From 29c0b9e8eb8c20fdb7710f15b0ae8d95dae75aee Mon Sep 17 00:00:00 2001 From: Smiril Date: Sat, 11 May 2024 21:18:22 +0200 Subject: [PATCH 04/42] Create SysMacOSFileIO.h --- .../include/Kyty/Sys/MacOS/SysMacOSFileIO.h | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 source/include/Kyty/Sys/MacOS/SysMacOSFileIO.h diff --git a/source/include/Kyty/Sys/MacOS/SysMacOSFileIO.h b/source/include/Kyty/Sys/MacOS/SysMacOSFileIO.h new file mode 100644 index 0000000..6745e5b --- /dev/null +++ b/source/include/Kyty/Sys/MacOS/SysMacOSFileIO.h @@ -0,0 +1,110 @@ +#ifndef SYS_MACOS_INCLUDE_KYTY_SYSFILEIO_H_ +#define SYS_MACOS_INCLUDE_KYTY_SYSFILEIO_H_ + +// IWYU pragma: private + +#if KYTY_PLATFORM != KYTY_PLATFORM_MACOS +//#error "KYTY_PLATFORM != KYTY_PLATFORM_LINUX" +#else + +#include "Kyty/Core/String.h" +#include "Kyty/Sys/MacOS/SysMacOSTimer.h" + +namespace Kyty { + +// NOLINTNEXTLINE(readability-identifier-naming) +enum sys_file_type_t +{ + SYS_FILE_ERROR, // NOLINT(readability-identifier-naming) + SYS_FILE_MEMORY_STAT, // NOLINT(readability-identifier-naming) + SYS_FILE_FILE, // NOLINT(readability-identifier-naming) + SYS_FILE_MEMORY_DYN // NOLINT(readability-identifier-naming) +}; + +// NOLINTNEXTLINE(readability-identifier-naming) +enum sys_file_cache_type_t +{ + SYS_FILE_CACHE_AUTO = 0, // NOLINT(readability-identifier-naming) + SYS_FILE_CACHE_RANDOM_ACCESS = 1, // NOLINT(readability-identifier-naming) + SYS_FILE_CACHE_SEQUENTIAL_SCAN = 2 // NOLINT(readability-identifier-naming) +}; + +// NOLINTNEXTLINE(readability-identifier-naming) +struct sys_file_mem_buf_t +{ + uint8_t* base; + uint8_t* ptr; + uint32_t size; +}; + +// NOLINTNEXTLINE(readability-identifier-naming) +struct sys_file_t +{ + sys_file_type_t type; + union + { + FILE* f; + sys_file_mem_buf_t* buf; + }; +}; + +// NOLINTNEXTLINE(readability-identifier-naming) +struct sys_file_find_t +{ + String path_with_name; + SysFileTimeStruct last_access_time; + SysFileTimeStruct last_write_time; + uint64_t size; +}; + +// NOLINTNEXTLINE(readability-identifier-naming) +struct sys_dir_entry_t +{ + String name; + bool is_file; +}; + +void sys_file_read(void* data, uint32_t size, sys_file_t& f, uint32_t* bytes_read = nullptr); +void sys_file_write(const void* data, uint32_t size, sys_file_t& f, uint32_t* bytes_written = nullptr); +void sys_file_read_r(void* data, uint32_t size, sys_file_t& f); +void sys_file_write_r(const void* data, uint32_t size, sys_file_t& f); +sys_file_t* sys_file_create(const String& file_name); +sys_file_t* sys_file_open_r(const String& file_name, sys_file_cache_type_t cache_type = SYS_FILE_CACHE_AUTO); +sys_file_t* sys_file_open_w(const String& file_name, sys_file_cache_type_t cache_type = SYS_FILE_CACHE_AUTO); +sys_file_t* sys_file_open(uint8_t* buf, uint32_t buf_size); +sys_file_t* sys_file_create(); +sys_file_t* sys_file_open_rw(const String& file_name, sys_file_cache_type_t cache_type = SYS_FILE_CACHE_AUTO); +void sys_file_close(sys_file_t* f); +uint64_t sys_file_size(sys_file_t& f); +bool sys_file_seek(sys_file_t& f, uint64_t offset); +uint64_t sys_file_tell(sys_file_t& f); +bool sys_file_truncate(sys_file_t& f, uint64_t size); +void sys_file_write(uint32_t n, sys_file_t& f); +void sys_file_write_r(uint32_t n, sys_file_t& f); +uint64_t sys_file_size(const String& file_name); +bool sys_file_is_error(sys_file_t& f); +bool sys_file_io_init(); +bool sys_file_is_directory_existing(const String& path); +bool sys_file_is_file_existing(const String& name); +bool sys_file_create_directory(const String& path); +bool sys_file_delete_directory(const String& path); +bool sys_file_delete_file(const String& name); +bool sys_file_flush(sys_file_t& f); +SysFileTimeStruct sys_file_get_last_access_time_utc(const String& name); +SysFileTimeStruct sys_file_get_last_write_time_utc(const String& name); +void sys_file_get_last_access_and_write_time_utc(const String& name, SysFileTimeStruct& a, SysFileTimeStruct& w); +void sys_file_get_last_access_and_write_time_utc(sys_file_t& f, SysFileTimeStruct& a, SysFileTimeStruct& w); +bool sys_file_set_last_access_time_utc(const String& name, SysFileTimeStruct& access); +bool sys_file_set_last_write_time_utc(const String& name, SysFileTimeStruct& write); +bool sys_file_set_last_access_and_write_time_utc(const String& name, SysFileTimeStruct& access, SysFileTimeStruct& write); +void sys_file_find_files(const String& path, Kyty::Vector& out); +void sys_file_get_dents(const String& path, Kyty::Vector& out); +bool sys_file_copy_file(const String& src, const String& dst); +bool sys_file_move_file(const String& src, const String& dst); +void sys_file_remove_readonly(const String& name); + +} // namespace Kyty + +#endif + +#endif /* SYS_LINUX_INCLUDE_KYTY_SYSFILEIO_H_ */ From 0bfa25162550ff40c28afcd06f32a72686436c3b Mon Sep 17 00:00:00 2001 From: Smiril Date: Sat, 11 May 2024 21:19:06 +0200 Subject: [PATCH 05/42] Update SysMacOSDbg.h --- source/include/Kyty/Sys/MacOS/SysMacOSDbg.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/include/Kyty/Sys/MacOS/SysMacOSDbg.h b/source/include/Kyty/Sys/MacOS/SysMacOSDbg.h index 2276afb..57a6649 100644 --- a/source/include/Kyty/Sys/MacOS/SysMacOSDbg.h +++ b/source/include/Kyty/Sys/MacOS/SysMacOSDbg.h @@ -4,7 +4,7 @@ // IWYU pragma: private #if KYTY_PLATFORM != KYTY_PLATFORM_MACOS -//#error "KYTY_PLATFORM != KYTY_PLATFORM_LINUX" +//#error "KYTY_PLATFORM != KYTY_PLATFORM_MACOS" #else namespace Kyty { @@ -32,4 +32,4 @@ void sys_set_exception_filter(exception_filter_func_t func); #endif -#endif /* SYS_LINUX_INCLUDE_KYTY_SYSDBG_H_ */ +#endif /* SYS_MACOS_INCLUDE_KYTY_SYSDBG_H_ */ From e6aaeeab77f3a74b3c1a8ffeccaca55c8b8087c7 Mon Sep 17 00:00:00 2001 From: Smiril Date: Sat, 11 May 2024 21:21:31 +0200 Subject: [PATCH 06/42] Create SysMacOSHeap.h --- source/include/Kyty/Sys/MacOS/SysMacOSHeap.h | 104 +++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 source/include/Kyty/Sys/MacOS/SysMacOSHeap.h diff --git a/source/include/Kyty/Sys/MacOS/SysMacOSHeap.h b/source/include/Kyty/Sys/MacOS/SysMacOSHeap.h new file mode 100644 index 0000000..38cb46c --- /dev/null +++ b/source/include/Kyty/Sys/MacOS/SysMacOSHeap.h @@ -0,0 +1,104 @@ +#ifndef SYS_MACOS_INCLUDE_KYTY_SYSHEAP_H_ +#define SYS_MACOS_INCLUDE_KYTY_SYSHEAP_H_ + +// IWYU pragma: private + +#if KYTY_PLATFORM != KYTY_PLATFORM_MACOS +//#error "KYTY_PLATFORM != KYTY_PLATFORM_MACOS" +#else + +#include "Kyty/Sys/MacOS/SysMacOSSync.h" + +namespace Kyty { + +using sys_heap_id_t = SysCS*; + +inline sys_heap_id_t sys_heap_create() +{ + return nullptr; +} + +inline sys_heap_id_t sys_heap_deafult() +{ + return nullptr; +} + +inline void* sys_heap_alloc(sys_heap_id_t /*heap_id*/, size_t size) +{ + // NOLINTNEXTLINE(cppcoreguidelines-no-malloc,hicpp-no-malloc) + void* m = malloc(size); + + EXIT_IF(m == nullptr); + + return m; +} + +inline void* sys_heap_realloc(sys_heap_id_t /*heap_id*/, void* p, size_t size) +{ + // NOLINTNEXTLINE(cppcoreguidelines-no-malloc,hicpp-no-malloc) + void* m = p != nullptr ? realloc(p, size) : malloc(size); + + if (m == nullptr) + { + EXIT_IF(m == nullptr); + } + + return m; +} + +inline void sys_heap_free(sys_heap_id_t /*heap_id*/, void* p) +{ + // NOLINTNEXTLINE(cppcoreguidelines-no-malloc,hicpp-no-malloc) + free(p); +} + +inline sys_heap_id_t sys_heap_create_s() +{ + auto* cs = new SysCS; + cs->Init(); + return cs; +} + +inline void* sys_heap_alloc_s(sys_heap_id_t heap_id, size_t size) +{ + heap_id->Enter(); + + // NOLINTNEXTLINE(cppcoreguidelines-no-malloc,hicpp-no-malloc) + void* m = malloc(size); + + heap_id->Leave(); + + EXIT_IF(m == nullptr); + + return m; +} + +inline void* sys_heap_realloc_s(sys_heap_id_t heap_id, void* p, size_t size) +{ + heap_id->Enter(); + + // NOLINTNEXTLINE(cppcoreguidelines-no-malloc,hicpp-no-malloc) + void* m = p != nullptr ? realloc(p, size) : malloc(size); + + heap_id->Leave(); + + EXIT_IF(m == nullptr); + + return m; +} + +inline void sys_heap_free_s(sys_heap_id_t heap_id, void* p) +{ + heap_id->Enter(); + + // NOLINTNEXTLINE(cppcoreguidelines-no-malloc,hicpp-no-malloc) + free(p); + + heap_id->Leave(); +} + +} // namespace Kyty + +#endif + +#endif /* SYS_MACOS_INCLUDE_KYTY_SYSHEAP_H_ */ From 4688387d09f4a8e452a21070a3a3f4e44340a340 Mon Sep 17 00:00:00 2001 From: Smiril Date: Sat, 11 May 2024 21:23:28 +0200 Subject: [PATCH 07/42] Create SysMacOSStdio.h --- source/include/Kyty/Sys/MacOS/SysMacOSStdio.h | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 source/include/Kyty/Sys/MacOS/SysMacOSStdio.h diff --git a/source/include/Kyty/Sys/MacOS/SysMacOSStdio.h b/source/include/Kyty/Sys/MacOS/SysMacOSStdio.h new file mode 100644 index 0000000..c3eeb29 --- /dev/null +++ b/source/include/Kyty/Sys/MacOS/SysMacOSStdio.h @@ -0,0 +1,34 @@ +#ifndef SYS_MACOS_INCLUDE_KYTY_SYSSTDIO_H_ +#define SYS_MACOS_INCLUDE_KYTY_SYSSTDIO_H_ + +// IWYU pragma: private + +#if KYTY_PLATFORM != KYTY_PLATFORM_MACOS +//#error "KYTY_PLATFORM != KYTY_PLATFORM_MACOS" +#else + +//#include +#include + +namespace Kyty { + +inline uint32_t sys_vscprintf(const char* format, va_list argptr) +{ + va_list argcopy; + va_copy(argcopy, argptr); + int len = vsnprintf(nullptr, 0, format, argcopy); + va_end(argcopy); + return len < 0 ? 0 : len; +} + +inline uint32_t sys_vsnprintf(char* dest, size_t count, const char* format, va_list args) +{ + int len = vsnprintf(dest, count + 1, format, args); + return len < 0 ? 0 : len; +} + +} // namespace Kyty + +#endif + +#endif /* SYS_MACOS_INCLUDE_KYTY_SYSSTDIO_H_ */ From 455c053ae7db973feb9bd84dd82e415bf81751c9 Mon Sep 17 00:00:00 2001 From: Smiril Date: Sat, 11 May 2024 21:25:20 +0200 Subject: [PATCH 08/42] Create SysMacOSStdlib.h --- .../include/Kyty/Sys/MacOS/SysMacOSStdlib.h | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 source/include/Kyty/Sys/MacOS/SysMacOSStdlib.h diff --git a/source/include/Kyty/Sys/MacOS/SysMacOSStdlib.h b/source/include/Kyty/Sys/MacOS/SysMacOSStdlib.h new file mode 100644 index 0000000..c24f89f --- /dev/null +++ b/source/include/Kyty/Sys/MacOS/SysMacOSStdlib.h @@ -0,0 +1,55 @@ +#ifndef SYS_MACOS_INCLUDE_KYTY_SYSSTDLIB_H_ +#define SYS_MACOS_INCLUDE_KYTY_SYSSTDLIB_H_ + +// IWYU pragma: private + +#if KYTY_PLATFORM != KYTY_PLATFORM_MACOS +//#error "KYTY_PLATFORM != KYTY_PLATFORM_MACOS" +#else + +namespace Kyty { + +inline float sys_strtof(const char* nptr, char** endptr) +{ + return strtof(nptr, endptr); +} + +inline double sys_strtod(const char* nptr, char** endptr) +{ + return strtod(nptr, endptr); +} + +inline int32_t sys_strtoi32(const char* nptr, char** endptr, int base) +{ + int64_t r = strtol(nptr, endptr, base); + if (r >= static_cast(INT32_MAX)) + { + return INT32_MAX; + } + if (r <= static_cast(INT32_MIN)) + { + return INT32_MIN; + } + return static_cast(r); +} + +inline uint32_t sys_strtoui32(const char* nptr, char** endptr, int base) +{ + return strtoul(nptr, endptr, base); +} + +inline int64_t sys_strtoi64(const char* nptr, char** endptr, int base) +{ + return strtoll(nptr, endptr, base); +} + +inline uint64_t sys_strtoui64(const char* nptr, char** endptr, int base) +{ + return strtoull(nptr, endptr, base); +} + +} // namespace Kyty + +#endif + +#endif /* SYS_MACOS_INCLUDE_KYTY_SYSSTDLIB_H_ */ From 89c4bbc3899931ed8f1e51e710e2f3eeda895e77 Mon Sep 17 00:00:00 2001 From: Smiril Date: Sat, 11 May 2024 21:27:01 +0200 Subject: [PATCH 09/42] Create SysMacOSSync.h --- source/include/Kyty/Sys/MacOS/SysMacOSSync.h | 84 ++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 source/include/Kyty/Sys/MacOS/SysMacOSSync.h diff --git a/source/include/Kyty/Sys/MacOS/SysMacOSSync.h b/source/include/Kyty/Sys/MacOS/SysMacOSSync.h new file mode 100644 index 0000000..2e6fc70 --- /dev/null +++ b/source/include/Kyty/Sys/MacOS/SysMacOSSync.h @@ -0,0 +1,84 @@ +#ifndef SYS_MACOS_INCLUDE_KYTY_SYSSYNC_H_ +#define SYS_MACOS_INCLUDE_KYTY_SYSSYNC_H_ + +// IWYU pragma: private + +#if KYTY_PLATFORM != KYTY_PLATFORM_MACOS +//#error "KYTY_PLATFORM != KYTY_PLATFORM_MACOS" +#else + +#include +#include + +namespace Kyty { + +class SysCS +{ +public: + SysCS() = default; + + void Init() + { + EXIT_IF(m_check_ptr != nullptr); + + m_check_ptr = this; + + pthread_mutexattr_t attr {}; + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP); + pthread_mutex_init(&m_mutex, &attr); + pthread_mutexattr_destroy(&attr); + } + + void Delete() + { + EXIT_IF(m_check_ptr != this); + + m_check_ptr = nullptr; + pthread_mutex_destroy(&m_mutex); + } + + ~SysCS() { EXIT_IF(m_check_ptr != nullptr); } + + void Enter() + { + EXIT_IF(m_check_ptr != this); + + pthread_mutex_lock(&m_mutex); + } + + bool TryEnter() + { + EXIT_IF(m_check_ptr != this); + + return pthread_mutex_trylock(&m_mutex) == 0; + } + + void Leave() + { + EXIT_IF(m_check_ptr != this); + + pthread_mutex_unlock(&m_mutex); + } + + KYTY_CLASS_NO_COPY(SysCS); + +private: + SysCS* m_check_ptr = nullptr; + pthread_mutex_t m_mutex {}; +}; + +inline void sys_sleep(uint32_t ms) +{ + struct timespec ts + { + }; + ts.tv_sec = ms / 1000; + ts.tv_nsec = static_cast(ms % 1000) * 1000000; + nanosleep(&ts, nullptr); +} + +} // namespace Kyty + +#endif + +#endif /* SYS_MACOS_INCLUDE_KYTY_SYSSYNC_H_ */ From 20a87036ab42e094a7c723dd7d5242f6c785df54 Mon Sep 17 00:00:00 2001 From: Smiril Date: Sat, 11 May 2024 21:32:34 +0200 Subject: [PATCH 10/42] Create SysMacOSTimer.h --- source/include/Kyty/Sys/MacOS/SysMacOSTimer.h | 151 ++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 source/include/Kyty/Sys/MacOS/SysMacOSTimer.h diff --git a/source/include/Kyty/Sys/MacOS/SysMacOSTimer.h b/source/include/Kyty/Sys/MacOS/SysMacOSTimer.h new file mode 100644 index 0000000..b2e182a --- /dev/null +++ b/source/include/Kyty/Sys/MacOS/SysMacOSTimer.h @@ -0,0 +1,151 @@ +#ifndef SYS_MACOS_INCLUDE_KYTY_SYSTIMER_H_ +#define SYS_MACOS_INCLUDE_KYTY_SYSTIMER_H_ + +// IWYU pragma: private + +#if KYTY_PLATFORM != KYTY_PLATFORM_MACOS +//#error "KYTY_PLATFORM != KYTY_PLATFORM_MACOS" +#else + +#include + +namespace Kyty { + +struct SysTimeStruct +{ + uint16_t Year; + uint16_t Month; + uint16_t Day; + uint16_t Hour; + uint16_t Minute; + uint16_t Second; + uint16_t Milliseconds; + bool is_invalid; +}; + +struct SysFileTimeStruct +{ + time_t time; + bool is_invalid; +}; + +inline void sys_file_to_system_time_utc(const SysFileTimeStruct& f, SysTimeStruct& t) +{ + struct tm i + { + }; + + if (f.is_invalid || gmtime_r(&f.time, &i) == nullptr) + { + t.is_invalid = true; + return; + } + + t.is_invalid = false; + t.Year = i.tm_year + 1900; + t.Month = i.tm_mon + 1; + t.Day = i.tm_mday; + t.Hour = i.tm_hour; + t.Minute = i.tm_min; + t.Second = (i.tm_sec == 60 ? 59 : i.tm_sec); + t.Milliseconds = 0; +} + +inline void sys_time_t_to_system(time_t t, SysTimeStruct& s) +{ + SysTimeStruct ft {}; + ft.time = t; + ft.is_invalid = false; + sys_file_to_system_time_utc(ft, s); +} + +inline time_t sys_timegm(struct tm* tm) +{ + return timegm(tm); + // time_t t = mktime(tm); + // return t == (time_t)-1 ? (time_t)-1 : t + localtime(&t)->tm_gmtoff; +} + +inline void sys_system_to_file_time_utc(const SysTimeStruct& f, SysFileTimeStruct& t) +{ + struct tm i + { + }; + + i.tm_year = f.Year - 1900; + i.tm_mon = f.Month - 1; + i.tm_mday = f.Day; + i.tm_hour = f.Hour; + i.tm_min = f.Minute; + i.tm_sec = f.Second; + + t.is_invalid = (f.is_invalid || (t.time = sys_timegm(&i)) == static_cast(-1)); +} + +// Retrieves the current local date and time +inline void sys_get_system_time(SysTimeStruct& t) +{ + time_t st {}; + struct tm i + { + }; + + if (time(&st) == static_cast(-1) || localtime_r(&st, &i) == nullptr) + { + t.is_invalid = true; + return; + } + + t.is_invalid = false; + t.Year = i.tm_year + 1900; + t.Month = i.tm_mon + 1; + t.Day = i.tm_mday; + t.Hour = i.tm_hour; + t.Minute = i.tm_min; + t.Second = (i.tm_sec == 60 ? 59 : i.tm_sec); + t.Milliseconds = 0; +} + +// Retrieves the current system date and time in Coordinated Universal Time (UTC). +inline void sys_get_system_time_utc(SysTimeStruct& t) +{ + time_t st {}; + struct tm i + { + }; + + if (time(&st) == static_cast(-1) || gmtime_r(&st, &i) == nullptr) + { + t.is_invalid = true; + return; + } + + t.is_invalid = false; + t.Year = i.tm_year + 1900; + t.Month = i.tm_mon + 1; + t.Day = i.tm_mday; + t.Hour = i.tm_hour; + t.Minute = i.tm_min; + t.Second = (i.tm_sec == 60 ? 59 : i.tm_sec); + t.Milliseconds = 0; +} + +inline void sys_query_performance_frequency(uint64_t* freq) +{ + *freq = 1000000000LL; +} + +inline void sys_query_performance_counter(uint64_t* counter) +{ + struct timespec now + { + }; + clock_gettime(CLOCK_MONOTONIC, &now); + *counter = now.tv_sec * 1000000000LL + now.tv_nsec; +} + +} // namespace Kyty + +#endif + +#endif /* SYS_MACOS_INCLUDE_KYTY_SYSTIMER_H_ */ From bccef0b6cb39e674a9e3ce04e9bca39447685631 Mon Sep 17 00:00:00 2001 From: Smiril Date: Sat, 11 May 2024 21:34:17 +0200 Subject: [PATCH 11/42] Create SysMacOSVirtual.h --- .../include/Kyty/Sys/MacOS/SysMacOSVirtual.h | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 source/include/Kyty/Sys/MacOS/SysMacOSVirtual.h diff --git a/source/include/Kyty/Sys/MacOS/SysMacOSVirtual.h b/source/include/Kyty/Sys/MacOS/SysMacOSVirtual.h new file mode 100644 index 0000000..e3cf79a --- /dev/null +++ b/source/include/Kyty/Sys/MacOS/SysMacOSVirtual.h @@ -0,0 +1,30 @@ +#ifndef INCLUDE_KYTY_SYS_MACOS_SYSLINUXVIRTUAL_H_ +#define INCLUDE_KYTY_SYS_MACOS_SYSLINUXVIRTUAL_H_ + +#if KYTY_PLATFORM != KYTY_PLATFORM_MACOS +//#error "KYTY_PLATFORM != KYTY_PLATFORM_MACOS" +#else + +#include "Kyty/Core/Common.h" +#include "Kyty/Core/String.h" +#include "Kyty/Core/VirtualMemory.h" + +namespace Kyty::Core { + +void sys_get_system_info(SystemInfo* info); + +void sys_virtual_init(); + +uint64_t sys_virtual_alloc(uint64_t address, uint64_t size, VirtualMemory::Mode mode); +uint64_t sys_virtual_alloc_aligned(uint64_t address, uint64_t size, VirtualMemory::Mode mode, uint64_t alignment); +bool sys_virtual_alloc_fixed(uint64_t address, uint64_t size, VirtualMemory::Mode mode); +bool sys_virtual_free(uint64_t address); +bool sys_virtual_protect(uint64_t address, uint64_t size, VirtualMemory::Mode mode, VirtualMemory::Mode* old_mode = nullptr); +bool sys_virtual_flush_instruction_cache(uint64_t address, uint64_t size); +bool sys_virtual_patch_replace(uint64_t vaddr, uint64_t value); + +} // namespace Kyty::Core + +#endif + +#endif /* INCLUDE_KYTY_SYS_MACOS_SYSLINUXVIRTUAL_H_ */ From c6a7ce47c48ae7a59263c877cb0d7cc3e8c5243d Mon Sep 17 00:00:00 2001 From: Smiril Date: Sat, 11 May 2024 21:38:20 +0200 Subject: [PATCH 12/42] Create SysMacOSDbg.cpp --- source/lib/Sys/src/SysMacOSDbg.cpp | 152 +++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 source/lib/Sys/src/SysMacOSDbg.cpp diff --git a/source/lib/Sys/src/SysMacOSDbg.cpp b/source/lib/Sys/src/SysMacOSDbg.cpp new file mode 100644 index 0000000..b44f5a5 --- /dev/null +++ b/source/lib/Sys/src/SysMacOSDbg.cpp @@ -0,0 +1,152 @@ +#include "Kyty/Core/Common.h" + +#if KYTY_PLATFORM != KYTY_PLATFORM_MACOS +//#error "KYTY_PLATFORM != KYTY_PLATFORM_MACOS" +#else + +#include "Kyty/Sys/SysDbg.h" + +#include +#include +#include +#include +#include + +namespace Kyty { + +static thread_local sys_dbg_stack_info_t g_stack = {0}; + +void sys_stack_walk(void** /*stack*/, int* depth) +{ + *depth = 0; +} + +void sys_stack_usage_print(sys_dbg_stack_info_t& stack) +{ + printf("stack: (0x%" PRIx64 ", %" PRIu64 ")\n", static_cast(stack.commited_addr), static_cast(stack.commited_size)); + printf("code: (0x%" PRIx64 ", %" PRIu64 ")\n", static_cast(stack.code_addr), static_cast(stack.code_size)); +} + +void sys_stack_usage(sys_dbg_stack_info_t& s) +{ + pid_t pid = getpid(); + + // printf("pid = %"I64"d\n", (int64_t)pid); + + [[maybe_unused]] int result = 0; + + char str[1024]; + char str2[1024]; + result = sprintf(str, "/proc/%d/exe", static_cast(pid)); + + ssize_t buff_len = 0; + if ((buff_len = readlink(str, str2, 1023)) == -1) + { + return; + } + str2[buff_len] = '\0'; + const char* name = basename(str2); + + result = sprintf(str, "/proc/%d/maps", static_cast(pid)); + + memset(&s, 0, sizeof(sys_dbg_stack_info_t)); + + FILE* f = fopen(str, "r"); + + if (f == nullptr) + { + return; + } + + // printf("&str = %"I64"x\n", (uint64_t)&str); + + uint64_t addr = 0; + uint64_t endaddr = 0; + [[maybe_unused]] uint64_t size = 0; + uint64_t offset = 0; + uint64_t inode = 0; + char permissions[8] = {}; + char device[8] = {}; + char filename[MAXPATHLEN] = {}; + + auto check_addr = reinterpret_cast(&f); + + while (true) + { + if (feof(f) != 0) + { + break; + } + + if (fgets(str, sizeof(str), f) == nullptr) + { + break; + } + + filename[0] = 0; + permissions[0] = 0; + addr = 0; + size = 0; + + // printf("%s", str); + + // NOLINTNEXTLINE(cert-err34-c) + result = sscanf(str, "%" SCNx64 "-%" SCNx64 " %s %" SCNx64 " %s %" SCNx64 " %s", &addr, &endaddr, permissions, &offset, device, + &inode, filename); + + size = endaddr - addr; + + bool read = (strchr(permissions, 'r') != nullptr); + bool write = (strchr(permissions, 'w') != nullptr); + bool exec = (strchr(permissions, 'x') != nullptr); + + // printf("%016"I64"x, %"I64"d, %s, %d, %d\n", addr, size, filename, read, write); + + if (read && write && !exec && strncmp(filename, "[stack", 6) == 0) + { + // printf("stack: %016"I64"x, %"I64"d\n", addr, size); + + if (check_addr >= addr && check_addr < addr + size) + { + s.addr = addr; + s.total_size = size; + s.commited_addr = addr; + s.commited_size = size; + + if (s.code_addr != 0) + { + break; + } + } + } + + if (read && !write && exec && strstr(filename, name) != nullptr) + { + s.code_addr = addr; + s.code_size = size; + + if (s.addr != 0) + { + break; + } + } + } + + result = fclose(f); +} + +void sys_get_code_info(uintptr_t* addr, size_t* size) +{ + if (g_stack.code_size == 0) + { + sys_stack_usage(g_stack); + } + + *addr = g_stack.code_addr; + *size = g_stack.code_size; +} + +void sys_set_exception_filter(exception_filter_func_t /*func*/) {} + +} // namespace Kyty +#endif From 8dfb2704f20eaa3bec807102051a80f4dcd42afb Mon Sep 17 00:00:00 2001 From: Smiril Date: Sat, 11 May 2024 21:40:42 +0200 Subject: [PATCH 13/42] Create SysMacOSFileIO.cpp --- source/lib/Sys/src/SysMacOSFileIO.cpp | 672 ++++++++++++++++++++++++++ 1 file changed, 672 insertions(+) create mode 100644 source/lib/Sys/src/SysMacOSFileIO.cpp diff --git a/source/lib/Sys/src/SysMacOSFileIO.cpp b/source/lib/Sys/src/SysMacOSFileIO.cpp new file mode 100644 index 0000000..56ab07d --- /dev/null +++ b/source/lib/Sys/src/SysMacOSFileIO.cpp @@ -0,0 +1,672 @@ +#include "Kyty/Core/Common.h" + +#if KYTY_PLATFORM != KYTY_PLATFORM_MACOS +//#error "KYTY_PLATFORM != KYTY_PLATFORM_MACOS" +#else + +#include "Kyty/Core/DbgAssert.h" +#include "Kyty/Core/MemoryAlloc.h" +#include "Kyty/Core/String.h" +#include "Kyty/Sys/SysFileIO.h" +#include "Kyty/Sys/SysTimer.h" + +#include "SDL_system.h" + +#include +#include +#include +#include + +namespace Kyty { + +template +class Vector; + +static String* g_internal_files_dir = nullptr; + +static String get_internal_name(const String& name) +{ + return name.StartsWith(U"/") ? name : *g_internal_files_dir + U"/" + name; +} + +bool sys_file_io_init() +{ + g_internal_files_dir = new String(); + + *g_internal_files_dir = U"."; + + return !g_internal_files_dir->IsEmpty(); +} + +void sys_file_read(void* data, uint32_t size, sys_file_t& f, uint32_t* bytes_read) +{ + if (f.type == SYS_FILE_FILE) + { + size_t w = fread(data, 1, size, f.f); + if (bytes_read != nullptr) + { + *bytes_read = w; + } + } else if (f.type == SYS_FILE_MEMORY_STAT) + { + uint32_t s = size; + if (f.buf->size != 0) + { + uint32_t l = f.buf->size - (f.buf->ptr - f.buf->base); + if (s > l) + { + s = l; + } + } + memcpy(data, f.buf->ptr, s); + f.buf->ptr += s; + if (bytes_read != nullptr) + { + *bytes_read = s; + } + } else if (f.type == SYS_FILE_MEMORY_DYN) + { + uint32_t s = size; + if (f.buf->size != 0) + { + uint32_t l = f.buf->size - (f.buf->ptr - f.buf->base); + if (s > l) + { + s = l; + } + } else + { + s = 0; + } + memcpy(data, f.buf->ptr, s); + f.buf->ptr += s; + if (bytes_read != nullptr) + { + *bytes_read = s; + } + } +} + +void sys_file_write(const void* data, uint32_t size, sys_file_t& f, uint32_t* bytes_written) +{ + if (f.type == SYS_FILE_FILE) + { + size_t w = fwrite(data, 1, size, f.f); + if (bytes_written != nullptr) + { + *bytes_written = w; + } + } else if (f.type == SYS_FILE_MEMORY_STAT) + { + uint32_t s = size; + if (f.buf->size != 0) + { + uint32_t l = f.buf->size - (f.buf->ptr - f.buf->base); + if (s > l) + { + s = l; + } + } + memcpy(f.buf->ptr, data, s); + f.buf->ptr += s; + if (bytes_written != nullptr) + { + *bytes_written = s; + } + } else if (f.type == SYS_FILE_MEMORY_DYN) + { + uint32_t pos = f.buf->ptr - f.buf->base; + if (f.buf->size < pos + size) + { + f.buf->base = static_cast(Core::mem_realloc(f.buf->base, pos + size)); + f.buf->ptr = f.buf->base + pos; + f.buf->size = pos + size; + } + memcpy(f.buf->ptr, data, size); + f.buf->ptr += size; + if (bytes_written != nullptr) + { + *bytes_written = size; + } + } +} + +void sys_file_read_r(void* data, uint32_t size, sys_file_t& f) +{ + // DWORD w; + // ReadFile(f, data, size, &w, 0); + + sys_file_read(data, size, f); + + for (uint32_t i = 0; i < size / 2; i++) + { + char t = (static_cast(data))[i]; + (static_cast(data))[i] = (static_cast(data))[size - i - 1]; + (static_cast(data))[size - i - 1] = t; + } +} + +void sys_file_write_r(const void* data, uint32_t size, sys_file_t& f) +{ + char* data_r = new char[size]; + for (uint32_t i = 0; i < size; i++) + { + data_r[i] = (static_cast(data))[size - i - 1]; + } + + sys_file_write(data_r, size, f); + + delete[] data_r; +} + +void sys_file_write(uint32_t n, sys_file_t& f) +{ + sys_file_write(&n, 4, f); +} + +void sys_file_write_r(uint32_t n, sys_file_t& f) +{ + sys_file_write_r(&n, 4, f); +} + +sys_file_t* sys_file_create(const String& file_name) +{ + auto* ret = new sys_file_t; + + String real_name = get_internal_name(file_name); + + ret->f = fopen(real_name.utf8_str().GetData(), "w+"); + + if (ret->f == nullptr) + { + printf("can't create file: %s\n", real_name.utf8_str().GetData()); + } + + ret->type = SYS_FILE_FILE; + + return ret; +} + +sys_file_t* sys_file_open_r(const String& file_name, sys_file_cache_type_t /*cache_type*/) +{ + auto* ret = new sys_file_t; + + ret->type = SYS_FILE_FILE; + + String internal_name = get_internal_name(file_name); + ; + + FILE* f = fopen(internal_name.utf8_str().GetData(), "r"); + + if (f == nullptr) + { + ret->type = SYS_FILE_ERROR; + } + + ret->f = f; + + return ret; +} + +sys_file_t* sys_file_open(uint8_t* buf, uint32_t buf_size) +{ + auto* ret = new sys_file_t; + + ret->type = SYS_FILE_MEMORY_STAT; + ret->buf = new sys_file_mem_buf_t; + ret->buf->base = buf; + ret->buf->ptr = buf; + ret->buf->size = buf_size; + + return ret; +} + +sys_file_t* sys_file_create() +{ + auto* ret = new sys_file_t; + + ret->type = SYS_FILE_MEMORY_DYN; + ret->buf = new sys_file_mem_buf_t; + ret->buf->base = nullptr; + ret->buf->ptr = nullptr; + ret->buf->size = 0; + + return ret; +} + +sys_file_t* sys_file_open_w(const String& file_name, sys_file_cache_type_t /*cache_type*/) +{ + auto* ret = new sys_file_t; + + String real_name = get_internal_name(file_name); + ; + + FILE* f = fopen(real_name.utf8_str().GetData(), "r+"); + + if (f == nullptr) + { + ret->type = SYS_FILE_ERROR; + } else + { + ret->type = SYS_FILE_FILE; + } + + ret->f = f; + + return ret; +} + +sys_file_t* sys_file_open_rw(const String& file_name, sys_file_cache_type_t /*cache_type*/) +{ + auto* ret = new sys_file_t; + + String real_name = get_internal_name(file_name); + + FILE* f = fopen(real_name.utf8_str().GetData(), "r+"); + + if (f == nullptr) + { + ret->type = SYS_FILE_ERROR; + } else + { + ret->type = SYS_FILE_FILE; + } + + ret->f = f; + + return ret; +} + +void sys_file_close(sys_file_t* f) +{ + [[maybe_unused]] int result = 0; + + if (f->type == SYS_FILE_FILE && f->f != nullptr) + { + result = fclose(f->f); + } else if (f->type == SYS_FILE_MEMORY_STAT) + { + delete f->buf; + } else if (f->type == SYS_FILE_MEMORY_DYN) + { + Core::mem_free(f->buf->base); + delete f->buf; + } + + // f.type = SYS_FILE_ERROR; + delete f; +} + +uint64_t sys_file_size(sys_file_t& f) +{ + [[maybe_unused]] int result = 0; + + if (f.type == SYS_FILE_FILE) + { + uint32_t pos = ftell(f.f); + result = fseek(f.f, 0, SEEK_END); + uint32_t size = ftell(f.f); + result = fseek(f.f, pos, SEEK_SET); + return size; + } + + if (f.type == SYS_FILE_MEMORY_STAT || f.type == SYS_FILE_MEMORY_DYN) + { + return f.buf->size; + } + + return 0; +} + +uint64_t sys_file_size(const String& file_name) +{ + sys_file_t* f = sys_file_open_r(file_name); + uint64_t size = sys_file_size(*f); + sys_file_close(f); + return size; +} + +bool sys_file_truncate(sys_file_t& /*f*/, uint64_t /*size*/) +{ + return false; +} + +bool sys_file_seek(sys_file_t& f, uint64_t offset) +{ + bool ok = true; + if (f.type == SYS_FILE_FILE) + { + ok = (fseek(f.f, static_cast(offset), SEEK_SET) == 0); + // LARGE_INTEGER s; + // s.QuadPart = offset; + // SetFilePointerEx(f.handle, s, 0, FILE_BEGIN); + // printf("seek: %u\n", offset); + } else if (f.type == SYS_FILE_MEMORY_STAT || f.type == SYS_FILE_MEMORY_DYN) + { + f.buf->ptr = f.buf->base + offset; + } + return ok; +} + +uint64_t sys_file_tell(sys_file_t& f) +{ + if (f.type == SYS_FILE_FILE) + { + return ftell(f.f); + } + + if (f.type == SYS_FILE_MEMORY_STAT || f.type == SYS_FILE_MEMORY_DYN) + { + return f.buf->ptr - f.buf->base; + } + + return 0; +} + +bool sys_file_is_error(sys_file_t& f) +{ + return f.type == SYS_FILE_ERROR || (f.type == SYS_FILE_FILE && f.f == nullptr); +} + +bool sys_file_is_directory_existing(const String& path) +{ + String real_name = get_internal_name(path); + + struct stat s + { + }; + + if (0 != stat(real_name.utf8_str().GetData(), &s)) + { + return false; + } + + return S_ISDIR(s.st_mode); // NOLINT +} + +bool sys_file_is_file_existing(const String& name) +{ + String real_name = get_internal_name(name); + + struct stat s + { + }; + + if (0 != stat(real_name.utf8_str().GetData(), &s)) + { + return false; + } + + return !S_ISDIR(s.st_mode); // NOLINT +} + +bool sys_file_create_directory(const String& path) +{ + String real_name = get_internal_name(path); + + mode_t m = S_IRWXU | S_IRWXG | S_IRWXO; // NOLINT + + String::Utf8 u = real_name.utf8_str(); + + int r = mkdir(u.GetDataConst(), m); + + if (r != 0) + { + int e = errno; + if (e == EEXIST) + { + printf("mkdir(%s, %" PRIx32 ") failed: The named file exists\n", real_name.C_Str(), static_cast(m)); + } else + { + printf("mkdir(%s, %" PRIx32 ") failed: %d\n", real_name.C_Str(), static_cast(m), e); + return false; + } + } + + return true; +} + +bool sys_file_delete_directory(const String& path) +{ + String real_name = get_internal_name(path); + + return 0 == remove(real_name.utf8_str().GetData()); +} + +bool sys_file_delete_file(const String& name) +{ + String real_name = get_internal_name(name); + + return 0 == unlink(real_name.utf8_str().GetData()); +} + +bool sys_file_flush(sys_file_t& f) +{ + if (f.type == SYS_FILE_FILE && f.f != nullptr) + { + return (fflush(f.f) == 0); + } + + return false; +} + +SysFileTimeStruct sys_file_get_last_access_time_utc(const String& name) +{ + SysFileTimeStruct r {}; + + String real_name = get_internal_name(name); + + struct stat s + { + }; + + if (0 != stat(real_name.utf8_str().GetData(), &s)) + { + r.is_invalid = true; + } else + { + r.is_invalid = false; + r.time = s.st_atime; + } + + return r; +} + +SysFileTimeStruct sys_file_get_last_write_time_utc(const String& name) +{ + SysFileTimeStruct r {}; + + String real_name = get_internal_name(name); + + struct stat s + { + }; + + if (0 != stat(real_name.utf8_str().GetData(), &s)) + { + r.is_invalid = true; + } else + { + r.is_invalid = false; + r.time = s.st_mtime; + } + + return r; +} + +void sys_file_get_last_access_and_write_time_utc(const String& name, SysFileTimeStruct& a, SysFileTimeStruct& w) +{ + String real_name = get_internal_name(name); + + struct stat s + { + }; + + if (0 != stat(real_name.utf8_str().GetData(), &s)) + { + a.is_invalid = true; + w.is_invalid = true; + } else + { + a.is_invalid = false; + w.is_invalid = false; + a.time = s.st_atime; + w.time = s.st_mtime; + } +} + +void sys_file_get_last_access_and_write_time_utc(sys_file_t& /*f*/, SysFileTimeStruct& /*a*/, SysFileTimeStruct& /*w*/) +{ + EXIT("not implemented\n"); +} + +bool sys_file_set_last_access_time_utc(const String& name, SysFileTimeStruct& access) +{ + if (access.is_invalid) + { + return false; + } + + String real_name = get_internal_name(name); + + struct stat s + { + }; + + String::Utf8 n = real_name.utf8_str(); + + if (0 != stat(n.GetData(), &s)) + { + return false; + } + + utimbuf times {}; + times.actime = access.time; + times.modtime = s.st_mtime; + + return !(0 != utime(n.GetData(), ×)); + + // if (0 != stat(n.GetData(), &s)) + // { + // return false; + // } + // + // times.actime += times.actime - s.st_atime; + // times.modtime += times.modtime - s.st_mtime; + // + // if (0 != utime(n.GetData(), ×)) + // { + // return false; + // } +} + +bool sys_file_set_last_write_time_utc(const String& name, SysFileTimeStruct& write) +{ + if (write.is_invalid) + { + return false; + } + + String real_name = get_internal_name(name); + + struct stat s + { + }; + + String::Utf8 n = real_name.utf8_str(); + + if (0 != stat(n.GetData(), &s)) + { + return false; + } + + utimbuf times {}; + times.actime = s.st_atime; + times.modtime = write.time; + + return !(0 != utime(n.GetData(), ×)); + + // if (0 != stat(n.GetData(), &s)) + // { + // return false; + // } + // + // times.actime += times.actime - s.st_atime; + // times.modtime += times.modtime - s.st_mtime; + // + // if (0 != utime(n.GetData(), ×)) + // { + // return false; + // } +} + +bool sys_file_set_last_access_and_write_time_utc(const String& name, SysFileTimeStruct& access, SysFileTimeStruct& write) +{ + if (access.is_invalid || write.is_invalid) + { + return false; + } + + String real_name = get_internal_name(name); + + struct stat s + { + }; + + String::Utf8 n = real_name.utf8_str(); + + if (0 != stat(n.GetData(), &s)) + { + return false; + } + + utimbuf times {}; + times.actime = access.time; + times.modtime = write.time; + + return !(0 != utime(n.GetData(), ×)); + + // if (0 != stat(n.GetData(), &s)) + // { + // return false; + // } + // + // times.actime += times.actime - s.st_atime; + // times.modtime += times.modtime - s.st_mtime; + // + // if (0 != utime(n.GetData(), ×)) + // { + // return false; + // } +} + +void sys_file_find_files(const String& /*path*/, Vector& /*out*/) +{ + EXIT("not implemented\n"); +} + +void sys_file_get_dents(const String& /*path*/, Kyty::Vector& /*out*/) +{ + EXIT("not implemented\n"); +} + +bool sys_file_copy_file(const String& /*src*/, const String& /*dst*/) +{ + EXIT("not implemented\n"); + return false; +} + +bool sys_file_move_file(const String& /*src*/, const String& /*dst*/) +{ + EXIT("not implemented\n"); + return false; +} + +void sys_file_remove_readonly(const String& /*name*/) +{ + EXIT("not implemented\n"); +} + +} // namespace Kyty + +#endif From 4182943f6133baebc2b2068fb833de4b7d729aad Mon Sep 17 00:00:00 2001 From: Smiril Date: Sat, 11 May 2024 21:42:48 +0200 Subject: [PATCH 14/42] Create SysMacOSVirtual.cpp --- source/lib/Sys/src/SysMacOSVirtual.cpp | 373 +++++++++++++++++++++++++ 1 file changed, 373 insertions(+) create mode 100644 source/lib/Sys/src/SysMacOSVirtual.cpp diff --git a/source/lib/Sys/src/SysMacOSVirtual.cpp b/source/lib/Sys/src/SysMacOSVirtual.cpp new file mode 100644 index 0000000..96c82c8 --- /dev/null +++ b/source/lib/Sys/src/SysMacOSVirtual.cpp @@ -0,0 +1,373 @@ +#include "Kyty/Core/Common.h" + +#if KYTY_PLATFORM != KYTY_PLATFORM_MACOS +//#error "KYTY_PLATFORM != KYTY_PLATFORM_MACOS" +#else + +#include "Kyty/Core/DbgAssert.h" +#include "Kyty/Core/String.h" +#include "Kyty/Core/VirtualMemory.h" +#include "Kyty/Sys/SysVirtual.h" + +#include "cpuinfo.h" + +#include +#include +#include +#include + +// IWYU pragma: no_include +// IWYU pragma: no_include +// IWYU pragma: no_include +// IWYU pragma: no_include + +#if defined(MAP_FIXED_NOREPLACE) && KYTY_PLATFORM == KYTY_PLATFORM_MACOS +#define KYTY_FIXED_NOREPLACE +#endif + +namespace Kyty::Core { + +static pthread_mutex_t g_virtual_mutex {}; +static std::map* g_allocs = nullptr; +static std::map* g_protects = nullptr; + +void sys_get_system_info(SystemInfo* info) +{ + EXIT_IF(info == nullptr); + + const auto* p = cpuinfo_get_package(0); + + EXIT_IF(p == nullptr); + + info->ProcessorName = String::FromUtf8(p->name); +} + +void sys_virtual_init() +{ + pthread_mutexattr_t attr {}; + + pthread_mutexattr_init(&attr); +#if KYTY_PLATFORM == KYTY_PLATFORM_LINUX + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_FAST_NP); +#else + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL); +#endif + pthread_mutex_init(&g_virtual_mutex, &attr); + pthread_mutexattr_destroy(&attr); + + g_allocs = new std::map; + g_protects = new std::map; + + cpuinfo_initialize(); +} + +static int get_protection_flag(VirtualMemory::Mode mode) +{ + int protect = PROT_NONE; + switch (mode) + { + case VirtualMemory::Mode::Read: protect = PROT_READ; break; + case VirtualMemory::Mode::Write: + case VirtualMemory::Mode::ReadWrite: protect = PROT_READ | PROT_WRITE; break; // NOLINT + case VirtualMemory::Mode::Execute: protect = PROT_EXEC; break; + case VirtualMemory::Mode::ExecuteRead: protect = PROT_EXEC | PROT_READ; break; // NOLINT + case VirtualMemory::Mode::ExecuteWrite: + case VirtualMemory::Mode::ExecuteReadWrite: protect = PROT_EXEC | PROT_WRITE | PROT_READ; break; // NOLINT + case VirtualMemory::Mode::NoAccess: + default: protect = PROT_NONE; break; + } + return protect; +} + +static VirtualMemory::Mode get_protection_flag(int mode) +{ + switch (mode) + { + case PROT_NONE: return VirtualMemory::Mode::NoAccess; + case PROT_READ: return VirtualMemory::Mode::Read; + case PROT_WRITE: return VirtualMemory::Mode::Write; + case PROT_READ | PROT_WRITE: return VirtualMemory::Mode::ReadWrite; // NOLINT + case PROT_EXEC: return VirtualMemory::Mode::Execute; + case PROT_EXEC | PROT_WRITE: return VirtualMemory::Mode::ExecuteWrite; // NOLINT + case PROT_EXEC | PROT_READ: return VirtualMemory::Mode::ExecuteRead; // NOLINT + case PROT_EXEC | PROT_WRITE | PROT_READ: return VirtualMemory::Mode::ExecuteReadWrite; // NOLINT + default: return VirtualMemory::Mode::NoAccess; + } +} + +uint64_t sys_virtual_alloc(uint64_t address, uint64_t size, VirtualMemory::Mode mode) +{ + EXIT_IF(g_allocs == nullptr); + + auto addr = static_cast(address); + + int protect = get_protection_flag(mode); + + void* ptr = mmap(reinterpret_cast(addr), size, protect, MAP_PRIVATE | MAP_ANON, -1, 0); // NOLINT + + auto ret_addr = reinterpret_cast(ptr); + + if (ptr != MAP_FAILED) + { + pthread_mutex_lock(&g_virtual_mutex); + (*g_allocs)[ret_addr] = size; + uintptr_t page_start = ret_addr >> 12u; + uintptr_t page_end = (ret_addr + size - 1) >> 12u; + for (uintptr_t page = page_start; page <= page_end; page++) + { + (*g_protects)[page] = protect; + } + pthread_mutex_unlock(&g_virtual_mutex); + } + + return ret_addr; +} + +static uintptr_t align_up(uintptr_t addr, uint64_t alignment) +{ + return (addr + alignment - 1) & ~(alignment - 1); +} + +uint64_t sys_virtual_alloc_aligned(uint64_t address, uint64_t size, VirtualMemory::Mode mode, uint64_t alignment) +{ + if (alignment == 0) + { + return 0; + } + + EXIT_IF(g_allocs == nullptr); + + auto addr = static_cast(address); + int protect = get_protection_flag(mode); + + void* ptr = mmap(reinterpret_cast(addr), size, protect, MAP_PRIVATE | MAP_ANON, -1, 0); // NOLINT + + auto ret_addr = reinterpret_cast(ptr); + + if (ptr != MAP_FAILED && ((ret_addr & (alignment - 1)) != 0)) + { + munmap(ptr, size); + + ptr = mmap(reinterpret_cast(addr), size + alignment, protect, MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, -1, 0); // NOLINT + ret_addr = reinterpret_cast(ptr); + if (ptr != MAP_FAILED) + { + munmap(ptr, size + alignment); + auto aligned_addr = align_up(ret_addr, alignment); +#ifdef KYTY_FIXED_NOREPLACE + // NOLINTNEXTLINE + ptr = mmap(reinterpret_cast(aligned_addr), size, protect, MAP_FIXED_NOREPLACE | MAP_PRIVATE | MAP_ANON, -1, 0); +#else + // NOLINTNEXTLINE + ptr = mmap(reinterpret_cast(aligned_addr), size, protect, MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0); +#endif + ret_addr = reinterpret_cast(ptr); + if (ptr == MAP_FAILED) + { + [[maybe_unused]] int err = errno; + // printf("mmap failed: %d\n", err); + } + if (ptr != MAP_FAILED && ((ret_addr & (alignment - 1)) != 0)) + { + munmap(ptr, size); + ret_addr = 0; + ptr = MAP_FAILED; + } + } + } + + if (ptr == MAP_FAILED) + { + return sys_virtual_alloc_aligned(address, size, mode, alignment << 1u); + } + + pthread_mutex_lock(&g_virtual_mutex); + (*g_allocs)[ret_addr] = size; + uintptr_t page_start = ret_addr >> 12u; + uintptr_t page_end = (ret_addr + size - 1) >> 12u; + for (uintptr_t page = page_start; page <= page_end; page++) + { + (*g_protects)[page] = protect; + } + pthread_mutex_unlock(&g_virtual_mutex); + + return ret_addr; +} + +[[maybe_unused]] static bool is_mmaped(void* ptr, size_t length) +{ + FILE* file = fopen("/proc/self/maps", "r"); + char line[1024]; + bool ret = false; + auto addr = reinterpret_cast(ptr); + + [[maybe_unused]] int result = 0; + + while (feof(file) == 0) + { + if (fgets(line, 1024, file) == nullptr) + { + break; + } + uint64_t start = 0; + uint64_t end = 0; + // NOLINTNEXTLINE(cert-err34-c) + if (sscanf(line, "%" SCNx64 "-%" SCNx64, &start, &end) != 2) + { + continue; + } + if (addr >= start && addr + length <= end) + { + ret = true; + break; + } + } + result = fclose(file); + return ret; +} + +bool sys_virtual_alloc_fixed(uint64_t address, uint64_t size, VirtualMemory::Mode mode) +{ + EXIT_IF(g_allocs == nullptr); + + auto addr = static_cast(address); + int protect = get_protection_flag(mode); + +#ifdef KYTY_FIXED_NOREPLACE + // NOLINTNEXTLINE + void* ptr = mmap(reinterpret_cast(addr), size, protect, MAP_FIXED_NOREPLACE | MAP_PRIVATE | MAP_ANON, -1, 0); +#else + // NOLINTNEXTLINE + void* ptr = (is_mmaped(reinterpret_cast(addr), size) + ? MAP_FAILED + : mmap(reinterpret_cast(addr), size, protect, MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0)); +#endif + + auto ret_addr = reinterpret_cast(ptr); + + if (ptr != MAP_FAILED && ret_addr != addr) + { + munmap(ptr, size); + ret_addr = 0; + ptr = MAP_FAILED; + } + + if (ptr != MAP_FAILED) + { + pthread_mutex_lock(&g_virtual_mutex); + (*g_allocs)[ret_addr] = size; + uintptr_t page_start = ret_addr >> 12u; + uintptr_t page_end = (ret_addr + size - 1) >> 12u; + for (uintptr_t page = page_start; page <= page_end; page++) + { + (*g_protects)[page] = protect; + } + pthread_mutex_unlock(&g_virtual_mutex); + + return true; + } + + return false; +} + +bool sys_virtual_free(uint64_t address) +{ + EXIT_IF(g_allocs == nullptr); + size_t size = 0; + + auto addr = static_cast(address & ~static_cast(0xfffu)); + + pthread_mutex_lock(&g_virtual_mutex); + if (auto s = g_allocs->find(addr); s != g_allocs->end()) + { + size = s->second; + g_allocs->erase(s); + } + pthread_mutex_unlock(&g_virtual_mutex); + + if (size == 0) + { + return false; + } + + if (munmap(reinterpret_cast(addr), size) == 0) + { + uintptr_t page_start = addr >> 12u; + uintptr_t page_end = (addr + size - 1) >> 12u; + pthread_mutex_lock(&g_virtual_mutex); + for (uintptr_t page = page_start; page <= page_end; page++) + { + if (auto s = g_protects->find(page); s != g_protects->end()) + { + g_protects->erase(s); + } + } + pthread_mutex_unlock(&g_virtual_mutex); + return true; + } + + return false; +} + +bool sys_virtual_protect(uint64_t address, uint64_t size, VirtualMemory::Mode mode, VirtualMemory::Mode* old_mode) +{ + auto addr = static_cast(address); + + pthread_mutex_lock(&g_virtual_mutex); + if (old_mode != nullptr) + { + if (auto s = g_protects->find(addr >> 12u); s != g_protects->end()) + { + *old_mode = get_protection_flag(s->second); + } else + { + *old_mode = VirtualMemory::Mode::NoAccess; + } + } + pthread_mutex_unlock(&g_virtual_mutex); + + uintptr_t page_start = addr >> 12u; + uintptr_t page_end = (addr + size - 1) >> 12u; + if (mprotect(reinterpret_cast(page_start << 12u), (page_end - page_start + 1) << 12u, get_protection_flag(mode)) == 0) + { + pthread_mutex_lock(&g_virtual_mutex); + for (uintptr_t page = page_start; page <= page_end; page++) + { + (*g_protects)[page] = get_protection_flag(mode); + } + pthread_mutex_unlock(&g_virtual_mutex); + return true; + } + + return false; +} + +bool sys_virtual_flush_instruction_cache(uint64_t /*address*/, uint64_t /*size*/) +{ + return true; +} + +bool sys_virtual_patch_replace(uint64_t vaddr, uint64_t value) +{ + VirtualMemory::Mode old_mode {}; + sys_virtual_protect(vaddr, 8, VirtualMemory::Mode::ReadWrite, &old_mode); + + auto* ptr = reinterpret_cast(vaddr); + + bool ret = (*ptr != value); + + *ptr = value; + + sys_virtual_protect(vaddr, 8, old_mode); + + if (VirtualMemory::IsExecute(old_mode)) + { + sys_virtual_flush_instruction_cache(vaddr, 8); + } + + return ret; +} + +} // namespace Kyty::Core + +#endif From b645002bad11c1214c577c4b494282181021467e Mon Sep 17 00:00:00 2001 From: Smiril Date: Sat, 11 May 2024 21:46:37 +0200 Subject: [PATCH 15/42] Update SysVirtual.h add macOS support --- source/include/Kyty/Sys/SysVirtual.h | 1 + 1 file changed, 1 insertion(+) diff --git a/source/include/Kyty/Sys/SysVirtual.h b/source/include/Kyty/Sys/SysVirtual.h index 42af285..2bb6c28 100644 --- a/source/include/Kyty/Sys/SysVirtual.h +++ b/source/include/Kyty/Sys/SysVirtual.h @@ -3,6 +3,7 @@ #include "Kyty/Core/Common.h" #include "Kyty/Sys/Linux/SysLinuxVirtual.h" // IWYU pragma: export +#include "Kyty/Sys/MacOS/SysMacOSVirtual.h" // IWYU pragma: export #include "Kyty/Sys/Windows/SysWindowsVirtual.h" // IWYU pragma: export #endif /* INCLUDE_KYTY_SYS_SYSVIRTUAL_H_ */ From 8487cdb2735c38f123ade4bd10715cd65b0417dc Mon Sep 17 00:00:00 2001 From: Smiril Date: Sat, 11 May 2024 21:47:36 +0200 Subject: [PATCH 16/42] Update SysTimer.h add macOS support --- source/include/Kyty/Sys/SysTimer.h | 1 + 1 file changed, 1 insertion(+) diff --git a/source/include/Kyty/Sys/SysTimer.h b/source/include/Kyty/Sys/SysTimer.h index b2c5262..66a93b2 100644 --- a/source/include/Kyty/Sys/SysTimer.h +++ b/source/include/Kyty/Sys/SysTimer.h @@ -3,6 +3,7 @@ #include "Kyty/Core/Common.h" #include "Kyty/Sys/Linux/SysLinuxTimer.h" // IWYU pragma: export +#include "Kyty/Sys/MacOS/SysMacOSTimer.h" // IWYU pragma: export #include "Kyty/Sys/Windows/SysWindowsTimer.h" // IWYU pragma: export #endif /* INCLUDE_KYTY_SYS_SYSTIMER_H_ */ From d762b92ce7df00aceca8142eaa91011f25e35a92 Mon Sep 17 00:00:00 2001 From: Smiril Date: Sat, 11 May 2024 21:48:31 +0200 Subject: [PATCH 17/42] Update SysSync.h add macOS support --- source/include/Kyty/Sys/SysSync.h | 1 + 1 file changed, 1 insertion(+) diff --git a/source/include/Kyty/Sys/SysSync.h b/source/include/Kyty/Sys/SysSync.h index cf18c2e..aff1454 100644 --- a/source/include/Kyty/Sys/SysSync.h +++ b/source/include/Kyty/Sys/SysSync.h @@ -3,6 +3,7 @@ #include "Kyty/Core/Common.h" #include "Kyty/Sys/Linux/SysLinuxSync.h" // IWYU pragma: export +#include "Kyty/Sys/MacOS/SysMacOSSync.h" // IWYU pragma: export #include "Kyty/Sys/Windows/SysWindowsSync.h" // IWYU pragma: export #endif /* INCLUDE_KYTY_SYS_SYSSYNC_H_ */ From 011f85dc0adc02206bf64e5836e2e6fb81ac59bc Mon Sep 17 00:00:00 2001 From: Smiril Date: Sat, 11 May 2024 21:49:42 +0200 Subject: [PATCH 18/42] Update SysStdlib.h add macOS support --- source/include/Kyty/Sys/SysStdlib.h | 1 + 1 file changed, 1 insertion(+) diff --git a/source/include/Kyty/Sys/SysStdlib.h b/source/include/Kyty/Sys/SysStdlib.h index 55be2a1..c25685a 100644 --- a/source/include/Kyty/Sys/SysStdlib.h +++ b/source/include/Kyty/Sys/SysStdlib.h @@ -3,6 +3,7 @@ #include "Kyty/Core/Common.h" #include "Kyty/Sys/Linux/SysLinuxStdlib.h" // IWYU pragma: export +#include "Kyty/Sys/MacOS/SysMacOSStdlib.h" // IWYU pragma: export #include "Kyty/Sys/Windows/SysWindowsStdlib.h" // IWYU pragma: export #endif /* INCLUDE_KYTY_SYS_SYSSTDLIB_H_ */ From bfc71fecc1e43776a87f90ed1fd9b1504c11ab24 Mon Sep 17 00:00:00 2001 From: Smiril Date: Sat, 11 May 2024 21:50:32 +0200 Subject: [PATCH 19/42] Update SysStdio.h add macOS support --- source/include/Kyty/Sys/SysStdio.h | 1 + 1 file changed, 1 insertion(+) diff --git a/source/include/Kyty/Sys/SysStdio.h b/source/include/Kyty/Sys/SysStdio.h index 01c1f68..b73e05c 100644 --- a/source/include/Kyty/Sys/SysStdio.h +++ b/source/include/Kyty/Sys/SysStdio.h @@ -3,6 +3,7 @@ #include "Kyty/Core/Common.h" #include "Kyty/Sys/Linux/SysLinuxStdio.h" // IWYU pragma: export +#include "Kyty/Sys/MacOS/SysMacOSStdio.h" // IWYU pragma: export #include "Kyty/Sys/Windows/SysWindowsStdio.h" // IWYU pragma: export #endif /* INCLUDE_KYTY_SYS_SYSSTDIO_H_ */ From 7e6d80b1ce7b87b781d90de6d6414943dcd5fa62 Mon Sep 17 00:00:00 2001 From: Smiril Date: Sat, 11 May 2024 21:51:19 +0200 Subject: [PATCH 20/42] Update SysHeap.h add macOS support --- source/include/Kyty/Sys/SysHeap.h | 1 + 1 file changed, 1 insertion(+) diff --git a/source/include/Kyty/Sys/SysHeap.h b/source/include/Kyty/Sys/SysHeap.h index dfa9f82..24bc644 100644 --- a/source/include/Kyty/Sys/SysHeap.h +++ b/source/include/Kyty/Sys/SysHeap.h @@ -3,6 +3,7 @@ #include "Kyty/Core/Common.h" #include "Kyty/Sys/Linux/SysLinuxHeap.h" // IWYU pragma: export +#include "Kyty/Sys/MacOS/SysMacOSHeap.h" // IWYU pragma: export #include "Kyty/Sys/Windows/SysWindowsHeap.h" // IWYU pragma: export #endif /* INCLUDE_KYTY_SYS_SYSHEAP_H_ */ From 9ead86a9e7cbb1b6717f24e663032b6c143f6ee7 Mon Sep 17 00:00:00 2001 From: Smiril Date: Sat, 11 May 2024 21:52:11 +0200 Subject: [PATCH 21/42] Update SysFileIO.h add macOS support --- source/include/Kyty/Sys/SysFileIO.h | 1 + 1 file changed, 1 insertion(+) diff --git a/source/include/Kyty/Sys/SysFileIO.h b/source/include/Kyty/Sys/SysFileIO.h index 0a8aeb1..2e3520e 100644 --- a/source/include/Kyty/Sys/SysFileIO.h +++ b/source/include/Kyty/Sys/SysFileIO.h @@ -3,6 +3,7 @@ #include "Kyty/Core/Common.h" #include "Kyty/Sys/Linux/SysLinuxFileIO.h" // IWYU pragma: export +#include "Kyty/Sys/MacOS/SysMacOSFileIO.h" // IWYU pragma: export #include "Kyty/Sys/Windows/SysWindowsFileIO.h" // IWYU pragma: export #endif /* INCLUDE_KYTY_SYS_SYSFILEIO_H_ */ From 18bcdeb6c5a596362c74040a9520881f0514e62d Mon Sep 17 00:00:00 2001 From: Smiril Date: Sat, 11 May 2024 21:53:00 +0200 Subject: [PATCH 22/42] Update SysDbg.h add macOS support --- source/include/Kyty/Sys/SysDbg.h | 1 + 1 file changed, 1 insertion(+) diff --git a/source/include/Kyty/Sys/SysDbg.h b/source/include/Kyty/Sys/SysDbg.h index 60394b8..8997f17 100644 --- a/source/include/Kyty/Sys/SysDbg.h +++ b/source/include/Kyty/Sys/SysDbg.h @@ -3,6 +3,7 @@ #include "Kyty/Core/Common.h" #include "Kyty/Sys/Linux/SysLinuxDbg.h" // IWYU pragma: export +#include "Kyty/Sys/MacOS/SysMacOSDbg.h" // IWYU pragma: export #include "Kyty/Sys/Windows/SysWindowsDbg.h" // IWYU pragma: export #endif /* INCLUDE_KYTY_SYS_SYSDBG_H_ */ From cbad2533e455937593177e7e29ef553c782a172d Mon Sep 17 00:00:00 2001 From: Smiril Date: Sat, 11 May 2024 21:55:06 +0200 Subject: [PATCH 23/42] Update kyty_config.h typo fix --- source/include/kyty_config.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/include/kyty_config.h b/source/include/kyty_config.h index 202886a..a97574d 100644 --- a/source/include/kyty_config.h +++ b/source/include/kyty_config.h @@ -3,7 +3,7 @@ #define KYTY_PLATFORM_WINDOWS 1 #define KYTY_PLATFORM_ANDROID 2 -#define KYTY_PLATFORM_OSX 3 +#define KYTY_PLATFORM_MACOS 3 #define KYTY_PLATFORM_IOS 4 #define KYTY_PLATFORM_LINUX 5 From c6701e2f0de0b37e828e7cf4c2917adc112732e8 Mon Sep 17 00:00:00 2001 From: Smiril Date: Sat, 11 May 2024 23:01:33 +0200 Subject: [PATCH 24/42] add macos support --- _Build/cmake_MacOSMakeGcc.sh | 0 source/.DS_Store | Bin 0 -> 6148 bytes source/CMakeLists.txt | 3 - source/include/Kyty/Sys/SysDbg.h | 10 +- source/include/Kyty/Sys/SysFileIO.h | 10 +- source/include/Kyty/Sys/SysHeap.h | 10 +- source/include/Kyty/Sys/SysStdio.h | 13 +- source/include/Kyty/Sys/SysStdlib.h | 10 +- source/include/Kyty/Sys/SysSync.h | 10 +- source/include/Kyty/Sys/SysTimer.h | 10 +- source/include/Kyty/Sys/SysVirtual.h | 10 +- .../Kyty/Sys/Windows/SysWindowsFileIO.h | 4 +- .../Kyty/Sys/Windows/SysWindowsTimer.h | 136 +++---- source/launcher/CMakeLists.txt | 7 - source/lib/Core/src/Compression.cpp | 2 +- source/lib/Sys/src/SysWindowsDbg.cpp | 333 +++++++++--------- source/lib/Sys/src/SysWindowsFileIO.cpp | 10 +- source/lib/Sys/src/SysWindowsVirtual.cpp | 11 +- 18 files changed, 331 insertions(+), 258 deletions(-) mode change 100644 => 100755 _Build/cmake_MacOSMakeGcc.sh create mode 100644 source/.DS_Store diff --git a/_Build/cmake_MacOSMakeGcc.sh b/_Build/cmake_MacOSMakeGcc.sh old mode 100644 new mode 100755 diff --git a/source/.DS_Store b/source/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..1ba28d1789d62c2cf562716f7574e192f28d9d3a GIT binary patch literal 6148 zcmeHK%}xR_5S{`;k(hAMgkux0B;rp1<7I>R04cG+PejaAc1>`;nV(=K1SaBUK|aEwc0z&WOEz&0x$3qzjeEE><+!* zXwdTd*YtEDW$e%9o_`sI<4$?|R3<|&O2WP>h=LGPZmyyvkmHsdCBZ<|x;ns%yx1vM zCX+_3Srv_Xb6ORX(#nh$2a%uI*E)YtL-LZt(9G}_TBU=^5GAgdqCbpM}y-~Z1S*(a-jRbZ_YV7a#2 zZXqRew=R*RyH=pzp_5Tws!^5TV6I~|p{uxpt_)*>CWwx~R3omS**^kG20K^ww1k literal 0 HcmV?d00001 diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 2432499..22742b4 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -6,9 +6,6 @@ if(CMAKE_SYSTEM_NAME MATCHES ".*Linux") set(LINUX TRUE) endif() -if (NOT ((WIN32 AND (MINGW OR MSVC)) OR LINUX)) - message(FATAL_ERROR "only mingw and msvc supported (or linux)") -endif() set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) diff --git a/source/include/Kyty/Sys/SysDbg.h b/source/include/Kyty/Sys/SysDbg.h index 8997f17..24dd2f5 100644 --- a/source/include/Kyty/Sys/SysDbg.h +++ b/source/include/Kyty/Sys/SysDbg.h @@ -2,8 +2,14 @@ #define INCLUDE_KYTY_SYS_SYSDBG_H_ #include "Kyty/Core/Common.h" -#include "Kyty/Sys/Linux/SysLinuxDbg.h" // IWYU pragma: export -#include "Kyty/Sys/MacOS/SysMacOSDbg.h" // IWYU pragma: export +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) #include "Kyty/Sys/Windows/SysWindowsDbg.h" // IWYU pragma: export +#elif __linux__ +#include "Kyty/Sys/Linux/SysLinuxDbg.h" // IWYU pragma: export +#elif __APPLE__ +#include "Kyty/Sys/MacOS/SysMacOSDbg.h" // IWYU pragma: export +#else +# error "Unknown compiler" +#endif #endif /* INCLUDE_KYTY_SYS_SYSDBG_H_ */ diff --git a/source/include/Kyty/Sys/SysFileIO.h b/source/include/Kyty/Sys/SysFileIO.h index 2e3520e..999218c 100644 --- a/source/include/Kyty/Sys/SysFileIO.h +++ b/source/include/Kyty/Sys/SysFileIO.h @@ -2,8 +2,14 @@ #define INCLUDE_KYTY_SYS_SYSFILEIO_H_ #include "Kyty/Core/Common.h" -#include "Kyty/Sys/Linux/SysLinuxFileIO.h" // IWYU pragma: export -#include "Kyty/Sys/MacOS/SysMacOSFileIO.h" // IWYU pragma: export +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) #include "Kyty/Sys/Windows/SysWindowsFileIO.h" // IWYU pragma: export +#elif __linux__ +#include "Kyty/Sys/Linux/SysLinuxFileIO.h" // IWYU pragma: export +#elif __APPLE__ +#include "Kyty/Sys/MacOS/SysMacOSFileIO.h" // IWYU pragma: export +#else +# error "Unknown compiler" +#endif #endif /* INCLUDE_KYTY_SYS_SYSFILEIO_H_ */ diff --git a/source/include/Kyty/Sys/SysHeap.h b/source/include/Kyty/Sys/SysHeap.h index 24bc644..69e7e6a 100644 --- a/source/include/Kyty/Sys/SysHeap.h +++ b/source/include/Kyty/Sys/SysHeap.h @@ -2,8 +2,14 @@ #define INCLUDE_KYTY_SYS_SYSHEAP_H_ #include "Kyty/Core/Common.h" -#include "Kyty/Sys/Linux/SysLinuxHeap.h" // IWYU pragma: export -#include "Kyty/Sys/MacOS/SysMacOSHeap.h" // IWYU pragma: export +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) #include "Kyty/Sys/Windows/SysWindowsHeap.h" // IWYU pragma: export +#elif __linux__ +#include "Kyty/Sys/Linux/SysLinuxHeap.h" // IWYU pragma: export +#elif __APPLE__ +#include "Kyty/Sys/MacOS/SysMacOSHeap.h" // IWYU pragma: export +#else +# error "Unknown compiler" +#endif #endif /* INCLUDE_KYTY_SYS_SYSHEAP_H_ */ diff --git a/source/include/Kyty/Sys/SysStdio.h b/source/include/Kyty/Sys/SysStdio.h index b73e05c..bf7659e 100644 --- a/source/include/Kyty/Sys/SysStdio.h +++ b/source/include/Kyty/Sys/SysStdio.h @@ -2,8 +2,15 @@ #define INCLUDE_KYTY_SYS_SYSSTDIO_H_ #include "Kyty/Core/Common.h" -#include "Kyty/Sys/Linux/SysLinuxStdio.h" // IWYU pragma: export -#include "Kyty/Sys/MacOS/SysMacOSStdio.h" // IWYU pragma: export -#include "Kyty/Sys/Windows/SysWindowsStdio.h" // IWYU pragma: export + +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) +#include "Kyty/Sys/Windows/SysWindowsStdio.h" // +#elif __APPLE__ +#include "Kyty/Sys/MacOS/SysMacOSStdio.h" // +#elif __linux__ +#include "Kyty/Sys/Linux/SysLinuxStdio.h" // +#else +# error "Unknown compiler" +#endif #endif /* INCLUDE_KYTY_SYS_SYSSTDIO_H_ */ diff --git a/source/include/Kyty/Sys/SysStdlib.h b/source/include/Kyty/Sys/SysStdlib.h index c25685a..a8462df 100644 --- a/source/include/Kyty/Sys/SysStdlib.h +++ b/source/include/Kyty/Sys/SysStdlib.h @@ -2,8 +2,14 @@ #define INCLUDE_KYTY_SYS_SYSSTDLIB_H_ #include "Kyty/Core/Common.h" -#include "Kyty/Sys/Linux/SysLinuxStdlib.h" // IWYU pragma: export -#include "Kyty/Sys/MacOS/SysMacOSStdlib.h" // IWYU pragma: export +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) #include "Kyty/Sys/Windows/SysWindowsStdlib.h" // IWYU pragma: export +#elif __linux__ +#include "Kyty/Sys/Linux/SysLinuxStdlib.h" // IWYU pragma: export +#elif __APPLE__ +#include "Kyty/Sys/MacOS/SysMacOSStdlib.h" // IWYU pragma: export +#else +# error "Unknown compiler" +#endif #endif /* INCLUDE_KYTY_SYS_SYSSTDLIB_H_ */ diff --git a/source/include/Kyty/Sys/SysSync.h b/source/include/Kyty/Sys/SysSync.h index aff1454..86303f2 100644 --- a/source/include/Kyty/Sys/SysSync.h +++ b/source/include/Kyty/Sys/SysSync.h @@ -2,8 +2,14 @@ #define INCLUDE_KYTY_SYS_SYSSYNC_H_ #include "Kyty/Core/Common.h" -#include "Kyty/Sys/Linux/SysLinuxSync.h" // IWYU pragma: export -#include "Kyty/Sys/MacOS/SysMacOSSync.h" // IWYU pragma: export +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) #include "Kyty/Sys/Windows/SysWindowsSync.h" // IWYU pragma: export +#elif __linux__ +#include "Kyty/Sys/Linux/SysLinuxSync.h" // IWYU pragma: export +#elif __APPLE__ +#include "Kyty/Sys/MacOS/SysMacOSSync.h" // IWYU pragma: export +#else +# error "Unknown compiler" +#endif #endif /* INCLUDE_KYTY_SYS_SYSSYNC_H_ */ diff --git a/source/include/Kyty/Sys/SysTimer.h b/source/include/Kyty/Sys/SysTimer.h index 66a93b2..7d3730a 100644 --- a/source/include/Kyty/Sys/SysTimer.h +++ b/source/include/Kyty/Sys/SysTimer.h @@ -2,8 +2,14 @@ #define INCLUDE_KYTY_SYS_SYSTIMER_H_ #include "Kyty/Core/Common.h" -#include "Kyty/Sys/Linux/SysLinuxTimer.h" // IWYU pragma: export -#include "Kyty/Sys/MacOS/SysMacOSTimer.h" // IWYU pragma: export +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) #include "Kyty/Sys/Windows/SysWindowsTimer.h" // IWYU pragma: export +#elif __linux__ +#include "Kyty/Sys/Linux/SysLinuxTimer.h" // IWYU pragma: export +#elif __APPLE__ +#include "Kyty/Sys/MacOS/SysMacOSTimer.h" // IWYU pragma: export +#else +# error "Unknown compiler" +#endif #endif /* INCLUDE_KYTY_SYS_SYSTIMER_H_ */ diff --git a/source/include/Kyty/Sys/SysVirtual.h b/source/include/Kyty/Sys/SysVirtual.h index 2bb6c28..473902a 100644 --- a/source/include/Kyty/Sys/SysVirtual.h +++ b/source/include/Kyty/Sys/SysVirtual.h @@ -2,8 +2,14 @@ #define INCLUDE_KYTY_SYS_SYSVIRTUAL_H_ #include "Kyty/Core/Common.h" -#include "Kyty/Sys/Linux/SysLinuxVirtual.h" // IWYU pragma: export -#include "Kyty/Sys/MacOS/SysMacOSVirtual.h" // IWYU pragma: export +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) #include "Kyty/Sys/Windows/SysWindowsVirtual.h" // IWYU pragma: export +#elif __linux__ +#include "Kyty/Sys/Linux/SysLinuxVirtual.h" // IWYU pragma: export +#elif __APPLE__ +#include "Kyty/Sys/MacOS/SysMacOSVirtual.h" // IWYU pragma: export +#else +# error "Unknown compiler" +#endif #endif /* INCLUDE_KYTY_SYS_SYSVIRTUAL_H_ */ diff --git a/source/include/Kyty/Sys/Windows/SysWindowsFileIO.h b/source/include/Kyty/Sys/Windows/SysWindowsFileIO.h index 8d4459b..4c5dbb1 100644 --- a/source/include/Kyty/Sys/Windows/SysWindowsFileIO.h +++ b/source/include/Kyty/Sys/Windows/SysWindowsFileIO.h @@ -10,7 +10,7 @@ #include "Kyty/Core/Common.h" #include "Kyty/Core/String.h" #include "Kyty/Sys/SysTimer.h" - +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) namespace Kyty { using KYTY_HANDLE = void*; @@ -120,4 +120,6 @@ void sys_file_remove_readonly(const String& name); #endif +#endif + #endif /* SYS_WIN32_INCLUDE_KYTY_SYSFILEIO_H_ */ diff --git a/source/include/Kyty/Sys/Windows/SysWindowsTimer.h b/source/include/Kyty/Sys/Windows/SysWindowsTimer.h index c1bd07b..d3ed606 100644 --- a/source/include/Kyty/Sys/Windows/SysWindowsTimer.h +++ b/source/include/Kyty/Sys/Windows/SysWindowsTimer.h @@ -7,122 +7,124 @@ //#error "KYTY_PLATFORM != KYTY_PLATFORM_WINDOWS" #else -#include + +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) + #include // IWYU pragma: keep namespace Kyty { struct SysTimeStruct { - uint16_t Year; // NOLINT(readability-identifier-naming) - uint16_t Month; // NOLINT(readability-identifier-naming) - uint16_t Day; // NOLINT(readability-identifier-naming) - uint16_t Hour; // NOLINT(readability-identifier-naming) - uint16_t Minute; // NOLINT(readability-identifier-naming) - uint16_t Second; // NOLINT(readability-identifier-naming) - uint16_t Milliseconds; // NOLINT(readability-identifier-naming) - bool is_invalid; // NOLINT(readability-identifier-naming) + uint16_t Year; // NOLINT(readability-identifier-naming) + uint16_t Month; // NOLINT(readability-identifier-naming) + uint16_t Day; // NOLINT(readability-identifier-naming) + uint16_t Hour; // NOLINT(readability-identifier-naming) + uint16_t Minute; // NOLINT(readability-identifier-naming) + uint16_t Second; // NOLINT(readability-identifier-naming) + uint16_t Milliseconds; // NOLINT(readability-identifier-naming) + bool is_invalid; // NOLINT(readability-identifier-naming) }; struct SysFileTimeStruct { - FILETIME time; - bool is_invalid; + FILETIME time; + bool is_invalid; }; // NOLINTNEXTLINE(google-runtime-references) inline void sys_file_to_system_time_utc(const SysFileTimeStruct& f, SysTimeStruct& t) { - SYSTEMTIME s; + SYSTEMTIME s; - if (f.is_invalid || (FileTimeToSystemTime(&f.time, &s) == 0)) - { - t.is_invalid = true; - return; - } + if (f.is_invalid || (FileTimeToSystemTime(&f.time, &s) == 0)) + { + t.is_invalid = true; + return; + } - t.is_invalid = false; - t.Year = s.wYear; - t.Month = s.wMonth; - t.Day = s.wDay; - t.Hour = s.wHour; - t.Minute = s.wMinute; - t.Second = (s.wSecond == 60 ? 59 : s.wSecond); - t.Milliseconds = s.wMilliseconds; + t.is_invalid = false; + t.Year = s.wYear; + t.Month = s.wMonth; + t.Day = s.wDay; + t.Hour = s.wHour; + t.Minute = s.wMinute; + t.Second = (s.wSecond == 60 ? 59 : s.wSecond); + t.Milliseconds = s.wMilliseconds; } // NOLINTNEXTLINE(google-runtime-references) inline void sys_time_t_to_system(time_t t, SysTimeStruct& s) { - SysFileTimeStruct ft {}; - LONGLONG ll = Int32x32To64(t, 10000000) + 116444736000000000; - ft.time.dwLowDateTime = static_cast(ll); - ft.time.dwHighDateTime = static_cast(static_cast(ll) >> 32u); - ft.is_invalid = false; - sys_file_to_system_time_utc(ft, s); + SysFileTimeStruct ft {}; + LONGLONG ll = Int32x32To64(t, 10000000) + 116444736000000000; + ft.time.dwLowDateTime = static_cast(ll); + ft.time.dwHighDateTime = static_cast(static_cast(ll) >> 32u); + ft.is_invalid = false; + sys_file_to_system_time_utc(ft, s); } // NOLINTNEXTLINE(google-runtime-references) inline void sys_system_to_file_time_utc(const SysTimeStruct& f, SysFileTimeStruct& t) { - SYSTEMTIME s; + SYSTEMTIME s; - s.wYear = f.Year; - s.wMonth = f.Month; - s.wDay = f.Day; - s.wHour = f.Hour; - s.wMinute = f.Minute; - s.wSecond = f.Second; - s.wMilliseconds = f.Milliseconds; + s.wYear = f.Year; + s.wMonth = f.Month; + s.wDay = f.Day; + s.wHour = f.Hour; + s.wMinute = f.Minute; + s.wSecond = f.Second; + s.wMilliseconds = f.Milliseconds; - t.is_invalid = (f.is_invalid || (SystemTimeToFileTime(&s, &t.time) == 0)); + t.is_invalid = (f.is_invalid || (SystemTimeToFileTime(&s, &t.time) == 0)); } // Retrieves the current local date and time // NOLINTNEXTLINE(google-runtime-references) inline void sys_get_system_time(SysTimeStruct& t) { - SYSTEMTIME s; - GetLocalTime(&s); + SYSTEMTIME s; + GetLocalTime(&s); - t.is_invalid = false; - t.Year = s.wYear; - t.Month = s.wMonth; - t.Day = s.wDay; - t.Hour = s.wHour; - t.Minute = s.wMinute; - t.Second = (s.wSecond == 60 ? 59 : s.wSecond); - t.Milliseconds = s.wMilliseconds; + t.is_invalid = false; + t.Year = s.wYear; + t.Month = s.wMonth; + t.Day = s.wDay; + t.Hour = s.wHour; + t.Minute = s.wMinute; + t.Second = (s.wSecond == 60 ? 59 : s.wSecond); + t.Milliseconds = s.wMilliseconds; } // Retrieves the current system date and time in Coordinated Universal Time (UTC). // NOLINTNEXTLINE(google-runtime-references) inline void sys_get_system_time_utc(SysTimeStruct& t) { - SYSTEMTIME s; - GetSystemTime(&s); + SYSTEMTIME s; + GetSystemTime(&s); - t.is_invalid = false; - t.Year = s.wYear; - t.Month = s.wMonth; - t.Day = s.wDay; - t.Hour = s.wHour; - t.Minute = s.wMinute; - t.Second = (s.wSecond == 60 ? 59 : s.wSecond); - t.Milliseconds = s.wMilliseconds; + t.is_invalid = false; + t.Year = s.wYear; + t.Month = s.wMonth; + t.Day = s.wDay; + t.Hour = s.wHour; + t.Minute = s.wMinute; + t.Second = (s.wSecond == 60 ? 59 : s.wSecond); + t.Milliseconds = s.wMilliseconds; } inline void sys_query_performance_frequency(uint64_t* freq) { - LARGE_INTEGER f; - QueryPerformanceFrequency(&f); - *freq = f.QuadPart; + LARGE_INTEGER f; + QueryPerformanceFrequency(&f); + *freq = f.QuadPart; } inline void sys_query_performance_counter(uint64_t* counter) { - LARGE_INTEGER c; - QueryPerformanceCounter(&c); - *counter = c.QuadPart; + LARGE_INTEGER c; + QueryPerformanceCounter(&c); + *counter = c.QuadPart; } } // namespace Kyty @@ -130,3 +132,5 @@ inline void sys_query_performance_counter(uint64_t* counter) #endif #endif /* SYS_WIN32_INCLUDE_KYTY_SYSTIMER_H_ */ + +#endif diff --git a/source/launcher/CMakeLists.txt b/source/launcher/CMakeLists.txt index eb536ce..112539c 100644 --- a/source/launcher/CMakeLists.txt +++ b/source/launcher/CMakeLists.txt @@ -47,13 +47,6 @@ endif() add_dependencies(launcher KytyGitVersion) -if(NOT LINUX) - find_program(QT_WINDEPLOYQT NAMES windeployqt PATHS "${Qt5_DIR}/../../../bin") - if(NOT QT_WINDEPLOYQT) - message(FATAL_ERROR "Could not find windeployqt") - endif() -endif() - set(launcher_name "launcher") set_target_properties(launcher PROPERTIES OUTPUT_NAME ${launcher_name}) diff --git a/source/lib/Core/src/Compression.cpp b/source/lib/Core/src/Compression.cpp index 7af6512..45b1895 100644 --- a/source/lib/Core/src/Compression.cpp +++ b/source/lib/Core/src/Compression.cpp @@ -1188,7 +1188,7 @@ bool ZipReader::GetFileStat(int file_index, ZipFileStat* o) o->m_file_index = s.m_file_index; SysTimeStruct at = {0}; - sys_time_t_to_system(s.m_time, at); + sys_time_t_to_system(s.m_time, at); o->m_time = DateTime(Date(at.Year, at.Month, at.Day), Time(at.Hour, at.Minute, at.Second, at.Milliseconds)); o->m_crc32 = s.m_crc32; diff --git a/source/lib/Sys/src/SysWindowsDbg.cpp b/source/lib/Sys/src/SysWindowsDbg.cpp index f75189e..83bc7c8 100644 --- a/source/lib/Sys/src/SysWindowsDbg.cpp +++ b/source/lib/Sys/src/SysWindowsDbg.cpp @@ -17,11 +17,25 @@ #include "Kyty/Core/DbgAssert.h" #include "Kyty/Sys/SysDbg.h" -#include // IWYU pragma: keep +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) + #include // IWYU pragma: keep + #include // IWYU pragma: keep + #ifdef _WIN64 + #include // IWYU pragma: keep + #include // IWYU pragma: keep + #else + #include // IWYU pragma: keep + #include // IWYU pragma: keep +#endif +#endif + +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) + + #if KYTY_COMPILER == KYTY_COMPILER_MSVC #include #endif -#include // IWYU pragma: keep + namespace Kyty { @@ -35,10 +49,9 @@ static thread_local sys_dbg_stack_info_t g_stack = {0}; struct FrameS { - struct FrameS* next; - void* ret_addr; + struct FrameS* next; + void* ret_addr; }; - constexpr DWORD READABLE = (static_cast(PAGE_EXECUTE_READ) | static_cast(PAGE_EXECUTE_READWRITE) | static_cast(PAGE_EXECUTE_WRITECOPY) | static_cast(PAGE_READONLY) | static_cast(PAGE_READWRITE) | static_cast(PAGE_WRITECOPY)); @@ -48,94 +61,94 @@ exception_filter_func_t g_exception_filter_func = nullptr; bool sys_mem_read_allowed(void* ptr) { - MEMORY_BASIC_INFORMATION mbi; + MEMORY_BASIC_INFORMATION mbi; - size_t s = VirtualQuery(ptr, &mbi, sizeof(mbi)); + size_t s = VirtualQuery(ptr, &mbi, sizeof(mbi)); - if (s == 0) - { - EXIT_IF(s == 0); - } + if (s == 0) + { + EXIT_IF(s == 0); + } - return ((mbi.Protect & PROTECTED) == 0u) && ((mbi.State & static_cast(MEM_COMMIT)) != 0u) && - ((mbi.AllocationProtect & READABLE) != 0u); + return ((mbi.Protect & PROTECTED) == 0u) && ((mbi.State & static_cast(MEM_COMMIT)) != 0u) && + ((mbi.AllocationProtect & READABLE) != 0u); } // LONG WINAPI // VectoredHandlerSkip(struct _EXCEPTION_POINTERS *ExceptionInfo) //{ -// PCONTEXT Context; +// PCONTEXT Context; // -// g_need_break = true; +// g_need_break = true; // -// Context = ExceptionInfo->ContextRecord; +// Context = ExceptionInfo->ContextRecord; //#ifdef _AMD64_ -// Context->Rip++; +// Context->Rip++; //#else -// Context->Eip++; +// Context->Eip++; //#endif -// return EXCEPTION_CONTINUE_EXECUTION; +// return EXCEPTION_CONTINUE_EXECUTION; //} #if KYTY_BITNESS == 32 static void stackwalk(void* ebp, void** stack, int* depth, uintptr_t stack_addr, size_t stack_size) { - frame_t* frame = (frame_t*)ebp; + frame_t* frame = (frame_t*)ebp; - int d = *depth; + int d = *depth; - int i; + int i; - // printf("1\n"); - for (i = 0; i < KYTY_FRAME_SKIP; i++) - { - // if (uintptr_t(frame) <= 0xffff || (uintptr_t(frame) & 0xf0000000) - // || frame->ret_addr == 0 - // || (uintptr_t(frame->ret_addr) & 0xf0000000) - // || (uintptr_t(frame->next) & 0xf0000000)) break; - // if (!sys_mem_read_allowed(&frame->next)) break; - if (!(uintptr_t(frame) >= stack_addr && uintptr_t(frame) < stack_addr + stack_size)) break; + // printf("1\n"); + for (i = 0; i < KYTY_FRAME_SKIP; i++) + { + // if (uintptr_t(frame) <= 0xffff || (uintptr_t(frame) & 0xf0000000) + // || frame->ret_addr == 0 + // || (uintptr_t(frame->ret_addr) & 0xf0000000) + // || (uintptr_t(frame->next) & 0xf0000000)) break; + // if (!sys_mem_read_allowed(&frame->next)) break; + if (!(uintptr_t(frame) >= stack_addr && uintptr_t(frame) < stack_addr + stack_size)) break; - frame = frame->next; - } - // printf("2\n"); - for (i = 0; i < d; i++) - { - //#ifdef _MSC_VER - // __try - // { - //#endif - // FILE *f = fopen("_sw", "wt"); - // printf("%d, %08x\n", i, (uint32_t)frame); - // fflush(stdout); - // fclose(f); - // printf("%d, %08x, %08x, %08x\n", i, (uint32_t)frame, - // (uint32_t)frame->ret_addr, (uint32_t)frame->next); + frame = frame->next; + } + // printf("2\n"); + for (i = 0; i < d; i++) + { + //#ifdef _MSC_VER + // __try + // { + //#endif + // FILE *f = fopen("_sw", "wt"); + // printf("%d, %08x\n", i, (uint32_t)frame); + // fflush(stdout); + // fclose(f); + // printf("%d, %08x, %08x, %08x\n", i, (uint32_t)frame, + // (uint32_t)frame->ret_addr, (uint32_t)frame->next); - // if (uintptr_t(frame) <= 0xffff || (uintptr_t(frame) & 0xf0000000) - // || frame->ret_addr == 0 - // || (uintptr_t(frame->ret_addr) & 0xf0000000) - // || (uintptr_t(frame->next) & 0xf0000000)) break; - // if (uintptr_t(frame) == 0 || frame->ret_addr == 0 ) break; + // if (uintptr_t(frame) <= 0xffff || (uintptr_t(frame) & 0xf0000000) + // || frame->ret_addr == 0 + // || (uintptr_t(frame->ret_addr) & 0xf0000000) + // || (uintptr_t(frame->next) & 0xf0000000)) break; + // if (uintptr_t(frame) == 0 || frame->ret_addr == 0 ) break; - // if (!sys_mem_read_allowed(&frame->next) || !sys_mem_read_allowed(&frame->ret_addr)) break; - if (!(uintptr_t(frame) >= stack_addr && uintptr_t(frame) < stack_addr + stack_size)) break; + // if (!sys_mem_read_allowed(&frame->next) || !sys_mem_read_allowed(&frame->ret_addr)) break; + if (!(uintptr_t(frame) >= stack_addr && uintptr_t(frame) < stack_addr + stack_size)) break; - // if (g_need_break) break; + // if (g_need_break) break; - stack[i] = frame->ret_addr; + stack[i] = frame->ret_addr; - frame = frame->next; - //#ifdef _MSC_VER - // } __except(EXCEPTION_EXECUTE_HANDLER) - // { - // break; - // } - //#endif - } - // printf("3\n"); + frame = frame->next; + //#ifdef _MSC_VER + // } __except(EXCEPTION_EXECUTE_HANDLER) + // { + // break; + // } + //#endif + } + // printf("3\n"); - *depth = i; + *depth = i; } #endif @@ -143,159 +156,161 @@ static void stackwalk(void* ebp, void** stack, int* depth, uintptr_t stack_addr, void sys_stack_walk(void** stack, int* depth) { #if KYTY_COMPILER == KYTY_COMPILER_MSVC - void* ebp = (size_t*)_AddressOfReturnAddress() - 1; + void* ebp = (size_t*)_AddressOfReturnAddress() - 1; #else - void* ebp = __builtin_frame_address(0); + void* ebp = __builtin_frame_address(0); #endif - // g_need_break = false; + // g_need_break = false; - //#ifndef _MSC_VER - // PVOID p = AddVectoredExceptionHandler(1000, VectoredHandlerSkip); - // if (!g_stack_addr) g_stack_addr = (uintptr_t)&depth; - ///#endif + //#ifndef _MSC_VER + // PVOID p = AddVectoredExceptionHandler(1000, VectoredHandlerSkip); + // if (!g_stack_addr) g_stack_addr = (uintptr_t)&depth; + ///#endif - if (g_stack.total_size == 0) - { - sys_stack_usage(g_stack); - } + if (g_stack.total_size == 0) + { + sys_stack_usage(g_stack); + } - stackwalk(ebp, stack, depth, g_stack.addr, g_stack.total_size); + stackwalk(ebp, stack, depth, g_stack.addr, g_stack.total_size); - //#ifndef _MSC_VER - // RemoveVectoredExceptionHandler(p); - //#endif + //#ifndef _MSC_VER + // RemoveVectoredExceptionHandler(p); + //#endif } #else //#include // struct unwind_info_t //{ -// void **stack; -// int depth; -// int max_depth; +// void **stack; +// int depth; +// int max_depth; //}; //_Unwind_Reason_Code trace_fcn(_Unwind_Context *ctx, void *d) //{ -// unwind_info_t *info = (unwind_info_t*)d; +// unwind_info_t *info = (unwind_info_t*)d; // //printf("\t#%d: program counter at %08x\n", *depth, _Unwind_GetIP(ctx)); // //(*depth)++; -// if (info->depth < info->max_depth) -// { -// void *ptr = (void*)_Unwind_GetIP(ctx); -// info->stack[info->depth] = ptr; -// info->depth++; -// } +// if (info->depth < info->max_depth) +// { +// void *ptr = (void*)_Unwind_GetIP(ctx); +// info->stack[info->depth] = ptr; +// info->depth++; +// } // return _URC_NO_REASON; //} int WalkStack(int z_stack_depth, void** z_stack_trace) { - CONTEXT context; - // KNONVOLATILE_CONTEXT_POINTERS NvContext; - PRUNTIME_FUNCTION runtime_function = nullptr; - PVOID handler_data = nullptr; - ULONG64 establisher_frame = 0; - ULONG64 image_base = 0; + CONTEXT context; + // KNONVOLATILE_CONTEXT_POINTERS NvContext; + PRUNTIME_FUNCTION runtime_function = nullptr; + PVOID handler_data = nullptr; + ULONG64 establisher_frame = 0; + ULONG64 image_base = 0; - RtlCaptureContext(&context); + RtlCaptureContext(&context); - int frame = 0; + int frame = 0; - while (true) - { - if (frame >= z_stack_depth) - { - break; - } + while (true) + { + if (frame >= z_stack_depth) + { + break; + } - z_stack_trace[frame] = reinterpret_cast(context.Rip); + z_stack_trace[frame] = reinterpret_cast(context.Rip); - frame++; + frame++; - runtime_function = RtlLookupFunctionEntry(context.Rip, &image_base, nullptr); + runtime_function = RtlLookupFunctionEntry(context.Rip, &image_base, nullptr); - if (runtime_function == nullptr) - { - break; - } + if (runtime_function == nullptr) + { + break; + } - // RtlZeroMemory(&NvContext, sizeof(KNONVOLATILE_CONTEXT_POINTERS)); - RtlVirtualUnwind(0, image_base, context.Rip, runtime_function, &context, &handler_data, &establisher_frame, nullptr /*&NvContext*/); + // RtlZeroMemory(&NvContext, sizeof(KNONVOLATILE_CONTEXT_POINTERS)); + RtlVirtualUnwind(0, image_base, context.Rip, runtime_function, &context, &handler_data, &establisher_frame, nullptr /*&NvContext*/); - if (context.Rip == 0u) - { - break; - } - } + if (context.Rip == 0u) + { + break; + } + } - return frame; + return frame; } void sys_stack_walk(void** stack, int* depth) { - // USHORT n = CaptureStackBackTrace(KYTY_FRAME_SKIP, *depth, stack, 0); - int n = WalkStack(*depth, stack); - *depth = n; - // unwind_info_t info = {stack, 0, *depth}; - // _Unwind_Backtrace(&trace_fcn, &info); - // *depth = info.depth; + // USHORT n = CaptureStackBackTrace(KYTY_FRAME_SKIP, *depth, stack, 0); + int n = WalkStack(*depth, stack); + *depth = n; + // unwind_info_t info = {stack, 0, *depth}; + // _Unwind_Backtrace(&trace_fcn, &info); + // *depth = info.depth; } #endif void sys_stack_usage_print(sys_dbg_stack_info_t& stack) { - printf("stack: (0x%" PRIx64 ", %" PRIu64 ") + (0x%" PRIx64 ", %" PRIu64 ") + (0x%" PRIx64 ", %" PRIu64 ")\n", - static_cast(stack.reserved_addr), static_cast(stack.reserved_size), static_cast(stack.guard_addr), - static_cast(stack.guard_size), static_cast(stack.commited_addr), static_cast(stack.commited_size)); + printf("stack: (0x%" PRIx64 ", %" PRIu64 ") + (0x%" PRIx64 ", %" PRIu64 ") + (0x%" PRIx64 ", %" PRIu64 ")\n", + static_cast(stack.reserved_addr), static_cast(stack.reserved_size), static_cast(stack.guard_addr), + static_cast(stack.guard_size), static_cast(stack.commited_addr), static_cast(stack.commited_size)); } void sys_stack_usage(sys_dbg_stack_info_t& s) { - MEMORY_BASIC_INFORMATION mbi {}; - [[maybe_unused]] size_t ss = VirtualQuery(&mbi, &mbi, sizeof(mbi)); - EXIT_IF(ss == 0); - PVOID reserved = mbi.AllocationBase; - ss = VirtualQuery(reserved, &mbi, sizeof(mbi)); - EXIT_IF(ss == 0); - size_t reserved_size = mbi.RegionSize; - ss = VirtualQuery(static_cast(reserved) + reserved_size, &mbi, sizeof(mbi)); - EXIT_IF(ss == 0); - void* guard_page = mbi.BaseAddress; - size_t guard_page_size = mbi.RegionSize; - ss = VirtualQuery(static_cast(guard_page) + guard_page_size, &mbi, sizeof(mbi)); - EXIT_IF(ss == 0); - void* commited = mbi.BaseAddress; - size_t commited_size = mbi.RegionSize; - s.reserved_addr = reinterpret_cast(reserved); - s.reserved_size = reserved_size; - s.guard_addr = reinterpret_cast(guard_page); - s.guard_size = guard_page_size; - s.commited_addr = reinterpret_cast(commited); - s.commited_size = commited_size; + MEMORY_BASIC_INFORMATION mbi {}; + [[maybe_unused]] size_t ss = VirtualQuery(&mbi, &mbi, sizeof(mbi)); + EXIT_IF(ss == 0); + PVOID reserved = mbi.AllocationBase; + ss = VirtualQuery(reserved, &mbi, sizeof(mbi)); + EXIT_IF(ss == 0); + size_t reserved_size = mbi.RegionSize; + ss = VirtualQuery(static_cast(reserved) + reserved_size, &mbi, sizeof(mbi)); + EXIT_IF(ss == 0); + void* guard_page = mbi.BaseAddress; + size_t guard_page_size = mbi.RegionSize; + ss = VirtualQuery(static_cast(guard_page) + guard_page_size, &mbi, sizeof(mbi)); + EXIT_IF(ss == 0); + void* commited = mbi.BaseAddress; + size_t commited_size = mbi.RegionSize; + s.reserved_addr = reinterpret_cast(reserved); + s.reserved_size = reserved_size; + s.guard_addr = reinterpret_cast(guard_page); + s.guard_size = guard_page_size; + s.commited_addr = reinterpret_cast(commited); + s.commited_size = commited_size; - s.addr = s.reserved_addr; - s.total_size = s.reserved_size + s.guard_size + s.commited_size; + s.addr = s.reserved_addr; + s.total_size = s.reserved_size + s.guard_size + s.commited_size; } void sys_get_code_info(uintptr_t* addr, size_t* size) { - MODULEINFO info {}; - GetModuleInformation(GetCurrentProcess(), GetModuleHandle(nullptr), &info, sizeof(MODULEINFO)); - *addr = reinterpret_cast(info.lpBaseOfDll); - *size = static_cast(info.SizeOfImage); + MODULEINFO info {}; + GetModuleInformation(GetCurrentProcess(), GetModuleHandle(nullptr), &info, sizeof(MODULEINFO)); + *addr = reinterpret_cast(info.lpBaseOfDll); + *size = static_cast(info.SizeOfImage); } static LONG WINAPI ExceptionFilter(PEXCEPTION_POINTERS exception) { - g_exception_filter_func(exception->ExceptionRecord->ExceptionAddress); - return EXCEPTION_EXECUTE_HANDLER; + g_exception_filter_func(exception->ExceptionRecord->ExceptionAddress); + return EXCEPTION_EXECUTE_HANDLER; } void sys_set_exception_filter(exception_filter_func_t func) { - g_exception_filter_func = func; - SetUnhandledExceptionFilter(ExceptionFilter); + g_exception_filter_func = func; + SetUnhandledExceptionFilter(ExceptionFilter); } } // namespace Kyty #endif +#endif + diff --git a/source/lib/Sys/src/SysWindowsFileIO.cpp b/source/lib/Sys/src/SysWindowsFileIO.cpp index ac5203e..45c0dc0 100644 --- a/source/lib/Sys/src/SysWindowsFileIO.cpp +++ b/source/lib/Sys/src/SysWindowsFileIO.cpp @@ -12,8 +12,13 @@ #include "Kyty/Sys/SysFileIO.h" #include "Kyty/Sys/SysTimer.h" -#include // IWYU pragma: keep - +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) + #include // IWYU pragma: keep + #ifdef _WIN64 + #include // IWYU pragma: keep + #else + #include // IWYU pragma: keep +#endif // IWYU pragma: no_include // IWYU pragma: no_include // IWYU pragma: no_include @@ -641,3 +646,4 @@ void sys_file_remove_readonly(const String& name) } // namespace Kyty #endif +#endif diff --git a/source/lib/Sys/src/SysWindowsVirtual.cpp b/source/lib/Sys/src/SysWindowsVirtual.cpp index e6de202..427cc44 100644 --- a/source/lib/Sys/src/SysWindowsVirtual.cpp +++ b/source/lib/Sys/src/SysWindowsVirtual.cpp @@ -10,8 +10,13 @@ #include "Kyty/Sys/SysVirtual.h" #include "cpuinfo.h" - -#include // IWYU pragma: keep +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) + #include // IWYU pragma: keep + #ifdef _WIN64 + #include // IWYU pragma: keep + #else + #include // IWYU pragma: keep +#endif // IWYU pragma: no_include // IWYU pragma: no_include @@ -257,3 +262,5 @@ bool sys_virtual_patch_replace(uint64_t vaddr, uint64_t value) } // namespace Kyty::Core #endif +#endif + From 5a249be0615edd74a3943a78ed0b6ef9c4cd0a33 Mon Sep 17 00:00:00 2001 From: Smiril Date: Sat, 11 May 2024 23:03:33 +0200 Subject: [PATCH 25/42] Update .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index c4fa31e..bd3e13f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ # Visual Studio 2015 cache/options directory .vs/ .idea/ +.DS_Stor* From 33705d776e29344c5f8d70e36bb36a8a014c6505 Mon Sep 17 00:00:00 2001 From: Smiril Date: Sat, 11 May 2024 23:25:51 +0200 Subject: [PATCH 26/42] Update CMakeLists.txt --- source/launcher/CMakeLists.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/source/launcher/CMakeLists.txt b/source/launcher/CMakeLists.txt index 112539c..42768f8 100644 --- a/source/launcher/CMakeLists.txt +++ b/source/launcher/CMakeLists.txt @@ -47,6 +47,10 @@ endif() add_dependencies(launcher KytyGitVersion) +<<<<<<< Updated upstream +======= + +>>>>>>> Stashed changes set(launcher_name "launcher") set_target_properties(launcher PROPERTIES OUTPUT_NAME ${launcher_name}) From 1b429de9768688127514a01f11df23315aaeec60 Mon Sep 17 00:00:00 2001 From: Smiril Date: Sat, 11 May 2024 23:42:24 +0200 Subject: [PATCH 27/42] Delete source/.DS_Store --- source/.DS_Store | Bin 6148 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 source/.DS_Store diff --git a/source/.DS_Store b/source/.DS_Store deleted file mode 100644 index 1ba28d1789d62c2cf562716f7574e192f28d9d3a..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHK%}xR_5S{`;k(hAMgkux0B;rp1<7I>R04cG+PejaAc1>`;nV(=K1SaBUK|aEwc0z&WOEz&0x$3qzjeEE><+!* zXwdTd*YtEDW$e%9o_`sI<4$?|R3<|&O2WP>h=LGPZmyyvkmHsdCBZ<|x;ns%yx1vM zCX+_3Srv_Xb6ORX(#nh$2a%uI*E)YtL-LZt(9G}_TBU=^5GAgdqCbpM}y-~Z1S*(a-jRbZ_YV7a#2 zZXqRew=R*RyH=pzp_5Tws!^5TV6I~|p{uxpt_)*>CWwx~R3omS**^kG20K^ww1k From b2afe0b182624380088e19853b8461925ca0f42e Mon Sep 17 00:00:00 2001 From: Smiril Date: Sun, 12 May 2024 10:00:46 +0200 Subject: [PATCH 28/42] Update CMakeLists.txt --- source/launcher/CMakeLists.txt | 4 ---- 1 file changed, 4 deletions(-) diff --git a/source/launcher/CMakeLists.txt b/source/launcher/CMakeLists.txt index 42768f8..112539c 100644 --- a/source/launcher/CMakeLists.txt +++ b/source/launcher/CMakeLists.txt @@ -47,10 +47,6 @@ endif() add_dependencies(launcher KytyGitVersion) -<<<<<<< Updated upstream -======= - ->>>>>>> Stashed changes set(launcher_name "launcher") set_target_properties(launcher PROPERTIES OUTPUT_NAME ${launcher_name}) From d95a6d38b1a54a412d1134a373aebc4a188a769a Mon Sep 17 00:00:00 2001 From: Smiril Date: Sun, 12 May 2024 10:29:53 +0200 Subject: [PATCH 29/42] pre work for time macos --- source/lib/Core/src/Compression.cpp | 55 +++++++++-- source/lib/Core/src/DateTime.cpp | 142 +++++++++++++++++++++++++++- 2 files changed, 183 insertions(+), 14 deletions(-) diff --git a/source/lib/Core/src/Compression.cpp b/source/lib/Core/src/Compression.cpp index 45b1895..e9adad1 100644 --- a/source/lib/Core/src/Compression.cpp +++ b/source/lib/Core/src/Compression.cpp @@ -1185,18 +1185,55 @@ bool ZipReader::GetFileStat(int file_index, ZipFileStat* o) if (mz_zip_reader_file_stat(&m_p->zip, file_index, &s) != 0) { - o->m_file_index = s.m_file_index; - - SysTimeStruct at = {0}; + o->m_file_index = s.m_file_index; +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) + //define something for Windows (32-bit and 64-bit, this part is common) + SysTimeStruct at = {0}; sys_time_t_to_system(s.m_time, at); - o->m_time = DateTime(Date(at.Year, at.Month, at.Day), Time(at.Hour, at.Minute, at.Second, at.Milliseconds)); + o->m_time = DateTime(Date(at.Year, at.Month, at.Day), Time(at.Hour, at.Minute, at.Second, at.Milliseconds)); - o->m_crc32 = s.m_crc32; - o->m_comp_size = s.m_comp_size; - o->m_uncomp_size = s.m_uncomp_size; - o->m_filename = String::FromUtf8(s.m_filename); - o->m_comment = String::FromUtf8(s.m_comment); + o->m_crc32 = s.m_crc32; + o->m_comp_size = s.m_comp_size; + o->m_uncomp_size = s.m_uncomp_size; + o->m_filename = String::FromUtf8(s.m_filename); + o->m_comment = String::FromUtf8(s.m_comment); + #ifdef _WIN64 + //define something for Windows (64-bit only) + SysTimeStruct at = {0}; + sys_time_t_to_system(s.m_time, at); + o->m_time = DateTime(Date(at.Year, at.Month, at.Day), Time(at.Hour, at.Minute, at.Second, at.Milliseconds)); + o->m_crc32 = s.m_crc32; + o->m_comp_size = s.m_comp_size; + o->m_uncomp_size = s.m_uncomp_size; + o->m_filename = String::FromUtf8(s.m_filename); + o->m_comment = String::FromUtf8(s.m_comment); + #else + //define something for Windows (32-bit only) + #endif +#elif __APPLE__ + // apple +#elif __ANDROID__ + // Below __linux__ check should be enough to handle Android, + // but something may be unique to Android. +#elif __linux__ + // linux + SysTimeStruct at = {0}; + sys_time_t_to_system(s.m_time, at); + o->m_time = DateTime(Date(at.Year, at.Month, at.Day), Time(at.Hour, at.Minute, at.Second, at.Milliseconds)); + + o->m_crc32 = s.m_crc32; + o->m_comp_size = s.m_comp_size; + o->m_uncomp_size = s.m_uncomp_size; + o->m_filename = String::FromUtf8(s.m_filename); + o->m_comment = String::FromUtf8(s.m_comment); +#elif __unix__ // all unices not caught above + // Unix +#elif defined(_POSIX_VERSION) + // POSIX +#else +# error "Unknown compiler" +#endif return true; } diff --git a/source/lib/Core/src/DateTime.cpp b/source/lib/Core/src/DateTime.cpp index 177be0d..44e3871 100644 --- a/source/lib/Core/src/DateTime.cpp +++ b/source/lib/Core/src/DateTime.cpp @@ -414,7 +414,29 @@ Date Date::FromMacros(const String& date) Date Date::FromSystem() { +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) SysTimeStruct t {}; +#ifdef _WIN64 + SysTimeStruct t {}; +#else + //define something for Windows (32-bit only) +#endif +#elif __APPLE__ + // apple + struct timeval t {}; +#elif __ANDROID__ + // Below __linux__ check should be enough to handle Android, + // but something may be unique to Android. +#elif __linux__ + // linux + SysTimeStruct t {}; +#elif __unix__ // all unices not caught above + // Unix +#elif defined(_POSIX_VERSION) + // POSIX +#else +# error "Unknown compiler" +#endif sys_get_system_time(t); return Date(t.Year, t.Month, t.Day); @@ -422,7 +444,29 @@ Date Date::FromSystem() Date Date::FromSystemUTC() { - SysTimeStruct t {}; +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) + SysTimeStruct t {}; +#ifdef _WIN64 + SysTimeStruct t {}; +#else + //define something for Windows (32-bit only) +#endif +#elif __APPLE__ + // apple + struct timeval t {}; +#elif __ANDROID__ + // Below __linux__ check should be enough to handle Android, + // but something may be unique to Android. +#elif __linux__ + // linux + SysTimeStruct t {}; +#elif __unix__ // all unices not caught above + // Unix +#elif defined(_POSIX_VERSION) + // POSIX +#else +# error "Unknown compiler" +#endif sys_get_system_time_utc(t); return Date(t.Year, t.Month, t.Day); @@ -435,7 +479,29 @@ Time::Time(int hour, int minute, int second, int msec) Time Time::FromSystem() { - SysTimeStruct t {}; +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) + SysTimeStruct t {}; +#ifdef _WIN64 + SysTimeStruct t {}; +#else + //define something for Windows (32-bit only) +#endif +#elif __APPLE__ + // apple + struct timeval t {}; +#elif __ANDROID__ + // Below __linux__ check should be enough to handle Android, + // but something may be unique to Android. +#elif __linux__ + // linux + SysTimeStruct t {}; +#elif __unix__ // all unices not caught above + // Unix +#elif defined(_POSIX_VERSION) + // POSIX +#else +# error "Unknown compiler" +#endif sys_get_system_time(t); return Time(t.Hour, t.Minute, t.Second, t.Milliseconds); @@ -443,7 +509,29 @@ Time Time::FromSystem() Time Time::FromSystemUTC() { - SysTimeStruct t {}; +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) + SysTimeStruct t {}; +#ifdef _WIN64 + SysTimeStruct t {}; +#else + //define something for Windows (32-bit only) +#endif +#elif __APPLE__ + // apple + struct timeval t {}; +#elif __ANDROID__ + // Below __linux__ check should be enough to handle Android, + // but something may be unique to Android. +#elif __linux__ + // linux + SysTimeStruct t {}; +#elif __unix__ // all unices not caught above + // Unix +#elif defined(_POSIX_VERSION) + // POSIX +#else +# error "Unknown compiler" +#endif sys_get_system_time_utc(t); return Time(t.Hour, t.Minute, t.Second, t.Milliseconds); @@ -763,7 +851,29 @@ Time Time::operator-=(int secs) DateTime DateTime::FromSystem() { - SysTimeStruct t {}; +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) + SysTimeStruct t {}; +#ifdef _WIN64 + SysTimeStruct t {}; +#else + //define something for Windows (32-bit only) +#endif +#elif __APPLE__ + // apple + struct timeval t {}; +#elif __ANDROID__ + // Below __linux__ check should be enough to handle Android, + // but something may be unique to Android. +#elif __linux__ + // linux + SysTimeStruct t {}; +#elif __unix__ // all unices not caught above + // Unix +#elif defined(_POSIX_VERSION) + // POSIX +#else +# error "Unknown compiler" +#endif sys_get_system_time(t); if (t.is_invalid) @@ -776,7 +886,29 @@ DateTime DateTime::FromSystem() DateTime DateTime::FromSystemUTC() { - SysTimeStruct t {}; +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) + SysTimeStruct t {}; +#ifdef _WIN64 + SysTimeStruct t {}; +#else + //define something for Windows (32-bit only) +#endif +#elif __APPLE__ + // apple + struct timeval t {}; +#elif __ANDROID__ + // Below __linux__ check should be enough to handle Android, + // but something may be unique to Android. +#elif __linux__ + // linux + SysTimeStruct t {}; +#elif __unix__ // all unices not caught above + // Unix +#elif defined(_POSIX_VERSION) + // POSIX +#else +# error "Unknown compiler" +#endif sys_get_system_time_utc(t); if (t.is_invalid) From 0bef00e77cbfe1bf5b905251a3f660dae4fb09ea Mon Sep 17 00:00:00 2001 From: Smiril Date: Sun, 12 May 2024 11:36:30 +0200 Subject: [PATCH 30/42] add macos support --- source/lib/Core/src/DateTime.cpp | 145 +++++++++++++++++++++++------- source/lib/Core/src/DbgAssert.cpp | 9 +- source/lib/Core/src/Debug.cpp | 37 +++++--- 3 files changed, 142 insertions(+), 49 deletions(-) diff --git a/source/lib/Core/src/DateTime.cpp b/source/lib/Core/src/DateTime.cpp index 44e3871..6f0e87f 100644 --- a/source/lib/Core/src/DateTime.cpp +++ b/source/lib/Core/src/DateTime.cpp @@ -1,3 +1,4 @@ +#include #include "Kyty/Core/DateTime.h" #include "Kyty/Core/DbgAssert.h" @@ -416,20 +417,32 @@ Date Date::FromSystem() { #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) SysTimeStruct t {}; + sys_get_system_time(t); + + return Date(t.Year, t.Month, t.Day); #ifdef _WIN64 SysTimeStruct t {}; + sys_get_system_time(t); + + return Date(t.Year, t.Month, t.Day); #else //define something for Windows (32-bit only) #endif #elif __APPLE__ // apple - struct timeval t {}; + // current date and time on the current system + time_t now = time(0); + + return Date(now); #elif __ANDROID__ // Below __linux__ check should be enough to handle Android, // but something may be unique to Android. #elif __linux__ // linux SysTimeStruct t {}; + sys_get_system_time(t); + + return Date(t.Year, t.Month, t.Day); #elif __unix__ // all unices not caught above // Unix #elif defined(_POSIX_VERSION) @@ -437,29 +450,41 @@ Date Date::FromSystem() #else # error "Unknown compiler" #endif - sys_get_system_time(t); - - return Date(t.Year, t.Month, t.Day); } Date Date::FromSystemUTC() { #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) SysTimeStruct t {}; + + sys_get_system_time_utc(t); + + return Date(t.Year, t.Month, t.Day); #ifdef _WIN64 SysTimeStruct t {}; + + sys_get_system_time_utc(t); + + return Date(t.Year, t.Month, t.Day); #else //define something for Windows (32-bit only) #endif #elif __APPLE__ // apple - struct timeval t {}; + // current date and time on the current system + time_t now = time(0); + + return Date(now); #elif __ANDROID__ // Below __linux__ check should be enough to handle Android, // but something may be unique to Android. #elif __linux__ // linux SysTimeStruct t {}; + + sys_get_system_time_utc(t); + + return Date(t.Year, t.Month, t.Day); #elif __unix__ // all unices not caught above // Unix #elif defined(_POSIX_VERSION) @@ -467,9 +492,7 @@ Date Date::FromSystemUTC() #else # error "Unknown compiler" #endif - sys_get_system_time_utc(t); - return Date(t.Year, t.Month, t.Day); } Time::Time(int hour, int minute, int second, int msec) @@ -481,20 +504,35 @@ Time Time::FromSystem() { #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) SysTimeStruct t {}; + + sys_get_system_time(t); + + return Time(t.Hour, t.Minute, t.Second, t.Milliseconds); #ifdef _WIN64 SysTimeStruct t {}; + + sys_get_system_time(t); + + return Time(t.Hour, t.Minute, t.Second, t.Milliseconds); #else //define something for Windows (32-bit only) #endif #elif __APPLE__ // apple - struct timeval t {}; + // current date and time on the current system + time_t now = time(0); + + return Time(now); #elif __ANDROID__ // Below __linux__ check should be enough to handle Android, // but something may be unique to Android. #elif __linux__ // linux SysTimeStruct t {}; + + sys_get_system_time(t); + + return Time(t.Hour, t.Minute, t.Second, t.Milliseconds); #elif __unix__ // all unices not caught above // Unix #elif defined(_POSIX_VERSION) @@ -502,29 +540,39 @@ Time Time::FromSystem() #else # error "Unknown compiler" #endif - sys_get_system_time(t); - return Time(t.Hour, t.Minute, t.Second, t.Milliseconds); } Time Time::FromSystemUTC() { #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) SysTimeStruct t {}; + sys_get_system_time_utc(t); + + return Time(t.Hour, t.Minute, t.Second, t.Milliseconds); #ifdef _WIN64 SysTimeStruct t {}; + sys_get_system_time_utc(t); + + return Time(t.Hour, t.Minute, t.Second, t.Milliseconds); #else //define something for Windows (32-bit only) #endif #elif __APPLE__ // apple - struct timeval t {}; + // current date and time on the current system + time_t now = time(0); + + return Time(now); #elif __ANDROID__ // Below __linux__ check should be enough to handle Android, // but something may be unique to Android. #elif __linux__ // linux SysTimeStruct t {}; + sys_get_system_time_utc(t); + + return Time(t.Hour, t.Minute, t.Second, t.Milliseconds); #elif __unix__ // all unices not caught above // Unix #elif defined(_POSIX_VERSION) @@ -532,9 +580,6 @@ Time Time::FromSystemUTC() #else # error "Unknown compiler" #endif - sys_get_system_time_utc(t); - - return Time(t.Hour, t.Minute, t.Second, t.Milliseconds); } void Time::Set(int hour, int minute, int second, int msec) @@ -853,20 +898,46 @@ DateTime DateTime::FromSystem() { #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) SysTimeStruct t {}; + sys_get_system_time(t); + + if (t.is_invalid) + { + return {}; + } + + return DateTime(Date(t.Year, t.Month, t.Day), Time(t.Hour, t.Minute, t.Second, t.Milliseconds)); #ifdef _WIN64 SysTimeStruct t {}; + sys_get_system_time(t); + + if (t.is_invalid) + { + return {}; + } + + return DateTime(Date(t.Year, t.Month, t.Day), Time(t.Hour, t.Minute, t.Second, t.Milliseconds)); #else //define something for Windows (32-bit only) #endif #elif __APPLE__ // apple - struct timeval t {}; + time_t now = time(0); + + return DateTime(Date(now),Time(now)); #elif __ANDROID__ // Below __linux__ check should be enough to handle Android, // but something may be unique to Android. #elif __linux__ // linux SysTimeStruct t {}; + sys_get_system_time(t); + + if (t.is_invalid) + { + return {}; + } + + return DateTime(Date(t.Year, t.Month, t.Day), Time(t.Hour, t.Minute, t.Second, t.Milliseconds)); #elif __unix__ // all unices not caught above // Unix #elif defined(_POSIX_VERSION) @@ -874,34 +945,52 @@ DateTime DateTime::FromSystem() #else # error "Unknown compiler" #endif - sys_get_system_time(t); - - if (t.is_invalid) - { - return {}; - } - - return DateTime(Date(t.Year, t.Month, t.Day), Time(t.Hour, t.Minute, t.Second, t.Milliseconds)); } DateTime DateTime::FromSystemUTC() { #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) SysTimeStruct t {}; + sys_get_system_time_utc(t); + + if (t.is_invalid) + { + return {}; + } + + return DateTime(Date(t.Year, t.Month, t.Day), Time(t.Hour, t.Minute, t.Second, t.Milliseconds)); #ifdef _WIN64 SysTimeStruct t {}; + sys_get_system_time_utc(t); + + if (t.is_invalid) + { + return {}; + } + + return DateTime(Date(t.Year, t.Month, t.Day), Time(t.Hour, t.Minute, t.Second, t.Milliseconds)); #else //define something for Windows (32-bit only) #endif #elif __APPLE__ // apple - struct timeval t {}; + time_t now = time(0); + + return DateTime(Date(now),Time(now)); #elif __ANDROID__ // Below __linux__ check should be enough to handle Android, // but something may be unique to Android. #elif __linux__ // linux SysTimeStruct t {}; + sys_get_system_time_utc(t); + + if (t.is_invalid) + { + return {}; + } + + return DateTime(Date(t.Year, t.Month, t.Day), Time(t.Hour, t.Minute, t.Second, t.Milliseconds)); #elif __unix__ // all unices not caught above // Unix #elif defined(_POSIX_VERSION) @@ -909,14 +998,6 @@ DateTime DateTime::FromSystemUTC() #else # error "Unknown compiler" #endif - sys_get_system_time_utc(t); - - if (t.is_invalid) - { - return {}; - } - - return DateTime(Date(t.Year, t.Month, t.Day), Time(t.Hour, t.Minute, t.Second, t.Milliseconds)); } /** diff --git a/source/lib/Core/src/DbgAssert.cpp b/source/lib/Core/src/DbgAssert.cpp index 5af90d1..4a337ed 100644 --- a/source/lib/Core/src/DbgAssert.cpp +++ b/source/lib/Core/src/DbgAssert.cpp @@ -5,11 +5,12 @@ #include "Kyty/Core/Subsystems.h" #include - +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) #if KYTY_PLATFORM == KYTY_PLATFORM_WINDOWS #include // IWYU pragma: keep // IWYU pragma: no_include #endif +#endif namespace Kyty::Core { @@ -18,7 +19,7 @@ constexpr int PRINT_STACK_FROM = 4; #else constexpr int PRINT_STACK_FROM = 2; #endif - +#if defined(__linux__) #if KYTY_PLATFORM == KYTY_PLATFORM_LINUX int IsDebuggerPresent() { @@ -48,7 +49,7 @@ int IsDebuggerPresent() return (dbg ? 1 : 0); } #endif - +#endif void dbg_print_stack() { DebugStack s; @@ -110,8 +111,10 @@ void dbg_exit(int status) bool dbg_is_debugger_present() { +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) || defined(__linux__) #if KYTY_PLATFORM == KYTY_PLATFORM_WINDOWS || KYTY_PLATFORM == KYTY_PLATFORM_LINUX return !(IsDebuggerPresent() == 0); +#endif #endif return false; } diff --git a/source/lib/Core/src/Debug.cpp b/source/lib/Core/src/Debug.cpp index 635f9af..81f71c1 100644 --- a/source/lib/Core/src/Debug.cpp +++ b/source/lib/Core/src/Debug.cpp @@ -87,8 +87,9 @@ static void exception_filter(void* /*addr*/) void core_debug_init(const char* app_name) { +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) sys_set_exception_filter(exception_filter); - +#endif g_exe_name = new String(String::FromUtf8(app_name)); g_dbg_map = new DebugMap; // g_dbg_map->LoadMap(); @@ -792,6 +793,7 @@ static const T* find_info(const T* data, uintptr_t addr, uint32_t low, uint32_t void DebugMapPrivate::FixBaseAddress() { +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) uintptr_t code_base = 0; size_t code_size = 0; sys_get_code_info(&code_base, &code_size); @@ -811,6 +813,7 @@ void DebugMapPrivate::FixBaseAddress() p.addr += code_base; } } +#endif } const DebugFunctionInfo* DebugMapPrivate::FindFunc(DebugMap* map, uintptr_t addr) @@ -864,31 +867,35 @@ void DebugMap::LoadCsv(const String& name) m_p->buf[size] = 0; char* context = nullptr; - [[maybe_unused]] char* p = strtok_s(m_p->buf, "\r\n;", &context); + [[maybe_unused]] char* p = strtok_r(m_p->buf, "\r\n;", &context); EXIT_IF(String::FromUtf8(p) != U"Addr"); - p = strtok_s(nullptr, "\r\n;", &context); + p = strtok_r(nullptr, "\r\n;", &context); EXIT_IF(String::FromUtf8(p) != U"Size"); - p = strtok_s(nullptr, "\r\n;", &context); + p = strtok_r(nullptr, "\r\n;", &context); EXIT_IF(String::FromUtf8(p) != U"Func"); - p = strtok_s(nullptr, "\r\n;", &context); + p = strtok_r(nullptr, "\r\n;", &context); EXIT_IF(String::FromUtf8(p) != U"Obj"); for (;;) { - char* s1 = strtok_s(nullptr, "\r\n;", &context); - char* s2 = strtok_s(nullptr, "\r\n;", &context); - char* s3 = strtok_s(nullptr, "\r\n;", &context); - char* s4 = strtok_s(nullptr, "\r\n;", &context); + char* s1 = strtok_r(nullptr, "\r\n;", &context); + char* s2 = strtok_r(nullptr, "\r\n;", &context); + char* s3 = strtok_r(nullptr, "\r\n;", &context); + char* s4 = strtok_r(nullptr, "\r\n;", &context); if ((s1 == nullptr) || (s2 == nullptr) || (s3 == nullptr) || (s4 == nullptr)) { break; } - +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) || defined(__linux__) DebugFunctionInfo2 inf = {static_cast(sys_strtoui64(s1 + 2, nullptr, 16)), - static_cast(sys_strtoui64(s2, nullptr, 10)), s3, s4}; + static_cast(sys_strtoui64(s2, nullptr, 10)), s3, s4}; + + m_p->data2.Add(inf); +#elif defined(APPLE) - m_p->data2.Add(inf); +#endif + } } @@ -913,8 +920,10 @@ void DebugStack::Print(int from, bool with_name) const void DebugStack::Trace(DebugStack* stack) { - stack->depth = DEBUG_MAX_STACK_DEPTH; - sys_stack_walk(stack->stack, &stack->depth); +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) || defined(__linux__) + stack->depth = DEBUG_MAX_STACK_DEPTH; + sys_stack_walk(stack->stack, &stack->depth); +#endif } void DebugStack::PrintAndroid(int from, bool with_name) const From b295f8a683cfc7abe317f8b9be9389a234caee80 Mon Sep 17 00:00:00 2001 From: Smiril Date: Sun, 12 May 2024 12:15:34 +0200 Subject: [PATCH 31/42] Update File.cpp --- source/lib/Core/src/File.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/lib/Core/src/File.cpp b/source/lib/Core/src/File.cpp index 664072a..48c6c28 100644 --- a/source/lib/Core/src/File.cpp +++ b/source/lib/Core/src/File.cpp @@ -37,7 +37,7 @@ namespace Kyty::Core { struct File::FilePrivate { Encoding e; - sys_file_t* f; + FilePrivate* f; }; String* g_assets_dir = nullptr; From a982605fb80cc14d7a6ea2fb39a8fbcbe73fb2b1 Mon Sep 17 00:00:00 2001 From: Smiril Date: Sun, 12 May 2024 12:24:04 +0200 Subject: [PATCH 32/42] typo fix --- source/include/Kyty/Sys/MacOS/SysMacOSFileIO.h | 2 +- source/lib/Core/src/Debug.cpp | 2 +- source/lib/Core/src/File.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/source/include/Kyty/Sys/MacOS/SysMacOSFileIO.h b/source/include/Kyty/Sys/MacOS/SysMacOSFileIO.h index 6745e5b..848ca82 100644 --- a/source/include/Kyty/Sys/MacOS/SysMacOSFileIO.h +++ b/source/include/Kyty/Sys/MacOS/SysMacOSFileIO.h @@ -4,7 +4,7 @@ // IWYU pragma: private #if KYTY_PLATFORM != KYTY_PLATFORM_MACOS -//#error "KYTY_PLATFORM != KYTY_PLATFORM_LINUX" +//#error "KYTY_PLATFORM != KYTY_PLATFORM_MACOS" #else #include "Kyty/Core/String.h" diff --git a/source/lib/Core/src/Debug.cpp b/source/lib/Core/src/Debug.cpp index 81f71c1..edc9a08 100644 --- a/source/lib/Core/src/Debug.cpp +++ b/source/lib/Core/src/Debug.cpp @@ -892,7 +892,7 @@ void DebugMap::LoadCsv(const String& name) static_cast(sys_strtoui64(s2, nullptr, 10)), s3, s4}; m_p->data2.Add(inf); -#elif defined(APPLE) +#elif __APPLE__ #endif diff --git a/source/lib/Core/src/File.cpp b/source/lib/Core/src/File.cpp index 48c6c28..14c50fd 100644 --- a/source/lib/Core/src/File.cpp +++ b/source/lib/Core/src/File.cpp @@ -37,7 +37,7 @@ namespace Kyty::Core { struct File::FilePrivate { Encoding e; - FilePrivate* f; + sys_file_t* f; }; String* g_assets_dir = nullptr; From ae49898b35ae8333bce8f95711766e1b328f6f09 Mon Sep 17 00:00:00 2001 From: Smiril Date: Sun, 12 May 2024 13:43:03 +0200 Subject: [PATCH 33/42] typo fix --- .../3rdparty/easy_profiler/src/profiler.cpp | 42 +++---------------- source/3rdparty/vulkan/source/print.cpp | 2 + source/launcher/src/MainDialog.cpp | 8 ++-- 3 files changed, 12 insertions(+), 40 deletions(-) diff --git a/source/3rdparty/easy_profiler/src/profiler.cpp b/source/3rdparty/easy_profiler/src/profiler.cpp index cb13be2..99364da 100644 --- a/source/3rdparty/easy_profiler/src/profiler.cpp +++ b/source/3rdparty/easy_profiler/src/profiler.cpp @@ -62,22 +62,15 @@ # endif #endif -#if !defined(EASY_PROFILER_VERSION_MAJOR) || !defined(EASY_PROFILER_VERSION_MINOR) || !defined(EASY_PROFILER_VERSION_PATCH) -# ifdef _WIN32 -# error EASY_PROFILER_VERSION_MAJOR and EASY_PROFILER_VERSION_MINOR and EASY_PROFILER_VERSION_PATCH macros must be defined -# else -# error "EASY_PROFILER_VERSION_MAJOR and EASY_PROFILER_VERSION_MINOR and EASY_PROFILER_VERSION_PATCH macros must be defined" -# endif -#endif -#define EASY_PROFILER_PRODUCT_VERSION "v" EASY_STRINGIFICATION(EASY_PROFILER_VERSION_MAJOR) "." \ - EASY_STRINGIFICATION(EASY_PROFILER_VERSION_MINOR) "." \ - EASY_STRINGIFICATION(EASY_PROFILER_VERSION_PATCH) +#define EASY_PROFILER_PRODUCT_VERSION "v" EASY_STRINGIFICATION(EASY_PROFILER_VERSION) "." \ + EASY_STRINGIFICATION(EASY_PROFILER_VERSION) "." \ + EASY_STRINGIFICATION(EASY_PROFILER_VERSION) extern const uint32_t EASY_PROFILER_SIGNATURE = ('E' << 24) | ('a' << 16) | ('s' << 8) | 'y'; -extern const uint32_t EASY_PROFILER_VERSION = (static_cast(EASY_PROFILER_VERSION_MAJOR) << 24) | - (static_cast(EASY_PROFILER_VERSION_MINOR) << 16) | - static_cast(EASY_PROFILER_VERSION_PATCH); +extern const uint32_t EASY_PROFILER_VERSION = (static_cast(EASY_PROFILER_VERSION) << 24) | + (static_cast(EASY_PROFILER_VERSION) << 16) | + static_cast(EASY_PROFILER_VERSION); #undef EASY_VERSION_INT @@ -85,29 +78,6 @@ extern const uint32_t EASY_PROFILER_VERSION = (static_cast(EASY_PROFIL extern "C" { -PROFILER_API uint8_t versionMajor() -{ - static_assert(0 <= EASY_PROFILER_VERSION_MAJOR && EASY_PROFILER_VERSION_MAJOR <= 255, - "EASY_PROFILER_VERSION_MAJOR must be defined in range [0, 255]"); - - return EASY_PROFILER_VERSION_MAJOR; -} - -PROFILER_API uint8_t versionMinor() -{ - static_assert(0 <= EASY_PROFILER_VERSION_MINOR && EASY_PROFILER_VERSION_MINOR <= 255, - "EASY_PROFILER_VERSION_MINOR must be defined in range [0, 255]"); - - return EASY_PROFILER_VERSION_MINOR; -} - -PROFILER_API uint16_t versionPatch() -{ - static_assert(0 <= EASY_PROFILER_VERSION_PATCH && EASY_PROFILER_VERSION_PATCH <= 65535, - "EASY_PROFILER_VERSION_PATCH must be defined in range [0, 65535]"); - - return EASY_PROFILER_VERSION_PATCH; -} PROFILER_API uint32_t version() { diff --git a/source/3rdparty/vulkan/source/print.cpp b/source/3rdparty/vulkan/source/print.cpp index 2418c5b..ac9b784 100644 --- a/source/3rdparty/vulkan/source/print.cpp +++ b/source/3rdparty/vulkan/source/print.cpp @@ -33,6 +33,7 @@ clr::blue::operator const char*() { return "\x1b[34m"; } } // namespace spvtools #elif defined(SPIRV_WINDOWS) +#if defined(WIN32) #include namespace spvtools { @@ -107,6 +108,7 @@ clr::blue::operator const char*() { } } // namespace spvtools +#endif #else namespace spvtools { diff --git a/source/launcher/src/MainDialog.cpp b/source/launcher/src/MainDialog.cpp index e307a05..0750dbc 100644 --- a/source/launcher/src/MainDialog.cpp +++ b/source/launcher/src/MainDialog.cpp @@ -27,7 +27,7 @@ #include "ui_main_dialog.h" -#ifndef __linux__ +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) #include // IWYU pragma: keep #endif @@ -37,7 +37,7 @@ class QWidget; -#ifdef __linux__ +#ifdef __linux__ || __APPLE__ constexpr char SCRIPT_EXE[] = "fc_script"; #else constexpr char SCRIPT_EXE[] = "fc_script.exe"; @@ -59,7 +59,7 @@ constexpr char KYTY_LOAD_SYMBOLS_ALL[] = "kyty_load_symbols_all"; constexpr char KYTY_LOAD_PARAM_SFO[] = "kyty_load_param_sfo"; constexpr char KYTY_INIT[] = "kyty_init"; constexpr char KYTY_LUA_FILE[] = "kyty_run.lua"; -#ifndef __linux__ +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) constexpr DWORD CMD_X_CHARS = 175; constexpr DWORD CMD_Y_CHARS = 1000; #endif @@ -359,7 +359,7 @@ void MainDialog::RunInterpreter(QProcess* process, Kyty::Configuration* info, [[ } #endif process->setWorkingDirectory(dir.path()); -#ifndef __linux__ +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) process->setCreateProcessArgumentsModifier( [](QProcess::CreateProcessArguments* args) { From 98413fb57abcb9849727a3768319b620ff748cd2 Mon Sep 17 00:00:00 2001 From: Smiril Date: Sun, 12 May 2024 14:25:29 +0200 Subject: [PATCH 34/42] compiler fix --- source/include/Kyty/Sys/MacOS/SysMacOSTimer.h | 4 +- source/include/Kyty/Sys/SysTimer.h | 4 +- .../Kyty/Sys/Windows/SysWindowsFileIO.h | 8 +- source/lib/Core/src/DateTime.cpp | 210 ------------------ source/lib/Core/src/File.cpp | 2 + source/lib/Core/src/Threads.cpp | 2 + source/lib/Core/src/VirtualMemory.cpp | 2 +- source/lib/Sys/src/SysLinuxDbg.cpp | 4 +- source/lib/Sys/src/SysLinuxFileIO.cpp | 4 +- source/lib/Sys/src/SysLinuxVirtual.cpp | 4 +- source/lib/Sys/src/SysMacOSDbg.cpp | 14 +- source/lib/Sys/src/SysMacOSFileIO.cpp | 4 +- source/lib/Sys/src/SysMacOSVirtual.cpp | 4 +- source/lib/Sys/src/SysWindowsDbg.cpp | 21 +- source/lib/Sys/src/SysWindowsFileIO.cpp | 14 +- source/lib/Sys/src/SysWindowsVirtual.cpp | 14 +- 16 files changed, 29 insertions(+), 286 deletions(-) diff --git a/source/include/Kyty/Sys/MacOS/SysMacOSTimer.h b/source/include/Kyty/Sys/MacOS/SysMacOSTimer.h index b2e182a..2404c94 100644 --- a/source/include/Kyty/Sys/MacOS/SysMacOSTimer.h +++ b/source/include/Kyty/Sys/MacOS/SysMacOSTimer.h @@ -3,9 +3,7 @@ // IWYU pragma: private -#if KYTY_PLATFORM != KYTY_PLATFORM_MACOS -//#error "KYTY_PLATFORM != KYTY_PLATFORM_MACOS" -#else +#if __APPLE__ #include diff --git a/source/include/Kyty/Sys/SysTimer.h b/source/include/Kyty/Sys/SysTimer.h index 7d3730a..d9776de 100644 --- a/source/include/Kyty/Sys/SysTimer.h +++ b/source/include/Kyty/Sys/SysTimer.h @@ -4,9 +4,9 @@ #include "Kyty/Core/Common.h" #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) #include "Kyty/Sys/Windows/SysWindowsTimer.h" // IWYU pragma: export -#elif __linux__ +#elif defined(__linux__) #include "Kyty/Sys/Linux/SysLinuxTimer.h" // IWYU pragma: export -#elif __APPLE__ +#elif defined(__APPLE__) #include "Kyty/Sys/MacOS/SysMacOSTimer.h" // IWYU pragma: export #else # error "Unknown compiler" diff --git a/source/include/Kyty/Sys/Windows/SysWindowsFileIO.h b/source/include/Kyty/Sys/Windows/SysWindowsFileIO.h index 4c5dbb1..4c9e3be 100644 --- a/source/include/Kyty/Sys/Windows/SysWindowsFileIO.h +++ b/source/include/Kyty/Sys/Windows/SysWindowsFileIO.h @@ -3,14 +3,12 @@ // IWYU pragma: private -#if KYTY_PLATFORM != KYTY_PLATFORM_WINDOWS -//#error "KYTY_PLATFORM != KYTY_PLATFORM_WINDOWS" -#else +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) #include "Kyty/Core/Common.h" #include "Kyty/Core/String.h" #include "Kyty/Sys/SysTimer.h" -#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) + namespace Kyty { using KYTY_HANDLE = void*; @@ -120,6 +118,4 @@ void sys_file_remove_readonly(const String& name); #endif -#endif - #endif /* SYS_WIN32_INCLUDE_KYTY_SYSFILEIO_H_ */ diff --git a/source/lib/Core/src/DateTime.cpp b/source/lib/Core/src/DateTime.cpp index 6f0e87f..b1939c3 100644 --- a/source/lib/Core/src/DateTime.cpp +++ b/source/lib/Core/src/DateTime.cpp @@ -415,84 +415,19 @@ Date Date::FromMacros(const String& date) Date Date::FromSystem() { -#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) - SysTimeStruct t {}; - sys_get_system_time(t); - - return Date(t.Year, t.Month, t.Day); -#ifdef _WIN64 SysTimeStruct t {}; sys_get_system_time(t); return Date(t.Year, t.Month, t.Day); -#else - //define something for Windows (32-bit only) -#endif -#elif __APPLE__ - // apple - // current date and time on the current system - time_t now = time(0); - - return Date(now); -#elif __ANDROID__ - // Below __linux__ check should be enough to handle Android, - // but something may be unique to Android. -#elif __linux__ - // linux - SysTimeStruct t {}; - sys_get_system_time(t); - - return Date(t.Year, t.Month, t.Day); -#elif __unix__ // all unices not caught above - // Unix -#elif defined(_POSIX_VERSION) - // POSIX -#else -# error "Unknown compiler" -#endif } Date Date::FromSystemUTC() { -#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) SysTimeStruct t {}; sys_get_system_time_utc(t); return Date(t.Year, t.Month, t.Day); -#ifdef _WIN64 - SysTimeStruct t {}; - - sys_get_system_time_utc(t); - - return Date(t.Year, t.Month, t.Day); -#else - //define something for Windows (32-bit only) -#endif -#elif __APPLE__ - // apple - // current date and time on the current system - time_t now = time(0); - - return Date(now); -#elif __ANDROID__ - // Below __linux__ check should be enough to handle Android, - // but something may be unique to Android. -#elif __linux__ - // linux - SysTimeStruct t {}; - - sys_get_system_time_utc(t); - - return Date(t.Year, t.Month, t.Day); -#elif __unix__ // all unices not caught above - // Unix -#elif defined(_POSIX_VERSION) - // POSIX -#else -# error "Unknown compiler" -#endif - } Time::Time(int hour, int minute, int second, int msec) @@ -502,84 +437,19 @@ Time::Time(int hour, int minute, int second, int msec) Time Time::FromSystem() { -#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) SysTimeStruct t {}; sys_get_system_time(t); return Time(t.Hour, t.Minute, t.Second, t.Milliseconds); -#ifdef _WIN64 - SysTimeStruct t {}; - - sys_get_system_time(t); - - return Time(t.Hour, t.Minute, t.Second, t.Milliseconds); -#else - //define something for Windows (32-bit only) -#endif -#elif __APPLE__ - // apple - // current date and time on the current system - time_t now = time(0); - - return Time(now); -#elif __ANDROID__ - // Below __linux__ check should be enough to handle Android, - // but something may be unique to Android. -#elif __linux__ - // linux - SysTimeStruct t {}; - - sys_get_system_time(t); - - return Time(t.Hour, t.Minute, t.Second, t.Milliseconds); -#elif __unix__ // all unices not caught above - // Unix -#elif defined(_POSIX_VERSION) - // POSIX -#else -# error "Unknown compiler" -#endif - } Time Time::FromSystemUTC() { -#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) SysTimeStruct t {}; sys_get_system_time_utc(t); return Time(t.Hour, t.Minute, t.Second, t.Milliseconds); -#ifdef _WIN64 - SysTimeStruct t {}; - sys_get_system_time_utc(t); - - return Time(t.Hour, t.Minute, t.Second, t.Milliseconds); -#else - //define something for Windows (32-bit only) -#endif -#elif __APPLE__ - // apple - // current date and time on the current system - time_t now = time(0); - - return Time(now); -#elif __ANDROID__ - // Below __linux__ check should be enough to handle Android, - // but something may be unique to Android. -#elif __linux__ - // linux - SysTimeStruct t {}; - sys_get_system_time_utc(t); - - return Time(t.Hour, t.Minute, t.Second, t.Milliseconds); -#elif __unix__ // all unices not caught above - // Unix -#elif defined(_POSIX_VERSION) - // POSIX -#else -# error "Unknown compiler" -#endif } void Time::Set(int hour, int minute, int second, int msec) @@ -896,7 +766,6 @@ Time Time::operator-=(int secs) DateTime DateTime::FromSystem() { -#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) SysTimeStruct t {}; sys_get_system_time(t); @@ -906,50 +775,10 @@ DateTime DateTime::FromSystem() } return DateTime(Date(t.Year, t.Month, t.Day), Time(t.Hour, t.Minute, t.Second, t.Milliseconds)); -#ifdef _WIN64 - SysTimeStruct t {}; - sys_get_system_time(t); - - if (t.is_invalid) - { - return {}; - } - - return DateTime(Date(t.Year, t.Month, t.Day), Time(t.Hour, t.Minute, t.Second, t.Milliseconds)); -#else - //define something for Windows (32-bit only) -#endif -#elif __APPLE__ - // apple - time_t now = time(0); - - return DateTime(Date(now),Time(now)); -#elif __ANDROID__ - // Below __linux__ check should be enough to handle Android, - // but something may be unique to Android. -#elif __linux__ - // linux - SysTimeStruct t {}; - sys_get_system_time(t); - - if (t.is_invalid) - { - return {}; - } - - return DateTime(Date(t.Year, t.Month, t.Day), Time(t.Hour, t.Minute, t.Second, t.Milliseconds)); -#elif __unix__ // all unices not caught above - // Unix -#elif defined(_POSIX_VERSION) - // POSIX -#else -# error "Unknown compiler" -#endif } DateTime DateTime::FromSystemUTC() { -#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) SysTimeStruct t {}; sys_get_system_time_utc(t); @@ -959,45 +788,6 @@ DateTime DateTime::FromSystemUTC() } return DateTime(Date(t.Year, t.Month, t.Day), Time(t.Hour, t.Minute, t.Second, t.Milliseconds)); -#ifdef _WIN64 - SysTimeStruct t {}; - sys_get_system_time_utc(t); - - if (t.is_invalid) - { - return {}; - } - - return DateTime(Date(t.Year, t.Month, t.Day), Time(t.Hour, t.Minute, t.Second, t.Milliseconds)); -#else - //define something for Windows (32-bit only) -#endif -#elif __APPLE__ - // apple - time_t now = time(0); - - return DateTime(Date(now),Time(now)); -#elif __ANDROID__ - // Below __linux__ check should be enough to handle Android, - // but something may be unique to Android. -#elif __linux__ - // linux - SysTimeStruct t {}; - sys_get_system_time_utc(t); - - if (t.is_invalid) - { - return {}; - } - - return DateTime(Date(t.Year, t.Month, t.Day), Time(t.Hour, t.Minute, t.Second, t.Milliseconds)); -#elif __unix__ // all unices not caught above - // Unix -#elif defined(_POSIX_VERSION) - // POSIX -#else -# error "Unknown compiler" -#endif } /** diff --git a/source/lib/Core/src/File.cpp b/source/lib/Core/src/File.cpp index 14c50fd..69c9c86 100644 --- a/source/lib/Core/src/File.cpp +++ b/source/lib/Core/src/File.cpp @@ -927,6 +927,7 @@ void File::Write(const ByteBuffer& buf, uint32_t* bytes_written) DateTime File::GetLastAccessTimeUTC(const String& name) { +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) SysTimeStruct t {}; sys_file_to_system_time_utc(sys_file_get_last_access_time_utc(name), t); @@ -940,6 +941,7 @@ DateTime File::GetLastAccessTimeUTC(const String& name) DateTime File::GetLastWriteTimeUTC(const String& name) { +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) SysTimeStruct t {}; sys_file_to_system_time_utc(sys_file_get_last_write_time_utc(name), t); diff --git a/source/lib/Core/src/Threads.cpp b/source/lib/Core/src/Threads.cpp index b7c29a6..f120572 100644 --- a/source/lib/Core/src/Threads.cpp +++ b/source/lib/Core/src/Threads.cpp @@ -42,6 +42,7 @@ #endif #if !(defined(KYTY_DEBUG_LOCKS) || defined(KYTY_DEBUG_LOCKS_TIMED)) && defined(KYTY_WIN_CS) +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) #include // IWYU pragma: keep // IWYU pragma: no_include constexpr DWORD KYTY_CS_SPIN_COUNT = 4000; @@ -92,6 +93,7 @@ static SleepConditionVariableCS_func_t ResolveSleepConditionVariableCS() return nullptr; } +#endif #endif namespace Kyty::Core { diff --git a/source/lib/Core/src/VirtualMemory.cpp b/source/lib/Core/src/VirtualMemory.cpp index eb61605..5926fda 100644 --- a/source/lib/Core/src/VirtualMemory.cpp +++ b/source/lib/Core/src/VirtualMemory.cpp @@ -6,7 +6,7 @@ #define KYTY_HAS_EXCEPTIONS #endif -#ifdef KYTY_HAS_EXCEPTIONS +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) #include // IWYU pragma: keep #endif diff --git a/source/lib/Sys/src/SysLinuxDbg.cpp b/source/lib/Sys/src/SysLinuxDbg.cpp index cd2788c..ab17f2b 100644 --- a/source/lib/Sys/src/SysLinuxDbg.cpp +++ b/source/lib/Sys/src/SysLinuxDbg.cpp @@ -1,8 +1,6 @@ #include "Kyty/Core/Common.h" -#if KYTY_PLATFORM != KYTY_PLATFORM_LINUX -//#error "KYTY_PLATFORM != KYTY_PLATFORM_LINUX" -#else +#if defined(__linux__) #include "Kyty/Sys/SysDbg.h" diff --git a/source/lib/Sys/src/SysLinuxFileIO.cpp b/source/lib/Sys/src/SysLinuxFileIO.cpp index ab8ba1a..969ffcd 100644 --- a/source/lib/Sys/src/SysLinuxFileIO.cpp +++ b/source/lib/Sys/src/SysLinuxFileIO.cpp @@ -1,8 +1,6 @@ #include "Kyty/Core/Common.h" -#if KYTY_PLATFORM != KYTY_PLATFORM_LINUX -//#error "KYTY_PLATFORM != KYTY_PLATFORM_LINUX" -#else +#if defined(__linux__) #include "Kyty/Core/DbgAssert.h" #include "Kyty/Core/MemoryAlloc.h" diff --git a/source/lib/Sys/src/SysLinuxVirtual.cpp b/source/lib/Sys/src/SysLinuxVirtual.cpp index 18c9401..456b2f5 100644 --- a/source/lib/Sys/src/SysLinuxVirtual.cpp +++ b/source/lib/Sys/src/SysLinuxVirtual.cpp @@ -1,8 +1,6 @@ #include "Kyty/Core/Common.h" -#if KYTY_PLATFORM != KYTY_PLATFORM_LINUX -//#error "KYTY_PLATFORM != KYTY_PLATFORM_LINUX" -#else +#if defined(__linux__) #include "Kyty/Core/DbgAssert.h" #include "Kyty/Core/String.h" diff --git a/source/lib/Sys/src/SysMacOSDbg.cpp b/source/lib/Sys/src/SysMacOSDbg.cpp index b44f5a5..62cd982 100644 --- a/source/lib/Sys/src/SysMacOSDbg.cpp +++ b/source/lib/Sys/src/SysMacOSDbg.cpp @@ -1,8 +1,6 @@ #include "Kyty/Core/Common.h" -#if KYTY_PLATFORM != KYTY_PLATFORM_MACOS -//#error "KYTY_PLATFORM != KYTY_PLATFORM_MACOS" -#else +#if defined(__APPLE__) #include "Kyty/Sys/SysDbg.h" @@ -14,20 +12,20 @@ namespace Kyty { -static thread_local sys_dbg_stack_info_t g_stack = {0}; +static thread_local dbg_stack_info_t g_stack = {0}; void sys_stack_walk(void** /*stack*/, int* depth) { *depth = 0; } -void sys_stack_usage_print(sys_dbg_stack_info_t& stack) +void sys_stack_usage_print(dbg_stack_info_t& stack) { printf("stack: (0x%" PRIx64 ", %" PRIu64 ")\n", static_cast(stack.commited_addr), static_cast(stack.commited_size)); printf("code: (0x%" PRIx64 ", %" PRIu64 ")\n", static_cast(stack.code_addr), static_cast(stack.code_size)); } -void sys_stack_usage(sys_dbg_stack_info_t& s) +void sys_stack_usage(dbg_stack_info_t& s) { pid_t pid = getpid(); @@ -49,7 +47,7 @@ void sys_stack_usage(sys_dbg_stack_info_t& s) result = sprintf(str, "/proc/%d/maps", static_cast(pid)); - memset(&s, 0, sizeof(sys_dbg_stack_info_t)); + memset(&s, 0, sizeof(dbg_stack_info_t)); FILE* f = fopen(str, "r"); @@ -146,7 +144,7 @@ void sys_get_code_info(uintptr_t* addr, size_t* size) *size = g_stack.code_size; } -void sys_set_exception_filter(exception_filter_func_t /*func*/) {} +void sys_set_exception_filter(exception_filter_func_t) {} } // namespace Kyty #endif diff --git a/source/lib/Sys/src/SysMacOSFileIO.cpp b/source/lib/Sys/src/SysMacOSFileIO.cpp index 56ab07d..7e2fd82 100644 --- a/source/lib/Sys/src/SysMacOSFileIO.cpp +++ b/source/lib/Sys/src/SysMacOSFileIO.cpp @@ -1,8 +1,6 @@ #include "Kyty/Core/Common.h" -#if KYTY_PLATFORM != KYTY_PLATFORM_MACOS -//#error "KYTY_PLATFORM != KYTY_PLATFORM_MACOS" -#else +#if defined(__APPLE__) #include "Kyty/Core/DbgAssert.h" #include "Kyty/Core/MemoryAlloc.h" diff --git a/source/lib/Sys/src/SysMacOSVirtual.cpp b/source/lib/Sys/src/SysMacOSVirtual.cpp index 96c82c8..a3c4541 100644 --- a/source/lib/Sys/src/SysMacOSVirtual.cpp +++ b/source/lib/Sys/src/SysMacOSVirtual.cpp @@ -1,8 +1,6 @@ #include "Kyty/Core/Common.h" -#if KYTY_PLATFORM != KYTY_PLATFORM_MACOS -//#error "KYTY_PLATFORM != KYTY_PLATFORM_MACOS" -#else +#if defined(__APPLE__) #include "Kyty/Core/DbgAssert.h" #include "Kyty/Core/String.h" diff --git a/source/lib/Sys/src/SysWindowsDbg.cpp b/source/lib/Sys/src/SysWindowsDbg.cpp index 83bc7c8..8c3a6ae 100644 --- a/source/lib/Sys/src/SysWindowsDbg.cpp +++ b/source/lib/Sys/src/SysWindowsDbg.cpp @@ -10,26 +10,13 @@ // IWYU pragma: no_include // IWYU pragma: no_include -#if KYTY_PLATFORM != KYTY_PLATFORM_WINDOWS -//#error "KYTY_PLATFORM != KYTY_PLATFORM_WINDOWS" -#else +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) #include "Kyty/Core/DbgAssert.h" #include "Kyty/Sys/SysDbg.h" -#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) - #include // IWYU pragma: keep - #include // IWYU pragma: keep - #ifdef _WIN64 - #include // IWYU pragma: keep - #include // IWYU pragma: keep - #else - #include // IWYU pragma: keep - #include // IWYU pragma: keep -#endif -#endif - -#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) +#include // IWYU pragma: keep +#include // IWYU pragma: keep #if KYTY_COMPILER == KYTY_COMPILER_MSVC @@ -312,5 +299,3 @@ void sys_set_exception_filter(exception_filter_func_t func) } // namespace Kyty #endif -#endif - diff --git a/source/lib/Sys/src/SysWindowsFileIO.cpp b/source/lib/Sys/src/SysWindowsFileIO.cpp index 45c0dc0..e7b6b6e 100644 --- a/source/lib/Sys/src/SysWindowsFileIO.cpp +++ b/source/lib/Sys/src/SysWindowsFileIO.cpp @@ -2,9 +2,7 @@ #include "Kyty/Core/Common.h" -#if KYTY_PLATFORM != KYTY_PLATFORM_WINDOWS -//#error "KYTY_PLATFORM != KYTY_PLATFORM_WINDOWS" -#else +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) #include "Kyty/Core/MemoryAlloc.h" #include "Kyty/Core/String.h" @@ -12,13 +10,8 @@ #include "Kyty/Sys/SysFileIO.h" #include "Kyty/Sys/SysTimer.h" -#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) - #include // IWYU pragma: keep - #ifdef _WIN64 - #include // IWYU pragma: keep - #else - #include // IWYU pragma: keep -#endif +#include // IWYU pragma: keep + // IWYU pragma: no_include // IWYU pragma: no_include // IWYU pragma: no_include @@ -646,4 +639,3 @@ void sys_file_remove_readonly(const String& name) } // namespace Kyty #endif -#endif diff --git a/source/lib/Sys/src/SysWindowsVirtual.cpp b/source/lib/Sys/src/SysWindowsVirtual.cpp index 427cc44..d6e7f2e 100644 --- a/source/lib/Sys/src/SysWindowsVirtual.cpp +++ b/source/lib/Sys/src/SysWindowsVirtual.cpp @@ -1,8 +1,6 @@ #include "Kyty/Core/Common.h" -#if KYTY_PLATFORM != KYTY_PLATFORM_WINDOWS -//#error "KYTY_PLATFORM != KYTY_PLATFORM_WINDOWS" -#else +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) #include "Kyty/Core/DbgAssert.h" #include "Kyty/Core/String.h" @@ -10,13 +8,7 @@ #include "Kyty/Sys/SysVirtual.h" #include "cpuinfo.h" -#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) - #include // IWYU pragma: keep - #ifdef _WIN64 - #include // IWYU pragma: keep - #else - #include // IWYU pragma: keep -#endif +#include // IWYU pragma: keep // IWYU pragma: no_include // IWYU pragma: no_include @@ -262,5 +254,3 @@ bool sys_virtual_patch_replace(uint64_t vaddr, uint64_t value) } // namespace Kyty::Core #endif -#endif - From 7ca9d29be87924b81d1be24716b19f357848cac7 Mon Sep 17 00:00:00 2001 From: Smiril Date: Sun, 12 May 2024 14:32:18 +0200 Subject: [PATCH 35/42] Update SysMacOSDbg.cpp --- source/lib/Sys/src/SysMacOSDbg.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/source/lib/Sys/src/SysMacOSDbg.cpp b/source/lib/Sys/src/SysMacOSDbg.cpp index 62cd982..bb0a46e 100644 --- a/source/lib/Sys/src/SysMacOSDbg.cpp +++ b/source/lib/Sys/src/SysMacOSDbg.cpp @@ -12,20 +12,20 @@ namespace Kyty { -static thread_local dbg_stack_info_t g_stack = {0}; +static thread_local sys_dbg_stack_info_t g_stack = {0}; void sys_stack_walk(void** /*stack*/, int* depth) { *depth = 0; } -void sys_stack_usage_print(dbg_stack_info_t& stack) +void sys_stack_usage_print(sys_dbg_stack_info_t& stack) { printf("stack: (0x%" PRIx64 ", %" PRIu64 ")\n", static_cast(stack.commited_addr), static_cast(stack.commited_size)); printf("code: (0x%" PRIx64 ", %" PRIu64 ")\n", static_cast(stack.code_addr), static_cast(stack.code_size)); } -void sys_stack_usage(dbg_stack_info_t& s) +void sys_stack_usage(sys_dbg_stack_info_t& s) { pid_t pid = getpid(); @@ -47,7 +47,7 @@ void sys_stack_usage(dbg_stack_info_t& s) result = sprintf(str, "/proc/%d/maps", static_cast(pid)); - memset(&s, 0, sizeof(dbg_stack_info_t)); + memset(&s, 0, sizeof(sys_dbg_stack_info_t)); FILE* f = fopen(str, "r"); From b451e22bfb52d19312ff1d0ac5d89ecdc27251e6 Mon Sep 17 00:00:00 2001 From: Smiril Date: Sun, 12 May 2024 14:56:05 +0200 Subject: [PATCH 36/42] Update SysMacOSDbg.cpp --- source/lib/Sys/src/SysMacOSDbg.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/lib/Sys/src/SysMacOSDbg.cpp b/source/lib/Sys/src/SysMacOSDbg.cpp index bb0a46e..2257d0d 100644 --- a/source/lib/Sys/src/SysMacOSDbg.cpp +++ b/source/lib/Sys/src/SysMacOSDbg.cpp @@ -35,7 +35,7 @@ void sys_stack_usage(sys_dbg_stack_info_t& s) char str[1024]; char str2[1024]; - result = sprintf(str, "/proc/%d/exe", static_cast(pid)); + result = snprintf(str, 1024, "/proc/%d/exe", static_cast(pid)); ssize_t buff_len = 0; if ((buff_len = readlink(str, str2, 1023)) == -1) @@ -45,7 +45,7 @@ void sys_stack_usage(sys_dbg_stack_info_t& s) str2[buff_len] = '\0'; const char* name = basename(str2); - result = sprintf(str, "/proc/%d/maps", static_cast(pid)); + result = snprintf(str, 1024,"/proc/%d/maps", static_cast(pid)); memset(&s, 0, sizeof(sys_dbg_stack_info_t)); From 0892292b61f7601c8458dbf114bc2d1e967ff7fe Mon Sep 17 00:00:00 2001 From: Smiril Date: Sun, 12 May 2024 15:18:36 +0200 Subject: [PATCH 37/42] typo fix --- _Build/cmake_MacOSMakeClang.sh | 37 ++++++++++++++++++++++++++++ source/include/Kyty/Sys/SysDbg.h | 4 +-- source/include/Kyty/Sys/SysFileIO.h | 4 +-- source/include/Kyty/Sys/SysHeap.h | 4 +-- source/include/Kyty/Sys/SysStdio.h | 4 +-- source/include/Kyty/Sys/SysStdlib.h | 4 +-- source/include/Kyty/Sys/SysSync.h | 4 +-- source/include/Kyty/Sys/SysVirtual.h | 4 +-- 8 files changed, 51 insertions(+), 14 deletions(-) create mode 100755 _Build/cmake_MacOSMakeClang.sh diff --git a/_Build/cmake_MacOSMakeClang.sh b/_Build/cmake_MacOSMakeClang.sh new file mode 100755 index 0000000..d94e615 --- /dev/null +++ b/_Build/cmake_MacOSMakeClang.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash + +PS3='Please enter your choice: ' +options=("Debug" "Debug Final" "Release" "Release Final" "Exit") +select opt in "${options[@]}" +do + case $opt in + "Debug") + DirName=_DebugMacOSMakeClang + Options="-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=g++ -D CMAKE_BUILD_TYPE=Debug -D CMAKE_INSTALL_PREFIX=_bin ../../source" + ;; + "Debug Final") + DirName=_DebugFinalMacOSMakeClang + Options="-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=g++ -D CMAKE_BUILD_TYPE=Debug -D KYTY_FINAL=1 -D CMAKE_INSTALL_PREFIX=_bin ../../source" + ;; + "Release") + DirName=_ReleaseMacOSMakeClang + Options="-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=g++ -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=_bin ../../source" + ;; + "Release Final") + DirName=_ReleaseFinalMacOSMakeClang + Options="-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=g++ -D CMAKE_BUILD_TYPE=Release -D KYTY_FINAL=1 -D CMAKE_INSTALL_PREFIX=_bin ../../source" + ;; + esac + break +done + +if [ -z "$DirName" ]; then + exit +fi + +mkdir $DirName +cd $DirName +echo make >_build +echo make install/strip >>_build +chmod +x _build +cmake -G "Unix Makefiles" $Options diff --git a/source/include/Kyty/Sys/SysDbg.h b/source/include/Kyty/Sys/SysDbg.h index 24dd2f5..46a7846 100644 --- a/source/include/Kyty/Sys/SysDbg.h +++ b/source/include/Kyty/Sys/SysDbg.h @@ -4,9 +4,9 @@ #include "Kyty/Core/Common.h" #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) #include "Kyty/Sys/Windows/SysWindowsDbg.h" // IWYU pragma: export -#elif __linux__ +#elif defined(__linux__) #include "Kyty/Sys/Linux/SysLinuxDbg.h" // IWYU pragma: export -#elif __APPLE__ +#elif defined(__APPLE__) #include "Kyty/Sys/MacOS/SysMacOSDbg.h" // IWYU pragma: export #else # error "Unknown compiler" diff --git a/source/include/Kyty/Sys/SysFileIO.h b/source/include/Kyty/Sys/SysFileIO.h index 999218c..3e0a259 100644 --- a/source/include/Kyty/Sys/SysFileIO.h +++ b/source/include/Kyty/Sys/SysFileIO.h @@ -4,9 +4,9 @@ #include "Kyty/Core/Common.h" #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) #include "Kyty/Sys/Windows/SysWindowsFileIO.h" // IWYU pragma: export -#elif __linux__ +#elif defined(__linux__) #include "Kyty/Sys/Linux/SysLinuxFileIO.h" // IWYU pragma: export -#elif __APPLE__ +#elif defined(__APPLE__) #include "Kyty/Sys/MacOS/SysMacOSFileIO.h" // IWYU pragma: export #else # error "Unknown compiler" diff --git a/source/include/Kyty/Sys/SysHeap.h b/source/include/Kyty/Sys/SysHeap.h index 69e7e6a..7e6a2ef 100644 --- a/source/include/Kyty/Sys/SysHeap.h +++ b/source/include/Kyty/Sys/SysHeap.h @@ -4,9 +4,9 @@ #include "Kyty/Core/Common.h" #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) #include "Kyty/Sys/Windows/SysWindowsHeap.h" // IWYU pragma: export -#elif __linux__ +#elif defined(__linux__) #include "Kyty/Sys/Linux/SysLinuxHeap.h" // IWYU pragma: export -#elif __APPLE__ +#elif defined(__APPLE__) #include "Kyty/Sys/MacOS/SysMacOSHeap.h" // IWYU pragma: export #else # error "Unknown compiler" diff --git a/source/include/Kyty/Sys/SysStdio.h b/source/include/Kyty/Sys/SysStdio.h index bf7659e..066b2cd 100644 --- a/source/include/Kyty/Sys/SysStdio.h +++ b/source/include/Kyty/Sys/SysStdio.h @@ -5,9 +5,9 @@ #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) #include "Kyty/Sys/Windows/SysWindowsStdio.h" // -#elif __APPLE__ +#elif defined(__APPLE__) #include "Kyty/Sys/MacOS/SysMacOSStdio.h" // -#elif __linux__ +#elif defined(__linux__) #include "Kyty/Sys/Linux/SysLinuxStdio.h" // #else # error "Unknown compiler" diff --git a/source/include/Kyty/Sys/SysStdlib.h b/source/include/Kyty/Sys/SysStdlib.h index a8462df..26ac792 100644 --- a/source/include/Kyty/Sys/SysStdlib.h +++ b/source/include/Kyty/Sys/SysStdlib.h @@ -4,9 +4,9 @@ #include "Kyty/Core/Common.h" #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) #include "Kyty/Sys/Windows/SysWindowsStdlib.h" // IWYU pragma: export -#elif __linux__ +#elif defined(__linux__) #include "Kyty/Sys/Linux/SysLinuxStdlib.h" // IWYU pragma: export -#elif __APPLE__ +#elif defined(__APPLE__) #include "Kyty/Sys/MacOS/SysMacOSStdlib.h" // IWYU pragma: export #else # error "Unknown compiler" diff --git a/source/include/Kyty/Sys/SysSync.h b/source/include/Kyty/Sys/SysSync.h index 86303f2..092fafd 100644 --- a/source/include/Kyty/Sys/SysSync.h +++ b/source/include/Kyty/Sys/SysSync.h @@ -4,9 +4,9 @@ #include "Kyty/Core/Common.h" #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) #include "Kyty/Sys/Windows/SysWindowsSync.h" // IWYU pragma: export -#elif __linux__ +#elif defined(__linux__) #include "Kyty/Sys/Linux/SysLinuxSync.h" // IWYU pragma: export -#elif __APPLE__ +#elif defined(__APPLE__) #include "Kyty/Sys/MacOS/SysMacOSSync.h" // IWYU pragma: export #else # error "Unknown compiler" diff --git a/source/include/Kyty/Sys/SysVirtual.h b/source/include/Kyty/Sys/SysVirtual.h index 473902a..74d5cad 100644 --- a/source/include/Kyty/Sys/SysVirtual.h +++ b/source/include/Kyty/Sys/SysVirtual.h @@ -4,9 +4,9 @@ #include "Kyty/Core/Common.h" #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) #include "Kyty/Sys/Windows/SysWindowsVirtual.h" // IWYU pragma: export -#elif __linux__ +#elif defined(__linux__) #include "Kyty/Sys/Linux/SysLinuxVirtual.h" // IWYU pragma: export -#elif __APPLE__ +#elif defined(__APPLE__) #include "Kyty/Sys/MacOS/SysMacOSVirtual.h" // IWYU pragma: export #else # error "Unknown compiler" From 9745196d5dd7fd7d895c4e200a6fecfd11a6f0c0 Mon Sep 17 00:00:00 2001 From: Smiril Date: Sun, 12 May 2024 17:19:53 +0200 Subject: [PATCH 38/42] 3rd_party fixes --- .../sdl2/src/audio/coreaudio/SDL_coreaudio.m | 16 +++++------ .../sdl2/src/haptic/darwin/SDL_syshaptic.c | 2 +- .../sdl2/sdl2/src/hidapi/SDL_hidapi.c | 2 +- .../src/joystick/iphoneos/SDL_mfijoystick.m | 28 +++++++++---------- .../sdl2/src/video/cocoa/SDL_cocoaevents.m | 7 ++--- .../sdl2/src/video/cocoa/SDL_cocoamodes.m | 2 +- .../sdl2/src/video/cocoa/SDL_cocoashape.m | 2 +- .../sdl2/src/video/cocoa/SDL_cocoawindow.m | 14 +++++----- 8 files changed, 36 insertions(+), 37 deletions(-) diff --git a/source/3rdparty/sdl2/sdl2/src/audio/coreaudio/SDL_coreaudio.m b/source/3rdparty/sdl2/sdl2/src/audio/coreaudio/SDL_coreaudio.m index 4d35756..9796617 100644 --- a/source/3rdparty/sdl2/sdl2/src/audio/coreaudio/SDL_coreaudio.m +++ b/source/3rdparty/sdl2/sdl2/src/audio/coreaudio/SDL_coreaudio.m @@ -53,7 +53,7 @@ static const AudioObjectPropertyAddress devlist_address = { kAudioHardwarePropertyDevices, kAudioObjectPropertyScopeGlobal, - kAudioObjectPropertyElementMaster + kAudioObjectPropertyElementMain }; typedef void (*addDevFn)(const char *name, SDL_AudioSpec *spec, const int iscapture, AudioDeviceID devId, void *data); @@ -131,17 +131,17 @@ build_device_list(int iscapture, addDevFn addfn, void *addfndata) const AudioObjectPropertyAddress addr = { kAudioDevicePropertyStreamConfiguration, iscapture ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput, - kAudioObjectPropertyElementMaster + kAudioObjectPropertyElementMain }; const AudioObjectPropertyAddress nameaddr = { kAudioObjectPropertyName, iscapture ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput, - kAudioObjectPropertyElementMaster + kAudioObjectPropertyElementMain }; const AudioObjectPropertyAddress freqaddr = { kAudioDevicePropertyNominalSampleRate, iscapture ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput, - kAudioObjectPropertyElementMaster + kAudioObjectPropertyElementMain }; result = AudioObjectGetPropertyDataSize(dev, &addr, 0, NULL, &size); @@ -635,7 +635,7 @@ static const AudioObjectPropertyAddress alive_address = { kAudioDevicePropertyDeviceIsAlive, kAudioObjectPropertyScopeGlobal, - kAudioObjectPropertyElementMaster + kAudioObjectPropertyElementMain }; static OSStatus @@ -756,7 +756,7 @@ prepare_device(_THIS) AudioObjectPropertyAddress addr = { 0, kAudioObjectPropertyScopeGlobal, - kAudioObjectPropertyElementMaster + kAudioObjectPropertyElementMain }; if (handle == NULL) { @@ -803,7 +803,7 @@ assign_device_to_audioqueue(_THIS) const AudioObjectPropertyAddress prop = { kAudioDevicePropertyDeviceUID, this->iscapture ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput, - kAudioObjectPropertyElementMaster + kAudioObjectPropertyElementMain }; OSStatus result; @@ -952,7 +952,7 @@ audioqueue_thread(void *arg) const AudioObjectPropertyAddress default_device_address = { this->iscapture ? kAudioHardwarePropertyDefaultInputDevice : kAudioHardwarePropertyDefaultOutputDevice, kAudioObjectPropertyScopeGlobal, - kAudioObjectPropertyElementMaster + kAudioObjectPropertyElementMain }; if (this->handle == NULL) { /* opened the default device? Register to know if the user picks a new default. */ diff --git a/source/3rdparty/sdl2/sdl2/src/haptic/darwin/SDL_syshaptic.c b/source/3rdparty/sdl2/sdl2/src/haptic/darwin/SDL_syshaptic.c index 1141bec..50905e1 100644 --- a/source/3rdparty/sdl2/sdl2/src/haptic/darwin/SDL_syshaptic.c +++ b/source/3rdparty/sdl2/sdl2/src/haptic/darwin/SDL_syshaptic.c @@ -167,7 +167,7 @@ SDL_SYS_HapticInit(void) } /* Now search I/O Registry for matching devices. */ - result = IOServiceGetMatchingServices(kIOMasterPortDefault, match, &iter); + result = IOServiceGetMatchingServices(kIOMainPortDefault, match, &iter); if (result != kIOReturnSuccess) { return SDL_SetError("Haptic: Couldn't create a HID object iterator."); } diff --git a/source/3rdparty/sdl2/sdl2/src/hidapi/SDL_hidapi.c b/source/3rdparty/sdl2/sdl2/src/hidapi/SDL_hidapi.c index 718e5c1..6fb937a 100644 --- a/source/3rdparty/sdl2/sdl2/src/hidapi/SDL_hidapi.c +++ b/source/3rdparty/sdl2/sdl2/src/hidapi/SDL_hidapi.c @@ -249,7 +249,7 @@ HIDAPI_InitializeDiscovery() #endif /* defined(__WIN32__) || defined(__WINGDK__) */ #if defined(__MACOSX__) - SDL_HIDAPI_discovery.m_notificationPort = IONotificationPortCreate(kIOMasterPortDefault); + SDL_HIDAPI_discovery.m_notificationPort = IONotificationPortCreate(kIOMainPortDefault); if (SDL_HIDAPI_discovery.m_notificationPort) { { io_iterator_t portIterator = 0; diff --git a/source/3rdparty/sdl2/sdl2/src/joystick/iphoneos/SDL_mfijoystick.m b/source/3rdparty/sdl2/sdl2/src/joystick/iphoneos/SDL_mfijoystick.m index 662d820..1224709 100644 --- a/source/3rdparty/sdl2/sdl2/src/joystick/iphoneos/SDL_mfijoystick.m +++ b/source/3rdparty/sdl2/sdl2/src/joystick/iphoneos/SDL_mfijoystick.m @@ -415,7 +415,7 @@ IOS_AddMFIJoystickDevice(SDL_JoystickDeviceItem *device, GCController *controlle device->nhats = 1; /* d-pad */ device->nbuttons = nbuttons; - } else if (controller.gamepad) { + } else if (controller.extendedGamepad) { BOOL is_switch_joyconL = IsControllerSwitchJoyConL(controller); BOOL is_switch_joyconR = IsControllerSwitchJoyConR(controller); int nbuttons = 0; @@ -606,8 +606,8 @@ IOS_RemoveJoystickDevice(SDL_JoystickDeviceItem *device) if (device->controller) { /* The controller was explicitly retained in the struct, so it * should be explicitly released before freeing the struct. */ - GCController *controller = CFBridgingRelease((__bridge CFTypeRef)(device->controller)); - controller.controllerPausedHandler = nil; + //GCController *controller = CFBridgingRelease((__bridge CFTypeRef)(device->controller)); + //controller.controllerPausedHandler = nil; device->controller = nil; } } @@ -816,12 +816,12 @@ IOS_JoystickOpen(SDL_Joystick *joystick, int device_index) } else { #ifdef SDL_JOYSTICK_MFI if (device->uses_pause_handler) { - GCController *controller = device->controller; - controller.controllerPausedHandler = ^(GCController *c) { - if (joystick->hwdata) { - ++joystick->hwdata->num_pause_presses; - } - }; + //GCController *controller = device->controller; + //controller.controllerPausedHandler = ^(GCController *c) { + // if (joystick->hwdata) { + // ++joystick->hwdata->num_pause_presses; + // } + //}; } #ifdef ENABLE_MFI_SENSORS @@ -1089,8 +1089,8 @@ IOS_MFIJoystickUpdate(SDL_Joystick *joystick) } #endif /* ENABLE_MFI_SENSORS */ - } else if (controller.gamepad) { - GCGamepad *gamepad = controller.gamepad; + } else if (controller.extendedGamepad) { + GCExtendedGamepad *gamepad = controller.extendedGamepad; /* Button order matches the XInput Windows mappings. */ Uint8 buttons[joystick->nbuttons]; @@ -1595,7 +1595,7 @@ IOS_JoystickClose(SDL_Joystick *joystick) } else if (device->controller) { #ifdef SDL_JOYSTICK_MFI GCController *controller = device->controller; - controller.controllerPausedHandler = nil; + //controller.controllerPausedHandler = nil; controller.playerIndex = -1; #ifdef ENABLE_MFI_SYSTEM_GESTURE_STATE @@ -1711,8 +1711,8 @@ GetDirectionalPadForController(GCController *controller) return controller.extendedGamepad.dpad; } - if (controller.gamepad) { - return controller.gamepad.dpad; + if (controller.extendedGamepad) { + return controller.extendedGamepad.dpad; } if (controller.microGamepad) { diff --git a/source/3rdparty/sdl2/sdl2/src/video/cocoa/SDL_cocoaevents.m b/source/3rdparty/sdl2/sdl2/src/video/cocoa/SDL_cocoaevents.m index 4903a8a..0c32103 100644 --- a/source/3rdparty/sdl2/sdl2/src/video/cocoa/SDL_cocoaevents.m +++ b/source/3rdparty/sdl2/sdl2/src/video/cocoa/SDL_cocoaevents.m @@ -288,10 +288,9 @@ static void Cocoa_DispatchEvent(NSEvent *theEvent) */ if (!SDL_GetHintBoolean(SDL_HINT_MAC_BACKGROUND_APP, SDL_FALSE)) { /* Get more aggressive for Catalina: activate the Dock first so we definitely reset all activation state. */ - for (NSRunningApplication *i in [NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.apple.dock"]) { - [i activateWithOptions:NSApplicationActivateIgnoringOtherApps]; - break; - } + //for (NSRunningApplication *i in [NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.apple.dock"]) { + // break; + //} SDL_Delay(300); /* !!! FIXME: this isn't right. */ [NSApp activateIgnoringOtherApps:YES]; } diff --git a/source/3rdparty/sdl2/sdl2/src/video/cocoa/SDL_cocoamodes.m b/source/3rdparty/sdl2/sdl2/src/video/cocoa/SDL_cocoamodes.m index 072afc8..4a6d663 100644 --- a/source/3rdparty/sdl2/sdl2/src/video/cocoa/SDL_cocoamodes.m +++ b/source/3rdparty/sdl2/sdl2/src/video/cocoa/SDL_cocoamodes.m @@ -287,7 +287,7 @@ static const char * Cocoa_GetDisplayName(CGDirectDisplayID displayID) { /* This API is deprecated in 10.9 with no good replacement (as of 10.15). */ - io_service_t servicePort = CGDisplayIOServicePort(displayID); + io_service_t servicePort = CGDisplayIOServicePort(CGDirectDisplayID display); CFDictionaryRef deviceInfo = IODisplayCreateInfoDictionary(servicePort, kIODisplayOnlyPreferredName); NSDictionary *localizedNames = [(__bridge NSDictionary *)deviceInfo objectForKey:[NSString stringWithUTF8String:kDisplayProductName]]; const char* displayName = NULL; diff --git a/source/3rdparty/sdl2/sdl2/src/video/cocoa/SDL_cocoashape.m b/source/3rdparty/sdl2/sdl2/src/video/cocoa/SDL_cocoashape.m index f594cb0..827f6df 100644 --- a/source/3rdparty/sdl2/sdl2/src/video/cocoa/SDL_cocoashape.m +++ b/source/3rdparty/sdl2/sdl2/src/video/cocoa/SDL_cocoashape.m @@ -48,7 +48,7 @@ Cocoa_CreateShaper(SDL_Window* window) data = (SDL_ShapeData *)SDL_malloc(sizeof(SDL_ShapeData)); result->driverdata = data; - data->context = [windata.nswindow graphicsContext]; + data->context = [windata.nswindow NSView:graphicsContext]; data->saved = SDL_FALSE; data->shape = NULL; diff --git a/source/3rdparty/sdl2/sdl2/src/video/cocoa/SDL_cocoawindow.m b/source/3rdparty/sdl2/sdl2/src/video/cocoa/SDL_cocoawindow.m index ad56e7b..84bcb8c 100644 --- a/source/3rdparty/sdl2/sdl2/src/video/cocoa/SDL_cocoawindow.m +++ b/source/3rdparty/sdl2/sdl2/src/video/cocoa/SDL_cocoawindow.m @@ -161,7 +161,7 @@ { @autoreleasepool { NSPasteboard *pasteboard = [sender draggingPasteboard]; - NSArray *types = [NSArray arrayWithObject:NSFilenamesPboardType]; + NSArray *types = [NSArray arrayWithObject:NSPasteboardTypeFileURL]; NSString *desiredType = [pasteboard availableTypeFromArray:types]; SDL_Window *sdlwindow = [self findSDLWindow]; NSData *data; @@ -179,8 +179,8 @@ return NO; } - SDL_assert([desiredType isEqualToString:NSFilenamesPboardType]); - array = [pasteboard propertyListForType:@"NSFilenamesPboardType"]; + SDL_assert([desiredType isEqualToString:NSPasteboardTypeFileURL]); + array = [pasteboard propertyListForType:@"NSPasteboardTypeFileURL"]; /* Code addon to update the mouse location */ point = [sender draggingLocation]; @@ -260,7 +260,7 @@ static void ConvertNSRect(NSScreen *screen, BOOL fullscreen, NSRect *r) static void ScheduleContextUpdates(SDL_WindowData *data) { - NSOpenGLContext *currentContext; + NSOpenGLContext : NSObject *currentContext; NSMutableArray *contexts; if (!data || !data.nscontexts) { return; @@ -515,7 +515,7 @@ Cocoa_UpdateClipCursor(SDL_Window * window) [view setNextResponder:self]; - [view setAcceptsTouchEvents:YES]; + [view allowedTouchTypes:YES]; } - (void)observeValueForKeyPath:(NSString *)keyPath @@ -1678,7 +1678,7 @@ SetupWindowData(_THIS, SDL_Window * window, NSWindow *nswindow, NSView *nsview, /* Prevents the window's "window device" from being destroyed when it is * hidden. See http://www.mikeash.com/pyblog/nsopenglcontext-and-one-shot.html */ - [nswindow setOneShot:NO]; + //[nswindow setOneShot:NO]; /* All done! */ window->driverdata = (void *)CFBridgingRetain(data); @@ -2421,7 +2421,7 @@ Cocoa_AcceptDragAndDrop(SDL_Window * window, SDL_bool accept) { SDL_WindowData *data = (__bridge SDL_WindowData *) window->driverdata; if (accept) { - [data.nswindow registerForDraggedTypes:[NSArray arrayWithObject:(NSString *)kUTTypeFileURL]]; + [data.nswindow registerForDraggedTypes:[NSArray arrayWithObject:(NSString *)UTTypeFileURL]]; } else { [data.nswindow unregisterDraggedTypes]; } From 43666a62616cda492165daa368e65419935a9250 Mon Sep 17 00:00:00 2001 From: Smiril Date: Sun, 12 May 2024 18:19:45 +0200 Subject: [PATCH 39/42] 3rd_party fixes --- .../sdl2/src/video/cocoa/SDL_cocoamodes.m | 28 ++++++++----------- .../sdl2/src/video/cocoa/SDL_cocoashape.m | 2 +- .../sdl2/src/video/cocoa/SDL_cocoawindow.m | 6 ++-- 3 files changed, 15 insertions(+), 21 deletions(-) diff --git a/source/3rdparty/sdl2/sdl2/src/video/cocoa/SDL_cocoamodes.m b/source/3rdparty/sdl2/sdl2/src/video/cocoa/SDL_cocoamodes.m index 4a6d663..c414c94 100644 --- a/source/3rdparty/sdl2/sdl2/src/video/cocoa/SDL_cocoamodes.m +++ b/source/3rdparty/sdl2/sdl2/src/video/cocoa/SDL_cocoamodes.m @@ -123,25 +123,19 @@ static Uint32 GetDisplayModePixelFormat(CGDisplayModeRef vidmode) { /* This API is deprecated in 10.11 with no good replacement (as of 10.15). */ - CFStringRef fmt = CGDisplayModeCopyPixelEncoding(vidmode); - Uint32 pixelformat = SDL_PIXELFORMAT_UNKNOWN; - - if (CFStringCompare(fmt, CFSTR(IO32BitDirectPixels), - kCFCompareCaseInsensitive) == kCFCompareEqualTo) { - pixelformat = SDL_PIXELFORMAT_ARGB8888; - } else if (CFStringCompare(fmt, CFSTR(IO16BitDirectPixels), - kCFCompareCaseInsensitive) == kCFCompareEqualTo) { - pixelformat = SDL_PIXELFORMAT_ARGB1555; - } else if (CFStringCompare(fmt, CFSTR(kIO30BitDirectPixels), - kCFCompareCaseInsensitive) == kCFCompareEqualTo) { - pixelformat = SDL_PIXELFORMAT_ARGB2101010; - } else { - /* ignore 8-bit and such for now. */ + //CGDisplayModeRef vidmode = CGDisplayCopyDisplayMode(eDisplay); + CFDictionaryRef dict = (CFDictionaryRef)*((int64_t *)vidmode + 2); + CFTypeRef num; + Uint32 bpp = 0; + if (CFGetTypeID(dict) == CFDictionaryGetTypeID() + && CFDictionaryGetValueIfPresent(dict, kCGDisplayBitsPerPixel, (const void**)&num)) + { + CFNumberGetValue(num, kCFNumberSInt32Type, (void*)&bpp); } - CFRelease(fmt); + CFRelease(num); - return pixelformat; + return bpp; } static SDL_bool @@ -287,7 +281,7 @@ static const char * Cocoa_GetDisplayName(CGDirectDisplayID displayID) { /* This API is deprecated in 10.9 with no good replacement (as of 10.15). */ - io_service_t servicePort = CGDisplayIOServicePort(CGDirectDisplayID display); + io_service_t servicePort = CGDisplayIOServicePort(displayID); CFDictionaryRef deviceInfo = IODisplayCreateInfoDictionary(servicePort, kIODisplayOnlyPreferredName); NSDictionary *localizedNames = [(__bridge NSDictionary *)deviceInfo objectForKey:[NSString stringWithUTF8String:kDisplayProductName]]; const char* displayName = NULL; diff --git a/source/3rdparty/sdl2/sdl2/src/video/cocoa/SDL_cocoashape.m b/source/3rdparty/sdl2/sdl2/src/video/cocoa/SDL_cocoashape.m index 827f6df..f594cb0 100644 --- a/source/3rdparty/sdl2/sdl2/src/video/cocoa/SDL_cocoashape.m +++ b/source/3rdparty/sdl2/sdl2/src/video/cocoa/SDL_cocoashape.m @@ -48,7 +48,7 @@ Cocoa_CreateShaper(SDL_Window* window) data = (SDL_ShapeData *)SDL_malloc(sizeof(SDL_ShapeData)); result->driverdata = data; - data->context = [windata.nswindow NSView:graphicsContext]; + data->context = [windata.nswindow graphicsContext]; data->saved = SDL_FALSE; data->shape = NULL; diff --git a/source/3rdparty/sdl2/sdl2/src/video/cocoa/SDL_cocoawindow.m b/source/3rdparty/sdl2/sdl2/src/video/cocoa/SDL_cocoawindow.m index 84bcb8c..f602d3b 100644 --- a/source/3rdparty/sdl2/sdl2/src/video/cocoa/SDL_cocoawindow.m +++ b/source/3rdparty/sdl2/sdl2/src/video/cocoa/SDL_cocoawindow.m @@ -260,7 +260,7 @@ static void ConvertNSRect(NSScreen *screen, BOOL fullscreen, NSRect *r) static void ScheduleContextUpdates(SDL_WindowData *data) { - NSOpenGLContext : NSObject *currentContext; + NSOpenGLContext *currentContext; NSMutableArray *contexts; if (!data || !data.nscontexts) { return; @@ -515,7 +515,7 @@ Cocoa_UpdateClipCursor(SDL_Window * window) [view setNextResponder:self]; - [view allowedTouchTypes:YES]; + [view setAcceptsTouchEvents:YES]; } - (void)observeValueForKeyPath:(NSString *)keyPath @@ -2421,7 +2421,7 @@ Cocoa_AcceptDragAndDrop(SDL_Window * window, SDL_bool accept) { SDL_WindowData *data = (__bridge SDL_WindowData *) window->driverdata; if (accept) { - [data.nswindow registerForDraggedTypes:[NSArray arrayWithObject:(NSString *)UTTypeFileURL]]; + [data.nswindow registerForDraggedTypes:[NSArray arrayWithObject:(NSString *)kUTTypeFileURL]]; } else { [data.nswindow unregisterDraggedTypes]; } From a089c3cdf70096abc0c2a49a867f4d03f70b1784 Mon Sep 17 00:00:00 2001 From: Smiril Date: Sun, 12 May 2024 18:39:44 +0200 Subject: [PATCH 40/42] compiler fix in build scripts --- _Build/cmake_MacOSMakeClang.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/_Build/cmake_MacOSMakeClang.sh b/_Build/cmake_MacOSMakeClang.sh index d94e615..dfdeae4 100755 --- a/_Build/cmake_MacOSMakeClang.sh +++ b/_Build/cmake_MacOSMakeClang.sh @@ -7,19 +7,19 @@ do case $opt in "Debug") DirName=_DebugMacOSMakeClang - Options="-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=g++ -D CMAKE_BUILD_TYPE=Debug -D CMAKE_INSTALL_PREFIX=_bin ../../source" + Options="-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -D CMAKE_BUILD_TYPE=Debug -D CMAKE_INSTALL_PREFIX=_bin ../../source" ;; "Debug Final") DirName=_DebugFinalMacOSMakeClang - Options="-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=g++ -D CMAKE_BUILD_TYPE=Debug -D KYTY_FINAL=1 -D CMAKE_INSTALL_PREFIX=_bin ../../source" + Options="-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -D CMAKE_BUILD_TYPE=Debug -D KYTY_FINAL=1 -D CMAKE_INSTALL_PREFIX=_bin ../../source" ;; "Release") DirName=_ReleaseMacOSMakeClang - Options="-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=g++ -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=_bin ../../source" + Options="-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=_bin ../../source" ;; "Release Final") DirName=_ReleaseFinalMacOSMakeClang - Options="-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=g++ -D CMAKE_BUILD_TYPE=Release -D KYTY_FINAL=1 -D CMAKE_INSTALL_PREFIX=_bin ../../source" + Options="-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -D CMAKE_BUILD_TYPE=Release -D KYTY_FINAL=1 -D CMAKE_INSTALL_PREFIX=_bin ../../source" ;; esac break From bda46b2ff5a352b9fe7c54e529155f778e651a63 Mon Sep 17 00:00:00 2001 From: Smiril Date: Sun, 12 May 2024 19:03:22 +0200 Subject: [PATCH 41/42] Create FUNDING.yml --- .github/FUNDING.yml | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .github/FUNDING.yml diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..fa4d8d5 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,3 @@ +# These are supported funding model platforms + +github:[Smiril,InoriRus] # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] From 5c502e3196a555828fdf79651d8b1d61bbfee8e9 Mon Sep 17 00:00:00 2001 From: Smiril Date: Mon, 13 May 2024 20:32:52 +0200 Subject: [PATCH 42/42] huge fix --- .../magic_enum/include/magic_enum.hpp | 1741 ++++++++++------- source/emulator/src/Audio.cpp | 6 +- source/emulator/src/Kernel/Memory.cpp | 2 +- source/emulator/src/Kernel/Pthread.cpp | 13 +- source/include/Kyty/Sys/MacOS/SysMacOSDbg.h | 4 +- .../include/Kyty/Sys/MacOS/SysMacOSFileIO.h | 4 +- source/include/Kyty/Sys/MacOS/SysMacOSHeap.h | 22 +- source/include/Kyty/Sys/MacOS/SysMacOSStdio.h | 4 +- .../include/Kyty/Sys/MacOS/SysMacOSStdlib.h | 4 +- source/include/Kyty/Sys/MacOS/SysMacOSSync.h | 6 +- source/include/Kyty/Sys/MacOS/SysMacOSTimer.h | 4 +- .../include/Kyty/Sys/MacOS/SysMacOSVirtual.h | 4 +- source/lib/Core/src/File.cpp | 2 - source/lib/Core/src/MemoryAlloc.cpp | 16 +- source/lib/Core/src/Threads.cpp | 4 +- source/lib/Core/src/VirtualMemory.cpp | 2 +- source/lib/Scripts/src/Scripts.cpp | 2 +- source/lib/Sys/src/SysMacOSDbg.cpp | 5 +- 18 files changed, 1102 insertions(+), 743 deletions(-) diff --git a/source/3rdparty/magic_enum/include/magic_enum.hpp b/source/3rdparty/magic_enum/include/magic_enum.hpp index ac16305..7adf877 100644 --- a/source/3rdparty/magic_enum/include/magic_enum.hpp +++ b/source/3rdparty/magic_enum/include/magic_enum.hpp @@ -5,11 +5,11 @@ // | | | | (_| | (_| | | (__ | |____| | | | |_| | | | | | | | |____|_| |_| // |_| |_|\__,_|\__, |_|\___| |______|_| |_|\__,_|_| |_| |_| \_____| // __/ | https://github.com/Neargye/magic_enum -// |___/ version 0.7.0 +// |___/ version 0.9.5 // // Licensed under the MIT License . // SPDX-License-Identifier: MIT -// Copyright (c) 2019 - 2020 Daniil Goncharov . +// Copyright (c) 2019 - 2024 Daniil Goncharov . // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -33,40 +33,57 @@ #define NEARGYE_MAGIC_ENUM_HPP #define MAGIC_ENUM_VERSION_MAJOR 0 -#define MAGIC_ENUM_VERSION_MINOR 7 -#define MAGIC_ENUM_VERSION_PATCH 0 +#define MAGIC_ENUM_VERSION_MINOR 9 +#define MAGIC_ENUM_VERSION_PATCH 5 #include -#include -#include #include -#include +#include +#include #include #include #include +#if defined(MAGIC_ENUM_CONFIG_FILE) +# include MAGIC_ENUM_CONFIG_FILE +#endif + #if !defined(MAGIC_ENUM_USING_ALIAS_OPTIONAL) -#include +# include #endif #if !defined(MAGIC_ENUM_USING_ALIAS_STRING) -#include +# include #endif #if !defined(MAGIC_ENUM_USING_ALIAS_STRING_VIEW) -#include +# include +#endif + +#if defined(MAGIC_ENUM_NO_ASSERT) +# define MAGIC_ENUM_ASSERT(...) static_cast(0) +#elif !defined(MAGIC_ENUM_ASSERT) +# include +# define MAGIC_ENUM_ASSERT(...) assert((__VA_ARGS__)) #endif #if defined(__clang__) # pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wunknown-warning-option" +# pragma clang diagnostic ignored "-Wenum-constexpr-conversion" +# pragma clang diagnostic ignored "-Wuseless-cast" // suppresses 'static_cast('\0')' for char_type = char (common on Linux). #elif defined(__GNUC__) # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Wmaybe-uninitialized" // May be used uninitialized 'return {};'. +# pragma GCC diagnostic ignored "-Wuseless-cast" // suppresses 'static_cast('\0')' for char_type = char (common on Linux). #elif defined(_MSC_VER) # pragma warning(push) -# pragma warning(disable : 26495) // Variable 'static_string::chars' is uninitialized. +# pragma warning(disable : 26495) // Variable 'static_str::chars_' is uninitialized. +# pragma warning(disable : 28020) // Arithmetic overflow: Using operator '-' on a 4 byte value and then casting the result to a 8 byte value. +# pragma warning(disable : 26451) // The expression '0<=_Param_(1)&&_Param_(1)<=1-1' is not true at this call. +# pragma warning(disable : 4514) // Unreferenced inline function has been removed. #endif // Checks magic_enum compiler compatibility. -#if defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 9 || defined(_MSC_VER) && _MSC_VER >= 1910 +#if defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 9 || defined(_MSC_VER) && _MSC_VER >= 1910 || defined(__RESHARPER__) # undef MAGIC_ENUM_SUPPORTED # define MAGIC_ENUM_SUPPORTED 1 #endif @@ -86,7 +103,20 @@ // Enum value must be less or equals than MAGIC_ENUM_RANGE_MAX. By default MAGIC_ENUM_RANGE_MAX = 128. // If need another max range for all enum types by default, redefine the macro MAGIC_ENUM_RANGE_MAX. #if !defined(MAGIC_ENUM_RANGE_MAX) -# define MAGIC_ENUM_RANGE_MAX 128 +# define MAGIC_ENUM_RANGE_MAX 127 +#endif + +// Improve ReSharper C++ intellisense performance with builtins, avoiding unnecessary template instantiations. +#if defined(__RESHARPER__) +# undef MAGIC_ENUM_GET_ENUM_NAME_BUILTIN +# undef MAGIC_ENUM_GET_TYPE_NAME_BUILTIN +# if __RESHARPER__ >= 20230100 +# define MAGIC_ENUM_GET_ENUM_NAME_BUILTIN(V) __rscpp_enumerator_name(V) +# define MAGIC_ENUM_GET_TYPE_NAME_BUILTIN(T) __rscpp_type_name() +# else +# define MAGIC_ENUM_GET_ENUM_NAME_BUILTIN(V) nullptr +# define MAGIC_ENUM_GET_TYPE_NAME_BUILTIN(T) nullptr +# endif #endif namespace magic_enum { @@ -95,24 +125,40 @@ namespace magic_enum { #if defined(MAGIC_ENUM_USING_ALIAS_OPTIONAL) MAGIC_ENUM_USING_ALIAS_OPTIONAL #else -template -using optional = std::optional; +using std::optional; #endif -// If need another optional type, define the macro MAGIC_ENUM_USING_ALIAS_STRING_VIEW. +// If need another string_view type, define the macro MAGIC_ENUM_USING_ALIAS_STRING_VIEW. #if defined(MAGIC_ENUM_USING_ALIAS_STRING_VIEW) MAGIC_ENUM_USING_ALIAS_STRING_VIEW #else -using string_view = std::string_view; +using std::string_view; #endif -// If need another optional type, define the macro MAGIC_ENUM_USING_ALIAS_STRING. +// If need another string type, define the macro MAGIC_ENUM_USING_ALIAS_STRING. #if defined(MAGIC_ENUM_USING_ALIAS_STRING) MAGIC_ENUM_USING_ALIAS_STRING #else -using string = std::string; +using std::string; #endif +using char_type = string_view::value_type; +static_assert(std::is_same_v, "magic_enum::customize requires same string_view::value_type and string::value_type"); +static_assert([] { + if constexpr (std::is_same_v) { + constexpr const char c[] = "abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789|"; + constexpr const wchar_t wc[] = L"abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789|"; + static_assert(std::size(c) == std::size(wc), "magic_enum::customize identifier characters are multichars in wchar_t."); + + for (std::size_t i = 0; i < std::size(c); ++i) { + if (c[i] != wc[i]) { + return false; + } + } + } + return true; +} (), "magic_enum::customize wchar_t is not compatible with ASCII."); + namespace customize { // Enum value must be in range [MAGIC_ENUM_RANGE_MIN, MAGIC_ENUM_RANGE_MAX]. By default MAGIC_ENUM_RANGE_MIN = -128, MAGIC_ENUM_RANGE_MAX = 128. @@ -120,26 +166,46 @@ namespace customize { // If need another range for specific enum type, add specialization enum_range for necessary enum type. template struct enum_range { - static_assert(std::is_enum_v, "magic_enum::customize::enum_range requires enum type."); - inline static constexpr int min = MAGIC_ENUM_RANGE_MIN; - inline static constexpr int max = MAGIC_ENUM_RANGE_MAX; - static_assert(max > min, "magic_enum::customize::enum_range requires max > min."); + static constexpr int min = MAGIC_ENUM_RANGE_MIN; + static constexpr int max = MAGIC_ENUM_RANGE_MAX; }; -static_assert(MAGIC_ENUM_RANGE_MIN <= 0, "MAGIC_ENUM_RANGE_MIN must be less or equals than 0."); -static_assert(MAGIC_ENUM_RANGE_MIN > (std::numeric_limits::min)(), "MAGIC_ENUM_RANGE_MIN must be greater than INT16_MIN."); - -static_assert(MAGIC_ENUM_RANGE_MAX > 0, "MAGIC_ENUM_RANGE_MAX must be greater than 0."); -static_assert(MAGIC_ENUM_RANGE_MAX < (std::numeric_limits::max)(), "MAGIC_ENUM_RANGE_MAX must be less than INT16_MAX."); - static_assert(MAGIC_ENUM_RANGE_MAX > MAGIC_ENUM_RANGE_MIN, "MAGIC_ENUM_RANGE_MAX must be greater than MAGIC_ENUM_RANGE_MIN."); -// If need cunstom names for enum type, add specialization enum_name for necessary enum type. -template -constexpr string_view enum_name(E) noexcept { - static_assert(std::is_enum_v, "magic_enum::customize::enum_name requires enum type."); +namespace detail { - return {}; +enum class customize_tag { + default_tag, + invalid_tag, + custom_tag +}; + +} // namespace magic_enum::customize::detail + +class customize_t : public std::pair { + public: + constexpr customize_t(string_view srt) : std::pair{detail::customize_tag::custom_tag, srt} {} + constexpr customize_t(const char_type* srt) : customize_t{string_view{srt}} {} + constexpr customize_t(detail::customize_tag tag) : std::pair{tag, string_view{}} { + MAGIC_ENUM_ASSERT(tag != detail::customize_tag::custom_tag); + } +}; + +// Default customize. +inline constexpr auto default_tag = customize_t{detail::customize_tag::default_tag}; +// Invalid customize. +inline constexpr auto invalid_tag = customize_t{detail::customize_tag::invalid_tag}; + +// If need custom names for enum, add specialization enum_name for necessary enum type. +template +constexpr customize_t enum_name(E) noexcept { + return default_tag; +} + +// If need custom type name for enum, add specialization enum_type_name for necessary enum type. +template +constexpr customize_t enum_type_name() noexcept { + return default_tag; } } // namespace magic_enum::customize @@ -149,76 +215,106 @@ namespace detail { template struct supported #if defined(MAGIC_ENUM_SUPPORTED) && MAGIC_ENUM_SUPPORTED || defined(MAGIC_ENUM_NO_CHECK_SUPPORT) - : std::true_type {}; + : std::true_type {}; #else - : std::false_type {}; + : std::false_type {}; #endif -template -struct static_string { - constexpr explicit static_string(string_view str) noexcept : static_string{str, std::make_index_sequence{}} { - assert(str.size() == N); +template , std::enable_if_t, int> = 0> +using enum_constant = std::integral_constant; + +template +inline constexpr bool always_false_v = false; + +template +struct has_is_flags : std::false_type {}; + +template +struct has_is_flags::is_flags)>> : std::bool_constant::is_flags)>>> {}; + +template +struct range_min : std::integral_constant {}; + +template +struct range_min::min)>> : std::integral_constant::min), customize::enum_range::min> {}; + +template +struct range_max : std::integral_constant {}; + +template +struct range_max::max)>> : std::integral_constant::max), customize::enum_range::max> {}; + +struct str_view { + const char* str_ = nullptr; + std::size_t size_ = 0; +}; + +template +class static_str { + public: + constexpr explicit static_str(str_view str) noexcept : static_str{str.str_, std::make_integer_sequence{}} { + MAGIC_ENUM_ASSERT(str.size_ == N); } - constexpr const char* data() const noexcept { return chars.data(); } + constexpr explicit static_str(string_view str) noexcept : static_str{str.data(), std::make_integer_sequence{}} { + MAGIC_ENUM_ASSERT(str.size() == N); + } - constexpr std::size_t size() const noexcept { return N; } + constexpr const char_type* data() const noexcept { return chars_; } + + constexpr std::uint16_t size() const noexcept { return N; } constexpr operator string_view() const noexcept { return {data(), size()}; } private: - template - constexpr static_string(string_view str, std::index_sequence) noexcept : chars{{str[I]..., '\0'}} {} + template + constexpr static_str(const char* str, std::integer_sequence) noexcept : chars_{static_cast(str[I])..., static_cast('\0')} {} - const std::array chars; + template + constexpr static_str(string_view str, std::integer_sequence) noexcept : chars_{str[I]..., static_cast('\0')} {} + + char_type chars_[static_cast(N) + 1]; }; template <> -struct static_string<0> { - constexpr explicit static_string(string_view) noexcept {} +class static_str<0> { + public: + constexpr explicit static_str() = default; - constexpr const char* data() const noexcept { return nullptr; } + constexpr explicit static_str(str_view) noexcept {} - constexpr std::size_t size() const noexcept { return 0; } + constexpr explicit static_str(string_view) noexcept {} + + constexpr const char_type* data() const noexcept { return nullptr; } + + constexpr std::uint16_t size() const noexcept { return 0; } constexpr operator string_view() const noexcept { return {}; } }; -struct char_equal_to { - constexpr bool operator()(char lhs, char rhs) const noexcept { - return lhs == rhs; +template > +class case_insensitive { + static constexpr char_type to_lower(char_type c) noexcept { + return (c >= static_cast('A') && c <= static_cast('Z')) ? static_cast(c + (static_cast('a') - static_cast('A'))) : c; + } + + public: + template + constexpr auto operator()(L lhs, R rhs) const noexcept -> std::enable_if_t, char_type> && std::is_same_v, char_type>, bool> { + return Op{}(to_lower(lhs), to_lower(rhs)); } }; -constexpr string_view pretty_name(string_view name) noexcept { - for (std::size_t i = name.size(); i > 0; --i) { - if (!((name[i - 1] >= '0' && name[i - 1] <= '9') || - (name[i - 1] >= 'a' && name[i - 1] <= 'z') || - (name[i - 1] >= 'A' && name[i - 1] <= 'Z') || - (name[i - 1] == '_'))) { - name.remove_prefix(i); - break; - } - } - - if (name.size() > 0 && ((name.front() >= 'a' && name.front() <= 'z') || - (name.front() >= 'A' && name.front() <= 'Z') || - (name.front() == '_'))) { - return name; - } - - return {}; // Invalid name. -} - -constexpr std::size_t find(string_view str, char c) noexcept { -#if defined(__clang__) && __clang_major__ < 9 && defined(__GLIBCXX__) || defined(_MSC_VER) && _MSC_VER < 1920 +constexpr std::size_t find(string_view str, char_type c) noexcept { +#if defined(__clang__) && __clang_major__ < 9 && defined(__GLIBCXX__) || defined(_MSC_VER) && _MSC_VER < 1920 && !defined(__clang__) // https://stackoverflow.com/questions/56484834/constexpr-stdstring-viewfind-last-of-doesnt-work-on-clang-8-with-libstdc // https://developercommunity.visualstudio.com/content/problem/360432/vs20178-regression-c-failed-in-test.html - constexpr auto workaroung = true; + constexpr bool workaround = true; #else - constexpr auto workaroung = false; + constexpr bool workaround = false; #endif - if constexpr (workaroung) { + + if constexpr (workaround) { for (std::size_t i = 0; i < str.size(); ++i) { if (str[i] == c) { return i; @@ -227,23 +323,33 @@ constexpr std::size_t find(string_view str, char c) noexcept { return string_view::npos; } else { - return str.find_first_of(c); + return str.find(c); } } template -constexpr bool cmp_equal(string_view lhs, string_view rhs, BinaryPredicate&& p) noexcept(std::is_nothrow_invocable_r_v) { -#if defined(_MSC_VER) && _MSC_VER < 1920 +constexpr bool is_default_predicate() noexcept { + return std::is_same_v, std::equal_to> || + std::is_same_v, std::equal_to<>>; +} + +template +constexpr bool is_nothrow_invocable() { + return is_default_predicate() || + std::is_nothrow_invocable_r_v; +} + +template +constexpr bool cmp_equal(string_view lhs, string_view rhs, [[maybe_unused]] BinaryPredicate&& p) noexcept(is_nothrow_invocable()) { +#if defined(_MSC_VER) && _MSC_VER < 1920 && !defined(__clang__) // https://developercommunity.visualstudio.com/content/problem/360432/vs20178-regression-c-failed-in-test.html // https://developercommunity.visualstudio.com/content/problem/232218/c-constexpr-string-view.html - constexpr auto workaroung = true; + constexpr bool workaround = true; #else - constexpr auto workaroung = false; + constexpr bool workaround = false; #endif - if constexpr (std::is_same_v, char_equal_to> && !workaroung) { - static_cast(p); - return lhs == rhs; - } else { + + if constexpr (!is_default_predicate() || workaround) { if (lhs.size() != rhs.size()) { return false; } @@ -256,6 +362,8 @@ constexpr bool cmp_equal(string_view lhs, string_view rhs, BinaryPredicate&& p) } return true; + } else { + return lhs == rhs; } } @@ -266,6 +374,10 @@ constexpr bool cmp_less(L lhs, R rhs) noexcept { if constexpr (std::is_signed_v == std::is_signed_v) { // If same signedness (both signed or both unsigned). return lhs < rhs; + } else if constexpr (std::is_same_v) { // bool special case + return static_cast(lhs) < rhs; + } else if constexpr (std::is_same_v) { // bool special case + return lhs < static_cast(rhs); } else if constexpr (std::is_signed_v) { // If 'right' is negative, then result is 'false', otherwise cast & compare. return rhs > 0 && lhs < static_cast>(rhs); @@ -279,18 +391,24 @@ template constexpr I log2(I value) noexcept { static_assert(std::is_integral_v, "magic_enum::detail::log2 requires integral type."); - auto ret = I{0}; - for (; value > I{1}; value >>= I{1}, ++ret) {} + if constexpr (std::is_same_v) { // bool special case + return MAGIC_ENUM_ASSERT(false), value; + } else { + auto ret = I{0}; + for (; value > I{1}; value >>= I{1}, ++ret) {} - return ret; + return ret; + } } -template -constexpr bool is_pow2(I x) noexcept { - static_assert(std::is_integral_v, "magic_enum::detail::is_pow2 requires integral type."); - - return x != 0 && (x & (x - 1)) == 0; +#if defined(__cpp_lib_array_constexpr) && __cpp_lib_array_constexpr >= 201603L +# define MAGIC_ENUM_ARRAY_CONSTEXPR 1 +#else +template +constexpr std::array, N> to_array(T (&a)[N], std::index_sequence) noexcept { + return {{a[I]...}}; } +#endif template inline constexpr bool is_enum_v = std::is_enum_v && std::is_same_v>; @@ -298,83 +416,277 @@ inline constexpr bool is_enum_v = std::is_enum_v && std::is_same_v constexpr auto n() noexcept { static_assert(is_enum_v, "magic_enum::detail::n requires enum type."); -#if defined(MAGIC_ENUM_SUPPORTED) && MAGIC_ENUM_SUPPORTED -# if defined(__clang__) - constexpr string_view name{__PRETTY_FUNCTION__ + 34, sizeof(__PRETTY_FUNCTION__) - 36}; -# elif defined(__GNUC__) - constexpr string_view name{__PRETTY_FUNCTION__ + 49, sizeof(__PRETTY_FUNCTION__) - 51}; -# elif defined(_MSC_VER) - constexpr string_view name{__FUNCSIG__ + 40, sizeof(__FUNCSIG__) - 57}; -# endif - return static_string{name}; + + if constexpr (supported::value) { +#if defined(MAGIC_ENUM_GET_TYPE_NAME_BUILTIN) + constexpr auto name_ptr = MAGIC_ENUM_GET_TYPE_NAME_BUILTIN(E); + constexpr auto name = name_ptr ? str_view{name_ptr, std::char_traits::length(name_ptr)} : str_view{}; +#elif defined(__clang__) + str_view name; + if constexpr (sizeof(__PRETTY_FUNCTION__) == sizeof(__FUNCTION__)) { + static_assert(always_false_v, "magic_enum::detail::n requires __PRETTY_FUNCTION__."); + return str_view{}; + } else { + name.size_ = sizeof(__PRETTY_FUNCTION__) - 36; + name.str_ = __PRETTY_FUNCTION__ + 34; + } +#elif defined(__GNUC__) + auto name = str_view{__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1}; + if constexpr (sizeof(__PRETTY_FUNCTION__) == sizeof(__FUNCTION__)) { + static_assert(always_false_v, "magic_enum::detail::n requires __PRETTY_FUNCTION__."); + return str_view{}; + } else if (name.str_[name.size_ - 1] == ']') { + name.size_ -= 50; + name.str_ += 49; + } else { + name.size_ -= 40; + name.str_ += 37; + } +#elif defined(_MSC_VER) + // CLI/C++ workaround (see https://github.com/Neargye/magic_enum/issues/284). + str_view name; + name.str_ = __FUNCSIG__; + name.str_ += 40; + name.size_ += sizeof(__FUNCSIG__) - 57; #else - return string_view{}; // Unsupported compiler. + auto name = str_view{}; #endif + std::size_t p = 0; + for (std::size_t i = name.size_; i > 0; --i) { + if (name.str_[i] == ':') { + p = i + 1; + break; + } + } + if (p > 0) { + name.size_ -= p; + name.str_ += p; + } + return name; + } else { + return str_view{}; // Unsupported compiler or Invalid customize. + } } template -inline constexpr auto type_name_v = n(); +constexpr auto type_name() noexcept { + [[maybe_unused]] constexpr auto custom = customize::enum_type_name(); + static_assert(std::is_same_v, customize::customize_t>, "magic_enum::customize requires customize_t type."); + if constexpr (custom.first == customize::detail::customize_tag::custom_tag) { + constexpr auto name = custom.second; + static_assert(!name.empty(), "magic_enum::customize requires not empty string."); + return static_str{name}; + } else if constexpr (custom.first == customize::detail::customize_tag::invalid_tag) { + return static_str<0>{}; + } else if constexpr (custom.first == customize::detail::customize_tag::default_tag) { + constexpr auto name = n(); + return static_str{name}; + } else { + static_assert(always_false_v, "magic_enum::customize invalid."); + } +} +template +inline constexpr auto type_name_v = type_name(); + +template +constexpr auto n() noexcept { + static_assert(is_enum_v, "magic_enum::detail::n requires enum type."); + + if constexpr (supported::value) { +#if defined(MAGIC_ENUM_GET_ENUM_NAME_BUILTIN) + constexpr auto name_ptr = MAGIC_ENUM_GET_ENUM_NAME_BUILTIN(V); + auto name = name_ptr ? str_view{name_ptr, std::char_traits::length(name_ptr)} : str_view{}; +#elif defined(__clang__) + str_view name; + if constexpr (sizeof(__PRETTY_FUNCTION__) == sizeof(__FUNCTION__)) { + static_assert(always_false_v, "magic_enum::detail::n requires __PRETTY_FUNCTION__."); + return str_view{}; + } else { + name.size_ = sizeof(__PRETTY_FUNCTION__) - 36; + name.str_ = __PRETTY_FUNCTION__ + 34; + } + if (name.size_ > 22 && name.str_[0] == '(' && name.str_[1] == 'a' && name.str_[10] == ' ' && name.str_[22] == ':') { + name.size_ -= 23; + name.str_ += 23; + } + if (name.str_[0] == '(' || name.str_[0] == '-' || (name.str_[0] >= '0' && name.str_[0] <= '9')) { + name = str_view{}; + } +#elif defined(__GNUC__) + auto name = str_view{__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1}; + if constexpr (sizeof(__PRETTY_FUNCTION__) == sizeof(__FUNCTION__)) { + static_assert(always_false_v, "magic_enum::detail::n requires __PRETTY_FUNCTION__."); + return str_view{}; + } else if (name.str_[name.size_ - 1] == ']') { + name.size_ -= 55; + name.str_ += 54; + } else { + name.size_ -= 40; + name.str_ += 37; + } + if (name.str_[0] == '(') { + name = str_view{}; + } +#elif defined(_MSC_VER) + str_view name; + if ((__FUNCSIG__[5] == '_' && __FUNCSIG__[35] != '(') || (__FUNCSIG__[5] == 'c' && __FUNCSIG__[41] != '(')) { + // CLI/C++ workaround (see https://github.com/Neargye/magic_enum/issues/284). + name.str_ = __FUNCSIG__; + name.str_ += 35; + name.size_ = sizeof(__FUNCSIG__) - 52; + } +#else + auto name = str_view{}; +#endif + std::size_t p = 0; + for (std::size_t i = name.size_; i > 0; --i) { + if (name.str_[i] == ':') { + p = i + 1; + break; + } + } + if (p > 0) { + name.size_ -= p; + name.str_ += p; + } + return name; + } else { + return str_view{}; // Unsupported compiler or Invalid customize. + } +} + +#if defined(_MSC_VER) && !defined(__clang__) && _MSC_VER < 1920 +# define MAGIC_ENUM_VS_2017_WORKAROUND 1 +#endif + +#if defined(MAGIC_ENUM_VS_2017_WORKAROUND) template constexpr auto n() noexcept { static_assert(is_enum_v, "magic_enum::detail::n requires enum type."); - constexpr auto custom_name = customize::enum_name(V); - if constexpr (custom_name.empty()) { - static_cast(custom_name); -#if defined(MAGIC_ENUM_SUPPORTED) && MAGIC_ENUM_SUPPORTED -# if defined(__clang__) || defined(__GNUC__) - constexpr auto name = pretty_name({__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 2}); -# elif defined(_MSC_VER) - constexpr auto name = pretty_name({__FUNCSIG__, sizeof(__FUNCSIG__) - 17}); +# if defined(MAGIC_ENUM_GET_ENUM_NAME_BUILTIN) + constexpr auto name_ptr = MAGIC_ENUM_GET_ENUM_NAME_BUILTIN(V); + auto name = name_ptr ? str_view{name_ptr, std::char_traits::length(name_ptr)} : str_view{}; +# else + // CLI/C++ workaround (see https://github.com/Neargye/magic_enum/issues/284). + str_view name; + name.str_ = __FUNCSIG__; + name.size_ = sizeof(__FUNCSIG__) - 17; + std::size_t p = 0; + for (std::size_t i = name.size_; i > 0; --i) { + if (name.str_[i] == ',' || name.str_[i] == ':') { + p = i + 1; + break; + } + } + if (p > 0) { + name.size_ -= p; + name.str_ += p; + } + if (name.str_[0] == '(' || name.str_[0] == '-' || (name.str_[0] >= '0' && name.str_[0] <= '9')) { + name = str_view{}; + } + return name; # endif - return static_string{name}; -#else - return string_view{}; // Unsupported compiler. +} #endif + +template +constexpr auto enum_name() noexcept { + [[maybe_unused]] constexpr auto custom = customize::enum_name(V); + static_assert(std::is_same_v, customize::customize_t>, "magic_enum::customize requires customize_t type."); + if constexpr (custom.first == customize::detail::customize_tag::custom_tag) { + constexpr auto name = custom.second; + static_assert(!name.empty(), "magic_enum::customize requires not empty string."); + return static_str{name}; + } else if constexpr (custom.first == customize::detail::customize_tag::invalid_tag) { + return static_str<0>{}; + } else if constexpr (custom.first == customize::detail::customize_tag::default_tag) { +#if defined(MAGIC_ENUM_VS_2017_WORKAROUND) + constexpr auto name = n(); +#else + constexpr auto name = n(); +#endif + return static_str{name}; } else { - return static_string{custom_name}; + static_assert(always_false_v, "magic_enum::customize invalid."); } } template -inline constexpr auto enum_name_v = n(); +inline constexpr auto enum_name_v = enum_name(); template constexpr bool is_valid() noexcept { - static_assert(is_enum_v, "magic_enum::detail::is_valid requires enum type."); - - return n(V)>().size() != 0; +#if defined(__clang__) && __clang_major__ >= 16 + // https://reviews.llvm.org/D130058, https://reviews.llvm.org/D131307 + constexpr E v = __builtin_bit_cast(E, V); +#else + constexpr E v = static_cast(V); +#endif + [[maybe_unused]] constexpr auto custom = customize::enum_name(v); + static_assert(std::is_same_v, customize::customize_t>, "magic_enum::customize requires customize_t type."); + if constexpr (custom.first == customize::detail::customize_tag::custom_tag) { + constexpr auto name = custom.second; + static_assert(!name.empty(), "magic_enum::customize requires not empty string."); + return name.size() != 0; + } else if constexpr (custom.first == customize::detail::customize_tag::default_tag) { +#if defined(MAGIC_ENUM_VS_2017_WORKAROUND) + return n().size_ != 0; +#else + return n().size_ != 0; +#endif + } else { + return false; + } } -template > -constexpr int reflected_min() noexcept { - static_assert(is_enum_v, "magic_enum::detail::reflected_min requires enum type."); +enum class enum_subtype { + common, + flags +}; - if constexpr (IsFlags) { +template > +constexpr U ualue(std::size_t i) noexcept { + if constexpr (std::is_same_v) { // bool special case + static_assert(O == 0, "magic_enum::detail::ualue requires valid offset."); + + return static_cast(i); + } else if constexpr (S == enum_subtype::flags) { + return static_cast(U{1} << static_cast(static_cast(i) + O)); + } else { + return static_cast(static_cast(i) + O); + } +} + +template > +constexpr E value(std::size_t i) noexcept { + return static_cast(ualue(i)); +} + +template > +constexpr int reflected_min() noexcept { + if constexpr (S == enum_subtype::flags) { return 0; } else { - constexpr auto lhs = customize::enum_range::min; - static_assert(lhs > (std::numeric_limits::min)(), "magic_enum::enum_range requires min must be greater than INT16_MIN."); + constexpr auto lhs = range_min::value; constexpr auto rhs = (std::numeric_limits::min)(); - if constexpr (cmp_less(lhs, rhs)) { - return rhs; - } else { + if constexpr (cmp_less(rhs, lhs)) { return lhs; + } else { + return rhs; } } } -template > +template > constexpr int reflected_max() noexcept { - static_assert(is_enum_v, "magic_enum::detail::reflected_max requires enum type."); - - if constexpr (IsFlags) { + if constexpr (S == enum_subtype::flags) { return std::numeric_limits::digits - 1; } else { - constexpr auto lhs = customize::enum_range::max; - static_assert(lhs < (std::numeric_limits::max)(), "magic_enum::enum_range requires max must be less than INT16_MAX."); + constexpr auto lhs = range_max::value; constexpr auto rhs = (std::numeric_limits::max)(); if constexpr (cmp_less(lhs, rhs)) { @@ -385,191 +697,198 @@ constexpr int reflected_max() noexcept { } } -template -inline constexpr auto reflected_min_v = reflected_min(); +#define MAGIC_ENUM_FOR_EACH_256(T) \ + T( 0)T( 1)T( 2)T( 3)T( 4)T( 5)T( 6)T( 7)T( 8)T( 9)T( 10)T( 11)T( 12)T( 13)T( 14)T( 15)T( 16)T( 17)T( 18)T( 19)T( 20)T( 21)T( 22)T( 23)T( 24)T( 25)T( 26)T( 27)T( 28)T( 29)T( 30)T( 31) \ + T( 32)T( 33)T( 34)T( 35)T( 36)T( 37)T( 38)T( 39)T( 40)T( 41)T( 42)T( 43)T( 44)T( 45)T( 46)T( 47)T( 48)T( 49)T( 50)T( 51)T( 52)T( 53)T( 54)T( 55)T( 56)T( 57)T( 58)T( 59)T( 60)T( 61)T( 62)T( 63) \ + T( 64)T( 65)T( 66)T( 67)T( 68)T( 69)T( 70)T( 71)T( 72)T( 73)T( 74)T( 75)T( 76)T( 77)T( 78)T( 79)T( 80)T( 81)T( 82)T( 83)T( 84)T( 85)T( 86)T( 87)T( 88)T( 89)T( 90)T( 91)T( 92)T( 93)T( 94)T( 95) \ + T( 96)T( 97)T( 98)T( 99)T(100)T(101)T(102)T(103)T(104)T(105)T(106)T(107)T(108)T(109)T(110)T(111)T(112)T(113)T(114)T(115)T(116)T(117)T(118)T(119)T(120)T(121)T(122)T(123)T(124)T(125)T(126)T(127) \ + T(128)T(129)T(130)T(131)T(132)T(133)T(134)T(135)T(136)T(137)T(138)T(139)T(140)T(141)T(142)T(143)T(144)T(145)T(146)T(147)T(148)T(149)T(150)T(151)T(152)T(153)T(154)T(155)T(156)T(157)T(158)T(159) \ + T(160)T(161)T(162)T(163)T(164)T(165)T(166)T(167)T(168)T(169)T(170)T(171)T(172)T(173)T(174)T(175)T(176)T(177)T(178)T(179)T(180)T(181)T(182)T(183)T(184)T(185)T(186)T(187)T(188)T(189)T(190)T(191) \ + T(192)T(193)T(194)T(195)T(196)T(197)T(198)T(199)T(200)T(201)T(202)T(203)T(204)T(205)T(206)T(207)T(208)T(209)T(210)T(211)T(212)T(213)T(214)T(215)T(216)T(217)T(218)T(219)T(220)T(221)T(222)T(223) \ + T(224)T(225)T(226)T(227)T(228)T(229)T(230)T(231)T(232)T(233)T(234)T(235)T(236)T(237)T(238)T(239)T(240)T(241)T(242)T(243)T(244)T(245)T(246)T(247)T(248)T(249)T(250)T(251)T(252)T(253)T(254)T(255) -template -inline constexpr auto reflected_max_v = reflected_max(); - -template > -constexpr E value(std::size_t i) noexcept { - static_assert(is_enum_v, "magic_enum::detail::value requires enum type."); - - if constexpr (IsFlags) { - return static_cast(U{1} << static_cast(static_cast(i) + O)); - } else { - return static_cast(static_cast(i) + O); - } -} - -template -constexpr auto values(std::index_sequence) noexcept { - static_assert(is_enum_v, "magic_enum::detail::values requires enum type."); - constexpr std::array valid{{is_valid(I)>()...}}; - constexpr std::size_t count = [](decltype((valid)) valid_) constexpr noexcept -> std::size_t { - auto count_ = std::size_t{0}; - for (std::size_t i_ = 0; i_ < valid_.size(); ++i_) { - if (valid_[i_]) { - ++count_; - } - } - return count_; - }(valid); - - std::array values{}; - for (std::size_t i = 0, v = 0; v < count; ++i) { - if (valid[i]) { - values[v++] = value(i); - } +template +constexpr void valid_count(bool* valid, std::size_t& count) noexcept { +#define MAGIC_ENUM_V(O) \ + if constexpr ((I + O) < Size) { \ + if constexpr (is_valid(I + O)>()) { \ + valid[I + O] = true; \ + ++count; \ + } \ } - return values; -} + MAGIC_ENUM_FOR_EACH_256(MAGIC_ENUM_V) -template > -constexpr auto values() noexcept { - static_assert(is_enum_v, "magic_enum::detail::values requires enum type."); - constexpr auto range_size = reflected_max_v - reflected_min_v + 1; - static_assert(range_size > 0, "magic_enum::enum_range requires valid size."); - static_assert(range_size < (std::numeric_limits::max)(), "magic_enum::enum_range requires valid size."); - - return values>(std::make_index_sequence{}); -} - -template -inline constexpr auto values_v = values(); - -template > -using values_t = decltype((values_v)); - -template -inline constexpr auto count_v = values_v.size(); - -template > -inline constexpr auto min_v = static_cast(values_v.front()); - -template > -inline constexpr auto max_v = static_cast(values_v.back()); - -template > -constexpr std::size_t range_size() noexcept { - static_assert(is_enum_v, "magic_enum::detail::range_size requires enum type."); - constexpr auto max = IsFlags ? log2(max_v) : max_v; - constexpr auto min = IsFlags ? log2(min_v) : min_v; - constexpr auto range_size = max - min + U{1}; - static_assert(range_size > 0, "magic_enum::enum_range requires valid size."); - static_assert(range_size < (std::numeric_limits::max)(), "magic_enum::enum_range requires valid size."); - - return static_cast(range_size); -} - -template -inline constexpr auto range_size_v = range_size(); - -template -using index_t = std::conditional_t < (std::numeric_limits::max)(), std::uint8_t, std::uint16_t>; - -template -inline constexpr auto invalid_index_v = (std::numeric_limits>::max)(); - -template -constexpr auto indexes(std::index_sequence) noexcept { - static_assert(is_enum_v, "magic_enum::detail::indexes requires enum type."); - constexpr auto min = IsFlags ? log2(min_v) : min_v; - [[maybe_unused]] auto i = index_t{0}; - - return std::array{{(is_valid(I)>() ? i++ : invalid_index_v)...}}; -} - -template -inline constexpr auto indexes_v = indexes(std::make_index_sequence>{}); - -template -constexpr auto names(std::index_sequence) noexcept { - static_assert(is_enum_v, "magic_enum::detail::names requires enum type."); - - return std::array{{enum_name_v[I]>...}}; -} - -template -inline constexpr auto names_v = names(std::make_index_sequence>{}); - -template > -using names_t = decltype((names_v)); - -template -constexpr auto entries(std::index_sequence) noexcept { - static_assert(is_enum_v, "magic_enum::detail::entries requires enum type."); - - return std::array, sizeof...(I)>{{{values_v[I], enum_name_v[I]>}...}}; -} - -template -inline constexpr auto entries_v = entries(std::make_index_sequence>{}); - -template > -using entries_t = decltype((entries_v)); - -template > -constexpr bool is_sparse() noexcept { - static_assert(is_enum_v, "magic_enum::detail::is_sparse requires enum type."); - - return range_size_v != count_v; -} - -template -inline constexpr bool is_sparse_v = is_sparse(); - -template > -constexpr std::size_t undex(U value) noexcept { - static_assert(is_enum_v, "magic_enum::detail::undex requires enum type."); - - if (const auto i = static_cast(value - min_v); value >= min_v && value <= max_v) { - if constexpr (is_sparse_v) { - if (const auto idx = indexes_v[i]; idx != invalid_index_v) { - return idx; - } - } else { - return i; - } + if constexpr ((I + 256) < Size) { + valid_count(valid, count); } - - return invalid_index_v; // Value out of range. +#undef MAGIC_ENUM_V } -template > -constexpr std::size_t endex(E value) noexcept { - static_assert(is_enum_v, "magic_enum::detail::endex requires enum type."); - - return undex(static_cast(value)); -} - -template > -constexpr U value_ors() noexcept { - static_assert(is_enum_v, "magic_enum::detail::endex requires enum type."); - - auto value = U{0}; - for (std::size_t i = 0; i < count_v; ++i) { - value |= static_cast(values_v[i]); - } - - return value; -} - -template -struct enable_if_enum {}; - -template -struct enable_if_enum { - using type = R; - using D = std::decay_t; - static_assert(supported::value, "magic_enum unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); +template +struct valid_count_t { + std::size_t count = 0; + bool valid[N] = {}; }; -template -using enable_if_enum_t = std::enable_if_t>, R>; +template +constexpr auto valid_count() noexcept { + valid_count_t vc; + valid_count(vc.valid, vc.count); + return vc; +} -template >>> +template +constexpr auto values() noexcept { + constexpr auto vc = valid_count(); + + if constexpr (vc.count > 0) { +#if defined(MAGIC_ENUM_ARRAY_CONSTEXPR) + std::array values = {}; +#else + E values[vc.count] = {}; +#endif + for (std::size_t i = 0, v = 0; v < vc.count; ++i) { + if (vc.valid[i]) { + values[v++] = value(i); + } + } +#if defined(MAGIC_ENUM_ARRAY_CONSTEXPR) + return values; +#else + return to_array(values, std::make_index_sequence{}); +#endif + } else { + return std::array{}; + } +} + +template > +constexpr auto values() noexcept { + constexpr auto min = reflected_min(); + constexpr auto max = reflected_max(); + constexpr auto range_size = max - min + 1; + static_assert(range_size > 0, "magic_enum::enum_range requires valid size."); + + return values(); +} + +template > +constexpr enum_subtype subtype(std::true_type) noexcept { + if constexpr (std::is_same_v) { // bool special case + return enum_subtype::common; + } else if constexpr (has_is_flags::value) { + return customize::enum_range::is_flags ? enum_subtype::flags : enum_subtype::common; + } else { +#if defined(MAGIC_ENUM_AUTO_IS_FLAGS) + constexpr auto flags_values = values(); + constexpr auto default_values = values(); + if (flags_values.size() == 0 || default_values.size() > flags_values.size()) { + return enum_subtype::common; + } + for (std::size_t i = 0; i < default_values.size(); ++i) { + const auto v = static_cast(default_values[i]); + if (v != 0 && (v & (v - 1)) != 0) { + return enum_subtype::common; + } + } + return enum_subtype::flags; +#else + return enum_subtype::common; +#endif + } +} + +template +constexpr enum_subtype subtype(std::false_type) noexcept { + // For non-enum type return default common subtype. + return enum_subtype::common; +} + +template > +inline constexpr auto subtype_v = subtype(std::is_enum{}); + +template +inline constexpr auto values_v = values(); + +template > +using values_t = decltype((values_v)); + +template +inline constexpr auto count_v = values_v.size(); + +template > +inline constexpr auto min_v = (count_v > 0) ? static_cast(values_v.front()) : U{0}; + +template > +inline constexpr auto max_v = (count_v > 0) ? static_cast(values_v.back()) : U{0}; + +template +constexpr auto names(std::index_sequence) noexcept { + constexpr auto names = std::array{{enum_name_v[I]>...}}; + return names; +} + +template +inline constexpr auto names_v = names(std::make_index_sequence>{}); + +template > +using names_t = decltype((names_v)); + +template +constexpr auto entries(std::index_sequence) noexcept { + constexpr auto entries = std::array, sizeof...(I)>{{{values_v[I], enum_name_v[I]>}...}}; + return entries; +} + +template +inline constexpr auto entries_v = entries(std::make_index_sequence>{}); + +template > +using entries_t = decltype((entries_v)); + +template > +constexpr bool is_sparse() noexcept { + if constexpr (count_v == 0) { + return false; + } else if constexpr (std::is_same_v) { // bool special case + return false; + } else { + constexpr auto max = (S == enum_subtype::flags) ? log2(max_v) : max_v; + constexpr auto min = (S == enum_subtype::flags) ? log2(min_v) : min_v; + constexpr auto range_size = max - min + 1; + + return range_size != count_v; + } +} + +template > +inline constexpr bool is_sparse_v = is_sparse(); + +template +struct is_reflected +#if defined(MAGIC_ENUM_NO_CHECK_REFLECTED_ENUM) + : std::true_type {}; +#else + : std::bool_constant && (count_v != 0)> {}; +#endif + +template +inline constexpr bool is_reflected_v = is_reflected, S>{}; + +template +struct enable_if_enum {}; + +template +struct enable_if_enum { + using type = R; + static_assert(supported::value, "magic_enum unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); +}; + +template , typename D = std::decay_t> +using enable_if_t = typename enable_if_enum && std::is_invocable_r_v, R>::type; + +template >, int> = 0> using enum_concept = T; template > @@ -590,6 +909,217 @@ struct underlying_type {}; template struct underlying_type : std::underlying_type> {}; +#if defined(MAGIC_ENUM_ENABLE_HASH) || defined(MAGIC_ENUM_ENABLE_HASH_SWITCH) + +template +struct constexpr_hash_t; + +template +struct constexpr_hash_t>> { + constexpr auto operator()(Value value) const noexcept { + using U = typename underlying_type::type; + if constexpr (std::is_same_v) { // bool special case + return static_cast(value); + } else { + return static_cast(value); + } + } + using secondary_hash = constexpr_hash_t; +}; + +template +struct constexpr_hash_t>> { + static constexpr std::uint32_t crc_table[256] { + 0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L, 0x706af48fL, 0xe963a535L, 0x9e6495a3L, + 0x0edb8832L, 0x79dcb8a4L, 0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L, 0x90bf1d91L, + 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL, 0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, + 0x136c9856L, 0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L, 0xfa0f3d63L, 0x8d080df5L, + 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L, 0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL, + 0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L, 0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, + 0x26d930acL, 0x51de003aL, 0xc8d75180L, 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L, 0xb8bda50fL, + 0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L, 0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL, + 0x76dc4190L, 0x01db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, 0x06b6b51fL, 0x9fbfe4a5L, 0xe8b8d433L, + 0x7807c9a2L, 0x0f00f934L, 0x9609a88eL, 0xe10e9818L, 0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L, + 0x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL, 0x1b01a57bL, 0x8208f4c1L, 0xf50fc457L, + 0x65b0d9c6L, 0x12b7e950L, 0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L, 0xfbd44c65L, + 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L, 0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, + 0x4369e96aL, 0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L, 0xaa0a4c5fL, 0xdd0d7cc9L, + 0x5005713cL, 0x270241aaL, 0xbe0b1010L, 0xc90c2086L, 0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL, + 0x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L, 0x59b33d17L, 0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, + 0xedb88320L, 0x9abfb3b6L, 0x03b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x04db2615L, 0x73dc1683L, + 0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L, 0xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L, + 0xf00f9344L, 0x8708a3d2L, 0x1e01f268L, 0x6906c2feL, 0xf762575dL, 0x806567cbL, 0x196c3671L, 0x6e6b06e7L, + 0xfed41b76L, 0x89d32be0L, 0x10da7a5aL, 0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L, + 0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L, 0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, + 0xd80d2bdaL, 0xaf0a1b4cL, 0x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL, 0x4669be79L, + 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L, 0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL, + 0xc5ba3bbeL, 0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L, 0x2cd99e8bL, 0x5bdeae1dL, + 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL, 0x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L, + 0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL, 0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L, + 0x86d3d2d4L, 0xf1d4e242L, 0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L, 0x18b74777L, + 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL, 0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, + 0xa00ae278L, 0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L, 0x4969474dL, 0x3e6e77dbL, + 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L, 0x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L, + 0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L, 0xcdd70693L, 0x54de5729L, 0x23d967bfL, + 0xb3667a2eL, 0xc4614ab8L, 0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL, 0x2d02ef8dL + }; + constexpr std::uint32_t operator()(string_view value) const noexcept { + auto crc = static_cast(0xffffffffL); + for (const auto c : value) { + crc = (crc >> 8) ^ crc_table[(crc ^ static_cast(c)) & 0xff]; + } + return crc ^ 0xffffffffL; + } + + struct secondary_hash { + constexpr std::uint32_t operator()(string_view value) const noexcept { + auto acc = static_cast(2166136261ULL); + for (const auto c : value) { + acc = ((acc ^ static_cast(c)) * static_cast(16777619ULL)) & (std::numeric_limits::max)(); + } + return static_cast(acc); + } + }; +}; + +template +inline constexpr Hash hash_v{}; + +template +constexpr auto calculate_cases(std::size_t Page) noexcept { + constexpr std::array values = *GlobValues; + constexpr std::size_t size = values.size(); + + using switch_t = std::invoke_result_t; + static_assert(std::is_integral_v && !std::is_same_v); + const std::size_t values_to = (std::min)(static_cast(256), size - Page); + + std::array result{}; + auto fill = result.begin(); + { + auto first = values.begin() + static_cast(Page); + auto last = values.begin() + static_cast(Page + values_to); + while (first != last) { + *fill++ = hash_v(*first++); + } + } + + // dead cases, try to avoid case collisions + for (switch_t last_value = result[values_to - 1]; fill != result.end() && last_value != (std::numeric_limits::max)(); *fill++ = ++last_value) { + } + + { + auto it = result.begin(); + auto last_value = (std::numeric_limits::min)(); + for (; fill != result.end(); *fill++ = last_value++) { + while (last_value == *it) { + ++last_value, ++it; + } + } + } + + return result; +} + +template +constexpr R invoke_r(F&& f, Args&&... args) noexcept(std::is_nothrow_invocable_r_v) { + if constexpr (std::is_void_v) { + std::forward(f)(std::forward(args)...); + } else { + return static_cast(std::forward(f)(std::forward(args)...)); + } +} + +enum class case_call_t { + index, + value +}; + +template +inline constexpr auto default_result_type_lambda = []() noexcept(std::is_nothrow_default_constructible_v) { return T{}; }; + +template <> +inline constexpr auto default_result_type_lambda = []() noexcept {}; + +template +constexpr bool has_duplicate() noexcept { + using value_t = std::decay_t; + using hash_value_t = std::invoke_result_t; + std::arraysize()> hashes{}; + std::size_t size = 0; + for (auto elem : *Arr) { + hashes[size] = hash_v(elem); + for (auto i = size++; i > 0; --i) { + if (hashes[i] < hashes[i - 1]) { + auto tmp = hashes[i]; + hashes[i] = hashes[i - 1]; + hashes[i - 1] = tmp; + } else if (hashes[i] == hashes[i - 1]) { + return false; + } else { + break; + } + } + } + return true; +} + +#define MAGIC_ENUM_CASE(val) \ + case cases[val]: \ + if constexpr ((val) + Page < size) { \ + if (!pred(values[val + Page], searched)) { \ + break; \ + } \ + if constexpr (CallValue == case_call_t::index) { \ + if constexpr (std::is_invocable_r_v>) { \ + return detail::invoke_r(std::forward(lambda), std::integral_constant{}); \ + } else if constexpr (std::is_invocable_v>) { \ + MAGIC_ENUM_ASSERT(false && "magic_enum::detail::constexpr_switch wrong result type."); \ + } \ + } else if constexpr (CallValue == case_call_t::value) { \ + if constexpr (std::is_invocable_r_v>) { \ + return detail::invoke_r(std::forward(lambda), enum_constant{}); \ + } else if constexpr (std::is_invocable_r_v>) { \ + MAGIC_ENUM_ASSERT(false && "magic_enum::detail::constexpr_switch wrong result type."); \ + } \ + } \ + break; \ + } else [[fallthrough]]; + +template ::value_type>, + typename BinaryPredicate = std::equal_to<>, + typename Lambda, + typename ResultGetterType> +constexpr decltype(auto) constexpr_switch( + Lambda&& lambda, + typename std::decay_t::value_type searched, + ResultGetterType&& def, + BinaryPredicate&& pred = {}) { + using result_t = std::invoke_result_t; + using hash_t = std::conditional_t(), Hash, typename Hash::secondary_hash>; + static_assert(has_duplicate(), "magic_enum::detail::constexpr_switch duplicated hash found, please report it: https://github.com/Neargye/magic_enum/issues."); + constexpr std::array values = *GlobValues; + constexpr std::size_t size = values.size(); + constexpr std::array cases = calculate_cases(Page); + + switch (hash_v(searched)) { + MAGIC_ENUM_FOR_EACH_256(MAGIC_ENUM_CASE) + default: + if constexpr (size > 256 + Page) { + return constexpr_switch(std::forward(lambda), searched, std::forward(def)); + } + break; + } + return def(); +} + +#undef MAGIC_ENUM_CASE + +#endif + } // namespace magic_enum::detail // Checks is magic_enum supported compiler. @@ -622,478 +1152,309 @@ struct underlying_type : detail::underlying_type {}; template using underlying_type_t = typename underlying_type::type; +template +using enum_constant = detail::enum_constant; + // Returns type name of enum. template -[[nodiscard]] constexpr auto enum_type_name() noexcept -> detail::enable_if_enum_t { - using D = std::decay_t; - constexpr string_view name = detail::type_name_v; - static_assert(name.size() > 0, "Enum type does not have a name."); +[[nodiscard]] constexpr auto enum_type_name() noexcept -> detail::enable_if_t { + constexpr string_view name = detail::type_name_v>; + static_assert(!name.empty(), "magic_enum::enum_type_name enum type does not have a name."); return name; } // Returns number of enum values. -template -[[nodiscard]] constexpr auto enum_count() noexcept -> detail::enable_if_enum_t { - using D = std::decay_t; - - return detail::count_v; +template > +[[nodiscard]] constexpr auto enum_count() noexcept -> detail::enable_if_t { + return detail::count_v, S>; } // Returns enum value at specified index. // No bounds checking is performed: the behavior is undefined if index >= number of enum values. -template -[[nodiscard]] constexpr auto enum_value(std::size_t index) noexcept -> detail::enable_if_enum_t> { +template > +[[nodiscard]] constexpr auto enum_value(std::size_t index) noexcept -> detail::enable_if_t> { using D = std::decay_t; - static_assert(detail::count_v > 0, "magic_enum requires enum implementation and valid max and min."); + static_assert(detail::is_reflected_v, "magic_enum requires enum implementation and valid max and min."); - if constexpr (detail::is_sparse_v) { - return assert((index < detail::count_v)), detail::values_v[index]; + if constexpr (detail::is_sparse_v) { + return MAGIC_ENUM_ASSERT(index < detail::count_v), detail::values_v[index]; } else { - return assert((index < detail::count_v)), detail::value>(index); + constexpr auto min = (S == detail::enum_subtype::flags) ? detail::log2(detail::min_v) : detail::min_v; + + return MAGIC_ENUM_ASSERT(index < detail::count_v), detail::value(index); } } -// Returns std::array with enum values, sorted by enum value. -template -[[nodiscard]] constexpr auto enum_values() noexcept -> detail::enable_if_enum_t> { +// Returns enum value at specified index. +template > +[[nodiscard]] constexpr auto enum_value() noexcept -> detail::enable_if_t> { using D = std::decay_t; - static_assert(detail::count_v > 0, "magic_enum requires enum implementation and valid max and min."); + static_assert(detail::is_reflected_v, "magic_enum requires enum implementation and valid max and min."); + static_assert(I < detail::count_v, "magic_enum::enum_value out of range."); - return detail::values_v; + return enum_value(I); +} + +// Returns std::array with enum values, sorted by enum value. +template > +[[nodiscard]] constexpr auto enum_values() noexcept -> detail::enable_if_t> { + using D = std::decay_t; + static_assert(detail::is_reflected_v, "magic_enum requires enum implementation and valid max and min."); + + return detail::values_v; +} + +// Returns integer value from enum value. +template +[[nodiscard]] constexpr auto enum_integer(E value) noexcept -> detail::enable_if_t> { + return static_cast>(value); +} + +// Returns underlying value from enum value. +template +[[nodiscard]] constexpr auto enum_underlying(E value) noexcept -> detail::enable_if_t> { + return static_cast>(value); +} + +// Obtains index in enum values from enum value. +// Returns optional with index. +template > +[[nodiscard]] constexpr auto enum_index(E value) noexcept -> detail::enable_if_t> { + using D = std::decay_t; + using U = underlying_type_t; + static_assert(detail::is_reflected_v, "magic_enum requires enum implementation and valid max and min."); + + if constexpr (detail::is_sparse_v || (S == detail::enum_subtype::flags)) { +#if defined(MAGIC_ENUM_ENABLE_HASH) + return detail::constexpr_switch<&detail::values_v, detail::case_call_t::index>( + [](std::size_t i) { return optional{i}; }, + value, + detail::default_result_type_lambda>); +#else + for (std::size_t i = 0; i < detail::count_v; ++i) { + if (enum_value(i) == value) { + return i; + } + } + return {}; // Invalid value or out of range. +#endif + } else { + const auto v = static_cast(value); + if (v >= detail::min_v && v <= detail::max_v) { + return static_cast(v - detail::min_v); + } + return {}; // Invalid value or out of range. + } +} + +// Obtains index in enum values from enum value. +// Returns optional with index. +template +[[nodiscard]] constexpr auto enum_index(E value) noexcept -> detail::enable_if_t> { + using D = std::decay_t; + static_assert(detail::is_reflected_v, "magic_enum requires enum implementation and valid max and min."); + + return enum_index(value); +} + +// Obtains index in enum values from static storage enum variable. +template >> +[[nodiscard]] constexpr auto enum_index() noexcept -> detail::enable_if_t {\ + using D = std::decay_t; + static_assert(detail::is_reflected_v, "magic_enum requires enum implementation and valid max and min."); + constexpr auto index = enum_index(V); + static_assert(index, "magic_enum::enum_index enum value does not have a index."); + + return *index; } // Returns name from static storage enum variable. // This version is much lighter on the compile times and is not restricted to the enum_range limitation. template -[[nodiscard]] constexpr auto enum_name() noexcept -> detail::enable_if_enum_t { - using D = std::decay_t; - constexpr string_view name = detail::enum_name_v; - static_assert(name.size() > 0, "Enum value does not have a name."); +[[nodiscard]] constexpr auto enum_name() noexcept -> detail::enable_if_t { + constexpr string_view name = detail::enum_name_v, V>; + static_assert(!name.empty(), "magic_enum::enum_name enum value does not have a name."); return name; } // Returns name from enum value. // If enum value does not have name or value out of range, returns empty string. -template -[[nodiscard]] constexpr auto enum_name(E value) noexcept -> detail::enable_if_enum_t { +template > +[[nodiscard]] constexpr auto enum_name(E value) noexcept -> detail::enable_if_t { using D = std::decay_t; + static_assert(detail::is_reflected_v, "magic_enum requires enum implementation and valid max and min."); - if (const auto i = detail::endex(value); i != detail::invalid_index_v) { - return detail::names_v[i]; + if (const auto i = enum_index(value)) { + return detail::names_v[*i]; } + return {}; +} - return {}; // Invalid value or out of range. +// Returns name from enum value. +// If enum value does not have name or value out of range, returns empty string. +template +[[nodiscard]] constexpr auto enum_name(E value) -> detail::enable_if_t { + using D = std::decay_t; + static_assert(detail::is_reflected_v, "magic_enum requires enum implementation and valid max and min."); + + return enum_name(value); } // Returns std::array with names, sorted by enum value. -template -[[nodiscard]] constexpr auto enum_names() noexcept -> detail::enable_if_enum_t> { +template > +[[nodiscard]] constexpr auto enum_names() noexcept -> detail::enable_if_t> { using D = std::decay_t; - static_assert(detail::count_v > 0, "magic_enum requires enum implementation and valid max and min."); + static_assert(detail::is_reflected_v, "magic_enum requires enum implementation and valid max and min."); - return detail::names_v; + return detail::names_v; } // Returns std::array with pairs (value, name), sorted by enum value. -template -[[nodiscard]] constexpr auto enum_entries() noexcept -> detail::enable_if_enum_t> { +template > +[[nodiscard]] constexpr auto enum_entries() noexcept -> detail::enable_if_t> { using D = std::decay_t; - static_assert(detail::count_v > 0, "magic_enum requires enum implementation and valid max and min."); + static_assert(detail::is_reflected_v, "magic_enum requires enum implementation and valid max and min."); - return detail::entries_v; + return detail::entries_v; } +// Allows you to write magic_enum::enum_cast("bar", magic_enum::case_insensitive); +inline constexpr auto case_insensitive = detail::case_insensitive<>{}; + // Obtains enum value from integer value. // Returns optional with enum value. -template -[[nodiscard]] constexpr auto enum_cast(underlying_type_t value) noexcept -> detail::enable_if_enum_t>> { +template > +[[nodiscard]] constexpr auto enum_cast(underlying_type_t value) noexcept -> detail::enable_if_t>> { using D = std::decay_t; + static_assert(detail::is_reflected_v, "magic_enum requires enum implementation and valid max and min."); - if (detail::undex(value) != detail::invalid_index_v) { - return static_cast(value); + if constexpr (detail::is_sparse_v || (S == detail::enum_subtype::flags)) { +#if defined(MAGIC_ENUM_ENABLE_HASH) + return detail::constexpr_switch<&detail::values_v, detail::case_call_t::value>( + [](D v) { return optional{v}; }, + static_cast(value), + detail::default_result_type_lambda>); +#else + for (std::size_t i = 0; i < detail::count_v; ++i) { + if (value == static_cast>(enum_value(i))) { + return static_cast(value); + } + } + return {}; // Invalid value or out of range. +#endif + } else { + if (value >= detail::min_v && value <= detail::max_v) { + return static_cast(value); + } + return {}; // Invalid value or out of range. } - - return {}; // Invalid value or out of range. } // Obtains enum value from name. // Returns optional with enum value. -template -[[nodiscard]] constexpr auto enum_cast(string_view value, BinaryPredicate p) noexcept(std::is_nothrow_invocable_r_v) -> detail::enable_if_enum_t>> { - static_assert(std::is_invocable_r_v, "magic_enum::enum_cast requires bool(char, char) invocable predicate."); +template , typename BinaryPredicate = std::equal_to<>> +[[nodiscard]] constexpr auto enum_cast(string_view value, [[maybe_unused]] BinaryPredicate p = {}) noexcept(detail::is_nothrow_invocable()) -> detail::enable_if_t>, BinaryPredicate> { using D = std::decay_t; + static_assert(detail::is_reflected_v, "magic_enum requires enum implementation and valid max and min."); - for (std::size_t i = 0; i < detail::count_v; ++i) { - if (detail::cmp_equal(value, detail::names_v[i], p)) { - return enum_value(i); - } - } - - return {}; // Invalid value or out of range. -} - -// Obtains enum value from name. -// Returns optional with enum value. -template -[[nodiscard]] constexpr auto enum_cast(string_view value) noexcept -> detail::enable_if_enum_t>> { - using D = std::decay_t; - - return enum_cast(value, detail::char_equal_to{}); -} - -// Returns integer value from enum value. -template -[[nodiscard]] constexpr auto enum_integer(E value) noexcept -> detail::enable_if_enum_t> { - return static_cast>(value); -} - -// Obtains index in enum values from enum value. -// Returns optional with index. -template -[[nodiscard]] constexpr auto enum_index(E value) noexcept -> detail::enable_if_enum_t> { - using D = std::decay_t; - - if (const auto i = detail::endex(value); i != detail::invalid_index_v) { - return i; - } - - return {}; // Invalid value or out of range. -} - -// Checks whether enum contains enumerator with such enum value. -template -[[nodiscard]] constexpr auto enum_contains(E value) noexcept -> detail::enable_if_enum_t { - using D = std::decay_t; - - return detail::endex(value) != detail::invalid_index_v; -} - -// Checks whether enum contains enumerator with such integer value. -template -[[nodiscard]] constexpr auto enum_contains(underlying_type_t value) noexcept -> detail::enable_if_enum_t { - using D = std::decay_t; - - return detail::undex(value) != detail::invalid_index_v; -} - -// Checks whether enum contains enumerator with such name. -template -[[nodiscard]] constexpr auto enum_contains(string_view value, BinaryPredicate p) noexcept(std::is_nothrow_invocable_r_v) -> detail::enable_if_enum_t { - using D = std::decay_t; - static_assert(std::is_invocable_r_v, "magic_enum::enum_contains requires bool(char, char) invocable predicate."); - - return enum_cast(value, std::move_if_noexcept(p)).has_value(); -} - -// Checks whether enum contains enumerator with such name. -template -[[nodiscard]] constexpr auto enum_contains(string_view value) noexcept -> detail::enable_if_enum_t { - using D = std::decay_t; - - return enum_cast(value).has_value(); -} - -namespace ostream_operators { - -template , int> = 0> -std::basic_ostream& operator<<(std::basic_ostream& os, E value) { - using D = std::decay_t; - using U = underlying_type_t; -#if defined(MAGIC_ENUM_SUPPORTED) && MAGIC_ENUM_SUPPORTED - if (const auto name = magic_enum::enum_name(value); !name.empty()) { - for (const auto c : name) { - os.put(c); - } - return os; +#if defined(MAGIC_ENUM_ENABLE_HASH) + if constexpr (detail::is_default_predicate()) { + return detail::constexpr_switch<&detail::names_v, detail::case_call_t::index>( + [](std::size_t i) { return optional{detail::values_v[i]}; }, + value, + detail::default_result_type_lambda>, + [&p](string_view lhs, string_view rhs) { return detail::cmp_equal(lhs, rhs, p); }); } #endif - return (os << static_cast(value)); + for (std::size_t i = 0; i < detail::count_v; ++i) { + if (detail::cmp_equal(value, detail::names_v[i], p)) { + return enum_value(i); + } + } + return {}; // Invalid value or out of range. } -template , int> = 0> -std::basic_ostream& operator<<(std::basic_ostream& os, optional value) { - return value.has_value() ? (os << value.value()) : os; +// Checks whether enum contains value with such value. +template > +[[nodiscard]] constexpr auto enum_contains(E value) noexcept -> detail::enable_if_t { + using D = std::decay_t; + using U = underlying_type_t; + + return static_cast(enum_cast(static_cast(value))); } -} // namespace magic_enum::ostream_operators +// Checks whether enum contains value with such value. +template +[[nodiscard]] constexpr auto enum_contains(E value) noexcept -> detail::enable_if_t { + using D = std::decay_t; + using U = underlying_type_t; + + return static_cast(enum_cast(static_cast(value))); +} + +// Checks whether enum contains value with such integer value. +template > +[[nodiscard]] constexpr auto enum_contains(underlying_type_t value) noexcept -> detail::enable_if_t { + using D = std::decay_t; + + return static_cast(enum_cast(value)); +} + +// Checks whether enum contains enumerator with such name. +template , typename BinaryPredicate = std::equal_to<>> +[[nodiscard]] constexpr auto enum_contains(string_view value, BinaryPredicate p = {}) noexcept(detail::is_nothrow_invocable()) -> detail::enable_if_t { + using D = std::decay_t; + + return static_cast(enum_cast(value, std::move(p))); +} + +template +inline constexpr auto as_flags = AsFlags ? detail::enum_subtype::flags : detail::enum_subtype::common; + +template +inline constexpr auto as_common = AsFlags ? detail::enum_subtype::common : detail::enum_subtype::flags; namespace bitwise_operators { -template , int> = 0> +template = 0> constexpr E operator~(E rhs) noexcept { return static_cast(~static_cast>(rhs)); } -template , int> = 0> +template = 0> constexpr E operator|(E lhs, E rhs) noexcept { return static_cast(static_cast>(lhs) | static_cast>(rhs)); } -template , int> = 0> +template = 0> constexpr E operator&(E lhs, E rhs) noexcept { return static_cast(static_cast>(lhs) & static_cast>(rhs)); } -template , int> = 0> +template = 0> constexpr E operator^(E lhs, E rhs) noexcept { return static_cast(static_cast>(lhs) ^ static_cast>(rhs)); } -template , int> = 0> +template = 0> constexpr E& operator|=(E& lhs, E rhs) noexcept { return lhs = (lhs | rhs); } -template , int> = 0> +template = 0> constexpr E& operator&=(E& lhs, E rhs) noexcept { return lhs = (lhs & rhs); } -template , int> = 0> +template = 0> constexpr E& operator^=(E& lhs, E rhs) noexcept { return lhs = (lhs ^ rhs); } } // namespace magic_enum::bitwise_operators -namespace flags { - -// Returns type name of enum. -using magic_enum::enum_type_name; - -// Returns number of enum-flags values. -template -[[nodiscard]] constexpr auto enum_count() noexcept -> detail::enable_if_enum_t { - using D = std::decay_t; - - return detail::count_v; -} - -// Returns enum-flags value at specified index. -// No bounds checking is performed: the behavior is undefined if index >= number of enum-flags values. -template -[[nodiscard]] constexpr auto enum_value(std::size_t index) noexcept -> detail::enable_if_enum_t> { - using D = std::decay_t; - static_assert(detail::count_v > 0, "magic_enum::flags requires enum-flags implementation."); - - if constexpr (detail::is_sparse_v) { - return assert((index < detail::count_v)), detail::values_v[index]; - } else { - constexpr auto min = detail::log2(detail::min_v); - - return assert((index < detail::count_v)), detail::value(index); - } -} - -// Returns std::array with enum-flags values, sorted by enum-flags value. -template -[[nodiscard]] constexpr auto enum_values() noexcept -> detail::enable_if_enum_t> { - using D = std::decay_t; - static_assert(detail::count_v > 0, "magic_enum::flags requires enum-flags implementation."); - - return detail::values_v; -} - -// Returns name from enum-flags value. -// If enum-flags value does not have name or value out of range, returns empty string. -template -[[nodiscard]] auto enum_name(E value) -> detail::enable_if_enum_t { - using D = std::decay_t; - using U = underlying_type_t; - - string name; - auto check_value = U{0}; - for (std::size_t i = 0; i < detail::count_v; ++i) { - if (const auto v = static_cast(enum_value(i)); (static_cast(value) & v) != 0) { - check_value |= v; - const auto n = detail::names_v[i]; - if (!name.empty()) { - name.append(1, '|'); - } - name.append(n.data(), n.size()); - } - } - - if (check_value != 0 && check_value == static_cast(value)) { - return name; - } - - return {}; // Invalid value or out of range. -} - -// Returns std::array with string names, sorted by enum-flags value. -template -[[nodiscard]] constexpr auto enum_names() noexcept -> detail::enable_if_enum_t> { - using D = std::decay_t; - static_assert(detail::count_v > 0, "magic_enum::flags requires enum-flags implementation."); - - return detail::names_v; -} - -// Returns std::array with pairs (value, name), sorted by enum-flags value. -template -[[nodiscard]] constexpr auto enum_entries() noexcept -> detail::enable_if_enum_t> { - using D = std::decay_t; - static_assert(detail::count_v > 0, "magic_enum::flags requires enum-flags implementation."); - - return detail::entries_v; -} - -// Obtains enum-flags value from integer value. -// Returns optional with enum-flags value. -template -[[nodiscard]] constexpr auto enum_cast(underlying_type_t value) noexcept -> detail::enable_if_enum_t>> { - using D = std::decay_t; - using U = underlying_type_t; - - if constexpr (detail::is_sparse_v) { - auto check_value = U{0}; - for (std::size_t i = 0; i < detail::count_v; ++i) { - if (const auto v = static_cast(enum_value(i)); (value & v) != 0) { - check_value |= v; - } - } - - if (check_value != 0 && check_value == value) { - return static_cast(value); - } - } else { - constexpr auto min = detail::min_v; - constexpr auto max = detail::value_ors(); - - if (value >= min && value <= max) { - return static_cast(value); - } - } - - return {}; // Invalid value or out of range. -} - -// Obtains enum-flags value from name. -// Returns optional with enum-flags value. -template -[[nodiscard]] constexpr auto enum_cast(string_view value, BinaryPredicate p) noexcept(std::is_nothrow_invocable_r_v) -> detail::enable_if_enum_t>> { - static_assert(std::is_invocable_r_v, "magic_enum::flags::enum_cast requires bool(char, char) invocable predicate."); - using D = std::decay_t; - using U = underlying_type_t; - - auto result = U{0}; - while (!value.empty()) { - const auto d = detail::find(value, '|'); - const auto s = (d == string_view::npos) ? value : value.substr(0, d); - auto f = U{0}; - for (std::size_t i = 0; i < detail::count_v; ++i) { - if (detail::cmp_equal(s, detail::names_v[i], p)) { - f = static_cast(enum_value(i)); - result |= f; - break; - } - } - if (f == U{0}) { - return {}; // Invalid value or out of range. - } - value.remove_prefix((d == string_view::npos) ? value.size() : d + 1); - } - - if (result == U{0}) { - return {}; // Invalid value or out of range. - } else { - return static_cast(result); - } -} - -// Obtains enum-flags value from name. -// Returns optional with enum-flags value. -template -[[nodiscard]] constexpr auto enum_cast(string_view value) noexcept -> detail::enable_if_enum_t>> { - using D = std::decay_t; - - return enum_cast(value, detail::char_equal_to{}); -} - -// Returns integer value from enum value. -using magic_enum::enum_integer; - -// Obtains index in enum-flags values from enum-flags value. -// Returns optional with index. -template -[[nodiscard]] constexpr auto enum_index(E value) noexcept -> detail::enable_if_enum_t> { - using D = std::decay_t; - using U = underlying_type_t; - - if (detail::is_pow2(static_cast(value))) { - for (std::size_t i = 0; i < detail::count_v; ++i) { - if (enum_value(i) == value) { - return i; - } - } - } - - return {}; // Invalid value or out of range. -} - -// Checks whether enum-flags contains enumerator with such enum-flags value. -template -[[nodiscard]] constexpr auto enum_contains(E value) noexcept -> detail::enable_if_enum_t { - using D = std::decay_t; - using U = underlying_type_t; - - return enum_cast(static_cast(value)).has_value(); -} - -// Checks whether enum-flags contains enumerator with such integer value. -template -[[nodiscard]] constexpr auto enum_contains(underlying_type_t value) noexcept -> detail::enable_if_enum_t { - using D = std::decay_t; - - return enum_cast(value).has_value(); -} - -// Checks whether enum-flags contains enumerator with such name. -template -[[nodiscard]] constexpr auto enum_contains(string_view value, BinaryPredicate p) noexcept(std::is_nothrow_invocable_r_v) -> detail::enable_if_enum_t { - static_assert(std::is_invocable_r_v, "magic_enum::flags::enum_contains requires bool(char, char) invocable predicate."); - using D = std::decay_t; - - return enum_cast(value, std::move_if_noexcept(p)).has_value(); -} - -// Checks whether enum-flags contains enumerator with such name. -template -[[nodiscard]] constexpr auto enum_contains(string_view value) noexcept -> detail::enable_if_enum_t { - using D = std::decay_t; - - return enum_cast(value).has_value(); -} - -} // namespace magic_enum::flags - -namespace flags::ostream_operators { - -template , int> = 0> -std::basic_ostream& operator<<(std::basic_ostream& os, E value) { - using D = std::decay_t; - using U = underlying_type_t; -#if defined(MAGIC_ENUM_SUPPORTED) && MAGIC_ENUM_SUPPORTED - if (const auto name = magic_enum::flags::enum_name(value); !name.empty()) { - for (const auto c : name) { - os.put(c); - } - return os; - } -#endif - return (os << static_cast(value)); -} - -template , int> = 0> -std::basic_ostream& operator<<(std::basic_ostream& os, optional value) { - return value.has_value() ? (os << value.value()) : os; -} - -} // namespace magic_enum::flags::ostream_operators - } // namespace magic_enum #if defined(__clang__) @@ -1104,4 +1465,10 @@ std::basic_ostream& operator<<(std::basic_ostream& o # pragma warning(pop) #endif +#undef MAGIC_ENUM_GET_ENUM_NAME_BUILTIN +#undef MAGIC_ENUM_GET_TYPE_NAME_BUILTIN +#undef MAGIC_ENUM_VS_2017_WORKAROUND +#undef MAGIC_ENUM_ARRAY_CONSTEXPR +#undef MAGIC_ENUM_FOR_EACH_256 + #endif // NEARGYE_MAGIC_ENUM_HPP diff --git a/source/emulator/src/Audio.cpp b/source/emulator/src/Audio.cpp index 19b1bc0..ecc5fb7 100644 --- a/source/emulator/src/Audio.cpp +++ b/source/emulator/src/Audio.cpp @@ -1649,7 +1649,7 @@ int KYTY_SYSV_ABI Ngs2SystemCreateWithAllocator(const Ngs2SystemOption* option, printf("\t sample_rate = %u\n", option->sample_rate); printf("\t alloc_handler = 0x%016" PRIx64 "\n", reinterpret_cast(allocator->alloc_handler)); printf("\t free_handler = 0x%016" PRIx64 "\n", reinterpret_cast(allocator->free_handler)); - printf("\t user_data = 0x%016" PRIx64 "\n", reinterpret_cast(allocator->user_data)); + //printf("\t user_data = 0x%016" PRIx64 "\n", reinterpret_cast(allocator->user_data)); Ngs2ContextBufferInfo buf {}; buf.host_buffer = nullptr; @@ -1697,7 +1697,7 @@ int KYTY_SYSV_ABI Ngs2RackCreate(uintptr_t system_handle, uint32_t rack_id, cons printf("\t max_matrices = %u\n", option->max_matrices); printf("\t max_ports = %u\n", option->max_ports); printf("\t host_buffer = 0x%016" PRIx64 "\n", reinterpret_cast(buffer_info->host_buffer)); - printf("\t host_buffer_size = 0x%016" PRIx64 "\n", reinterpret_cast(buffer_info->host_buffer_size)); + //printf("\t host_buffer_size = 0x%016" PRIx64 "\n", reinterpret_cast(buffer_info->host_buffer_size)); auto* ngs = reinterpret_cast(system_handle); auto* rack = static_cast(buffer_info->host_buffer); @@ -1779,7 +1779,7 @@ int KYTY_SYSV_ABI Ngs2RackCreateWithAllocator(uintptr_t system_handle, uint32_t printf("\t max_ports = %u\n", option->max_ports); printf("\t alloc_handler = 0x%016" PRIx64 "\n", reinterpret_cast(allocator->alloc_handler)); printf("\t free_handler = 0x%016" PRIx64 "\n", reinterpret_cast(allocator->free_handler)); - printf("\t user_data = 0x%016" PRIx64 "\n", reinterpret_cast(allocator->user_data)); + //printf("\t user_data = 0x%016" PRIx64 "\n", reinterpret_cast(allocator->user_data)); Ngs2ContextBufferInfo buf {}; buf.host_buffer = nullptr; diff --git a/source/emulator/src/Kernel/Memory.cpp b/source/emulator/src/Kernel/Memory.cpp index 4da59a9..fcb108e 100644 --- a/source/emulator/src/Kernel/Memory.cpp +++ b/source/emulator/src/Kernel/Memory.cpp @@ -847,7 +847,7 @@ int KYTY_SYSV_ABI KernelMprotect(const void* addr, size_t len, int prot) auto vaddr = reinterpret_cast(addr); printf("\t addr = 0x%016" PRIx64 "\n", vaddr); - printf("\t len = 0x%016" PRIx64 "\n", reinterpret_cast(len)); + //printf("\t len = 0x%016" PRIx64 "\n", reinterpret_cast(len)); VirtualMemory::Mode mode = VirtualMemory::Mode::NoAccess; Graphics::GpuMemoryMode gpu_mode = Graphics::GpuMemoryMode::NoAccess; diff --git a/source/emulator/src/Kernel/Pthread.cpp b/source/emulator/src/Kernel/Pthread.cpp index b88319d..646295b 100644 --- a/source/emulator/src/Kernel/Pthread.cpp +++ b/source/emulator/src/Kernel/Pthread.cpp @@ -27,7 +27,7 @@ #include -#if KYTY_PLATFORM == KYTY_PLATFORM_WINDOWS +#if defined(__WIN64__) #include #endif @@ -380,7 +380,7 @@ static void pthread_attr_dbg_print(const PthreadAttr* src) printf("\tsched_priority = %d\n", param.sched_priority); printf("\tpolicy = %d\n", policy); printf("\tstack_addr = 0x%016" PRIx64 "\n", reinterpret_cast(stack_addr)); - printf("\tstack_size = %" PRIu64 "\n", reinterpret_cast(stack_size)); + //printf("\tstack_size = %" PRIu64 "\n", reinterpret_cast(stack_size)); } static void usec_to_timespec(struct timespec* ts, KernelUseconds usec) @@ -1434,7 +1434,7 @@ int KYTY_SYSV_ABI PthreadRwlockTimedrdlock(PthreadRwlock* rwlock, KernelUseconds timespec t {}; usec_to_timespec(&t, usec); - int result = pthread_rwlock_timedrdlock(&(*rwlock)->p, &t); + int result = pthread_rwlock_timedrdlock(&(*rwlock)->p,&t); // printf("\trwlock timedrdlock: %s, %d\n", (*rwlock)->name.C_Str(), result); @@ -1462,7 +1462,8 @@ int KYTY_SYSV_ABI PthreadRwlockTimedwrlock(PthreadRwlock* rwlock, KernelUseconds timespec t {}; usec_to_timespec(&t, usec); - int result = pthread_rwlock_timedwrlock(&(*rwlock)->p, &t); + + int result = pthread_rwlock_timedrdlock(&(*rwlock)->p,&t); // printf("\trwlock timedwrlock: %s, %d\n", (*rwlock)->name.C_Str(), result); @@ -1480,12 +1481,12 @@ int KYTY_SYSV_ABI PthreadRwlockTryrdlock(PthreadRwlock* rwlock) { PRINT_NAME(); - if (rwlock == nullptr) + if (rwlock == 0) { return KERNEL_ERROR_EINVAL; } - EXIT_NOT_IMPLEMENTED(*rwlock == nullptr); + EXIT_NOT_IMPLEMENTED(*rwlock == 0); int result = pthread_rwlock_tryrdlock(&(*rwlock)->p); diff --git a/source/include/Kyty/Sys/MacOS/SysMacOSDbg.h b/source/include/Kyty/Sys/MacOS/SysMacOSDbg.h index 57a6649..355278a 100644 --- a/source/include/Kyty/Sys/MacOS/SysMacOSDbg.h +++ b/source/include/Kyty/Sys/MacOS/SysMacOSDbg.h @@ -3,9 +3,7 @@ // IWYU pragma: private -#if KYTY_PLATFORM != KYTY_PLATFORM_MACOS -//#error "KYTY_PLATFORM != KYTY_PLATFORM_MACOS" -#else +#if defined(__APPLE__) namespace Kyty { diff --git a/source/include/Kyty/Sys/MacOS/SysMacOSFileIO.h b/source/include/Kyty/Sys/MacOS/SysMacOSFileIO.h index 848ca82..5d30cda 100644 --- a/source/include/Kyty/Sys/MacOS/SysMacOSFileIO.h +++ b/source/include/Kyty/Sys/MacOS/SysMacOSFileIO.h @@ -3,9 +3,7 @@ // IWYU pragma: private -#if KYTY_PLATFORM != KYTY_PLATFORM_MACOS -//#error "KYTY_PLATFORM != KYTY_PLATFORM_MACOS" -#else +#if defined(__APPLE__) #include "Kyty/Core/String.h" #include "Kyty/Sys/MacOS/SysMacOSTimer.h" diff --git a/source/include/Kyty/Sys/MacOS/SysMacOSHeap.h b/source/include/Kyty/Sys/MacOS/SysMacOSHeap.h index 38cb46c..8efa4b2 100644 --- a/source/include/Kyty/Sys/MacOS/SysMacOSHeap.h +++ b/source/include/Kyty/Sys/MacOS/SysMacOSHeap.h @@ -3,9 +3,7 @@ // IWYU pragma: private -#if KYTY_PLATFORM != KYTY_PLATFORM_MACOS -//#error "KYTY_PLATFORM != KYTY_PLATFORM_MACOS" -#else +#if defined(__APPLE__) #include "Kyty/Sys/MacOS/SysMacOSSync.h" @@ -15,12 +13,12 @@ using sys_heap_id_t = SysCS*; inline sys_heap_id_t sys_heap_create() { - return nullptr; + return 0; } inline sys_heap_id_t sys_heap_deafult() { - return nullptr; + return 0; } inline void* sys_heap_alloc(sys_heap_id_t /*heap_id*/, size_t size) @@ -28,7 +26,7 @@ inline void* sys_heap_alloc(sys_heap_id_t /*heap_id*/, size_t size) // NOLINTNEXTLINE(cppcoreguidelines-no-malloc,hicpp-no-malloc) void* m = malloc(size); - EXIT_IF(m == nullptr); + EXIT_IF(m == 0); return m; } @@ -36,11 +34,11 @@ inline void* sys_heap_alloc(sys_heap_id_t /*heap_id*/, size_t size) inline void* sys_heap_realloc(sys_heap_id_t /*heap_id*/, void* p, size_t size) { // NOLINTNEXTLINE(cppcoreguidelines-no-malloc,hicpp-no-malloc) - void* m = p != nullptr ? realloc(p, size) : malloc(size); + void* m = p != 0 ? realloc(p, size) : malloc(size); - if (m == nullptr) + if (m == 0) { - EXIT_IF(m == nullptr); + EXIT_IF(m == 0); } return m; @@ -68,7 +66,7 @@ inline void* sys_heap_alloc_s(sys_heap_id_t heap_id, size_t size) heap_id->Leave(); - EXIT_IF(m == nullptr); + EXIT_IF(m == 0); return m; } @@ -78,11 +76,11 @@ inline void* sys_heap_realloc_s(sys_heap_id_t heap_id, void* p, size_t size) heap_id->Enter(); // NOLINTNEXTLINE(cppcoreguidelines-no-malloc,hicpp-no-malloc) - void* m = p != nullptr ? realloc(p, size) : malloc(size); + void* m = p != 0 ? realloc(p, size) : malloc(size); heap_id->Leave(); - EXIT_IF(m == nullptr); + EXIT_IF(m == 0); return m; } diff --git a/source/include/Kyty/Sys/MacOS/SysMacOSStdio.h b/source/include/Kyty/Sys/MacOS/SysMacOSStdio.h index c3eeb29..ea89e3e 100644 --- a/source/include/Kyty/Sys/MacOS/SysMacOSStdio.h +++ b/source/include/Kyty/Sys/MacOS/SysMacOSStdio.h @@ -3,9 +3,7 @@ // IWYU pragma: private -#if KYTY_PLATFORM != KYTY_PLATFORM_MACOS -//#error "KYTY_PLATFORM != KYTY_PLATFORM_MACOS" -#else +#if defined(__APPLE__) //#include #include diff --git a/source/include/Kyty/Sys/MacOS/SysMacOSStdlib.h b/source/include/Kyty/Sys/MacOS/SysMacOSStdlib.h index c24f89f..ff56911 100644 --- a/source/include/Kyty/Sys/MacOS/SysMacOSStdlib.h +++ b/source/include/Kyty/Sys/MacOS/SysMacOSStdlib.h @@ -3,9 +3,7 @@ // IWYU pragma: private -#if KYTY_PLATFORM != KYTY_PLATFORM_MACOS -//#error "KYTY_PLATFORM != KYTY_PLATFORM_MACOS" -#else +#if defined(__APPLE__) namespace Kyty { diff --git a/source/include/Kyty/Sys/MacOS/SysMacOSSync.h b/source/include/Kyty/Sys/MacOS/SysMacOSSync.h index 2e6fc70..40b416b 100644 --- a/source/include/Kyty/Sys/MacOS/SysMacOSSync.h +++ b/source/include/Kyty/Sys/MacOS/SysMacOSSync.h @@ -3,9 +3,7 @@ // IWYU pragma: private -#if KYTY_PLATFORM != KYTY_PLATFORM_MACOS -//#error "KYTY_PLATFORM != KYTY_PLATFORM_MACOS" -#else +#if defined(__APPLE__) #include #include @@ -24,7 +22,7 @@ public: m_check_ptr = this; pthread_mutexattr_t attr {}; - pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP); + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); pthread_mutex_init(&m_mutex, &attr); pthread_mutexattr_destroy(&attr); } diff --git a/source/include/Kyty/Sys/MacOS/SysMacOSTimer.h b/source/include/Kyty/Sys/MacOS/SysMacOSTimer.h index 2404c94..1722095 100644 --- a/source/include/Kyty/Sys/MacOS/SysMacOSTimer.h +++ b/source/include/Kyty/Sys/MacOS/SysMacOSTimer.h @@ -3,7 +3,7 @@ // IWYU pragma: private -#if __APPLE__ +#if defined(__APPLE__) #include @@ -51,7 +51,7 @@ inline void sys_file_to_system_time_utc(const SysFileTimeStruct& f, SysTimeStruc inline void sys_time_t_to_system(time_t t, SysTimeStruct& s) { - SysTimeStruct ft {}; + SysFileTimeStruct ft {}; ft.time = t; ft.is_invalid = false; sys_file_to_system_time_utc(ft, s); diff --git a/source/include/Kyty/Sys/MacOS/SysMacOSVirtual.h b/source/include/Kyty/Sys/MacOS/SysMacOSVirtual.h index e3cf79a..b186809 100644 --- a/source/include/Kyty/Sys/MacOS/SysMacOSVirtual.h +++ b/source/include/Kyty/Sys/MacOS/SysMacOSVirtual.h @@ -1,9 +1,7 @@ #ifndef INCLUDE_KYTY_SYS_MACOS_SYSLINUXVIRTUAL_H_ #define INCLUDE_KYTY_SYS_MACOS_SYSLINUXVIRTUAL_H_ -#if KYTY_PLATFORM != KYTY_PLATFORM_MACOS -//#error "KYTY_PLATFORM != KYTY_PLATFORM_MACOS" -#else +#if defined(__APPLE__) #include "Kyty/Core/Common.h" #include "Kyty/Core/String.h" diff --git a/source/lib/Core/src/File.cpp b/source/lib/Core/src/File.cpp index 69c9c86..14c50fd 100644 --- a/source/lib/Core/src/File.cpp +++ b/source/lib/Core/src/File.cpp @@ -927,7 +927,6 @@ void File::Write(const ByteBuffer& buf, uint32_t* bytes_written) DateTime File::GetLastAccessTimeUTC(const String& name) { -#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) SysTimeStruct t {}; sys_file_to_system_time_utc(sys_file_get_last_access_time_utc(name), t); @@ -941,7 +940,6 @@ DateTime File::GetLastAccessTimeUTC(const String& name) DateTime File::GetLastWriteTimeUTC(const String& name) { -#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) SysTimeStruct t {}; sys_file_to_system_time_utc(sys_file_get_last_write_time_utc(name), t); diff --git a/source/lib/Core/src/MemoryAlloc.cpp b/source/lib/Core/src/MemoryAlloc.cpp index 7c47f2c..aec7fb5 100644 --- a/source/lib/Core/src/MemoryAlloc.cpp +++ b/source/lib/Core/src/MemoryAlloc.cpp @@ -20,25 +20,31 @@ namespace Kyty::Core { #define MEM_ALLOC_ALIGNED #ifdef MEM_ALLOC_ALIGNED -#if KYTY_PLATFORM == KYTY_PLATFORM_ANDROID +#if defined(__ANDROID__) constexpr int MEM_ALLOC_ALIGN = 8; #else constexpr int MEM_ALLOC_ALIGN = 16; #endif #endif -#if KYTY_PLATFORM == KYTY_PLATFORM_WINDOWS && KYTY_BITNESS == 64 +#if defined(__WIN64__) [[maybe_unused]] constexpr int STACK_CHECK_FROM = 5; -#elif KYTY_PLATFORM == KYTY_PLATFORM_ANDROID +#elif defined(__ANDROID__) [[maybe_unused]] constexpr int STACK_CHECK_FROM = 4; #else [[maybe_unused]] constexpr int STACK_CHECK_FROM = 2; #endif - +#if defined(__WIN64__) || defined(__linux__) static SysCS* g_mem_cs = nullptr; static bool g_mem_initialized = false; static sys_heap_id_t g_default_heap = nullptr; static size_t g_mem_max_size = 0; +#else +static SysCS* g_mem_cs = 0; +static bool g_mem_initialized = false; +static sys_heap_id_t g_default_heap = 0; +static size_t g_mem_max_size = 0; +#endif #ifdef MEM_TRACKER @@ -210,7 +216,7 @@ void* mem_alloc(size_t size) { if (size == 0) { -#if KYTY_PLATFORM == KYTY_PLATFORM_LINUX +#if defined(__linux__) || defined(__APPLE__) size = 1; #else EXIT("size == 0\n"); diff --git a/source/lib/Core/src/Threads.cpp b/source/lib/Core/src/Threads.cpp index f120572..13631f9 100644 --- a/source/lib/Core/src/Threads.cpp +++ b/source/lib/Core/src/Threads.cpp @@ -12,11 +12,11 @@ #include // IWYU pragma: keep #include -#if KYTY_PLATFORM == KYTY_PLATFORM_WINDOWS && KYTY_COMPILER == KYTY_COMPILER_CLANG +#if defined(__WIN64__) #define KYTY_WIN_CS #endif -#if KYTY_PLATFORM != KYTY_PLATFORM_WINDOWS && KYTY_PLATFORM != KYTY_PLATFORM_LINUX +#if defined(__APPLE__) #define KYTY_SDL_THREADS #define KYTY_SDL_CS #endif diff --git a/source/lib/Core/src/VirtualMemory.cpp b/source/lib/Core/src/VirtualMemory.cpp index 5926fda..9b44de7 100644 --- a/source/lib/Core/src/VirtualMemory.cpp +++ b/source/lib/Core/src/VirtualMemory.cpp @@ -2,7 +2,7 @@ #include "Kyty/Sys/SysVirtual.h" -#if KYTY_PLATFORM == KYTY_PLATFORM_WINDOWS +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) #define KYTY_HAS_EXCEPTIONS #endif diff --git a/source/lib/Scripts/src/Scripts.cpp b/source/lib/Scripts/src/Scripts.cpp index a531be8..a6fb10f 100644 --- a/source/lib/Scripts/src/Scripts.cpp +++ b/source/lib/Scripts/src/Scripts.cpp @@ -859,7 +859,7 @@ ScriptVar ScriptTable::GetValue(uint32_t index) const void ScriptFuncResult::SetError(const String& msg) { m_ok = false; -#if KYTY_PLATFORM == KYTY_PLATFORM_WINDOWS +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) strncpy_s(m_msg, SCRIPT_FUNC_ERR_SIZE + 1, msg.C_Str(), SCRIPT_FUNC_ERR_SIZE); #else strncpy(m_msg, msg.C_Str(), SCRIPT_FUNC_ERR_SIZE); diff --git a/source/lib/Sys/src/SysMacOSDbg.cpp b/source/lib/Sys/src/SysMacOSDbg.cpp index 2257d0d..9e48fc0 100644 --- a/source/lib/Sys/src/SysMacOSDbg.cpp +++ b/source/lib/Sys/src/SysMacOSDbg.cpp @@ -9,6 +9,7 @@ #include #include #include +#include namespace Kyty { @@ -43,7 +44,7 @@ void sys_stack_usage(sys_dbg_stack_info_t& s) return; } str2[buff_len] = '\0'; - const char* name = basename(str2); + const char* name = basename(str2); result = snprintf(str, 1024,"/proc/%d/maps", static_cast(pid)); @@ -81,7 +82,7 @@ void sys_stack_usage(sys_dbg_stack_info_t& s) break; } - filename[0] = 0; + filename[0]; permissions[0] = 0; addr = 0; size = 0;