2008-03-30 09:00:22 +02:00
|
|
|
/**
|
2015-08-26 06:35:45 +02:00
|
|
|
* vim: set ts=4 sw=4 tw=99 noet :
|
2008-03-30 09:00:22 +02:00
|
|
|
* =============================================================================
|
|
|
|
* 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.
|
2014-12-06 10:21:53 +01:00
|
|
|
*
|
2008-03-30 09:00:22 +02:00
|
|
|
* 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>
|
2009-05-14 03:55:50 +02:00
|
|
|
#include "common_logic.h"
|
2008-03-30 09:00:22 +02:00
|
|
|
#include "CellArray.h"
|
2015-08-26 06:35:45 +02:00
|
|
|
#include "stringutil.h"
|
2015-08-31 06:27:32 +02:00
|
|
|
#include <IHandleSys.h>
|
2008-03-30 09:00:22 +02:00
|
|
|
|
|
|
|
HandleType_t htCellArray;
|
|
|
|
|
2014-12-06 10:21:53 +01:00
|
|
|
class CellArrayHelpers :
|
2008-03-30 09:00:22 +02:00
|
|
|
public SMGlobalClass,
|
|
|
|
public IHandleTypeDispatch
|
|
|
|
{
|
|
|
|
public: //SMGlobalClass
|
|
|
|
void OnSourceModAllInitialized()
|
|
|
|
{
|
2009-05-14 03:55:50 +02:00
|
|
|
htCellArray = handlesys->CreateType("CellArray", this, 0, NULL, NULL, g_pCoreIdent, NULL);
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
|
|
|
void OnSourceModShutdown()
|
|
|
|
{
|
2009-05-14 03:55:50 +02:00
|
|
|
handlesys->RemoveType(htCellArray, g_pCoreIdent);
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
|
|
|
public: //IHandleTypeDispatch
|
|
|
|
void OnHandleDestroy(HandleType_t type, void *object)
|
|
|
|
{
|
|
|
|
CellArray *array = (CellArray *)object;
|
|
|
|
delete array;
|
|
|
|
}
|
|
|
|
bool GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize)
|
|
|
|
{
|
|
|
|
CellArray *pArray = (CellArray *)object;
|
|
|
|
*pSize = sizeof(CellArray) + pArray->mem_usage();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} s_CellArrayHelpers;
|
|
|
|
|
|
|
|
static cell_t CreateArray(IPluginContext *pContext, const cell_t *params)
|
|
|
|
{
|
|
|
|
if (!params[1])
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Invalid block size (must be > 0)");
|
|
|
|
}
|
|
|
|
|
|
|
|
CellArray *array = new CellArray(params[1]);
|
|
|
|
|
|
|
|
if (params[2])
|
|
|
|
{
|
|
|
|
array->resize(params[2]);
|
|
|
|
}
|
2014-12-06 10:21:53 +01:00
|
|
|
|
2009-05-14 03:55:50 +02:00
|
|
|
Handle_t hndl = handlesys->CreateHandle(htCellArray, array, pContext->GetIdentity(), g_pCoreIdent, NULL);
|
2008-03-30 09:00:22 +02:00
|
|
|
if (!hndl)
|
|
|
|
{
|
|
|
|
delete array;
|
|
|
|
}
|
|
|
|
|
|
|
|
return hndl;
|
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t ClearArray(IPluginContext *pContext, const cell_t *params)
|
|
|
|
{
|
|
|
|
CellArray *array;
|
|
|
|
HandleError err;
|
|
|
|
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
|
|
|
|
|
2014-12-06 10:21:53 +01:00
|
|
|
if ((err = handlesys->ReadHandle(params[1], htCellArray, &sec, (void **)&array))
|
2008-03-30 09:00:22 +02:00
|
|
|
!= HandleError_None)
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
|
|
|
|
}
|
|
|
|
|
|
|
|
array->clear();
|
2014-12-06 10:21:53 +01:00
|
|
|
|
2008-03-30 09:00:22 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t ResizeArray(IPluginContext *pContext, const cell_t *params)
|
|
|
|
{
|
|
|
|
CellArray *array;
|
|
|
|
HandleError err;
|
|
|
|
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
|
|
|
|
|
2014-12-06 10:21:53 +01:00
|
|
|
if ((err = handlesys->ReadHandle(params[1], htCellArray, &sec, (void **)&array))
|
2008-03-30 09:00:22 +02:00
|
|
|
!= HandleError_None)
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!array->resize(params[2]))
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Unable to resize array to \"%u\"", params[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t GetArraySize(IPluginContext *pContext, const cell_t *params)
|
|
|
|
{
|
|
|
|
CellArray *array;
|
|
|
|
HandleError err;
|
|
|
|
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
|
|
|
|
|
2014-12-06 10:21:53 +01:00
|
|
|
if ((err = handlesys->ReadHandle(params[1], htCellArray, &sec, (void **)&array))
|
2008-03-30 09:00:22 +02:00
|
|
|
!= HandleError_None)
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (cell_t)array->size();
|
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t PushArrayCell(IPluginContext *pContext, const cell_t *params)
|
|
|
|
{
|
|
|
|
CellArray *array;
|
|
|
|
HandleError err;
|
|
|
|
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
|
|
|
|
|
2014-12-06 10:21:53 +01:00
|
|
|
if ((err = handlesys->ReadHandle(params[1], htCellArray, &sec, (void **)&array))
|
2008-03-30 09:00:22 +02:00
|
|
|
!= HandleError_None)
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
|
|
|
|
}
|
|
|
|
|
|
|
|
cell_t *blk = array->push();
|
|
|
|
if (!blk)
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Failed to grow array");
|
|
|
|
}
|
|
|
|
|
|
|
|
*blk = params[2];
|
|
|
|
|
|
|
|
return (cell_t)(array->size() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t PushArrayString(IPluginContext *pContext, const cell_t *params)
|
|
|
|
{
|
|
|
|
CellArray *array;
|
|
|
|
HandleError err;
|
|
|
|
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
|
|
|
|
|
2014-12-06 10:21:53 +01:00
|
|
|
if ((err = handlesys->ReadHandle(params[1], htCellArray, &sec, (void **)&array))
|
2008-03-30 09:00:22 +02:00
|
|
|
!= HandleError_None)
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
|
|
|
|
}
|
|
|
|
|
|
|
|
cell_t *blk = array->push();
|
|
|
|
if (!blk)
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Failed to grow array");
|
|
|
|
}
|
|
|
|
|
|
|
|
char *str;
|
|
|
|
pContext->LocalToString(params[2], &str);
|
|
|
|
|
2015-08-26 06:35:45 +02:00
|
|
|
strncopy((char *)blk, str, array->blocksize() * sizeof(cell_t));
|
2008-03-30 09:00:22 +02:00
|
|
|
|
|
|
|
return (cell_t)(array->size() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t PushArrayArray(IPluginContext *pContext, const cell_t *params)
|
|
|
|
{
|
|
|
|
CellArray *array;
|
|
|
|
HandleError err;
|
|
|
|
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
|
|
|
|
|
2014-12-06 10:21:53 +01:00
|
|
|
if ((err = handlesys->ReadHandle(params[1], htCellArray, &sec, (void **)&array))
|
2008-03-30 09:00:22 +02:00
|
|
|
!= HandleError_None)
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
|
|
|
|
}
|
|
|
|
|
|
|
|
cell_t *blk = array->push();
|
|
|
|
if (!blk)
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Failed to grow array");
|
|
|
|
}
|
|
|
|
|
|
|
|
cell_t *addr;
|
|
|
|
pContext->LocalToPhysAddr(params[2], &addr);
|
|
|
|
|
|
|
|
size_t indexes = array->blocksize();
|
|
|
|
if (params[3] != -1 && (size_t)params[3] <= array->blocksize())
|
|
|
|
{
|
|
|
|
indexes = params[3];
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(blk, addr, sizeof(cell_t) * indexes);
|
|
|
|
|
|
|
|
return (cell_t)(array->size() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t GetArrayCell(IPluginContext *pContext, const cell_t *params)
|
|
|
|
{
|
|
|
|
CellArray *array;
|
|
|
|
HandleError err;
|
|
|
|
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
|
|
|
|
|
2014-12-06 10:21:53 +01:00
|
|
|
if ((err = handlesys->ReadHandle(params[1], htCellArray, &sec, (void **)&array))
|
2008-03-30 09:00:22 +02:00
|
|
|
!= HandleError_None)
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t idx = (size_t)params[2];
|
|
|
|
if (idx >= array->size())
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx, array->size());
|
|
|
|
}
|
|
|
|
|
|
|
|
cell_t *blk = array->at(idx);
|
|
|
|
|
|
|
|
idx = (size_t)params[3];
|
|
|
|
if (params[4] == 0)
|
|
|
|
{
|
|
|
|
if (idx >= array->blocksize())
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Invalid block %d (blocksize: %d)", idx, array->blocksize());
|
|
|
|
}
|
|
|
|
return blk[idx];
|
|
|
|
} else {
|
|
|
|
if (idx >= array->blocksize() * 4)
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Invalid byte %d (blocksize: %d bytes)", idx, array->blocksize() * 4);
|
|
|
|
}
|
|
|
|
return (cell_t)*((char *)blk + idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t GetArrayString(IPluginContext *pContext, const cell_t *params)
|
|
|
|
{
|
|
|
|
CellArray *array;
|
|
|
|
HandleError err;
|
|
|
|
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
|
|
|
|
|
2014-12-06 10:21:53 +01:00
|
|
|
if ((err = handlesys->ReadHandle(params[1], htCellArray, &sec, (void **)&array))
|
2008-03-30 09:00:22 +02:00
|
|
|
!= HandleError_None)
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t idx = (size_t)params[2];
|
|
|
|
if (idx >= array->size())
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx, array->size());
|
|
|
|
}
|
|
|
|
|
|
|
|
cell_t *blk = array->at(idx);
|
|
|
|
size_t numWritten = 0;
|
|
|
|
|
|
|
|
pContext->StringToLocalUTF8(params[3], params[4], (char *)blk, &numWritten);
|
|
|
|
|
|
|
|
return numWritten;
|
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t GetArrayArray(IPluginContext *pContext, const cell_t *params)
|
|
|
|
{
|
|
|
|
CellArray *array;
|
|
|
|
HandleError err;
|
|
|
|
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
|
|
|
|
|
2014-12-06 10:21:53 +01:00
|
|
|
if ((err = handlesys->ReadHandle(params[1], htCellArray, &sec, (void **)&array))
|
2008-03-30 09:00:22 +02:00
|
|
|
!= HandleError_None)
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t idx = (size_t)params[2];
|
|
|
|
if (idx >= array->size())
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx, array->size());
|
|
|
|
}
|
|
|
|
|
|
|
|
cell_t *blk = array->at(idx);
|
|
|
|
size_t indexes = array->blocksize();
|
|
|
|
if (params[4] != -1 && (size_t)params[4] <= array->blocksize())
|
|
|
|
{
|
|
|
|
indexes = params[4];
|
|
|
|
}
|
|
|
|
|
|
|
|
cell_t *addr;
|
|
|
|
pContext->LocalToPhysAddr(params[3], &addr);
|
|
|
|
|
|
|
|
memcpy(addr, blk, sizeof(cell_t) * indexes);
|
|
|
|
|
|
|
|
return indexes;
|
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t SetArrayCell(IPluginContext *pContext, const cell_t *params)
|
|
|
|
{
|
|
|
|
CellArray *array;
|
|
|
|
HandleError err;
|
|
|
|
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
|
|
|
|
|
2014-12-06 10:21:53 +01:00
|
|
|
if ((err = handlesys->ReadHandle(params[1], htCellArray, &sec, (void **)&array))
|
2008-03-30 09:00:22 +02:00
|
|
|
!= HandleError_None)
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t idx = (size_t)params[2];
|
|
|
|
if (idx >= array->size())
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx, array->size());
|
|
|
|
}
|
|
|
|
|
|
|
|
cell_t *blk = array->at(idx);
|
|
|
|
|
|
|
|
idx = (size_t)params[4];
|
|
|
|
if (params[5] == 0)
|
|
|
|
{
|
|
|
|
if (idx >= array->blocksize())
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Invalid block %d (blocksize: %d)", idx, array->blocksize());
|
|
|
|
}
|
|
|
|
blk[idx] = params[3];
|
|
|
|
} else {
|
|
|
|
if (idx >= array->blocksize() * 4)
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Invalid byte %d (blocksize: %d bytes)", idx, array->blocksize() * 4);
|
|
|
|
}
|
|
|
|
*((char *)blk + idx) = (char)params[3];
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t SetArrayString(IPluginContext *pContext, const cell_t *params)
|
|
|
|
{
|
|
|
|
CellArray *array;
|
|
|
|
HandleError err;
|
|
|
|
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
|
|
|
|
|
2014-12-06 10:21:53 +01:00
|
|
|
if ((err = handlesys->ReadHandle(params[1], htCellArray, &sec, (void **)&array))
|
2008-03-30 09:00:22 +02:00
|
|
|
!= HandleError_None)
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t idx = (size_t)params[2];
|
|
|
|
if (idx >= array->size())
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx, array->size());
|
|
|
|
}
|
|
|
|
|
|
|
|
cell_t *blk = array->at(idx);
|
|
|
|
|
|
|
|
char *str;
|
|
|
|
pContext->LocalToString(params[3], &str);
|
|
|
|
|
2015-08-26 06:35:45 +02:00
|
|
|
return strncopy((char *)blk, str, array->blocksize() * sizeof(cell_t));
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t SetArrayArray(IPluginContext *pContext, const cell_t *params)
|
|
|
|
{
|
|
|
|
CellArray *array;
|
|
|
|
HandleError err;
|
|
|
|
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
|
|
|
|
|
2014-12-06 10:21:53 +01:00
|
|
|
if ((err = handlesys->ReadHandle(params[1], htCellArray, &sec, (void **)&array))
|
2008-03-30 09:00:22 +02:00
|
|
|
!= HandleError_None)
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t idx = (size_t)params[2];
|
|
|
|
if (idx >= array->size())
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx, array->size());
|
|
|
|
}
|
|
|
|
|
|
|
|
cell_t *blk = array->at(idx);
|
|
|
|
size_t indexes = array->blocksize();
|
|
|
|
if (params[4] != -1 && (size_t)params[4] <= array->blocksize())
|
|
|
|
{
|
|
|
|
indexes = params[4];
|
|
|
|
}
|
|
|
|
|
|
|
|
cell_t *addr;
|
|
|
|
pContext->LocalToPhysAddr(params[3], &addr);
|
|
|
|
|
|
|
|
memcpy(blk, addr, sizeof(cell_t) * indexes);
|
|
|
|
|
|
|
|
return indexes;
|
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t ShiftArrayUp(IPluginContext *pContext, const cell_t *params)
|
|
|
|
{
|
|
|
|
CellArray *array;
|
|
|
|
HandleError err;
|
|
|
|
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
|
|
|
|
|
2014-12-06 10:21:53 +01:00
|
|
|
if ((err = handlesys->ReadHandle(params[1], htCellArray, &sec, (void **)&array))
|
2008-03-30 09:00:22 +02:00
|
|
|
!= HandleError_None)
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t idx = (size_t)params[2];
|
|
|
|
if (idx >= array->size())
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx, array->size());
|
|
|
|
}
|
|
|
|
|
|
|
|
array->insert_at(idx);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t RemoveFromArray(IPluginContext *pContext, const cell_t *params)
|
|
|
|
{
|
|
|
|
CellArray *array;
|
|
|
|
HandleError err;
|
|
|
|
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
|
|
|
|
|
2014-12-06 10:21:53 +01:00
|
|
|
if ((err = handlesys->ReadHandle(params[1], htCellArray, &sec, (void **)&array))
|
2008-03-30 09:00:22 +02:00
|
|
|
!= HandleError_None)
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t idx = (size_t)params[2];
|
|
|
|
if (idx >= array->size())
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx, array->size());
|
|
|
|
}
|
|
|
|
|
|
|
|
array->remove(idx);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t SwapArrayItems(IPluginContext *pContext, const cell_t *params)
|
|
|
|
{
|
|
|
|
CellArray *array;
|
|
|
|
HandleError err;
|
|
|
|
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
|
|
|
|
|
2014-12-06 10:21:53 +01:00
|
|
|
if ((err = handlesys->ReadHandle(params[1], htCellArray, &sec, (void **)&array))
|
2008-03-30 09:00:22 +02:00
|
|
|
!= HandleError_None)
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t idx1 = (size_t)params[2];
|
|
|
|
size_t idx2 = (size_t)params[3];
|
|
|
|
if (idx1 >= array->size())
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx1, array->size());
|
|
|
|
}
|
|
|
|
if (idx2 >= array->size())
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Invalid index %d (count: %d)", idx2, array->size());
|
|
|
|
}
|
|
|
|
|
|
|
|
array->swap(idx1, idx2);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t CloneArray(IPluginContext *pContext, const cell_t *params)
|
|
|
|
{
|
|
|
|
CellArray *oldArray;
|
|
|
|
HandleError err;
|
|
|
|
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
|
|
|
|
|
2014-12-06 10:21:53 +01:00
|
|
|
if ((err = handlesys->ReadHandle(params[1], htCellArray, &sec, (void **)&oldArray))
|
2008-03-30 09:00:22 +02:00
|
|
|
!= HandleError_None)
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
|
|
|
|
}
|
|
|
|
|
2016-08-21 21:32:23 +02:00
|
|
|
ICellArray *array = oldArray->clone();
|
2016-12-12 05:55:11 +01:00
|
|
|
if (!array)
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Failed to clone array. Out of memory.");
|
|
|
|
}
|
2014-12-06 10:21:53 +01:00
|
|
|
|
2009-05-14 03:55:50 +02:00
|
|
|
Handle_t hndl = handlesys->CreateHandle(htCellArray, array, pContext->GetIdentity(), g_pCoreIdent, NULL);
|
2008-03-30 09:00:22 +02:00
|
|
|
if (!hndl)
|
|
|
|
{
|
|
|
|
delete array;
|
|
|
|
}
|
|
|
|
|
|
|
|
return hndl;
|
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t FindStringInArray(IPluginContext *pContext, const cell_t *params)
|
|
|
|
{
|
|
|
|
CellArray *array;
|
|
|
|
HandleError err;
|
|
|
|
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
|
|
|
|
|
2014-12-06 10:21:53 +01:00
|
|
|
if ((err = handlesys->ReadHandle(params[1], htCellArray, &sec, (void **)&array))
|
2008-03-30 09:00:22 +02:00
|
|
|
!= HandleError_None)
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *str;
|
|
|
|
pContext->LocalToString(params[2], &str);
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < array->size(); i++)
|
|
|
|
{
|
|
|
|
const char *array_str = (const char *)array->at(i);
|
|
|
|
if (strcmp(str, array_str) == 0)
|
|
|
|
{
|
|
|
|
return (cell_t) i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t FindValueInArray(IPluginContext *pContext, const cell_t *params)
|
|
|
|
{
|
|
|
|
CellArray *array;
|
|
|
|
HandleError err;
|
|
|
|
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
|
|
|
|
|
2014-12-06 10:21:53 +01:00
|
|
|
if ((err = handlesys->ReadHandle(params[1], htCellArray, &sec, (void **)&array))
|
2008-03-30 09:00:22 +02:00
|
|
|
!= HandleError_None)
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
|
|
|
|
}
|
|
|
|
|
2014-12-07 00:07:54 +01:00
|
|
|
// the blocknumber is not guaranteed to always be passed
|
|
|
|
size_t blocknumber = 0;
|
|
|
|
if (params[0] >= 3)
|
|
|
|
{
|
|
|
|
blocknumber = (size_t) params[3];
|
|
|
|
}
|
|
|
|
|
2014-12-06 10:21:53 +01:00
|
|
|
if (blocknumber >= array->blocksize())
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Invalid block %d (blocksize: %d)", blocknumber, array->blocksize());
|
|
|
|
}
|
|
|
|
|
2008-03-30 09:00:22 +02:00
|
|
|
for (unsigned int i = 0; i < array->size(); i++)
|
|
|
|
{
|
2014-12-06 10:21:53 +01:00
|
|
|
cell_t *blk = array->at(i);
|
|
|
|
if (params[2] == blk[blocknumber])
|
2008-03-30 09:00:22 +02:00
|
|
|
{
|
|
|
|
return (cell_t) i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-02-09 20:26:14 +01:00
|
|
|
static cell_t GetArrayBlockSize(IPluginContext *pContext, const cell_t *params)
|
|
|
|
{
|
|
|
|
CellArray *array;
|
|
|
|
HandleError err;
|
|
|
|
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
|
|
|
|
|
|
|
|
if ((err = handlesys->ReadHandle(params[1], htCellArray, &sec, (void **)&array))
|
|
|
|
!= HandleError_None)
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return array->blocksize();
|
|
|
|
}
|
|
|
|
|
2008-03-30 09:00:22 +02:00
|
|
|
REGISTER_NATIVES(cellArrayNatives)
|
|
|
|
{
|
|
|
|
{"ClearArray", ClearArray},
|
|
|
|
{"CreateArray", CreateArray},
|
|
|
|
{"GetArrayArray", GetArrayArray},
|
|
|
|
{"GetArrayCell", GetArrayCell},
|
|
|
|
{"GetArraySize", GetArraySize},
|
|
|
|
{"GetArrayString", GetArrayString},
|
|
|
|
{"ResizeArray", ResizeArray},
|
|
|
|
{"PushArrayArray", PushArrayArray},
|
|
|
|
{"PushArrayCell", PushArrayCell},
|
|
|
|
{"PushArrayString", PushArrayString},
|
|
|
|
{"RemoveFromArray", RemoveFromArray},
|
|
|
|
{"SetArrayCell", SetArrayCell},
|
|
|
|
{"SetArrayString", SetArrayString},
|
|
|
|
{"SetArrayArray", SetArrayArray},
|
|
|
|
{"ShiftArrayUp", ShiftArrayUp},
|
|
|
|
{"SwapArrayItems", SwapArrayItems},
|
|
|
|
{"CloneArray", CloneArray},
|
|
|
|
{"FindStringInArray", FindStringInArray},
|
|
|
|
{"FindValueInArray", FindValueInArray},
|
2017-02-09 20:26:14 +01:00
|
|
|
{"GetArrayBlockSize", GetArrayBlockSize},
|
2014-11-09 01:31:33 +01:00
|
|
|
|
|
|
|
// Transitional syntax support.
|
|
|
|
{"ArrayList.ArrayList", CreateArray},
|
|
|
|
{"ArrayList.Clear", ClearArray},
|
|
|
|
{"ArrayList.Length.get", GetArraySize},
|
|
|
|
{"ArrayList.Resize", ResizeArray},
|
|
|
|
{"ArrayList.Get", GetArrayCell},
|
|
|
|
{"ArrayList.GetString", GetArrayString},
|
|
|
|
{"ArrayList.GetArray", GetArrayArray},
|
|
|
|
{"ArrayList.Push", PushArrayCell},
|
|
|
|
{"ArrayList.PushString", PushArrayString},
|
|
|
|
{"ArrayList.PushArray", PushArrayArray},
|
|
|
|
{"ArrayList.Set", SetArrayCell},
|
|
|
|
{"ArrayList.SetString", SetArrayString},
|
|
|
|
{"ArrayList.SetArray", SetArrayArray},
|
2015-01-04 17:58:44 +01:00
|
|
|
{"ArrayList.Erase", RemoveFromArray},
|
2014-11-09 01:31:33 +01:00
|
|
|
{"ArrayList.ShiftUp", ShiftArrayUp},
|
|
|
|
{"ArrayList.SwapAt", SwapArrayItems},
|
|
|
|
{"ArrayList.Clone", CloneArray},
|
|
|
|
{"ArrayList.FindString", FindStringInArray},
|
|
|
|
{"ArrayList.FindValue", FindValueInArray},
|
2017-02-09 20:26:14 +01:00
|
|
|
{"ArrayList.BlockSize.get", GetArrayBlockSize},
|
2014-11-09 01:31:33 +01:00
|
|
|
|
2008-03-30 09:00:22 +02:00
|
|
|
{NULL, NULL},
|
|
|
|
};
|