Merge pull request #537 from peace-maker/cellarray_bridge

Add ICellArray creation/deletion to logic bridge
This commit is contained in:
David Anderson 2016-08-27 18:17:37 -07:00 committed by GitHub
commit ab53e580db
7 changed files with 184 additions and 6 deletions

View File

@ -35,7 +35,7 @@ namespace SourceMod {
// Add 1 to the RHS of this expression to bump the intercom file // Add 1 to the RHS of this expression to bump the intercom file
// This is to prevent mismatching core/logic binaries // 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 } // namespace SourceMod

View File

@ -51,6 +51,7 @@ class IExtensionSys;
class ITextParsers; class ITextParsers;
class ILogger; class ILogger;
class IDataPack; class IDataPack;
class ICellArray;
struct sm_logic_t struct sm_logic_t
{ {
@ -71,6 +72,8 @@ struct sm_logic_t
void (*RegisterProfiler)(IProfilingTool *tool); void (*RegisterProfiler)(IProfilingTool *tool);
IDataPack * (*CreateDataPack)(); IDataPack * (*CreateDataPack)();
void (*FreeDataPack)(IDataPack *pack); void (*FreeDataPack)(IDataPack *pack);
ICellArray * (*CreateCellArray)(size_t blocksize);
void (*FreeCellArray)(ICellArray *arr);
IScriptManager *scripts; IScriptManager *scripts;
IShareSys *sharesys; IShareSys *sharesys;
IExtensionSys *extsys; IExtensionSys *extsys;

View File

@ -34,10 +34,11 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <ICellArray.h>
extern HandleType_t htCellArray; extern HandleType_t htCellArray;
class CellArray class CellArray : public ICellArray
{ {
public: public:
CellArray(size_t blocksize) : m_Data(NULL), m_BlockSize(blocksize), m_AllocSize(0), m_Size(0) CellArray(size_t blocksize) : m_Data(NULL), m_BlockSize(blocksize), m_AllocSize(0), m_Size(0)
@ -49,6 +50,31 @@ public:
free(m_Data); 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 size_t size() const
{ {
return m_Size; return m_Size;
@ -154,7 +180,7 @@ public:
return true; return true;
} }
CellArray *clone() ICellArray *clone()
{ {
CellArray *array = new CellArray(m_BlockSize); CellArray *array = new CellArray(m_BlockSize);
array->m_AllocSize = m_AllocSize; array->m_AllocSize = m_AllocSize;

View File

@ -56,6 +56,7 @@
#include "LibrarySys.h" #include "LibrarySys.h"
#include "RootConsoleMenu.h" #include "RootConsoleMenu.h"
#include "CDataPack.h" #include "CDataPack.h"
#include "CellArray.h"
#include <bridge/include/BridgeAPI.h> #include <bridge/include/BridgeAPI.h>
#include <bridge/include/IProviderCallbacks.h> #include <bridge/include/IProviderCallbacks.h>
@ -147,6 +148,8 @@ static sm_logic_t logic =
RegisterProfiler, RegisterProfiler,
CDataPack::New, CDataPack::New,
CDataPack::Free, CDataPack::Free,
CellArray::New,
CellArray::Free,
&g_PluginSys, &g_PluginSys,
&g_ShareSys, &g_ShareSys,
&g_Extensions, &g_Extensions,

View File

@ -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); 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); Handle_t hndl = handlesys->CreateHandle(htCellArray, array, pContext->GetIdentity(), g_pCoreIdent, NULL);
if (!hndl) if (!hndl)

View File

@ -321,7 +321,7 @@ public:
{ {
return strcmp((char *)str1, (char *)str2); 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; int change_serial;
CellArray *pNewArray = NULL; CellArray *pNewArray = NULL;
@ -594,7 +594,7 @@ static cell_t LoadMapList(IPluginContext *pContext, const cell_t *params)
char *str; char *str;
Handle_t hndl; Handle_t hndl;
cell_t *addr, flags; cell_t *addr, flags;
CellArray *pArray, *pNewArray; ICellArray *pArray, *pNewArray;
hndl = params[1]; hndl = params[1];
pContext->LocalToPhysAddr(params[2], &addr); pContext->LocalToPhysAddr(params[2], &addr);

146
public/ICellArray.h Normal file
View File

@ -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 <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$
*/
#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_