From 4932f36da4fab3fc8965822bba473cbd20f7f405 Mon Sep 17 00:00:00 2001 From: rtk0c Date: Fri, 12 Nov 2021 13:34:50 -0800 Subject: Fix compile errors under MSVC, remove PLATFORM_* macros in favor of compiler builtin macros, and add win32 support to FileStream --- core/CMakeLists.txt | 13 ++-- core/src/Entrypoint/Backend_OpenGL3.cpp | 2 +- core/src/Entrypoint/main.cpp | 6 +- core/src/Model/Template/TableTemplate.cpp | 2 + core/src/Model/Workflow/Workflow.hpp | 16 ++-- core/src/Model/Workflow/Workflow_Main.cpp | 86 +++------------------ core/src/Utils/IO/DataStream.cpp | 28 +++---- core/src/Utils/IO/FileStream.cpp | 122 ++++++++++++++++++++++++++++-- core/src/Utils/IO/FileStream.hpp | 2 +- core/src/Utils/StandardDirectories.cpp | 18 ++--- 10 files changed, 168 insertions(+), 127 deletions(-) (limited to 'core') diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 346713d..9885886 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -17,7 +17,7 @@ elseif(APPLE) endif() macro(add_source_group GROUP_NAME) - list(APPEND CPLT_CORE_SOURCES ${ARGN}) + list(APPEND CPLT_CORE_SOURCES ${ARGN}) set_source_files_properties(${ARGN} PROPERTIES UNITY_GROUP "${GROUP_NAME}" @@ -85,12 +85,15 @@ list(APPEND CPLT_CORE_SOURCES # UI add_source_group(Utils src/Utils/IO/Archive.cpp src/Utils/IO/DataStream.cpp - src/Utils/IO/FileStream.cpp src/Utils/Sigslot.cpp src/Utils/StandardDirectories.cpp src/Utils/Time.cpp ) +list(APPEND CPLT_CORE_SOURCES + src/Utils/IO/FileStream.cpp +) + function(add_executable_variant TARGET_NAME) message("CpltCore: generating executable ${TARGET_NAME}") @@ -122,12 +125,6 @@ function(add_executable_variant TARGET_NAME) implot imgui-node-editor ) - target_compile_definitions(${TARGET_NAME} - PRIVATE - PLATFORM_WIN32=$ - PLATFORM_MACOS=$ - PLATFORM_LINUX=$ - ) if(NOT TARGET_LOCALE STREQUAL "en_US") target_compile_definitions(${TARGET_NAME} diff --git a/core/src/Entrypoint/Backend_OpenGL3.cpp b/core/src/Entrypoint/Backend_OpenGL3.cpp index 50dc78f..b4ae368 100644 --- a/core/src/Entrypoint/Backend_OpenGL3.cpp +++ b/core/src/Entrypoint/Backend_OpenGL3.cpp @@ -25,7 +25,7 @@ public: throw std::runtime_error("Failed to initialize GLFW."); } -# if PLATFORM_APPLE +# if defined(__APPLE__) // GL 3.2 + GLSL 150 const char* glslVersion = "#version 150"; glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); diff --git a/core/src/Entrypoint/main.cpp b/core/src/Entrypoint/main.cpp index 3cd3862..5ed7d84 100644 --- a/core/src/Entrypoint/main.cpp +++ b/core/src/Entrypoint/main.cpp @@ -21,7 +21,7 @@ using namespace std::literals::string_view_literals; static std::unique_ptr CreateDefaultBackend() { -#if PLATFORM_WIN32 +#if defined(_WIN32) # if BUILD_CORE_WITH_DX12_BACKEND if (auto backend = RenderingBackend::CreateDx12Backend()) { return backend; @@ -47,10 +47,10 @@ static std::unique_ptr CreateDefaultBackend() return backend; } # endif -#elif PLATFORM_MACOS +#elif defined(__APPLE__) // We currently only support using metal on macos return RenderingBackend::CreateMetalBackend(); -#elif PLATFORM_LINUX +#elif defined(__linux__) # if BUILD_CORE_WITH_VULKAN_BACKEND if (auto backend = RenderingBackend::CreateVulkanBackend()) { return backend; diff --git a/core/src/Model/Template/TableTemplate.cpp b/core/src/Model/Template/TableTemplate.cpp index 16cd333..57caac4 100644 --- a/core/src/Model/Template/TableTemplate.cpp +++ b/core/src/Model/Template/TableTemplate.cpp @@ -68,6 +68,8 @@ int TableArrayGroup::GetCount() const Vec2i TableArrayGroup::FindCell(std::string_view name) { + // TODO + return Vec2i{}; } template diff --git a/core/src/Model/Workflow/Workflow.hpp b/core/src/Model/Workflow/Workflow.hpp index c156582..3c4d320 100644 --- a/core/src/Model/Workflow/Workflow.hpp +++ b/core/src/Model/Workflow/Workflow.hpp @@ -193,14 +193,15 @@ protected: class Workflow : public Asset { + friend class WorkflowNode; + friend class WorkflowEvaluationContext; + class Private; + public: using CategoryType = WorkflowAssetList; static constinit const WorkflowAssetList Category; private: - friend class WorkflowNode; - friend class WorkflowEvaluationContext; - std::vector mConnections; std::vector> mNodes; std::vector> mConstants; @@ -279,13 +280,8 @@ public: /* Serialization */ - enum ReadResult - { - RR_Success, - RR_InvalidVersion, - }; - ReadResult ReadFrom(std::istream& stream); - void WriteTo(std::ostream& stream) const; + void ReadFromDataStream(InputDataStream& stream); + void WriteToDataStream(OutputDataStream& stream) const; private: std::pair AllocWorkflowConnection(); diff --git a/core/src/Model/Workflow/Workflow_Main.cpp b/core/src/Model/Workflow/Workflow_Main.cpp index 1f00b19..7c76611 100644 --- a/core/src/Model/Workflow/Workflow_Main.cpp +++ b/core/src/Model/Workflow/Workflow_Main.cpp @@ -635,85 +635,23 @@ Workflow::GraphUpdateResult Workflow::UpdateGraph(GraphUpdateDetails* details) return GUR_Success; } -Workflow::ReadResult Workflow::ReadFrom(std::istream& stream) +class Workflow::Private { - auto DeserializeV0 = [&]() { - uint32_t connectionCount, nodeCount, constantCount; - stream >> connectionCount >> nodeCount >> constantCount; - - mConnections.reserve(connectionCount); - for (uint32_t i = 0; i < connectionCount; ++i) { - mConnections.emplace_back().ReadFrom(stream); - } - - mNodes.reserve(connectionCount); - for (uint32_t i = 0; i < nodeCount; ++i) { - uint32_t nKind; - stream >> nKind; - auto kind = (WorkflowNode::Kind)nKind; - - mNodes.push_back(WorkflowNode::CreateByKind(kind)); - mNodes.back()->ReadFrom(stream); - } - - mConstants.reserve(connectionCount); - for (uint32_t i = 0; i < constantCount; ++i) { - uint32_t nKind; - stream >> nKind; - auto kind = (BaseValue::Kind)nKind; - - mConstants.push_back(BaseValue::CreateByKind(kind)); - mConstants.back()->ReadFrom(stream); - } - }; - - uint64_t version; - stream >> version; - - switch (version) { - case 0: DeserializeV0(); break; - default: return RR_InvalidVersion; + template + static void OperateStream(TSelf& self, TProxy& proxy) + { + // TODO } - return RR_Success; -} +}; -void Workflow::WriteTo(std::ostream& stream) const +void Workflow::ReadFromDataStream(InputDataStream& stream) { - // Version - stream << (uint64_t)0; - - // TODO id compacting - - stream << (size_t)mConnections.size(); - stream << (size_t)mNodes.size(); - stream << (size_t)mConstants.size(); - stream << (uint32_t)mConnectionCount; - stream << (uint32_t)mNodeCount; - stream << (uint32_t)mConstantCount; - - for (size_t i = 0; i < mConnections.size(); ++i) { - auto& conn = mConnections[i]; - if (conn.IsValid()) { - stream << i; - conn.WriteTo(stream); - } - } - - for (size_t i = 0; i < mNodes.size(); ++i) { - auto& node = mNodes[i]; - if (node) { - stream << i << (uint32_t)node->GetKind(); - node->WriteTo(stream); - } - } + Private::OperateStream(*this, stream); +} - for (size_t i = 0; i < mConstants.size(); ++i) { - auto& constant = mConstants[i]; - if (constant) { - stream << i; - constant->WriteTo(stream); - } - } +void Workflow::WriteToDataStream(OutputDataStream& stream) const +{ + Private::OperateStream(*this, stream); } std::pair Workflow::AllocWorkflowConnection() diff --git a/core/src/Utils/IO/DataStream.cpp b/core/src/Utils/IO/DataStream.cpp index bfc6252..c0797e3 100644 --- a/core/src/Utils/IO/DataStream.cpp +++ b/core/src/Utils/IO/DataStream.cpp @@ -21,7 +21,7 @@ static uint16_t ByteSwap(uint16_t n) static uint32_t ByteSwap(uint32_t n) { #ifdef _MSC_VER - // TODO + return _byteswap_ulong(n); #else return __builtin_bswap32(n); #endif @@ -30,7 +30,7 @@ static uint32_t ByteSwap(uint32_t n) static uint64_t ByteSwap(uint64_t n) { #ifdef _MSC_VER - // TODO + return _byteswap_uint64(n); #else return __builtin_bswap64(n); #endif @@ -62,34 +62,34 @@ InputDataStream::InputDataStream(InputFileStream stream) void InputDataStream::ReadBytes(size_t byteCount, std::byte* buffer) { - mBackend.ReadBytes(static_cast(byteCount),reinterpret_cast(buffer)); + mBackend.ReadBytes(static_cast(byteCount), reinterpret_cast(buffer)); } void InputDataStream::ReadBytes(size_t byteCount, char* buffer) { - mBackend.ReadBytes(static_cast(byteCount),reinterpret_cast(buffer)); + mBackend.ReadBytes(static_cast(byteCount), reinterpret_cast(buffer)); } void InputDataStream::ReadBytes(size_t byteCount, signed char* buffer) { - mBackend.ReadBytes(static_cast(byteCount),reinterpret_cast(buffer)); + mBackend.ReadBytes(static_cast(byteCount), reinterpret_cast(buffer)); } void InputDataStream::ReadBytes(size_t byteCount, unsigned char* buffer) { - mBackend.ReadBytes(static_cast(byteCount),reinterpret_cast(buffer)); + mBackend.ReadBytes(static_cast(byteCount), reinterpret_cast(buffer)); } void InputDataStream::Read(int8_t& n) { // sizeof() of a reference type yields the size of the reference - mBackend.ReadBytes(sizeof(n),reinterpret_cast(&n)); + mBackend.ReadBytes(sizeof(n), reinterpret_cast(&n)); } void InputDataStream::Read(int16_t& n) { int16_t tmp; - mBackend.ReadBytes(sizeof(tmp),reinterpret_cast(&tmp)); + mBackend.ReadBytes(sizeof(tmp), reinterpret_cast(&tmp)); if (GetEndianness() != std::endian::native) { n = ::ByteSwap(tmp); } else { @@ -100,7 +100,7 @@ void InputDataStream::Read(int16_t& n) void InputDataStream::Read(int32_t& n) { int32_t tmp; - mBackend.ReadBytes(sizeof(tmp),reinterpret_cast(&tmp)); + mBackend.ReadBytes(sizeof(tmp), reinterpret_cast(&tmp)); if (GetEndianness() != std::endian::native) { n = ::ByteSwap(tmp); } else { @@ -111,7 +111,7 @@ void InputDataStream::Read(int32_t& n) void InputDataStream::Read(int64_t& n) { int64_t tmp; - mBackend.ReadBytes(sizeof(tmp),reinterpret_cast(&tmp)); + mBackend.ReadBytes(sizeof(tmp), reinterpret_cast(&tmp)); if (GetEndianness() != std::endian::native) { n = ::ByteSwap(tmp); } else { @@ -121,13 +121,13 @@ void InputDataStream::Read(int64_t& n) void InputDataStream::Read(uint8_t& n) { - mBackend.ReadBytes(sizeof(n),reinterpret_cast(&n)); + mBackend.ReadBytes(sizeof(n), reinterpret_cast(&n)); } void InputDataStream::Read(uint16_t& n) { uint16_t tmp; - mBackend.ReadBytes(sizeof(tmp),reinterpret_cast(&tmp)); + mBackend.ReadBytes(sizeof(tmp), reinterpret_cast(&tmp)); if (GetEndianness() != std::endian::native) { n = ::ByteSwap(tmp); } else { @@ -138,7 +138,7 @@ void InputDataStream::Read(uint16_t& n) void InputDataStream::Read(uint32_t& n) { uint32_t tmp; - mBackend.ReadBytes(sizeof(tmp),reinterpret_cast(&tmp)); + mBackend.ReadBytes(sizeof(tmp), reinterpret_cast(&tmp)); if (GetEndianness() != std::endian::native) { n = ::ByteSwap(tmp); } else { @@ -149,7 +149,7 @@ void InputDataStream::Read(uint32_t& n) void InputDataStream::Read(uint64_t& n) { uint64_t tmp; - mBackend.ReadBytes(sizeof(tmp),reinterpret_cast(&tmp)); + mBackend.ReadBytes(sizeof(tmp), reinterpret_cast(&tmp)); if (GetEndianness() != std::endian::native) { n = ::ByteSwap(tmp); } else { diff --git a/core/src/Utils/IO/FileStream.cpp b/core/src/Utils/IO/FileStream.cpp index bc95b7e..b9ef2a7 100644 --- a/core/src/Utils/IO/FileStream.cpp +++ b/core/src/Utils/IO/FileStream.cpp @@ -3,17 +3,125 @@ #include #include -#if PLATFORM_WIN32 +namespace fs = std::filesystem; -// TODO +#if defined(_WIN32) +# define WIN32_LEAN_AND_MEAN +# define NOMINMAX +# include -#elif PLATFORM_MACOS || PLATFORM_LINUX +InputFileStream::InputFileStream(const fs::path& path) + : mOsFileHandle{ 0 } +{ + auto handle = reinterpret_cast(mOsFileHandle); + + *handle = CreateFileW( + path.c_str(), // fs::path::c_str() returns a wide string on Windows + GENERIC_READ, + /* No sharing */ 0, + /* Use default security*/ nullptr, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, + /* No attribute template */ nullptr); + + // TODO handle error +} + +InputFileStream::~InputFileStream() +{ + auto handle = reinterpret_cast(mOsFileHandle); + CloseHandle(*handle); +} + +OutputFileStream::OutputFileStream(const fs::path& path, WriteMode mode) + : mOsFileHandle{ 0 } +{ + auto handle = reinterpret_cast(mOsFileHandle); + + DWORD creationDisposition; + switch (mode) { + case AppendFile: creationDisposition = OPEN_ALWAYS; break; + case TruncateFile: creationDisposition = CREATE_ALWAYS; break; + } + + *handle = CreateFileW( + path.c_str(), + GENERIC_WRITE, + /* No sharing */ 0, + /* Use default security*/ nullptr, + creationDisposition, + FILE_ATTRIBUTE_NORMAL, + /* No attribute template */ nullptr); + + // TODO handle error +} + +OutputFileStream::~OutputFileStream() +{ + auto handle = reinterpret_cast(mOsFileHandle); + CloseHandle(*handle); +} + +static IoResult::ErrorKind MapErrorCodeToIoResult(DWORD error) +{ + switch (error) { + // TODO + + default: + std::cerr << "Unimplemented win32 error code " << error << ", report bug immediately.\n"; + std::abort(); + } +} + +static IoResult ReadBytesDirect(HANDLE hFile, size_t byteCount, std::byte* bytes) +{ + DWORD bytesRead; + BOOL result = ReadFile(hFile, bytes, byteCount, &bytesRead, nullptr); + + if (result) { + return IoResult{ + .Error = IoResult::ERR_None, + .SystemError = 0, + .BytesMoved = bytesRead, + }; + } else { + DWORD errorCode = GetLastError(); + return IoResult{ + .Error = ::MapErrorCodeToIoResult(errorCode), + .SystemError = errorCode, + .BytesMoved = bytesRead, + }; + } +} + +static IoResult WriteBytesDirect(HANDLE hFile, size_t byteCount, const std::byte* bytes) +{ + DWORD bytesWritten; + BOOL result = WriteFile(hFile, bytes, byteCount, &bytesWritten, nullptr); + + if (result) { + return IoResult{ + .Error = IoResult::ERR_None, + .SystemError = 0, + .BytesMoved = bytesWritten, + }; + } else { + DWORD errorCode = GetLastError(); + return IoResult{ + .Error = ::MapErrorCodeToIoResult(errorCode), + .SystemError = errorCode, + .BytesMoved = bytesWritten, + }; + } +} + +#elif defined(__APPLE__) || defined(__linux__) # include # include # include # include -InputFileStream::InputFileStream(const std::filesystem::path& path) +InputFileStream::InputFileStream(const fs::path& path) : mOsFileHandle{ 0 } { auto fd = reinterpret_cast(mOsFileHandle); @@ -26,7 +134,7 @@ InputFileStream::~InputFileStream() close(*fd); } -OutputFileStream::OutputFileStream(const std::filesystem::path& path, WriteMode mode) +OutputFileStream::OutputFileStream(const fs::path& path, WriteMode mode) : mOsFileHandle{ 0 } { auto fd = reinterpret_cast(mOsFileHandle); @@ -70,7 +178,7 @@ static IoResult ReadBytesDirect(const char* osFileHandle, size_t byteCount, std: int err = errno; return IoResult{ .Error = ::MapErrnoToIoResult(err), - .SystemError = err, + .SystemError = (uint32_t)err, .BytesMoved = 0, }; } else { @@ -91,7 +199,7 @@ static IoResult WriteBytesDirect(const char* osFileHandle, size_t byteCount, con int err = errno; return IoResult{ .Error = ::MapErrnoToIoResult(err), - .SystemError = err, + .SystemError = (uint32_t)err, .BytesMoved = 0, }; } else { diff --git a/core/src/Utils/IO/FileStream.hpp b/core/src/Utils/IO/FileStream.hpp index 9f5f24a..5b91632 100644 --- a/core/src/Utils/IO/FileStream.hpp +++ b/core/src/Utils/IO/FileStream.hpp @@ -18,7 +18,7 @@ struct IoResult }; ErrorKind Error; - int32_t SystemError; + uint32_t SystemError; size_t BytesMoved; }; diff --git a/core/src/Utils/StandardDirectories.cpp b/core/src/Utils/StandardDirectories.cpp index e7d3657..2202f51 100644 --- a/core/src/Utils/StandardDirectories.cpp +++ b/core/src/Utils/StandardDirectories.cpp @@ -5,7 +5,7 @@ namespace fs = std::filesystem; -#if PLATFORM_WIN32 +#if defined(_WIN32) // https://stackoverflow.com/questions/54499256/how-to-find-the-saved-games-folder-programmatically-in-c-c # include # include @@ -29,9 +29,9 @@ static fs::path GetAppDataRoaming() } } -#elif PLATFORM_MACOS +#elif defined(__APPLE__) // TODO -#elif PLATFORM_LINUX +#elif defined(__linux__) # include static fs::path GetEnvVar(const char* name, const char* backup) @@ -52,11 +52,11 @@ static fs::path GetEnvVar(const char* name, const char* backup) const std::filesystem::path& StandardDirectories::UserData() { static auto userDataDir = []() -> fs::path { -#if PLATFORM_WIN32 +#if defined(_WIN32) return GetAppDataRoaming(); -#elif PLATFORM_MACOS +#elif defined(__APPLE__) // TODO where? -#elif PLATFORM_LINUX +#elif defined(__linux__) return GetEnvVar("XDG_DATA_HOME", "~/.local/share"); #endif }(); @@ -66,11 +66,11 @@ const std::filesystem::path& StandardDirectories::UserData() const std::filesystem::path& StandardDirectories::UserConfig() { static auto userConfigDir = []() -> fs::path { -#if PLATFORM_WIN32 +#if defined(_WIN32) return GetAppDataRoaming(); -#elif PLATFORM_MACOS +#elif defined(__APPLE__) // TODO where? -#elif PLATFORM_LINUX +#elif defined(__linux__) return GetEnvVar("XDG_CONFIG_HOME", "~/.config"); #endif }(); -- cgit v1.2.3-70-g09d2