From 35420d681a86c9d02041d77e6915d71b825f7ca9 Mon Sep 17 00:00:00 2001 From: Kyle Sanderson Date: Tue, 19 Sep 2017 19:43:24 -0700 Subject: [PATCH 1/5] Migrate CDataPack from a Cassette Tape. Tested-By: Headline22. --- core/logic/CDataPack.cpp | 303 +++++++++++------------------------ core/logic/CDataPack.h | 40 +++-- core/logic/smn_datapacks.cpp | 63 +++++--- 3 files changed, 159 insertions(+), 247 deletions(-) diff --git a/core/logic/CDataPack.cpp b/core/logic/CDataPack.cpp index 4c8384100b..0ad5217891 100644 --- a/core/logic/CDataPack.cpp +++ b/core/logic/CDataPack.cpp @@ -33,25 +33,19 @@ #include #include "CDataPack.h" #include -#include - -using namespace ke; - -#define DATAPACK_INITIAL_SIZE 64 +#include CDataPack::CDataPack() { - m_pBase = (char *)malloc(DATAPACK_INITIAL_SIZE); - m_capacity = DATAPACK_INITIAL_SIZE; - Initialize(); + this->Initialize(); } CDataPack::~CDataPack() { - free(m_pBase); + this->Initialize(); } -static Vector> sDataPackCache; +static ke::Vector> sDataPackCache; IDataPack * CDataPack::New() { @@ -72,286 +66,171 @@ CDataPack::Free(IDataPack *pack) void CDataPack::Initialize() { - m_curptr = m_pBase; - m_size = 0; -} - -void CDataPack::CheckSize(size_t typesize) -{ - if (m_curptr - m_pBase + typesize <= m_capacity) + for (size_t iter = 0; iter < this->elements.length(); ++iter) { - return; + switch (this->elements[iter].type) + { + case CDataPackType::Raw: + { + delete static_cast(this->elements[iter].pData.vval); + this->elements.remove(iter--); + break; + } + + case CDataPackType::String: + { + delete static_cast(this->elements[iter].pData.vval); + this->elements.remove(iter--); + break; + } + } } - size_t pos = m_curptr - m_pBase; - do - { - m_capacity *= 2; - } while (pos + typesize > m_capacity); - - m_pBase = (char *)realloc(m_pBase, m_capacity); - m_curptr = m_pBase + pos; + this->elements.clear(); + this->position = 0; } void CDataPack::ResetSize() { - m_size = 0; + this->Initialize(); } size_t CDataPack::CreateMemory(size_t size, void **addr) { - CheckSize(sizeof(char) + sizeof(size_t) + size); - size_t pos = m_curptr - m_pBase; - - *(char *)m_curptr = Raw; - m_curptr += sizeof(char); - - *(size_t *)m_curptr = size; - m_curptr += sizeof(size_t); - - if (addr) - { - *addr = m_curptr; - } + InternalPack val; + val.type = CDataPackType::Raw; + val.pData.vval = new uint8_t[size + 1]; + this->elements.insert(this->position, val); - m_curptr += size; - m_size += sizeof(char) + sizeof(size_t) + size; - - return pos; + return this->position++; } void CDataPack::PackCell(cell_t cell) { - CheckSize(sizeof(char) + sizeof(size_t) + sizeof(cell_t)); - - *(char *)m_curptr = Cell; - m_curptr += sizeof(char); - - *(size_t *)m_curptr = sizeof(cell_t); - m_curptr += sizeof(size_t); - - *(cell_t *)m_curptr = cell; - m_curptr += sizeof(cell_t); - - m_size += sizeof(char) + sizeof(size_t) + sizeof(cell_t); + InternalPack val; + val.type = CDataPackType::Cell; + val.pData.cval = cell; + this->elements.insert(this->position++, val); } -void CDataPack::PackFloat(float val) +void CDataPack::PackFunction(cell_t function) { - CheckSize(sizeof(char) + sizeof(size_t) + sizeof(float)); - - *(char *)m_curptr = Float; - m_curptr += sizeof(char); - - *(size_t *)m_curptr = sizeof(float); - m_curptr += sizeof(size_t); - - *(float *)m_curptr = val; - m_curptr += sizeof(float); + InternalPack val; + val.type = CDataPackType::Function; + val.pData.cval = function; + this->elements.insert(this->position++, val); +} - m_size += sizeof(char) + sizeof(size_t) + sizeof(float); +void CDataPack::PackFloat(float floatval) +{ + InternalPack val; + val.type = CDataPackType::Float; + val.pData.fval = floatval; + this->elements.insert(this->position++, val); } void CDataPack::PackString(const char *string) { - size_t len = strlen(string); - size_t maxsize = sizeof(char) + sizeof(size_t) + len + 1; - CheckSize(maxsize); - - *(char *)m_curptr = String; - m_curptr += sizeof(char); - - // Pack the string length first for buffer overrun checking. - *(size_t *)m_curptr = len; - m_curptr += sizeof(size_t); - - // Now pack the string. - memcpy(m_curptr, string, len); - m_curptr[len] = '\0'; - m_curptr += len + 1; - - m_size += maxsize; + InternalPack val; + val.type = CDataPackType::String; + ke::AString *sval = new ke::AString(string); + val.pData.vval = static_cast(sval); + this->elements.insert(this->position++, val); } void CDataPack::Reset() const { - m_curptr = m_pBase; + this->position = 0; } size_t CDataPack::GetPosition() const { - return static_cast(m_curptr - m_pBase); + return this->position; } bool CDataPack::SetPosition(size_t pos) const { - if (pos > m_size) - { + if (pos > this->elements.length()) return false; - } - m_curptr = m_pBase + pos; + this->position = pos; return true; } cell_t CDataPack::ReadCell() const { - if (!IsReadable(sizeof(char) + sizeof(size_t) + sizeof(cell_t))) - { + if (!this->IsReadable()) return 0; - } - if (*reinterpret_cast(m_curptr) != Cell) - { - return 0; - } - m_curptr += sizeof(char); - if (*reinterpret_cast(m_curptr) != sizeof(cell_t)) - { + if (this->elements[this->position].type != CDataPackType::Cell) return 0; - } + + return this->elements[this->position++].pData.cval; +} - m_curptr += sizeof(size_t); +cell_t CDataPack::ReadFunction() const +{ + if (!this->IsReadable()) + return 0; - cell_t val = *reinterpret_cast(m_curptr); - m_curptr += sizeof(cell_t); - return val; + if (this->elements[this->position].type != CDataPackType::Function) + return 0; + + return this->elements[this->position++].pData.cval; } float CDataPack::ReadFloat() const { - if (!IsReadable(sizeof(char) + sizeof(size_t) + sizeof(float))) - { + if (!this->IsReadable()) return 0; - } - if (*reinterpret_cast(m_curptr) != Float) - { - return 0; - } - m_curptr += sizeof(char); - if (*reinterpret_cast(m_curptr) != sizeof(float)) - { + if (this->elements[this->position].type != CDataPackType::Float) return 0; - } - - m_curptr += sizeof(size_t); - - float val = *reinterpret_cast(m_curptr); - m_curptr += sizeof(float); - return val; + + return this->elements[this->position++].pData.fval; } bool CDataPack::IsReadable(size_t bytes) const { - return (bytes + (m_curptr - m_pBase) > m_size) ? false : true; + return (this->position < this->elements.length()); } const char *CDataPack::ReadString(size_t *len) const { - if (!IsReadable(sizeof(char) + sizeof(size_t))) - { - return NULL; - } - if (*reinterpret_cast(m_curptr) != String) - { - return NULL; - } - m_curptr += sizeof(char); - - size_t real_len = *(size_t *)m_curptr; + if (!this->IsReadable()) + return ""; - m_curptr += sizeof(size_t); - char *str = (char *)m_curptr; - - if ((strlen(str) != real_len) || !(IsReadable(real_len+1))) - { - return NULL; - } + if (this->elements[this->position].type != CDataPackType::String) + return ""; + const ke::AString &val = *static_cast(this->elements[this->position++].pData.vval); if (len) { - *len = real_len; + *len = val.length(); } - m_curptr += real_len + 1; - - return str; + return val.chars(); } void *CDataPack::GetMemory() const { - return m_curptr; + if (!this->IsReadable()) + return nullptr; + + if (this->elements[this->position].type != CDataPackType::Raw) + return nullptr; + + return this->elements[this->position].pData.vval; } void *CDataPack::ReadMemory(size_t *size) const { - if (!IsReadable(sizeof(size_t))) - { - return NULL; - } - if (*reinterpret_cast(m_curptr) != Raw) - { - return NULL; - } - m_curptr += sizeof(char); - - size_t bytecount = *(size_t *)m_curptr; - m_curptr += sizeof(size_t); - - if (!IsReadable(bytecount)) - { - return NULL; - } - - void *ptr = m_curptr; - + void *ptr = this->GetMemory(); + if (ptr != nullptr) + ++this->position; + if (size) - { - *size = bytecount; - } - - m_curptr += bytecount; + *size = (ptr != nullptr); /* Egor!!!! */ return ptr; } - -void CDataPack::PackFunction(cell_t function) -{ - CheckSize(sizeof(char) + sizeof(size_t) + sizeof(cell_t)); - - *(char *)m_curptr = Function; - m_curptr += sizeof(char); - - *(size_t *)m_curptr = sizeof(cell_t); - m_curptr += sizeof(size_t); - - *(cell_t *)m_curptr = function; - m_curptr += sizeof(cell_t); - - m_size += sizeof(char) + sizeof(size_t) + sizeof(cell_t); -} - -cell_t CDataPack::ReadFunction() const -{ - if (!IsReadable(sizeof(char) + sizeof(size_t) + sizeof(cell_t))) - { - return 0; - } - if (*reinterpret_cast(m_curptr) != Function) - { - return 0; - } - m_curptr += sizeof(char); - - if (*reinterpret_cast(m_curptr) != sizeof(cell_t)) - { - return 0; - } - - m_curptr += sizeof(size_t); - - cell_t val = *reinterpret_cast(m_curptr); - m_curptr += sizeof(cell_t); - return val; -} diff --git a/core/logic/CDataPack.h b/core/logic/CDataPack.h index 5acebd697f..5147c58884 100644 --- a/core/logic/CDataPack.h +++ b/core/logic/CDataPack.h @@ -33,9 +33,18 @@ #define _INCLUDE_SOURCEMOD_CDATAPACK_H_ #include +#include using namespace SourceMod; +enum CDataPackType { + Raw, + Cell, + Float, + String, + Function +}; + class CDataPack : public IDataPack { public: @@ -50,7 +59,7 @@ class CDataPack : public IDataPack bool SetPosition(size_t pos) const; cell_t ReadCell() const; float ReadFloat() const; - bool IsReadable(size_t bytes) const; + bool IsReadable(size_t bytes = 0) const; const char *ReadString(size_t *len) const; void *GetMemory() const; void *ReadMemory(size_t *size) const; @@ -64,22 +73,23 @@ class CDataPack : public IDataPack void PackFunction(cell_t function); public: void Initialize(); - inline size_t GetCapacity() const { return m_capacity; } + inline size_t GetCapacity() const { return this->elements.length(); }; + CDataPackType GetCurrentType(void) const { return this->elements[this->position].type; }; private: - void CheckSize(size_t sizetype); -private: - char *m_pBase; - mutable char *m_curptr; - size_t m_capacity; - size_t m_size; - enum DataPackType { - Raw, - Cell, - Float, - String, - Function - }; + typedef union { + cell_t cval; + float fval; + void *vval; + } InternalPackValue; + + typedef struct { + InternalPackValue pData; + CDataPackType type; + } InternalPack; + + ke::Vector elements; + mutable size_t position; }; #endif //_INCLUDE_SOURCEMOD_CDATAPACK_H_ diff --git a/core/logic/smn_datapacks.cpp b/core/logic/smn_datapacks.cpp index 1b588e12e0..bc18100520 100644 --- a/core/logic/smn_datapacks.cpp +++ b/core/logic/smn_datapacks.cpp @@ -61,7 +61,7 @@ class DataPackNatives : } void OnHandleDestroy(HandleType_t type, void *object) { - CDataPack::Free(reinterpret_cast(object)); + CDataPack::Free(reinterpret_cast(object)); } bool GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize) { @@ -73,7 +73,7 @@ class DataPackNatives : static cell_t smn_CreateDataPack(IPluginContext *pContext, const cell_t *params) { - IDataPack *pDataPack = CDataPack::New(); + CDataPack *pDataPack = static_cast(CDataPack::New()); if (!pDataPack) { @@ -88,7 +88,7 @@ static cell_t smn_WritePackCell(IPluginContext *pContext, const cell_t *params) Handle_t hndl = static_cast(params[1]); HandleError herr; HandleSecurity sec; - IDataPack *pDataPack; + CDataPack *pDataPack; sec.pOwner = pContext->GetIdentity(); sec.pIdentity = g_pCoreIdent; @@ -109,7 +109,7 @@ static cell_t smn_WritePackFloat(IPluginContext *pContext, const cell_t *params) Handle_t hndl = static_cast(params[1]); HandleError herr; HandleSecurity sec; - IDataPack *pDataPack; + CDataPack *pDataPack; sec.pOwner = pContext->GetIdentity(); sec.pIdentity = g_pCoreIdent; @@ -130,7 +130,7 @@ static cell_t smn_WritePackString(IPluginContext *pContext, const cell_t *params Handle_t hndl = static_cast(params[1]); HandleError herr; HandleSecurity sec; - IDataPack *pDataPack; + CDataPack *pDataPack; sec.pOwner = pContext->GetIdentity(); sec.pIdentity = g_pCoreIdent; @@ -153,7 +153,7 @@ static cell_t smn_WritePackFunction(IPluginContext *pContext, const cell_t *para Handle_t hndl = static_cast(params[1]); HandleError herr; HandleSecurity sec; - IDataPack *pDataPack; + CDataPack *pDataPack; sec.pOwner = pContext->GetIdentity(); sec.pIdentity = g_pCoreIdent; @@ -174,7 +174,7 @@ static cell_t smn_ReadPackCell(IPluginContext *pContext, const cell_t *params) Handle_t hndl = static_cast(params[1]); HandleError herr; HandleSecurity sec; - IDataPack *pDataPack; + CDataPack *pDataPack; sec.pOwner = pContext->GetIdentity(); sec.pIdentity = g_pCoreIdent; @@ -185,11 +185,16 @@ static cell_t smn_ReadPackCell(IPluginContext *pContext, const cell_t *params) return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr); } - if (!pDataPack->IsReadable(sizeof(char) + sizeof(size_t) + sizeof(cell_t))) + if (!pDataPack->IsReadable()) { return pContext->ThrowNativeError("DataPack operation is out of bounds."); } + if (pDataPack->GetCurrentType() != CDataPackType::Cell) + { + return pContext->ThrowNativeError("Invalid Datapack Type (got %d / expected %d).", pDataPack->GetCurrentType(), CDataPackType::Cell); + } + return pDataPack->ReadCell(); } @@ -198,7 +203,7 @@ static cell_t smn_ReadPackFloat(IPluginContext *pContext, const cell_t *params) Handle_t hndl = static_cast(params[1]); HandleError herr; HandleSecurity sec; - IDataPack *pDataPack; + CDataPack *pDataPack; sec.pOwner = pContext->GetIdentity(); sec.pIdentity = g_pCoreIdent; @@ -214,6 +219,11 @@ static cell_t smn_ReadPackFloat(IPluginContext *pContext, const cell_t *params) return pContext->ThrowNativeError("DataPack operation is out of bounds."); } + if (pDataPack->GetCurrentType() != CDataPackType::Float) + { + return pContext->ThrowNativeError("Invalid Datapack Type (got %d / expected %d).", pDataPack->GetCurrentType(), CDataPackType::Float); + } + return sp_ftoc(pDataPack->ReadFloat()); } @@ -222,7 +232,7 @@ static cell_t smn_ReadPackString(IPluginContext *pContext, const cell_t *params) Handle_t hndl = static_cast(params[1]); HandleError herr; HandleSecurity sec; - IDataPack *pDataPack; + CDataPack *pDataPack; sec.pOwner = pContext->GetIdentity(); sec.pIdentity = g_pCoreIdent; @@ -233,12 +243,17 @@ static cell_t smn_ReadPackString(IPluginContext *pContext, const cell_t *params) return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr); } - const char *str; - if (!(str=pDataPack->ReadString(NULL))) + if (!pDataPack->IsReadable()) { return pContext->ThrowNativeError("DataPack operation is out of bounds."); } + if (pDataPack->GetCurrentType() != CDataPackType::String) + { + return pContext->ThrowNativeError("Invalid Datapack Type (got %d / expected %d).", pDataPack->GetCurrentType(), CDataPackType::String); + } + + const char *str = pDataPack->ReadString(NULL); pContext->StringToLocal(params[2], params[3], str); return 1; @@ -249,7 +264,7 @@ static cell_t smn_ReadPackFunction(IPluginContext *pContext, const cell_t *param Handle_t hndl = static_cast(params[1]); HandleError herr; HandleSecurity sec; - IDataPack *pDataPack; + CDataPack *pDataPack; sec.pOwner = pContext->GetIdentity(); sec.pIdentity = g_pCoreIdent; @@ -260,11 +275,16 @@ static cell_t smn_ReadPackFunction(IPluginContext *pContext, const cell_t *param return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr); } - if (!pDataPack->IsReadable(sizeof(char) + sizeof(size_t) + sizeof(cell_t))) + if (!pDataPack->IsReadable()) { return pContext->ThrowNativeError("DataPack operation is out of bounds."); } + if (pDataPack->GetCurrentType() != CDataPackType::Function) + { + return pContext->ThrowNativeError("Invalid Datapack Type (got %d / expected %d).", pDataPack->GetCurrentType(), CDataPackType::Function); + } + return pDataPack->ReadFunction(); } @@ -273,7 +293,7 @@ static cell_t smn_ResetPack(IPluginContext *pContext, const cell_t *params) Handle_t hndl = static_cast(params[1]); HandleError herr; HandleSecurity sec; - IDataPack *pDataPack; + CDataPack *pDataPack; sec.pOwner = pContext->GetIdentity(); sec.pIdentity = g_pCoreIdent; @@ -284,11 +304,14 @@ static cell_t smn_ResetPack(IPluginContext *pContext, const cell_t *params) return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr); } - pDataPack->Reset(); if (params[2]) { pDataPack->ResetSize(); } + else + { + pDataPack->Reset(); + } return 1; } @@ -298,7 +321,7 @@ static cell_t smn_GetPackPosition(IPluginContext *pContext, const cell_t *params Handle_t hndl = static_cast(params[1]); HandleError herr; HandleSecurity sec; - IDataPack *pDataPack; + CDataPack *pDataPack; sec.pOwner = pContext->GetIdentity(); sec.pIdentity = g_pCoreIdent; @@ -317,7 +340,7 @@ static cell_t smn_SetPackPosition(IPluginContext *pContext, const cell_t *params Handle_t hndl = static_cast(params[1]); HandleError herr; HandleSecurity sec; - IDataPack *pDataPack; + CDataPack *pDataPack; sec.pOwner = pContext->GetIdentity(); sec.pIdentity = g_pCoreIdent; @@ -330,7 +353,7 @@ static cell_t smn_SetPackPosition(IPluginContext *pContext, const cell_t *params if (!pDataPack->SetPosition(params[2])) { - return pContext->ThrowNativeError("Invalid DataPack position, %d is out of bounds", params[2]); + return pContext->ThrowNativeError("Invalid DataPack position, %d is out of bounds (%d)", params[2], pDataPack->GetCapacity()); } return 1; @@ -341,7 +364,7 @@ static cell_t smn_IsPackReadable(IPluginContext *pContext, const cell_t *params) Handle_t hndl = static_cast(params[1]); HandleError herr; HandleSecurity sec; - IDataPack *pDataPack; + CDataPack *pDataPack; sec.pOwner = pContext->GetIdentity(); sec.pIdentity = g_pCoreIdent; From 5524ed8ee9ada59780b1df09d8c860d558c74a4e Mon Sep 17 00:00:00 2001 From: Kyle Sanderson Date: Tue, 19 Sep 2017 19:46:06 -0700 Subject: [PATCH 2/5] Remove last IsReadable param pass. --- core/logic/smn_datapacks.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/logic/smn_datapacks.cpp b/core/logic/smn_datapacks.cpp index bc18100520..56e500a02c 100644 --- a/core/logic/smn_datapacks.cpp +++ b/core/logic/smn_datapacks.cpp @@ -214,7 +214,7 @@ static cell_t smn_ReadPackFloat(IPluginContext *pContext, const cell_t *params) return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr); } - if (!pDataPack->IsReadable(sizeof(char) + sizeof(size_t) + sizeof(float))) + if (!pDataPack->IsReadable()) { return pContext->ThrowNativeError("DataPack operation is out of bounds."); } From 5dbabb319e030b48919d02c19a1c3eb55479f5fb Mon Sep 17 00:00:00 2001 From: Kyle Sanderson Date: Tue, 19 Sep 2017 19:54:09 -0700 Subject: [PATCH 3/5] populate len still if CDataPack::ReadString is unreadable or the wrong type. --- core/logic/CDataPack.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/core/logic/CDataPack.cpp b/core/logic/CDataPack.cpp index 0ad5217891..47c95b0570 100644 --- a/core/logic/CDataPack.cpp +++ b/core/logic/CDataPack.cpp @@ -198,10 +198,20 @@ bool CDataPack::IsReadable(size_t bytes) const const char *CDataPack::ReadString(size_t *len) const { if (!this->IsReadable()) + { + if (len) + *len = 0; + return ""; + } if (this->elements[this->position].type != CDataPackType::String) + { + if (len) + *len = 0; + return ""; + } const ke::AString &val = *static_cast(this->elements[this->position++].pData.vval); if (len) From 74cf0432c1ef42aab7833ad59db7d15aada78e15 Mon Sep 17 00:00:00 2001 From: Kyle Sanderson Date: Tue, 3 Oct 2017 12:22:12 -0700 Subject: [PATCH 4/5] Fyren Fixes(TM)(R)(C). --- core/logic/CDataPack.cpp | 100 ++++++++++++++--------------------- core/logic/CDataPack.h | 4 +- core/logic/smn_datapacks.cpp | 42 +++++++-------- 3 files changed, 63 insertions(+), 83 deletions(-) diff --git a/core/logic/CDataPack.cpp b/core/logic/CDataPack.cpp index 47c95b0570..cad45ed1b8 100644 --- a/core/logic/CDataPack.cpp +++ b/core/logic/CDataPack.cpp @@ -33,16 +33,15 @@ #include #include "CDataPack.h" #include -#include CDataPack::CDataPack() { - this->Initialize(); + Initialize(); } CDataPack::~CDataPack() { - this->Initialize(); + Initialize(); } static ke::Vector> sDataPackCache; @@ -66,43 +65,44 @@ CDataPack::Free(IDataPack *pack) void CDataPack::Initialize() { - for (size_t iter = 0; iter < this->elements.length(); ++iter) + for (size_t index = 0; index < elements.length(); ++index) { - switch (this->elements[iter].type) + switch (elements[index].type) { case CDataPackType::Raw: { - delete static_cast(this->elements[iter].pData.vval); - this->elements.remove(iter--); + delete elements[index].pData.vval; + elements.remove(index--); break; } case CDataPackType::String: { - delete static_cast(this->elements[iter].pData.vval); - this->elements.remove(iter--); + delete elements[index].pData.sval; + elements.remove(index--); break; } } } - this->elements.clear(); - this->position = 0; + elements.clear(); + position = 0; } void CDataPack::ResetSize() { - this->Initialize(); + Initialize(); } size_t CDataPack::CreateMemory(size_t size, void **addr) { InternalPack val; val.type = CDataPackType::Raw; - val.pData.vval = new uint8_t[size + 1]; - this->elements.insert(this->position, val); + val.pData.vval = new uint8_t[size + sizeof(size)]; + reinterpret_cast(val.pData.vval)[0] = size; + elements.insert(position, val); - return this->position++; + return position++; } void CDataPack::PackCell(cell_t cell) @@ -110,7 +110,7 @@ void CDataPack::PackCell(cell_t cell) InternalPack val; val.type = CDataPackType::Cell; val.pData.cval = cell; - this->elements.insert(this->position++, val); + elements.insert(position++, val); } void CDataPack::PackFunction(cell_t function) @@ -118,7 +118,7 @@ void CDataPack::PackFunction(cell_t function) InternalPack val; val.type = CDataPackType::Function; val.pData.cval = function; - this->elements.insert(this->position++, val); + elements.insert(position++, val); } void CDataPack::PackFloat(float floatval) @@ -126,7 +126,7 @@ void CDataPack::PackFloat(float floatval) InternalPack val; val.type = CDataPackType::Float; val.pData.fval = floatval; - this->elements.insert(this->position++, val); + elements.insert(position++, val); } void CDataPack::PackString(const char *string) @@ -134,113 +134,91 @@ void CDataPack::PackString(const char *string) InternalPack val; val.type = CDataPackType::String; ke::AString *sval = new ke::AString(string); - val.pData.vval = static_cast(sval); - this->elements.insert(this->position++, val); + val.pData.sval = sval; + elements.insert(position++, val); } void CDataPack::Reset() const { - this->position = 0; + position = 0; } size_t CDataPack::GetPosition() const { - return this->position; + return position; } bool CDataPack::SetPosition(size_t pos) const { - if (pos > this->elements.length()) + if (pos > elements.length()) return false; - this->position = pos; + position = pos; return true; } cell_t CDataPack::ReadCell() const { - if (!this->IsReadable()) - return 0; - - if (this->elements[this->position].type != CDataPackType::Cell) + if (!IsReadable() || elements[position].type != CDataPackType::Cell) return 0; - return this->elements[this->position++].pData.cval; + return elements[position++].pData.cval; } cell_t CDataPack::ReadFunction() const { - if (!this->IsReadable()) - return 0; - - if (this->elements[this->position].type != CDataPackType::Function) + if (!IsReadable() || elements[position].type != CDataPackType::Function) return 0; - return this->elements[this->position++].pData.cval; + return elements[position++].pData.cval; } float CDataPack::ReadFloat() const { - if (!this->IsReadable()) - return 0; - - if (this->elements[this->position].type != CDataPackType::Float) + if (!IsReadable() || elements[position].type != CDataPackType::Float) return 0; - return this->elements[this->position++].pData.fval; + return elements[position++].pData.fval; } bool CDataPack::IsReadable(size_t bytes) const { - return (this->position < this->elements.length()); + return (position < elements.length()); } const char *CDataPack::ReadString(size_t *len) const { - if (!this->IsReadable()) + if (!IsReadable() || elements[position].type != CDataPackType::String) { if (len) *len = 0; - return ""; - } - - if (this->elements[this->position].type != CDataPackType::String) - { - if (len) - *len = 0; - - return ""; + return nullptr; } - const ke::AString &val = *static_cast(this->elements[this->position++].pData.vval); + const ke::AString &val = *elements[position++].pData.sval; if (len) - { *len = val.length(); - } return val.chars(); } void *CDataPack::GetMemory() const { - if (!this->IsReadable()) - return nullptr; - - if (this->elements[this->position].type != CDataPackType::Raw) + if (!IsReadable() || elements[position].type != CDataPackType::Raw) return nullptr; - return this->elements[this->position].pData.vval; + return &(reinterpret_cast(elements[position].pData.vval)[1]); } void *CDataPack::ReadMemory(size_t *size) const { - void *ptr = this->GetMemory(); + void *ptr = GetMemory(); if (ptr != nullptr) - ++this->position; + ++position; if (size) - *size = (ptr != nullptr); /* Egor!!!! */ + *size = (reinterpret_cast(ptr)[-1]); /* Egor!!!! */ return ptr; } diff --git a/core/logic/CDataPack.h b/core/logic/CDataPack.h index 5147c58884..161f961165 100644 --- a/core/logic/CDataPack.h +++ b/core/logic/CDataPack.h @@ -34,6 +34,7 @@ #include #include +#include using namespace SourceMod; @@ -80,7 +81,8 @@ class CDataPack : public IDataPack typedef union { cell_t cval; float fval; - void *vval; + uint8_t *vval; + ke::AString *sval; } InternalPackValue; typedef struct { diff --git a/core/logic/smn_datapacks.cpp b/core/logic/smn_datapacks.cpp index 56e500a02c..2a2a12a1e0 100644 --- a/core/logic/smn_datapacks.cpp +++ b/core/logic/smn_datapacks.cpp @@ -96,7 +96,7 @@ static cell_t smn_WritePackCell(IPluginContext *pContext, const cell_t *params) if ((herr=handlesys->ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack)) != HandleError_None) { - return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr); + return pContext->ThrowNativeError("Invalid data pack handle %x (error %d).", hndl, herr); } pDataPack->PackCell(params[2]); @@ -117,7 +117,7 @@ static cell_t smn_WritePackFloat(IPluginContext *pContext, const cell_t *params) if ((herr=handlesys->ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack)) != HandleError_None) { - return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr); + return pContext->ThrowNativeError("Invalid data pack handle %x (error %d).", hndl, herr); } pDataPack->PackFloat(sp_ctof(params[2])); @@ -138,7 +138,7 @@ static cell_t smn_WritePackString(IPluginContext *pContext, const cell_t *params if ((herr=handlesys->ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack)) != HandleError_None) { - return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr); + return pContext->ThrowNativeError("Invalid data pack handle %x (error %d).", hndl, herr); } char *str; @@ -161,7 +161,7 @@ static cell_t smn_WritePackFunction(IPluginContext *pContext, const cell_t *para if ((herr = handlesys->ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack)) != HandleError_None) { - return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr); + return pContext->ThrowNativeError("Invalid data pack handle %x (error %d).", hndl, herr); } pDataPack->PackFunction(params[2]); @@ -182,17 +182,17 @@ static cell_t smn_ReadPackCell(IPluginContext *pContext, const cell_t *params) if ((herr=handlesys->ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack)) != HandleError_None) { - return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr); + return pContext->ThrowNativeError("Invalid data pack handle %x (error %d).", hndl, herr); } if (!pDataPack->IsReadable()) { - return pContext->ThrowNativeError("DataPack operation is out of bounds."); + return pContext->ThrowNativeError("Data pack operation is out of bounds."); } if (pDataPack->GetCurrentType() != CDataPackType::Cell) { - return pContext->ThrowNativeError("Invalid Datapack Type (got %d / expected %d).", pDataPack->GetCurrentType(), CDataPackType::Cell); + return pContext->ThrowNativeError("Invalid data pack type (got %d / expected %d).", pDataPack->GetCurrentType(), CDataPackType::Cell); } return pDataPack->ReadCell(); @@ -211,17 +211,17 @@ static cell_t smn_ReadPackFloat(IPluginContext *pContext, const cell_t *params) if ((herr=handlesys->ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack)) != HandleError_None) { - return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr); + return pContext->ThrowNativeError("Invalid data pack handle %x (error %d).", hndl, herr); } if (!pDataPack->IsReadable()) { - return pContext->ThrowNativeError("DataPack operation is out of bounds."); + return pContext->ThrowNativeError("Data pack operation is out of bounds."); } if (pDataPack->GetCurrentType() != CDataPackType::Float) { - return pContext->ThrowNativeError("Invalid Datapack Type (got %d / expected %d).", pDataPack->GetCurrentType(), CDataPackType::Float); + return pContext->ThrowNativeError("Invalid data pack type (got %d / expected %d).", pDataPack->GetCurrentType(), CDataPackType::Float); } return sp_ftoc(pDataPack->ReadFloat()); @@ -240,17 +240,17 @@ static cell_t smn_ReadPackString(IPluginContext *pContext, const cell_t *params) if ((herr=handlesys->ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack)) != HandleError_None) { - return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr); + return pContext->ThrowNativeError("Invalid data pack handle %x (error %d).", hndl, herr); } if (!pDataPack->IsReadable()) { - return pContext->ThrowNativeError("DataPack operation is out of bounds."); + return pContext->ThrowNativeError("Data pack operation is out of bounds."); } if (pDataPack->GetCurrentType() != CDataPackType::String) { - return pContext->ThrowNativeError("Invalid Datapack Type (got %d / expected %d).", pDataPack->GetCurrentType(), CDataPackType::String); + return pContext->ThrowNativeError("Invalid data pack type (got %d / expected %d).", pDataPack->GetCurrentType(), CDataPackType::String); } const char *str = pDataPack->ReadString(NULL); @@ -272,17 +272,17 @@ static cell_t smn_ReadPackFunction(IPluginContext *pContext, const cell_t *param if ((herr = handlesys->ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack)) != HandleError_None) { - return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr); + return pContext->ThrowNativeError("Invalid data pack handle %x (error %d).", hndl, herr); } if (!pDataPack->IsReadable()) { - return pContext->ThrowNativeError("DataPack operation is out of bounds."); + return pContext->ThrowNativeError("Data pack operation is out of bounds."); } if (pDataPack->GetCurrentType() != CDataPackType::Function) { - return pContext->ThrowNativeError("Invalid Datapack Type (got %d / expected %d).", pDataPack->GetCurrentType(), CDataPackType::Function); + return pContext->ThrowNativeError("Invalid data pack type (got %d / expected %d).", pDataPack->GetCurrentType(), CDataPackType::Function); } return pDataPack->ReadFunction(); @@ -301,7 +301,7 @@ static cell_t smn_ResetPack(IPluginContext *pContext, const cell_t *params) if ((herr=handlesys->ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack)) != HandleError_None) { - return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr); + return pContext->ThrowNativeError("Invalid data pack handle %x (error %d).", hndl, herr); } if (params[2]) @@ -329,7 +329,7 @@ static cell_t smn_GetPackPosition(IPluginContext *pContext, const cell_t *params if ((herr=handlesys->ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack)) != HandleError_None) { - return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr); + return pContext->ThrowNativeError("Invalid data pack handle %x (error %d).", hndl, herr); } return static_cast(pDataPack->GetPosition()); @@ -348,12 +348,12 @@ static cell_t smn_SetPackPosition(IPluginContext *pContext, const cell_t *params if ((herr=handlesys->ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack)) != HandleError_None) { - return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr); + return pContext->ThrowNativeError("Invalid data pack handle %x (error %d).", hndl, herr); } if (!pDataPack->SetPosition(params[2])) { - return pContext->ThrowNativeError("Invalid DataPack position, %d is out of bounds (%d)", params[2], pDataPack->GetCapacity()); + return pContext->ThrowNativeError("Invalid data pack position, %d is out of bounds (%d)", params[2], pDataPack->GetCapacity()); } return 1; @@ -372,7 +372,7 @@ static cell_t smn_IsPackReadable(IPluginContext *pContext, const cell_t *params) if ((herr=handlesys->ReadHandle(hndl, g_DataPackType, &sec, (void **)&pDataPack)) != HandleError_None) { - return pContext->ThrowNativeError("Invalid data pack handle %x (error %d)", hndl, herr); + return pContext->ThrowNativeError("Invalid data pack handle %x (error %d).", hndl, herr); } return pDataPack->IsReadable(params[2]) ? 1 : 0; From 6a16ddc76c43a28ee5319ccfd1e0cc7252ad8d1d Mon Sep 17 00:00:00 2001 From: Kyle Sanderson Date: Tue, 3 Oct 2017 15:29:49 -0700 Subject: [PATCH 5/5] Deprecate IDataPack. --- bridge/include/BridgeAPI.h | 2 +- core/logic/CDataPack.cpp | 21 +++++++++++---------- core/logic/CDataPack.h | 2 +- core/sourcemod.cpp | 2 +- public/IDataPack.h | 1 + 5 files changed, 15 insertions(+), 13 deletions(-) diff --git a/bridge/include/BridgeAPI.h b/bridge/include/BridgeAPI.h index d9e1cdaf70..91c8d74f01 100644 --- a/bridge/include/BridgeAPI.h +++ b/bridge/include/BridgeAPI.h @@ -35,7 +35,7 @@ namespace SourceMod { // Add 1 to the RHS of this expression to bump the intercom file // This is to prevent mismatching core/logic binaries -static const uint32_t SM_LOGIC_MAGIC = 0x0F47C0DE - 55; +static const uint32_t SM_LOGIC_MAGIC = 0x0F47C0DE - 56; } // namespace SourceMod diff --git a/core/logic/CDataPack.cpp b/core/logic/CDataPack.cpp index cad45ed1b8..7336e61ee2 100644 --- a/core/logic/CDataPack.cpp +++ b/core/logic/CDataPack.cpp @@ -46,7 +46,7 @@ CDataPack::~CDataPack() static ke::Vector> sDataPackCache; -IDataPack * CDataPack::New() +IDataPack *CDataPack::New() { if (sDataPackCache.empty()) return new CDataPack(); @@ -205,20 +205,21 @@ const char *CDataPack::ReadString(size_t *len) const void *CDataPack::GetMemory() const { - if (!IsReadable() || elements[position].type != CDataPackType::Raw) - return nullptr; - - return &(reinterpret_cast(elements[position].pData.vval)[1]); + return nullptr; } void *CDataPack::ReadMemory(size_t *size) const { - void *ptr = GetMemory(); - if (ptr != nullptr) - ++position; - + void *ptr = nullptr; + if (!IsReadable() || elements[position].type != CDataPackType::Raw) + return ptr; + + size_t *val = reinterpret_cast(elements[position].pData.vval); + ptr = &(val[1]); + ++position; + if (size) - *size = (reinterpret_cast(ptr)[-1]); /* Egor!!!! */ + *size = val[0]; /* Egor!!!! */ return ptr; } diff --git a/core/logic/CDataPack.h b/core/logic/CDataPack.h index 161f961165..8399908096 100644 --- a/core/logic/CDataPack.h +++ b/core/logic/CDataPack.h @@ -75,7 +75,7 @@ class CDataPack : public IDataPack public: void Initialize(); inline size_t GetCapacity() const { return this->elements.length(); }; - CDataPackType GetCurrentType(void) const { return this->elements[this->position].type; }; + inline CDataPackType GetCurrentType(void) const { return this->elements[this->position].type; }; private: typedef union { diff --git a/core/sourcemod.cpp b/core/sourcemod.cpp index 5522a7ecda..23c01daa9c 100644 --- a/core/sourcemod.cpp +++ b/core/sourcemod.cpp @@ -619,7 +619,7 @@ unsigned int SourceModBase::GetGlobalTarget() const IDataPack *SourceModBase::CreateDataPack() { - return logicore.CreateDataPack(); + return nullptr; } void SourceModBase::FreeDataPack(IDataPack *pack) diff --git a/public/IDataPack.h b/public/IDataPack.h index e069bff0dd..623e4e51e3 100644 --- a/public/IDataPack.h +++ b/public/IDataPack.h @@ -38,6 +38,7 @@ * @file IDataPack.h * @brief Contains functions for packing data abstractly to/from plugins. The wrappers * for creating these are contained in ISourceMod.h + * @deprecated Deprecated. Does nothing. */ namespace SourceMod