Preserve old DataPack behavior when overwriting data (#848)
* Match old DataPack behavior when overwriting data * Make RemoveItem more flexible * Ditch implied RemoveItem behavior & asher fixes * KyleS nits - but fixed before he says them * Add back implicit behavior * Update CDataPack.cpp Committing to the spec. * Update CDataPack.h * fixup removing last item if explicitly requested * Fix logic for accepting pack pos, rather than index * Fixup IsReadable * headache is over now
This commit is contained in:
parent
e4862dade8
commit
32d12ea4a6
@ -223,3 +223,24 @@ void *CDataPack::ReadMemory(size_t *size) const
|
|||||||
|
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool CDataPack::RemoveItem(size_t pos)
|
||||||
|
{
|
||||||
|
if (!elements.length())
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pos == static_cast<size_t>(-1))
|
||||||
|
{
|
||||||
|
pos = position;
|
||||||
|
}
|
||||||
|
if (pos >= elements.length())
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
elements.remove(pos);
|
||||||
|
--position;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
@ -76,6 +76,7 @@ public:
|
|||||||
void Initialize();
|
void Initialize();
|
||||||
inline size_t GetCapacity() const { return this->elements.length(); };
|
inline size_t GetCapacity() const { return this->elements.length(); };
|
||||||
inline CDataPackType GetCurrentType(void) const { return this->elements[this->position].type; };
|
inline CDataPackType GetCurrentType(void) const { return this->elements[this->position].type; };
|
||||||
|
bool RemoveItem(size_t pos = -1);
|
||||||
private:
|
private:
|
||||||
|
|
||||||
typedef union {
|
typedef union {
|
||||||
|
@ -99,6 +99,12 @@ static cell_t smn_WritePackCell(IPluginContext *pContext, const cell_t *params)
|
|||||||
return pContext->ThrowNativeError("Invalid data pack handle %x (error %d).", hndl, herr);
|
return pContext->ThrowNativeError("Invalid data pack handle %x (error %d).", hndl, herr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool insert = (params[0] >= 3) ? params[3] : false;
|
||||||
|
if (!insert)
|
||||||
|
{
|
||||||
|
pDataPack->RemoveItem();
|
||||||
|
}
|
||||||
|
|
||||||
pDataPack->PackCell(params[2]);
|
pDataPack->PackCell(params[2]);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
@ -120,6 +126,12 @@ static cell_t smn_WritePackFloat(IPluginContext *pContext, const cell_t *params)
|
|||||||
return pContext->ThrowNativeError("Invalid data pack handle %x (error %d).", hndl, herr);
|
return pContext->ThrowNativeError("Invalid data pack handle %x (error %d).", hndl, herr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool insert = (params[0] >= 3) ? params[3] : false;
|
||||||
|
if (!insert)
|
||||||
|
{
|
||||||
|
pDataPack->RemoveItem();
|
||||||
|
}
|
||||||
|
|
||||||
pDataPack->PackFloat(sp_ctof(params[2]));
|
pDataPack->PackFloat(sp_ctof(params[2]));
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
@ -141,6 +153,12 @@ static cell_t smn_WritePackString(IPluginContext *pContext, const cell_t *params
|
|||||||
return pContext->ThrowNativeError("Invalid data pack handle %x (error %d).", hndl, herr);
|
return pContext->ThrowNativeError("Invalid data pack handle %x (error %d).", hndl, herr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool insert = (params[0] >= 3) ? params[3] : false;
|
||||||
|
if (!insert)
|
||||||
|
{
|
||||||
|
pDataPack->RemoveItem();
|
||||||
|
}
|
||||||
|
|
||||||
char *str;
|
char *str;
|
||||||
pContext->LocalToString(params[2], &str);
|
pContext->LocalToString(params[2], &str);
|
||||||
pDataPack->PackString(str);
|
pDataPack->PackString(str);
|
||||||
@ -164,6 +182,12 @@ static cell_t smn_WritePackFunction(IPluginContext *pContext, const cell_t *para
|
|||||||
return pContext->ThrowNativeError("Invalid data pack handle %x (error %d).", hndl, herr);
|
return pContext->ThrowNativeError("Invalid data pack handle %x (error %d).", hndl, herr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool insert = (params[0] >= 3) ? params[3] : false;
|
||||||
|
if (!insert)
|
||||||
|
{
|
||||||
|
pDataPack->RemoveItem();
|
||||||
|
}
|
||||||
|
|
||||||
pDataPack->PackFunction(params[2]);
|
pDataPack->PackFunction(params[2]);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -50,22 +50,26 @@ methodmap DataPack < Handle
|
|||||||
// Packs a normal cell into a data pack.
|
// Packs a normal cell into a data pack.
|
||||||
//
|
//
|
||||||
// @param cell Cell to add.
|
// @param cell Cell to add.
|
||||||
public native void WriteCell(any cell);
|
// @param insert Determines whether mid-pack writes will insert instead of overwrite.
|
||||||
|
public native void WriteCell(any cell, bool insert = false);
|
||||||
|
|
||||||
// Packs a float into a data pack.
|
// Packs a float into a data pack.
|
||||||
//
|
//
|
||||||
// @param val Float to add.
|
// @param val Float to add.
|
||||||
public native void WriteFloat(float val);
|
// @param insert Determines whether mid-pack writes will insert instead of overwrite.
|
||||||
|
public native void WriteFloat(float val, bool insert = false);
|
||||||
|
|
||||||
// Packs a string into a data pack.
|
// Packs a string into a data pack.
|
||||||
//
|
//
|
||||||
// @param str String to add.
|
// @param str String to add.
|
||||||
public native void WriteString(const char[] str);
|
// @param insert Determines whether mid-pack writes will insert instead of overwrite.
|
||||||
|
public native void WriteString(const char[] str, bool insert = false);
|
||||||
|
|
||||||
// Packs a function pointer into a data pack.
|
// Packs a function pointer into a data pack.
|
||||||
//
|
//
|
||||||
// @param fktptr Function pointer to add.
|
// @param fktptr Function pointer to add.
|
||||||
public native void WriteFunction(Function fktptr);
|
// @param insert Determines whether mid-pack writes will insert instead of overwrite.
|
||||||
|
public native void WriteFunction(Function fktptr, bool insert = false);
|
||||||
|
|
||||||
// Reads a cell from a data pack.
|
// Reads a cell from a data pack.
|
||||||
//
|
//
|
||||||
@ -96,8 +100,8 @@ methodmap DataPack < Handle
|
|||||||
// Returns whether or not a specified number of bytes from the data pack
|
// Returns whether or not a specified number of bytes from the data pack
|
||||||
// position to the end can be read.
|
// position to the end can be read.
|
||||||
//
|
//
|
||||||
// @param bytes Number of bytes to simulate reading.
|
// @param unused Unused variable. Exists for backwards compatability.
|
||||||
public native bool IsReadable(int bytes);
|
public native bool IsReadable(int unused = 0);
|
||||||
|
|
||||||
// The read or write position in a data pack.
|
// The read or write position in a data pack.
|
||||||
property DataPackPos Position {
|
property DataPackPos Position {
|
||||||
|
Loading…
Reference in New Issue
Block a user