Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose ICellArray handle to extensions #2174

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions core/logic/CellArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,16 @@ class CellArray : public ICellArray
return m_AllocSize * m_BlockSize * sizeof(cell_t);
}

size_t size_of()
{
return sizeof(CellArray);
}

void delete_this()
{
delete this;
}

private:
bool GrowIfNeeded(size_t count)
{
Expand Down
14 changes: 7 additions & 7 deletions core/logic/smn_adt_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ class CellArrayHelpers :
public: //IHandleTypeDispatch
void OnHandleDestroy(HandleType_t type, void *object)
{
CellArray *array = (CellArray *)object;
delete array;
ICellArray *array = (ICellArray *)object;
array->delete_this();
}
bool GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize)
{
CellArray *pArray = (CellArray *)object;
*pSize = sizeof(CellArray) + pArray->mem_usage();
ICellArray *pArray = (ICellArray*)object;
*pSize = pArray->size_of() + pArray->mem_usage();
return true;
}
} s_CellArrayHelpers;
Expand All @@ -77,15 +77,15 @@ static cell_t CreateArray(IPluginContext *pContext, const cell_t *params)
{
if (!array->resize(params[2]))
{
delete array;
array->delete_this();
return pContext->ThrowNativeError("Failed to resize array to startsize \"%u\".", params[2]);
}
}

Handle_t hndl = handlesys->CreateHandle(htCellArray, array, pContext->GetIdentity(), g_pCoreIdent, NULL);
if (!hndl)
{
delete array;
array->delete_this();
}

return hndl;
Expand Down Expand Up @@ -569,7 +569,7 @@ static cell_t CloneArray(IPluginContext *pContext, const cell_t *params)
Handle_t hndl = handlesys->CreateHandle(htCellArray, array, pContext->GetIdentity(), g_pCoreIdent, NULL);
if (!hndl)
{
delete array;
array->delete_this();
}

return hndl;
Expand Down
36 changes: 36 additions & 0 deletions core/sourcemod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ bool sm_show_debug_spew = false;
bool sm_disable_jit = false;
int jit_metadata_flags = JIT_DEBUG_DELETE_ON_EXIT | JIT_DEBUG_PERF_BASIC;
SMGlobalClass *SMGlobalClass::head = nullptr;
HandleType_t htCellArray;

#ifdef PLATFORM_WINDOWS
ConVar sm_basepath("sm_basepath", "addons\\sourcemod", 0, "SourceMod base path (set via command line)");
Expand Down Expand Up @@ -330,6 +331,9 @@ void SourceModBase::StartSourceMod(bool late)
pBase = pBase->m_pGlobalClassNext;
}

htCellArray = 0;
logicore.handlesys->FindHandleType("CellArray", &htCellArray);

/* Add us now... */
sharesys->AddInterface(NULL, this);

Expand Down Expand Up @@ -836,6 +840,38 @@ uint32_t SourceModBase::ToPseudoAddress(void *addr)
return logicore.ToPseudoAddress(addr);
}

Handle_t SourceModBase::CreateCellArrayHandle(SourcePawn::IPluginContext *context, ICellArray* array, HandleError *err)
{
if (htCellArray == 0)
{
return 0;
}

return logicore.handlesys->CreateHandle(htCellArray, array, context->GetIdentity(), g_pCoreIdent, err);
}

ICellArray* SourceModBase::ReadCellArrayHandle(SourcePawn::IPluginContext *context, Handle_t hndl, HandleError *pErr)
{
if (htCellArray == 0)
{
return nullptr;
}

HandleSecurity security(context->GetIdentity(), g_pCoreIdent);
ICellArray* array = nullptr;

HandleError error;
if ((error = logicore.handlesys->ReadHandle(hndl, htCellArray, &security, (void**)&array)) != HandleError::HandleError_None)
{
if (pErr)
{
*pErr = error;
}
return nullptr;
}
return array;
}

class ConVarRegistrar :
public IConCommandBaseAccessor,
public SMGlobalClass
Expand Down
2 changes: 2 additions & 0 deletions core/sourcemod.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ class SourceModBase :
bool IsMapRunning();
void *FromPseudoAddress(uint32_t pseudoAddr);
uint32_t ToPseudoAddress(void *addr);
Handle_t CreateCellArrayHandle(IPluginContext *context, ICellArray* array, HandleError *err = nullptr);
ICellArray* ReadCellArrayHandle(IPluginContext *context, Handle_t hndl, HandleError *err = nullptr);
private:
void ShutdownServices();
private:
Expand Down
12 changes: 12 additions & 0 deletions public/ICellArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,18 @@ namespace SourceMod
* @return Amount of memory used in bytes.
*/
virtual size_t mem_usage() = 0;

/**
* @brief Retrieve the implementation size of this array in bytes.
*
* @return Amount of memory used in bytes.
*/
virtual size_t size_of() = 0;

/**
* @brief Delete the array object.
*/
virtual void delete_this() = 0;
};
}

Expand Down
23 changes: 23 additions & 0 deletions public/ISourceMod.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
*/

#include <IHandleSys.h>
#include <ICellArray.h>
#include <sp_vm_api.h>
#include <time.h>

Expand Down Expand Up @@ -331,6 +332,28 @@ namespace SourceMod
* @return Pseudo address, or 0 if memory address could not be converted.
*/
virtual uint32_t ToPseudoAddress(void *addr) = 0;

/**
* @brief Wraps the given ICellArray object into an handle.
*
* @param context The plugin context that should own the handle.
* @param array The cell array object to wrap into the handle.
* @param err Optional address to store a possible handle error.
*
* @return The Handle wrapping the ICellArray, or NULL otherwise.
*/
virtual Handle_t CreateCellArrayHandle(SourcePawn::IPluginContext *context, ICellArray* array, HandleError *err = nullptr) = 0;

/**
* @brief Retrieves a ICellArray pointer from a handle.
*
* @param context Plugin context owning the handle.
* @param hndl Handle from which to retrieve contents.
* @param err Optional address to store a possible handle error.
*
* @return The ICellArray object pointer, or nullptr for any error encountered.
*/
virtual ICellArray* ReadCellArrayHandle(SourcePawn::IPluginContext *context, Handle_t hndl, HandleError *err = nullptr) = 0;
};
}

Expand Down