Skip to content

Commit ab53e58

Browse files
authored
Merge pull request #537 from peace-maker/cellarray_bridge
Add ICellArray creation/deletion to logic bridge
2 parents f3c4540 + ecbedb7 commit ab53e58

File tree

7 files changed

+184
-6
lines changed

7 files changed

+184
-6
lines changed

bridge/include/BridgeAPI.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace SourceMod {
3535

3636
// Add 1 to the RHS of this expression to bump the intercom file
3737
// This is to prevent mismatching core/logic binaries
38-
static const uint32_t SM_LOGIC_MAGIC = 0x0F47C0DE - 54;
38+
static const uint32_t SM_LOGIC_MAGIC = 0x0F47C0DE - 55;
3939

4040
} // namespace SourceMod
4141

bridge/include/LogicProvider.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class IExtensionSys;
5151
class ITextParsers;
5252
class ILogger;
5353
class IDataPack;
54+
class ICellArray;
5455

5556
struct sm_logic_t
5657
{
@@ -71,6 +72,8 @@ struct sm_logic_t
7172
void (*RegisterProfiler)(IProfilingTool *tool);
7273
IDataPack * (*CreateDataPack)();
7374
void (*FreeDataPack)(IDataPack *pack);
75+
ICellArray * (*CreateCellArray)(size_t blocksize);
76+
void (*FreeCellArray)(ICellArray *arr);
7477
IScriptManager *scripts;
7578
IShareSys *sharesys;
7679
IExtensionSys *extsys;

core/logic/CellArray.h

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@
3434

3535
#include <stdlib.h>
3636
#include <string.h>
37+
#include <ICellArray.h>
3738

3839
extern HandleType_t htCellArray;
3940

40-
class CellArray
41+
class CellArray : public ICellArray
4142
{
4243
public:
4344
CellArray(size_t blocksize) : m_Data(NULL), m_BlockSize(blocksize), m_AllocSize(0), m_Size(0)
@@ -49,6 +50,31 @@ class CellArray
4950
free(m_Data);
5051
}
5152

53+
/**
54+
* @brief Creates a cell array object.
55+
*
56+
* @param blocksize The number of cells each member of the array can
57+
* hold. For example, 32 cells is equivalent to:
58+
* new Array[X][32]
59+
* @return A new ICellArray object.
60+
*/
61+
static ICellArray *New(size_t blocksize)
62+
{
63+
return new CellArray(blocksize);
64+
}
65+
66+
/**
67+
* @brief Releases a cell array's resources.
68+
*
69+
* @param pack An ICellArray object to release.
70+
*/
71+
static void Free(ICellArray *arr)
72+
{
73+
delete arr;
74+
}
75+
76+
// ICellArray
77+
public:
5278
size_t size() const
5379
{
5480
return m_Size;
@@ -154,7 +180,7 @@ class CellArray
154180
return true;
155181
}
156182

157-
CellArray *clone()
183+
ICellArray *clone()
158184
{
159185
CellArray *array = new CellArray(m_BlockSize);
160186
array->m_AllocSize = m_AllocSize;

core/logic/common_logic.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
#include "LibrarySys.h"
5757
#include "RootConsoleMenu.h"
5858
#include "CDataPack.h"
59+
#include "CellArray.h"
5960
#include <bridge/include/BridgeAPI.h>
6061
#include <bridge/include/IProviderCallbacks.h>
6162

@@ -147,6 +148,8 @@ static sm_logic_t logic =
147148
RegisterProfiler,
148149
CDataPack::New,
149150
CDataPack::Free,
151+
CellArray::New,
152+
CellArray::Free,
150153
&g_PluginSys,
151154
&g_ShareSys,
152155
&g_Extensions,

core/logic/smn_adt_array.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ static cell_t CloneArray(IPluginContext *pContext, const cell_t *params)
502502
return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
503503
}
504504

505-
CellArray *array = oldArray->clone();
505+
ICellArray *array = oldArray->clone();
506506

507507
Handle_t hndl = handlesys->CreateHandle(htCellArray, array, pContext->GetIdentity(), g_pCoreIdent, NULL);
508508
if (!hndl)

core/logic/smn_maplists.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ class MapLists : public SMGlobalClass, public ITextListener_SMC
321321
{
322322
return strcmp((char *)str1, (char *)str2);
323323
}
324-
CellArray *UpdateMapList(CellArray *pUseArray, const char *name, int *pSerial, unsigned int flags)
324+
ICellArray *UpdateMapList(ICellArray *pUseArray, const char *name, int *pSerial, unsigned int flags)
325325
{
326326
int change_serial;
327327
CellArray *pNewArray = NULL;
@@ -594,7 +594,7 @@ static cell_t LoadMapList(IPluginContext *pContext, const cell_t *params)
594594
char *str;
595595
Handle_t hndl;
596596
cell_t *addr, flags;
597-
CellArray *pArray, *pNewArray;
597+
ICellArray *pArray, *pNewArray;
598598

599599
hndl = params[1];
600600
pContext->LocalToPhysAddr(params[2], &addr);

public/ICellArray.h

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/**
2+
* vim: set ts=4 :
3+
* =============================================================================
4+
* SourceMod
5+
* Copyright (C) 2016 AlliedModders LLC. All rights reserved.
6+
* =============================================================================
7+
*
8+
* This program is free software; you can redistribute it and/or modify it under
9+
* the terms of the GNU General Public License, version 3.0, as published by the
10+
* Free Software Foundation.
11+
*
12+
* This program is distributed in the hope that it will be useful, but WITHOUT
13+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14+
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15+
* details.
16+
*
17+
* You should have received a copy of the GNU General Public License along with
18+
* this program. If not, see <http://www.gnu.org/licenses/>.
19+
*
20+
* As a special exception, AlliedModders LLC gives you permission to link the
21+
* code of this program (as well as its derivative works) to "Half-Life 2," the
22+
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23+
* by the Valve Corporation. You must obey the GNU General Public License in
24+
* all respects for all other code used. Additionally, AlliedModders LLC grants
25+
* this exception to all derivative works. AlliedModders LLC defines further
26+
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27+
* or <http://www.sourcemod.net/license.php>.
28+
*
29+
* Version: $Id$
30+
*/
31+
32+
#ifndef _INCLUDE_SOURCEMOD_INTERFACE_CELLARRAY_H_
33+
#define _INCLUDE_SOURCEMOD_INTERFACE_CELLARRAY_H_
34+
35+
/**
36+
* @file ICellArray.h
37+
* @brief Contains functions for dynamic arrays in plugins. The wrappers
38+
* for creating these are contained in ISourceMod.h
39+
*/
40+
41+
namespace SourceMod
42+
{
43+
/**
44+
* @brief Specifies a dynamic array data structure used in plugins.
45+
*/
46+
class ICellArray
47+
{
48+
public:
49+
/**
50+
* @brief Retrieve the size of the array.
51+
*
52+
* @return The size of the array.
53+
*/
54+
virtual size_t size() const = 0;
55+
56+
/**
57+
* @brief Increases the size of the array by one and returns
58+
* a pointer to the newly added item at the end of the array.
59+
*
60+
* @return A pointer to the new item added at the end
61+
* or NULL if growing the array failed.
62+
*/
63+
virtual cell_t *push() = 0;
64+
65+
/**
66+
* @brief Retrieve a pointer to the memory at a given index.
67+
*
68+
* @return A pointer to the memory for item at given index.
69+
*/
70+
virtual cell_t *at(size_t index) const = 0;
71+
72+
/**
73+
* @brief Retrieve the block size set while creating the CellArray.
74+
* It determines how many cells each array slot has.
75+
*
76+
* @return The block size of the array.
77+
*/
78+
virtual size_t blocksize() const = 0;
79+
80+
/**
81+
* @brief Clears an array of all entries.
82+
* This is the same as Resize(0).
83+
*/
84+
virtual void clear() = 0;
85+
86+
/**
87+
* @brief Swaps two items in the array.
88+
*
89+
* @param item1 First index.
90+
* @param item2 Second index.
91+
* @return True if items were swapped, false otherwise.
92+
*/
93+
virtual bool swap(size_t item1, size_t item2) = 0;
94+
95+
/**
96+
* @brief Removes an array index, shifting the entire array down from that position
97+
* on. For example, if item 8 of 10 is removed, the last 3 items will then be
98+
* (6,7,8) instead of (7,8,9), and all indexes before 8 will remain unchanged.
99+
*
100+
* @param index Index in the array to remove at.
101+
*/
102+
virtual void remove(size_t index) = 0;
103+
104+
/**
105+
* @brief Shifts items at the given index and following up by one
106+
* to make space for a new item at the index.
107+
*
108+
* @param index Index in the array to insert at.
109+
* @return Pointer to item space at the given index or NULL if shifting failed.
110+
*/
111+
virtual cell_t *insert_at(size_t index) = 0;
112+
113+
/**
114+
* @brief Resizes an array. If the size is smaller than the current size, the
115+
* array is truncated.
116+
*
117+
* @param newsize New size
118+
* @return True if resized, false otherwise.
119+
*/
120+
virtual bool resize(size_t newsize) = 0;
121+
122+
/**
123+
* @brief Clones an array, returning a new object
124+
* with the same size and data.
125+
*
126+
* @return Pointer to cloned array instance.
127+
*/
128+
virtual ICellArray *clone() = 0;
129+
130+
/**
131+
* @brief Retrieve a pointer to the array base.
132+
*
133+
* @return Pointer to the array base.
134+
*/
135+
virtual cell_t *base() = 0;
136+
137+
/**
138+
* @brief Retrieve the amount of memory used by this array in bytes.
139+
*
140+
* @return Amount of memory used in bytes.
141+
*/
142+
virtual size_t mem_usage() = 0;
143+
};
144+
}
145+
146+
#endif //_INCLUDE_SOURCEMOD_INTERFACE_CELLARRAY_H_

0 commit comments

Comments
 (0)