Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
deathkiller committed May 23, 2024
1 parent f669d6f commit 51e1f5d
Show file tree
Hide file tree
Showing 29 changed files with 218 additions and 120 deletions.
5 changes: 4 additions & 1 deletion Sources/Jazz2.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>

<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="$(SolutionDir)\VC-LTL helper for Visual Studio.props" />
<Import Project="$(SolutionDir)\Sign.props" Condition="Exists('$(SolutionDir)\Sign.props')" />
Expand All @@ -103,6 +104,7 @@
</PropertyGroup>
</Otherwise>
</Choose>

<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
Expand Down Expand Up @@ -185,7 +187,7 @@
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_HAS_EXCEPTIONS=0;DEATH_DEBUG;DEATH_TRACE;WITH_GLEW;__WITH_GLFW;WITH_SDL;WITH_AUDIO;WITH_THREADS;WITH_VORBIS;WITH_VORBIS_DYNAMIC;WITH_OPENMPT;WITH_ZLIB;WITH_BACKWARD;WITH_ANGELSCRIPT;WITH_MULTIPLAYER;_DEBUG;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_HAS_EXCEPTIONS=0;DEATH_DEBUG;DEATH_TRACE;WITH_GLEW;WITH_GLFW;__WITH_SDL;WITH_AUDIO;WITH_THREADS;WITH_VORBIS;WITH_VORBIS_DYNAMIC;WITH_OPENMPT;WITH_ZLIB;WITH_BACKWARD;WITH_ANGELSCRIPT;WITH_MULTIPLAYER;_DEBUG;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
Expand Down Expand Up @@ -692,6 +694,7 @@
<ClInclude Include="$(ExtensionLibraryPath)\Containers\StringUtils.h" />
<ClInclude Include="$(ExtensionLibraryPath)\Containers\StringConcatenable.h" />
<ClInclude Include="$(ExtensionLibraryPath)\Containers\StringStl.h" />
<ClInclude Include="$(ExtensionLibraryPath)\IO\FileAccessMode.h" />
<ClInclude Include="simdjson\simdjson.h" />
</ItemGroup>
<ItemGroup>
Expand Down
3 changes: 3 additions & 0 deletions Sources/Jazz2.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -1479,6 +1479,9 @@
<ClInclude Include="$(ExtensionLibraryPath)\IO\PakFile.h">
<Filter>Header Files\Shared\IO</Filter>
</ClInclude>
<ClInclude Include="$(ExtensionLibraryPath)\IO\FileAccessMode.h">
<Filter>Header Files\Shared\IO</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Main.cpp">
Expand Down
4 changes: 2 additions & 2 deletions Sources/Jazz2/ContentResolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ namespace Jazz2

auto buffer = std::make_unique<char[]>(fileSize + simdjson::SIMDJSON_PADDING);
s->Read(buffer.get(), fileSize);
s->Close();
s->Dispose();
buffer[fileSize] = '\0';

ondemand::parser parser;
Expand Down Expand Up @@ -1664,7 +1664,7 @@ namespace Jazz2

auto buffer = std::make_unique<char[]>(fileSize + simdjson::SIMDJSON_PADDING);
s->Read(buffer.get(), fileSize);
s->Close();
s->Dispose();
buffer[fileSize] = '\0';

ondemand::parser parser;
Expand Down
4 changes: 2 additions & 2 deletions Sources/Jazz2/PreferencesCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,8 @@ namespace Jazz2
co.Write(&pair.second.State, sizeof(EpisodeContinuationState));
}

co.Close();
so->Close();
co.Dispose();
so->Dispose();

#if defined(DEATH_TARGET_EMSCRIPTEN)
fs::SyncToPersistent();
Expand Down
2 changes: 1 addition & 1 deletion Sources/Jazz2/Scripting/ScriptLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ namespace Jazz2::Scripting

String scriptContent(NoInit, s->GetSize());
s->Read(scriptContent.data(), s->GetSize());
s->Close();
s->Dispose();

