Migrate CDataPack from a Compact Cassette tape. (#688)
* Migrate CDataPack from a Cassette Tape. Tested-By: Headline22. * Remove last IsReadable param pass. * populate len still if CDataPack::ReadString is unreadable or the wrong type. * Fyren Fixes(TM)(R)(C). * Deprecate IDataPack.
This commit is contained in:
parent
29b1926432
commit
5f5a6b3a16
@ -33,27 +33,20 @@
|
||||
#include <string.h>
|
||||
#include "CDataPack.h"
|
||||
#include <amtl/am-autoptr.h>
|
||||
#include <amtl/am-vector.h>
|
||||
|
||||
using namespace ke;
|
||||
|
||||
#define DATAPACK_INITIAL_SIZE 64
|
||||
|
||||
CDataPack::CDataPack()
|
||||
{
|
||||
m_pBase = (char *)malloc(DATAPACK_INITIAL_SIZE);
|
||||
m_capacity = DATAPACK_INITIAL_SIZE;
|
||||
Initialize();
|
||||
}
|
||||
|
||||
CDataPack::~CDataPack()
|
||||
{
|
||||
free(m_pBase);
|
||||
Initialize();
|
||||
}
|
||||
|
||||
static Vector<AutoPtr<CDataPack>> sDataPackCache;
|
||||
static ke::Vector<ke::AutoPtr<CDataPack>> sDataPackCache;
|
||||
|
||||
IDataPack * CDataPack::New()
|
||||
IDataPack *CDataPack::New()
|
||||
{
|
||||
if (sDataPackCache.empty())
|
||||
return new CDataPack();
|
||||
@ -72,286 +65,161 @@ 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 index = 0; index < elements.length(); ++index)
|
||||
{
|
||||
return;
|
||||
switch (elements[index].type)
|
||||
{
|
||||
case CDataPackType::Raw:
|
||||
{
|
||||
delete elements[index].pData.vval;
|
||||
elements.remove(index--);
|
||||
break;
|
||||
}
|
||||
|
||||
case CDataPackType::String:
|
||||
{
|
||||
delete elements[index].pData.sval;
|
||||
elements.remove(index--);
|
||||
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;
|
||||
elements.clear();
|
||||
position = 0;
|
||||
}
|
||||
|
||||
void CDataPack::ResetSize()
|
||||
{
|
||||
m_size = 0;
|
||||
Initialize();
|
||||
}
|
||||
|
||||
size_t CDataPack::CreateMemory(size_t size, void **addr)
|
||||
{
|
||||
CheckSize(sizeof(char) + sizeof(size_t) + size);
|
||||
size_t pos = m_curptr - m_pBase;
|
||||
InternalPack val;
|
||||
val.type = CDataPackType::Raw;
|
||||
val.pData.vval = new uint8_t[size + sizeof(size)];
|
||||
reinterpret_cast<size_t *>(val.pData.vval)[0] = size;
|
||||
elements.insert(position, val);
|
||||
|
||||
*(char *)m_curptr = Raw;
|
||||
m_curptr += sizeof(char);
|
||||
|
||||
*(size_t *)m_curptr = size;
|
||||
m_curptr += sizeof(size_t);
|
||||
|
||||
if (addr)
|
||||
{
|
||||
*addr = m_curptr;
|
||||
}
|
||||
|
||||
m_curptr += size;
|
||||
m_size += sizeof(char) + sizeof(size_t) + size;
|
||||
|
||||
return pos;
|
||||
return 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;
|
||||
elements.insert(position++, val);
|
||||
}
|
||||
|
||||
void CDataPack::PackFloat(float val)
|
||||
void CDataPack::PackFunction(cell_t function)
|
||||
{
|
||||
CheckSize(sizeof(char) + sizeof(size_t) + sizeof(float));
|
||||
InternalPack val;
|
||||
val.type = CDataPackType::Function;
|
||||
val.pData.cval = function;
|
||||
elements.insert(position++, val);
|
||||
}
|
||||
|
||||
*(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);
|
||||
|
||||
m_size += sizeof(char) + sizeof(size_t) + sizeof(float);
|
||||
void CDataPack::PackFloat(float floatval)
|
||||
{
|
||||
InternalPack val;
|
||||
val.type = CDataPackType::Float;
|
||||
val.pData.fval = floatval;
|
||||
elements.insert(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.sval = sval;
|
||||
elements.insert(position++, val);
|
||||
}
|
||||
|
||||
void CDataPack::Reset() const
|
||||
{
|
||||
m_curptr = m_pBase;
|
||||
position = 0;
|
||||
}
|
||||
|
||||
size_t CDataPack::GetPosition() const
|
||||
{
|
||||
return static_cast<size_t>(m_curptr - m_pBase);
|
||||
return position;
|
||||
}
|
||||
|
||||
bool CDataPack::SetPosition(size_t pos) const
|
||||
{
|
||||
if (pos > m_size)
|
||||
{
|
||||
if (pos > elements.length())
|
||||
return false;
|
||||
}
|
||||
m_curptr = m_pBase + pos;
|
||||
|
||||
position = pos;
|
||||
return true;
|
||||
}
|
||||
|
||||
cell_t CDataPack::ReadCell() const
|
||||
{
|
||||
if (!IsReadable(sizeof(char) + sizeof(size_t) + sizeof(cell_t)))
|
||||
{
|
||||
if (!IsReadable() || elements[position].type != CDataPackType::Cell)
|
||||
return 0;
|
||||
}
|
||||
if (*reinterpret_cast<char *>(m_curptr) != Cell)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
m_curptr += sizeof(char);
|
||||
|
||||
if (*reinterpret_cast<size_t *>(m_curptr) != sizeof(cell_t))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
m_curptr += sizeof(size_t);
|
||||
|
||||
cell_t val = *reinterpret_cast<cell_t *>(m_curptr);
|
||||
m_curptr += sizeof(cell_t);
|
||||
return val;
|
||||
}
|
||||
|
||||
float CDataPack::ReadFloat() const
|
||||
{
|
||||
if (!IsReadable(sizeof(char) + sizeof(size_t) + sizeof(float)))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (*reinterpret_cast<char *>(m_curptr) != Float)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
m_curptr += sizeof(char);
|
||||
|
||||
if (*reinterpret_cast<size_t *>(m_curptr) != sizeof(float))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
m_curptr += sizeof(size_t);
|
||||
|
||||
float val = *reinterpret_cast<float *>(m_curptr);
|
||||
m_curptr += sizeof(float);
|
||||
return val;
|
||||
}
|
||||
|
||||
bool CDataPack::IsReadable(size_t bytes) const
|
||||
{
|
||||
return (bytes + (m_curptr - m_pBase) > m_size) ? false : true;
|
||||
}
|
||||
|
||||
const char *CDataPack::ReadString(size_t *len) const
|
||||
{
|
||||
if (!IsReadable(sizeof(char) + sizeof(size_t)))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
if (*reinterpret_cast<char *>(m_curptr) != String)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
m_curptr += sizeof(char);
|
||||
|
||||
size_t real_len = *(size_t *)m_curptr;
|
||||
|
||||
m_curptr += sizeof(size_t);
|
||||
char *str = (char *)m_curptr;
|
||||
|
||||
if ((strlen(str) != real_len) || !(IsReadable(real_len+1)))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (len)
|
||||
{
|
||||
*len = real_len;
|
||||
}
|
||||
|
||||
m_curptr += real_len + 1;
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
void *CDataPack::GetMemory() const
|
||||
{
|
||||
return m_curptr;
|
||||
}
|
||||
|
||||
void *CDataPack::ReadMemory(size_t *size) const
|
||||
{
|
||||
if (!IsReadable(sizeof(size_t)))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
if (*reinterpret_cast<char *>(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;
|
||||
|
||||
if (size)
|
||||
{
|
||||
*size = bytecount;
|
||||
}
|
||||
|
||||
m_curptr += bytecount;
|
||||
|
||||
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);
|
||||
|
||||
return elements[position++].pData.cval;
|
||||
}
|
||||
|
||||
cell_t CDataPack::ReadFunction() const
|
||||
{
|
||||
if (!IsReadable(sizeof(char) + sizeof(size_t) + sizeof(cell_t)))
|
||||
{
|
||||
if (!IsReadable() || elements[position].type != CDataPackType::Function)
|
||||
return 0;
|
||||
}
|
||||
if (*reinterpret_cast<char *>(m_curptr) != Function)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
m_curptr += sizeof(char);
|
||||
|
||||
if (*reinterpret_cast<size_t *>(m_curptr) != sizeof(cell_t))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
m_curptr += sizeof(size_t);
|
||||
|
||||
cell_t val = *reinterpret_cast<cell_t *>(m_curptr);
|
||||
m_curptr += sizeof(cell_t);
|
||||
return val;
|
||||
|
||||
return elements[position++].pData.cval;
|
||||
}
|
||||
|
||||
float CDataPack::ReadFloat() const
|
||||
{
|
||||
if (!IsReadable() || elements[position].type != CDataPackType::Float)
|
||||
return 0;
|
||||
|
||||
return elements[position++].pData.fval;
|
||||
}
|
||||
|
||||
bool CDataPack::IsReadable(size_t bytes) const
|
||||
{
|
||||
return (position < elements.length());
|
||||
}
|
||||
|
||||
const char *CDataPack::ReadString(size_t *len) const
|
||||
{
|
||||
if (!IsReadable() || elements[position].type != CDataPackType::String)
|
||||
{
|
||||
if (len)
|
||||
*len = 0;
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const ke::AString &val = *elements[position++].pData.sval;
|
||||
if (len)
|
||||
*len = val.length();
|
||||
|
||||
return val.chars();
|
||||
}
|
||||
|
||||
void *CDataPack::GetMemory() const
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void *CDataPack::ReadMemory(size_t *size) const
|
||||
{
|
||||
void *ptr = nullptr;
|
||||
if (!IsReadable() || elements[position].type != CDataPackType::Raw)
|
||||
return ptr;
|
||||
|
||||
size_t *val = reinterpret_cast<size_t *>(elements[position].pData.vval);
|
||||
ptr = &(val[1]);
|
||||
++position;
|
||||
|
||||
if (size)
|
||||
*size = val[0]; /* Egor!!!! */
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
@ -33,9 +33,19 @@
|
||||
#define _INCLUDE_SOURCEMOD_CDATAPACK_H_
|
||||
|
||||
#include <IDataPack.h>
|
||||
#include <amtl/am-vector.h>
|
||||
#include <amtl/am-string.h>
|
||||
|
||||
using namespace SourceMod;
|
||||
|
||||
enum CDataPackType {
|
||||
Raw,
|
||||
Cell,
|
||||
Float,
|
||||
String,
|
||||
Function
|
||||
};
|
||||
|
||||
class CDataPack : public IDataPack
|
||||
{
|
||||
public:
|
||||
@ -50,7 +60,7 @@ public: //IDataReader
|
||||
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 +74,24 @@ 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(); };
|
||||
inline 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;
|
||||
uint8_t *vval;
|
||||
ke::AString *sval;
|
||||
} InternalPackValue;
|
||||
|
||||
typedef struct {
|
||||
InternalPackValue pData;
|
||||
CDataPackType type;
|
||||
} InternalPack;
|
||||
|
||||
ke::Vector<InternalPack> elements;
|
||||
mutable size_t position;
|
||||
};
|
||||
|
||||
#endif //_INCLUDE_SOURCEMOD_CDATAPACK_H_
|
||||
|
@ -61,7 +61,7 @@ public:
|
||||
}
|
||||
void OnHandleDestroy(HandleType_t type, void *object)
|
||||
{
|
||||
CDataPack::Free(reinterpret_cast<IDataPack *>(object));
|
||||
CDataPack::Free(reinterpret_cast<CDataPack *>(object));
|
||||
}
|
||||
bool GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize)
|
||||
{
|
||||
@ -73,7 +73,7 @@ public:
|
||||
|
||||
static cell_t smn_CreateDataPack(IPluginContext *pContext, const cell_t *params)
|
||||
{
|
||||
IDataPack *pDataPack = CDataPack::New();
|
||||
CDataPack *pDataPack = static_cast<CDataPack *>(CDataPack::New());
|
||||
|
||||
if (!pDataPack)
|
||||
{
|
||||
@ -88,7 +88,7 @@ static cell_t smn_WritePackCell(IPluginContext *pContext, const cell_t *params)
|
||||
Handle_t hndl = static_cast<Handle_t>(params[1]);
|
||||
HandleError herr;
|
||||
HandleSecurity sec;
|
||||
IDataPack *pDataPack;
|
||||
CDataPack *pDataPack;
|
||||
|
||||
sec.pOwner = pContext->GetIdentity();
|
||||
sec.pIdentity = g_pCoreIdent;
|
||||
@ -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]);
|
||||
@ -109,7 +109,7 @@ static cell_t smn_WritePackFloat(IPluginContext *pContext, const cell_t *params)
|
||||
Handle_t hndl = static_cast<Handle_t>(params[1]);
|
||||
HandleError herr;
|
||||
HandleSecurity sec;
|
||||
IDataPack *pDataPack;
|
||||
CDataPack *pDataPack;
|
||||
|
||||
sec.pOwner = pContext->GetIdentity();
|
||||
sec.pIdentity = g_pCoreIdent;
|
||||
@ -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]));
|
||||
@ -130,7 +130,7 @@ static cell_t smn_WritePackString(IPluginContext *pContext, const cell_t *params
|
||||
Handle_t hndl = static_cast<Handle_t>(params[1]);
|
||||
HandleError herr;
|
||||
HandleSecurity sec;
|
||||
IDataPack *pDataPack;
|
||||
CDataPack *pDataPack;
|
||||
|
||||
sec.pOwner = pContext->GetIdentity();
|
||||
sec.pIdentity = g_pCoreIdent;
|
||||
@ -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;
|
||||
@ -153,7 +153,7 @@ static cell_t smn_WritePackFunction(IPluginContext *pContext, const cell_t *para
|
||||
Handle_t hndl = static_cast<Handle_t>(params[1]);
|
||||
HandleError herr;
|
||||
HandleSecurity sec;
|
||||
IDataPack *pDataPack;
|
||||
CDataPack *pDataPack;
|
||||
|
||||
sec.pOwner = pContext->GetIdentity();
|
||||
sec.pIdentity = g_pCoreIdent;
|
||||
@ -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]);
|
||||
@ -174,7 +174,7 @@ static cell_t smn_ReadPackCell(IPluginContext *pContext, const cell_t *params)
|
||||
Handle_t hndl = static_cast<Handle_t>(params[1]);
|
||||
HandleError herr;
|
||||
HandleSecurity sec;
|
||||
IDataPack *pDataPack;
|
||||
CDataPack *pDataPack;
|
||||
|
||||
sec.pOwner = pContext->GetIdentity();
|
||||
sec.pIdentity = g_pCoreIdent;
|
||||
@ -182,12 +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(sizeof(char) + sizeof(size_t) + sizeof(cell_t)))
|
||||
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 data pack 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<Handle_t>(params[1]);
|
||||
HandleError herr;
|
||||
HandleSecurity sec;
|
||||
IDataPack *pDataPack;
|
||||
CDataPack *pDataPack;
|
||||
|
||||
sec.pOwner = pContext->GetIdentity();
|
||||
sec.pIdentity = g_pCoreIdent;
|
||||
@ -206,12 +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(sizeof(char) + sizeof(size_t) + sizeof(float)))
|
||||
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 data pack 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<Handle_t>(params[1]);
|
||||
HandleError herr;
|
||||
HandleSecurity sec;
|
||||
IDataPack *pDataPack;
|
||||
CDataPack *pDataPack;
|
||||
|
||||
sec.pOwner = pContext->GetIdentity();
|
||||
sec.pIdentity = g_pCoreIdent;
|
||||
@ -230,15 +240,20 @@ 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);
|
||||
}
|
||||
|
||||
const char *str;
|
||||
if (!(str=pDataPack->ReadString(NULL)))
|
||||
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 data pack 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<Handle_t>(params[1]);
|
||||
HandleError herr;
|
||||
HandleSecurity sec;
|
||||
IDataPack *pDataPack;
|
||||
CDataPack *pDataPack;
|
||||
|
||||
sec.pOwner = pContext->GetIdentity();
|
||||
sec.pIdentity = g_pCoreIdent;
|
||||
@ -257,12 +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(sizeof(char) + sizeof(size_t) + sizeof(cell_t)))
|
||||
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 data pack 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<Handle_t>(params[1]);
|
||||
HandleError herr;
|
||||
HandleSecurity sec;
|
||||
IDataPack *pDataPack;
|
||||
CDataPack *pDataPack;
|
||||
|
||||
sec.pOwner = pContext->GetIdentity();
|
||||
sec.pIdentity = g_pCoreIdent;
|
||||
@ -281,14 +301,17 @@ 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);
|
||||
}
|
||||
|
||||
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<Handle_t>(params[1]);
|
||||
HandleError herr;
|
||||
HandleSecurity sec;
|
||||
IDataPack *pDataPack;
|
||||
CDataPack *pDataPack;
|
||||
|
||||
sec.pOwner = pContext->GetIdentity();
|
||||
sec.pIdentity = g_pCoreIdent;
|
||||
@ -306,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<cell_t>(pDataPack->GetPosition());
|
||||
@ -317,7 +340,7 @@ static cell_t smn_SetPackPosition(IPluginContext *pContext, const cell_t *params
|
||||
Handle_t hndl = static_cast<Handle_t>(params[1]);
|
||||
HandleError herr;
|
||||
HandleSecurity sec;
|
||||
IDataPack *pDataPack;
|
||||
CDataPack *pDataPack;
|
||||
|
||||
sec.pOwner = pContext->GetIdentity();
|
||||
sec.pIdentity = g_pCoreIdent;
|
||||
@ -325,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", params[2]);
|
||||
return pContext->ThrowNativeError("Invalid data pack 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<Handle_t>(params[1]);
|
||||
HandleError herr;
|
||||
HandleSecurity sec;
|
||||
IDataPack *pDataPack;
|
||||
CDataPack *pDataPack;
|
||||
|
||||
sec.pOwner = pContext->GetIdentity();
|
||||
sec.pIdentity = g_pCoreIdent;
|
||||
@ -349,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;
|
||||
|
@ -619,7 +619,7 @@ unsigned int SourceModBase::GetGlobalTarget() const
|
||||
|
||||
IDataPack *SourceModBase::CreateDataPack()
|
||||
{
|
||||
return logicore.CreateDataPack();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void SourceModBase::FreeDataPack(IDataPack *pack)
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user