From 0c1ca785596f1c7215326796954b9637ee84a3aa Mon Sep 17 00:00:00 2001 From: dgelessus Date: Sun, 4 Jun 2023 18:14:20 +0200 Subject: [PATCH 01/13] Convert throwing default hsStream methods to pure virtual methods And implement them where missing. Only exception: CopyToMem is now no longer part of base hsStream, because it's only implemented and used for the in-memory streams. --- Sources/Plasma/CoreLib/hsSTLStream.h | 2 +- Sources/Plasma/CoreLib/hsStream.cpp | 62 ++++++++++--------- Sources/Plasma/CoreLib/hsStream.h | 21 ++++--- .../Plasma/FeatureLib/pfPatcher/pfPatcher.cpp | 1 + .../PubUtilLib/plCompression/plZlibStream.cpp | 5 ++ .../PubUtilLib/plCompression/plZlibStream.h | 1 + .../PubUtilLib/plFile/plEncryptedStream.cpp | 10 +++ .../PubUtilLib/plFile/plEncryptedStream.h | 1 + .../PubUtilLib/plFile/plSecureStream.cpp | 10 +++ .../Plasma/PubUtilLib/plFile/plSecureStream.h | 1 + .../plNetMessage/plNetMsgHelpers.cpp | 2 +- .../PubUtilLib/plNetMessage/plNetMsgHelpers.h | 3 +- .../MaxComponent/plMultistageBehComponent.cpp | 3 + 13 files changed, 82 insertions(+), 40 deletions(-) diff --git a/Sources/Plasma/CoreLib/hsSTLStream.h b/Sources/Plasma/CoreLib/hsSTLStream.h index 0c166713dd..80c9ba173b 100644 --- a/Sources/Plasma/CoreLib/hsSTLStream.h +++ b/Sources/Plasma/CoreLib/hsSTLStream.h @@ -71,7 +71,7 @@ class hsVectorStream : public hsStream void Truncate() override; uint32_t GetEOF() override; - void CopyToMem(void* mem) override; + void CopyToMem(void* mem); virtual void Reset(); // clears the buffers diff --git a/Sources/Plasma/CoreLib/hsStream.cpp b/Sources/Plasma/CoreLib/hsStream.cpp index 7ae63ec6be..8c61bb2b3e 100644 --- a/Sources/Plasma/CoreLib/hsStream.cpp +++ b/Sources/Plasma/CoreLib/hsStream.cpp @@ -54,12 +54,6 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #if HS_BUILD_FOR_UNIX #include #endif -////////////////////////////////////////////////////////////////////////////////// - -void hsStream::FastFwd() -{ - hsThrow("FastFwd unimplemented by subclass of stream"); -} uint32_t hsStream::GetPosition() const { @@ -74,11 +68,6 @@ void hsStream::SetPosition(uint32_t position) Skip(position); } -void hsStream::Truncate() -{ - hsThrow("Truncate unimplemented by subclass of stream"); -} - uint32_t hsStream::GetSizeLeft() { uint32_t ret = 0; @@ -96,19 +85,6 @@ uint32_t hsStream::GetSizeLeft() ////////////////////////////////////////////////////////////////////////////////// -uint32_t hsStream::GetEOF() -{ - hsThrow( "GetEOF() unimplemented by subclass of stream"); - return 0; -} - -void hsStream::CopyToMem(void* mem) -{ - hsThrow( "CopyToMem unimplemented by subclass of stream"); -} - -////////////////////////////////////////////////////////////////////////////////// - uint32_t hsStream::WriteSafeString(const ST::string &string) { size_t len = string.size(); @@ -217,12 +193,6 @@ uint8_t hsStream::ReadByte() return value; } -bool hsStream::AtEnd() -{ - hsAssert(0,"No hsStream::AtEnd() implemented for this stream class"); - return false; -} - bool hsStream::IsTokenSeparator(char c) { return (isspace(static_cast(c)) || c==',' || c=='='); @@ -725,6 +695,12 @@ void hsRAMStream::Rewind() fIter.ResetToHead(&fAppender); } +void hsRAMStream::FastFwd() +{ + fBytesRead = fPosition = GetEOF(); + fIter.ResetToTail(&fAppender); +} + void hsRAMStream::Truncate() { Reset(); @@ -742,6 +718,11 @@ void hsRAMStream::CopyToMem(void* mem) ////////////////////////////////////////////////////////////////////// +bool hsNullStream::AtEnd() +{ + return true; +} + uint32_t hsNullStream::Read(uint32_t byteCount, void * buffer) { hsThrow("hsNullStream: Can't read from this stream!"); @@ -768,6 +749,9 @@ void hsNullStream::Rewind() fPosition = 0; } +void hsNullStream::FastFwd() +{} + void hsNullStream::Truncate() { } @@ -816,6 +800,12 @@ void hsReadOnlyStream::Rewind() fData = fStart; } +void hsReadOnlyStream::FastFwd() +{ + fBytesRead = fPosition = GetEOF(); + fData = fStop; +} + void hsReadOnlyStream::Truncate() { hsThrow( "can't write to a readonly stream"); @@ -946,6 +936,11 @@ void hsQueueStream::FastFwd() fReadCursor = fWriteCursor; } +void hsQueueStream::Truncate() +{ + fWriteCursor = fReadCursor; +} + bool hsQueueStream::AtEnd() { return fReadCursor == fWriteCursor; @@ -1213,6 +1208,13 @@ void hsBufferedStream::Rewind() fPosition = 0; } +void hsBufferedStream::FastFwd() +{ + fseek(fRef, 0, SEEK_END); + fBufferLen = 0; + fPosition = ftell(fRef); +} + uint32_t hsBufferedStream::GetEOF() { if (fWriteBufferUsed) diff --git a/Sources/Plasma/CoreLib/hsStream.h b/Sources/Plasma/CoreLib/hsStream.h index 00131e7bb6..d576d6ba00 100644 --- a/Sources/Plasma/CoreLib/hsStream.h +++ b/Sources/Plasma/CoreLib/hsStream.h @@ -64,20 +64,19 @@ enum { virtual bool Open(const plFileName &, const char * = "rb") = 0; virtual bool Close()=0; - virtual bool AtEnd(); + virtual bool AtEnd() = 0; virtual uint32_t Read(uint32_t byteCount, void * buffer) = 0; virtual uint32_t Write(uint32_t byteCount, const void* buffer) = 0; virtual void Skip(uint32_t deltaByteCount) = 0; virtual void Rewind() = 0; - virtual void FastFwd(); + virtual void FastFwd() = 0; virtual uint32_t GetPosition() const; virtual void SetPosition(uint32_t position); - virtual void Truncate(); + virtual void Truncate() = 0; virtual void Flush() {} - virtual uint32_t GetEOF(); + virtual uint32_t GetEOF() = 0; uint32_t GetSizeLeft(); - virtual void CopyToMem(void* mem); virtual bool IsCompressed() { return false; } uint32_t WriteString(const ST::string & string) { return Write((uint32_t)string.size(), string.c_str()); } @@ -228,10 +227,11 @@ class hsRAMStream : public hsStream { uint32_t Write(uint32_t byteCount, const void* buffer) override; void Skip(uint32_t deltaByteCount) override; void Rewind() override; + void FastFwd() override; void Truncate() override; uint32_t GetEOF() override; - void CopyToMem(void* mem) override; + void CopyToMem(void* mem); void Reset(); // clears the buffers }; @@ -242,12 +242,15 @@ class hsNullStream : public hsStream { bool Open(const plFileName &, const char *) override { return true; } bool Close() override { return true; } + bool AtEnd() override; uint32_t Read(uint32_t byteCount, void * buffer) override; // throws exception uint32_t Write(uint32_t byteCount, const void* buffer) override; void Skip(uint32_t deltaByteCount) override; void Rewind() override; + void FastFwd() override; void Truncate() override; + uint32_t GetEOF() override { return GetBytesWritten(); } uint32_t GetBytesWritten() const { return fBytesRead; } void Reset( ) { fBytesRead = 0; } }; @@ -270,10 +273,11 @@ class hsReadOnlyStream : public hsStream { uint32_t Write(uint32_t byteCount, const void* buffer) override; // throws exception void Skip(uint32_t deltaByteCount) override; void Rewind() override; + void FastFwd() override; void Truncate() override; virtual uint32_t GetBytesRead() const { return fBytesRead; } uint32_t GetEOF() override { return (uint32_t)(fStop-fStart); } - void CopyToMem(void* mem) override; + void CopyToMem(void* mem); }; // write only mem stream @@ -310,8 +314,10 @@ class hsQueueStream : public hsStream { void Skip(uint32_t deltaByteCount) override; void Rewind() override; void FastFwd() override; + void Truncate() override; bool AtEnd() override; + uint32_t GetEOF() override { return fWriteCursor - fReadCursor; } uint32_t GetSize() { return fSize; } const char* GetQueue() { return fQueue; } uint32_t GetReadCursor() { return fReadCursor; } @@ -352,6 +358,7 @@ class hsBufferedStream : public hsStream uint32_t Write(uint32_t byteCount, const void* buffer) override; void Skip(uint32_t deltaByteCount) override; void Rewind() override; + void FastFwd() override; void Truncate() override; uint32_t GetEOF() override; diff --git a/Sources/Plasma/FeatureLib/pfPatcher/pfPatcher.cpp b/Sources/Plasma/FeatureLib/pfPatcher/pfPatcher.cpp index df25a5fe1b..e016264f9c 100644 --- a/Sources/Plasma/FeatureLib/pfPatcher/pfPatcher.cpp +++ b/Sources/Plasma/FeatureLib/pfPatcher/pfPatcher.cpp @@ -278,6 +278,7 @@ class pfPatcherStream : public plZlibStream uint32_t GetPosition() const override { return fOutput->GetPosition(); } uint32_t Read(uint32_t count, void* buf) override { return fOutput->Read(count, buf); } void Rewind() override { fOutput->Rewind(); } + void FastFwd() override { fOutput->FastFwd(); } void SetPosition(uint32_t pos) override { fOutput->SetPosition(pos); } void Skip(uint32_t deltaByteCount) override { fOutput->Skip(deltaByteCount); } diff --git a/Sources/Plasma/PubUtilLib/plCompression/plZlibStream.cpp b/Sources/Plasma/PubUtilLib/plCompression/plZlibStream.cpp index b334a9f3a8..ecd9290285 100644 --- a/Sources/Plasma/PubUtilLib/plCompression/plZlibStream.cpp +++ b/Sources/Plasma/PubUtilLib/plCompression/plZlibStream.cpp @@ -181,6 +181,11 @@ void plZlibStream::FastFwd() hsAssert(0, "FastFwd not supported"); } +void plZlibStream::Truncate() +{ + hsAssert(false, "Truncate not supported"); +} + uint32_t plZlibStream::GetEOF() { hsAssert(0, "GetEOF not supported"); diff --git a/Sources/Plasma/PubUtilLib/plCompression/plZlibStream.h b/Sources/Plasma/PubUtilLib/plCompression/plZlibStream.h index 0b65eee25e..8ded7cc5be 100644 --- a/Sources/Plasma/PubUtilLib/plCompression/plZlibStream.h +++ b/Sources/Plasma/PubUtilLib/plCompression/plZlibStream.h @@ -79,6 +79,7 @@ class plZlibStream : public hsStream void Skip(uint32_t deltaByteCount) override; void Rewind() override; void FastFwd() override; + void Truncate() override; uint32_t GetEOF() override; }; diff --git a/Sources/Plasma/PubUtilLib/plFile/plEncryptedStream.cpp b/Sources/Plasma/PubUtilLib/plFile/plEncryptedStream.cpp index 708e23ce43..32c71e6086 100644 --- a/Sources/Plasma/PubUtilLib/plFile/plEncryptedStream.cpp +++ b/Sources/Plasma/PubUtilLib/plFile/plEncryptedStream.cpp @@ -283,6 +283,16 @@ void plEncryptedStream::FastFwd() } } +void plEncryptedStream::Truncate() +{ + if (fOpenMode != kOpenWrite) { + hsAssert(false, "Trying to write to a read stream"); + return; + } + + return fRAMStream->Truncate(); +} + uint32_t plEncryptedStream::GetEOF() { return fActualFileSize; diff --git a/Sources/Plasma/PubUtilLib/plFile/plEncryptedStream.h b/Sources/Plasma/PubUtilLib/plFile/plEncryptedStream.h index 1ace5c2f8d..a978c1a80f 100644 --- a/Sources/Plasma/PubUtilLib/plFile/plEncryptedStream.h +++ b/Sources/Plasma/PubUtilLib/plFile/plEncryptedStream.h @@ -91,6 +91,7 @@ class plEncryptedStream : public hsStream void Skip(uint32_t deltaByteCount) override; void Rewind() override; void FastFwd() override; + void Truncate() override; uint32_t GetEOF() override; uint32_t GetActualFileSize() const { return fActualFileSize;} diff --git a/Sources/Plasma/PubUtilLib/plFile/plSecureStream.cpp b/Sources/Plasma/PubUtilLib/plFile/plSecureStream.cpp index cd568ced7f..482eb305b8 100644 --- a/Sources/Plasma/PubUtilLib/plFile/plSecureStream.cpp +++ b/Sources/Plasma/PubUtilLib/plFile/plSecureStream.cpp @@ -420,6 +420,16 @@ void plSecureStream::FastFwd() } } +void plSecureStream::Truncate() +{ + if (fOpenMode != kOpenWrite) { + hsAssert(false, "Trying to write to a read stream"); + return; + } + + return fRAMStream->Truncate(); +} + uint32_t plSecureStream::GetEOF() { return fActualFileSize; diff --git a/Sources/Plasma/PubUtilLib/plFile/plSecureStream.h b/Sources/Plasma/PubUtilLib/plFile/plSecureStream.h index 88f79b8619..c568a4ec6d 100644 --- a/Sources/Plasma/PubUtilLib/plFile/plSecureStream.h +++ b/Sources/Plasma/PubUtilLib/plFile/plSecureStream.h @@ -103,6 +103,7 @@ class plSecureStream: public hsStream void Skip(uint32_t deltaByteCount) override; void Rewind() override; void FastFwd() override; + void Truncate() override; uint32_t GetEOF() override; uint32_t GetActualFileSize() const {return fActualFileSize;} diff --git a/Sources/Plasma/PubUtilLib/plNetMessage/plNetMsgHelpers.cpp b/Sources/Plasma/PubUtilLib/plNetMessage/plNetMsgHelpers.cpp index 8e5eab7af6..9e50842cc1 100644 --- a/Sources/Plasma/PubUtilLib/plNetMessage/plNetMsgHelpers.cpp +++ b/Sources/Plasma/PubUtilLib/plNetMessage/plNetMsgHelpers.cpp @@ -240,7 +240,7 @@ void plNetMsgStreamHelper::IAllocStream(uint32_t len) fStreamBuf = new uint8_t[len]; } -void plNetMsgStreamHelper::CopyStream(hsStream* ssStream) +void plNetMsgStreamHelper::CopyStream(hsRAMStream* ssStream) { uint32_t len=ssStream->GetEOF(); IAllocStream(len); diff --git a/Sources/Plasma/PubUtilLib/plNetMessage/plNetMsgHelpers.h b/Sources/Plasma/PubUtilLib/plNetMessage/plNetMsgHelpers.h index 6f0f4b830a..f3970ab976 100644 --- a/Sources/Plasma/PubUtilLib/plNetMessage/plNetMsgHelpers.h +++ b/Sources/Plasma/PubUtilLib/plNetMessage/plNetMsgHelpers.h @@ -59,6 +59,7 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #include "plNetCommon/plClientGuid.h" class plKey; +class hsRAMStream; class hsStream; class hsStreamable; @@ -157,7 +158,7 @@ class plNetMsgStreamHelper : public plCreatable // copiers void CopyFrom(const plNetMsgStreamHelper* other); - void CopyStream(hsStream* ssStream); // copies to fStream + void CopyStream(hsRAMStream* ssStream); // copies to fStream void CopyStream(int32_t len, const void* buf); // copies to fStream // setters diff --git a/Sources/Tools/MaxComponent/plMultistageBehComponent.cpp b/Sources/Tools/MaxComponent/plMultistageBehComponent.cpp index 0860c535e7..a26c8f2190 100644 --- a/Sources/Tools/MaxComponent/plMultistageBehComponent.cpp +++ b/Sources/Tools/MaxComponent/plMultistageBehComponent.cpp @@ -489,8 +489,11 @@ class MaxStream : public hsStream // Don't support any of this bool Open(const plFileName &, const char * = "rb") override { hsAssert(0, "Not supported"); return false; } bool Close() override { hsAssert(0, "Not supported"); return false; } + bool AtEnd() override { hsAssert(false, "Not supported"); return false; } void Skip(uint32_t deltaByteCount) override { hsAssert(0, "Not supported"); } void Rewind() override { hsAssert(0, "Not supported"); } + void FastFwd() override { hsAssert(false, "Not supported"); } + void Truncate() override { hsAssert(false, "Not supported"); } uint32_t GetEOF() override { return (uint32_t)fLoad->CurChunkLength(); } From a6b5c4237117d597e031647bed3b32a000000918 Mon Sep 17 00:00:00 2001 From: dgelessus Date: Mon, 5 Jun 2023 22:39:34 +0200 Subject: [PATCH 02/13] Unify hsRAMStream and hsVectorStream Keeping the hsRAMStream name (the more common one of the two), but replacing it with the hsVectorStream implementation, which supports a few more methods and uses std::vector instead of the custom hsAppender. --- Sources/Plasma/CoreLib/CMakeLists.txt | 2 - Sources/Plasma/CoreLib/hsSTLStream.cpp | 142 ------------------ Sources/Plasma/CoreLib/hsSTLStream.h | 87 ----------- Sources/Plasma/CoreLib/hsStream.cpp | 92 +++++++----- Sources/Plasma/CoreLib/hsStream.h | 27 +++- .../PubUtilLib/plFile/plEncryptedStream.cpp | 6 +- .../PubUtilLib/plFile/plSecureStream.cpp | 6 +- .../PubUtilLib/plPhysX/plGenericPhysical.cpp | 1 - .../Plasma/PubUtilLib/plPhysX/plPXPhysical.h | 4 +- .../PubUtilLib/plResMgr/plResManager.cpp | 1 - Sources/Plasma/PubUtilLib/plVault/Pch.h | 1 - .../PubUtilLib/plVault/plVaultNodeAccess.cpp | 2 +- Sources/Tools/MaxMain/Pch.h | 1 - Sources/Tools/MaxMain/plMaxNode.cpp | 5 +- 14 files changed, 82 insertions(+), 295 deletions(-) delete mode 100644 Sources/Plasma/CoreLib/hsSTLStream.cpp delete mode 100644 Sources/Plasma/CoreLib/hsSTLStream.h diff --git a/Sources/Plasma/CoreLib/CMakeLists.txt b/Sources/Plasma/CoreLib/CMakeLists.txt index 7fdb8b375b..a3ce5ed3dc 100644 --- a/Sources/Plasma/CoreLib/CMakeLists.txt +++ b/Sources/Plasma/CoreLib/CMakeLists.txt @@ -12,7 +12,6 @@ set(CoreLib_SOURCES hsMemory.cpp hsQuat.cpp hsRefCnt.cpp - hsSTLStream.cpp hsStream.cpp hsStringTokenizer.cpp hsSystemInfo.cpp @@ -55,7 +54,6 @@ set(CoreLib_HEADERS hsQuat.h hsRefCnt.h hsSIMD.h - hsSTLStream.h hsStream.h hsStringTokenizer.h hsSystemInfo.h diff --git a/Sources/Plasma/CoreLib/hsSTLStream.cpp b/Sources/Plasma/CoreLib/hsSTLStream.cpp deleted file mode 100644 index cd0f5c6d1d..0000000000 --- a/Sources/Plasma/CoreLib/hsSTLStream.cpp +++ /dev/null @@ -1,142 +0,0 @@ -/*==LICENSE==* - -CyanWorlds.com Engine - MMOG client, server and tools -Copyright (C) 2011 Cyan Worlds, Inc. - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . - -Additional permissions under GNU GPL version 3 section 7 - -If you modify this Program, or any covered work, by linking or -combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK, -NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent -JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK -(or a modified version of those libraries), -containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA, -PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG -JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the -licensors of this Program grant you additional -permission to convey the resulting work. Corresponding Source for a -non-source form of such a combination shall include the source code for -the parts of OpenSSL and IJG JPEG Library used as well as that of the covered -work. - -You can contact Cyan Worlds, Inc. by email legal@cyan.com - or by snail mail at: - Cyan Worlds, Inc. - 14617 N Newport Hwy - Mead, WA 99021 - -*==LICENSE==*/ -#include "hsSTLStream.h" - -bool hsVectorStream::AtEnd() -{ - return (fBytesRead >= fEnd); -} - -uint32_t hsVectorStream::Read(uint32_t byteCount, void *buffer) -{ - if (fBytesRead + byteCount > fEnd) - { -// hsStatusMessageF("Reading past end of hsVectorStream (read %u of %u requested bytes)", fEnd-fBytesRead, byteCount); - byteCount = fEnd - fBytesRead; - } - - memcpy(buffer, &fVector[fBytesRead], byteCount); - - fBytesRead += byteCount; - fPosition += byteCount; - - return byteCount; -} - -uint32_t hsVectorStream::Write(uint32_t byteCount, const void* buffer) -{ - // If we are at the end of the vector, we can just do a block insert of the data - if (fPosition == fVector.size()) - fVector.insert(fVector.end(), (uint8_t*)buffer, (uint8_t*)buffer+byteCount); - // If we are in the middle, I don't know how to just overwrite a block of the vector. - // So, we make sure there is enough space and copy the elements one by one - else - { - fVector.reserve(fPosition+byteCount); - for (uint32_t i = 0; i < byteCount; i++) - fVector[fPosition+i] = ((uint8_t*)buffer)[i]; - } - - fPosition += byteCount; - - if (fPosition > fEnd) - fEnd = fPosition; - - return byteCount; -} - -void hsVectorStream::Skip(uint32_t deltaByteCount) -{ - fBytesRead += deltaByteCount; - fPosition += deltaByteCount; -} - -void hsVectorStream::Rewind() -{ - fBytesRead = 0; - fPosition = 0; -} - -void hsVectorStream::FastFwd() -{ - fBytesRead = fPosition = fEnd; -} - -void hsVectorStream::Truncate() -{ - fVector.erase(fVector.begin()+fPosition, fVector.end()); - fEnd = fPosition-1; -} - -uint32_t hsVectorStream::GetEOF() -{ - return fEnd; -} - -void hsVectorStream::CopyToMem(void* mem) -{ - memcpy(mem, &fVector[0], fEnd); -} - -void hsVectorStream::Erase(uint32_t bytes) -{ - hsAssert(fPosition+bytes <= fEnd, "Erasing past end of stream"); - - fVector.erase(fVector.begin()+fPosition, fVector.begin()+fPosition+bytes); - fEnd -= bytes; -} - -void hsVectorStream::Reset() -{ - fBytesRead = 0; - fPosition = 0; - fEnd = 0; - fVector.clear(); -} - -const void *hsVectorStream::GetData() -{ - if (fVector.size() > 0) - return &fVector[0]; - else - return nullptr; -} diff --git a/Sources/Plasma/CoreLib/hsSTLStream.h b/Sources/Plasma/CoreLib/hsSTLStream.h deleted file mode 100644 index 80c9ba173b..0000000000 --- a/Sources/Plasma/CoreLib/hsSTLStream.h +++ /dev/null @@ -1,87 +0,0 @@ -/*==LICENSE==* - -CyanWorlds.com Engine - MMOG client, server and tools -Copyright (C) 2011 Cyan Worlds, Inc. - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . - -Additional permissions under GNU GPL version 3 section 7 - -If you modify this Program, or any covered work, by linking or -combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK, -NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent -JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK -(or a modified version of those libraries), -containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA, -PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG -JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the -licensors of this Program grant you additional -permission to convey the resulting work. Corresponding Source for a -non-source form of such a combination shall include the source code for -the parts of OpenSSL and IJG JPEG Library used as well as that of the covered -work. - -You can contact Cyan Worlds, Inc. by email legal@cyan.com - or by snail mail at: - Cyan Worlds, Inc. - 14617 N Newport Hwy - Mead, WA 99021 - -*==LICENSE==*/ - -#ifndef _hsSTLStream_h_inc_ -#define _hsSTLStream_h_inc_ - -#include "hsStream.h" - -// -// In-memory only -// Erase function lets you cut a chunk out of the middle of the stream -// -class hsVectorStream : public hsStream -{ -protected: - std::vector fVector; - uint32_t fEnd; // End of file (one past the last byte) - -public: - hsVectorStream() : fEnd() { } - hsVectorStream(uint32_t chunkSize) : fEnd() { fVector.reserve(chunkSize); }; - - bool Open(const plFileName &, const char *) override { hsAssert(0, "hsVectorStream::Open Not Implemented"); return false; } - bool Close() override { hsAssert(0, "hsVectorStream::Close Not Implemented"); return false; } - - bool AtEnd() override; - uint32_t Read(uint32_t byteCount, void * buffer) override; - uint32_t Write(uint32_t byteCount, const void* buffer) override; - void Skip(uint32_t deltaByteCount) override; - void Rewind() override; - void FastFwd() override; - void Truncate() override; - - uint32_t GetEOF() override; - void CopyToMem(void* mem); - - virtual void Reset(); // clears the buffers - - // Erase number of bytes at the current position - virtual void Erase(uint32_t bytes); - // A pointer to the beginning of the data in the stream. This is only valid - // until someone modifies the stream. - const void *GetData(); - // In case you want to try and be efficient with your memory allocations - void Reserve(uint32_t bytes) { fVector.reserve(bytes); } -}; - -#endif // _hsSTLStream_h_inc_ diff --git a/Sources/Plasma/CoreLib/hsStream.cpp b/Sources/Plasma/CoreLib/hsStream.cpp index 8c61bb2b3e..0ad313c020 100644 --- a/Sources/Plasma/CoreLib/hsStream.cpp +++ b/Sources/Plasma/CoreLib/hsStream.cpp @@ -43,7 +43,6 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #include "hsStream.h" #include "hsExceptions.h" -#include "hsMemory.h" #include #if HS_BUILD_FOR_WIN32 @@ -630,90 +629,105 @@ uint32_t plReadOnlySubStream::GetEOF() ////////////////////////////////////////////////////////////////////////////////////// -#define kRAMStreamChunkSize 1024 - -hsRAMStream::hsRAMStream() : fAppender(1, kRAMStreamChunkSize) -{ - fIter.ResetToHead(&fAppender); -} - -hsRAMStream::hsRAMStream(uint32_t chunkSize) : fAppender(1, chunkSize) -{ - fIter.ResetToHead(&fAppender); -} - -hsRAMStream::~hsRAMStream() -{ -} - -void hsRAMStream::Reset() -{ - fBytesRead = 0; - fPosition = 0; - - fAppender.Reset(); - fIter.ResetToHead(&fAppender); -} - bool hsRAMStream::AtEnd() { - return (fBytesRead >= fAppender.Count() * fAppender.ElemSize()); + return (fBytesRead >= fEnd); } uint32_t hsRAMStream::Read(uint32_t byteCount, void * buffer) { - if (fBytesRead + byteCount > fAppender.Count() * fAppender.ElemSize()) - byteCount = (fAppender.Count() * fAppender.ElemSize()) - fBytesRead; + if (fBytesRead + byteCount > fEnd) { +// hsStatusMessageF("Reading past end of hsRAMStream (read %u of %u requested bytes)", fEnd-fBytesRead, byteCount); + byteCount = fEnd - fBytesRead; + } + + memcpy(buffer, &fVector[fBytesRead], byteCount); fBytesRead += byteCount; fPosition += byteCount; - fIter.Next(byteCount, buffer); - return byteCount; } uint32_t hsRAMStream::Write(uint32_t byteCount, const void* buffer) { + if (fPosition == fVector.size()) { + // If we are at the end of the vector, we can just do a block insert of the data + fVector.insert(fVector.end(), (uint8_t *)buffer, (uint8_t *)buffer + byteCount); + } else { + // If we are in the middle, I don't know how to just overwrite a block of the vector. + // So, we make sure there is enough space and copy the elements one by one + fVector.reserve(fPosition+byteCount); + for (uint32_t i = 0; i < byteCount; i++) { + fVector[fPosition + i] = ((uint8_t *)buffer)[i]; + } + } + fPosition += byteCount; - fAppender.PushTail(byteCount, buffer); + if (fPosition > fEnd) { + fEnd = fPosition; + } return byteCount; } void hsRAMStream::Skip(uint32_t deltaByteCount) { + fBytesRead += deltaByteCount; fPosition += deltaByteCount; - fIter.Next(deltaByteCount, nullptr); } void hsRAMStream::Rewind() { fBytesRead = 0; fPosition = 0; - fIter.ResetToHead(&fAppender); } void hsRAMStream::FastFwd() { - fBytesRead = fPosition = GetEOF(); - fIter.ResetToTail(&fAppender); + fBytesRead = fPosition = fEnd; } void hsRAMStream::Truncate() { - Reset(); + fVector.erase(fVector.begin()+fPosition, fVector.end()); + fEnd = fPosition-1; } uint32_t hsRAMStream::GetEOF() { - return fAppender.Count() * fAppender.ElemSize(); + return fEnd; } void hsRAMStream::CopyToMem(void* mem) { - (void)fAppender.CopyInto(mem); + memcpy(mem, &fVector[0], fEnd); +} + +void hsRAMStream::Erase(uint32_t bytes) +{ + hsAssert(fPosition+bytes <= fEnd, "Erasing past end of stream"); + + fVector.erase(fVector.begin()+fPosition, fVector.begin()+fPosition+bytes); + fEnd -= bytes; +} + +void hsRAMStream::Reset() +{ + fBytesRead = 0; + fPosition = 0; + fEnd = 0; + fVector.clear(); +} + +const void *hsRAMStream::GetData() +{ + if (fVector.size() > 0) { + return &fVector[0]; + } else { + return nullptr; + } } ////////////////////////////////////////////////////////////////////// diff --git a/Sources/Plasma/CoreLib/hsStream.h b/Sources/Plasma/CoreLib/hsStream.h index d576d6ba00..5e681756a1 100644 --- a/Sources/Plasma/CoreLib/hsStream.h +++ b/Sources/Plasma/CoreLib/hsStream.h @@ -43,9 +43,9 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #define hsStream_Defined #include "HeadSpin.h" -#include "hsMemory.h" #include "plFileSystem.h" #include +#include class hsStream { public: @@ -210,13 +210,18 @@ class plReadOnlySubStream: public hsStream uint32_t GetEOF() override; }; -class hsRAMStream : public hsStream { - hsAppender fAppender; - hsAppenderIterator fIter; +// +// In-memory only +// Erase function lets you cut a chunk out of the middle of the stream +// +class hsRAMStream : public hsStream +{ + std::vector fVector; + uint32_t fEnd; // End of file (one past the last byte) + public: - hsRAMStream(); - hsRAMStream(uint32_t chunkSize); - virtual ~hsRAMStream(); + hsRAMStream() : fEnd() {} + hsRAMStream(uint32_t chunkSize) : fEnd() { fVector.reserve(chunkSize); }; bool Open(const plFileName &, const char *) override { hsAssert(0, "hsRAMStream::Open NotImplemented"); return false; } bool Close() override { return false; } @@ -234,6 +239,14 @@ class hsRAMStream : public hsStream { void CopyToMem(void* mem); void Reset(); // clears the buffers + + // Erase number of bytes at the current position + void Erase(uint32_t bytes); + // A pointer to the beginning of the data in the stream. This is only valid + // until someone modifies the stream. + const void* GetData(); + // In case you want to try and be efficient with your memory allocations + void Reserve(uint32_t bytes) { fVector.reserve(bytes); } }; class hsNullStream : public hsStream { diff --git a/Sources/Plasma/PubUtilLib/plFile/plEncryptedStream.cpp b/Sources/Plasma/PubUtilLib/plFile/plEncryptedStream.cpp index 32c71e6086..f860575eac 100644 --- a/Sources/Plasma/PubUtilLib/plFile/plEncryptedStream.cpp +++ b/Sources/Plasma/PubUtilLib/plFile/plEncryptedStream.cpp @@ -41,8 +41,6 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com *==LICENSE==*/ #include "plEncryptedStream.h" -#include "hsSTLStream.h" - #include #include #include @@ -147,7 +145,7 @@ bool plEncryptedStream::Open(const plFileName& name, const char* mode) } else if (strcmp(mode, "wb") == 0) { - fRAMStream = new hsVectorStream; + fRAMStream = new hsRAMStream; fWriteFileName = name; fPosition = 0; @@ -216,7 +214,7 @@ uint32_t plEncryptedStream::IRead(uint32_t bytes, void* buffer) void plEncryptedStream::IBufferFile() { - fRAMStream = new hsVectorStream; + fRAMStream = new hsRAMStream; char buf[1024]; while (!AtEnd()) { diff --git a/Sources/Plasma/PubUtilLib/plFile/plSecureStream.cpp b/Sources/Plasma/PubUtilLib/plFile/plSecureStream.cpp index 482eb305b8..a4fa936d83 100644 --- a/Sources/Plasma/PubUtilLib/plFile/plSecureStream.cpp +++ b/Sources/Plasma/PubUtilLib/plFile/plSecureStream.cpp @@ -45,8 +45,6 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #include "plSecureStream.h" #include "hsWindows.h" -#include "hsSTLStream.h" - #if !HS_BUILD_FOR_WIN32 #include #define INVALID_HANDLE_VALUE nullptr @@ -219,7 +217,7 @@ bool plSecureStream::Open(const plFileName& name, const char* mode) } else if (strcmp(mode, "wb") == 0) { - fRAMStream = new hsVectorStream; + fRAMStream = new hsRAMStream; fWriteFileName = name; fPosition = 0; @@ -338,7 +336,7 @@ uint32_t plSecureStream::IRead(uint32_t bytes, void* buffer) void plSecureStream::IBufferFile() { - fRAMStream = new hsVectorStream; + fRAMStream = new hsRAMStream; char buf[1024]; while (!AtEnd()) { diff --git a/Sources/Plasma/PubUtilLib/plPhysX/plGenericPhysical.cpp b/Sources/Plasma/PubUtilLib/plPhysX/plGenericPhysical.cpp index 35caa607d7..a244f8e1c6 100644 --- a/Sources/Plasma/PubUtilLib/plPhysX/plGenericPhysical.cpp +++ b/Sources/Plasma/PubUtilLib/plPhysX/plGenericPhysical.cpp @@ -48,7 +48,6 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #include "plProfile.h" #include "hsQuat.h" #include "hsResMgr.h" -#include "hsSTLStream.h" #include "hsTimer.h" #include "pnMessage/plCorrectionMsg.h" diff --git a/Sources/Plasma/PubUtilLib/plPhysX/plPXPhysical.h b/Sources/Plasma/PubUtilLib/plPhysX/plPXPhysical.h index cfdeb4bd45..9ae4312c51 100644 --- a/Sources/Plasma/PubUtilLib/plPhysX/plPXPhysical.h +++ b/Sources/Plasma/PubUtilLib/plPhysX/plPXPhysical.h @@ -63,11 +63,11 @@ class plPhysicalProxy; struct hsPlane3; struct hsPoint3; class hsQuat; +class hsRAMStream; class plPhysicalSndGroup; class plSceneObject; class plSDLModifier; class plSimulationMsg; -class hsVectorStream; namespace physx { @@ -106,7 +106,7 @@ class PhysRecipe hsPoint3 bDimensions; hsPoint3 bOffset; - std::unique_ptr meshStream; + std::unique_ptr meshStream; PhysRecipe(); ~PhysRecipe(); diff --git a/Sources/Plasma/PubUtilLib/plResMgr/plResManager.cpp b/Sources/Plasma/PubUtilLib/plResMgr/plResManager.cpp index 795fbf2d24..2005765758 100644 --- a/Sources/Plasma/PubUtilLib/plResMgr/plResManager.cpp +++ b/Sources/Plasma/PubUtilLib/plResMgr/plResManager.cpp @@ -46,7 +46,6 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #include "plResManagerHelper.h" #include "plResMgrSettings.h" -#include "hsSTLStream.h" #include "hsTimer.h" #include "plTimerCallbackManager.h" diff --git a/Sources/Plasma/PubUtilLib/plVault/Pch.h b/Sources/Plasma/PubUtilLib/plVault/Pch.h index 0bf0e52ec3..7ef02ffad5 100644 --- a/Sources/Plasma/PubUtilLib/plVault/Pch.h +++ b/Sources/Plasma/PubUtilLib/plVault/Pch.h @@ -63,7 +63,6 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #include #include "hsGeometry3.h" -#include "hsSTLStream.h" #include "hsStringTokenizer.h" #include "hsTimer.h" diff --git a/Sources/Plasma/PubUtilLib/plVault/plVaultNodeAccess.cpp b/Sources/Plasma/PubUtilLib/plVault/plVaultNodeAccess.cpp index c68c0ea814..1f5cbcfdbd 100644 --- a/Sources/Plasma/PubUtilLib/plVault/plVaultNodeAccess.cpp +++ b/Sources/Plasma/PubUtilLib/plVault/plVaultNodeAccess.cpp @@ -456,7 +456,7 @@ void VaultMarkerGameNode::GetMarkerData(std::vector& data) const //============================================================================ void VaultMarkerGameNode::SetMarkerData(const std::vector& data) { - hsVectorStream stream; + hsRAMStream stream; stream.WriteLE32((uint32_t)data.size()); for (const VaultMarker& marker : data) { stream.WriteLE32(marker.id); diff --git a/Sources/Tools/MaxMain/Pch.h b/Sources/Tools/MaxMain/Pch.h index aa7e67d680..e3c9a7c3fc 100644 --- a/Sources/Tools/MaxMain/Pch.h +++ b/Sources/Tools/MaxMain/Pch.h @@ -66,7 +66,6 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #include "plLoadMask.h" #include "hsMatrix44.h" #include "plRenderLevel.h" -#include "hsSTLStream.h" #include "hsStream.h" #include "hsStringTokenizer.h" #include "plFileSystem.h" diff --git a/Sources/Tools/MaxMain/plMaxNode.cpp b/Sources/Tools/MaxMain/plMaxNode.cpp index 5f9b2e5c95..0d25f7af2b 100644 --- a/Sources/Tools/MaxMain/plMaxNode.cpp +++ b/Sources/Tools/MaxMain/plMaxNode.cpp @@ -47,7 +47,6 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #include "hsFastMath.h" #include "pnKeyedObject/plKey.h" #include "plRenderLevel.h" -#include "hsSTLStream.h" #include "plMaxNode.h" #include "plMaxNodeData.h" @@ -708,7 +707,7 @@ bool plMaxNode::MakePhysical(plErrorMsg *pErrMsg, plConvertSettings *settings) case plSimDefs::kProxyBounds: case plSimDefs::kExplicitBounds: { - recipe.meshStream = std::make_unique(); + recipe.meshStream = std::make_unique(); plPXCooking::WriteTriMesh(recipe.meshStream.get(), mesh.fNumFaces, mesh.fFaces, mesh.fNumVerts, mesh.fVerts); recipe.meshStream->Rewind(); @@ -741,7 +740,7 @@ bool plMaxNode::MakePhysical(plErrorMsg *pErrMsg, plConvertSettings *settings) break; case plSimDefs::kHullBounds: { - recipe.meshStream = std::make_unique(); + recipe.meshStream = std::make_unique(); plPXCooking::WriteConvexHull(recipe.meshStream.get(), mesh.fNumVerts, mesh.fVerts); recipe.meshStream->Rewind(); From 2d6508dc46b09e7f05880ac428b9c3631bc16bf4 Mon Sep 17 00:00:00 2001 From: dgelessus Date: Mon, 5 Jun 2023 23:11:27 +0200 Subject: [PATCH 03/13] Fix hsRAMStream::Write when not at end of stream --- Sources/Plasma/CoreLib/hsStream.cpp | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/Sources/Plasma/CoreLib/hsStream.cpp b/Sources/Plasma/CoreLib/hsStream.cpp index 0ad313c020..f44f382e5f 100644 --- a/Sources/Plasma/CoreLib/hsStream.cpp +++ b/Sources/Plasma/CoreLib/hsStream.cpp @@ -651,16 +651,13 @@ uint32_t hsRAMStream::Read(uint32_t byteCount, void * buffer) uint32_t hsRAMStream::Write(uint32_t byteCount, const void* buffer) { - if (fPosition == fVector.size()) { - // If we are at the end of the vector, we can just do a block insert of the data - fVector.insert(fVector.end(), (uint8_t *)buffer, (uint8_t *)buffer + byteCount); + size_t spaceUntilEof = fEnd - fPosition; + if (byteCount <= spaceUntilEof) { + memcpy(fVector.data() + fPosition, buffer, byteCount); } else { - // If we are in the middle, I don't know how to just overwrite a block of the vector. - // So, we make sure there is enough space and copy the elements one by one - fVector.reserve(fPosition+byteCount); - for (uint32_t i = 0; i < byteCount; i++) { - fVector[fPosition + i] = ((uint8_t *)buffer)[i]; - } + memcpy(fVector.data() + fPosition, buffer, spaceUntilEof); + auto buf = static_cast(buffer); + fVector.insert(fVector.end(), buf + spaceUntilEof, buf + byteCount); } fPosition += byteCount; From f94ed27463ca25cbb6f18122116bb03a5dde5135 Mon Sep 17 00:00:00 2001 From: dgelessus Date: Mon, 5 Jun 2023 23:18:07 +0200 Subject: [PATCH 04/13] Replace hsRAMStream::fEnd with fVector.size() --- Sources/Plasma/CoreLib/hsStream.cpp | 25 +++++++++---------------- Sources/Plasma/CoreLib/hsStream.h | 5 ++--- 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/Sources/Plasma/CoreLib/hsStream.cpp b/Sources/Plasma/CoreLib/hsStream.cpp index f44f382e5f..a116d5b5c1 100644 --- a/Sources/Plasma/CoreLib/hsStream.cpp +++ b/Sources/Plasma/CoreLib/hsStream.cpp @@ -631,14 +631,14 @@ uint32_t plReadOnlySubStream::GetEOF() bool hsRAMStream::AtEnd() { - return (fBytesRead >= fEnd); + return (fBytesRead >= fVector.size()); } uint32_t hsRAMStream::Read(uint32_t byteCount, void * buffer) { - if (fBytesRead + byteCount > fEnd) { -// hsStatusMessageF("Reading past end of hsRAMStream (read %u of %u requested bytes)", fEnd-fBytesRead, byteCount); - byteCount = fEnd - fBytesRead; + if (fBytesRead + byteCount > fVector.size()) { +// hsStatusMessageF("Reading past end of hsRAMStream (read %u of %u requested bytes)", fVector.size() - fBytesRead, byteCount); + byteCount = fVector.size() - fBytesRead; } memcpy(buffer, &fVector[fBytesRead], byteCount); @@ -651,7 +651,7 @@ uint32_t hsRAMStream::Read(uint32_t byteCount, void * buffer) uint32_t hsRAMStream::Write(uint32_t byteCount, const void* buffer) { - size_t spaceUntilEof = fEnd - fPosition; + size_t spaceUntilEof = fVector.size() - fPosition; if (byteCount <= spaceUntilEof) { memcpy(fVector.data() + fPosition, buffer, byteCount); } else { @@ -662,10 +662,6 @@ uint32_t hsRAMStream::Write(uint32_t byteCount, const void* buffer) fPosition += byteCount; - if (fPosition > fEnd) { - fEnd = fPosition; - } - return byteCount; } @@ -683,38 +679,35 @@ void hsRAMStream::Rewind() void hsRAMStream::FastFwd() { - fBytesRead = fPosition = fEnd; + fBytesRead = fPosition = fVector.size(); } void hsRAMStream::Truncate() { fVector.erase(fVector.begin()+fPosition, fVector.end()); - fEnd = fPosition-1; } uint32_t hsRAMStream::GetEOF() { - return fEnd; + return fVector.size(); } void hsRAMStream::CopyToMem(void* mem) { - memcpy(mem, &fVector[0], fEnd); + memcpy(mem, &fVector[0], fVector.size()); } void hsRAMStream::Erase(uint32_t bytes) { - hsAssert(fPosition+bytes <= fEnd, "Erasing past end of stream"); + hsAssert(fPosition+bytes <= fVector.size(), "Erasing past end of stream"); fVector.erase(fVector.begin()+fPosition, fVector.begin()+fPosition+bytes); - fEnd -= bytes; } void hsRAMStream::Reset() { fBytesRead = 0; fPosition = 0; - fEnd = 0; fVector.clear(); } diff --git a/Sources/Plasma/CoreLib/hsStream.h b/Sources/Plasma/CoreLib/hsStream.h index 5e681756a1..c2669fd284 100644 --- a/Sources/Plasma/CoreLib/hsStream.h +++ b/Sources/Plasma/CoreLib/hsStream.h @@ -217,11 +217,10 @@ class plReadOnlySubStream: public hsStream class hsRAMStream : public hsStream { std::vector fVector; - uint32_t fEnd; // End of file (one past the last byte) public: - hsRAMStream() : fEnd() {} - hsRAMStream(uint32_t chunkSize) : fEnd() { fVector.reserve(chunkSize); }; + hsRAMStream() {} + hsRAMStream(uint32_t chunkSize) { fVector.reserve(chunkSize); } bool Open(const plFileName &, const char *) override { hsAssert(0, "hsRAMStream::Open NotImplemented"); return false; } bool Close() override { return false; } From 3e7a131d516a11205efab2cd4eb1563594a67f9c Mon Sep 17 00:00:00 2001 From: dgelessus Date: Mon, 5 Jun 2023 23:22:44 +0200 Subject: [PATCH 05/13] Make hsRAMStream use more appropriate std::vector methods --- Sources/Plasma/CoreLib/hsStream.cpp | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/Sources/Plasma/CoreLib/hsStream.cpp b/Sources/Plasma/CoreLib/hsStream.cpp index a116d5b5c1..7746c04369 100644 --- a/Sources/Plasma/CoreLib/hsStream.cpp +++ b/Sources/Plasma/CoreLib/hsStream.cpp @@ -641,7 +641,7 @@ uint32_t hsRAMStream::Read(uint32_t byteCount, void * buffer) byteCount = fVector.size() - fBytesRead; } - memcpy(buffer, &fVector[fBytesRead], byteCount); + memcpy(buffer, fVector.data() + fBytesRead, byteCount); fBytesRead += byteCount; fPosition += byteCount; @@ -684,7 +684,7 @@ void hsRAMStream::FastFwd() void hsRAMStream::Truncate() { - fVector.erase(fVector.begin()+fPosition, fVector.end()); + fVector.resize(fPosition); } uint32_t hsRAMStream::GetEOF() @@ -694,7 +694,7 @@ uint32_t hsRAMStream::GetEOF() void hsRAMStream::CopyToMem(void* mem) { - memcpy(mem, &fVector[0], fVector.size()); + memcpy(mem, fVector.data(), fVector.size()); } void hsRAMStream::Erase(uint32_t bytes) @@ -713,11 +713,7 @@ void hsRAMStream::Reset() const void *hsRAMStream::GetData() { - if (fVector.size() > 0) { - return &fVector[0]; - } else { - return nullptr; - } + return fVector.data(); } ////////////////////////////////////////////////////////////////////// From 2ffb11a562b45272554b210f73c8251a6504e85d Mon Sep 17 00:00:00 2001 From: dgelessus Date: Tue, 6 Jun 2023 00:17:50 +0200 Subject: [PATCH 06/13] Remove hsStream::fBytesRead in favor of fPosition The two were almost always kept in sync, and the few places were they diverged look like bugs. --- Sources/Plasma/CoreLib/hsStream.cpp | 31 +++++-------------- Sources/Plasma/CoreLib/hsStream.h | 11 ++----- .../PubUtilLib/plFile/plEncryptedStream.cpp | 5 +-- .../PubUtilLib/plFile/plSecureStream.cpp | 7 ++--- .../Plasma/PubUtilLib/plGImage/plMipmap.cpp | 4 +-- 5 files changed, 16 insertions(+), 42 deletions(-) diff --git a/Sources/Plasma/CoreLib/hsStream.cpp b/Sources/Plasma/CoreLib/hsStream.cpp index 7746c04369..bbe7dfc8e2 100644 --- a/Sources/Plasma/CoreLib/hsStream.cpp +++ b/Sources/Plasma/CoreLib/hsStream.cpp @@ -453,7 +453,6 @@ uint32_t hsUNIXStream::Read(uint32_t bytes, void* buffer) if (!fRef || !bytes) return 0; size_t numItems = ::fread(buffer, 1 /*size*/, bytes /*count*/, fRef); - fBytesRead += numItems; fPosition += numItems; if (numItems < bytes) { @@ -488,7 +487,6 @@ void hsUNIXStream::SetPosition(uint32_t position) { if (!fRef || (position == fPosition)) return; - fBytesRead = position; fPosition = position; (void)::fseek(fRef, position, SEEK_SET); } @@ -497,7 +495,6 @@ void hsUNIXStream::Skip(uint32_t delta) { if (!fRef) return; - fBytesRead += delta; fPosition += delta; (void)::fseek(fRef, delta, SEEK_CUR); } @@ -506,7 +503,6 @@ void hsUNIXStream::Rewind() { if (!fRef) return; - fBytesRead = 0; fPosition = 0; (void)::fseek(fRef, 0, SEEK_SET); } @@ -516,7 +512,7 @@ void hsUNIXStream::FastFwd() if (!fRef) return; (void)::fseek(fRef, 0, SEEK_END); - fBytesRead = fPosition = ftell(fRef); + fPosition = ftell(fRef); } uint32_t hsUNIXStream::GetEOF() @@ -631,19 +627,18 @@ uint32_t plReadOnlySubStream::GetEOF() bool hsRAMStream::AtEnd() { - return (fBytesRead >= fVector.size()); + return (fPosition >= fVector.size()); } uint32_t hsRAMStream::Read(uint32_t byteCount, void * buffer) { - if (fBytesRead + byteCount > fVector.size()) { -// hsStatusMessageF("Reading past end of hsRAMStream (read %u of %u requested bytes)", fVector.size() - fBytesRead, byteCount); - byteCount = fVector.size() - fBytesRead; + if (fPosition + byteCount > fVector.size()) { +// hsStatusMessageF("Reading past end of hsRAMStream (read %u of %u requested bytes)", fVector.size() - fPosition, byteCount); + byteCount = fVector.size() - fPosition; } - memcpy(buffer, fVector.data() + fBytesRead, byteCount); + memcpy(buffer, fVector.data() + fPosition, byteCount); - fBytesRead += byteCount; fPosition += byteCount; return byteCount; @@ -667,19 +662,17 @@ uint32_t hsRAMStream::Write(uint32_t byteCount, const void* buffer) void hsRAMStream::Skip(uint32_t deltaByteCount) { - fBytesRead += deltaByteCount; fPosition += deltaByteCount; } void hsRAMStream::Rewind() { - fBytesRead = 0; fPosition = 0; } void hsRAMStream::FastFwd() { - fBytesRead = fPosition = fVector.size(); + fPosition = fVector.size(); } void hsRAMStream::Truncate() @@ -706,7 +699,6 @@ void hsRAMStream::Erase(uint32_t bytes) void hsRAMStream::Reset() { - fBytesRead = 0; fPosition = 0; fVector.clear(); } @@ -731,7 +723,6 @@ uint32_t hsNullStream::Read(uint32_t byteCount, void * buffer) uint32_t hsNullStream::Write(uint32_t byteCount, const void* buffer) { - fBytesRead += byteCount; fPosition += byteCount; return byteCount; @@ -739,13 +730,11 @@ uint32_t hsNullStream::Write(uint32_t byteCount, const void* buffer) void hsNullStream::Skip(uint32_t deltaByteCount) { - fBytesRead += deltaByteCount; fPosition += deltaByteCount; } void hsNullStream::Rewind() { - fBytesRead = 0; fPosition = 0; } @@ -773,7 +762,6 @@ uint32_t hsReadOnlyStream::Read(uint32_t byteCount, void* buffer) memmove(buffer, fData, byteCount); fData += byteCount; - fBytesRead += byteCount; fPosition += byteCount; return byteCount; } @@ -786,7 +774,6 @@ uint32_t hsReadOnlyStream::Write(uint32_t byteCount, const void* buffer) void hsReadOnlyStream::Skip(uint32_t deltaByteCount) { - fBytesRead += deltaByteCount; fPosition += deltaByteCount; fData += deltaByteCount; if (fData > fStop) @@ -795,14 +782,13 @@ void hsReadOnlyStream::Skip(uint32_t deltaByteCount) void hsReadOnlyStream::Rewind() { - fBytesRead = 0; fPosition = 0; fData = fStart; } void hsReadOnlyStream::FastFwd() { - fBytesRead = fPosition = GetEOF(); + fPosition = GetEOF(); fData = fStop; } @@ -831,7 +817,6 @@ uint32_t hsWriteOnlyStream::Write(uint32_t byteCount, const void* buffer) hsThrow("Write past end of stream"); memmove(fData, buffer, byteCount); fData += byteCount; - fBytesRead += byteCount; fPosition += byteCount; return byteCount; } diff --git a/Sources/Plasma/CoreLib/hsStream.h b/Sources/Plasma/CoreLib/hsStream.h index c2669fd284..ec5951aef7 100644 --- a/Sources/Plasma/CoreLib/hsStream.h +++ b/Sources/Plasma/CoreLib/hsStream.h @@ -54,12 +54,11 @@ enum { kComment = '#' }; protected: - uint32_t fBytesRead; uint32_t fPosition; bool IsTokenSeparator(char c); public: - hsStream() : fBytesRead(0), fPosition(0) {} + hsStream() : fPosition(0) {} virtual ~hsStream() { } virtual bool Open(const plFileName &, const char * = "rb") = 0; @@ -262,9 +261,8 @@ class hsNullStream : public hsStream { void FastFwd() override; void Truncate() override; - uint32_t GetEOF() override { return GetBytesWritten(); } - uint32_t GetBytesWritten() const { return fBytesRead; } - void Reset( ) { fBytesRead = 0; } + uint32_t GetEOF() override { return fPosition; } + void Reset() { fPosition = 0; } }; // read only mem stream @@ -287,7 +285,6 @@ class hsReadOnlyStream : public hsStream { void Rewind() override; void FastFwd() override; void Truncate() override; - virtual uint32_t GetBytesRead() const { return fBytesRead; } uint32_t GetEOF() override { return (uint32_t)(fStop-fStart); } void CopyToMem(void* mem); }; @@ -302,8 +299,6 @@ class hsWriteOnlyStream : public hsReadOnlyStream { bool Close() override { hsAssert(0, "hsWriteOnlyStream::Close NotImplemented"); return false; } uint32_t Read(uint32_t byteCount, void * buffer) override; // throws exception uint32_t Write(uint32_t byteCount, const void* buffer) override; - uint32_t GetBytesRead() const override { return 0; } - virtual uint32_t GetBytesWritten() const { return fBytesRead; } }; // circular queue stream diff --git a/Sources/Plasma/PubUtilLib/plFile/plEncryptedStream.cpp b/Sources/Plasma/PubUtilLib/plFile/plEncryptedStream.cpp index f860575eac..fdba009b17 100644 --- a/Sources/Plasma/PubUtilLib/plFile/plEncryptedStream.cpp +++ b/Sources/Plasma/PubUtilLib/plFile/plEncryptedStream.cpp @@ -196,7 +196,6 @@ uint32_t plEncryptedStream::IRead(uint32_t bytes, void* buffer) if (!fRef) return 0; int numItems = (int)(::fread(buffer, 1 /*size*/, bytes /*count*/, fRef)); - fBytesRead += numItems; fPosition += numItems; if ((unsigned)numItems < bytes) { if (feof(fRef)) { @@ -246,7 +245,6 @@ void plEncryptedStream::Skip(uint32_t delta) } else if (fRef) { - fBytesRead += delta; fPosition += delta; fseek(fRef, delta, SEEK_CUR); } @@ -261,7 +259,6 @@ void plEncryptedStream::Rewind() } else if (fRef) { - fBytesRead = 0; fPosition = 0; fseek(fRef, kFileStartOffset, SEEK_SET); } @@ -277,7 +274,7 @@ void plEncryptedStream::FastFwd() else if (fRef) { fseek(fRef, kFileStartOffset+fActualFileSize, SEEK_SET); - fBytesRead = fPosition = ftell(fRef); + fPosition = ftell(fRef); } } diff --git a/Sources/Plasma/PubUtilLib/plFile/plSecureStream.cpp b/Sources/Plasma/PubUtilLib/plFile/plSecureStream.cpp index a4fa936d83..7efec02bc0 100644 --- a/Sources/Plasma/PubUtilLib/plFile/plSecureStream.cpp +++ b/Sources/Plasma/PubUtilLib/plFile/plSecureStream.cpp @@ -311,7 +311,6 @@ uint32_t plSecureStream::IRead(uint32_t bytes, void* buffer) numItems = fread(buffer, bytes, 1, fRef); bool success = numItems != 0; #endif - fBytesRead += numItems; fPosition += numItems; if ((unsigned)numItems < bytes) { @@ -372,7 +371,6 @@ void plSecureStream::Skip(uint32_t delta) } else if (fRef != INVALID_HANDLE_VALUE) { - fBytesRead += delta; fPosition += delta; #if HS_BUILD_FOR_WIN32 SetFilePointer(fRef, delta, nullptr, FILE_CURRENT); @@ -391,7 +389,6 @@ void plSecureStream::Rewind() } else if (fRef != INVALID_HANDLE_VALUE) { - fBytesRead = 0; fPosition = 0; #if HS_BUILD_FOR_WIN32 SetFilePointer(fRef, kFileStartOffset, nullptr, FILE_BEGIN); @@ -411,9 +408,9 @@ void plSecureStream::FastFwd() else if (fRef != INVALID_HANDLE_VALUE) { #if HS_BUILD_FOR_WIN32 - fBytesRead = fPosition = SetFilePointer(fRef, kFileStartOffset + fActualFileSize, nullptr, FILE_BEGIN); + fPosition = SetFilePointer(fRef, kFileStartOffset + fActualFileSize, nullptr, FILE_BEGIN); #elif HS_BUILD_FOR_UNIX - fBytesRead = fPosition = fseek(fRef, 0, SEEK_END); + fPosition = fseek(fRef, 0, SEEK_END); #endif } } diff --git a/Sources/Plasma/PubUtilLib/plGImage/plMipmap.cpp b/Sources/Plasma/PubUtilLib/plGImage/plMipmap.cpp index 579dbe954b..21536f363a 100644 --- a/Sources/Plasma/PubUtilLib/plGImage/plMipmap.cpp +++ b/Sources/Plasma/PubUtilLib/plGImage/plMipmap.cpp @@ -561,12 +561,12 @@ void plMipmap::IWriteJPEGImage( hsStream *stream ) hsNullStream *nullStream = new hsNullStream(); IWriteRLEImage(nullStream,this); - if (nullStream->GetBytesWritten() < 5120) // we use RLE if it can get the image size under 5k, otherwise we use JPEG + if (nullStream->GetPosition() < 5120) // we use RLE if it can get the image size under 5k, otherwise we use JPEG flags |= kColorDataRLE; delete nullStream; nullStream = new hsNullStream(); IWriteRLEImage(nullStream,alpha); - if (nullStream->GetBytesWritten() < 5120) + if (nullStream->GetPosition() < 5120) flags |= kAlphaDataRLE; delete nullStream; stream->WriteByte(flags); From 76a9dea1058014b4b99502c5dd2dd9eb72233b45 Mon Sep 17 00:00:00 2001 From: dgelessus Date: Tue, 6 Jun 2023 00:27:45 +0200 Subject: [PATCH 07/13] Remove unused and unimplemented hsStream::IsCompressed --- Sources/Plasma/CoreLib/hsStream.h | 1 - 1 file changed, 1 deletion(-) diff --git a/Sources/Plasma/CoreLib/hsStream.h b/Sources/Plasma/CoreLib/hsStream.h index ec5951aef7..b0dd6ed700 100644 --- a/Sources/Plasma/CoreLib/hsStream.h +++ b/Sources/Plasma/CoreLib/hsStream.h @@ -76,7 +76,6 @@ enum { virtual uint32_t GetEOF() = 0; uint32_t GetSizeLeft(); - virtual bool IsCompressed() { return false; } uint32_t WriteString(const ST::string & string) { return Write((uint32_t)string.size(), string.c_str()); } From 9cab03df16ad1652ad7a67aadfdb65e37ab473f7 Mon Sep 17 00:00:00 2001 From: dgelessus Date: Tue, 6 Jun 2023 01:10:45 +0200 Subject: [PATCH 08/13] Inline rarely used hsStream::WriteFmt This uncovered some missing string_theory includes in a few places. --- Sources/Plasma/Apps/plPageInfo/plPageInfo.cpp | 2 ++ Sources/Plasma/CoreLib/hsStream.cpp | 5 +++-- Sources/Plasma/CoreLib/hsStream.h | 6 ++---- Sources/Plasma/FeatureLib/pfAnimation/plAnimDebugList.cpp | 2 ++ Sources/Plasma/NucleusLib/pnFactory/plFactory.cpp | 1 + Sources/Plasma/NucleusLib/pnNetCommon/plNetAddress.cpp | 1 + .../Plasma/PubUtilLib/plAgeDescription/plAgeDescription.cpp | 2 +- Sources/Plasma/PubUtilLib/plAgeLoader/plResPatcher.cpp | 2 ++ Sources/Plasma/PubUtilLib/plContainer/plConfigInfo.cpp | 1 + .../Plasma/PubUtilLib/plContainer/plConfigInfoLogging.cpp | 2 ++ .../Plasma/PubUtilLib/plStatGather/plProfileManagerFull.cpp | 2 ++ Sources/Tools/MaxExport/plExportDlg.cpp | 4 ++-- 12 files changed, 21 insertions(+), 9 deletions(-) diff --git a/Sources/Plasma/Apps/plPageInfo/plPageInfo.cpp b/Sources/Plasma/Apps/plPageInfo/plPageInfo.cpp index 985066e345..3ac18f90ae 100644 --- a/Sources/Plasma/Apps/plPageInfo/plPageInfo.cpp +++ b/Sources/Plasma/Apps/plPageInfo/plPageInfo.cpp @@ -44,6 +44,8 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #include "hsStream.h" #include "hsTimer.h" +#include + #include "pnKeyedObject/plKeyImp.h" #include "plAgeDescription/plAgeManifest.h" diff --git a/Sources/Plasma/CoreLib/hsStream.cpp b/Sources/Plasma/CoreLib/hsStream.cpp index bbe7dfc8e2..dc23a381d8 100644 --- a/Sources/Plasma/CoreLib/hsStream.cpp +++ b/Sources/Plasma/CoreLib/hsStream.cpp @@ -49,6 +49,7 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com # include #endif #include +#include #if HS_BUILD_FOR_UNIX #include @@ -1016,10 +1017,10 @@ bool hsBufferedStream::Close() if (fBufferReadIn + fReadDirect > 0) wasted -= int((float(fBufferReadOut+fReadDirect) / float(fBufferReadIn+fReadDirect)) * 100.f); - s.WriteFmt("{},{},{},{},{},{},{},{}\n", + s.WriteString(ST::format("{},{},{},{},{},{},{},{}\n", fFilename, fBufferHits, fBufferMisses, fBufferReadIn, fBufferReadOut, fReadDirect, wasted, - fCloseReason ? fCloseReason : "Unknown"); + fCloseReason ? fCloseReason : "Unknown")); s.Close(); #endif // LOG_BUFFERED diff --git a/Sources/Plasma/CoreLib/hsStream.h b/Sources/Plasma/CoreLib/hsStream.h index b0dd6ed700..3893c2a26c 100644 --- a/Sources/Plasma/CoreLib/hsStream.h +++ b/Sources/Plasma/CoreLib/hsStream.h @@ -44,7 +44,8 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #include "HeadSpin.h" #include "plFileSystem.h" -#include + +#include #include class hsStream { @@ -79,9 +80,6 @@ enum { uint32_t WriteString(const ST::string & string) { return Write((uint32_t)string.size(), string.c_str()); } - template - uint32_t WriteFmt(const char * fmt, _Args&&... args) { return WriteString(ST::format(fmt, std::forward<_Args>(args)...)); } - uint32_t WriteSafeString(const ST::string &string); uint32_t WriteSafeWString(const ST::string &string); ST::string ReadSafeString(); diff --git a/Sources/Plasma/FeatureLib/pfAnimation/plAnimDebugList.cpp b/Sources/Plasma/FeatureLib/pfAnimation/plAnimDebugList.cpp index 5f58c45ef2..2c8525899e 100644 --- a/Sources/Plasma/FeatureLib/pfAnimation/plAnimDebugList.cpp +++ b/Sources/Plasma/FeatureLib/pfAnimation/plAnimDebugList.cpp @@ -45,6 +45,8 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #include "hsResMgr.h" #include "hsTimer.h" +#include + #include "pnSceneObject/plSceneObject.h" #include "plAnimation/plAGAnim.h" diff --git a/Sources/Plasma/NucleusLib/pnFactory/plFactory.cpp b/Sources/Plasma/NucleusLib/pnFactory/plFactory.cpp index 3f9acb3136..c47b856326 100644 --- a/Sources/Plasma/NucleusLib/pnFactory/plFactory.cpp +++ b/Sources/Plasma/NucleusLib/pnFactory/plFactory.cpp @@ -50,6 +50,7 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com // For class names #include "plCreatableStrings.h" +#include static plFactory* theFactory = nullptr; diff --git a/Sources/Plasma/NucleusLib/pnNetCommon/plNetAddress.cpp b/Sources/Plasma/NucleusLib/pnNetCommon/plNetAddress.cpp index 25bed41130..f3151f2135 100644 --- a/Sources/Plasma/NucleusLib/pnNetCommon/plNetAddress.cpp +++ b/Sources/Plasma/NucleusLib/pnNetCommon/plNetAddress.cpp @@ -41,6 +41,7 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com *==LICENSE==*/ #include +#include #include "plNetAddress.h" #include "pnNetCommon.h" diff --git a/Sources/Plasma/PubUtilLib/plAgeDescription/plAgeDescription.cpp b/Sources/Plasma/PubUtilLib/plAgeDescription/plAgeDescription.cpp index bc1d701879..ab3f48bd4c 100644 --- a/Sources/Plasma/PubUtilLib/plAgeDescription/plAgeDescription.cpp +++ b/Sources/Plasma/PubUtilLib/plAgeDescription/plAgeDescription.cpp @@ -51,7 +51,7 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #include #include #include - +#include const uint32_t plAgePage::kInvalidSeqSuffix = (uint32_t)-1; diff --git a/Sources/Plasma/PubUtilLib/plAgeLoader/plResPatcher.cpp b/Sources/Plasma/PubUtilLib/plAgeLoader/plResPatcher.cpp index b1b0855d64..1b92e67cfe 100644 --- a/Sources/Plasma/PubUtilLib/plAgeLoader/plResPatcher.cpp +++ b/Sources/Plasma/PubUtilLib/plAgeLoader/plResPatcher.cpp @@ -44,6 +44,8 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #include "hsResMgr.h" #include "plgDispatch.h" +#include + #include "plAgeLoader.h" #include "plFile/plEncryptedStream.h" #include "plFile/plStreamSource.h" diff --git a/Sources/Plasma/PubUtilLib/plContainer/plConfigInfo.cpp b/Sources/Plasma/PubUtilLib/plContainer/plConfigInfo.cpp index 2826b5cc5e..cdba2c60da 100644 --- a/Sources/Plasma/PubUtilLib/plContainer/plConfigInfo.cpp +++ b/Sources/Plasma/PubUtilLib/plContainer/plConfigInfo.cpp @@ -45,6 +45,7 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #include #include #include +#include const ST::string& plConfigInfo::GlobalSection() { diff --git a/Sources/Plasma/PubUtilLib/plContainer/plConfigInfoLogging.cpp b/Sources/Plasma/PubUtilLib/plContainer/plConfigInfoLogging.cpp index 4666ced4aa..591d518f88 100644 --- a/Sources/Plasma/PubUtilLib/plContainer/plConfigInfoLogging.cpp +++ b/Sources/Plasma/PubUtilLib/plContainer/plConfigInfoLogging.cpp @@ -41,6 +41,8 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com *==LICENSE==*/ #include "plConfigInfo.h" +#include + plConfigInfoLogging::plConfigInfoLogging() { } diff --git a/Sources/Plasma/PubUtilLib/plStatGather/plProfileManagerFull.cpp b/Sources/Plasma/PubUtilLib/plStatGather/plProfileManagerFull.cpp index 310c13ed65..a86694f62b 100644 --- a/Sources/Plasma/PubUtilLib/plStatGather/plProfileManagerFull.cpp +++ b/Sources/Plasma/PubUtilLib/plStatGather/plProfileManagerFull.cpp @@ -45,6 +45,8 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #include "hsStream.h" +#include + #include "plPipeline/plDebugText.h" #include "plPipeline/plPlates.h" #include "plUnifiedTime/plUnifiedTime.h" diff --git a/Sources/Tools/MaxExport/plExportDlg.cpp b/Sources/Tools/MaxExport/plExportDlg.cpp index 2ac68eee04..36dc62390d 100644 --- a/Sources/Tools/MaxExport/plExportDlg.cpp +++ b/Sources/Tools/MaxExport/plExportDlg.cpp @@ -46,7 +46,7 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #include "MaxMain/MaxAPI.h" #include -#include +#include #include #include "plExportDlg.h" @@ -413,7 +413,7 @@ static bool AutoExportDir(const plFileName& inputDir, const plFileName& outputDi hsUNIXStream log; if (log.Open(outputLog, "ab")) { - log.WriteFmt("{}\r\n", iter->GetFileName()); + log.WriteString(ST::format("{}\r\n", iter->GetFileName())); log.Close(); } From 732437ccca86a03e9333af737ec2964da2aa01e6 Mon Sep 17 00:00:00 2001 From: dgelessus Date: Tue, 6 Jun 2023 01:15:30 +0200 Subject: [PATCH 09/13] Remove now unused hsAppender (hsMemory.h) --- Sources/Plasma/CoreLib/CMakeLists.txt | 2 - Sources/Plasma/CoreLib/hsMemory.cpp | 667 -------------------------- Sources/Plasma/CoreLib/hsMemory.h | 174 ------- 3 files changed, 843 deletions(-) delete mode 100644 Sources/Plasma/CoreLib/hsMemory.cpp delete mode 100644 Sources/Plasma/CoreLib/hsMemory.h diff --git a/Sources/Plasma/CoreLib/CMakeLists.txt b/Sources/Plasma/CoreLib/CMakeLists.txt index a3ce5ed3dc..6a45faf245 100644 --- a/Sources/Plasma/CoreLib/CMakeLists.txt +++ b/Sources/Plasma/CoreLib/CMakeLists.txt @@ -9,7 +9,6 @@ set(CoreLib_SOURCES hsGeometry3.cpp hsMatrix33.cpp hsMatrix44.cpp - hsMemory.cpp hsQuat.cpp hsRefCnt.cpp hsStream.cpp @@ -48,7 +47,6 @@ set(CoreLib_HEADERS hsLockGuard.h hsMatrix44.h hsMatrixMath.h - hsMemory.h hsPoint2.h hsPoolVector.h hsQuat.h diff --git a/Sources/Plasma/CoreLib/hsMemory.cpp b/Sources/Plasma/CoreLib/hsMemory.cpp deleted file mode 100644 index cd1e0dcac1..0000000000 --- a/Sources/Plasma/CoreLib/hsMemory.cpp +++ /dev/null @@ -1,667 +0,0 @@ -/*==LICENSE==* - -CyanWorlds.com Engine - MMOG client, server and tools -Copyright (C) 2011 Cyan Worlds, Inc. - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . - -Additional permissions under GNU GPL version 3 section 7 - -If you modify this Program, or any covered work, by linking or -combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK, -NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent -JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK -(or a modified version of those libraries), -containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA, -PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG -JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the -licensors of this Program grant you additional -permission to convey the resulting work. Corresponding Source for a -non-source form of such a combination shall include the source code for -the parts of OpenSSL and IJG JPEG Library used as well as that of the covered -work. - -You can contact Cyan Worlds, Inc. by email legal@cyan.com - or by snail mail at: - Cyan Worlds, Inc. - 14617 N Newport Hwy - Mead, WA 99021 - -*==LICENSE==*/ - -#include "hsMemory.h" -#include "hsExceptions.h" - -#include - -#define DO_MEMORY_REPORTS // dumps memory reports upon start up of engine - -struct hsAppenderHead { - struct hsAppenderHead* fNext; - struct hsAppenderHead* fPrev; - void* fFirst; - void* fStop; - void* fBottom; - - void* GetTop() const { return (char*)this + sizeof(*this); } - void* GetBottom() const { return fBottom; } - void* GetStop() const { return fStop; } - - void* GetFirst() const { return fFirst; } - void* GetLast(size_t elemSize) const { return (char*)fStop - elemSize; } - size_t GetSize() const { return (char*)fStop - (char*)fFirst; } - - bool CanPrepend() const { return fFirst != this->GetTop(); } - size_t PrependSize() const { return (char*)fFirst - (char*)this->GetTop(); } - bool CanAppend() const { return fStop != this->GetBottom(); } - size_t AppendSize() const { return (char*)this->GetBottom() - (char*)fStop; } - - void* Prepend(size_t elemSize) - { - hsAssert(this->CanPrepend(), "bad prepend"); - fFirst = (char*)fFirst - elemSize; - hsAssert((char*)fFirst >= (char*)this->GetTop(), "bad elemSize"); - return fFirst; - } - void* Append(size_t elemSize) - { - hsAssert(this->CanAppend(), "bad append"); - void* data = fStop; - fStop = (char*)fStop + elemSize; - hsAssert((char*)fStop <= (char*)fBottom, "bad elemSize"); - return data; - } - bool PopHead(size_t elemSize, void* data) - { - hsAssert(fFirst != fStop, "Empty"); - if (data) - memmove(data, fFirst, elemSize); - fFirst = (char*)fFirst + elemSize; - return fFirst == fStop; - } - bool PopTail(size_t elemSize, void* data) - { - hsAssert(fFirst != fStop, "Empty"); - fStop = (char*)fStop - elemSize; - if (data) - memmove(data, fStop, elemSize); - return fFirst == fStop; - } - - static hsAppenderHead* NewAppend(size_t elemSize, size_t elemCount, hsAppenderHead* prev) - { - size_t dataSize = elemSize * elemCount; - hsAppenderHead* head = (hsAppenderHead*)malloc(sizeof(hsAppenderHead) + dataSize); - - head->fNext = nullptr; - head->fPrev = prev; - head->fFirst = head->GetTop(); - head->fStop = head->fFirst; - head->fBottom = (char*)head->fFirst + dataSize; - return head; - } - static hsAppenderHead* NewPrepend(size_t elemSize, size_t elemCount, hsAppenderHead* next) - { - size_t dataSize = elemSize * elemCount; - hsAppenderHead* head = (hsAppenderHead*)malloc(sizeof(hsAppenderHead) + dataSize); - - head->fNext = next; - head->fPrev = nullptr; - head->fBottom = (char*)head->GetTop() + dataSize; - head->fFirst = head->fBottom; - head->fStop = head->fBottom; - return head; - } -}; - -//////////////////////////////////////////////////////////////////////////////////////// - -size_t hsAppender::CopyInto(void* data) const -{ - if (data) { - const hsAppenderHead* head = fFirstBlock; - hsDebugCode(size_t totalSize = 0;) - - while (head != nullptr) { - size_t size = head->GetSize(); - memmove(data, head->GetFirst(), size); - - data = (char*)data + size; - head = head->fNext; - hsDebugCode(totalSize += size;) - } - hsAssert(totalSize == fCount * fElemSize, "bad size"); - } - return fCount * fElemSize; -} - -void hsAppender::Reset() -{ - hsAppenderHead* head = fFirstBlock; - - while (head != nullptr) { - hsAppenderHead* next = head->fNext; - free(head); - head = next; - } - - fCount = 0; - fFirstBlock = nullptr; - fLastBlock = nullptr; -} - -void* hsAppender::PushHead() -{ - if (fFirstBlock == nullptr) { - fFirstBlock = hsAppenderHead::NewPrepend(fElemSize, fElemCount, nullptr); - fLastBlock = fFirstBlock; - } - else if (fFirstBlock->CanPrepend() == false) - fFirstBlock = hsAppenderHead::NewPrepend(fElemSize, fElemCount, fFirstBlock); - - fCount += 1; - return fFirstBlock->Prepend(fElemSize); -} - -void hsAppender::PushHead(const void* data) -{ - void* addr = this->PushHead(); - if (data) - memmove(addr, data, fElemSize); -} - -void* hsAppender::PeekHead() const -{ - if (fFirstBlock) - return (char*)fFirstBlock->fFirst; - else - return nullptr; -} - -bool hsAppender::PopHead(void* data) -{ - if (fCount == 0) - return false; - - fCount -= 1; - - if (fFirstBlock->PopHead(fElemSize, data)) { - hsAppenderHead* next = fFirstBlock->fNext; - if (next) - next->fPrev = nullptr; - free(fFirstBlock); - fFirstBlock = next; - if (next == nullptr) - fLastBlock = nullptr; - } - return true; -} - -size_t hsAppender::PopHead(size_t count, void* data) -{ - hsThrowIfBadParam(count >= 0); - - size_t sizeNeeded = count * fElemSize; - size_t origCount = fCount; - - while (fCount > 0) { - size_t size = fFirstBlock->GetSize(); - if (size > sizeNeeded) - size = sizeNeeded; - - if (fFirstBlock->PopHead(size, data)) { - hsAppenderHead* next = fFirstBlock->fNext; - if (next) - next->fPrev = nullptr; - free(fFirstBlock); - fFirstBlock = next; - if (next == nullptr) - fLastBlock = nullptr; - } - - if (data) - data = (void*)((char*)data + size); - sizeNeeded -= size; - fCount -= size / fElemSize; - hsAssert(hsSsize_t(fCount) >= 0, "bad fElemSize"); - } - return origCount - fCount; // return number of elements popped -} - -void* hsAppender::PushTail() -{ - if (fFirstBlock == nullptr) { - fFirstBlock = hsAppenderHead::NewAppend(fElemSize, fElemCount, nullptr); - fLastBlock = fFirstBlock; - } - else if (fLastBlock->CanAppend() == false) { - fLastBlock->fNext = hsAppenderHead::NewAppend(fElemSize, fElemCount, fLastBlock); - fLastBlock = fLastBlock->fNext; - } - - fCount += 1; - return fLastBlock->Append(fElemSize); -} - -void hsAppender::PushTail(const void* data) -{ - void* addr = this->PushTail(); - if (data) - memmove(addr, data, fElemSize); -} - -void hsAppender::PushTail(size_t count, const void* data) -{ - hsThrowIfBadParam(count < 0); - - size_t sizeNeeded = count * fElemSize; - - while (sizeNeeded > 0) { - if (fFirstBlock == nullptr) { - hsAssert(fCount == 0, "uninited count"); - fFirstBlock = hsAppenderHead::NewAppend(fElemSize, fElemCount, nullptr); - fLastBlock = fFirstBlock; - } - else if (!fLastBlock->CanAppend()) { - fLastBlock->fNext = hsAppenderHead::NewAppend(fElemSize, fElemCount, fLastBlock); - fLastBlock = fLastBlock->fNext; - } - - size_t size = fLastBlock->AppendSize(); - hsAssert(size > 0, "bad appendsize"); - if (size > sizeNeeded) - size = sizeNeeded; - void* dst = fLastBlock->Append(size); - - if (data) { - memmove(dst, data, size); - data = (char*)data + size; - } - sizeNeeded -= size; - } - fCount += count; -} - -void* hsAppender::PeekTail() const -{ - if (fLastBlock) - return (char*)fLastBlock->fStop - fElemSize; - else - return nullptr; -} - -bool hsAppender::PopTail(void* data) -{ - if (fCount == 0) - return false; - - fCount -= 1; - - if (fLastBlock->PopTail(fElemSize, data)) { - hsAppenderHead* prev = fLastBlock->fPrev; - if (prev) - prev->fNext = nullptr; - free(fLastBlock); - fLastBlock = prev; - if (prev == nullptr) - fFirstBlock = nullptr; - } - return true; -} - -////////////////////////////////////////////////////////////////////////// - -hsAppenderIterator::hsAppenderIterator(const hsAppender* list) - : fCurrItem() -{ - this->ResetToHead(list); -} - -void hsAppenderIterator::ResetToHead(const hsAppender* list) -{ - fAppender = list; - fCurrBlock = nullptr; - - if (fAppender) { - fCurrBlock = fAppender->fFirstBlock; - if (fCurrBlock) - fCurrItem = fCurrBlock->GetFirst(); - } -} - -void hsAppenderIterator::ResetToTail(const hsAppender* list) -{ - fAppender = list; - fCurrBlock = nullptr; - - if (fAppender) { - fCurrBlock = fAppender->fLastBlock; - if (fCurrBlock) - fCurrItem = fCurrBlock->GetLast(fAppender->fElemSize); - } -} - -void* hsAppenderIterator::Next() -{ - void* item = nullptr; - - if (fCurrBlock) { - item = fCurrItem; - fCurrItem = (char*)fCurrItem + fAppender->fElemSize; - if (fCurrItem == fCurrBlock->GetBottom()) { - fCurrBlock = fCurrBlock->fNext; - if (fCurrBlock) - fCurrItem = fCurrBlock->GetFirst(); - } - else if (fCurrItem == fCurrBlock->GetStop()) { - hsAssert(fCurrBlock->fNext == nullptr, "oops"); - fCurrBlock = nullptr; - } - } - return item; -} - -bool hsAppenderIterator::Next(void* data) -{ - void* addr = this->Next(); - if (addr) { - if (data) - memmove(data, addr, fAppender->fElemSize); - return true; - } - return false; -} - -size_t hsAppenderIterator::Next(size_t count, void* data) -{ - size_t origCount = count; - - while (count > 0 && this->Next(data)) { - if (data) - data = (void*)((char*)data + fAppender->fElemSize); - count -= 1; - } - return origCount - count; -} - -void* hsAppenderIterator::Prev() -{ - void* item = nullptr; - - if (fCurrBlock) { - item = fCurrItem; - fCurrItem = (char*)fCurrItem - fAppender->fElemSize; - if (item == fCurrBlock->GetTop()) { - fCurrBlock = fCurrBlock->fPrev; - if (fCurrBlock) - fCurrItem = fCurrBlock->GetLast(fAppender->fElemSize); - } - else if (item == fCurrBlock->GetFirst()) { - hsAssert(fCurrBlock->fPrev == nullptr, "oops"); - fCurrBlock = nullptr; - } - } - return item; -} - -bool hsAppenderIterator::Prev(void* data) -{ - void* addr = this->Prev(); - if (addr) { - if (data) - memmove(data, addr, fAppender->fElemSize); - return true; - } - return false; -} - -//------------------------------------------------------------- -// -// MEMORY USE REPORTING CODE -// -//------------------------------------------------------------- - -#if 1//!(defined(HS_DEBUGGING)&&(HS_BUILD_FOR_WIN32)&& defined(HS_FIND_MEM_LEAKS)) - // EMPTY STUB -void SortNDumpUnfreedMemory(const char *, bool ) // file name base, and FULL report indicator -{ -} - -#else - -typedef struct _CrtMemBlockHeader -{ -// Pointer to the block allocated just before this one: - struct _CrtMemBlockHeader *pBlockHeaderNext; -// Pointer to the block allocated just after this one: - struct _CrtMemBlockHeader *pBlockHeaderPrev; - char *szFileName; // File name - int nLine; // Line number - size_t nDataSize; // Size of user block - int nBlockUse; // Type of block - long lRequest; // Allocation number -// Buffer just before (lower than) the user's memory: - unsigned char gap[4]; -} _CrtMemBlockHeader; - -/* In an actual memory block in the debug heap, - * this structure is followed by: - * unsigned char data[nDataSize]; - * unsigned char anotherGap[4]; - */ - -// -// Dump formatted string to OutputDebugString -// -void __cdecl DebugMsg( LPSTR fmt, ... ) -{ - - char buff[256]; - wvsprintf(buff, fmt, (char *)(&fmt+1)); - hsStatusMessage(buff); - -} - -char *TrimFileName(char *name) // Trim file name of leading Directories -{ - int len = 0; - char *ptr; - if (!name) - return nullptr; - - len = strlen(name); - ptr = name + len; - for ( ptr--; ptr > name; ptr--) - { - if (*ptr == '\\') - { - ptr++; - break; - } - } - return ptr; -} - -// -// Loop thru all unfreed blocks in the heap and dump out detailed info -// - -struct looktbl { - char * fName; // Name of file -// long fAllocs; // Number of Alloc calls - long fBytes; // Total Bytes Alloc'd -}; -#define LTBLMAX 300 - -//--------------------------------------------------------------------------- -// This routine will report on the memory used in the engine. -// If argument full is true, it gives a full dump from the start of the program -// if !full, then each time the routine is called it remembers where it finishes off, then the next -// call with !full, it will (attempt) to report on the newest allocations, backward to the last checkpoint -//-------------------------------------------------------------------------- - -void SortNDumpUnfreedMemory(const char *nm, bool full) // file name base, and FULL report indicator -{ -#ifndef DO_MEMORY_REPORTS - if (!full) // full is launched by control M...partials are called each time the engine starts - return; -#endif - - char fname[512]; - snprintf(fname, std::size(fname), "%s_dmp.txt", nm); - char *errStr = ""; - - - _CrtMemState heap_state; -static uint32_t GrandTotal =0; -static _CrtMemBlockHeader *cmbh_last; // Remember this header for next incremental check DANGER this - // could break if this is freed...(gives bad report) - _CrtMemBlockHeader *cmbh_last_good; - - _CrtMemBlockHeader *cmbh; - // Get Current heap state - - _CrtMemCheckpoint(&heap_state); - - cmbh = heap_state.pBlockHeader; - - long totsize= 0; // Track Total Bytes - long normsize = 0; // Track total of NORMAL Blocks - - looktbl *ltb = new looktbl[LTBLMAX]; - long tblEnd=1; // first is "NULL"; - - memset((void *)ltb,0,sizeof(looktbl) * LTBLMAX); // clear table area - - char *ftrim; - - - ltb[0].fName = "NULL"; // Use first Table Pos for NULL - - long tblpos; - while (cmbh != nullptr) // Accumulate Stats to table - { - if (cmbh == cmbh_last && !full) // full indicates ignore last "checkpoint", stop at last checkpoint if !full - break; - cmbh_last_good = cmbh; - totsize += cmbh->nDataSize; - if (cmbh->nBlockUse == _NORMAL_BLOCK) - { - normsize += cmbh->nDataSize; - - if (cmbh->szFileName != nullptr) // Shorten to just the file name, looks better, and strcmps faster - { - ftrim = TrimFileName(cmbh->szFileName); - for (tblpos = 1; tblpos < tblEnd; tblpos++) // find the name in the table - { - if (!strcmp(ftrim,ltb[tblpos].fName)) - break; // found it - } - } - else - { - tblpos = 0; // Use "NULL", first pos of table - } - - if (tblpos == tblEnd) // Did not find it...add it - { - tblEnd++; - if (tblEnd >= LTBLMAX) - { DebugMsg("DumpUnfreedMemoryInfo: EXCEED MAX TABLE LENGTH\n"); - tblEnd--; - break; - } - ltb[tblpos].fName = ftrim; // Add name - } - // Add Stats -// ltb[tblpos].fAllocs++; - ltb[tblpos].fBytes += cmbh->nDataSize; - - - } - cmbh = cmbh->pBlockHeaderNext; - - } - // This Code relies on the _CrtMemBlockHeader *cmbh_last_good for the "last" checkpoint to still be around... - // If the following occurs, that chunk has been deleted. we could fix this by allocating our own - // chunk and keeping it (watch for mem leaks though) or figuring out an "approximat" re syncying routine - // that works before we run thru collecting data. PBG - - if (cmbh_last && !full && cmbh == nullptr) - { - //hsAssert(0,"Stats error: incremental mem check point has been deleted"); - errStr = "CHECK POINT ERROR, Results Inacurate"; - } - - if (normsize) // Don't write out about nothing - { - - CreateDirectory("Reports", nullptr); // stick em in a sub directory - char fnm[512]; - snprintf(fnm, std::size(fnm), "Reports\\%s", fname); - - FILE * DumpLogFile = fopen( fnm, "w" ); -// long allocs=0; - if (DumpLogFile != nullptr) - { - // Print Stats - fprintf(DumpLogFile, "Filename Total=%ld(k) %s\n",(normsize + 500)/1000,errStr); - for (int i = 0; i < tblEnd; i++) - { //fprintf(DumpLogFile,"%s\t%ld\n",ltb[i].fName, (ltb[i].fBytes+500)/1000);//,ltb[i].fAllocs); - fprintf(DumpLogFile,"%s ",ltb[i].fName); - int len = strlen(ltb[i].fName); - - for(int x=len; x < 25; x++) - fputc(' ',DumpLogFile); // make even columns - fprintf(DumpLogFile,"%5ld K\n",(uint32_t)( ltb[i].fBytes+500)/1000);//,ltb[i].fAllocs); - - //allocs += ltb[i].fAllocs; - } - - DebugMsg("MEMORY USE FILE DUMPED TO %s \n",fname); - DebugMsg("MEMORY Check: Total size %ld, Normal Size: %ld\n",totsize,normsize); - - fclose(DumpLogFile); - } - static int first=1; - if (!full) // if this is a partial mem dump, write to the ROOMS.txt file a summary - { - snprintf(fnm, std::size(fnm), "Reports\\%s", "ROOMS.txt"); - - if (first) - { DumpLogFile = fopen( fnm, "w" ); // first time clobber the old - if (DumpLogFile) - fprintf(DumpLogFile, "Filename Memory-Used(K) RunningTotal\n");// \tAllocation Calls \n" ); - first = 0; - } - else - DumpLogFile = fopen( fnm, "a+" ); - if( DumpLogFile) - { fprintf(DumpLogFile,"%s ",nm); - int len = strlen(nm); - GrandTotal += (uint32_t)(normsize+500)/1000; - - for(int x=len; x < 25; x++) - fputc(' ',DumpLogFile); // make even columns - fprintf(DumpLogFile,"%5ld K %5ld %s\n",(uint32_t)(normsize+500)/1000,GrandTotal,errStr);//, allocs); - fclose(DumpLogFile); - } - } - } - - - cmbh_last = heap_state.pBlockHeader; - delete ltb; -} -#endif diff --git a/Sources/Plasma/CoreLib/hsMemory.h b/Sources/Plasma/CoreLib/hsMemory.h deleted file mode 100644 index 80a4c1665c..0000000000 --- a/Sources/Plasma/CoreLib/hsMemory.h +++ /dev/null @@ -1,174 +0,0 @@ -/*==LICENSE==* - -CyanWorlds.com Engine - MMOG client, server and tools -Copyright (C) 2011 Cyan Worlds, Inc. - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . - -Additional permissions under GNU GPL version 3 section 7 - -If you modify this Program, or any covered work, by linking or -combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK, -NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent -JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK -(or a modified version of those libraries), -containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA, -PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG -JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the -licensors of this Program grant you additional -permission to convey the resulting work. Corresponding Source for a -non-source form of such a combination shall include the source code for -the parts of OpenSSL and IJG JPEG Library used as well as that of the covered -work. - -You can contact Cyan Worlds, Inc. by email legal@cyan.com - or by snail mail at: - Cyan Worlds, Inc. - 14617 N Newport Hwy - Mead, WA 99021 - -*==LICENSE==*/ -#ifndef hsMemoryDefined -#define hsMemoryDefined - -#include "HeadSpin.h" - -class hsAppender { - struct hsAppenderHead* fFirstBlock, *fLastBlock; - size_t fElemSize, fElemCount, fCount; - - friend class hsAppenderIterator; -public: - hsAppender(size_t elemSize, size_t elemCount = 16) - : fFirstBlock(), fLastBlock(), fElemSize(elemSize), fElemCount(elemCount), fCount() { } - ~hsAppender() { this->Reset(); }; - - size_t ElemSize() const { return fElemSize; } - size_t Count() const { return fCount; } - bool IsEmpty() const { return fCount == 0; } - void Reset(); - - size_t CopyInto(void* data = nullptr) const; // return size of data array in bytes - - void* PushHead(); - void PushHead(const void* data); - void* PushTail(); - void PushTail(const void* data); - void PushTail(size_t count, const void* data); // data[] = count * fElemSize - void* PeekHead() const; - void* PeekTail() const; - bool PopHead(void* data = nullptr); - size_t PopHead(size_t count, void* data = nullptr); // data[] = count * fElemSize - bool PopTail(void* data = nullptr); - - // Alternate interfaces - - void* Prepend() { return this->PushHead(); } - void* Append() { return this->PushTail(); } - - void* Push() { return this->PushHead(); } - void Push(const void* data) { this->PushHead(data); } - bool Pop(void* data = nullptr) { return this->PopHead(data); } - - void* Enqueue() { return this->PushTail(); }; - void Enqueue(const void* data) { this->PushTail(data); } - void Enqueue(size_t count, const void* data) { this->PushTail(count, data); } - bool Dequeue(void* data = nullptr) { return this->PopHead(data); } - size_t Dequeue(size_t count, void* data = nullptr) { return this->PopHead(count, data); } -}; - -class hsAppenderIterator { - const hsAppender* fAppender; - const struct hsAppenderHead* fCurrBlock; - void* fCurrItem; - -public: - hsAppenderIterator(const hsAppender* list = nullptr); - - void ResetToHead(const hsAppender* list = nullptr); - void ResetToTail(const hsAppender* list = nullptr); - void* Next(); - bool Next(void* data); - size_t Next(size_t count, void* data); - void* Prev(); - bool Prev(void* data); - - // Obsolete interface - void Reset(const hsAppender* list = nullptr) { this->ResetToHead(list); } -}; - -/////////////////////////////////////////////////////////////////////////////// - -template class hsTAppender : hsAppender { -public: - hsTAppender() : hsAppender(sizeof(T)) {} - hsTAppender(size_t minCount) : hsAppender(sizeof(T), minCount) {} - - hsAppender* GetAppender() { return this; } - const hsAppender* GetAppender() const { return this; } - - size_t Count() const { return hsAppender::Count(); } - bool IsEmpty() const { return hsAppender::IsEmpty(); } - void Reset() { hsAppender::Reset(); } - - size_t CopyInto(T copy[]) const { return hsAppender::CopyInto(copy); } - - T* PushHead() { return (T*)hsAppender::PushHead(); } - void PushHead(const T& item) { *this->PushHead() = item; } - T* PushTail() { return (T*)hsAppender::PushTail(); } - void PushTail(const T& item) { *this->PushTail() = item; }; - void PushTail(size_t count, const T item[]) { this->hsAppender::PushTail(count, item); }; - T* PeekHead() const { return (T*)hsAppender::PeekHead(); } - T* PeekTail() const { return (T*)hsAppender::PeekTail(); } - bool PopHead(T* item = nullptr) { return hsAppender::PopHead(item); } - size_t PopHead(size_t count, T item[] = nullptr) { return hsAppender::PopHead(count, item); } - bool PopTail(T* item = nullptr) { return hsAppender::PopTail(item); } - - // Alternate intefaces - - T* Prepend() { return this->PushHead(); } - T* Append() { return this->PushTail(); } - void PrependItem(const T& item) { this->PushHead(item); } - void AppendItem(const T& item) { this->PushTail(item); } - - T* Push() { return this->PushHead(); } - void Push(const T& item) { this->PushHead(item); } - bool Pop(T* item = nullptr) { return this->PopHead(item); } - - T* Enqueue() { return this->PushTail(); }; - void Enqueue(const T& item) { this->PushTail(item); } - void Enqueue(size_t count, const T item[]) { this->PushTail(count, item); } - bool Dequeue(T* item = nullptr) { return this->PopHead(item); } - size_t Dequeue(size_t count, T item[] = nullptr) { return this->PopHead(count, item); } -}; - -template class hsTAppenderIterator : hsAppenderIterator { -public: - hsTAppenderIterator() : hsAppenderIterator() {} - hsTAppenderIterator(const hsTAppender* list) : hsAppenderIterator(list->GetAppender()) {} - - void ResetToHead() { hsAppenderIterator::ResetToHead(nullptr); } - void ResetToHead(const hsTAppender* list) { hsAppenderIterator::ResetToHead(list->GetAppender()); } - void ResetToTail() { hsAppenderIterator::ResetToTail(nullptr); } - void ResetToTail(const hsTAppender* list) { hsAppenderIterator::ResetToTail(list->GetAppender()); } - T* Next() { return (T*)hsAppenderIterator::Next(); } - int Next(T* item) { return hsAppenderIterator::Next(item); } - T* Prev() { return (T*)hsAppenderIterator::Prev(); } - bool Prev(T* item) { return hsAppenderIterator::Prev(item); } - - // Obsolete interfaces - void Reset() { this->ResetToHead(); } - void Reset(const hsTAppender* list) { this->ResetToHead(list); } -}; -#endif From c09476a60815417f79f25a98f0ca02a5854d3249 Mon Sep 17 00:00:00 2001 From: dgelessus Date: Thu, 8 Jun 2023 18:04:38 +0200 Subject: [PATCH 10/13] Remove unused hsUNIXStream::fBuff --- Sources/Plasma/CoreLib/hsStream.cpp | 2 -- Sources/Plasma/CoreLib/hsStream.h | 3 +-- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/Sources/Plasma/CoreLib/hsStream.cpp b/Sources/Plasma/CoreLib/hsStream.cpp index dc23a381d8..717292b738 100644 --- a/Sources/Plasma/CoreLib/hsStream.cpp +++ b/Sources/Plasma/CoreLib/hsStream.cpp @@ -443,8 +443,6 @@ bool hsUNIXStream::Close() if (fRef) rtn = fclose(fRef); fRef = nullptr; - delete [] fBuff; - fBuff = nullptr; return !rtn; } diff --git a/Sources/Plasma/CoreLib/hsStream.h b/Sources/Plasma/CoreLib/hsStream.h index 3893c2a26c..137c306267 100644 --- a/Sources/Plasma/CoreLib/hsStream.h +++ b/Sources/Plasma/CoreLib/hsStream.h @@ -153,10 +153,9 @@ class hsStreamable { class hsUNIXStream: public hsStream { FILE* fRef; - char* fBuff; public: - hsUNIXStream(): fRef(), fBuff() { } + hsUNIXStream() : fRef() {} ~hsUNIXStream(); bool Open(const plFileName& name, const char* mode = "rb") override; bool Close() override; From cd89ad1b1ce44f8c2ea601e4e512058fb8931154 Mon Sep 17 00:00:00 2001 From: dgelessus Date: Sun, 23 Jul 2023 00:22:56 +0200 Subject: [PATCH 11/13] Add another missing include to plConfigInfo.h This wasn't noticeable on Windows for some reason. --- Sources/Plasma/PubUtilLib/plContainer/plConfigInfo.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Sources/Plasma/PubUtilLib/plContainer/plConfigInfo.h b/Sources/Plasma/PubUtilLib/plContainer/plConfigInfo.h index 6688a5f5e9..7f289dee07 100644 --- a/Sources/Plasma/PubUtilLib/plContainer/plConfigInfo.h +++ b/Sources/Plasma/PubUtilLib/plContainer/plConfigInfo.h @@ -43,11 +43,9 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #define plConfigInfo_h_inc #include "plKeysAndValues.h" -#include - -///////////////////////////////////////////////// -namespace ST { class string; } +#include +#include typedef std::vector plStringList; From 9056429ac103dddea7df241181c0366afb10e412 Mon Sep 17 00:00:00 2001 From: dgelessus Date: Sun, 23 Jul 2023 01:10:02 +0200 Subject: [PATCH 12/13] Replace hsRAMStream::Truncate call with Reset to match new behavior The previous implementation of hsRAMStream::Truncate incorrectly deleted the entire stream data, not just starting at the current seek position. --- Sources/Plasma/PubUtilLib/plNetCommon/plNetCommonHelpers.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Plasma/PubUtilLib/plNetCommon/plNetCommonHelpers.cpp b/Sources/Plasma/PubUtilLib/plNetCommon/plNetCommonHelpers.cpp index 18eee1a2e4..3ad3560935 100644 --- a/Sources/Plasma/PubUtilLib/plNetCommon/plNetCommonHelpers.cpp +++ b/Sources/Plasma/PubUtilLib/plNetCommon/plNetCommonHelpers.cpp @@ -275,7 +275,7 @@ void plCreatableListHelper::Write( hsStream* s, hsResMgr* mgr ) } } - ram.Truncate(); + ram.Reset(); ram.WriteByte(fFlags); ram.WriteLE32(bufSz); From 8b2f911d4cf4f8d8391759a6616d79c835dca428 Mon Sep 17 00:00:00 2001 From: dgelessus Date: Sat, 29 Jul 2023 18:44:59 +0200 Subject: [PATCH 13/13] Remove commented out debug message in hsRAMStream::Read Co-authored-by: Adam Johnson --- Sources/Plasma/CoreLib/hsStream.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/Sources/Plasma/CoreLib/hsStream.cpp b/Sources/Plasma/CoreLib/hsStream.cpp index 717292b738..19c26e6653 100644 --- a/Sources/Plasma/CoreLib/hsStream.cpp +++ b/Sources/Plasma/CoreLib/hsStream.cpp @@ -632,7 +632,6 @@ bool hsRAMStream::AtEnd() uint32_t hsRAMStream::Read(uint32_t byteCount, void * buffer) { if (fPosition + byteCount > fVector.size()) { -// hsStatusMessageF("Reading past end of hsRAMStream (read %u of %u requested bytes)", fVector.size() - fPosition, byteCount); byteCount = fVector.size() - fPosition; }