From 97f1ae087e7dfc87f5f77f2f8e925c2790d672d0 Mon Sep 17 00:00:00 2001 From: Reece Date: Tue, 7 Sep 2021 08:59:56 +0100 Subject: [PATCH] Stability/Bug fixes; wxWidgets, compression --- .../Aurora/Compression/CompressionInfo.hpp | 5 +-- .../Aurora/Compression/ICompressionStream.hpp | 3 -- Source/Compression/BlockDecompressor.cpp | 34 ++++++++++--------- Source/Compression/BlockDecompressor.hpp | 4 ++- .../ConsoleWxWidgets/ConsoleWxWidgets.cpp | 32 ++++++++++++----- 5 files changed, 48 insertions(+), 30 deletions(-) diff --git a/Include/Aurora/Compression/CompressionInfo.hpp b/Include/Aurora/Compression/CompressionInfo.hpp index 7cc12b3c..5c534dea 100644 --- a/Include/Aurora/Compression/CompressionInfo.hpp +++ b/Include/Aurora/Compression/CompressionInfo.hpp @@ -37,7 +37,8 @@ namespace Aurora::Compression struct DecompressInfo { - ECompresionType alg; - AuUInt32 internalStreamSize; + ECompresionType alg {ECompresionType::eDeflate}; + AuUInt32 internalStreamSize {}; + bool permitResize {}; }; } \ No newline at end of file diff --git a/Include/Aurora/Compression/ICompressionStream.hpp b/Include/Aurora/Compression/ICompressionStream.hpp index 57bc5f69..2aab27bb 100644 --- a/Include/Aurora/Compression/ICompressionStream.hpp +++ b/Include/Aurora/Compression/ICompressionStream.hpp @@ -14,9 +14,6 @@ namespace Aurora::Compression public: virtual AuStreamReadWrittenPair_t Ingest(AuUInt32 bytesFromUnprocessedInputSource) = 0; - // You should probably check this if you don't want to be DDoS'd - virtual AuUInt32 GetInternalBufferSize() = 0; - // Limited stream API virtual bool ReadByProcessedN (void * /*opt*/, AuUInt32 minimumProcessed, AuStreamReadWrittenPair_t &pair, bool ingestUntilEOS = true) = 0; virtual bool ReadByProcessedN (void * /*opt*/, AuUInt32 minimumInflated) = 0; diff --git a/Source/Compression/BlockDecompressor.cpp b/Source/Compression/BlockDecompressor.cpp index f1b9e0b4..b757a3ec 100644 --- a/Source/Compression/BlockDecompressor.cpp +++ b/Source/Compression/BlockDecompressor.cpp @@ -81,6 +81,7 @@ namespace Aurora::Compression return false; } + auto remaining = length - written; written = this->_outbuffer.Write(reinterpret_cast(a) + written, remaining); @@ -101,9 +102,8 @@ namespace Aurora::Compression class ZSTDInflate : public BaseStream { public: - AuUInt32 bufferSize; - - ZSTDInflate(AuUInt32 bufferSize) : bufferSize(bufferSize), BaseStream(bufferSize) + DecompressInfo meta; + ZSTDInflate(const DecompressInfo &meta) : meta(meta), BaseStream(meta.internalStreamSize) {} ~ZSTDInflate() @@ -181,9 +181,9 @@ namespace Aurora::Compression class ZIPInflate : public BaseStream { public: - AuUInt32 bufferSize; + DecompressInfo meta; - ZIPInflate(AuUInt32 bufferSize) : bufferSize(bufferSize), BaseStream(bufferSize) + ZIPInflate(const DecompressInfo &meta) : meta(meta), BaseStream(meta.internalStreamSize) {} ~ZIPInflate() @@ -272,9 +272,9 @@ namespace Aurora::Compression class BZIPInflate : public BaseStream { public: - AuUInt32 bufferSize; + DecompressInfo meta; - BZIPInflate(AuUInt32 bufferSize) : bufferSize(bufferSize), BaseStream(bufferSize) + BZIPInflate(const DecompressInfo &meta) : meta(meta), BaseStream(meta.internalStreamSize) {} ~BZIPInflate() @@ -358,9 +358,9 @@ namespace Aurora::Compression { public: - AuUInt32 bufferSize; + DecompressInfo meta; - LZ4Inflate(AuUInt32 bufferSize) : bufferSize(bufferSize), BaseStream(bufferSize / 2) + LZ4Inflate(const DecompressInfo &meta) : meta(meta), BaseStream(meta.internalStreamSize) {} ~LZ4Inflate() @@ -393,8 +393,10 @@ namespace Aurora::Compression size_t bytesRemInFrame {}; LZ4F_decompressOptions_t opts {}; - auto bufferIn = AuSPtr(new char[bufferSize / 2], std::default_delete()); - auto bufferOut = AuSPtr(new char[bufferSize / 2], std::default_delete()); + auto bufferSize = meta.internalStreamSize / 2; + + auto bufferIn = AuSPtr(new char[bufferSize], std::default_delete()); + auto bufferOut = AuSPtr(new char[bufferSize], std::default_delete()); while (inputStat < input) { @@ -424,7 +426,7 @@ namespace Aurora::Compression { auto mustConsume = frameSize; size_t frameSPtr = mustConsume; - size_t frameS2Ptr = bufferSize / 2; + size_t frameS2Ptr = bufferSize; bytesRemInFrame = LZ4F_decompress(lz4Stream_, bufferOut.get(), &frameS2Ptr, bufferIn.get(), &frameSPtr, &opts); if (LZ4F_isError(bytesRemInFrame)) @@ -474,16 +476,16 @@ namespace Aurora::Compression switch (info.alg) { case ECompresionType::eZSTD: - ret = new ZSTDInflate(info.internalStreamSize); + ret = new ZSTDInflate(info); break; case ECompresionType::eBZIP2: - ret = new BZIPInflate(info.internalStreamSize); + ret = new BZIPInflate(info); break; case ECompresionType::eLZ4: - ret = new LZ4Inflate(info.internalStreamSize); + ret = new LZ4Inflate(info); break; case ECompresionType::eDeflate: - ret = new ZIPInflate(info.internalStreamSize); + ret = new ZIPInflate(info); break; default: ret = nullptr; diff --git a/Source/Compression/BlockDecompressor.hpp b/Source/Compression/BlockDecompressor.hpp index 158fc60c..8f83bf0a 100644 --- a/Source/Compression/BlockDecompressor.hpp +++ b/Source/Compression/BlockDecompressor.hpp @@ -22,7 +22,9 @@ namespace Aurora::Compression virtual bool ReadByProcessedN(void * /*opt*/, AuUInt32 minimumInflated, AuStreamReadWrittenPair_t &pair, bool ingestUntilEOS = true) override; virtual bool GoBackByProcessedN(AuUInt32 offset) override; virtual bool GoForwardByProcessedN(AuUInt32 offset) override; - virtual AuUInt32 GetInternalBufferSize() override; + + /// @deprecated + virtual AuUInt32 GetInternalBufferSize(); bool Write(const void *a, AuUInt32 length); diff --git a/Source/Console/ConsoleWxWidgets/ConsoleWxWidgets.cpp b/Source/Console/ConsoleWxWidgets/ConsoleWxWidgets.cpp index b8821a69..cb597537 100644 --- a/Source/Console/ConsoleWxWidgets/ConsoleWxWidgets.cpp +++ b/Source/Console/ConsoleWxWidgets/ConsoleWxWidgets.cpp @@ -692,22 +692,27 @@ namespace Aurora::Console::ConsoleWxWidgets void WxWidgetsRequestExit() { - gMutex.reset(); - if (!gWxConsoleReady) { + gMutex.reset(); return; } + + gMutex->Lock(); + gWxConsoleReady = false; + gMutex->Unlock(); auto window = wxTheApp->GetTopWindow(); if (!window) { + gMutex.reset(); return; } window->GetEventHandler()->CallAfter([]() { wxTheApp->Exit(); + gMutex.reset(); }); } @@ -741,18 +746,27 @@ namespace Aurora::Console::ConsoleWxWidgets Start(); } + class ConsoleMessageSubscriber : public Aurora::Console::Hooks::IConsoleSubscriber + { + public: + void OnMessage(const ConsoleMessage &string) override; + }; + + void ConsoleMessageSubscriber::OnMessage(const ConsoleMessage &string) + { + AU_LOCK_GUARD(gMutex); + gPendingLines.push_back(string); + } + + static ConsoleMessageSubscriber gConsoleMessageSubscriber; + void Start() { if (std::exchange(gConsoleStarted, true)) return; gMutex = Aurora::Threading::Primitives::MutexUnique(); - Aurora::Console::Hooks::AddFunctionalHook([&](const Aurora::Console::ConsoleMessage &string) -> void - { - gMutex->Lock(); - gPendingLines.push_back(string); - gMutex->Unlock(); - }); + Aurora::Console::Hooks::AddSubscription(AuUnsafeRaiiToShared(&gConsoleMessageSubscriber)); Aurora::Threading::Threads::AbstractThreadVectors handler; handler.DoRun = [](Aurora::Threading::Threads::IAuroraThread *) @@ -798,6 +812,8 @@ namespace Aurora::Console::ConsoleWxWidgets { WxWidgetsRequestExit(); + Aurora::Console::Hooks::RemoveSubscription(AuUnsafeRaiiToShared(&gConsoleMessageSubscriber)); + gWxWidgetsThread.reset(); auto wxHandle = wxTheApp;