Add ICellArray and create/free in ISourceMod

Add an ICellArray interface to expose the adt_array CellArray class from
logic to core.
Add methods to ISourceMod to create and delete ICellArray instances in
logic.
This commit is contained in:
Peace-Maker 2016-08-21 21:32:23 +02:00
parent f3c454084a
commit 5b9ae5917b
10 changed files with 202 additions and 7 deletions

View File

@ -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

View File

@ -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;

View File

@ -34,10 +34,11 @@
#include <stdlib.h>
#include <string.h>
#include <ICellArray.h>
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,18 @@ public:
free(m_Data);
}
static ICellArray *New(size_t blocksize)
{
return new CellArray(blocksize);
}
static void Free(ICellArray *arr)
{
delete arr;
}
// ICellArray
public:
size_t size() const
{
return m_Size;
@ -154,7 +167,7 @@ public:
return true;
}
CellArray *clone()
ICellArray *clone()
{
CellArray *array = new CellArray(m_BlockSize);
array->m_AllocSize = m_AllocSize;

View File

@ -56,6 +56,7 @@
#include "LibrarySys.h"
#include "RootConsoleMenu.h"
#include "CDataPack.h"
#include "CellArray.h"
#include <bridge/include/BridgeAPI.h>
#include <bridge/include/IProviderCallbacks.h>
@ -147,6 +148,8 @@ static sm_logic_t logic =
RegisterProfiler,
CDataPack::New,
CDataPack::Free,
CellArray::New,
CellArray::Free,
&g_PluginSys,
&g_ShareSys,
&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);
}
CellArray *array = oldArray->clone();
ICellArray *array = oldArray->clone();
Handle_t hndl = handlesys->CreateHandle(htCellArray, array, pContext->GetIdentity(), g_pCoreIdent, NULL);
if (!hndl)

View File

@ -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);

View File

@ -745,6 +745,16 @@ bool SourceModBase::IsMapRunning()
return g_OnMapStarted;
}
ICellArray *SourceModBase::CreateCellArray(size_t blocksize)
{
return logicore.CreateCellArray(blocksize);
}
void SourceModBase::FreeCellArray(ICellArray *arr)
{
logicore.FreeCellArray(arr);
}
class ConVarRegistrar :
public IConCommandBaseAccessor,
public SMGlobalClass

View File

@ -135,6 +135,8 @@ public: // ISourceMod
int GetPluginId();
int GetShApiVersion();
bool IsMapRunning();
ICellArray *CreateCellArray(size_t blocksize);
void FreeCellArray(ICellArray *arr);
private:
void ShutdownServices();
private:

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_

View File

@ -40,10 +40,11 @@
#include <IHandleSys.h>
#include <sp_vm_api.h>
#include <IDataPack.h>
#include <ICellArray.h>
#include <time.h>
#define SMINTERFACE_SOURCEMOD_NAME "ISourceMod"
#define SMINTERFACE_SOURCEMOD_VERSION 13
#define SMINTERFACE_SOURCEMOD_VERSION 14
/**
* @brief Forward declaration of the KeyValues class.
@ -318,6 +319,23 @@ namespace SourceMod
* @return True if a map is currently running, otherwise false.
*/
virtual bool IsMapRunning() = 0;
/**
* @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.
*/
virtual ICellArray *CreateCellArray(size_t blocksize) = 0;
/**
* @brief Releases a cell array's resources so it can be re-used.
*
* @param pack An ICellArray object to release.
*/
virtual void FreeCellArray(ICellArray *arr) = 0;
};
}