2008-03-30 09:00:22 +02:00
|
|
|
/**
|
|
|
|
* vim: set ts=4 :
|
|
|
|
* =============================================================================
|
|
|
|
* SourceMod
|
|
|
|
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
|
|
|
|
* =============================================================================
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU General Public License, version 3.0, as published by the
|
|
|
|
* Free Software Foundation.
|
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* As a special exception, AlliedModders LLC gives you permission to link the
|
|
|
|
* code of this program (as well as its derivative works) to "Half-Life 2," the
|
|
|
|
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
|
|
|
|
* by the Valve Corporation. You must obey the GNU General Public License in
|
|
|
|
* all respects for all other code used. Additionally, AlliedModders LLC grants
|
|
|
|
* this exception to all derivative works. AlliedModders LLC defines further
|
|
|
|
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
|
|
|
|
* or <http://www.sourcemod.net/license.php>.
|
|
|
|
*
|
|
|
|
* Version: $Id$
|
|
|
|
*/
|
|
|
|
|
2008-12-23 07:33:37 +01:00
|
|
|
#include <stdlib.h>
|
2008-03-30 09:00:22 +02:00
|
|
|
#include <string.h>
|
2020-05-17 08:15:32 +02:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
2008-03-30 09:00:22 +02:00
|
|
|
#include "CDataPack.h"
|
|
|
|
|
|
|
|
CDataPack::CDataPack()
|
|
|
|
{
|
|
|
|
Initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
CDataPack::~CDataPack()
|
|
|
|
{
|
2017-12-30 04:56:02 +01:00
|
|
|
Initialize();
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
|
|
|
|
2020-05-17 08:15:32 +02:00
|
|
|
static ke::Vector<std::unique_ptr<CDataPack>> sDataPackCache;
|
2015-09-06 06:10:32 +02:00
|
|
|
|
2018-08-14 00:03:48 +02:00
|
|
|
CDataPack *CDataPack::New()
|
2015-09-06 06:10:32 +02:00
|
|
|
{
|
|
|
|
if (sDataPackCache.empty())
|
|
|
|
return new CDataPack();
|
|
|
|
|
2020-05-17 08:15:32 +02:00
|
|
|
CDataPack *pack = sDataPackCache.back().release();
|
2015-09-06 06:10:32 +02:00
|
|
|
sDataPackCache.pop();
|
|
|
|
pack->Initialize();
|
|
|
|
return pack;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-08-14 00:03:48 +02:00
|
|
|
CDataPack::Free(CDataPack *pack)
|
2015-09-06 06:10:32 +02:00
|
|
|
{
|
|
|
|
sDataPackCache.append(static_cast<CDataPack *>(pack));
|
|
|
|
}
|
|
|
|
|
2008-03-30 09:00:22 +02:00
|
|
|
void CDataPack::Initialize()
|
|
|
|
{
|
2020-04-28 03:09:13 +02:00
|
|
|
position = 0;
|
|
|
|
|
2018-07-16 17:34:49 +02:00
|
|
|
do
|
2008-03-30 09:00:22 +02:00
|
|
|
{
|
2018-07-16 17:34:49 +02:00
|
|
|
} while (this->RemoveItem());
|
2008-03-30 09:00:22 +02:00
|
|
|
|
2017-12-30 04:56:02 +01:00
|
|
|
elements.clear();
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CDataPack::ResetSize()
|
|
|
|
{
|
2017-12-30 04:56:02 +01:00
|
|
|
Initialize();
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t CDataPack::CreateMemory(size_t size, void **addr)
|
|
|
|
{
|
2017-12-30 04:56:02 +01:00
|
|
|
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);
|
2008-03-30 09:00:22 +02:00
|
|
|
|
2017-12-30 04:56:02 +01:00
|
|
|
return position++;
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CDataPack::PackCell(cell_t cell)
|
|
|
|
{
|
2017-12-30 04:56:02 +01:00
|
|
|
InternalPack val;
|
|
|
|
val.type = CDataPackType::Cell;
|
|
|
|
val.pData.cval = cell;
|
|
|
|
elements.insert(position++, val);
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
|
|
|
|
2017-12-30 04:56:02 +01:00
|
|
|
void CDataPack::PackFunction(cell_t function)
|
2008-03-30 09:00:22 +02:00
|
|
|
{
|
2017-12-30 04:56:02 +01:00
|
|
|
InternalPack val;
|
|
|
|
val.type = CDataPackType::Function;
|
|
|
|
val.pData.cval = function;
|
|
|
|
elements.insert(position++, val);
|
|
|
|
}
|
2008-03-30 09:00:22 +02:00
|
|
|
|
2017-12-30 04:56:02 +01:00
|
|
|
void CDataPack::PackFloat(float floatval)
|
|
|
|
{
|
|
|
|
InternalPack val;
|
|
|
|
val.type = CDataPackType::Float;
|
|
|
|
val.pData.fval = floatval;
|
|
|
|
elements.insert(position++, val);
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CDataPack::PackString(const char *string)
|
|
|
|
{
|
2017-12-30 04:56:02 +01:00
|
|
|
InternalPack val;
|
|
|
|
val.type = CDataPackType::String;
|
|
|
|
ke::AString *sval = new ke::AString(string);
|
|
|
|
val.pData.sval = sval;
|
|
|
|
elements.insert(position++, val);
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
|
|
|
|
2020-05-09 00:23:48 +02:00
|
|
|
void CDataPack::PackCellArray(cell_t const *vals, cell_t count)
|
|
|
|
{
|
|
|
|
InternalPack val;
|
|
|
|
val.type = CDataPackType::CellArray;
|
|
|
|
|
|
|
|
val.pData.aval = new cell_t [count + 1];
|
|
|
|
memcpy(&val.pData.aval[1], vals, sizeof(cell_t) * (count + 1));
|
|
|
|
val.pData.aval[0] = count;
|
|
|
|
elements.insert(position++, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CDataPack::PackFloatArray(cell_t const *vals, cell_t count)
|
|
|
|
{
|
|
|
|
InternalPack val;
|
|
|
|
val.type = CDataPackType::FloatArray;
|
|
|
|
|
|
|
|
val.pData.aval = new cell_t [count + 1];
|
|
|
|
memcpy(&val.pData.aval[1], vals, sizeof(cell_t) * (count + 1));
|
|
|
|
val.pData.aval[0] = count;
|
|
|
|
elements.insert(position++, val);
|
|
|
|
}
|
|
|
|
|
2008-03-30 09:00:22 +02:00
|
|
|
void CDataPack::Reset() const
|
|
|
|
{
|
2017-12-30 04:56:02 +01:00
|
|
|
position = 0;
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t CDataPack::GetPosition() const
|
|
|
|
{
|
2017-12-30 04:56:02 +01:00
|
|
|
return position;
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CDataPack::SetPosition(size_t pos) const
|
|
|
|
{
|
2017-12-30 04:56:02 +01:00
|
|
|
if (pos > elements.length())
|
2008-03-30 09:00:22 +02:00
|
|
|
return false;
|
|
|
|
|
2017-12-30 04:56:02 +01:00
|
|
|
position = pos;
|
2008-03-30 09:00:22 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
cell_t CDataPack::ReadCell() const
|
|
|
|
{
|
2017-12-30 04:56:02 +01:00
|
|
|
if (!IsReadable() || elements[position].type != CDataPackType::Cell)
|
2014-08-04 02:05:25 +02:00
|
|
|
return 0;
|
2017-12-30 04:56:02 +01:00
|
|
|
|
|
|
|
return elements[position++].pData.cval;
|
|
|
|
}
|
2014-08-04 02:05:25 +02:00
|
|
|
|
2017-12-30 04:56:02 +01:00
|
|
|
cell_t CDataPack::ReadFunction() const
|
|
|
|
{
|
|
|
|
if (!IsReadable() || elements[position].type != CDataPackType::Function)
|
2008-03-30 09:00:22 +02:00
|
|
|
return 0;
|
2017-12-30 04:56:02 +01:00
|
|
|
|
|
|
|
return elements[position++].pData.cval;
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
float CDataPack::ReadFloat() const
|
|
|
|
{
|
2017-12-30 04:56:02 +01:00
|
|
|
if (!IsReadable() || elements[position].type != CDataPackType::Float)
|
2008-03-30 09:00:22 +02:00
|
|
|
return 0;
|
2017-12-30 04:56:02 +01:00
|
|
|
|
|
|
|
return elements[position++].pData.fval;
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CDataPack::IsReadable(size_t bytes) const
|
|
|
|
{
|
2017-12-30 04:56:02 +01:00
|
|
|
return (position < elements.length());
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *CDataPack::ReadString(size_t *len) const
|
|
|
|
{
|
2017-12-30 04:56:02 +01:00
|
|
|
if (!IsReadable() || elements[position].type != CDataPackType::String)
|
2014-08-04 02:05:25 +02:00
|
|
|
{
|
2017-12-30 04:56:02 +01:00
|
|
|
if (len)
|
|
|
|
*len = 0;
|
2008-03-30 09:00:22 +02:00
|
|
|
|
2017-12-30 04:56:02 +01:00
|
|
|
return nullptr;
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
|
|
|
|
2017-12-30 04:56:02 +01:00
|
|
|
const ke::AString &val = *elements[position++].pData.sval;
|
2008-03-30 09:00:22 +02:00
|
|
|
if (len)
|
2017-12-30 04:56:02 +01:00
|
|
|
*len = val.length();
|
2008-03-30 09:00:22 +02:00
|
|
|
|
2017-12-30 04:56:02 +01:00
|
|
|
return val.chars();
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
|
|
|
|
2020-05-09 00:23:48 +02:00
|
|
|
cell_t *CDataPack::ReadCellArray(cell_t *size) const
|
|
|
|
{
|
|
|
|
if (!IsReadable() || elements[position].type != CDataPackType::CellArray)
|
|
|
|
{
|
|
|
|
if(size)
|
|
|
|
*size = 0;
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
cell_t *val = elements[position].pData.aval;
|
|
|
|
cell_t *ptr = &(val[1]);
|
|
|
|
++position;
|
|
|
|
|
|
|
|
if (size)
|
|
|
|
*size = val[0];
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
cell_t *CDataPack::ReadFloatArray(cell_t *size) const
|
|
|
|
{
|
|
|
|
if (!IsReadable() || elements[position].type != CDataPackType::FloatArray)
|
|
|
|
{
|
|
|
|
if(size)
|
|
|
|
*size = 0;
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
cell_t *val = elements[position].pData.aval;
|
|
|
|
cell_t *ptr = &(val[1]);
|
|
|
|
++position;
|
|
|
|
|
|
|
|
if (size)
|
|
|
|
*size = val[0];
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2008-03-30 09:00:22 +02:00
|
|
|
void *CDataPack::ReadMemory(size_t *size) const
|
|
|
|
{
|
2017-12-30 04:56:02 +01:00
|
|
|
void *ptr = nullptr;
|
|
|
|
if (!IsReadable() || elements[position].type != CDataPackType::Raw)
|
|
|
|
return ptr;
|
2008-03-30 09:00:22 +02:00
|
|
|
|
2017-12-30 04:56:02 +01:00
|
|
|
size_t *val = reinterpret_cast<size_t *>(elements[position].pData.vval);
|
|
|
|
ptr = &(val[1]);
|
|
|
|
++position;
|
2008-03-30 09:00:22 +02:00
|
|
|
|
|
|
|
if (size)
|
2017-12-30 04:56:02 +01:00
|
|
|
*size = val[0]; /* Egor!!!! */
|
2008-03-30 09:00:22 +02:00
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
2018-07-15 02:00:17 +02:00
|
|
|
|
|
|
|
bool CDataPack::RemoveItem(size_t pos)
|
|
|
|
{
|
|
|
|
if (!elements.length())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pos == static_cast<size_t>(-1))
|
|
|
|
{
|
|
|
|
pos = position;
|
|
|
|
}
|
2020-04-28 03:09:13 +02:00
|
|
|
|
2018-07-15 02:00:17 +02:00
|
|
|
if (pos >= elements.length())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-11 14:31:33 +02:00
|
|
|
if (pos < position) // we're deleting under us, step back
|
|
|
|
{
|
|
|
|
--position;
|
|
|
|
}
|
|
|
|
|
2018-07-16 17:34:49 +02:00
|
|
|
switch (elements[pos].type)
|
|
|
|
{
|
|
|
|
case CDataPackType::Raw:
|
|
|
|
{
|
2020-04-28 03:09:13 +02:00
|
|
|
delete [] elements[pos].pData.vval;
|
2018-07-16 17:34:49 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case CDataPackType::String:
|
|
|
|
{
|
|
|
|
delete elements[pos].pData.sval;
|
|
|
|
break;
|
|
|
|
}
|
2020-05-09 00:23:48 +02:00
|
|
|
|
|
|
|
case CDataPackType::CellArray:
|
|
|
|
case CDataPackType::FloatArray:
|
|
|
|
{
|
|
|
|
delete elements[pos].pData.aval;
|
|
|
|
break;
|
|
|
|
}
|
2018-07-16 17:34:49 +02:00
|
|
|
}
|
|
|
|
|
2018-07-15 02:00:17 +02:00
|
|
|
elements.remove(pos);
|
|
|
|
return true;
|
|
|
|
}
|