ScriptContextType contextType = ScriptContextType::Legacy;
SmallVector<String, 4> metadata;
Expand Down
1 change: 1 addition & 0 deletions Sources/Shared/Base/Unaligned.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ namespace Death {
{
std::memcpy(p, &v, sizeof(v));
}

}
31 changes: 31 additions & 0 deletions Sources/Shared/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,37 @@ namespace Death { namespace Implementation {
# define DEFINE_PRIVATE_ENUM_OPERATORS(type)
#endif

/** @brief Workaround for MSVC not being able to expand __VA_ARGS__ correctly. Would work with /Zc:preprocessor. Source: https://stackoverflow.com/a/5134656 */
#define DEATH_HELPER_EXPAND(...) __VA_ARGS__
/** @brief Pick a macro implementation based on how many arguments were passed. Source: https://stackoverflow.com/a/11763277 */
#define DEATH_HELPER_PICK(_0, _1, _2, _3, _4, _5, _6, _7, macroName, ...) macroName
/** @brief Get number of arguments in a variadic macro */
#define DEATH_HELPER_ARGS_COUNT(...) DEATH_HELPER_EXPAND(DEATH_HELPER_PICK(__VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1, 0))

//namespace Death { namespace Implementation {
// template<typename T/*, typename = std::enable_if_t<std::is_invocable_v<T>>*/>
// class ScopeExit
// {
// public:
// constexpr explicit ScopeExit(T deferredCallback) noexcept : _deferredCallback(std::move(deferredCallback)) {}
// ScopeExit& operator=(const ScopeExit&) = delete;
// ~ScopeExit() noexcept { _deferredCallback(); }
//
// private:
// T _deferredCallback;
// };
//
// struct ScopeExitHelper {};
//
// template<typename T>
// ScopeExit<T> operator%(const ScopeExitHelper&, T deferredCallback) {
// return ScopeExit<T>{std::move(deferredCallback)};
// }
//}}
//
///** @brief Defer execution of following block to the end of current scope */
//#define DEATH_DEFER DEATH_UNUSED auto const& DEATH_PASTE(_deferred_, __LINE__) = Death::Implementation::ScopeExitHelper{} % [&]()

// Compile-time and runtime CPU instruction set dispatch
namespace Death { namespace Cpu {
class Features;
Expand Down
4 changes: 4 additions & 0 deletions Sources/Shared/CommonWindows.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,8 @@ DEATH_ALWAYS_INLINE BOOL WINAPI UnregisterClass(LPCWSTR lpClassName, HINSTANCE h
}
#endif

#if defined(Yield)
# undef Yield
#endif

#endif
4 changes: 2 additions & 2 deletions Sources/Shared/Containers/Array.h
Original file line number Diff line number Diff line change
Expand Up @@ -576,8 +576,8 @@ namespace Death { namespace Containers {
}

/** @brief Array size */
template<class T> std::size_t arraySize(const Array<T>& view) {
return view.size();
template<class S = std::size_t, class T> S arraySize(const Array<T>& view) {
return static_cast<S>(view.size());
}

template<class T, class D> inline Array<T, D>::Array(Array<T, D>&& other) noexcept : _data{other._data}, _size{other._size}, _deleter{other._deleter} {
Expand Down
8 changes: 4 additions & 4 deletions Sources/Shared/Containers/ArrayView.h
Original file line number Diff line number Diff line change
Expand Up @@ -545,17 +545,17 @@ namespace Death { namespace Containers {
}

/** @brief Array view size */
template<class T> constexpr std::size_t arraySize(ArrayView<T> view) {
return view.size();
template<class S = std::size_t, class T> constexpr S arraySize(ArrayView<T> view) {
return static_cast<S>(view.size());
}

/** @overload */
template<std::size_t size_, class T> constexpr std::size_t arraySize(StaticArrayView<size_, T>) {
template<class S = std::size_t, S size_, class T> constexpr S arraySize(StaticArrayView<size_, T>) {
return size_;
}

/** @overload */
template<std::size_t size_, class T> constexpr std::size_t arraySize(T(&)[size_]) {
template<class S = std::size_t, S size_, class T> constexpr S arraySize(T(&)[size_]) {
return size_;
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Shared/Containers/GrowableArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ namespace Death { namespace Containers {
std::is_trivially_constructible<T>::value
>::type* = nullptr) {
// Needs to be < because sometimes begin > end
for (; begin < end; ++begin) new(begin) T {};
for (; begin < end; ++begin) new(begin) T{};
}

template<class T> struct AllocatorTraits {
Expand Down
22 changes: 5 additions & 17 deletions Sources/Shared/Cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -1606,10 +1606,6 @@ namespace Death { namespace Cpu {
*/
#define DEATH_CPU_SELECT(tag) tag, Death::Cpu::Implementation::priority(tag)

// Called from _DEATH_CPU_DISPATCHER() and _DEATH_ENABLE_CONCATENATE() to pick a macro implementation based
// on how many arguments were passed. Source: https://stackoverflow.com/a/11763277
#define _DEATH_HELPER_PICK(_0, _1, _2, _3, _4, _5, _6, _7, macroName, ...) macroName

/**
@brief Create a function for a runtime dispatch on a base CPU instruction set
Expand Down Expand Up @@ -1732,7 +1728,7 @@ namespace Death { namespace Cpu {
} \
template<unsigned int value, class First, class ...Next> DEATH_ALWAYS_INLINE decltype(function(DEATH_CPU_SELECT(Death::Cpu::Scalar))) function ## Internal(Death::Cpu::Features features, Death::Cpu::Implementation::Tags<value> extra, First first, Next... next) { \
static_assert(!(static_cast<unsigned int>(Death::Cpu::Implementation::tags(First{Death::Cpu::Implementation::Init})) & Death::Cpu::Implementation::BaseTagMask), \
"only extra instruction set tags should be explicitly listed"); \
"Only extra instruction set tags should be explicitly listed"); \
if(features & first) \
return function ## Internal(features, extra|first, next...); \
else \
Expand Down Expand Up @@ -1760,16 +1756,8 @@ namespace Death { namespace Cpu {
For a dispatch using just the base instruction set use
@ref DEATH_CPU_DISPATCHER_BASE() instead.
*/
#if !defined(DEATH_TARGET_MSVC) || defined(DEATH_TARGET_CLANG_CL)
#define _DEATH_CPU_DISPATCHER(...) \
_DEATH_HELPER_PICK(__VA_ARGS__, _DEATH_CPU_DISPATCHERn, _DEATH_CPU_DISPATCHERn, _DEATH_CPU_DISPATCHERn, _DEATH_CPU_DISPATCHERn, _DEATH_CPU_DISPATCHERn, _DEATH_CPU_DISPATCHERn, _DEATH_CPU_DISPATCHERn, _DEATH_CPU_DISPATCHER0, )(__VA_ARGS__)
#else
// Workaround for MSVC not being able to expand __VA_ARGS__ correctly. Would work with /Zc:preprocessor or /experimental:preprocessor,
// but I'm not enabling that globally yet. Source: https://stackoverflow.com/a/5134656
#define _DEATH_CPU_DISPATCHER_FFS_MSVC_EXPAND_THIS(x) x
#define _DEATH_CPU_DISPATCHER(...) \
_DEATH_CPU_DISPATCHER_FFS_MSVC_EXPAND_THIS(_DEATH_HELPER_PICK(__VA_ARGS__, _DEATH_CPU_DISPATCHERn, _DEATH_CPU_DISPATCHERn, _DEATH_CPU_DISPATCHERn, _DEATH_CPU_DISPATCHERn, _DEATH_CPU_DISPATCHERn, _DEATH_CPU_DISPATCHERn, _DEATH_CPU_DISPATCHERn, _DEATH_CPU_DISPATCHER0, )(__VA_ARGS__))
#endif
#define _DEATH_CPU_DISPATCHER(...) \
DEATH_HELPER_EXPAND(DEATH_HELPER_PICK(__VA_ARGS__, _DEATH_CPU_DISPATCHERn, _DEATH_CPU_DISPATCHERn, _DEATH_CPU_DISPATCHERn, _DEATH_CPU_DISPATCHERn, _DEATH_CPU_DISPATCHERn, _DEATH_CPU_DISPATCHERn, _DEATH_CPU_DISPATCHERn, _DEATH_CPU_DISPATCHER0, )(__VA_ARGS__))

/**
@brief Create a runtime-dispatched function pointer
Expand Down Expand Up @@ -2695,7 +2683,7 @@ namespace Death { namespace Cpu {
#define _DEATH_ENABLE_CONCATENATE7(v0, v1, v2, v3, v4, v5, v6, unused) \
__attribute__((__target__(v0 "," v1 "," v2 "," v3 "," v4 "," v5 "," v6)))
#define _DEATH_ENABLE_CONCATENATE(...) \
_DEATH_HELPER_PICK(__VA_ARGS__, _DEATH_ENABLE_CONCATENATE7, _DEATH_ENABLE_CONCATENATE6, _DEATH_ENABLE_CONCATENATE5, _DEATH_ENABLE_CONCATENATE4, _DEATH_ENABLE_CONCATENATE3, _DEATH_ENABLE_CONCATENATE2, _DEATH_ENABLE_CONCATENATE1, _DEATH_ENABLE_CONCATENATE0, )(__VA_ARGS__)
DEATH_HELPER_PICK(__VA_ARGS__, _DEATH_ENABLE_CONCATENATE7, _DEATH_ENABLE_CONCATENATE6, _DEATH_ENABLE_CONCATENATE5, _DEATH_ENABLE_CONCATENATE4, _DEATH_ENABLE_CONCATENATE3, _DEATH_ENABLE_CONCATENATE2, _DEATH_ENABLE_CONCATENATE1, _DEATH_ENABLE_CONCATENATE0, )(__VA_ARGS__)
// No _DEATH_HELPER_PASTE() needed here, as there's enough other indirections to make that work
#define _DEATH_ENABLE1(v0) \
_DEATH_ENABLE_CONCATENATE( \
Expand Down Expand Up @@ -2824,7 +2812,7 @@ namespace Death { namespace Cpu {
@cpp #ifdef @ce guard.
*/
#if !defined(DEATH_TARGET_MSVC) || defined(DEATH_TARGET_CLANG_CL)
# define DEATH_ENABLE(...) _DEATH_HELPER_PICK(__VA_ARGS__, _DEATH_ENABLE8, _DEATH_ENABLE7, _DEATH_ENABLE6, _DEATH_ENABLE5, _DEATH_ENABLE4, _DEATH_ENABLE3, _DEATH_ENABLE2, _DEATH_ENABLE1, )(__VA_ARGS__)
# define DEATH_ENABLE(...) DEATH_HELPER_PICK(__VA_ARGS__, _DEATH_ENABLE8, _DEATH_ENABLE7, _DEATH_ENABLE6, _DEATH_ENABLE5, _DEATH_ENABLE4, _DEATH_ENABLE3, _DEATH_ENABLE2, _DEATH_ENABLE1, )(__VA_ARGS__)
#else
# define DEATH_ENABLE(...)
#endif
Expand Down
5 changes: 4 additions & 1 deletion Sources/Shared/Environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ namespace Death { namespace Environment {
{
FILE* fp = ::fopen("/etc/os-release", "r");
if (fp == nullptr) {
return { };
fp = ::fopen("/usr/lib/os-release", "r");
if (fp == nullptr) {
return { };
}
}

Containers::String result;
Expand Down
21 changes: 14 additions & 7 deletions Sources/Shared/IO/AndroidAssetStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ namespace Death { namespace IO {
const char* AndroidAssetStream::_internalDataPath = nullptr;

AndroidAssetStream::AndroidAssetStream(const Containers::String& path, FileAccessMode mode)
: _shouldCloseOnDestruction(true),
#if defined(DEATH_USE_FILE_DESCRIPTORS)
_fileDescriptor(-1), _startOffset(0L)
: _fileDescriptor(-1), _startOffset(0L)
#else
_asset(nullptr)
: _asset(nullptr)
#endif
{
_path = path;
Expand All @@ -26,13 +25,11 @@ namespace Death { namespace IO {

AndroidAssetStream::~AndroidAssetStream()
{
if (_shouldCloseOnDestruction) {
Close();
}
AndroidAssetStream::Dispose();
}

/*! This method will close a file both normally opened or fopened */
void AndroidAssetStream::Close()
void AndroidAssetStream::Dispose()
{
#if defined(DEATH_USE_FILE_DESCRIPTORS)
if (_fileDescriptor >= 0) {
Expand Down Expand Up @@ -100,6 +97,10 @@ namespace Death { namespace IO {
{
DEATH_ASSERT(buffer != nullptr, 0, "buffer is nullptr");

if (bytes <= 0) {
return 0;
}

std::int32_t bytesRead = 0;
#if defined(DEATH_USE_FILE_DESCRIPTORS)
if (_fileDescriptor >= 0) {
Expand All @@ -126,6 +127,12 @@ namespace Death { namespace IO {
return ErrorInvalidStream;
}

bool AndroidAssetStream::Flush()
{
// Not supported
return true;
}

bool AndroidAssetStream::IsValid()
{
#if defined(DEATH_USE_FILE_DESCRIPTORS)
Expand Down
10 changes: 3 additions & 7 deletions Sources/Shared/IO/AndroidAssetStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#if defined(DEATH_TARGET_ANDROID)

#include "Stream.h"
#include "FileAccessMode.h"
#include "../Containers/String.h"
#include "../Containers/StringView.h"

Expand All @@ -30,21 +31,17 @@ namespace Death { namespace IO {
AndroidAssetStream(const AndroidAssetStream&) = delete;
AndroidAssetStream& operator=(const AndroidAssetStream&) = delete;

void Close() override;
void Dispose() override;
std::int64_t Seek(std::int64_t offset, SeekOrigin origin) override;
std::int64_t GetPosition() const override;
std::int32_t Read(void* buffer, std::int32_t bytes) override;
std::int32_t Write(const void* buffer, std::int32_t bytes) override;

bool Flush() override;
bool IsValid() override;

/** @brief Returns file path */
Containers::StringView GetPath() const;

void SetCloseOnDestruction(bool shouldCloseOnDestruction) override {
_shouldCloseOnDestruction = shouldCloseOnDestruction;
}

#if defined(DEATH_USE_FILE_DESCRIPTORS)
/** @brief Returns file descriptor */
DEATH_ALWAYS_INLINE std::int32_t GetFileDescriptor() const {
Expand Down Expand Up @@ -88,7 +85,6 @@ namespace Death { namespace IO {
#else
AAsset* _asset;
#endif
bool _shouldCloseOnDestruction;

void Open(FileAccessMode mode);
};
Expand Down
Loading

0 comments on commit 51e1f5d

Please sign in to comment.