diff --git a/bridge/include/BridgeAPI.h b/bridge/include/BridgeAPI.h index 3845963e..d9e1cdaf 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 - 54; +static const uint32_t SM_LOGIC_MAGIC = 0x0F47C0DE - 55; } // namespace SourceMod diff --git a/bridge/include/LogicProvider.h b/bridge/include/LogicProvider.h index 253084f8..75821f37 100644 --- a/bridge/include/LogicProvider.h +++ b/bridge/include/LogicProvider.h @@ -51,6 +51,7 @@ class IExtensionSys; class ITextParsers; class ILogger; class IDataPack; +class ICellArray; struct sm_logic_t { @@ -71,6 +72,8 @@ struct sm_logic_t void (*RegisterProfiler)(IProfilingTool *tool); IDataPack * (*CreateDataPack)(); void (*FreeDataPack)(IDataPack *pack); + ICellArray * (*CreateCellArray)(size_t blocksize); + void (*FreeCellArray)(ICellArray *arr); IScriptManager *scripts; IShareSys *sharesys; IExtensionSys *extsys; diff --git a/core/logic/CellArray.h b/core/logic/CellArray.h index ee9a1fc7..6cea7b91 100644 --- a/core/logic/CellArray.h +++ b/core/logic/CellArray.h @@ -34,10 +34,11 @@ #include #include +#include extern HandleType_t htCellArray; -class CellArray +class CellArray : public ICellArray { public: CellArray(size_t blocksize) : m_Data(NULL), m_BlockSize(blocksize), m_AllocSize(0), m_Size(0) @@ -49,6 +50,31 @@ public: free(m_Data); } + /** + * @brief Creates a cell array object. + * + * @param blocksize The number of cells each member of the array can + * hold. For example, 32 cells is equivalent to: + * new Array[X][32] + * @return A new ICellArray object. + */ + static ICellArray *New(size_t blocksize) + { + return new CellArray(blocksize); + } + + /** + * @brief Releases a cell array's resources. + * + * @param pack An ICellArray object to release. + */ + static void Free(ICellArray *arr) + { + delete arr; + } + + // ICellArray +public: size_t size() const { return m_Size; @@ -154,7 +180,7 @@ public: return true; } - CellArray *clone() + ICellArray *clone() { CellArray *array = new CellArray(m_BlockSize); array->m_AllocSize = m_AllocSize; diff --git a/core/logic/common_logic.cpp b/core/logic/common_logic.cpp index a39ed8bd..e6a3f333 100644 --- a/core/logic/common_logic.cpp +++ b/core/logic/common_logic.cpp @@ -56,6 +56,7 @@ #include "LibrarySys.h" #include "RootConsoleMenu.h" #include "CDataPack.h" +#include "CellArray.h" #include #include @@ -147,6 +148,8 @@ static sm_logic_t logic = RegisterProfiler, CDataPack::New, CDataPack::Free, + CellArray::New, + CellArray::Free, &g_PluginSys, &g_ShareSys, &g_Extensions, diff --git a/core/logic/smn_adt_array.cpp b/core/logic/smn_adt_array.cpp index 3d3a74a8..3c0d5b9e 100644 --- a/core/logic/smn_adt_array.cpp +++ b/core/logic/smn_adt_array.cpp @@ -502,7 +502,7 @@ static cell_t CloneArray(IPluginContext *pContext, const cell_t *params) return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err); } - CellArray *array = oldArray->clone(); + ICellArray *array = oldArray->clone(); Handle_t hndl = handlesys->CreateHandle(htCellArray, array, pContext->GetIdentity(), g_pCoreIdent, NULL); if (!hndl) diff --git a/core/logic/smn_maplists.cpp b/core/logic/smn_maplists.cpp index 1532f8d5..c583a4eb 100644 --- a/core/logic/smn_maplists.cpp +++ b/core/logic/smn_maplists.cpp @@ -321,7 +321,7 @@ public: { return strcmp((char *)str1, (char *)str2); } - CellArray *UpdateMapList(CellArray *pUseArray, const char *name, int *pSerial, unsigned int flags) + ICellArray *UpdateMapList(ICellArray *pUseArray, const char *name, int *pSerial, unsigned int flags) { int change_serial; CellArray *pNewArray = NULL; @@ -594,7 +594,7 @@ static cell_t LoadMapList(IPluginContext *pContext, const cell_t *params) char *str; Handle_t hndl; cell_t *addr, flags; - CellArray *pArray, *pNewArray; + ICellArray *pArray, *pNewArray; hndl = params[1]; pContext->LocalToPhysAddr(params[2], &addr); diff --git a/public/ICellArray.h b/public/ICellArray.h new file mode 100644 index 00000000..389f0e4d --- /dev/null +++ b/public/ICellArray.h @@ -0,0 +1,146 @@ +/** + * vim: set ts=4 : + * ============================================================================= + * SourceMod + * Copyright (C) 2016 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 . + * + * 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 . + * + * Version: $Id$ + */ + +#ifndef _INCLUDE_SOURCEMOD_INTERFACE_CELLARRAY_H_ +#define _INCLUDE_SOURCEMOD_INTERFACE_CELLARRAY_H_ + +/** + * @file ICellArray.h + * @brief Contains functions for dynamic arrays in plugins. The wrappers + * for creating these are contained in ISourceMod.h + */ + +namespace SourceMod +{ + /** + * @brief Specifies a dynamic array data structure used in plugins. + */ + class ICellArray + { + public: + /** + * @brief Retrieve the size of the array. + * + * @return The size of the array. + */ + virtual size_t size() const = 0; + + /** + * @brief Increases the size of the array by one and returns + * a pointer to the newly added item at the end of the array. + * + * @return A pointer to the new item added at the end + * or NULL if growing the array failed. + */ + virtual cell_t *push() = 0; + + /** + * @brief Retrieve a pointer to the memory at a given index. + * + * @return A pointer to the memory for item at given index. + */ + virtual cell_t *at(size_t index) const = 0; + + /** + * @brief Retrieve the block size set while creating the CellArray. + * It determines how many cells each array slot has. + * + * @return The block size of the array. + */ + virtual size_t blocksize() const = 0; + + /** + * @brief Clears an array of all entries. + * This is the same as Resize(0). + */ + virtual void clear() = 0; + + /** + * @brief Swaps two items in the array. + * + * @param item1 First index. + * @param item2 Second index. + * @return True if items were swapped, false otherwise. + */ + virtual bool swap(size_t item1, size_t item2) = 0; + + /** + * @brief Removes an array index, shifting the entire array down from that position + * on. For example, if item 8 of 10 is removed, the last 3 items will then be + * (6,7,8) instead of (7,8,9), and all indexes before 8 will remain unchanged. + * + * @param index Index in the array to remove at. + */ + virtual void remove(size_t index) = 0; + + /** + * @brief Shifts items at the given index and following up by one + * to make space for a new item at the index. + * + * @param index Index in the array to insert at. + * @return Pointer to item space at the given index or NULL if shifting failed. + */ + virtual cell_t *insert_at(size_t index) = 0; + + /** + * @brief Resizes an array. If the size is smaller than the current size, the + * array is truncated. + * + * @param newsize New size + * @return True if resized, false otherwise. + */ + virtual bool resize(size_t newsize) = 0; + + /** + * @brief Clones an array, returning a new object + * with the same size and data. + * + * @return Pointer to cloned array instance. + */ + virtual ICellArray *clone() = 0; + + /** + * @brief Retrieve a pointer to the array base. + * + * @return Pointer to the array base. + */ + virtual cell_t *base() = 0; + + /** + * @brief Retrieve the amount of memory used by this array in bytes. + * + * @return Amount of memory used in bytes. + */ + virtual size_t mem_usage() = 0; + }; +} + +#endif //_INCLUDE_SOURCEMOD_INTERFACE_CELLARRAY_H_