Skip to content

Commit

Permalink
Reworked IO, minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
deathkiller committed Nov 16, 2024
1 parent 55afa27 commit a35ce35
Show file tree
Hide file tree
Showing 23 changed files with 643 additions and 540 deletions.
2 changes: 1 addition & 1 deletion Sources/Jazz2/UI/Cinematics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ namespace Jazz2::UI
std::int32_t x = 0;
while ((c = ReadValue<std::uint8_t>(0)) != 0x80) {
if (c < 0x80) {
int32_t u;
std::int32_t u;
if (c == 0x00) {
u = ReadValue<std::uint16_t>(0);
} else {
Expand Down
7 changes: 1 addition & 6 deletions Sources/Jazz2/UI/ControlScheme.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,10 @@ namespace Jazz2::UI
ControlScheme() = delete;
~ControlScheme() = delete;

#if defined(DEATH_TARGET_SWITCH)
static constexpr std::int32_t MaxSupportedPlayers = 4;
// TODO: Game is crashing on Switch if more than 1 gamepad is used
static constexpr std::int32_t MaxConnectedGamepads = 4;
#elif defined(DEATH_TARGET_WINDOWS_RT)
static constexpr std::int32_t MaxSupportedPlayers = 4;
#if defined(DEATH_TARGET_EMSCRIPTEN) || defined(DEATH_TARGET_SWITCH) || defined(DEATH_TARGET_WINDOWS_RT)
static constexpr std::int32_t MaxConnectedGamepads = 4;
#else
static constexpr std::int32_t MaxSupportedPlayers = 4;
static constexpr std::int32_t MaxConnectedGamepads = 6;
#endif

Expand Down
48 changes: 24 additions & 24 deletions Sources/Shared/Base/TypeInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,43 +56,43 @@ namespace Death { namespace TypeInfo { namespace Implementation {
};

template<std::size_t N>
constexpr TypeInfoSkip CreateTypeInfoSkip(std::size_t sizeAtBegin, std::size_t sizeAtEnd, bool moreAtRuntime, const char(&untilRuntime)[N]) {
static constexpr TypeInfoSkip CreateTypeInfoSkip(std::size_t sizeAtBegin, std::size_t sizeAtEnd, bool moreAtRuntime, const char(&untilRuntime)[N]) {
return TypeInfoSkip{sizeAtBegin, sizeAtEnd, untilRuntime, moreAtRuntime ? N - 1 : 0};
}

template<std::size_t N>
constexpr TypeInfoSkip CreateTypeInfoSkip(std::size_t sizeAtBegin, std::size_t sizeAtEnd, const char(&untilRuntime)[N]) {
static constexpr TypeInfoSkip CreateTypeInfoSkip(std::size_t sizeAtBegin, std::size_t sizeAtEnd, const char(&untilRuntime)[N]) {
return TypeInfoSkip{sizeAtBegin, sizeAtEnd, untilRuntime, N - 1};
}

#if defined(DEATH_TARGET_MSVC) && !defined(DEATH_TARGET_CLANG_CL)
// const char *__cdecl __ti<A>::n(void) noexcept
constexpr TypeInfoSkip skip() noexcept {
static constexpr TypeInfoSkip skip() noexcept {
return CreateTypeInfoSkip(25, 19, "");
}
#elif defined(DEATH_TARGET_CLANG)
// static const char *__ti<A>::n() [T = A]
constexpr TypeInfoSkip skip() noexcept {
static constexpr TypeInfoSkip skip() noexcept {
return CreateTypeInfoSkip(24, 1, "T = ");
}
#elif defined(DEATH_TARGET_GCC) && DEATH_CXX_STANDARD >= 201402
// static constexpr const char* __ti<T>::n() [with T = A]
constexpr TypeInfoSkip skip() noexcept {
static constexpr TypeInfoSkip skip() noexcept {
return CreateTypeInfoSkip(52, 1, "");
}
#elif defined(DEATH_TARGET_GCC)
// static const char* __ti<T>::n() [with T = A]
constexpr TypeInfoSkip skip() noexcept {
static constexpr TypeInfoSkip skip() noexcept {
return CreateTypeInfoSkip(42, 1, "");
}
#else
constexpr TypeInfoSkip skip() noexcept {
static constexpr TypeInfoSkip skip() noexcept {
return CreateTypeInfoSkip(0, 0, "");
}
#endif

#if defined(DEATH_HAS_BUILTIN_CONSTANT)
DEATH_CONSTEXPR14 DEATH_ALWAYS_INLINE bool IsConstantString(const char* str) noexcept {
static DEATH_CONSTEXPR14 DEATH_ALWAYS_INLINE bool IsConstantString(const char* str) noexcept {
while (DEATH_HAS_BUILTIN_CONSTANT(*str)) {
if (*str == '\0') {
return true;
Expand All @@ -104,7 +104,7 @@ namespace Death { namespace TypeInfo { namespace Implementation {
#endif

template<class ForwardIterator1, class ForwardIterator2>
DEATH_CONSTEXPR14 inline ForwardIterator1 constexpr_search(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2) noexcept {
static DEATH_CONSTEXPR14 inline ForwardIterator1 constexpr_search(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2) noexcept {
if (first2 == last2) {
return first1; // Specified in C++11
}
Expand All @@ -122,14 +122,14 @@ namespace Death { namespace TypeInfo { namespace Implementation {
return last1;
}

DEATH_CONSTEXPR14 inline std::int32_t constexpr_strcmp_loop(const char* v1, const char* v2) noexcept {
static DEATH_CONSTEXPR14 inline std::int32_t constexpr_strcmp_loop(const char* v1, const char* v2) noexcept {
while (*v1 != '\0' && *v1 == *v2) {
++v1; ++v2;
}
return static_cast<std::int32_t>(*v1) - *v2;
}

DEATH_CONSTEXPR14 inline std::int32_t constexpr_strcmp(const char* v1, const char* v2) noexcept {
static DEATH_CONSTEXPR14 inline std::int32_t constexpr_strcmp(const char* v1, const char* v2) noexcept {
#if DEATH_CXX_STANDARD >= 201402 && defined(DEATH_HAS_BUILTIN_CONSTANT) && defined(DEATH_HAS_BUILTIN_STRCMP)
if (IsConstantString(v1) && IsConstantString(v2)) {
return constexpr_strcmp_loop(v1, v2);
Expand All @@ -143,15 +143,15 @@ namespace Death { namespace TypeInfo { namespace Implementation {
}

template<std::size_t ArrayLength>
DEATH_CONSTEXPR14 inline const char* SkipBeginningRuntime(const char* begin) noexcept {
static DEATH_CONSTEXPR14 inline const char* SkipBeginningRuntime(const char* begin) noexcept {
const char* const it = constexpr_search(
begin, begin + ArrayLength,
skip().UntilRuntime, skip().UntilRuntime + skip().UntilRuntimeLength);
return (it == begin + ArrayLength ? begin : it + skip().UntilRuntimeLength);
}

template<std::size_t ArrayLength>
DEATH_CONSTEXPR14 inline const char* SkipBeginning(const char* begin) noexcept {
static DEATH_CONSTEXPR14 inline const char* SkipBeginning(const char* begin) noexcept {
static_assert(ArrayLength > skip().SizeAtBegin + skip().SizeAtEnd, "runtime_cast<T>() is misconfigured for your compiler");

return (skip().UntilRuntimeLength
Expand Down Expand Up @@ -184,22 +184,22 @@ struct __ti
namespace Death { namespace TypeInfo { namespace Implementation {
//###==##====#=====--==~--~=~- --- -- - - - -

using TypeId = const char*;
using TypeHandle = const void*;

struct Helpers
{
template<class T>
static DEATH_CONSTEXPR14 inline TypeId ConstructTypeId() noexcept {
static DEATH_CONSTEXPR14 inline TypeHandle ConstructTypeHandle() noexcept {
return __ti<T>::n();
}

template<class T>
static constexpr TypeId GetTypeId(const T*) noexcept {
return ConstructTypeId<T>();
static constexpr TypeHandle GetTypeHandle(const T*) noexcept {
return ConstructTypeHandle<T>();
}

/*template<class T>
Containers::Pair<const char*, std::size_t> GetTypeName() noexcept {
static Containers::Pair<const char*, std::size_t> GetTypeName() noexcept {
constexpr const char* name = __ti<T>::n();
std::size_t length = std::strlen(name + skip().SizeAtEnd);
while (name[length - 1] == ' ') length--; // MSVC sometimes adds trailing whitespaces
Expand All @@ -221,24 +221,24 @@ namespace Death { namespace TypeInfo { namespace Implementation {
if (u == nullptr)
return nullptr;
return const_cast<T*>(static_cast<const T*>(
u->__FindInstance(ConstructTypeId<T>())
u->__FindInstance(ConstructTypeHandle<T>())
));
}

template<class T, class U>
static const T* RuntimeCast(const U* u, std::integral_constant<bool, false>) noexcept {
if (u == nullptr)
return nullptr;
return static_cast<const T*>(u->__FindInstance(ConstructTypeId<T>()));
return static_cast<const T*>(u->__FindInstance(ConstructTypeHandle<T>()));
}

template<class Self>
static constexpr const void* FindInstance(TypeId, const Self*) noexcept {
static constexpr const void* FindInstance(TypeHandle, const Self*) noexcept {
return nullptr;
}

template<class Base, class ...OtherBases, class Self>
static const void* FindInstance(TypeId t, const Self* self) noexcept {
static const void* FindInstance(TypeHandle t, const Self* self) noexcept {
if (const void* ptr = self->Base::__FindInstance(t)) {
return ptr;
}
Expand All @@ -252,8 +252,8 @@ namespace Death { namespace TypeInfo { namespace Implementation {
__DEATH_WARNING_PUSH \
__DEATH_NO_OVERRIDE_WARNING \
friend struct Death::TypeInfo::Implementation::Helpers; \
virtual const void* __FindInstance(Death::TypeInfo::Implementation::TypeId t) const noexcept { \
if (t == Death::TypeInfo::Implementation::Helpers::GetTypeId(this)) \
virtual const void* __FindInstance(Death::TypeInfo::Implementation::TypeHandle t) const noexcept { \
if (t == Death::TypeInfo::Implementation::Helpers::GetTypeHandle(this)) \
return this; \
return Death::TypeInfo::Implementation::Helpers::FindInstance<__VA_ARGS__>(t, this); \
} \
Expand Down
46 changes: 15 additions & 31 deletions Sources/Shared/Core/Backward.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#define BACKWARD_INCLUDED

#include "../CommonBase.h"
#include "../IO/Stream.h"

#if DEATH_CXX_STANDARD >= 201703L
# define BACKWARD_ATLEAST_CXX17
Expand Down Expand Up @@ -3714,27 +3715,26 @@ namespace Death { namespace Backward {

class cfile_streambuf : public std::streambuf {
public:
cfile_streambuf(FILE* sink) : _sink(sink) {}
cfile_streambuf(IO::Stream* sink) : _sink(sink) {}
int_type underflow() override {
return traits_type::eof();
}
int_type overflow(int_type ch) override {
if (traits_type::not_eof(ch) && fputc(ch, _sink) != EOF) {
if (traits_type::not_eof(ch) && _sink->Write(&ch, sizeof(ch) > 0)) {
return ch;
}
return traits_type::eof();
}

std::streamsize xsputn(const char_type* s, std::streamsize count) override {
return static_cast<std::streamsize>(
fwrite(s, sizeof * s, static_cast<std::size_t>(count), _sink));
return static_cast<std::streamsize>(_sink->Write(s, sizeof(*s) * static_cast<std::int64_t>(count)));
}

private:
cfile_streambuf(const cfile_streambuf&) = delete;
cfile_streambuf& operator=(const cfile_streambuf&) = delete;

FILE* _sink;
IO::Stream* _sink;
};

# if defined(BACKWARD_TARGET_LINUX) || defined(BACKWARD_TARGET_WINDOWS)
Expand Down Expand Up @@ -3855,8 +3855,8 @@ namespace Death { namespace Backward {
: FeatureFlags(Flags::None), Address(false), Object(false), InlinerContextSize(5), TraceContextSize(7) {}

template<typename ST>
void Print(ST& st, FILE* fp = stderr, std::int32_t signal = 0) {
cfile_streambuf obuf(fp);
void Print(ST& st, IO::Stream* s, std::int32_t signal = 0) {
cfile_streambuf obuf(s);
std::ostream os(&obuf);
Colorize colorize(os);
colorize.SetEnabled((FeatureFlags & Flags::ColorizeOutput) == Flags::ColorizeOutput);
Expand All @@ -3870,22 +3870,6 @@ namespace Death { namespace Backward {
PrintStacktrace(st, os, signal, colorize);
}

//template<typename IT>
//void Print(IT begin, IT end, FILE* fp = stderr, std::size_t threadId = 0, std::int32_t signal = 0) {
// cfile_streambuf obuf(fp);
// std::ostream os(&obuf);
// Colorize colorize(os);
// colorize.activate(colorMode);
// print_stacktrace(begin, end, os, threadId, signal, colorize);
//}

//template<typename IT>
//void Print(IT begin, IT end, std::ostream& os, std::size_t threadId = 0, std::int32_t signal = 0) {
// Colorize colorize(os);
// colorize.activate(colorMode);
// print_stacktrace(begin, end, os, threadId, signal, colorize);
//}

TraceResolver const& GetResolver() const {
return _resolver;
}
Expand Down Expand Up @@ -4171,7 +4155,7 @@ namespace Death { namespace Backward {
return std::vector<std::int32_t>(posixSignals, posixSignals + sizeof(posixSignals) / sizeof(posixSignals[0]));
}

FILE* Destination;
IO::Stream* Destination;
Flags FeatureFlags;

ExceptionHandling(Flags flags = Flags::None) : Destination(nullptr), FeatureFlags(flags), _loaded(false) {
Expand Down Expand Up @@ -4271,12 +4255,12 @@ namespace Death { namespace Backward {
printer.FeatureFlags = FeatureFlags;
printer.Print(st, std::cerr, info->si_signo);

FILE* dest = Destination;
bool shouldWriteToDest = (dest != nullptr && dest != stderr && dest != stdout);
IO::Stream* dest = Destination;
bool shouldWriteToDest = (dest != nullptr);
if (shouldWriteToDest) {
printer.FeatureFlags = FeatureFlags & ~Flags::ColorizeOutput;
printer.Print(st, dest, info->si_signo);
::fflush(dest);
dest->Flush();
}
}

Expand Down Expand Up @@ -4312,7 +4296,7 @@ namespace Death { namespace Backward {

class ExceptionHandling {
public:
FILE* Destination;
IO::Stream* Destination;
Flags FeatureFlags;

ExceptionHandling(Flags flags = Flags::None) : Destination(nullptr), FeatureFlags(flags) {
Expand Down Expand Up @@ -4565,8 +4549,8 @@ namespace Death { namespace Backward {
HANDLE hStdError = ::GetStdHandle(STD_ERROR_HANDLE);
bool shouldWriteToStdErr = (::GetFileType(hStdError) != FILE_TYPE_UNKNOWN);

FILE* dest = Destination;
bool shouldWriteToDest = (dest != nullptr && dest != stderr && dest != stdout);
IO::Stream* dest = Destination;
bool shouldWriteToDest = (dest != nullptr);

if (!shouldWriteToStdErr && !shouldWriteToDest) {
return;
Expand All @@ -4589,7 +4573,7 @@ namespace Death { namespace Backward {
if (shouldWriteToDest) {
printer.FeatureFlags = FeatureFlags & ~Flags::ColorizeOutput;
printer.Print(st, dest);
::fflush(dest);
dest->Flush();
}
}

Expand Down
16 changes: 8 additions & 8 deletions Sources/Shared/Cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,23 @@ namespace Death { namespace Cpu {
# if defined(DEATH_TARGET_32BIT)
// Apple says I should use hw.optional.AdvSIMD instead tho
if (appleSysctlByName("hw.optional.neon")) out |= TypeTraits<NeonT>::Index;
// On 32bit I have no idea how to query FMA / vfpv4 support, so that'll only be implied if FP16 is available as well.
// Since I don't think there are many 32bit iOS devices left, that's not worth bothering with.
// On 32-bit I have no idea how to query FMA / vfpv4 support, so that'll only be implied if FP16 is available as well.
// Since I don't think there are many 32-bit iOS devices left, that's not worth bothering with.
# else
// To avoid string operations, on 64bit I just assume NEON and FMA being present, like in the Linux case.
// To avoid string operations, on 64-bit I just assume NEON and FMA being present, like in the Linux case.
// Again, for extra security make use of the DEATH_TARGET_ defines (which should be always there on ARM64)
out |=
out |= 0
# if defined(DEATH_TARGET_NEON)
TypeTraits<NeonT>::Index |
| TypeTraits<NeonT>::Index
# endif
# if defined(DEATH_TARGET_NEON_FMA)
TypeTraits<NeonFmaT>::Index |
| TypeTraits<NeonFmaT>::Index
# endif
0;
;
# endif
// Apple says I should use hw.optional.arm.FEAT_FP16 instead though
if (appleSysctlByName("hw.optional.neon_fp16")) {
// As noted above, if FP16 is available on 32bit, bite the bullet and assume FMA is there as well
// As noted above, if FP16 is available on 32-bit, bite the bullet and assume FMA is there as well
# if defined(DEATH_TARGET_32BIT)
out |= TypeTraits<NeonFmaT>::Index;
# endif
Expand Down
12 changes: 6 additions & 6 deletions Sources/Shared/Cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

#include "Common.h"

// Because can't use inline assembly when targeting 64bit on MSVC, and because <intrin.h> and <immintrin.h> is just
// Because can't use inline assembly when targeting 64-bit on MSVC, and because <intrin.h> and <immintrin.h> is just
// too damn heavy to be included in a header. Declarations copied verbatim. Clang-cl doesn't like this (undefined
// reference to __cpuidex), so using the GCC/Clang codepath on it instead.
#if defined(DEATH_TARGET_MSVC) && !defined(DEATH_TARGET_CLANG_CL) && defined(DEATH_TARGET_X86)
Expand Down Expand Up @@ -1489,7 +1489,7 @@ namespace Death { namespace Cpu {
*/
constexpr Features compiledFeatures() {
// Clang 14 warns if zero (an int) isn't first because bitwise operations between different enums are deprecated in C++20.
return Features { 0
return Features{0
#if defined(DEATH_TARGET_X86)
# if defined(DEATH_TARGET_SSE2)
| TypeTraits<Sse2T>::Index
Expand Down Expand Up @@ -2959,14 +2959,14 @@ namespace Death { namespace Cpu {
if (caps & (1 << 16) /*HWCAP_VFPv4*/) out |= TypeTraits<NeonFmaT>::Index;
# else
// On ARM64 NEON and NEON FMA is implicit. For extra security make use of the DEATH_TARGET_ defines (which should be always there).
out |=
out |= 0
# if defined(DEATH_TARGET_NEON)
TypeTraits<NeonT>::Index |
| TypeTraits<NeonT>::Index
# endif
# if defined(DEATH_TARGET_NEON_FMA)
TypeTraits<NeonFmaT>::Index |
| TypeTraits<NeonFmaT>::Index
# endif
0;
;
// The HWCAP flags are extremely cryptic. The only vague confirmation is in a *commit message* to the kernel hwcaps file, FFS.
// The HWCAP_FPHP seems to correspond to scalar FP16, so the other should be the vector one?
// https://github.com/torvalds/linux/blame/master/arch/arm64/include/uapi/asm/hwcap.h
Expand Down
Loading

0 comments on commit a35ce35

Please sign in to comment.